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