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 | ||
44 | #define DRV_NAME "rbd" | |
45 | #define DRV_NAME_LONG "rbd (rados block device)" | |
46 | ||
47 | #define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */ | |
48 | ||
21079786 | 49 | #define RBD_MAX_MD_NAME_LEN (RBD_MAX_OBJ_NAME_LEN + sizeof(RBD_SUFFIX)) |
602adf40 YS |
50 | #define RBD_MAX_POOL_NAME_LEN 64 |
51 | #define RBD_MAX_SNAP_NAME_LEN 32 | |
52 | #define RBD_MAX_OPT_LEN 1024 | |
53 | ||
54 | #define RBD_SNAP_HEAD_NAME "-" | |
55 | ||
56 | #define DEV_NAME_LEN 32 | |
57 | ||
59c2be1e YS |
58 | #define RBD_NOTIFY_TIMEOUT_DEFAULT 10 |
59 | ||
602adf40 YS |
60 | /* |
61 | * block device image metadata (in-memory version) | |
62 | */ | |
63 | struct rbd_image_header { | |
64 | u64 image_size; | |
65 | char block_name[32]; | |
66 | __u8 obj_order; | |
67 | __u8 crypt_type; | |
68 | __u8 comp_type; | |
69 | struct rw_semaphore snap_rwsem; | |
70 | struct ceph_snap_context *snapc; | |
71 | size_t snap_names_len; | |
72 | u64 snap_seq; | |
73 | u32 total_snaps; | |
74 | ||
75 | char *snap_names; | |
76 | u64 *snap_sizes; | |
59c2be1e YS |
77 | |
78 | u64 obj_version; | |
79 | }; | |
80 | ||
81 | struct rbd_options { | |
82 | int notify_timeout; | |
602adf40 YS |
83 | }; |
84 | ||
85 | /* | |
86 | * an instance of the client. multiple devices may share a client. | |
87 | */ | |
88 | struct rbd_client { | |
89 | struct ceph_client *client; | |
59c2be1e | 90 | struct rbd_options *rbd_opts; |
602adf40 YS |
91 | struct kref kref; |
92 | struct list_head node; | |
93 | }; | |
94 | ||
1fec7093 YS |
95 | struct rbd_req_coll; |
96 | ||
602adf40 YS |
97 | /* |
98 | * a single io request | |
99 | */ | |
100 | struct rbd_request { | |
101 | struct request *rq; /* blk layer request */ | |
102 | struct bio *bio; /* cloned bio */ | |
103 | struct page **pages; /* list of used pages */ | |
104 | u64 len; | |
1fec7093 YS |
105 | int coll_index; |
106 | struct rbd_req_coll *coll; | |
107 | }; | |
108 | ||
109 | struct rbd_req_status { | |
110 | int done; | |
111 | int rc; | |
112 | u64 bytes; | |
113 | }; | |
114 | ||
115 | /* | |
116 | * a collection of requests | |
117 | */ | |
118 | struct rbd_req_coll { | |
119 | int total; | |
120 | int num_done; | |
121 | struct kref kref; | |
122 | struct rbd_req_status status[0]; | |
602adf40 YS |
123 | }; |
124 | ||
dfc5606d YS |
125 | struct rbd_snap { |
126 | struct device dev; | |
127 | const char *name; | |
128 | size_t size; | |
129 | struct list_head node; | |
130 | u64 id; | |
131 | }; | |
132 | ||
602adf40 YS |
133 | /* |
134 | * a single device | |
135 | */ | |
136 | struct rbd_device { | |
137 | int id; /* blkdev unique id */ | |
138 | ||
139 | int major; /* blkdev assigned major */ | |
140 | struct gendisk *disk; /* blkdev's gendisk and rq */ | |
141 | struct request_queue *q; | |
142 | ||
602adf40 YS |
143 | struct rbd_client *rbd_client; |
144 | ||
145 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ | |
146 | ||
147 | spinlock_t lock; /* queue lock */ | |
148 | ||
149 | struct rbd_image_header header; | |
150 | char obj[RBD_MAX_OBJ_NAME_LEN]; /* rbd image name */ | |
151 | int obj_len; | |
152 | char obj_md_name[RBD_MAX_MD_NAME_LEN]; /* hdr nm. */ | |
153 | char pool_name[RBD_MAX_POOL_NAME_LEN]; | |
154 | int poolid; | |
155 | ||
59c2be1e YS |
156 | struct ceph_osd_event *watch_event; |
157 | struct ceph_osd_request *watch_request; | |
158 | ||
602adf40 YS |
159 | char snap_name[RBD_MAX_SNAP_NAME_LEN]; |
160 | u32 cur_snap; /* index+1 of current snapshot within snap context | |
161 | 0 - for the head */ | |
162 | int read_only; | |
163 | ||
164 | struct list_head node; | |
dfc5606d YS |
165 | |
166 | /* list of snapshots */ | |
167 | struct list_head snaps; | |
168 | ||
169 | /* sysfs related */ | |
170 | struct device dev; | |
171 | }; | |
172 | ||
173 | static struct bus_type rbd_bus_type = { | |
174 | .name = "rbd", | |
602adf40 YS |
175 | }; |
176 | ||
602adf40 | 177 | static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */ |
e124a82f | 178 | |
602adf40 | 179 | static LIST_HEAD(rbd_dev_list); /* devices */ |
e124a82f AE |
180 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
181 | ||
602adf40 | 182 | static LIST_HEAD(rbd_client_list); /* clients */ |
e124a82f | 183 | static DEFINE_SPINLOCK(node_lock); /* protects client get/put */ |
602adf40 | 184 | |
dfc5606d YS |
185 | static int __rbd_init_snaps_header(struct rbd_device *rbd_dev); |
186 | static void rbd_dev_release(struct device *dev); | |
dfc5606d YS |
187 | static ssize_t rbd_snap_add(struct device *dev, |
188 | struct device_attribute *attr, | |
189 | const char *buf, | |
190 | size_t count); | |
191 | static void __rbd_remove_snap_dev(struct rbd_device *rbd_dev, | |
69932487 | 192 | struct rbd_snap *snap); |
dfc5606d YS |
193 | |
194 | ||
195 | static struct rbd_device *dev_to_rbd(struct device *dev) | |
196 | { | |
197 | return container_of(dev, struct rbd_device, dev); | |
198 | } | |
199 | ||
200 | static struct device *rbd_get_dev(struct rbd_device *rbd_dev) | |
201 | { | |
202 | return get_device(&rbd_dev->dev); | |
203 | } | |
204 | ||
205 | static void rbd_put_dev(struct rbd_device *rbd_dev) | |
206 | { | |
207 | put_device(&rbd_dev->dev); | |
208 | } | |
602adf40 | 209 | |
59c2be1e YS |
210 | static int __rbd_update_snaps(struct rbd_device *rbd_dev); |
211 | ||
602adf40 YS |
212 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
213 | { | |
214 | struct gendisk *disk = bdev->bd_disk; | |
215 | struct rbd_device *rbd_dev = disk->private_data; | |
216 | ||
dfc5606d YS |
217 | rbd_get_dev(rbd_dev); |
218 | ||
602adf40 YS |
219 | set_device_ro(bdev, rbd_dev->read_only); |
220 | ||
221 | if ((mode & FMODE_WRITE) && rbd_dev->read_only) | |
222 | return -EROFS; | |
223 | ||
224 | return 0; | |
225 | } | |
226 | ||
dfc5606d YS |
227 | static int rbd_release(struct gendisk *disk, fmode_t mode) |
228 | { | |
229 | struct rbd_device *rbd_dev = disk->private_data; | |
230 | ||
231 | rbd_put_dev(rbd_dev); | |
232 | ||
233 | return 0; | |
234 | } | |
235 | ||
602adf40 YS |
236 | static const struct block_device_operations rbd_bd_ops = { |
237 | .owner = THIS_MODULE, | |
238 | .open = rbd_open, | |
dfc5606d | 239 | .release = rbd_release, |
602adf40 YS |
240 | }; |
241 | ||
242 | /* | |
243 | * Initialize an rbd client instance. | |
244 | * We own *opt. | |
245 | */ | |
59c2be1e YS |
246 | static struct rbd_client *rbd_client_create(struct ceph_options *opt, |
247 | struct rbd_options *rbd_opts) | |
602adf40 YS |
248 | { |
249 | struct rbd_client *rbdc; | |
250 | int ret = -ENOMEM; | |
251 | ||
252 | dout("rbd_client_create\n"); | |
253 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); | |
254 | if (!rbdc) | |
255 | goto out_opt; | |
256 | ||
257 | kref_init(&rbdc->kref); | |
258 | INIT_LIST_HEAD(&rbdc->node); | |
259 | ||
6ab00d46 | 260 | rbdc->client = ceph_create_client(opt, rbdc, 0, 0); |
602adf40 YS |
261 | if (IS_ERR(rbdc->client)) |
262 | goto out_rbdc; | |
28f259b7 | 263 | opt = NULL; /* Now rbdc->client is responsible for opt */ |
602adf40 YS |
264 | |
265 | ret = ceph_open_session(rbdc->client); | |
266 | if (ret < 0) | |
267 | goto out_err; | |
268 | ||
59c2be1e YS |
269 | rbdc->rbd_opts = rbd_opts; |
270 | ||
602adf40 YS |
271 | spin_lock(&node_lock); |
272 | list_add_tail(&rbdc->node, &rbd_client_list); | |
273 | spin_unlock(&node_lock); | |
274 | ||
275 | dout("rbd_client_create created %p\n", rbdc); | |
276 | return rbdc; | |
277 | ||
278 | out_err: | |
279 | ceph_destroy_client(rbdc->client); | |
602adf40 YS |
280 | out_rbdc: |
281 | kfree(rbdc); | |
282 | out_opt: | |
28f259b7 VK |
283 | if (opt) |
284 | ceph_destroy_options(opt); | |
285 | return ERR_PTR(ret); | |
602adf40 YS |
286 | } |
287 | ||
288 | /* | |
289 | * Find a ceph client with specific addr and configuration. | |
290 | */ | |
291 | static struct rbd_client *__rbd_client_find(struct ceph_options *opt) | |
292 | { | |
293 | struct rbd_client *client_node; | |
294 | ||
295 | if (opt->flags & CEPH_OPT_NOSHARE) | |
296 | return NULL; | |
297 | ||
298 | list_for_each_entry(client_node, &rbd_client_list, node) | |
299 | if (ceph_compare_options(opt, client_node->client) == 0) | |
300 | return client_node; | |
301 | return NULL; | |
302 | } | |
303 | ||
59c2be1e YS |
304 | /* |
305 | * mount options | |
306 | */ | |
307 | enum { | |
308 | Opt_notify_timeout, | |
309 | Opt_last_int, | |
310 | /* int args above */ | |
311 | Opt_last_string, | |
312 | /* string args above */ | |
313 | }; | |
314 | ||
315 | static match_table_t rbdopt_tokens = { | |
316 | {Opt_notify_timeout, "notify_timeout=%d"}, | |
317 | /* int args above */ | |
318 | /* string args above */ | |
319 | {-1, NULL} | |
320 | }; | |
321 | ||
322 | static int parse_rbd_opts_token(char *c, void *private) | |
323 | { | |
324 | struct rbd_options *rbdopt = private; | |
325 | substring_t argstr[MAX_OPT_ARGS]; | |
326 | int token, intval, ret; | |
327 | ||
21079786 | 328 | token = match_token(c, rbdopt_tokens, argstr); |
59c2be1e YS |
329 | if (token < 0) |
330 | return -EINVAL; | |
331 | ||
332 | if (token < Opt_last_int) { | |
333 | ret = match_int(&argstr[0], &intval); | |
334 | if (ret < 0) { | |
335 | pr_err("bad mount option arg (not int) " | |
336 | "at '%s'\n", c); | |
337 | return ret; | |
338 | } | |
339 | dout("got int token %d val %d\n", token, intval); | |
340 | } else if (token > Opt_last_int && token < Opt_last_string) { | |
341 | dout("got string token %d val %s\n", token, | |
342 | argstr[0].from); | |
343 | } else { | |
344 | dout("got token %d\n", token); | |
345 | } | |
346 | ||
347 | switch (token) { | |
348 | case Opt_notify_timeout: | |
349 | rbdopt->notify_timeout = intval; | |
350 | break; | |
351 | default: | |
352 | BUG_ON(token); | |
353 | } | |
354 | return 0; | |
355 | } | |
356 | ||
602adf40 YS |
357 | /* |
358 | * Get a ceph client with specific addr and configuration, if one does | |
359 | * not exist create it. | |
360 | */ | |
361 | static int rbd_get_client(struct rbd_device *rbd_dev, const char *mon_addr, | |
362 | char *options) | |
363 | { | |
364 | struct rbd_client *rbdc; | |
365 | struct ceph_options *opt; | |
366 | int ret; | |
59c2be1e YS |
367 | struct rbd_options *rbd_opts; |
368 | ||
369 | rbd_opts = kzalloc(sizeof(*rbd_opts), GFP_KERNEL); | |
370 | if (!rbd_opts) | |
371 | return -ENOMEM; | |
372 | ||
373 | rbd_opts->notify_timeout = RBD_NOTIFY_TIMEOUT_DEFAULT; | |
602adf40 | 374 | |
ee57741c | 375 | opt = ceph_parse_options(options, mon_addr, |
21079786 AE |
376 | mon_addr + strlen(mon_addr), |
377 | parse_rbd_opts_token, rbd_opts); | |
ee57741c AE |
378 | if (IS_ERR(opt)) { |
379 | ret = PTR_ERR(opt); | |
59c2be1e | 380 | goto done_err; |
ee57741c | 381 | } |
602adf40 YS |
382 | |
383 | spin_lock(&node_lock); | |
384 | rbdc = __rbd_client_find(opt); | |
385 | if (rbdc) { | |
602adf40 YS |
386 | /* using an existing client */ |
387 | kref_get(&rbdc->kref); | |
602adf40 | 388 | spin_unlock(&node_lock); |
e6994d3d AE |
389 | |
390 | rbd_dev->rbd_client = rbdc; | |
391 | ||
392 | ceph_destroy_options(opt); | |
393 | kfree(rbd_opts); | |
394 | ||
602adf40 YS |
395 | return 0; |
396 | } | |
397 | spin_unlock(&node_lock); | |
398 | ||
d97081b0 | 399 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
59c2be1e | 400 | rbdc = rbd_client_create(opt, rbd_opts); |
d97081b0 AE |
401 | mutex_unlock(&ctl_mutex); |
402 | ||
59c2be1e YS |
403 | if (IS_ERR(rbdc)) { |
404 | ret = PTR_ERR(rbdc); | |
405 | goto done_err; | |
406 | } | |
602adf40 YS |
407 | |
408 | rbd_dev->rbd_client = rbdc; | |
602adf40 | 409 | return 0; |
59c2be1e YS |
410 | done_err: |
411 | kfree(rbd_opts); | |
412 | return ret; | |
602adf40 YS |
413 | } |
414 | ||
415 | /* | |
416 | * Destroy ceph client | |
d23a4b3f AE |
417 | * |
418 | * Caller must hold node_lock. | |
602adf40 YS |
419 | */ |
420 | static void rbd_client_release(struct kref *kref) | |
421 | { | |
422 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); | |
423 | ||
424 | dout("rbd_release_client %p\n", rbdc); | |
602adf40 | 425 | list_del(&rbdc->node); |
602adf40 YS |
426 | |
427 | ceph_destroy_client(rbdc->client); | |
59c2be1e | 428 | kfree(rbdc->rbd_opts); |
602adf40 YS |
429 | kfree(rbdc); |
430 | } | |
431 | ||
432 | /* | |
433 | * Drop reference to ceph client node. If it's not referenced anymore, release | |
434 | * it. | |
435 | */ | |
436 | static void rbd_put_client(struct rbd_device *rbd_dev) | |
437 | { | |
d23a4b3f | 438 | spin_lock(&node_lock); |
602adf40 | 439 | kref_put(&rbd_dev->rbd_client->kref, rbd_client_release); |
d23a4b3f | 440 | spin_unlock(&node_lock); |
602adf40 | 441 | rbd_dev->rbd_client = NULL; |
602adf40 YS |
442 | } |
443 | ||
1fec7093 YS |
444 | /* |
445 | * Destroy requests collection | |
446 | */ | |
447 | static void rbd_coll_release(struct kref *kref) | |
448 | { | |
449 | struct rbd_req_coll *coll = | |
450 | container_of(kref, struct rbd_req_coll, kref); | |
451 | ||
452 | dout("rbd_coll_release %p\n", coll); | |
453 | kfree(coll); | |
454 | } | |
602adf40 YS |
455 | |
456 | /* | |
457 | * Create a new header structure, translate header format from the on-disk | |
458 | * header. | |
459 | */ | |
460 | static int rbd_header_from_disk(struct rbd_image_header *header, | |
461 | struct rbd_image_header_ondisk *ondisk, | |
462 | int allocated_snaps, | |
463 | gfp_t gfp_flags) | |
464 | { | |
465 | int i; | |
466 | u32 snap_count = le32_to_cpu(ondisk->snap_count); | |
467 | int ret = -ENOMEM; | |
468 | ||
21079786 | 469 | if (memcmp(ondisk, RBD_HEADER_TEXT, sizeof(RBD_HEADER_TEXT))) |
81e759fb | 470 | return -ENXIO; |
81e759fb | 471 | |
602adf40 | 472 | init_rwsem(&header->snap_rwsem); |
602adf40 YS |
473 | header->snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
474 | header->snapc = kmalloc(sizeof(struct ceph_snap_context) + | |
21079786 | 475 | snap_count * sizeof (*ondisk), |
602adf40 YS |
476 | gfp_flags); |
477 | if (!header->snapc) | |
478 | return -ENOMEM; | |
479 | if (snap_count) { | |
480 | header->snap_names = kmalloc(header->snap_names_len, | |
481 | GFP_KERNEL); | |
482 | if (!header->snap_names) | |
483 | goto err_snapc; | |
484 | header->snap_sizes = kmalloc(snap_count * sizeof(u64), | |
485 | GFP_KERNEL); | |
486 | if (!header->snap_sizes) | |
487 | goto err_names; | |
488 | } else { | |
489 | header->snap_names = NULL; | |
490 | header->snap_sizes = NULL; | |
491 | } | |
492 | memcpy(header->block_name, ondisk->block_name, | |
493 | sizeof(ondisk->block_name)); | |
494 | ||
495 | header->image_size = le64_to_cpu(ondisk->image_size); | |
496 | header->obj_order = ondisk->options.order; | |
497 | header->crypt_type = ondisk->options.crypt_type; | |
498 | header->comp_type = ondisk->options.comp_type; | |
499 | ||
500 | atomic_set(&header->snapc->nref, 1); | |
501 | header->snap_seq = le64_to_cpu(ondisk->snap_seq); | |
502 | header->snapc->num_snaps = snap_count; | |
503 | header->total_snaps = snap_count; | |
504 | ||
21079786 | 505 | if (snap_count && allocated_snaps == snap_count) { |
602adf40 YS |
506 | for (i = 0; i < snap_count; i++) { |
507 | header->snapc->snaps[i] = | |
508 | le64_to_cpu(ondisk->snaps[i].id); | |
509 | header->snap_sizes[i] = | |
510 | le64_to_cpu(ondisk->snaps[i].image_size); | |
511 | } | |
512 | ||
513 | /* copy snapshot names */ | |
514 | memcpy(header->snap_names, &ondisk->snaps[i], | |
515 | header->snap_names_len); | |
516 | } | |
517 | ||
518 | return 0; | |
519 | ||
520 | err_names: | |
521 | kfree(header->snap_names); | |
522 | err_snapc: | |
523 | kfree(header->snapc); | |
524 | return ret; | |
525 | } | |
526 | ||
527 | static int snap_index(struct rbd_image_header *header, int snap_num) | |
528 | { | |
529 | return header->total_snaps - snap_num; | |
530 | } | |
531 | ||
532 | static u64 cur_snap_id(struct rbd_device *rbd_dev) | |
533 | { | |
534 | struct rbd_image_header *header = &rbd_dev->header; | |
535 | ||
536 | if (!rbd_dev->cur_snap) | |
537 | return 0; | |
538 | ||
539 | return header->snapc->snaps[snap_index(header, rbd_dev->cur_snap)]; | |
540 | } | |
541 | ||
542 | static int snap_by_name(struct rbd_image_header *header, const char *snap_name, | |
543 | u64 *seq, u64 *size) | |
544 | { | |
545 | int i; | |
546 | char *p = header->snap_names; | |
547 | ||
548 | for (i = 0; i < header->total_snaps; i++, p += strlen(p) + 1) { | |
549 | if (strcmp(snap_name, p) == 0) | |
550 | break; | |
551 | } | |
552 | if (i == header->total_snaps) | |
553 | return -ENOENT; | |
554 | if (seq) | |
555 | *seq = header->snapc->snaps[i]; | |
556 | ||
557 | if (size) | |
558 | *size = header->snap_sizes[i]; | |
559 | ||
560 | return i; | |
561 | } | |
562 | ||
cc9d734c | 563 | static int rbd_header_set_snap(struct rbd_device *dev, u64 *size) |
602adf40 YS |
564 | { |
565 | struct rbd_image_header *header = &dev->header; | |
566 | struct ceph_snap_context *snapc = header->snapc; | |
567 | int ret = -ENOENT; | |
568 | ||
cc9d734c JD |
569 | BUILD_BUG_ON(sizeof (dev->snap_name) < sizeof (RBD_SNAP_HEAD_NAME)); |
570 | ||
602adf40 YS |
571 | down_write(&header->snap_rwsem); |
572 | ||
cc9d734c JD |
573 | if (!memcmp(dev->snap_name, RBD_SNAP_HEAD_NAME, |
574 | sizeof (RBD_SNAP_HEAD_NAME))) { | |
602adf40 YS |
575 | if (header->total_snaps) |
576 | snapc->seq = header->snap_seq; | |
577 | else | |
578 | snapc->seq = 0; | |
579 | dev->cur_snap = 0; | |
580 | dev->read_only = 0; | |
581 | if (size) | |
582 | *size = header->image_size; | |
583 | } else { | |
cc9d734c | 584 | ret = snap_by_name(header, dev->snap_name, &snapc->seq, size); |
602adf40 YS |
585 | if (ret < 0) |
586 | goto done; | |
587 | ||
588 | dev->cur_snap = header->total_snaps - ret; | |
589 | dev->read_only = 1; | |
590 | } | |
591 | ||
592 | ret = 0; | |
593 | done: | |
594 | up_write(&header->snap_rwsem); | |
595 | return ret; | |
596 | } | |
597 | ||
598 | static void rbd_header_free(struct rbd_image_header *header) | |
599 | { | |
600 | kfree(header->snapc); | |
601 | kfree(header->snap_names); | |
602 | kfree(header->snap_sizes); | |
603 | } | |
604 | ||
605 | /* | |
606 | * get the actual striped segment name, offset and length | |
607 | */ | |
608 | static u64 rbd_get_segment(struct rbd_image_header *header, | |
609 | const char *block_name, | |
610 | u64 ofs, u64 len, | |
611 | char *seg_name, u64 *segofs) | |
612 | { | |
613 | u64 seg = ofs >> header->obj_order; | |
614 | ||
615 | if (seg_name) | |
616 | snprintf(seg_name, RBD_MAX_SEG_NAME_LEN, | |
617 | "%s.%012llx", block_name, seg); | |
618 | ||
619 | ofs = ofs & ((1 << header->obj_order) - 1); | |
620 | len = min_t(u64, len, (1 << header->obj_order) - ofs); | |
621 | ||
622 | if (segofs) | |
623 | *segofs = ofs; | |
624 | ||
625 | return len; | |
626 | } | |
627 | ||
1fec7093 YS |
628 | static int rbd_get_num_segments(struct rbd_image_header *header, |
629 | u64 ofs, u64 len) | |
630 | { | |
631 | u64 start_seg = ofs >> header->obj_order; | |
632 | u64 end_seg = (ofs + len - 1) >> header->obj_order; | |
633 | return end_seg - start_seg + 1; | |
634 | } | |
635 | ||
029bcbd8 JD |
636 | /* |
637 | * returns the size of an object in the image | |
638 | */ | |
639 | static u64 rbd_obj_bytes(struct rbd_image_header *header) | |
640 | { | |
641 | return 1 << header->obj_order; | |
642 | } | |
643 | ||
602adf40 YS |
644 | /* |
645 | * bio helpers | |
646 | */ | |
647 | ||
648 | static void bio_chain_put(struct bio *chain) | |
649 | { | |
650 | struct bio *tmp; | |
651 | ||
652 | while (chain) { | |
653 | tmp = chain; | |
654 | chain = chain->bi_next; | |
655 | bio_put(tmp); | |
656 | } | |
657 | } | |
658 | ||
659 | /* | |
660 | * zeros a bio chain, starting at specific offset | |
661 | */ | |
662 | static void zero_bio_chain(struct bio *chain, int start_ofs) | |
663 | { | |
664 | struct bio_vec *bv; | |
665 | unsigned long flags; | |
666 | void *buf; | |
667 | int i; | |
668 | int pos = 0; | |
669 | ||
670 | while (chain) { | |
671 | bio_for_each_segment(bv, chain, i) { | |
672 | if (pos + bv->bv_len > start_ofs) { | |
673 | int remainder = max(start_ofs - pos, 0); | |
674 | buf = bvec_kmap_irq(bv, &flags); | |
675 | memset(buf + remainder, 0, | |
676 | bv->bv_len - remainder); | |
85b5aaa6 | 677 | bvec_kunmap_irq(buf, &flags); |
602adf40 YS |
678 | } |
679 | pos += bv->bv_len; | |
680 | } | |
681 | ||
682 | chain = chain->bi_next; | |
683 | } | |
684 | } | |
685 | ||
686 | /* | |
687 | * bio_chain_clone - clone a chain of bios up to a certain length. | |
688 | * might return a bio_pair that will need to be released. | |
689 | */ | |
690 | static struct bio *bio_chain_clone(struct bio **old, struct bio **next, | |
691 | struct bio_pair **bp, | |
692 | int len, gfp_t gfpmask) | |
693 | { | |
694 | struct bio *tmp, *old_chain = *old, *new_chain = NULL, *tail = NULL; | |
695 | int total = 0; | |
696 | ||
697 | if (*bp) { | |
698 | bio_pair_release(*bp); | |
699 | *bp = NULL; | |
700 | } | |
701 | ||
702 | while (old_chain && (total < len)) { | |
703 | tmp = bio_kmalloc(gfpmask, old_chain->bi_max_vecs); | |
704 | if (!tmp) | |
705 | goto err_out; | |
706 | ||
707 | if (total + old_chain->bi_size > len) { | |
708 | struct bio_pair *bp; | |
709 | ||
710 | /* | |
711 | * this split can only happen with a single paged bio, | |
712 | * split_bio will BUG_ON if this is not the case | |
713 | */ | |
714 | dout("bio_chain_clone split! total=%d remaining=%d" | |
715 | "bi_size=%d\n", | |
716 | (int)total, (int)len-total, | |
717 | (int)old_chain->bi_size); | |
718 | ||
719 | /* split the bio. We'll release it either in the next | |
720 | call, or it will have to be released outside */ | |
721 | bp = bio_split(old_chain, (len - total) / 512ULL); | |
722 | if (!bp) | |
723 | goto err_out; | |
724 | ||
725 | __bio_clone(tmp, &bp->bio1); | |
726 | ||
727 | *next = &bp->bio2; | |
728 | } else { | |
729 | __bio_clone(tmp, old_chain); | |
730 | *next = old_chain->bi_next; | |
731 | } | |
732 | ||
733 | tmp->bi_bdev = NULL; | |
734 | gfpmask &= ~__GFP_WAIT; | |
735 | tmp->bi_next = NULL; | |
736 | ||
737 | if (!new_chain) { | |
738 | new_chain = tail = tmp; | |
739 | } else { | |
740 | tail->bi_next = tmp; | |
741 | tail = tmp; | |
742 | } | |
743 | old_chain = old_chain->bi_next; | |
744 | ||
745 | total += tmp->bi_size; | |
746 | } | |
747 | ||
748 | BUG_ON(total < len); | |
749 | ||
750 | if (tail) | |
751 | tail->bi_next = NULL; | |
752 | ||
753 | *old = old_chain; | |
754 | ||
755 | return new_chain; | |
756 | ||
757 | err_out: | |
758 | dout("bio_chain_clone with err\n"); | |
759 | bio_chain_put(new_chain); | |
760 | return NULL; | |
761 | } | |
762 | ||
763 | /* | |
764 | * helpers for osd request op vectors. | |
765 | */ | |
766 | static int rbd_create_rw_ops(struct ceph_osd_req_op **ops, | |
767 | int num_ops, | |
768 | int opcode, | |
769 | u32 payload_len) | |
770 | { | |
771 | *ops = kzalloc(sizeof(struct ceph_osd_req_op) * (num_ops + 1), | |
772 | GFP_NOIO); | |
773 | if (!*ops) | |
774 | return -ENOMEM; | |
775 | (*ops)[0].op = opcode; | |
776 | /* | |
777 | * op extent offset and length will be set later on | |
778 | * in calc_raw_layout() | |
779 | */ | |
780 | (*ops)[0].payload_len = payload_len; | |
781 | return 0; | |
782 | } | |
783 | ||
784 | static void rbd_destroy_ops(struct ceph_osd_req_op *ops) | |
785 | { | |
786 | kfree(ops); | |
787 | } | |
788 | ||
1fec7093 YS |
789 | static void rbd_coll_end_req_index(struct request *rq, |
790 | struct rbd_req_coll *coll, | |
791 | int index, | |
792 | int ret, u64 len) | |
793 | { | |
794 | struct request_queue *q; | |
795 | int min, max, i; | |
796 | ||
797 | dout("rbd_coll_end_req_index %p index %d ret %d len %lld\n", | |
798 | coll, index, ret, len); | |
799 | ||
800 | if (!rq) | |
801 | return; | |
802 | ||
803 | if (!coll) { | |
804 | blk_end_request(rq, ret, len); | |
805 | return; | |
806 | } | |
807 | ||
808 | q = rq->q; | |
809 | ||
810 | spin_lock_irq(q->queue_lock); | |
811 | coll->status[index].done = 1; | |
812 | coll->status[index].rc = ret; | |
813 | coll->status[index].bytes = len; | |
814 | max = min = coll->num_done; | |
815 | while (max < coll->total && coll->status[max].done) | |
816 | max++; | |
817 | ||
818 | for (i = min; i<max; i++) { | |
819 | __blk_end_request(rq, coll->status[i].rc, | |
820 | coll->status[i].bytes); | |
821 | coll->num_done++; | |
822 | kref_put(&coll->kref, rbd_coll_release); | |
823 | } | |
824 | spin_unlock_irq(q->queue_lock); | |
825 | } | |
826 | ||
827 | static void rbd_coll_end_req(struct rbd_request *req, | |
828 | int ret, u64 len) | |
829 | { | |
830 | rbd_coll_end_req_index(req->rq, req->coll, req->coll_index, ret, len); | |
831 | } | |
832 | ||
602adf40 YS |
833 | /* |
834 | * Send ceph osd request | |
835 | */ | |
836 | static int rbd_do_request(struct request *rq, | |
837 | struct rbd_device *dev, | |
838 | struct ceph_snap_context *snapc, | |
839 | u64 snapid, | |
840 | const char *obj, u64 ofs, u64 len, | |
841 | struct bio *bio, | |
842 | struct page **pages, | |
843 | int num_pages, | |
844 | int flags, | |
845 | struct ceph_osd_req_op *ops, | |
846 | int num_reply, | |
1fec7093 YS |
847 | struct rbd_req_coll *coll, |
848 | int coll_index, | |
602adf40 | 849 | void (*rbd_cb)(struct ceph_osd_request *req, |
59c2be1e YS |
850 | struct ceph_msg *msg), |
851 | struct ceph_osd_request **linger_req, | |
852 | u64 *ver) | |
602adf40 YS |
853 | { |
854 | struct ceph_osd_request *req; | |
855 | struct ceph_file_layout *layout; | |
856 | int ret; | |
857 | u64 bno; | |
858 | struct timespec mtime = CURRENT_TIME; | |
859 | struct rbd_request *req_data; | |
860 | struct ceph_osd_request_head *reqhead; | |
861 | struct rbd_image_header *header = &dev->header; | |
1dbb4399 | 862 | struct ceph_osd_client *osdc; |
602adf40 | 863 | |
602adf40 | 864 | req_data = kzalloc(sizeof(*req_data), GFP_NOIO); |
1fec7093 YS |
865 | if (!req_data) { |
866 | if (coll) | |
867 | rbd_coll_end_req_index(rq, coll, coll_index, | |
868 | -ENOMEM, len); | |
869 | return -ENOMEM; | |
870 | } | |
871 | ||
872 | if (coll) { | |
873 | req_data->coll = coll; | |
874 | req_data->coll_index = coll_index; | |
875 | } | |
602adf40 | 876 | |
1fec7093 | 877 | dout("rbd_do_request obj=%s ofs=%lld len=%lld\n", obj, len, ofs); |
602adf40 YS |
878 | |
879 | down_read(&header->snap_rwsem); | |
880 | ||
1dbb4399 AE |
881 | osdc = &dev->rbd_client->client->osdc; |
882 | req = ceph_osdc_alloc_request(osdc, flags, snapc, ops, | |
883 | false, GFP_NOIO, pages, bio); | |
4ad12621 | 884 | if (!req) { |
602adf40 | 885 | up_read(&header->snap_rwsem); |
4ad12621 | 886 | ret = -ENOMEM; |
602adf40 YS |
887 | goto done_pages; |
888 | } | |
889 | ||
890 | req->r_callback = rbd_cb; | |
891 | ||
892 | req_data->rq = rq; | |
893 | req_data->bio = bio; | |
894 | req_data->pages = pages; | |
895 | req_data->len = len; | |
896 | ||
897 | req->r_priv = req_data; | |
898 | ||
899 | reqhead = req->r_request->front.iov_base; | |
900 | reqhead->snapid = cpu_to_le64(CEPH_NOSNAP); | |
901 | ||
902 | strncpy(req->r_oid, obj, sizeof(req->r_oid)); | |
903 | req->r_oid_len = strlen(req->r_oid); | |
904 | ||
905 | layout = &req->r_file_layout; | |
906 | memset(layout, 0, sizeof(*layout)); | |
907 | layout->fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER); | |
908 | layout->fl_stripe_count = cpu_to_le32(1); | |
909 | layout->fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER); | |
910 | layout->fl_pg_preferred = cpu_to_le32(-1); | |
911 | layout->fl_pg_pool = cpu_to_le32(dev->poolid); | |
1dbb4399 AE |
912 | ceph_calc_raw_layout(osdc, layout, snapid, ofs, &len, &bno, |
913 | req, ops); | |
602adf40 YS |
914 | |
915 | ceph_osdc_build_request(req, ofs, &len, | |
916 | ops, | |
917 | snapc, | |
918 | &mtime, | |
919 | req->r_oid, req->r_oid_len); | |
920 | up_read(&header->snap_rwsem); | |
921 | ||
59c2be1e | 922 | if (linger_req) { |
1dbb4399 | 923 | ceph_osdc_set_request_linger(osdc, req); |
59c2be1e YS |
924 | *linger_req = req; |
925 | } | |
926 | ||
1dbb4399 | 927 | ret = ceph_osdc_start_request(osdc, req, false); |
602adf40 YS |
928 | if (ret < 0) |
929 | goto done_err; | |
930 | ||
931 | if (!rbd_cb) { | |
1dbb4399 | 932 | ret = ceph_osdc_wait_request(osdc, req); |
59c2be1e YS |
933 | if (ver) |
934 | *ver = le64_to_cpu(req->r_reassert_version.version); | |
1fec7093 YS |
935 | dout("reassert_ver=%lld\n", |
936 | le64_to_cpu(req->r_reassert_version.version)); | |
602adf40 YS |
937 | ceph_osdc_put_request(req); |
938 | } | |
939 | return ret; | |
940 | ||
941 | done_err: | |
942 | bio_chain_put(req_data->bio); | |
943 | ceph_osdc_put_request(req); | |
944 | done_pages: | |
1fec7093 | 945 | rbd_coll_end_req(req_data, ret, len); |
602adf40 | 946 | kfree(req_data); |
602adf40 YS |
947 | return ret; |
948 | } | |
949 | ||
950 | /* | |
951 | * Ceph osd op callback | |
952 | */ | |
953 | static void rbd_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg) | |
954 | { | |
955 | struct rbd_request *req_data = req->r_priv; | |
956 | struct ceph_osd_reply_head *replyhead; | |
957 | struct ceph_osd_op *op; | |
958 | __s32 rc; | |
959 | u64 bytes; | |
960 | int read_op; | |
961 | ||
962 | /* parse reply */ | |
963 | replyhead = msg->front.iov_base; | |
964 | WARN_ON(le32_to_cpu(replyhead->num_ops) == 0); | |
965 | op = (void *)(replyhead + 1); | |
966 | rc = le32_to_cpu(replyhead->result); | |
967 | bytes = le64_to_cpu(op->extent.length); | |
968 | read_op = (le32_to_cpu(op->op) == CEPH_OSD_OP_READ); | |
969 | ||
970 | dout("rbd_req_cb bytes=%lld readop=%d rc=%d\n", bytes, read_op, rc); | |
971 | ||
972 | if (rc == -ENOENT && read_op) { | |
973 | zero_bio_chain(req_data->bio, 0); | |
974 | rc = 0; | |
975 | } else if (rc == 0 && read_op && bytes < req_data->len) { | |
976 | zero_bio_chain(req_data->bio, bytes); | |
977 | bytes = req_data->len; | |
978 | } | |
979 | ||
1fec7093 | 980 | rbd_coll_end_req(req_data, rc, bytes); |
602adf40 YS |
981 | |
982 | if (req_data->bio) | |
983 | bio_chain_put(req_data->bio); | |
984 | ||
985 | ceph_osdc_put_request(req); | |
986 | kfree(req_data); | |
987 | } | |
988 | ||
59c2be1e YS |
989 | static void rbd_simple_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg) |
990 | { | |
991 | ceph_osdc_put_request(req); | |
992 | } | |
993 | ||
602adf40 YS |
994 | /* |
995 | * Do a synchronous ceph osd operation | |
996 | */ | |
997 | static int rbd_req_sync_op(struct rbd_device *dev, | |
998 | struct ceph_snap_context *snapc, | |
999 | u64 snapid, | |
1000 | int opcode, | |
1001 | int flags, | |
1002 | struct ceph_osd_req_op *orig_ops, | |
1003 | int num_reply, | |
1004 | const char *obj, | |
1005 | u64 ofs, u64 len, | |
59c2be1e YS |
1006 | char *buf, |
1007 | struct ceph_osd_request **linger_req, | |
1008 | u64 *ver) | |
602adf40 YS |
1009 | { |
1010 | int ret; | |
1011 | struct page **pages; | |
1012 | int num_pages; | |
1013 | struct ceph_osd_req_op *ops = orig_ops; | |
1014 | u32 payload_len; | |
1015 | ||
1016 | num_pages = calc_pages_for(ofs , len); | |
1017 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); | |
b8d0638a DC |
1018 | if (IS_ERR(pages)) |
1019 | return PTR_ERR(pages); | |
602adf40 YS |
1020 | |
1021 | if (!orig_ops) { | |
1022 | payload_len = (flags & CEPH_OSD_FLAG_WRITE ? len : 0); | |
1023 | ret = rbd_create_rw_ops(&ops, 1, opcode, payload_len); | |
1024 | if (ret < 0) | |
1025 | goto done; | |
1026 | ||
1027 | if ((flags & CEPH_OSD_FLAG_WRITE) && buf) { | |
1028 | ret = ceph_copy_to_page_vector(pages, buf, ofs, len); | |
1029 | if (ret < 0) | |
1030 | goto done_ops; | |
1031 | } | |
1032 | } | |
1033 | ||
1034 | ret = rbd_do_request(NULL, dev, snapc, snapid, | |
1035 | obj, ofs, len, NULL, | |
1036 | pages, num_pages, | |
1037 | flags, | |
1038 | ops, | |
1039 | 2, | |
1fec7093 | 1040 | NULL, 0, |
59c2be1e YS |
1041 | NULL, |
1042 | linger_req, ver); | |
602adf40 YS |
1043 | if (ret < 0) |
1044 | goto done_ops; | |
1045 | ||
1046 | if ((flags & CEPH_OSD_FLAG_READ) && buf) | |
1047 | ret = ceph_copy_from_page_vector(pages, buf, ofs, ret); | |
1048 | ||
1049 | done_ops: | |
1050 | if (!orig_ops) | |
1051 | rbd_destroy_ops(ops); | |
1052 | done: | |
1053 | ceph_release_page_vector(pages, num_pages); | |
1054 | return ret; | |
1055 | } | |
1056 | ||
1057 | /* | |
1058 | * Do an asynchronous ceph osd operation | |
1059 | */ | |
1060 | static int rbd_do_op(struct request *rq, | |
1061 | struct rbd_device *rbd_dev , | |
1062 | struct ceph_snap_context *snapc, | |
1063 | u64 snapid, | |
1064 | int opcode, int flags, int num_reply, | |
1065 | u64 ofs, u64 len, | |
1fec7093 YS |
1066 | struct bio *bio, |
1067 | struct rbd_req_coll *coll, | |
1068 | int coll_index) | |
602adf40 YS |
1069 | { |
1070 | char *seg_name; | |
1071 | u64 seg_ofs; | |
1072 | u64 seg_len; | |
1073 | int ret; | |
1074 | struct ceph_osd_req_op *ops; | |
1075 | u32 payload_len; | |
1076 | ||
1077 | seg_name = kmalloc(RBD_MAX_SEG_NAME_LEN + 1, GFP_NOIO); | |
1078 | if (!seg_name) | |
1079 | return -ENOMEM; | |
1080 | ||
1081 | seg_len = rbd_get_segment(&rbd_dev->header, | |
1082 | rbd_dev->header.block_name, | |
1083 | ofs, len, | |
1084 | seg_name, &seg_ofs); | |
602adf40 YS |
1085 | |
1086 | payload_len = (flags & CEPH_OSD_FLAG_WRITE ? seg_len : 0); | |
1087 | ||
1088 | ret = rbd_create_rw_ops(&ops, 1, opcode, payload_len); | |
1089 | if (ret < 0) | |
1090 | goto done; | |
1091 | ||
1092 | /* we've taken care of segment sizes earlier when we | |
1093 | cloned the bios. We should never have a segment | |
1094 | truncated at this point */ | |
1095 | BUG_ON(seg_len < len); | |
1096 | ||
1097 | ret = rbd_do_request(rq, rbd_dev, snapc, snapid, | |
1098 | seg_name, seg_ofs, seg_len, | |
1099 | bio, | |
1100 | NULL, 0, | |
1101 | flags, | |
1102 | ops, | |
1103 | num_reply, | |
1fec7093 | 1104 | coll, coll_index, |
59c2be1e | 1105 | rbd_req_cb, 0, NULL); |
11f77002 SW |
1106 | |
1107 | rbd_destroy_ops(ops); | |
602adf40 YS |
1108 | done: |
1109 | kfree(seg_name); | |
1110 | return ret; | |
1111 | } | |
1112 | ||
1113 | /* | |
1114 | * Request async osd write | |
1115 | */ | |
1116 | static int rbd_req_write(struct request *rq, | |
1117 | struct rbd_device *rbd_dev, | |
1118 | struct ceph_snap_context *snapc, | |
1119 | u64 ofs, u64 len, | |
1fec7093 YS |
1120 | struct bio *bio, |
1121 | struct rbd_req_coll *coll, | |
1122 | int coll_index) | |
602adf40 YS |
1123 | { |
1124 | return rbd_do_op(rq, rbd_dev, snapc, CEPH_NOSNAP, | |
1125 | CEPH_OSD_OP_WRITE, | |
1126 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, | |
1127 | 2, | |
1fec7093 | 1128 | ofs, len, bio, coll, coll_index); |
602adf40 YS |
1129 | } |
1130 | ||
1131 | /* | |
1132 | * Request async osd read | |
1133 | */ | |
1134 | static int rbd_req_read(struct request *rq, | |
1135 | struct rbd_device *rbd_dev, | |
1136 | u64 snapid, | |
1137 | u64 ofs, u64 len, | |
1fec7093 YS |
1138 | struct bio *bio, |
1139 | struct rbd_req_coll *coll, | |
1140 | int coll_index) | |
602adf40 YS |
1141 | { |
1142 | return rbd_do_op(rq, rbd_dev, NULL, | |
1143 | (snapid ? snapid : CEPH_NOSNAP), | |
1144 | CEPH_OSD_OP_READ, | |
1145 | CEPH_OSD_FLAG_READ, | |
1146 | 2, | |
1fec7093 | 1147 | ofs, len, bio, coll, coll_index); |
602adf40 YS |
1148 | } |
1149 | ||
1150 | /* | |
1151 | * Request sync osd read | |
1152 | */ | |
1153 | static int rbd_req_sync_read(struct rbd_device *dev, | |
1154 | struct ceph_snap_context *snapc, | |
1155 | u64 snapid, | |
1156 | const char *obj, | |
1157 | u64 ofs, u64 len, | |
59c2be1e YS |
1158 | char *buf, |
1159 | u64 *ver) | |
602adf40 YS |
1160 | { |
1161 | return rbd_req_sync_op(dev, NULL, | |
1162 | (snapid ? snapid : CEPH_NOSNAP), | |
1163 | CEPH_OSD_OP_READ, | |
1164 | CEPH_OSD_FLAG_READ, | |
1165 | NULL, | |
59c2be1e | 1166 | 1, obj, ofs, len, buf, NULL, ver); |
602adf40 YS |
1167 | } |
1168 | ||
1169 | /* | |
59c2be1e YS |
1170 | * Request sync osd watch |
1171 | */ | |
1172 | static int rbd_req_sync_notify_ack(struct rbd_device *dev, | |
1173 | u64 ver, | |
1174 | u64 notify_id, | |
1175 | const char *obj) | |
1176 | { | |
1177 | struct ceph_osd_req_op *ops; | |
1178 | struct page **pages = NULL; | |
11f77002 SW |
1179 | int ret; |
1180 | ||
1181 | ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_NOTIFY_ACK, 0); | |
59c2be1e YS |
1182 | if (ret < 0) |
1183 | return ret; | |
1184 | ||
1185 | ops[0].watch.ver = cpu_to_le64(dev->header.obj_version); | |
1186 | ops[0].watch.cookie = notify_id; | |
1187 | ops[0].watch.flag = 0; | |
1188 | ||
1189 | ret = rbd_do_request(NULL, dev, NULL, CEPH_NOSNAP, | |
1190 | obj, 0, 0, NULL, | |
1191 | pages, 0, | |
1192 | CEPH_OSD_FLAG_READ, | |
1193 | ops, | |
1194 | 1, | |
1fec7093 | 1195 | NULL, 0, |
59c2be1e YS |
1196 | rbd_simple_req_cb, 0, NULL); |
1197 | ||
1198 | rbd_destroy_ops(ops); | |
1199 | return ret; | |
1200 | } | |
1201 | ||
1202 | static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data) | |
1203 | { | |
1204 | struct rbd_device *dev = (struct rbd_device *)data; | |
13143d2d SW |
1205 | int rc; |
1206 | ||
59c2be1e YS |
1207 | if (!dev) |
1208 | return; | |
1209 | ||
1210 | dout("rbd_watch_cb %s notify_id=%lld opcode=%d\n", dev->obj_md_name, | |
1211 | notify_id, (int)opcode); | |
1212 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); | |
13143d2d | 1213 | rc = __rbd_update_snaps(dev); |
59c2be1e | 1214 | mutex_unlock(&ctl_mutex); |
13143d2d SW |
1215 | if (rc) |
1216 | pr_warning(DRV_NAME "%d got notification but failed to update" | |
1217 | " snaps: %d\n", dev->major, rc); | |
59c2be1e YS |
1218 | |
1219 | rbd_req_sync_notify_ack(dev, ver, notify_id, dev->obj_md_name); | |
1220 | } | |
1221 | ||
1222 | /* | |
1223 | * Request sync osd watch | |
1224 | */ | |
1225 | static int rbd_req_sync_watch(struct rbd_device *dev, | |
1226 | const char *obj, | |
1227 | u64 ver) | |
1228 | { | |
1229 | struct ceph_osd_req_op *ops; | |
1dbb4399 | 1230 | struct ceph_osd_client *osdc = &dev->rbd_client->client->osdc; |
59c2be1e YS |
1231 | |
1232 | int ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_WATCH, 0); | |
1233 | if (ret < 0) | |
1234 | return ret; | |
1235 | ||
1236 | ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0, | |
1237 | (void *)dev, &dev->watch_event); | |
1238 | if (ret < 0) | |
1239 | goto fail; | |
1240 | ||
1241 | ops[0].watch.ver = cpu_to_le64(ver); | |
1242 | ops[0].watch.cookie = cpu_to_le64(dev->watch_event->cookie); | |
1243 | ops[0].watch.flag = 1; | |
1244 | ||
1245 | ret = rbd_req_sync_op(dev, NULL, | |
1246 | CEPH_NOSNAP, | |
1247 | 0, | |
1248 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, | |
1249 | ops, | |
1250 | 1, obj, 0, 0, NULL, | |
1251 | &dev->watch_request, NULL); | |
1252 | ||
1253 | if (ret < 0) | |
1254 | goto fail_event; | |
1255 | ||
1256 | rbd_destroy_ops(ops); | |
1257 | return 0; | |
1258 | ||
1259 | fail_event: | |
1260 | ceph_osdc_cancel_event(dev->watch_event); | |
1261 | dev->watch_event = NULL; | |
1262 | fail: | |
1263 | rbd_destroy_ops(ops); | |
1264 | return ret; | |
1265 | } | |
1266 | ||
79e3057c YS |
1267 | /* |
1268 | * Request sync osd unwatch | |
1269 | */ | |
1270 | static int rbd_req_sync_unwatch(struct rbd_device *dev, | |
1271 | const char *obj) | |
1272 | { | |
1273 | struct ceph_osd_req_op *ops; | |
1274 | ||
1275 | int ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_WATCH, 0); | |
1276 | if (ret < 0) | |
1277 | return ret; | |
1278 | ||
1279 | ops[0].watch.ver = 0; | |
1280 | ops[0].watch.cookie = cpu_to_le64(dev->watch_event->cookie); | |
1281 | ops[0].watch.flag = 0; | |
1282 | ||
1283 | ret = rbd_req_sync_op(dev, NULL, | |
1284 | CEPH_NOSNAP, | |
1285 | 0, | |
1286 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, | |
1287 | ops, | |
1288 | 1, obj, 0, 0, NULL, NULL, NULL); | |
1289 | ||
1290 | rbd_destroy_ops(ops); | |
1291 | ceph_osdc_cancel_event(dev->watch_event); | |
1292 | dev->watch_event = NULL; | |
1293 | return ret; | |
1294 | } | |
1295 | ||
59c2be1e YS |
1296 | struct rbd_notify_info { |
1297 | struct rbd_device *dev; | |
1298 | }; | |
1299 | ||
1300 | static void rbd_notify_cb(u64 ver, u64 notify_id, u8 opcode, void *data) | |
1301 | { | |
1302 | struct rbd_device *dev = (struct rbd_device *)data; | |
1303 | if (!dev) | |
1304 | return; | |
1305 | ||
1306 | dout("rbd_notify_cb %s notify_id=%lld opcode=%d\n", dev->obj_md_name, | |
1307 | notify_id, (int)opcode); | |
1308 | } | |
1309 | ||
1310 | /* | |
1311 | * Request sync osd notify | |
1312 | */ | |
1313 | static int rbd_req_sync_notify(struct rbd_device *dev, | |
1314 | const char *obj) | |
1315 | { | |
1316 | struct ceph_osd_req_op *ops; | |
1dbb4399 | 1317 | struct ceph_osd_client *osdc = &dev->rbd_client->client->osdc; |
59c2be1e YS |
1318 | struct ceph_osd_event *event; |
1319 | struct rbd_notify_info info; | |
1320 | int payload_len = sizeof(u32) + sizeof(u32); | |
1321 | int ret; | |
1322 | ||
1323 | ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_NOTIFY, payload_len); | |
1324 | if (ret < 0) | |
1325 | return ret; | |
1326 | ||
1327 | info.dev = dev; | |
1328 | ||
1329 | ret = ceph_osdc_create_event(osdc, rbd_notify_cb, 1, | |
1330 | (void *)&info, &event); | |
1331 | if (ret < 0) | |
1332 | goto fail; | |
1333 | ||
1334 | ops[0].watch.ver = 1; | |
1335 | ops[0].watch.flag = 1; | |
1336 | ops[0].watch.cookie = event->cookie; | |
1337 | ops[0].watch.prot_ver = RADOS_NOTIFY_VER; | |
1338 | ops[0].watch.timeout = 12; | |
1339 | ||
1340 | ret = rbd_req_sync_op(dev, NULL, | |
1341 | CEPH_NOSNAP, | |
1342 | 0, | |
1343 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, | |
1344 | ops, | |
1345 | 1, obj, 0, 0, NULL, NULL, NULL); | |
1346 | if (ret < 0) | |
1347 | goto fail_event; | |
1348 | ||
1349 | ret = ceph_osdc_wait_event(event, CEPH_OSD_TIMEOUT_DEFAULT); | |
1350 | dout("ceph_osdc_wait_event returned %d\n", ret); | |
1351 | rbd_destroy_ops(ops); | |
1352 | return 0; | |
1353 | ||
1354 | fail_event: | |
1355 | ceph_osdc_cancel_event(event); | |
1356 | fail: | |
1357 | rbd_destroy_ops(ops); | |
1358 | return ret; | |
1359 | } | |
1360 | ||
602adf40 YS |
1361 | /* |
1362 | * Request sync osd read | |
1363 | */ | |
1364 | static int rbd_req_sync_exec(struct rbd_device *dev, | |
1365 | const char *obj, | |
1366 | const char *cls, | |
1367 | const char *method, | |
1368 | const char *data, | |
59c2be1e YS |
1369 | int len, |
1370 | u64 *ver) | |
602adf40 YS |
1371 | { |
1372 | struct ceph_osd_req_op *ops; | |
1373 | int cls_len = strlen(cls); | |
1374 | int method_len = strlen(method); | |
1375 | int ret = rbd_create_rw_ops(&ops, 1, CEPH_OSD_OP_CALL, | |
1376 | cls_len + method_len + len); | |
1377 | if (ret < 0) | |
1378 | return ret; | |
1379 | ||
1380 | ops[0].cls.class_name = cls; | |
1381 | ops[0].cls.class_len = (__u8)cls_len; | |
1382 | ops[0].cls.method_name = method; | |
1383 | ops[0].cls.method_len = (__u8)method_len; | |
1384 | ops[0].cls.argc = 0; | |
1385 | ops[0].cls.indata = data; | |
1386 | ops[0].cls.indata_len = len; | |
1387 | ||
1388 | ret = rbd_req_sync_op(dev, NULL, | |
1389 | CEPH_NOSNAP, | |
1390 | 0, | |
1391 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, | |
1392 | ops, | |
59c2be1e | 1393 | 1, obj, 0, 0, NULL, NULL, ver); |
602adf40 YS |
1394 | |
1395 | rbd_destroy_ops(ops); | |
1396 | ||
1397 | dout("cls_exec returned %d\n", ret); | |
1398 | return ret; | |
1399 | } | |
1400 | ||
1fec7093 YS |
1401 | static struct rbd_req_coll *rbd_alloc_coll(int num_reqs) |
1402 | { | |
1403 | struct rbd_req_coll *coll = | |
1404 | kzalloc(sizeof(struct rbd_req_coll) + | |
1405 | sizeof(struct rbd_req_status) * num_reqs, | |
1406 | GFP_ATOMIC); | |
1407 | ||
1408 | if (!coll) | |
1409 | return NULL; | |
1410 | coll->total = num_reqs; | |
1411 | kref_init(&coll->kref); | |
1412 | return coll; | |
1413 | } | |
1414 | ||
602adf40 YS |
1415 | /* |
1416 | * block device queue callback | |
1417 | */ | |
1418 | static void rbd_rq_fn(struct request_queue *q) | |
1419 | { | |
1420 | struct rbd_device *rbd_dev = q->queuedata; | |
1421 | struct request *rq; | |
1422 | struct bio_pair *bp = NULL; | |
1423 | ||
1424 | rq = blk_fetch_request(q); | |
1425 | ||
1426 | while (1) { | |
1427 | struct bio *bio; | |
1428 | struct bio *rq_bio, *next_bio = NULL; | |
1429 | bool do_write; | |
1430 | int size, op_size = 0; | |
1431 | u64 ofs; | |
1fec7093 YS |
1432 | int num_segs, cur_seg = 0; |
1433 | struct rbd_req_coll *coll; | |
602adf40 YS |
1434 | |
1435 | /* peek at request from block layer */ | |
1436 | if (!rq) | |
1437 | break; | |
1438 | ||
1439 | dout("fetched request\n"); | |
1440 | ||
1441 | /* filter out block requests we don't understand */ | |
1442 | if ((rq->cmd_type != REQ_TYPE_FS)) { | |
1443 | __blk_end_request_all(rq, 0); | |
1444 | goto next; | |
1445 | } | |
1446 | ||
1447 | /* deduce our operation (read, write) */ | |
1448 | do_write = (rq_data_dir(rq) == WRITE); | |
1449 | ||
1450 | size = blk_rq_bytes(rq); | |
1451 | ofs = blk_rq_pos(rq) * 512ULL; | |
1452 | rq_bio = rq->bio; | |
1453 | if (do_write && rbd_dev->read_only) { | |
1454 | __blk_end_request_all(rq, -EROFS); | |
1455 | goto next; | |
1456 | } | |
1457 | ||
1458 | spin_unlock_irq(q->queue_lock); | |
1459 | ||
1460 | dout("%s 0x%x bytes at 0x%llx\n", | |
1461 | do_write ? "write" : "read", | |
1462 | size, blk_rq_pos(rq) * 512ULL); | |
1463 | ||
1fec7093 YS |
1464 | num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size); |
1465 | coll = rbd_alloc_coll(num_segs); | |
1466 | if (!coll) { | |
1467 | spin_lock_irq(q->queue_lock); | |
1468 | __blk_end_request_all(rq, -ENOMEM); | |
1469 | goto next; | |
1470 | } | |
1471 | ||
602adf40 YS |
1472 | do { |
1473 | /* a bio clone to be passed down to OSD req */ | |
1474 | dout("rq->bio->bi_vcnt=%d\n", rq->bio->bi_vcnt); | |
1475 | op_size = rbd_get_segment(&rbd_dev->header, | |
1476 | rbd_dev->header.block_name, | |
1477 | ofs, size, | |
1478 | NULL, NULL); | |
1fec7093 | 1479 | kref_get(&coll->kref); |
602adf40 YS |
1480 | bio = bio_chain_clone(&rq_bio, &next_bio, &bp, |
1481 | op_size, GFP_ATOMIC); | |
1482 | if (!bio) { | |
1fec7093 YS |
1483 | rbd_coll_end_req_index(rq, coll, cur_seg, |
1484 | -ENOMEM, op_size); | |
1485 | goto next_seg; | |
602adf40 YS |
1486 | } |
1487 | ||
1fec7093 | 1488 | |
602adf40 YS |
1489 | /* init OSD command: write or read */ |
1490 | if (do_write) | |
1491 | rbd_req_write(rq, rbd_dev, | |
1492 | rbd_dev->header.snapc, | |
1493 | ofs, | |
1fec7093 YS |
1494 | op_size, bio, |
1495 | coll, cur_seg); | |
602adf40 YS |
1496 | else |
1497 | rbd_req_read(rq, rbd_dev, | |
1498 | cur_snap_id(rbd_dev), | |
1499 | ofs, | |
1fec7093 YS |
1500 | op_size, bio, |
1501 | coll, cur_seg); | |
602adf40 | 1502 | |
1fec7093 | 1503 | next_seg: |
602adf40 YS |
1504 | size -= op_size; |
1505 | ofs += op_size; | |
1506 | ||
1fec7093 | 1507 | cur_seg++; |
602adf40 YS |
1508 | rq_bio = next_bio; |
1509 | } while (size > 0); | |
1fec7093 | 1510 | kref_put(&coll->kref, rbd_coll_release); |
602adf40 YS |
1511 | |
1512 | if (bp) | |
1513 | bio_pair_release(bp); | |
602adf40 YS |
1514 | spin_lock_irq(q->queue_lock); |
1515 | next: | |
1516 | rq = blk_fetch_request(q); | |
1517 | } | |
1518 | } | |
1519 | ||
1520 | /* | |
1521 | * a queue callback. Makes sure that we don't create a bio that spans across | |
1522 | * multiple osd objects. One exception would be with a single page bios, | |
1523 | * which we handle later at bio_chain_clone | |
1524 | */ | |
1525 | static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd, | |
1526 | struct bio_vec *bvec) | |
1527 | { | |
1528 | struct rbd_device *rbd_dev = q->queuedata; | |
1529 | unsigned int chunk_sectors = 1 << (rbd_dev->header.obj_order - 9); | |
1530 | sector_t sector = bmd->bi_sector + get_start_sect(bmd->bi_bdev); | |
1531 | unsigned int bio_sectors = bmd->bi_size >> 9; | |
1532 | int max; | |
1533 | ||
1534 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) | |
1535 | + bio_sectors)) << 9; | |
1536 | if (max < 0) | |
1537 | max = 0; /* bio_add cannot handle a negative return */ | |
1538 | if (max <= bvec->bv_len && bio_sectors == 0) | |
1539 | return bvec->bv_len; | |
1540 | return max; | |
1541 | } | |
1542 | ||
1543 | static void rbd_free_disk(struct rbd_device *rbd_dev) | |
1544 | { | |
1545 | struct gendisk *disk = rbd_dev->disk; | |
1546 | ||
1547 | if (!disk) | |
1548 | return; | |
1549 | ||
1550 | rbd_header_free(&rbd_dev->header); | |
1551 | ||
1552 | if (disk->flags & GENHD_FL_UP) | |
1553 | del_gendisk(disk); | |
1554 | if (disk->queue) | |
1555 | blk_cleanup_queue(disk->queue); | |
1556 | put_disk(disk); | |
1557 | } | |
1558 | ||
1559 | /* | |
1560 | * reload the ondisk the header | |
1561 | */ | |
1562 | static int rbd_read_header(struct rbd_device *rbd_dev, | |
1563 | struct rbd_image_header *header) | |
1564 | { | |
1565 | ssize_t rc; | |
1566 | struct rbd_image_header_ondisk *dh; | |
1567 | int snap_count = 0; | |
1568 | u64 snap_names_len = 0; | |
59c2be1e | 1569 | u64 ver; |
602adf40 YS |
1570 | |
1571 | while (1) { | |
1572 | int len = sizeof(*dh) + | |
1573 | snap_count * sizeof(struct rbd_image_snap_ondisk) + | |
1574 | snap_names_len; | |
1575 | ||
1576 | rc = -ENOMEM; | |
1577 | dh = kmalloc(len, GFP_KERNEL); | |
1578 | if (!dh) | |
1579 | return -ENOMEM; | |
1580 | ||
1581 | rc = rbd_req_sync_read(rbd_dev, | |
1582 | NULL, CEPH_NOSNAP, | |
1583 | rbd_dev->obj_md_name, | |
1584 | 0, len, | |
59c2be1e | 1585 | (char *)dh, &ver); |
602adf40 YS |
1586 | if (rc < 0) |
1587 | goto out_dh; | |
1588 | ||
1589 | rc = rbd_header_from_disk(header, dh, snap_count, GFP_KERNEL); | |
81e759fb JD |
1590 | if (rc < 0) { |
1591 | if (rc == -ENXIO) { | |
1592 | pr_warning("unrecognized header format" | |
1593 | " for image %s", rbd_dev->obj); | |
1594 | } | |
602adf40 | 1595 | goto out_dh; |
81e759fb | 1596 | } |
602adf40 YS |
1597 | |
1598 | if (snap_count != header->total_snaps) { | |
1599 | snap_count = header->total_snaps; | |
1600 | snap_names_len = header->snap_names_len; | |
1601 | rbd_header_free(header); | |
1602 | kfree(dh); | |
1603 | continue; | |
1604 | } | |
1605 | break; | |
1606 | } | |
59c2be1e | 1607 | header->obj_version = ver; |
602adf40 YS |
1608 | |
1609 | out_dh: | |
1610 | kfree(dh); | |
1611 | return rc; | |
1612 | } | |
1613 | ||
1614 | /* | |
1615 | * create a snapshot | |
1616 | */ | |
1617 | static int rbd_header_add_snap(struct rbd_device *dev, | |
1618 | const char *snap_name, | |
1619 | gfp_t gfp_flags) | |
1620 | { | |
1621 | int name_len = strlen(snap_name); | |
1622 | u64 new_snapid; | |
1623 | int ret; | |
916d4d67 | 1624 | void *data, *p, *e; |
59c2be1e | 1625 | u64 ver; |
1dbb4399 | 1626 | struct ceph_mon_client *monc; |
602adf40 YS |
1627 | |
1628 | /* we should create a snapshot only if we're pointing at the head */ | |
1629 | if (dev->cur_snap) | |
1630 | return -EINVAL; | |
1631 | ||
1dbb4399 AE |
1632 | monc = &dev->rbd_client->client->monc; |
1633 | ret = ceph_monc_create_snapid(monc, dev->poolid, &new_snapid); | |
602adf40 YS |
1634 | dout("created snapid=%lld\n", new_snapid); |
1635 | if (ret < 0) | |
1636 | return ret; | |
1637 | ||
1638 | data = kmalloc(name_len + 16, gfp_flags); | |
1639 | if (!data) | |
1640 | return -ENOMEM; | |
1641 | ||
916d4d67 SW |
1642 | p = data; |
1643 | e = data + name_len + 16; | |
602adf40 | 1644 | |
916d4d67 SW |
1645 | ceph_encode_string_safe(&p, e, snap_name, name_len, bad); |
1646 | ceph_encode_64_safe(&p, e, new_snapid, bad); | |
602adf40 YS |
1647 | |
1648 | ret = rbd_req_sync_exec(dev, dev->obj_md_name, "rbd", "snap_add", | |
916d4d67 | 1649 | data, p - data, &ver); |
602adf40 | 1650 | |
916d4d67 | 1651 | kfree(data); |
602adf40 YS |
1652 | |
1653 | if (ret < 0) | |
1654 | return ret; | |
1655 | ||
1656 | dev->header.snapc->seq = new_snapid; | |
1657 | ||
1658 | return 0; | |
1659 | bad: | |
1660 | return -ERANGE; | |
1661 | } | |
1662 | ||
dfc5606d YS |
1663 | static void __rbd_remove_all_snaps(struct rbd_device *rbd_dev) |
1664 | { | |
1665 | struct rbd_snap *snap; | |
1666 | ||
1667 | while (!list_empty(&rbd_dev->snaps)) { | |
1668 | snap = list_first_entry(&rbd_dev->snaps, struct rbd_snap, node); | |
1669 | __rbd_remove_snap_dev(rbd_dev, snap); | |
1670 | } | |
1671 | } | |
1672 | ||
602adf40 YS |
1673 | /* |
1674 | * only read the first part of the ondisk header, without the snaps info | |
1675 | */ | |
dfc5606d | 1676 | static int __rbd_update_snaps(struct rbd_device *rbd_dev) |
602adf40 YS |
1677 | { |
1678 | int ret; | |
1679 | struct rbd_image_header h; | |
1680 | u64 snap_seq; | |
59c2be1e | 1681 | int follow_seq = 0; |
602adf40 YS |
1682 | |
1683 | ret = rbd_read_header(rbd_dev, &h); | |
1684 | if (ret < 0) | |
1685 | return ret; | |
1686 | ||
9db4b3e3 SW |
1687 | /* resized? */ |
1688 | set_capacity(rbd_dev->disk, h.image_size / 512ULL); | |
1689 | ||
602adf40 YS |
1690 | down_write(&rbd_dev->header.snap_rwsem); |
1691 | ||
1692 | snap_seq = rbd_dev->header.snapc->seq; | |
59c2be1e YS |
1693 | if (rbd_dev->header.total_snaps && |
1694 | rbd_dev->header.snapc->snaps[0] == snap_seq) | |
1695 | /* pointing at the head, will need to follow that | |
1696 | if head moves */ | |
1697 | follow_seq = 1; | |
602adf40 YS |
1698 | |
1699 | kfree(rbd_dev->header.snapc); | |
1700 | kfree(rbd_dev->header.snap_names); | |
1701 | kfree(rbd_dev->header.snap_sizes); | |
1702 | ||
1703 | rbd_dev->header.total_snaps = h.total_snaps; | |
1704 | rbd_dev->header.snapc = h.snapc; | |
1705 | rbd_dev->header.snap_names = h.snap_names; | |
dfc5606d | 1706 | rbd_dev->header.snap_names_len = h.snap_names_len; |
602adf40 | 1707 | rbd_dev->header.snap_sizes = h.snap_sizes; |
59c2be1e YS |
1708 | if (follow_seq) |
1709 | rbd_dev->header.snapc->seq = rbd_dev->header.snapc->snaps[0]; | |
1710 | else | |
1711 | rbd_dev->header.snapc->seq = snap_seq; | |
602adf40 | 1712 | |
dfc5606d YS |
1713 | ret = __rbd_init_snaps_header(rbd_dev); |
1714 | ||
602adf40 YS |
1715 | up_write(&rbd_dev->header.snap_rwsem); |
1716 | ||
dfc5606d | 1717 | return ret; |
602adf40 YS |
1718 | } |
1719 | ||
1720 | static int rbd_init_disk(struct rbd_device *rbd_dev) | |
1721 | { | |
1722 | struct gendisk *disk; | |
1723 | struct request_queue *q; | |
1724 | int rc; | |
1725 | u64 total_size = 0; | |
1726 | ||
1727 | /* contact OSD, request size info about the object being mapped */ | |
1728 | rc = rbd_read_header(rbd_dev, &rbd_dev->header); | |
1729 | if (rc) | |
1730 | return rc; | |
1731 | ||
dfc5606d YS |
1732 | /* no need to lock here, as rbd_dev is not registered yet */ |
1733 | rc = __rbd_init_snaps_header(rbd_dev); | |
1734 | if (rc) | |
1735 | return rc; | |
1736 | ||
cc9d734c | 1737 | rc = rbd_header_set_snap(rbd_dev, &total_size); |
602adf40 YS |
1738 | if (rc) |
1739 | return rc; | |
1740 | ||
1741 | /* create gendisk info */ | |
1742 | rc = -ENOMEM; | |
1743 | disk = alloc_disk(RBD_MINORS_PER_MAJOR); | |
1744 | if (!disk) | |
1745 | goto out; | |
1746 | ||
aedfec59 SW |
1747 | snprintf(disk->disk_name, sizeof(disk->disk_name), DRV_NAME "%d", |
1748 | rbd_dev->id); | |
602adf40 YS |
1749 | disk->major = rbd_dev->major; |
1750 | disk->first_minor = 0; | |
1751 | disk->fops = &rbd_bd_ops; | |
1752 | disk->private_data = rbd_dev; | |
1753 | ||
1754 | /* init rq */ | |
1755 | rc = -ENOMEM; | |
1756 | q = blk_init_queue(rbd_rq_fn, &rbd_dev->lock); | |
1757 | if (!q) | |
1758 | goto out_disk; | |
029bcbd8 JD |
1759 | |
1760 | /* set io sizes to object size */ | |
1761 | blk_queue_max_hw_sectors(q, rbd_obj_bytes(&rbd_dev->header) / 512ULL); | |
1762 | blk_queue_max_segment_size(q, rbd_obj_bytes(&rbd_dev->header)); | |
1763 | blk_queue_io_min(q, rbd_obj_bytes(&rbd_dev->header)); | |
1764 | blk_queue_io_opt(q, rbd_obj_bytes(&rbd_dev->header)); | |
1765 | ||
602adf40 YS |
1766 | blk_queue_merge_bvec(q, rbd_merge_bvec); |
1767 | disk->queue = q; | |
1768 | ||
1769 | q->queuedata = rbd_dev; | |
1770 | ||
1771 | rbd_dev->disk = disk; | |
1772 | rbd_dev->q = q; | |
1773 | ||
1774 | /* finally, announce the disk to the world */ | |
1775 | set_capacity(disk, total_size / 512ULL); | |
1776 | add_disk(disk); | |
1777 | ||
1778 | pr_info("%s: added with size 0x%llx\n", | |
1779 | disk->disk_name, (unsigned long long)total_size); | |
1780 | return 0; | |
1781 | ||
1782 | out_disk: | |
1783 | put_disk(disk); | |
1784 | out: | |
1785 | return rc; | |
1786 | } | |
1787 | ||
dfc5606d YS |
1788 | /* |
1789 | sysfs | |
1790 | */ | |
1791 | ||
1792 | static ssize_t rbd_size_show(struct device *dev, | |
1793 | struct device_attribute *attr, char *buf) | |
1794 | { | |
1795 | struct rbd_device *rbd_dev = dev_to_rbd(dev); | |
1796 | ||
1797 | return sprintf(buf, "%llu\n", (unsigned long long)rbd_dev->header.image_size); | |
1798 | } | |
1799 | ||
1800 | static ssize_t rbd_major_show(struct device *dev, | |
1801 | struct device_attribute *attr, char *buf) | |
1802 | { | |
1803 | struct rbd_device *rbd_dev = dev_to_rbd(dev); | |
602adf40 | 1804 | |
dfc5606d YS |
1805 | return sprintf(buf, "%d\n", rbd_dev->major); |
1806 | } | |
1807 | ||
1808 | static ssize_t rbd_client_id_show(struct device *dev, | |
1809 | struct device_attribute *attr, char *buf) | |
602adf40 | 1810 | { |
dfc5606d YS |
1811 | struct rbd_device *rbd_dev = dev_to_rbd(dev); |
1812 | ||
1dbb4399 AE |
1813 | return sprintf(buf, "client%lld\n", |
1814 | ceph_client_id(rbd_dev->rbd_client->client)); | |
602adf40 YS |
1815 | } |
1816 | ||
dfc5606d YS |
1817 | static ssize_t rbd_pool_show(struct device *dev, |
1818 | struct device_attribute *attr, char *buf) | |
602adf40 | 1819 | { |
dfc5606d YS |
1820 | struct rbd_device *rbd_dev = dev_to_rbd(dev); |
1821 | ||
1822 | return sprintf(buf, "%s\n", rbd_dev->pool_name); | |
1823 | } | |
1824 | ||
1825 | static ssize_t rbd_name_show(struct device *dev, | |
1826 | struct device_attribute *attr, char *buf) | |
1827 | { | |
1828 | struct rbd_device *rbd_dev = dev_to_rbd(dev); | |
1829 | ||
1830 | return sprintf(buf, "%s\n", rbd_dev->obj); | |
1831 | } | |
1832 | ||
1833 | static ssize_t rbd_snap_show(struct device *dev, | |
1834 | struct device_attribute *attr, | |
1835 | char *buf) | |
1836 | { | |
1837 | struct rbd_device *rbd_dev = dev_to_rbd(dev); | |
1838 | ||
1839 | return sprintf(buf, "%s\n", rbd_dev->snap_name); | |
1840 | } | |
1841 | ||
1842 | static ssize_t rbd_image_refresh(struct device *dev, | |
1843 | struct device_attribute *attr, | |
1844 | const char *buf, | |
1845 | size_t size) | |
1846 | { | |
1847 | struct rbd_device *rbd_dev = dev_to_rbd(dev); | |
1848 | int rc; | |
1849 | int ret = size; | |
602adf40 YS |
1850 | |
1851 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); | |
1852 | ||
dfc5606d YS |
1853 | rc = __rbd_update_snaps(rbd_dev); |
1854 | if (rc < 0) | |
1855 | ret = rc; | |
602adf40 | 1856 | |
dfc5606d YS |
1857 | mutex_unlock(&ctl_mutex); |
1858 | return ret; | |
1859 | } | |
602adf40 | 1860 | |
dfc5606d YS |
1861 | static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL); |
1862 | static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL); | |
1863 | static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL); | |
1864 | static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL); | |
1865 | static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL); | |
1866 | static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh); | |
1867 | static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL); | |
1868 | static DEVICE_ATTR(create_snap, S_IWUSR, NULL, rbd_snap_add); | |
dfc5606d YS |
1869 | |
1870 | static struct attribute *rbd_attrs[] = { | |
1871 | &dev_attr_size.attr, | |
1872 | &dev_attr_major.attr, | |
1873 | &dev_attr_client_id.attr, | |
1874 | &dev_attr_pool.attr, | |
1875 | &dev_attr_name.attr, | |
1876 | &dev_attr_current_snap.attr, | |
1877 | &dev_attr_refresh.attr, | |
1878 | &dev_attr_create_snap.attr, | |
dfc5606d YS |
1879 | NULL |
1880 | }; | |
1881 | ||
1882 | static struct attribute_group rbd_attr_group = { | |
1883 | .attrs = rbd_attrs, | |
1884 | }; | |
1885 | ||
1886 | static const struct attribute_group *rbd_attr_groups[] = { | |
1887 | &rbd_attr_group, | |
1888 | NULL | |
1889 | }; | |
1890 | ||
1891 | static void rbd_sysfs_dev_release(struct device *dev) | |
1892 | { | |
1893 | } | |
1894 | ||
1895 | static struct device_type rbd_device_type = { | |
1896 | .name = "rbd", | |
1897 | .groups = rbd_attr_groups, | |
1898 | .release = rbd_sysfs_dev_release, | |
1899 | }; | |
1900 | ||
1901 | ||
1902 | /* | |
1903 | sysfs - snapshots | |
1904 | */ | |
1905 | ||
1906 | static ssize_t rbd_snap_size_show(struct device *dev, | |
1907 | struct device_attribute *attr, | |
1908 | char *buf) | |
1909 | { | |
1910 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); | |
1911 | ||
1912 | return sprintf(buf, "%lld\n", (long long)snap->size); | |
1913 | } | |
1914 | ||
1915 | static ssize_t rbd_snap_id_show(struct device *dev, | |
1916 | struct device_attribute *attr, | |
1917 | char *buf) | |
1918 | { | |
1919 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); | |
1920 | ||
1921 | return sprintf(buf, "%lld\n", (long long)snap->id); | |
1922 | } | |
1923 | ||
1924 | static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL); | |
1925 | static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL); | |
1926 | ||
1927 | static struct attribute *rbd_snap_attrs[] = { | |
1928 | &dev_attr_snap_size.attr, | |
1929 | &dev_attr_snap_id.attr, | |
1930 | NULL, | |
1931 | }; | |
1932 | ||
1933 | static struct attribute_group rbd_snap_attr_group = { | |
1934 | .attrs = rbd_snap_attrs, | |
1935 | }; | |
1936 | ||
1937 | static void rbd_snap_dev_release(struct device *dev) | |
1938 | { | |
1939 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); | |
1940 | kfree(snap->name); | |
1941 | kfree(snap); | |
1942 | } | |
1943 | ||
1944 | static const struct attribute_group *rbd_snap_attr_groups[] = { | |
1945 | &rbd_snap_attr_group, | |
1946 | NULL | |
1947 | }; | |
1948 | ||
1949 | static struct device_type rbd_snap_device_type = { | |
1950 | .groups = rbd_snap_attr_groups, | |
1951 | .release = rbd_snap_dev_release, | |
1952 | }; | |
1953 | ||
1954 | static void __rbd_remove_snap_dev(struct rbd_device *rbd_dev, | |
1955 | struct rbd_snap *snap) | |
1956 | { | |
1957 | list_del(&snap->node); | |
1958 | device_unregister(&snap->dev); | |
1959 | } | |
1960 | ||
1961 | static int rbd_register_snap_dev(struct rbd_device *rbd_dev, | |
1962 | struct rbd_snap *snap, | |
1963 | struct device *parent) | |
1964 | { | |
1965 | struct device *dev = &snap->dev; | |
1966 | int ret; | |
1967 | ||
1968 | dev->type = &rbd_snap_device_type; | |
1969 | dev->parent = parent; | |
1970 | dev->release = rbd_snap_dev_release; | |
1971 | dev_set_name(dev, "snap_%s", snap->name); | |
1972 | ret = device_register(dev); | |
1973 | ||
1974 | return ret; | |
1975 | } | |
1976 | ||
1977 | static int __rbd_add_snap_dev(struct rbd_device *rbd_dev, | |
1978 | int i, const char *name, | |
1979 | struct rbd_snap **snapp) | |
1980 | { | |
1981 | int ret; | |
1982 | struct rbd_snap *snap = kzalloc(sizeof(*snap), GFP_KERNEL); | |
1983 | if (!snap) | |
1984 | return -ENOMEM; | |
1985 | snap->name = kstrdup(name, GFP_KERNEL); | |
1986 | snap->size = rbd_dev->header.snap_sizes[i]; | |
1987 | snap->id = rbd_dev->header.snapc->snaps[i]; | |
1988 | if (device_is_registered(&rbd_dev->dev)) { | |
1989 | ret = rbd_register_snap_dev(rbd_dev, snap, | |
1990 | &rbd_dev->dev); | |
1991 | if (ret < 0) | |
1992 | goto err; | |
1993 | } | |
1994 | *snapp = snap; | |
1995 | return 0; | |
1996 | err: | |
1997 | kfree(snap->name); | |
1998 | kfree(snap); | |
1999 | return ret; | |
2000 | } | |
2001 | ||
2002 | /* | |
2003 | * search for the previous snap in a null delimited string list | |
2004 | */ | |
2005 | const char *rbd_prev_snap_name(const char *name, const char *start) | |
2006 | { | |
2007 | if (name < start + 2) | |
2008 | return NULL; | |
2009 | ||
2010 | name -= 2; | |
2011 | while (*name) { | |
2012 | if (name == start) | |
2013 | return start; | |
2014 | name--; | |
2015 | } | |
2016 | return name + 1; | |
2017 | } | |
2018 | ||
2019 | /* | |
2020 | * compare the old list of snapshots that we have to what's in the header | |
2021 | * and update it accordingly. Note that the header holds the snapshots | |
2022 | * in a reverse order (from newest to oldest) and we need to go from | |
2023 | * older to new so that we don't get a duplicate snap name when | |
2024 | * doing the process (e.g., removed snapshot and recreated a new | |
2025 | * one with the same name. | |
2026 | */ | |
2027 | static int __rbd_init_snaps_header(struct rbd_device *rbd_dev) | |
2028 | { | |
2029 | const char *name, *first_name; | |
2030 | int i = rbd_dev->header.total_snaps; | |
2031 | struct rbd_snap *snap, *old_snap = NULL; | |
2032 | int ret; | |
2033 | struct list_head *p, *n; | |
2034 | ||
2035 | first_name = rbd_dev->header.snap_names; | |
2036 | name = first_name + rbd_dev->header.snap_names_len; | |
2037 | ||
2038 | list_for_each_prev_safe(p, n, &rbd_dev->snaps) { | |
2039 | u64 cur_id; | |
2040 | ||
2041 | old_snap = list_entry(p, struct rbd_snap, node); | |
2042 | ||
2043 | if (i) | |
2044 | cur_id = rbd_dev->header.snapc->snaps[i - 1]; | |
2045 | ||
2046 | if (!i || old_snap->id < cur_id) { | |
2047 | /* old_snap->id was skipped, thus was removed */ | |
2048 | __rbd_remove_snap_dev(rbd_dev, old_snap); | |
2049 | continue; | |
2050 | } | |
2051 | if (old_snap->id == cur_id) { | |
2052 | /* we have this snapshot already */ | |
2053 | i--; | |
2054 | name = rbd_prev_snap_name(name, first_name); | |
2055 | continue; | |
2056 | } | |
2057 | for (; i > 0; | |
2058 | i--, name = rbd_prev_snap_name(name, first_name)) { | |
2059 | if (!name) { | |
2060 | WARN_ON(1); | |
2061 | return -EINVAL; | |
2062 | } | |
2063 | cur_id = rbd_dev->header.snapc->snaps[i]; | |
2064 | /* snapshot removal? handle it above */ | |
2065 | if (cur_id >= old_snap->id) | |
2066 | break; | |
2067 | /* a new snapshot */ | |
2068 | ret = __rbd_add_snap_dev(rbd_dev, i - 1, name, &snap); | |
2069 | if (ret < 0) | |
2070 | return ret; | |
2071 | ||
2072 | /* note that we add it backward so using n and not p */ | |
2073 | list_add(&snap->node, n); | |
2074 | p = &snap->node; | |
2075 | } | |
2076 | } | |
2077 | /* we're done going over the old snap list, just add what's left */ | |
2078 | for (; i > 0; i--) { | |
2079 | name = rbd_prev_snap_name(name, first_name); | |
2080 | if (!name) { | |
2081 | WARN_ON(1); | |
2082 | return -EINVAL; | |
2083 | } | |
2084 | ret = __rbd_add_snap_dev(rbd_dev, i - 1, name, &snap); | |
2085 | if (ret < 0) | |
2086 | return ret; | |
2087 | list_add(&snap->node, &rbd_dev->snaps); | |
2088 | } | |
2089 | ||
2090 | return 0; | |
2091 | } | |
2092 | ||
2093 | ||
2094 | static void rbd_root_dev_release(struct device *dev) | |
2095 | { | |
2096 | } | |
2097 | ||
2098 | static struct device rbd_root_dev = { | |
2099 | .init_name = "rbd", | |
2100 | .release = rbd_root_dev_release, | |
2101 | }; | |
2102 | ||
2103 | static int rbd_bus_add_dev(struct rbd_device *rbd_dev) | |
2104 | { | |
2105 | int ret = -ENOMEM; | |
2106 | struct device *dev; | |
2107 | struct rbd_snap *snap; | |
2108 | ||
2109 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); | |
2110 | dev = &rbd_dev->dev; | |
2111 | ||
2112 | dev->bus = &rbd_bus_type; | |
2113 | dev->type = &rbd_device_type; | |
2114 | dev->parent = &rbd_root_dev; | |
2115 | dev->release = rbd_dev_release; | |
2116 | dev_set_name(dev, "%d", rbd_dev->id); | |
2117 | ret = device_register(dev); | |
2118 | if (ret < 0) | |
2119 | goto done_free; | |
2120 | ||
2121 | list_for_each_entry(snap, &rbd_dev->snaps, node) { | |
2122 | ret = rbd_register_snap_dev(rbd_dev, snap, | |
2123 | &rbd_dev->dev); | |
2124 | if (ret < 0) | |
602adf40 YS |
2125 | break; |
2126 | } | |
2127 | ||
2128 | mutex_unlock(&ctl_mutex); | |
dfc5606d YS |
2129 | return 0; |
2130 | done_free: | |
2131 | mutex_unlock(&ctl_mutex); | |
2132 | return ret; | |
602adf40 YS |
2133 | } |
2134 | ||
dfc5606d YS |
2135 | static void rbd_bus_del_dev(struct rbd_device *rbd_dev) |
2136 | { | |
2137 | device_unregister(&rbd_dev->dev); | |
2138 | } | |
2139 | ||
59c2be1e YS |
2140 | static int rbd_init_watch_dev(struct rbd_device *rbd_dev) |
2141 | { | |
2142 | int ret, rc; | |
2143 | ||
2144 | do { | |
2145 | ret = rbd_req_sync_watch(rbd_dev, rbd_dev->obj_md_name, | |
2146 | rbd_dev->header.obj_version); | |
2147 | if (ret == -ERANGE) { | |
2148 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); | |
2149 | rc = __rbd_update_snaps(rbd_dev); | |
2150 | mutex_unlock(&ctl_mutex); | |
2151 | if (rc < 0) | |
2152 | return rc; | |
2153 | } | |
2154 | } while (ret == -ERANGE); | |
2155 | ||
2156 | return ret; | |
2157 | } | |
2158 | ||
1ddbe94e AE |
2159 | static atomic64_t rbd_id_max = ATOMIC64_INIT(0); |
2160 | ||
2161 | /* | |
499afd5b AE |
2162 | * Get a unique rbd identifier for the given new rbd_dev, and add |
2163 | * the rbd_dev to the global list. The minimum rbd id is 1. | |
1ddbe94e | 2164 | */ |
499afd5b | 2165 | static void rbd_id_get(struct rbd_device *rbd_dev) |
b7f23c36 | 2166 | { |
499afd5b AE |
2167 | rbd_dev->id = atomic64_inc_return(&rbd_id_max); |
2168 | ||
2169 | spin_lock(&rbd_dev_list_lock); | |
2170 | list_add_tail(&rbd_dev->node, &rbd_dev_list); | |
2171 | spin_unlock(&rbd_dev_list_lock); | |
1ddbe94e | 2172 | } |
b7f23c36 | 2173 | |
1ddbe94e | 2174 | /* |
499afd5b AE |
2175 | * Remove an rbd_dev from the global list, and record that its |
2176 | * identifier is no longer in use. | |
1ddbe94e | 2177 | */ |
499afd5b | 2178 | static void rbd_id_put(struct rbd_device *rbd_dev) |
1ddbe94e | 2179 | { |
d184f6bf AE |
2180 | struct list_head *tmp; |
2181 | int rbd_id = rbd_dev->id; | |
2182 | int max_id; | |
2183 | ||
2184 | BUG_ON(rbd_id < 1); | |
499afd5b AE |
2185 | |
2186 | spin_lock(&rbd_dev_list_lock); | |
2187 | list_del_init(&rbd_dev->node); | |
d184f6bf AE |
2188 | |
2189 | /* | |
2190 | * If the id being "put" is not the current maximum, there | |
2191 | * is nothing special we need to do. | |
2192 | */ | |
2193 | if (rbd_id != atomic64_read(&rbd_id_max)) { | |
2194 | spin_unlock(&rbd_dev_list_lock); | |
2195 | return; | |
2196 | } | |
2197 | ||
2198 | /* | |
2199 | * We need to update the current maximum id. Search the | |
2200 | * list to find out what it is. We're more likely to find | |
2201 | * the maximum at the end, so search the list backward. | |
2202 | */ | |
2203 | max_id = 0; | |
2204 | list_for_each_prev(tmp, &rbd_dev_list) { | |
2205 | struct rbd_device *rbd_dev; | |
2206 | ||
2207 | rbd_dev = list_entry(tmp, struct rbd_device, node); | |
2208 | if (rbd_id > max_id) | |
2209 | max_id = rbd_id; | |
2210 | } | |
499afd5b | 2211 | spin_unlock(&rbd_dev_list_lock); |
b7f23c36 | 2212 | |
1ddbe94e | 2213 | /* |
d184f6bf AE |
2214 | * The max id could have been updated by rbd_id_get(), in |
2215 | * which case it now accurately reflects the new maximum. | |
2216 | * Be careful not to overwrite the maximum value in that | |
2217 | * case. | |
1ddbe94e | 2218 | */ |
d184f6bf | 2219 | atomic64_cmpxchg(&rbd_id_max, rbd_id, max_id); |
b7f23c36 AE |
2220 | } |
2221 | ||
59c2be1e YS |
2222 | static ssize_t rbd_add(struct bus_type *bus, |
2223 | const char *buf, | |
2224 | size_t count) | |
602adf40 YS |
2225 | { |
2226 | struct ceph_osd_client *osdc; | |
2227 | struct rbd_device *rbd_dev; | |
2228 | ssize_t rc = -ENOMEM; | |
b7f23c36 | 2229 | int irc; |
602adf40 YS |
2230 | char *mon_dev_name; |
2231 | char *options; | |
2232 | ||
2233 | if (!try_module_get(THIS_MODULE)) | |
2234 | return -ENODEV; | |
2235 | ||
2236 | mon_dev_name = kmalloc(RBD_MAX_OPT_LEN, GFP_KERNEL); | |
2237 | if (!mon_dev_name) | |
2238 | goto err_out_mod; | |
2239 | ||
2240 | options = kmalloc(RBD_MAX_OPT_LEN, GFP_KERNEL); | |
2241 | if (!options) | |
2242 | goto err_mon_dev; | |
2243 | ||
2244 | /* new rbd_device object */ | |
2245 | rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL); | |
2246 | if (!rbd_dev) | |
2247 | goto err_out_opt; | |
2248 | ||
2249 | /* static rbd_device initialization */ | |
2250 | spin_lock_init(&rbd_dev->lock); | |
2251 | INIT_LIST_HEAD(&rbd_dev->node); | |
dfc5606d | 2252 | INIT_LIST_HEAD(&rbd_dev->snaps); |
602adf40 | 2253 | |
0e805a1d AE |
2254 | init_rwsem(&rbd_dev->header.snap_rwsem); |
2255 | ||
d184f6bf | 2256 | /* generate unique id: find highest unique id, add one */ |
499afd5b | 2257 | rbd_id_get(rbd_dev); |
602adf40 YS |
2258 | |
2259 | /* parse add command */ | |
2260 | if (sscanf(buf, "%" __stringify(RBD_MAX_OPT_LEN) "s " | |
2261 | "%" __stringify(RBD_MAX_OPT_LEN) "s " | |
2262 | "%" __stringify(RBD_MAX_POOL_NAME_LEN) "s " | |
2263 | "%" __stringify(RBD_MAX_OBJ_NAME_LEN) "s" | |
2264 | "%" __stringify(RBD_MAX_SNAP_NAME_LEN) "s", | |
2265 | mon_dev_name, options, rbd_dev->pool_name, | |
2266 | rbd_dev->obj, rbd_dev->snap_name) < 4) { | |
2267 | rc = -EINVAL; | |
2268 | goto err_out_slot; | |
2269 | } | |
2270 | ||
2271 | if (rbd_dev->snap_name[0] == 0) | |
cc9d734c JD |
2272 | memcpy(rbd_dev->snap_name, RBD_SNAP_HEAD_NAME, |
2273 | sizeof (RBD_SNAP_HEAD_NAME)); | |
602adf40 YS |
2274 | |
2275 | rbd_dev->obj_len = strlen(rbd_dev->obj); | |
2276 | snprintf(rbd_dev->obj_md_name, sizeof(rbd_dev->obj_md_name), "%s%s", | |
2277 | rbd_dev->obj, RBD_SUFFIX); | |
2278 | ||
2279 | /* initialize rest of new object */ | |
2280 | snprintf(rbd_dev->name, DEV_NAME_LEN, DRV_NAME "%d", rbd_dev->id); | |
e124a82f | 2281 | |
602adf40 YS |
2282 | rc = rbd_get_client(rbd_dev, mon_dev_name, options); |
2283 | if (rc < 0) | |
2284 | goto err_out_slot; | |
2285 | ||
602adf40 | 2286 | /* pick the pool */ |
1dbb4399 | 2287 | osdc = &rbd_dev->rbd_client->client->osdc; |
602adf40 YS |
2288 | rc = ceph_pg_poolid_by_name(osdc->osdmap, rbd_dev->pool_name); |
2289 | if (rc < 0) | |
2290 | goto err_out_client; | |
2291 | rbd_dev->poolid = rc; | |
2292 | ||
2293 | /* register our block device */ | |
2294 | irc = register_blkdev(0, rbd_dev->name); | |
2295 | if (irc < 0) { | |
2296 | rc = irc; | |
2297 | goto err_out_client; | |
2298 | } | |
2299 | rbd_dev->major = irc; | |
2300 | ||
dfc5606d YS |
2301 | rc = rbd_bus_add_dev(rbd_dev); |
2302 | if (rc) | |
766fc439 YS |
2303 | goto err_out_blkdev; |
2304 | ||
602adf40 YS |
2305 | /* set up and announce blkdev mapping */ |
2306 | rc = rbd_init_disk(rbd_dev); | |
2307 | if (rc) | |
766fc439 | 2308 | goto err_out_bus; |
602adf40 | 2309 | |
59c2be1e YS |
2310 | rc = rbd_init_watch_dev(rbd_dev); |
2311 | if (rc) | |
2312 | goto err_out_bus; | |
2313 | ||
602adf40 YS |
2314 | return count; |
2315 | ||
766fc439 | 2316 | err_out_bus: |
499afd5b | 2317 | rbd_id_put(rbd_dev); |
766fc439 YS |
2318 | |
2319 | /* this will also clean up rest of rbd_dev stuff */ | |
2320 | ||
2321 | rbd_bus_del_dev(rbd_dev); | |
2322 | kfree(options); | |
2323 | kfree(mon_dev_name); | |
2324 | return rc; | |
2325 | ||
602adf40 YS |
2326 | err_out_blkdev: |
2327 | unregister_blkdev(rbd_dev->major, rbd_dev->name); | |
2328 | err_out_client: | |
2329 | rbd_put_client(rbd_dev); | |
602adf40 | 2330 | err_out_slot: |
499afd5b | 2331 | rbd_id_put(rbd_dev); |
602adf40 YS |
2332 | |
2333 | kfree(rbd_dev); | |
2334 | err_out_opt: | |
2335 | kfree(options); | |
2336 | err_mon_dev: | |
2337 | kfree(mon_dev_name); | |
2338 | err_out_mod: | |
2339 | dout("Error adding device %s\n", buf); | |
2340 | module_put(THIS_MODULE); | |
2341 | return rc; | |
2342 | } | |
2343 | ||
2344 | static struct rbd_device *__rbd_get_dev(unsigned long id) | |
2345 | { | |
2346 | struct list_head *tmp; | |
2347 | struct rbd_device *rbd_dev; | |
2348 | ||
e124a82f | 2349 | spin_lock(&rbd_dev_list_lock); |
602adf40 YS |
2350 | list_for_each(tmp, &rbd_dev_list) { |
2351 | rbd_dev = list_entry(tmp, struct rbd_device, node); | |
e124a82f AE |
2352 | if (rbd_dev->id == id) { |
2353 | spin_unlock(&rbd_dev_list_lock); | |
602adf40 | 2354 | return rbd_dev; |
e124a82f | 2355 | } |
602adf40 | 2356 | } |
e124a82f | 2357 | spin_unlock(&rbd_dev_list_lock); |
602adf40 YS |
2358 | return NULL; |
2359 | } | |
2360 | ||
dfc5606d | 2361 | static void rbd_dev_release(struct device *dev) |
602adf40 | 2362 | { |
dfc5606d YS |
2363 | struct rbd_device *rbd_dev = |
2364 | container_of(dev, struct rbd_device, dev); | |
602adf40 | 2365 | |
1dbb4399 AE |
2366 | if (rbd_dev->watch_request) { |
2367 | struct ceph_client *client = rbd_dev->rbd_client->client; | |
2368 | ||
2369 | ceph_osdc_unregister_linger_request(&client->osdc, | |
59c2be1e | 2370 | rbd_dev->watch_request); |
1dbb4399 | 2371 | } |
59c2be1e | 2372 | if (rbd_dev->watch_event) |
79e3057c | 2373 | rbd_req_sync_unwatch(rbd_dev, rbd_dev->obj_md_name); |
59c2be1e | 2374 | |
602adf40 YS |
2375 | rbd_put_client(rbd_dev); |
2376 | ||
2377 | /* clean up and free blkdev */ | |
2378 | rbd_free_disk(rbd_dev); | |
2379 | unregister_blkdev(rbd_dev->major, rbd_dev->name); | |
2380 | kfree(rbd_dev); | |
2381 | ||
2382 | /* release module ref */ | |
2383 | module_put(THIS_MODULE); | |
602adf40 YS |
2384 | } |
2385 | ||
dfc5606d YS |
2386 | static ssize_t rbd_remove(struct bus_type *bus, |
2387 | const char *buf, | |
2388 | size_t count) | |
602adf40 YS |
2389 | { |
2390 | struct rbd_device *rbd_dev = NULL; | |
2391 | int target_id, rc; | |
2392 | unsigned long ul; | |
2393 | int ret = count; | |
2394 | ||
2395 | rc = strict_strtoul(buf, 10, &ul); | |
2396 | if (rc) | |
2397 | return rc; | |
2398 | ||
2399 | /* convert to int; abort if we lost anything in the conversion */ | |
2400 | target_id = (int) ul; | |
2401 | if (target_id != ul) | |
2402 | return -EINVAL; | |
2403 | ||
2404 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); | |
2405 | ||
2406 | rbd_dev = __rbd_get_dev(target_id); | |
2407 | if (!rbd_dev) { | |
2408 | ret = -ENOENT; | |
2409 | goto done; | |
2410 | } | |
2411 | ||
499afd5b | 2412 | rbd_id_put(rbd_dev); |
dfc5606d YS |
2413 | |
2414 | __rbd_remove_all_snaps(rbd_dev); | |
2415 | rbd_bus_del_dev(rbd_dev); | |
602adf40 YS |
2416 | |
2417 | done: | |
2418 | mutex_unlock(&ctl_mutex); | |
2419 | return ret; | |
2420 | } | |
2421 | ||
dfc5606d YS |
2422 | static ssize_t rbd_snap_add(struct device *dev, |
2423 | struct device_attribute *attr, | |
2424 | const char *buf, | |
2425 | size_t count) | |
602adf40 | 2426 | { |
dfc5606d YS |
2427 | struct rbd_device *rbd_dev = dev_to_rbd(dev); |
2428 | int ret; | |
2429 | char *name = kmalloc(count + 1, GFP_KERNEL); | |
602adf40 YS |
2430 | if (!name) |
2431 | return -ENOMEM; | |
2432 | ||
dfc5606d | 2433 | snprintf(name, count, "%s", buf); |
602adf40 YS |
2434 | |
2435 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); | |
2436 | ||
602adf40 YS |
2437 | ret = rbd_header_add_snap(rbd_dev, |
2438 | name, GFP_KERNEL); | |
2439 | if (ret < 0) | |
59c2be1e | 2440 | goto err_unlock; |
602adf40 | 2441 | |
dfc5606d | 2442 | ret = __rbd_update_snaps(rbd_dev); |
602adf40 | 2443 | if (ret < 0) |
59c2be1e YS |
2444 | goto err_unlock; |
2445 | ||
2446 | /* shouldn't hold ctl_mutex when notifying.. notify might | |
2447 | trigger a watch callback that would need to get that mutex */ | |
2448 | mutex_unlock(&ctl_mutex); | |
2449 | ||
2450 | /* make a best effort, don't error if failed */ | |
2451 | rbd_req_sync_notify(rbd_dev, rbd_dev->obj_md_name); | |
602adf40 YS |
2452 | |
2453 | ret = count; | |
59c2be1e YS |
2454 | kfree(name); |
2455 | return ret; | |
2456 | ||
2457 | err_unlock: | |
602adf40 | 2458 | mutex_unlock(&ctl_mutex); |
602adf40 YS |
2459 | kfree(name); |
2460 | return ret; | |
2461 | } | |
2462 | ||
dfc5606d YS |
2463 | static struct bus_attribute rbd_bus_attrs[] = { |
2464 | __ATTR(add, S_IWUSR, NULL, rbd_add), | |
2465 | __ATTR(remove, S_IWUSR, NULL, rbd_remove), | |
602adf40 YS |
2466 | __ATTR_NULL |
2467 | }; | |
2468 | ||
2469 | /* | |
2470 | * create control files in sysfs | |
dfc5606d | 2471 | * /sys/bus/rbd/... |
602adf40 YS |
2472 | */ |
2473 | static int rbd_sysfs_init(void) | |
2474 | { | |
dfc5606d | 2475 | int ret; |
602adf40 | 2476 | |
dfc5606d | 2477 | rbd_bus_type.bus_attrs = rbd_bus_attrs; |
602adf40 | 2478 | |
dfc5606d | 2479 | ret = bus_register(&rbd_bus_type); |
21079786 | 2480 | if (ret < 0) |
dfc5606d | 2481 | return ret; |
602adf40 | 2482 | |
dfc5606d | 2483 | ret = device_register(&rbd_root_dev); |
602adf40 | 2484 | |
602adf40 YS |
2485 | return ret; |
2486 | } | |
2487 | ||
2488 | static void rbd_sysfs_cleanup(void) | |
2489 | { | |
dfc5606d YS |
2490 | device_unregister(&rbd_root_dev); |
2491 | bus_unregister(&rbd_bus_type); | |
602adf40 YS |
2492 | } |
2493 | ||
2494 | int __init rbd_init(void) | |
2495 | { | |
2496 | int rc; | |
2497 | ||
2498 | rc = rbd_sysfs_init(); | |
2499 | if (rc) | |
2500 | return rc; | |
602adf40 YS |
2501 | pr_info("loaded " DRV_NAME_LONG "\n"); |
2502 | return 0; | |
2503 | } | |
2504 | ||
2505 | void __exit rbd_exit(void) | |
2506 | { | |
2507 | rbd_sysfs_cleanup(); | |
2508 | } | |
2509 | ||
2510 | module_init(rbd_init); | |
2511 | module_exit(rbd_exit); | |
2512 | ||
2513 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); | |
2514 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); | |
2515 | MODULE_DESCRIPTION("rados block device"); | |
2516 | ||
2517 | /* following authorship retained from original osdblk.c */ | |
2518 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); | |
2519 | ||
2520 | MODULE_LICENSE("GPL"); |