]>
Commit | Line | Data |
---|---|---|
602adf40 YS |
1 | /* |
2 | rbd.c -- Export ceph rados objects as a Linux block device | |
3 | ||
4 | ||
5 | based on drivers/block/osdblk.c: | |
6 | ||
7 | Copyright 2009 Red Hat, Inc. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; see the file COPYING. If not, write to | |
20 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | ||
22 | ||
23 | ||
dfc5606d | 24 | For usage instructions, please refer to: |
602adf40 | 25 | |
dfc5606d | 26 | Documentation/ABI/testing/sysfs-bus-rbd |
602adf40 YS |
27 | |
28 | */ | |
29 | ||
30 | #include <linux/ceph/libceph.h> | |
31 | #include <linux/ceph/osd_client.h> | |
32 | #include <linux/ceph/mon_client.h> | |
33 | #include <linux/ceph/decode.h> | |
59c2be1e | 34 | #include <linux/parser.h> |
602adf40 YS |
35 | |
36 | #include <linux/kernel.h> | |
37 | #include <linux/device.h> | |
38 | #include <linux/module.h> | |
39 | #include <linux/fs.h> | |
40 | #include <linux/blkdev.h> | |
41 | ||
42 | #include "rbd_types.h" | |
43 | ||
aafb230e AE |
44 | #define RBD_DEBUG /* Activate rbd_assert() calls */ |
45 | ||
593a9e7b AE |
46 | /* |
47 | * The basic unit of block I/O is a sector. It is interpreted in a | |
48 | * number of contexts in Linux (blk, bio, genhd), but the default is | |
49 | * universally 512 bytes. These symbols are just slightly more | |
50 | * meaningful than the bare numbers they represent. | |
51 | */ | |
52 | #define SECTOR_SHIFT 9 | |
53 | #define SECTOR_SIZE (1ULL << SECTOR_SHIFT) | |
54 | ||
df111be6 AE |
55 | /* It might be useful to have this defined elsewhere too */ |
56 | ||
0ec8ce87 | 57 | #define U32_MAX ((u32) (~0U)) |
df111be6 AE |
58 | #define U64_MAX ((u64) (~0ULL)) |
59 | ||
f0f8cef5 AE |
60 | #define RBD_DRV_NAME "rbd" |
61 | #define RBD_DRV_NAME_LONG "rbd (rados block device)" | |
602adf40 YS |
62 | |
63 | #define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */ | |
64 | ||
d4b125e9 AE |
65 | #define RBD_SNAP_DEV_NAME_PREFIX "snap_" |
66 | #define RBD_MAX_SNAP_NAME_LEN \ | |
67 | (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1)) | |
68 | ||
35d489f9 | 69 | #define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */ |
602adf40 YS |
70 | #define RBD_MAX_OPT_LEN 1024 |
71 | ||
72 | #define RBD_SNAP_HEAD_NAME "-" | |
73 | ||
9e15b77d AE |
74 | /* This allows a single page to hold an image name sent by OSD */ |
75 | #define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1) | |
1e130199 | 76 | #define RBD_IMAGE_ID_LEN_MAX 64 |
9e15b77d | 77 | |
1e130199 | 78 | #define RBD_OBJ_PREFIX_LEN_MAX 64 |
589d30e0 | 79 | |
d889140c AE |
80 | /* Feature bits */ |
81 | ||
82 | #define RBD_FEATURE_LAYERING 1 | |
83 | ||
84 | /* Features supported by this (client software) implementation. */ | |
85 | ||
86 | #define RBD_FEATURES_ALL (0) | |
87 | ||
81a89793 AE |
88 | /* |
89 | * An RBD device name will be "rbd#", where the "rbd" comes from | |
90 | * RBD_DRV_NAME above, and # is a unique integer identifier. | |
91 | * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big | |
92 | * enough to hold all possible device names. | |
93 | */ | |
602adf40 | 94 | #define DEV_NAME_LEN 32 |
81a89793 | 95 | #define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1) |
602adf40 | 96 | |
cc0538b6 | 97 | #define RBD_READ_ONLY_DEFAULT false |
59c2be1e | 98 | |
602adf40 YS |
99 | /* |
100 | * block device image metadata (in-memory version) | |
101 | */ | |
102 | struct rbd_image_header { | |
f84344f3 | 103 | /* These four fields never change for a given rbd image */ |
849b4260 | 104 | char *object_prefix; |
34b13184 | 105 | u64 features; |
602adf40 YS |
106 | __u8 obj_order; |
107 | __u8 crypt_type; | |
108 | __u8 comp_type; | |
602adf40 | 109 | |
f84344f3 AE |
110 | /* The remaining fields need to be updated occasionally */ |
111 | u64 image_size; | |
112 | struct ceph_snap_context *snapc; | |
602adf40 YS |
113 | char *snap_names; |
114 | u64 *snap_sizes; | |
59c2be1e YS |
115 | |
116 | u64 obj_version; | |
117 | }; | |
118 | ||
0d7dbfce AE |
119 | /* |
120 | * An rbd image specification. | |
121 | * | |
122 | * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely | |
c66c6e0c AE |
123 | * identify an image. Each rbd_dev structure includes a pointer to |
124 | * an rbd_spec structure that encapsulates this identity. | |
125 | * | |
126 | * Each of the id's in an rbd_spec has an associated name. For a | |
127 | * user-mapped image, the names are supplied and the id's associated | |
128 | * with them are looked up. For a layered image, a parent image is | |
129 | * defined by the tuple, and the names are looked up. | |
130 | * | |
131 | * An rbd_dev structure contains a parent_spec pointer which is | |
132 | * non-null if the image it represents is a child in a layered | |
133 | * image. This pointer will refer to the rbd_spec structure used | |
134 | * by the parent rbd_dev for its own identity (i.e., the structure | |
135 | * is shared between the parent and child). | |
136 | * | |
137 | * Since these structures are populated once, during the discovery | |
138 | * phase of image construction, they are effectively immutable so | |
139 | * we make no effort to synchronize access to them. | |
140 | * | |
141 | * Note that code herein does not assume the image name is known (it | |
142 | * could be a null pointer). | |
0d7dbfce AE |
143 | */ |
144 | struct rbd_spec { | |
145 | u64 pool_id; | |
146 | char *pool_name; | |
147 | ||
148 | char *image_id; | |
0d7dbfce | 149 | char *image_name; |
0d7dbfce AE |
150 | |
151 | u64 snap_id; | |
152 | char *snap_name; | |
153 | ||
154 | struct kref kref; | |
155 | }; | |
156 | ||
59c2be1e | 157 | struct rbd_options { |
cc0538b6 | 158 | bool read_only; |
602adf40 YS |
159 | }; |
160 | ||
161 | /* | |
f0f8cef5 | 162 | * an instance of the client. multiple devices may share an rbd client. |
602adf40 YS |
163 | */ |
164 | struct rbd_client { | |
165 | struct ceph_client *client; | |
166 | struct kref kref; | |
167 | struct list_head node; | |
168 | }; | |
169 | ||
170 | /* | |
f0f8cef5 | 171 | * a request completion status |
602adf40 | 172 | */ |
1fec7093 YS |
173 | struct rbd_req_status { |
174 | int done; | |
8986cb37 | 175 | s32 rc; |
1fec7093 YS |
176 | u64 bytes; |
177 | }; | |
178 | ||
179 | /* | |
180 | * a collection of requests | |
181 | */ | |
182 | struct rbd_req_coll { | |
183 | int total; | |
184 | int num_done; | |
185 | struct kref kref; | |
186 | struct rbd_req_status status[0]; | |
602adf40 YS |
187 | }; |
188 | ||
f0f8cef5 AE |
189 | /* |
190 | * a single io request | |
191 | */ | |
192 | struct rbd_request { | |
193 | struct request *rq; /* blk layer request */ | |
194 | struct bio *bio; /* cloned bio */ | |
195 | struct page **pages; /* list of used pages */ | |
196 | u64 len; | |
197 | int coll_index; | |
198 | struct rbd_req_coll *coll; | |
199 | }; | |
200 | ||
dfc5606d YS |
201 | struct rbd_snap { |
202 | struct device dev; | |
203 | const char *name; | |
3591538f | 204 | u64 size; |
dfc5606d YS |
205 | struct list_head node; |
206 | u64 id; | |
34b13184 | 207 | u64 features; |
dfc5606d YS |
208 | }; |
209 | ||
f84344f3 | 210 | struct rbd_mapping { |
99c1f08f | 211 | u64 size; |
34b13184 | 212 | u64 features; |
f84344f3 AE |
213 | bool read_only; |
214 | }; | |
215 | ||
602adf40 YS |
216 | /* |
217 | * a single device | |
218 | */ | |
219 | struct rbd_device { | |
de71a297 | 220 | int dev_id; /* blkdev unique id */ |
602adf40 YS |
221 | |
222 | int major; /* blkdev assigned major */ | |
223 | struct gendisk *disk; /* blkdev's gendisk and rq */ | |
602adf40 | 224 | |
a30b71b9 | 225 | u32 image_format; /* Either 1 or 2 */ |
602adf40 YS |
226 | struct rbd_client *rbd_client; |
227 | ||
228 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ | |
229 | ||
230 | spinlock_t lock; /* queue lock */ | |
231 | ||
232 | struct rbd_image_header header; | |
d78b650a | 233 | atomic_t exists; |
0d7dbfce | 234 | struct rbd_spec *spec; |
602adf40 | 235 | |
0d7dbfce | 236 | char *header_name; |
971f839a | 237 | |
59c2be1e YS |
238 | struct ceph_osd_event *watch_event; |
239 | struct ceph_osd_request *watch_request; | |
240 | ||
86b00e0d AE |
241 | struct rbd_spec *parent_spec; |
242 | u64 parent_overlap; | |
243 | ||
c666601a JD |
244 | /* protects updating the header */ |
245 | struct rw_semaphore header_rwsem; | |
f84344f3 AE |
246 | |
247 | struct rbd_mapping mapping; | |
602adf40 YS |
248 | |
249 | struct list_head node; | |
dfc5606d YS |
250 | |
251 | /* list of snapshots */ | |
252 | struct list_head snaps; | |
253 | ||
254 | /* sysfs related */ | |
255 | struct device dev; | |
42382b70 | 256 | unsigned long open_count; |
dfc5606d YS |
257 | }; |
258 | ||
602adf40 | 259 | static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */ |
e124a82f | 260 | |
602adf40 | 261 | static LIST_HEAD(rbd_dev_list); /* devices */ |
e124a82f AE |
262 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
263 | ||
432b8587 AE |
264 | static LIST_HEAD(rbd_client_list); /* clients */ |
265 | static DEFINE_SPINLOCK(rbd_client_list_lock); | |
602adf40 | 266 | |
304f6808 AE |
267 | static int rbd_dev_snaps_update(struct rbd_device *rbd_dev); |
268 | static int rbd_dev_snaps_register(struct rbd_device *rbd_dev); | |
269 | ||
dfc5606d | 270 | static void rbd_dev_release(struct device *dev); |
41f38c2b | 271 | static void rbd_remove_snap_dev(struct rbd_snap *snap); |
dfc5606d | 272 | |
f0f8cef5 AE |
273 | static ssize_t rbd_add(struct bus_type *bus, const char *buf, |
274 | size_t count); | |
275 | static ssize_t rbd_remove(struct bus_type *bus, const char *buf, | |
276 | size_t count); | |
277 | ||
278 | static struct bus_attribute rbd_bus_attrs[] = { | |
279 | __ATTR(add, S_IWUSR, NULL, rbd_add), | |
280 | __ATTR(remove, S_IWUSR, NULL, rbd_remove), | |
281 | __ATTR_NULL | |
282 | }; | |
283 | ||
284 | static struct bus_type rbd_bus_type = { | |
285 | .name = "rbd", | |
286 | .bus_attrs = rbd_bus_attrs, | |
287 | }; | |
288 | ||
289 | static void rbd_root_dev_release(struct device *dev) | |
290 | { | |
291 | } | |
292 | ||
293 | static struct device rbd_root_dev = { | |
294 | .init_name = "rbd", | |
295 | .release = rbd_root_dev_release, | |
296 | }; | |
297 | ||
06ecc6cb AE |
298 | static __printf(2, 3) |
299 | void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...) | |
300 | { | |
301 | struct va_format vaf; | |
302 | va_list args; | |
303 | ||
304 | va_start(args, fmt); | |
305 | vaf.fmt = fmt; | |
306 | vaf.va = &args; | |
307 | ||
308 | if (!rbd_dev) | |
309 | printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf); | |
310 | else if (rbd_dev->disk) | |
311 | printk(KERN_WARNING "%s: %s: %pV\n", | |
312 | RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf); | |
313 | else if (rbd_dev->spec && rbd_dev->spec->image_name) | |
314 | printk(KERN_WARNING "%s: image %s: %pV\n", | |
315 | RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf); | |
316 | else if (rbd_dev->spec && rbd_dev->spec->image_id) | |
317 | printk(KERN_WARNING "%s: id %s: %pV\n", | |
318 | RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf); | |
319 | else /* punt */ | |
320 | printk(KERN_WARNING "%s: rbd_dev %p: %pV\n", | |
321 | RBD_DRV_NAME, rbd_dev, &vaf); | |
322 | va_end(args); | |
323 | } | |
324 | ||
aafb230e AE |
325 | #ifdef RBD_DEBUG |
326 | #define rbd_assert(expr) \ | |
327 | if (unlikely(!(expr))) { \ | |
328 | printk(KERN_ERR "\nAssertion failure in %s() " \ | |
329 | "at line %d:\n\n" \ | |
330 | "\trbd_assert(%s);\n\n", \ | |
331 | __func__, __LINE__, #expr); \ | |
332 | BUG(); \ | |
333 | } | |
334 | #else /* !RBD_DEBUG */ | |
335 | # define rbd_assert(expr) ((void) 0) | |
336 | #endif /* !RBD_DEBUG */ | |
dfc5606d | 337 | |
117973fb AE |
338 | static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver); |
339 | static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver); | |
59c2be1e | 340 | |
602adf40 YS |
341 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
342 | { | |
f0f8cef5 | 343 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
602adf40 | 344 | |
f84344f3 | 345 | if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only) |
602adf40 YS |
346 | return -EROFS; |
347 | ||
42382b70 | 348 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
c3e946ce | 349 | (void) get_device(&rbd_dev->dev); |
f84344f3 | 350 | set_device_ro(bdev, rbd_dev->mapping.read_only); |
42382b70 AE |
351 | rbd_dev->open_count++; |
352 | mutex_unlock(&ctl_mutex); | |
340c7a2b | 353 | |
602adf40 YS |
354 | return 0; |
355 | } | |
356 | ||
dfc5606d YS |
357 | static int rbd_release(struct gendisk *disk, fmode_t mode) |
358 | { | |
359 | struct rbd_device *rbd_dev = disk->private_data; | |
360 | ||
42382b70 AE |
361 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
362 | rbd_assert(rbd_dev->open_count > 0); | |
363 | rbd_dev->open_count--; | |
c3e946ce | 364 | put_device(&rbd_dev->dev); |
42382b70 | 365 | mutex_unlock(&ctl_mutex); |
dfc5606d YS |
366 | |
367 | return 0; | |
368 | } | |
369 | ||
602adf40 YS |
370 | static const struct block_device_operations rbd_bd_ops = { |
371 | .owner = THIS_MODULE, | |
372 | .open = rbd_open, | |
dfc5606d | 373 | .release = rbd_release, |
602adf40 YS |
374 | }; |
375 | ||
376 | /* | |
377 | * Initialize an rbd client instance. | |
43ae4701 | 378 | * We own *ceph_opts. |
602adf40 | 379 | */ |
f8c38929 | 380 | static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts) |
602adf40 YS |
381 | { |
382 | struct rbd_client *rbdc; | |
383 | int ret = -ENOMEM; | |
384 | ||
385 | dout("rbd_client_create\n"); | |
386 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); | |
387 | if (!rbdc) | |
388 | goto out_opt; | |
389 | ||
390 | kref_init(&rbdc->kref); | |
391 | INIT_LIST_HEAD(&rbdc->node); | |
392 | ||
bc534d86 AE |
393 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
394 | ||
43ae4701 | 395 | rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0); |
602adf40 | 396 | if (IS_ERR(rbdc->client)) |
bc534d86 | 397 | goto out_mutex; |
43ae4701 | 398 | ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */ |
602adf40 YS |
399 | |
400 | ret = ceph_open_session(rbdc->client); | |
401 | if (ret < 0) | |
402 | goto out_err; | |
403 | ||
432b8587 | 404 | spin_lock(&rbd_client_list_lock); |
602adf40 | 405 | list_add_tail(&rbdc->node, &rbd_client_list); |
432b8587 | 406 | spin_unlock(&rbd_client_list_lock); |
602adf40 | 407 | |
bc534d86 AE |
408 | mutex_unlock(&ctl_mutex); |
409 | ||
602adf40 YS |
410 | dout("rbd_client_create created %p\n", rbdc); |
411 | return rbdc; | |
412 | ||
413 | out_err: | |
414 | ceph_destroy_client(rbdc->client); | |
bc534d86 AE |
415 | out_mutex: |
416 | mutex_unlock(&ctl_mutex); | |
602adf40 YS |
417 | kfree(rbdc); |
418 | out_opt: | |
43ae4701 AE |
419 | if (ceph_opts) |
420 | ceph_destroy_options(ceph_opts); | |
28f259b7 | 421 | return ERR_PTR(ret); |
602adf40 YS |
422 | } |
423 | ||
424 | /* | |
1f7ba331 AE |
425 | * Find a ceph client with specific addr and configuration. If |
426 | * found, bump its reference count. | |
602adf40 | 427 | */ |
1f7ba331 | 428 | static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts) |
602adf40 YS |
429 | { |
430 | struct rbd_client *client_node; | |
1f7ba331 | 431 | bool found = false; |
602adf40 | 432 | |
43ae4701 | 433 | if (ceph_opts->flags & CEPH_OPT_NOSHARE) |
602adf40 YS |
434 | return NULL; |
435 | ||
1f7ba331 AE |
436 | spin_lock(&rbd_client_list_lock); |
437 | list_for_each_entry(client_node, &rbd_client_list, node) { | |
438 | if (!ceph_compare_options(ceph_opts, client_node->client)) { | |
439 | kref_get(&client_node->kref); | |
440 | found = true; | |
441 | break; | |
442 | } | |
443 | } | |
444 | spin_unlock(&rbd_client_list_lock); | |
445 | ||
446 | return found ? client_node : NULL; | |
602adf40 YS |
447 | } |
448 | ||
59c2be1e YS |
449 | /* |
450 | * mount options | |
451 | */ | |
452 | enum { | |
59c2be1e YS |
453 | Opt_last_int, |
454 | /* int args above */ | |
455 | Opt_last_string, | |
456 | /* string args above */ | |
cc0538b6 AE |
457 | Opt_read_only, |
458 | Opt_read_write, | |
459 | /* Boolean args above */ | |
460 | Opt_last_bool, | |
59c2be1e YS |
461 | }; |
462 | ||
43ae4701 | 463 | static match_table_t rbd_opts_tokens = { |
59c2be1e YS |
464 | /* int args above */ |
465 | /* string args above */ | |
be466c1c | 466 | {Opt_read_only, "read_only"}, |
cc0538b6 AE |
467 | {Opt_read_only, "ro"}, /* Alternate spelling */ |
468 | {Opt_read_write, "read_write"}, | |
469 | {Opt_read_write, "rw"}, /* Alternate spelling */ | |
470 | /* Boolean args above */ | |
59c2be1e YS |
471 | {-1, NULL} |
472 | }; | |
473 | ||
474 | static int parse_rbd_opts_token(char *c, void *private) | |
475 | { | |
43ae4701 | 476 | struct rbd_options *rbd_opts = private; |
59c2be1e YS |
477 | substring_t argstr[MAX_OPT_ARGS]; |
478 | int token, intval, ret; | |
479 | ||
43ae4701 | 480 | token = match_token(c, rbd_opts_tokens, argstr); |
59c2be1e YS |
481 | if (token < 0) |
482 | return -EINVAL; | |
483 | ||
484 | if (token < Opt_last_int) { | |
485 | ret = match_int(&argstr[0], &intval); | |
486 | if (ret < 0) { | |
487 | pr_err("bad mount option arg (not int) " | |
488 | "at '%s'\n", c); | |
489 | return ret; | |
490 | } | |
491 | dout("got int token %d val %d\n", token, intval); | |
492 | } else if (token > Opt_last_int && token < Opt_last_string) { | |
493 | dout("got string token %d val %s\n", token, | |
494 | argstr[0].from); | |
cc0538b6 AE |
495 | } else if (token > Opt_last_string && token < Opt_last_bool) { |
496 | dout("got Boolean token %d\n", token); | |
59c2be1e YS |
497 | } else { |
498 | dout("got token %d\n", token); | |
499 | } | |
500 | ||
501 | switch (token) { | |
cc0538b6 AE |
502 | case Opt_read_only: |
503 | rbd_opts->read_only = true; | |
504 | break; | |
505 | case Opt_read_write: | |
506 | rbd_opts->read_only = false; | |
507 | break; | |
59c2be1e | 508 | default: |
aafb230e AE |
509 | rbd_assert(false); |
510 | break; | |
59c2be1e YS |
511 | } |
512 | return 0; | |
513 | } | |
514 | ||
602adf40 YS |
515 | /* |
516 | * Get a ceph client with specific addr and configuration, if one does | |
517 | * not exist create it. | |
518 | */ | |
9d3997fd | 519 | static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts) |
602adf40 | 520 | { |
f8c38929 | 521 | struct rbd_client *rbdc; |
59c2be1e | 522 | |
1f7ba331 | 523 | rbdc = rbd_client_find(ceph_opts); |
9d3997fd | 524 | if (rbdc) /* using an existing client */ |
43ae4701 | 525 | ceph_destroy_options(ceph_opts); |
9d3997fd | 526 | else |
f8c38929 | 527 | rbdc = rbd_client_create(ceph_opts); |
602adf40 | 528 | |
9d3997fd | 529 | return rbdc; |
602adf40 YS |
530 | } |
531 | ||
532 | /* | |
533 | * Destroy ceph client | |
d23a4b3f | 534 | * |
432b8587 | 535 | * Caller must hold rbd_client_list_lock. |
602adf40 YS |
536 | */ |
537 | static void rbd_client_release(struct kref *kref) | |
538 | { | |
539 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); | |
540 | ||
541 | dout("rbd_release_client %p\n", rbdc); | |
cd9d9f5d | 542 | spin_lock(&rbd_client_list_lock); |
602adf40 | 543 | list_del(&rbdc->node); |
cd9d9f5d | 544 | spin_unlock(&rbd_client_list_lock); |
602adf40 YS |
545 | |
546 | ceph_destroy_client(rbdc->client); | |
547 | kfree(rbdc); | |
548 | } | |
549 | ||
550 | /* | |
551 | * Drop reference to ceph client node. If it's not referenced anymore, release | |
552 | * it. | |
553 | */ | |
9d3997fd | 554 | static void rbd_put_client(struct rbd_client *rbdc) |
602adf40 | 555 | { |
c53d5893 AE |
556 | if (rbdc) |
557 | kref_put(&rbdc->kref, rbd_client_release); | |
602adf40 YS |
558 | } |
559 | ||
1fec7093 YS |
560 | /* |
561 | * Destroy requests collection | |
562 | */ | |
563 | static void rbd_coll_release(struct kref *kref) | |
564 | { | |
565 | struct rbd_req_coll *coll = | |
566 | container_of(kref, struct rbd_req_coll, kref); | |
567 | ||
568 | dout("rbd_coll_release %p\n", coll); | |
569 | kfree(coll); | |
570 | } | |
602adf40 | 571 | |
a30b71b9 AE |
572 | static bool rbd_image_format_valid(u32 image_format) |
573 | { | |
574 | return image_format == 1 || image_format == 2; | |
575 | } | |
576 | ||
8e94af8e AE |
577 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
578 | { | |
103a150f AE |
579 | size_t size; |
580 | u32 snap_count; | |
581 | ||
582 | /* The header has to start with the magic rbd header text */ | |
583 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) | |
584 | return false; | |
585 | ||
db2388b6 AE |
586 | /* The bio layer requires at least sector-sized I/O */ |
587 | ||
588 | if (ondisk->options.order < SECTOR_SHIFT) | |
589 | return false; | |
590 | ||
591 | /* If we use u64 in a few spots we may be able to loosen this */ | |
592 | ||
593 | if (ondisk->options.order > 8 * sizeof (int) - 1) | |
594 | return false; | |
595 | ||
103a150f AE |
596 | /* |
597 | * The size of a snapshot header has to fit in a size_t, and | |
598 | * that limits the number of snapshots. | |
599 | */ | |
600 | snap_count = le32_to_cpu(ondisk->snap_count); | |
601 | size = SIZE_MAX - sizeof (struct ceph_snap_context); | |
602 | if (snap_count > size / sizeof (__le64)) | |
603 | return false; | |
604 | ||
605 | /* | |
606 | * Not only that, but the size of the entire the snapshot | |
607 | * header must also be representable in a size_t. | |
608 | */ | |
609 | size -= snap_count * sizeof (__le64); | |
610 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) | |
611 | return false; | |
612 | ||
613 | return true; | |
8e94af8e AE |
614 | } |
615 | ||
602adf40 YS |
616 | /* |
617 | * Create a new header structure, translate header format from the on-disk | |
618 | * header. | |
619 | */ | |
620 | static int rbd_header_from_disk(struct rbd_image_header *header, | |
4156d998 | 621 | struct rbd_image_header_ondisk *ondisk) |
602adf40 | 622 | { |
ccece235 | 623 | u32 snap_count; |
58c17b0e | 624 | size_t len; |
d2bb24e5 | 625 | size_t size; |
621901d6 | 626 | u32 i; |
602adf40 | 627 | |
6a52325f AE |
628 | memset(header, 0, sizeof (*header)); |
629 | ||
103a150f AE |
630 | snap_count = le32_to_cpu(ondisk->snap_count); |
631 | ||
58c17b0e AE |
632 | len = strnlen(ondisk->object_prefix, sizeof (ondisk->object_prefix)); |
633 | header->object_prefix = kmalloc(len + 1, GFP_KERNEL); | |
6a52325f | 634 | if (!header->object_prefix) |
602adf40 | 635 | return -ENOMEM; |
58c17b0e AE |
636 | memcpy(header->object_prefix, ondisk->object_prefix, len); |
637 | header->object_prefix[len] = '\0'; | |
00f1f36f | 638 | |
602adf40 | 639 | if (snap_count) { |
f785cc1d AE |
640 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
641 | ||
621901d6 AE |
642 | /* Save a copy of the snapshot names */ |
643 | ||
f785cc1d AE |
644 | if (snap_names_len > (u64) SIZE_MAX) |
645 | return -EIO; | |
646 | header->snap_names = kmalloc(snap_names_len, GFP_KERNEL); | |
602adf40 | 647 | if (!header->snap_names) |
6a52325f | 648 | goto out_err; |
f785cc1d AE |
649 | /* |
650 | * Note that rbd_dev_v1_header_read() guarantees | |
651 | * the ondisk buffer we're working with has | |
652 | * snap_names_len bytes beyond the end of the | |
653 | * snapshot id array, this memcpy() is safe. | |
654 | */ | |
655 | memcpy(header->snap_names, &ondisk->snaps[snap_count], | |
656 | snap_names_len); | |
6a52325f | 657 | |
621901d6 AE |
658 | /* Record each snapshot's size */ |
659 | ||
d2bb24e5 AE |
660 | size = snap_count * sizeof (*header->snap_sizes); |
661 | header->snap_sizes = kmalloc(size, GFP_KERNEL); | |
602adf40 | 662 | if (!header->snap_sizes) |
6a52325f | 663 | goto out_err; |
621901d6 AE |
664 | for (i = 0; i < snap_count; i++) |
665 | header->snap_sizes[i] = | |
666 | le64_to_cpu(ondisk->snaps[i].image_size); | |
602adf40 | 667 | } else { |
ccece235 | 668 | WARN_ON(ondisk->snap_names_len); |
602adf40 YS |
669 | header->snap_names = NULL; |
670 | header->snap_sizes = NULL; | |
671 | } | |
849b4260 | 672 | |
34b13184 | 673 | header->features = 0; /* No features support in v1 images */ |
602adf40 YS |
674 | header->obj_order = ondisk->options.order; |
675 | header->crypt_type = ondisk->options.crypt_type; | |
676 | header->comp_type = ondisk->options.comp_type; | |
6a52325f | 677 | |
621901d6 AE |
678 | /* Allocate and fill in the snapshot context */ |
679 | ||
f84344f3 | 680 | header->image_size = le64_to_cpu(ondisk->image_size); |
6a52325f AE |
681 | size = sizeof (struct ceph_snap_context); |
682 | size += snap_count * sizeof (header->snapc->snaps[0]); | |
683 | header->snapc = kzalloc(size, GFP_KERNEL); | |
684 | if (!header->snapc) | |
685 | goto out_err; | |
602adf40 YS |
686 | |
687 | atomic_set(&header->snapc->nref, 1); | |
505cbb9b | 688 | header->snapc->seq = le64_to_cpu(ondisk->snap_seq); |
602adf40 | 689 | header->snapc->num_snaps = snap_count; |
621901d6 AE |
690 | for (i = 0; i < snap_count; i++) |
691 | header->snapc->snaps[i] = | |
692 | le64_to_cpu(ondisk->snaps[i].id); | |
602adf40 YS |
693 | |
694 | return 0; | |
695 | ||
6a52325f | 696 | out_err: |
849b4260 | 697 | kfree(header->snap_sizes); |
ccece235 | 698 | header->snap_sizes = NULL; |
602adf40 | 699 | kfree(header->snap_names); |
ccece235 | 700 | header->snap_names = NULL; |
6a52325f AE |
701 | kfree(header->object_prefix); |
702 | header->object_prefix = NULL; | |
ccece235 | 703 | |
00f1f36f | 704 | return -ENOMEM; |
602adf40 YS |
705 | } |
706 | ||
9e15b77d AE |
707 | static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id) |
708 | { | |
709 | struct rbd_snap *snap; | |
710 | ||
711 | if (snap_id == CEPH_NOSNAP) | |
712 | return RBD_SNAP_HEAD_NAME; | |
713 | ||
714 | list_for_each_entry(snap, &rbd_dev->snaps, node) | |
715 | if (snap_id == snap->id) | |
716 | return snap->name; | |
717 | ||
718 | return NULL; | |
719 | } | |
720 | ||
8836b995 | 721 | static int snap_by_name(struct rbd_device *rbd_dev, const char *snap_name) |
602adf40 | 722 | { |
602adf40 | 723 | |
e86924a8 | 724 | struct rbd_snap *snap; |
602adf40 | 725 | |
e86924a8 AE |
726 | list_for_each_entry(snap, &rbd_dev->snaps, node) { |
727 | if (!strcmp(snap_name, snap->name)) { | |
0d7dbfce | 728 | rbd_dev->spec->snap_id = snap->id; |
e86924a8 | 729 | rbd_dev->mapping.size = snap->size; |
34b13184 | 730 | rbd_dev->mapping.features = snap->features; |
602adf40 | 731 | |
e86924a8 | 732 | return 0; |
00f1f36f | 733 | } |
00f1f36f | 734 | } |
e86924a8 | 735 | |
00f1f36f | 736 | return -ENOENT; |
602adf40 YS |
737 | } |
738 | ||
819d52bf | 739 | static int rbd_dev_set_mapping(struct rbd_device *rbd_dev) |
602adf40 | 740 | { |
78dc447d | 741 | int ret; |
602adf40 | 742 | |
0d7dbfce | 743 | if (!memcmp(rbd_dev->spec->snap_name, RBD_SNAP_HEAD_NAME, |
cc9d734c | 744 | sizeof (RBD_SNAP_HEAD_NAME))) { |
0d7dbfce | 745 | rbd_dev->spec->snap_id = CEPH_NOSNAP; |
99c1f08f | 746 | rbd_dev->mapping.size = rbd_dev->header.image_size; |
34b13184 | 747 | rbd_dev->mapping.features = rbd_dev->header.features; |
e86924a8 | 748 | ret = 0; |
602adf40 | 749 | } else { |
0d7dbfce | 750 | ret = snap_by_name(rbd_dev, rbd_dev->spec->snap_name); |
602adf40 YS |
751 | if (ret < 0) |
752 | goto done; | |
f84344f3 | 753 | rbd_dev->mapping.read_only = true; |
602adf40 | 754 | } |
d78b650a | 755 | atomic_set(&rbd_dev->exists, 1); |
602adf40 | 756 | done: |
602adf40 YS |
757 | return ret; |
758 | } | |
759 | ||
760 | static void rbd_header_free(struct rbd_image_header *header) | |
761 | { | |
849b4260 | 762 | kfree(header->object_prefix); |
d78fd7ae | 763 | header->object_prefix = NULL; |
602adf40 | 764 | kfree(header->snap_sizes); |
d78fd7ae | 765 | header->snap_sizes = NULL; |
849b4260 | 766 | kfree(header->snap_names); |
d78fd7ae | 767 | header->snap_names = NULL; |
d1d25646 | 768 | ceph_put_snap_context(header->snapc); |
d78fd7ae | 769 | header->snapc = NULL; |
602adf40 YS |
770 | } |
771 | ||
65ccfe21 | 772 | static char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset) |
602adf40 | 773 | { |
65ccfe21 AE |
774 | char *name; |
775 | u64 segment; | |
776 | int ret; | |
602adf40 | 777 | |
2fd82b9e | 778 | name = kmalloc(MAX_OBJ_NAME_SIZE + 1, GFP_NOIO); |
65ccfe21 AE |
779 | if (!name) |
780 | return NULL; | |
781 | segment = offset >> rbd_dev->header.obj_order; | |
2fd82b9e | 782 | ret = snprintf(name, MAX_OBJ_NAME_SIZE + 1, "%s.%012llx", |
65ccfe21 | 783 | rbd_dev->header.object_prefix, segment); |
2fd82b9e | 784 | if (ret < 0 || ret > MAX_OBJ_NAME_SIZE) { |
65ccfe21 AE |
785 | pr_err("error formatting segment name for #%llu (%d)\n", |
786 | segment, ret); | |
787 | kfree(name); | |
788 | name = NULL; | |
789 | } | |
602adf40 | 790 | |
65ccfe21 AE |
791 | return name; |
792 | } | |
602adf40 | 793 | |
65ccfe21 AE |
794 | static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset) |
795 | { | |
796 | u64 segment_size = (u64) 1 << rbd_dev->header.obj_order; | |
602adf40 | 797 | |
65ccfe21 AE |
798 | return offset & (segment_size - 1); |
799 | } | |
800 | ||
801 | static u64 rbd_segment_length(struct rbd_device *rbd_dev, | |
802 | u64 offset, u64 length) | |
803 | { | |
804 | u64 segment_size = (u64) 1 << rbd_dev->header.obj_order; | |
805 | ||
806 | offset &= segment_size - 1; | |
807 | ||
aafb230e | 808 | rbd_assert(length <= U64_MAX - offset); |
65ccfe21 AE |
809 | if (offset + length > segment_size) |
810 | length = segment_size - offset; | |
811 | ||
812 | return length; | |
602adf40 YS |
813 | } |
814 | ||
1fec7093 YS |
815 | static int rbd_get_num_segments(struct rbd_image_header *header, |
816 | u64 ofs, u64 len) | |
817 | { | |
df111be6 AE |
818 | u64 start_seg; |
819 | u64 end_seg; | |
820 | ||
821 | if (!len) | |
822 | return 0; | |
823 | if (len - 1 > U64_MAX - ofs) | |
824 | return -ERANGE; | |
825 | ||
826 | start_seg = ofs >> header->obj_order; | |
827 | end_seg = (ofs + len - 1) >> header->obj_order; | |
828 | ||
1fec7093 YS |
829 | return end_seg - start_seg + 1; |
830 | } | |
831 | ||
029bcbd8 JD |
832 | /* |
833 | * returns the size of an object in the image | |
834 | */ | |
835 | static u64 rbd_obj_bytes(struct rbd_image_header *header) | |
836 | { | |
837 | return 1 << header->obj_order; | |
838 | } | |
839 | ||
602adf40 YS |
840 | /* |
841 | * bio helpers | |
842 | */ | |
843 | ||
844 | static void bio_chain_put(struct bio *chain) | |
845 | { | |
846 | struct bio *tmp; | |
847 | ||
848 | while (chain) { | |
849 | tmp = chain; | |
850 | chain = chain->bi_next; | |
851 | bio_put(tmp); | |
852 | } | |
853 | } | |
854 | ||
855 | /* | |
856 | * zeros a bio chain, starting at specific offset | |
857 | */ | |
858 | static void zero_bio_chain(struct bio *chain, int start_ofs) | |
859 | { | |
860 | struct bio_vec *bv; | |
861 | unsigned long flags; | |
862 | void *buf; | |
863 | int i; | |
864 | int pos = 0; | |
865 | ||
866 | while (chain) { | |
867 | bio_for_each_segment(bv, chain, i) { | |
868 | if (pos + bv->bv_len > start_ofs) { | |
869 | int remainder = max(start_ofs - pos, 0); | |
870 | buf = bvec_kmap_irq(bv, &flags); | |
871 | memset(buf + remainder, 0, | |
872 | bv->bv_len - remainder); | |
85b5aaa6 | 873 | bvec_kunmap_irq(buf, &flags); |
602adf40 YS |
874 | } |
875 | pos += bv->bv_len; | |
876 | } | |
877 | ||
878 | chain = chain->bi_next; | |
879 | } | |
880 | } | |
881 | ||
882 | /* | |
f7760dad AE |
883 | * Clone a portion of a bio, starting at the given byte offset |
884 | * and continuing for the number of bytes indicated. | |
602adf40 | 885 | */ |
f7760dad AE |
886 | static struct bio *bio_clone_range(struct bio *bio_src, |
887 | unsigned int offset, | |
888 | unsigned int len, | |
889 | gfp_t gfpmask) | |
602adf40 | 890 | { |
f7760dad AE |
891 | struct bio_vec *bv; |
892 | unsigned int resid; | |
893 | unsigned short idx; | |
894 | unsigned int voff; | |
895 | unsigned short end_idx; | |
896 | unsigned short vcnt; | |
897 | struct bio *bio; | |
898 | ||
899 | /* Handle the easy case for the caller */ | |
900 | ||
901 | if (!offset && len == bio_src->bi_size) | |
902 | return bio_clone(bio_src, gfpmask); | |
903 | ||
904 | if (WARN_ON_ONCE(!len)) | |
905 | return NULL; | |
906 | if (WARN_ON_ONCE(len > bio_src->bi_size)) | |
907 | return NULL; | |
908 | if (WARN_ON_ONCE(offset > bio_src->bi_size - len)) | |
909 | return NULL; | |
910 | ||
911 | /* Find first affected segment... */ | |
912 | ||
913 | resid = offset; | |
914 | __bio_for_each_segment(bv, bio_src, idx, 0) { | |
915 | if (resid < bv->bv_len) | |
916 | break; | |
917 | resid -= bv->bv_len; | |
602adf40 | 918 | } |
f7760dad | 919 | voff = resid; |
602adf40 | 920 | |
f7760dad | 921 | /* ...and the last affected segment */ |
602adf40 | 922 | |
f7760dad AE |
923 | resid += len; |
924 | __bio_for_each_segment(bv, bio_src, end_idx, idx) { | |
925 | if (resid <= bv->bv_len) | |
926 | break; | |
927 | resid -= bv->bv_len; | |
928 | } | |
929 | vcnt = end_idx - idx + 1; | |
930 | ||
931 | /* Build the clone */ | |
932 | ||
933 | bio = bio_alloc(gfpmask, (unsigned int) vcnt); | |
934 | if (!bio) | |
935 | return NULL; /* ENOMEM */ | |
602adf40 | 936 | |
f7760dad AE |
937 | bio->bi_bdev = bio_src->bi_bdev; |
938 | bio->bi_sector = bio_src->bi_sector + (offset >> SECTOR_SHIFT); | |
939 | bio->bi_rw = bio_src->bi_rw; | |
940 | bio->bi_flags |= 1 << BIO_CLONED; | |
941 | ||
942 | /* | |
943 | * Copy over our part of the bio_vec, then update the first | |
944 | * and last (or only) entries. | |
945 | */ | |
946 | memcpy(&bio->bi_io_vec[0], &bio_src->bi_io_vec[idx], | |
947 | vcnt * sizeof (struct bio_vec)); | |
948 | bio->bi_io_vec[0].bv_offset += voff; | |
949 | if (vcnt > 1) { | |
950 | bio->bi_io_vec[0].bv_len -= voff; | |
951 | bio->bi_io_vec[vcnt - 1].bv_len = resid; | |
952 | } else { | |
953 | bio->bi_io_vec[0].bv_len = len; | |
602adf40 YS |
954 | } |
955 | ||
f7760dad AE |
956 | bio->bi_vcnt = vcnt; |
957 | bio->bi_size = len; | |
958 | bio->bi_idx = 0; | |
959 | ||
960 | return bio; | |
961 | } | |
962 | ||
963 | /* | |
964 | * Clone a portion of a bio chain, starting at the given byte offset | |
965 | * into the first bio in the source chain and continuing for the | |
966 | * number of bytes indicated. The result is another bio chain of | |
967 | * exactly the given length, or a null pointer on error. | |
968 | * | |
969 | * The bio_src and offset parameters are both in-out. On entry they | |
970 | * refer to the first source bio and the offset into that bio where | |
971 | * the start of data to be cloned is located. | |
972 | * | |
973 | * On return, bio_src is updated to refer to the bio in the source | |
974 | * chain that contains first un-cloned byte, and *offset will | |
975 | * contain the offset of that byte within that bio. | |
976 | */ | |
977 | static struct bio *bio_chain_clone_range(struct bio **bio_src, | |
978 | unsigned int *offset, | |
979 | unsigned int len, | |
980 | gfp_t gfpmask) | |
981 | { | |
982 | struct bio *bi = *bio_src; | |
983 | unsigned int off = *offset; | |
984 | struct bio *chain = NULL; | |
985 | struct bio **end; | |
986 | ||
987 | /* Build up a chain of clone bios up to the limit */ | |
988 | ||
989 | if (!bi || off >= bi->bi_size || !len) | |
990 | return NULL; /* Nothing to clone */ | |
602adf40 | 991 | |
f7760dad AE |
992 | end = &chain; |
993 | while (len) { | |
994 | unsigned int bi_size; | |
995 | struct bio *bio; | |
996 | ||
f5400b7a AE |
997 | if (!bi) { |
998 | rbd_warn(NULL, "bio_chain exhausted with %u left", len); | |
f7760dad | 999 | goto out_err; /* EINVAL; ran out of bio's */ |
f5400b7a | 1000 | } |
f7760dad AE |
1001 | bi_size = min_t(unsigned int, bi->bi_size - off, len); |
1002 | bio = bio_clone_range(bi, off, bi_size, gfpmask); | |
1003 | if (!bio) | |
1004 | goto out_err; /* ENOMEM */ | |
1005 | ||
1006 | *end = bio; | |
1007 | end = &bio->bi_next; | |
602adf40 | 1008 | |
f7760dad AE |
1009 | off += bi_size; |
1010 | if (off == bi->bi_size) { | |
1011 | bi = bi->bi_next; | |
1012 | off = 0; | |
1013 | } | |
1014 | len -= bi_size; | |
1015 | } | |
1016 | *bio_src = bi; | |
1017 | *offset = off; | |
1018 | ||
1019 | return chain; | |
1020 | out_err: | |
1021 | bio_chain_put(chain); | |
602adf40 | 1022 | |
602adf40 YS |
1023 | return NULL; |
1024 | } | |
1025 | ||
1026 | /* | |
1027 | * helpers for osd request op vectors. | |
1028 | */ | |
57cfc106 AE |
1029 | static struct ceph_osd_req_op *rbd_create_rw_ops(int num_ops, |
1030 | int opcode, u32 payload_len) | |
602adf40 | 1031 | { |
57cfc106 AE |
1032 | struct ceph_osd_req_op *ops; |
1033 | ||
1034 | ops = kzalloc(sizeof (*ops) * (num_ops + 1), GFP_NOIO); | |
1035 | if (!ops) | |
1036 | return NULL; | |
1037 | ||
1038 | ops[0].op = opcode; | |
1039 | ||
602adf40 YS |
1040 | /* |
1041 | * op extent offset and length will be set later on | |
1042 | * in calc_raw_layout() | |
1043 | */ | |
57cfc106 AE |
1044 | ops[0].payload_len = payload_len; |
1045 | ||
1046 | return ops; | |
602adf40 YS |
1047 | } |
1048 | ||
1049 | static void rbd_destroy_ops(struct ceph_osd_req_op *ops) | |
1050 | { | |
1051 | kfree(ops); | |
1052 | } | |
1053 | ||
1fec7093 YS |
1054 | static void rbd_coll_end_req_index(struct request *rq, |
1055 | struct rbd_req_coll *coll, | |
1056 | int index, | |
8986cb37 | 1057 | s32 ret, u64 len) |
1fec7093 YS |
1058 | { |
1059 | struct request_queue *q; | |
1060 | int min, max, i; | |
1061 | ||
bd919d45 | 1062 | dout("rbd_coll_end_req_index %p index %d ret %d len %llu\n", |
8986cb37 | 1063 | coll, index, (int)ret, (unsigned long long)len); |
1fec7093 YS |
1064 | |
1065 | if (!rq) | |
1066 | return; | |
1067 | ||
1068 | if (!coll) { | |
1069 | blk_end_request(rq, ret, len); | |
1070 | return; | |
1071 | } | |
1072 | ||
1073 | q = rq->q; | |
1074 | ||
1075 | spin_lock_irq(q->queue_lock); | |
1076 | coll->status[index].done = 1; | |
1077 | coll->status[index].rc = ret; | |
1078 | coll->status[index].bytes = len; | |
1079 | max = min = coll->num_done; | |
1080 | while (max < coll->total && coll->status[max].done) | |
1081 | max++; | |
1082 | ||
1083 | for (i = min; i<max; i++) { | |
8986cb37 | 1084 | __blk_end_request(rq, (int)coll->status[i].rc, |
1fec7093 YS |
1085 | coll->status[i].bytes); |
1086 | coll->num_done++; | |
1087 | kref_put(&coll->kref, rbd_coll_release); | |
1088 | } | |
1089 | spin_unlock_irq(q->queue_lock); | |
1090 | } | |
1091 | ||
725afc97 | 1092 | static void rbd_coll_end_req(struct rbd_request *rbd_req, |
8986cb37 | 1093 | s32 ret, u64 len) |
1fec7093 | 1094 | { |
725afc97 AE |
1095 | rbd_coll_end_req_index(rbd_req->rq, |
1096 | rbd_req->coll, rbd_req->coll_index, | |
1097 | ret, len); | |
1fec7093 YS |
1098 | } |
1099 | ||
0ec8ce87 AE |
1100 | static void rbd_layout_init(struct ceph_file_layout *layout, u64 pool_id) |
1101 | { | |
1102 | memset(layout, 0, sizeof (*layout)); | |
1103 | layout->fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER); | |
1104 | layout->fl_stripe_count = cpu_to_le32(1); | |
1105 | layout->fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER); | |
1106 | rbd_assert(pool_id <= (u64) U32_MAX); | |
1107 | layout->fl_pg_pool = cpu_to_le32((u32) pool_id); | |
1108 | } | |
1109 | ||
602adf40 YS |
1110 | /* |
1111 | * Send ceph osd request | |
1112 | */ | |
1113 | static int rbd_do_request(struct request *rq, | |
0ce1a794 | 1114 | struct rbd_device *rbd_dev, |
602adf40 YS |
1115 | struct ceph_snap_context *snapc, |
1116 | u64 snapid, | |
aded07ea | 1117 | const char *object_name, u64 ofs, u64 len, |
602adf40 YS |
1118 | struct bio *bio, |
1119 | struct page **pages, | |
1120 | int num_pages, | |
1121 | int flags, | |
1122 | struct ceph_osd_req_op *ops, | |
1fec7093 YS |
1123 | struct rbd_req_coll *coll, |
1124 | int coll_index, | |
5f29ddd4 AE |
1125 | void (*rbd_cb)(struct ceph_osd_request *, |
1126 | struct ceph_msg *), | |
59c2be1e YS |
1127 | struct ceph_osd_request **linger_req, |
1128 | u64 *ver) | |
602adf40 | 1129 | { |
5f29ddd4 | 1130 | struct ceph_osd_request *osd_req; |
602adf40 YS |
1131 | int ret; |
1132 | u64 bno; | |
1133 | struct timespec mtime = CURRENT_TIME; | |
725afc97 | 1134 | struct rbd_request *rbd_req; |
602adf40 | 1135 | struct ceph_osd_request_head *reqhead; |
1dbb4399 | 1136 | struct ceph_osd_client *osdc; |
602adf40 | 1137 | |
725afc97 | 1138 | rbd_req = kzalloc(sizeof(*rbd_req), GFP_NOIO); |
cd323ac0 | 1139 | if (!rbd_req) |
1fec7093 | 1140 | return -ENOMEM; |
1fec7093 YS |
1141 | |
1142 | if (coll) { | |
725afc97 AE |
1143 | rbd_req->coll = coll; |
1144 | rbd_req->coll_index = coll_index; | |
1fec7093 | 1145 | } |
602adf40 | 1146 | |
f7760dad AE |
1147 | dout("rbd_do_request object_name=%s ofs=%llu len=%llu coll=%p[%d]\n", |
1148 | object_name, (unsigned long long) ofs, | |
1149 | (unsigned long long) len, coll, coll_index); | |
602adf40 | 1150 | |
0ce1a794 | 1151 | osdc = &rbd_dev->rbd_client->client->osdc; |
5f29ddd4 | 1152 | osd_req = ceph_osdc_alloc_request(osdc, flags, snapc, ops, |
1dbb4399 | 1153 | false, GFP_NOIO, pages, bio); |
5f29ddd4 | 1154 | if (!osd_req) { |
4ad12621 | 1155 | ret = -ENOMEM; |
602adf40 YS |
1156 | goto done_pages; |
1157 | } | |
1158 | ||
5f29ddd4 | 1159 | osd_req->r_callback = rbd_cb; |
602adf40 | 1160 | |
725afc97 AE |
1161 | rbd_req->rq = rq; |
1162 | rbd_req->bio = bio; | |
1163 | rbd_req->pages = pages; | |
1164 | rbd_req->len = len; | |
602adf40 | 1165 | |
5f29ddd4 | 1166 | osd_req->r_priv = rbd_req; |
602adf40 | 1167 | |
5f29ddd4 | 1168 | reqhead = osd_req->r_request->front.iov_base; |
602adf40 YS |
1169 | reqhead->snapid = cpu_to_le64(CEPH_NOSNAP); |
1170 | ||
5f29ddd4 AE |
1171 | strncpy(osd_req->r_oid, object_name, sizeof(osd_req->r_oid)); |
1172 | osd_req->r_oid_len = strlen(osd_req->r_oid); | |
602adf40 | 1173 | |
0ec8ce87 AE |
1174 | rbd_layout_init(&osd_req->r_file_layout, rbd_dev->spec->pool_id); |
1175 | ret = ceph_calc_raw_layout(osdc, &osd_req->r_file_layout, | |
1176 | snapid, ofs, &len, &bno, osd_req, ops); | |
6cae3717 | 1177 | rbd_assert(ret == 0); |
602adf40 | 1178 | |
af77f26c | 1179 | ceph_osdc_build_request(osd_req, ofs, &len, ops, snapc, &mtime); |
602adf40 | 1180 | |
59c2be1e | 1181 | if (linger_req) { |
5f29ddd4 AE |
1182 | ceph_osdc_set_request_linger(osdc, osd_req); |
1183 | *linger_req = osd_req; | |
59c2be1e YS |
1184 | } |
1185 | ||
5f29ddd4 | 1186 | ret = ceph_osdc_start_request(osdc, osd_req, false); |
602adf40 YS |
1187 | if (ret < 0) |
1188 | goto done_err; | |
1189 | ||
1190 | if (!rbd_cb) { | |
5f29ddd4 AE |
1191 | u64 version; |
1192 | ||
1193 | ret = ceph_osdc_wait_request(osdc, osd_req); | |
1194 | version = le64_to_cpu(osd_req->r_reassert_version.version); | |
59c2be1e | 1195 | if (ver) |
5f29ddd4 AE |
1196 | *ver = version; |
1197 | dout("reassert_ver=%llu\n", (unsigned long long) version); | |
1198 | ceph_osdc_put_request(osd_req); | |
602adf40 YS |
1199 | } |
1200 | return ret; | |
1201 | ||
1202 | done_err: | |
725afc97 | 1203 | bio_chain_put(rbd_req->bio); |
5f29ddd4 | 1204 | ceph_osdc_put_request(osd_req); |
602adf40 | 1205 | done_pages: |
725afc97 | 1206 | kfree(rbd_req); |
602adf40 YS |
1207 | return ret; |
1208 | } | |
1209 | ||
1210 | /* | |
1211 | * Ceph osd op callback | |
1212 | */ | |
5f29ddd4 | 1213 | static void rbd_req_cb(struct ceph_osd_request *osd_req, struct ceph_msg *msg) |
602adf40 | 1214 | { |
5f29ddd4 | 1215 | struct rbd_request *rbd_req = osd_req->r_priv; |
602adf40 YS |
1216 | struct ceph_osd_reply_head *replyhead; |
1217 | struct ceph_osd_op *op; | |
8986cb37 | 1218 | s32 rc; |
602adf40 YS |
1219 | u64 bytes; |
1220 | int read_op; | |
1221 | ||
1222 | /* parse reply */ | |
1223 | replyhead = msg->front.iov_base; | |
1224 | WARN_ON(le32_to_cpu(replyhead->num_ops) == 0); | |
1225 | op = (void *)(replyhead + 1); | |
8986cb37 | 1226 | rc = (s32)le32_to_cpu(replyhead->result); |
602adf40 | 1227 | bytes = le64_to_cpu(op->extent.length); |
895cfcc8 | 1228 | read_op = (le16_to_cpu(op->op) == CEPH_OSD_OP_READ); |
602adf40 | 1229 | |
bd919d45 AE |
1230 | dout("rbd_req_cb bytes=%llu readop=%d rc=%d\n", |
1231 | (unsigned long long) bytes, read_op, (int) rc); | |
602adf40 | 1232 | |
8986cb37 | 1233 | if (rc == (s32)-ENOENT && read_op) { |
725afc97 | 1234 | zero_bio_chain(rbd_req->bio, 0); |
602adf40 | 1235 | rc = 0; |
725afc97 AE |
1236 | } else if (rc == 0 && read_op && bytes < rbd_req->len) { |
1237 | zero_bio_chain(rbd_req->bio, bytes); | |
1238 | bytes = rbd_req->len; | |
602adf40 YS |
1239 | } |
1240 | ||
725afc97 | 1241 | rbd_coll_end_req(rbd_req, rc, bytes); |
602adf40 | 1242 | |
725afc97 AE |
1243 | if (rbd_req->bio) |
1244 | bio_chain_put(rbd_req->bio); | |
602adf40 | 1245 | |
5f29ddd4 | 1246 | ceph_osdc_put_request(osd_req); |
725afc97 | 1247 | kfree(rbd_req); |
602adf40 YS |
1248 | } |
1249 | ||
5f29ddd4 AE |
1250 | static void rbd_simple_req_cb(struct ceph_osd_request *osd_req, |
1251 | struct ceph_msg *msg) | |
59c2be1e | 1252 | { |
5f29ddd4 | 1253 | ceph_osdc_put_request(osd_req); |
59c2be1e YS |
1254 | } |
1255 | ||
602adf40 YS |
1256 | /* |
1257 | * Do a synchronous ceph osd operation | |
1258 | */ | |
0ce1a794 | 1259 | static int rbd_req_sync_op(struct rbd_device *rbd_dev, |
602adf40 YS |
1260 | struct ceph_snap_context *snapc, |
1261 | u64 snapid, | |
602adf40 | 1262 | int flags, |
913d2fdc | 1263 | struct ceph_osd_req_op *ops, |
aded07ea | 1264 | const char *object_name, |
f8d4de6e AE |
1265 | u64 ofs, u64 inbound_size, |
1266 | char *inbound, | |
59c2be1e YS |
1267 | struct ceph_osd_request **linger_req, |
1268 | u64 *ver) | |
602adf40 YS |
1269 | { |
1270 | int ret; | |
1271 | struct page **pages; | |
1272 | int num_pages; | |
913d2fdc | 1273 | |
aafb230e | 1274 | rbd_assert(ops != NULL); |
602adf40 | 1275 | |
f8d4de6e | 1276 | num_pages = calc_pages_for(ofs, inbound_size); |
602adf40 | 1277 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); |
b8d0638a DC |
1278 | if (IS_ERR(pages)) |
1279 | return PTR_ERR(pages); | |
602adf40 | 1280 | |
0ce1a794 | 1281 | ret = rbd_do_request(NULL, rbd_dev, snapc, snapid, |
f8d4de6e | 1282 | object_name, ofs, inbound_size, NULL, |
602adf40 YS |
1283 | pages, num_pages, |
1284 | flags, | |
1285 | ops, | |
1fec7093 | 1286 | NULL, 0, |
59c2be1e YS |
1287 | NULL, |
1288 | linger_req, ver); | |
602adf40 | 1289 | if (ret < 0) |
913d2fdc | 1290 | goto done; |
602adf40 | 1291 | |
f8d4de6e AE |
1292 | if ((flags & CEPH_OSD_FLAG_READ) && inbound) |
1293 | ret = ceph_copy_from_page_vector(pages, inbound, ofs, ret); | |
602adf40 | 1294 | |
602adf40 YS |
1295 | done: |
1296 | ceph_release_page_vector(pages, num_pages); | |
1297 | return ret; | |
1298 | } | |
1299 | ||
1300 | /* | |
1301 | * Do an asynchronous ceph osd operation | |
1302 | */ | |
1303 | static int rbd_do_op(struct request *rq, | |
0ce1a794 | 1304 | struct rbd_device *rbd_dev, |
602adf40 | 1305 | struct ceph_snap_context *snapc, |
602adf40 | 1306 | u64 ofs, u64 len, |
1fec7093 YS |
1307 | struct bio *bio, |
1308 | struct rbd_req_coll *coll, | |
1309 | int coll_index) | |
602adf40 YS |
1310 | { |
1311 | char *seg_name; | |
1312 | u64 seg_ofs; | |
1313 | u64 seg_len; | |
1314 | int ret; | |
1315 | struct ceph_osd_req_op *ops; | |
1316 | u32 payload_len; | |
ff2e4bb5 AE |
1317 | int opcode; |
1318 | int flags; | |
4634246d | 1319 | u64 snapid; |
602adf40 | 1320 | |
65ccfe21 | 1321 | seg_name = rbd_segment_name(rbd_dev, ofs); |
602adf40 YS |
1322 | if (!seg_name) |
1323 | return -ENOMEM; | |
65ccfe21 AE |
1324 | seg_len = rbd_segment_length(rbd_dev, ofs, len); |
1325 | seg_ofs = rbd_segment_offset(rbd_dev, ofs); | |
602adf40 | 1326 | |
ff2e4bb5 AE |
1327 | if (rq_data_dir(rq) == WRITE) { |
1328 | opcode = CEPH_OSD_OP_WRITE; | |
1329 | flags = CEPH_OSD_FLAG_WRITE|CEPH_OSD_FLAG_ONDISK; | |
4634246d | 1330 | snapid = CEPH_NOSNAP; |
ff2e4bb5 AE |
1331 | payload_len = seg_len; |
1332 | } else { | |
1333 | opcode = CEPH_OSD_OP_READ; | |
1334 | flags = CEPH_OSD_FLAG_READ; | |
a7b4c65f | 1335 | rbd_assert(!snapc); |
0d7dbfce | 1336 | snapid = rbd_dev->spec->snap_id; |
ff2e4bb5 AE |
1337 | payload_len = 0; |
1338 | } | |
602adf40 | 1339 | |
57cfc106 AE |
1340 | ret = -ENOMEM; |
1341 | ops = rbd_create_rw_ops(1, opcode, payload_len); | |
1342 | if (!ops) | |
602adf40 YS |
1343 | goto done; |
1344 | ||
1345 | /* we've taken care of segment sizes earlier when we | |
1346 | cloned the bios. We should never have a segment | |
1347 | truncated at this point */ | |
aafb230e | 1348 | rbd_assert(seg_len == len); |
602adf40 YS |
1349 | |
1350 | ret = rbd_do_request(rq, rbd_dev, snapc, snapid, | |
1351 | seg_name, seg_ofs, seg_len, | |
1352 | bio, | |
1353 | NULL, 0, | |
1354 | flags, | |
1355 | ops, | |
1fec7093 | 1356 | coll, coll_index, |
59c2be1e | 1357 | rbd_req_cb, 0, NULL); |
cd323ac0 AE |
1358 | if (ret < 0) |
1359 | rbd_coll_end_req_index(rq, coll, coll_index, | |
1360 | (s32)ret, seg_len); | |
11f77002 | 1361 | rbd_destroy_ops(ops); |
602adf40 YS |
1362 | done: |
1363 | kfree(seg_name); | |
1364 | return ret; | |
1365 | } | |
1366 | ||
602adf40 YS |
1367 | /* |
1368 | * Request sync osd read | |
1369 | */ | |
0ce1a794 | 1370 | static int rbd_req_sync_read(struct rbd_device *rbd_dev, |
aded07ea | 1371 | const char *object_name, |
602adf40 | 1372 | u64 ofs, u64 len, |
59c2be1e YS |
1373 | char *buf, |
1374 | u64 *ver) | |
602adf40 | 1375 | { |
913d2fdc AE |
1376 | struct ceph_osd_req_op *ops; |
1377 | int ret; | |
1378 | ||
1379 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_READ, 0); | |
1380 | if (!ops) | |
1381 | return -ENOMEM; | |
1382 | ||
1383 | ret = rbd_req_sync_op(rbd_dev, NULL, | |
4775618d | 1384 | CEPH_NOSNAP, |
602adf40 | 1385 | CEPH_OSD_FLAG_READ, |
913d2fdc AE |
1386 | ops, object_name, ofs, len, buf, NULL, ver); |
1387 | rbd_destroy_ops(ops); | |
1388 | ||
1389 | return ret; | |
602adf40 YS |
1390 | } |
1391 | ||
1392 | /* | |
59c2be1e YS |
1393 | * Request sync osd watch |
1394 | */ | |
0ce1a794 | 1395 | static int rbd_req_sync_notify_ack(struct rbd_device *rbd_dev, |
59c2be1e | 1396 | u64 ver, |
7f0a24d8 | 1397 | u64 notify_id) |
59c2be1e YS |
1398 | { |
1399 | struct ceph_osd_req_op *ops; | |
11f77002 SW |
1400 | int ret; |
1401 | ||
57cfc106 AE |
1402 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_NOTIFY_ACK, 0); |
1403 | if (!ops) | |
1404 | return -ENOMEM; | |
59c2be1e | 1405 | |
a71b891b | 1406 | ops[0].watch.ver = cpu_to_le64(ver); |
59c2be1e YS |
1407 | ops[0].watch.cookie = notify_id; |
1408 | ops[0].watch.flag = 0; | |
1409 | ||
0ce1a794 | 1410 | ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP, |
7f0a24d8 | 1411 | rbd_dev->header_name, 0, 0, NULL, |
ad4f232f | 1412 | NULL, 0, |
59c2be1e YS |
1413 | CEPH_OSD_FLAG_READ, |
1414 | ops, | |
1fec7093 | 1415 | NULL, 0, |
59c2be1e YS |
1416 | rbd_simple_req_cb, 0, NULL); |
1417 | ||
1418 | rbd_destroy_ops(ops); | |
1419 | return ret; | |
1420 | } | |
1421 | ||
1422 | static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data) | |
1423 | { | |
0ce1a794 | 1424 | struct rbd_device *rbd_dev = (struct rbd_device *)data; |
a71b891b | 1425 | u64 hver; |
13143d2d SW |
1426 | int rc; |
1427 | ||
0ce1a794 | 1428 | if (!rbd_dev) |
59c2be1e YS |
1429 | return; |
1430 | ||
bd919d45 AE |
1431 | dout("rbd_watch_cb %s notify_id=%llu opcode=%u\n", |
1432 | rbd_dev->header_name, (unsigned long long) notify_id, | |
1433 | (unsigned int) opcode); | |
117973fb | 1434 | rc = rbd_dev_refresh(rbd_dev, &hver); |
13143d2d | 1435 | if (rc) |
06ecc6cb AE |
1436 | rbd_warn(rbd_dev, "got notification but failed to " |
1437 | " update snaps: %d\n", rc); | |
59c2be1e | 1438 | |
7f0a24d8 | 1439 | rbd_req_sync_notify_ack(rbd_dev, hver, notify_id); |
59c2be1e YS |
1440 | } |
1441 | ||
1442 | /* | |
1443 | * Request sync osd watch | |
1444 | */ | |
0e6f322d | 1445 | static int rbd_req_sync_watch(struct rbd_device *rbd_dev) |
59c2be1e YS |
1446 | { |
1447 | struct ceph_osd_req_op *ops; | |
0ce1a794 | 1448 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
57cfc106 | 1449 | int ret; |
59c2be1e | 1450 | |
57cfc106 AE |
1451 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0); |
1452 | if (!ops) | |
1453 | return -ENOMEM; | |
59c2be1e YS |
1454 | |
1455 | ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0, | |
0ce1a794 | 1456 | (void *)rbd_dev, &rbd_dev->watch_event); |
59c2be1e YS |
1457 | if (ret < 0) |
1458 | goto fail; | |
1459 | ||
0e6f322d | 1460 | ops[0].watch.ver = cpu_to_le64(rbd_dev->header.obj_version); |
0ce1a794 | 1461 | ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie); |
59c2be1e YS |
1462 | ops[0].watch.flag = 1; |
1463 | ||
0ce1a794 | 1464 | ret = rbd_req_sync_op(rbd_dev, NULL, |
59c2be1e | 1465 | CEPH_NOSNAP, |
59c2be1e YS |
1466 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, |
1467 | ops, | |
0e6f322d AE |
1468 | rbd_dev->header_name, |
1469 | 0, 0, NULL, | |
0ce1a794 | 1470 | &rbd_dev->watch_request, NULL); |
59c2be1e YS |
1471 | |
1472 | if (ret < 0) | |
1473 | goto fail_event; | |
1474 | ||
1475 | rbd_destroy_ops(ops); | |
1476 | return 0; | |
1477 | ||
1478 | fail_event: | |
0ce1a794 AE |
1479 | ceph_osdc_cancel_event(rbd_dev->watch_event); |
1480 | rbd_dev->watch_event = NULL; | |
59c2be1e YS |
1481 | fail: |
1482 | rbd_destroy_ops(ops); | |
1483 | return ret; | |
1484 | } | |
1485 | ||
79e3057c YS |
1486 | /* |
1487 | * Request sync osd unwatch | |
1488 | */ | |
070c633f | 1489 | static int rbd_req_sync_unwatch(struct rbd_device *rbd_dev) |
79e3057c YS |
1490 | { |
1491 | struct ceph_osd_req_op *ops; | |
57cfc106 | 1492 | int ret; |
79e3057c | 1493 | |
57cfc106 AE |
1494 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_WATCH, 0); |
1495 | if (!ops) | |
1496 | return -ENOMEM; | |
79e3057c YS |
1497 | |
1498 | ops[0].watch.ver = 0; | |
0ce1a794 | 1499 | ops[0].watch.cookie = cpu_to_le64(rbd_dev->watch_event->cookie); |
79e3057c YS |
1500 | ops[0].watch.flag = 0; |
1501 | ||
0ce1a794 | 1502 | ret = rbd_req_sync_op(rbd_dev, NULL, |
79e3057c | 1503 | CEPH_NOSNAP, |
79e3057c YS |
1504 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, |
1505 | ops, | |
070c633f AE |
1506 | rbd_dev->header_name, |
1507 | 0, 0, NULL, NULL, NULL); | |
1508 | ||
79e3057c YS |
1509 | |
1510 | rbd_destroy_ops(ops); | |
0ce1a794 AE |
1511 | ceph_osdc_cancel_event(rbd_dev->watch_event); |
1512 | rbd_dev->watch_event = NULL; | |
79e3057c YS |
1513 | return ret; |
1514 | } | |
1515 | ||
602adf40 | 1516 | /* |
3cb4a687 | 1517 | * Synchronous osd object method call |
602adf40 | 1518 | */ |
0ce1a794 | 1519 | static int rbd_req_sync_exec(struct rbd_device *rbd_dev, |
aded07ea AE |
1520 | const char *object_name, |
1521 | const char *class_name, | |
1522 | const char *method_name, | |
3cb4a687 AE |
1523 | const char *outbound, |
1524 | size_t outbound_size, | |
f8d4de6e AE |
1525 | char *inbound, |
1526 | size_t inbound_size, | |
3cb4a687 | 1527 | int flags, |
59c2be1e | 1528 | u64 *ver) |
602adf40 YS |
1529 | { |
1530 | struct ceph_osd_req_op *ops; | |
aded07ea AE |
1531 | int class_name_len = strlen(class_name); |
1532 | int method_name_len = strlen(method_name); | |
3cb4a687 | 1533 | int payload_size; |
57cfc106 AE |
1534 | int ret; |
1535 | ||
3cb4a687 AE |
1536 | /* |
1537 | * Any input parameters required by the method we're calling | |
1538 | * will be sent along with the class and method names as | |
1539 | * part of the message payload. That data and its size are | |
1540 | * supplied via the indata and indata_len fields (named from | |
1541 | * the perspective of the server side) in the OSD request | |
1542 | * operation. | |
1543 | */ | |
1544 | payload_size = class_name_len + method_name_len + outbound_size; | |
1545 | ops = rbd_create_rw_ops(1, CEPH_OSD_OP_CALL, payload_size); | |
57cfc106 AE |
1546 | if (!ops) |
1547 | return -ENOMEM; | |
602adf40 | 1548 | |
aded07ea AE |
1549 | ops[0].cls.class_name = class_name; |
1550 | ops[0].cls.class_len = (__u8) class_name_len; | |
1551 | ops[0].cls.method_name = method_name; | |
1552 | ops[0].cls.method_len = (__u8) method_name_len; | |
602adf40 | 1553 | ops[0].cls.argc = 0; |
3cb4a687 AE |
1554 | ops[0].cls.indata = outbound; |
1555 | ops[0].cls.indata_len = outbound_size; | |
602adf40 | 1556 | |
0ce1a794 | 1557 | ret = rbd_req_sync_op(rbd_dev, NULL, |
602adf40 | 1558 | CEPH_NOSNAP, |
3cb4a687 | 1559 | flags, ops, |
f8d4de6e AE |
1560 | object_name, 0, inbound_size, inbound, |
1561 | NULL, ver); | |
602adf40 YS |
1562 | |
1563 | rbd_destroy_ops(ops); | |
1564 | ||
1565 | dout("cls_exec returned %d\n", ret); | |
1566 | return ret; | |
1567 | } | |
1568 | ||
1fec7093 YS |
1569 | static struct rbd_req_coll *rbd_alloc_coll(int num_reqs) |
1570 | { | |
1571 | struct rbd_req_coll *coll = | |
1572 | kzalloc(sizeof(struct rbd_req_coll) + | |
1573 | sizeof(struct rbd_req_status) * num_reqs, | |
1574 | GFP_ATOMIC); | |
1575 | ||
1576 | if (!coll) | |
1577 | return NULL; | |
1578 | coll->total = num_reqs; | |
1579 | kref_init(&coll->kref); | |
1580 | return coll; | |
1581 | } | |
1582 | ||
8295cda7 AE |
1583 | static int rbd_dev_do_request(struct request *rq, |
1584 | struct rbd_device *rbd_dev, | |
1585 | struct ceph_snap_context *snapc, | |
1586 | u64 ofs, unsigned int size, | |
1587 | struct bio *bio_chain) | |
1588 | { | |
1589 | int num_segs; | |
1590 | struct rbd_req_coll *coll; | |
1591 | unsigned int bio_offset; | |
1592 | int cur_seg = 0; | |
1593 | ||
1594 | dout("%s 0x%x bytes at 0x%llx\n", | |
1595 | rq_data_dir(rq) == WRITE ? "write" : "read", | |
1596 | size, (unsigned long long) blk_rq_pos(rq) * SECTOR_SIZE); | |
1597 | ||
1598 | num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size); | |
1599 | if (num_segs <= 0) | |
1600 | return num_segs; | |
1601 | ||
1602 | coll = rbd_alloc_coll(num_segs); | |
1603 | if (!coll) | |
1604 | return -ENOMEM; | |
1605 | ||
1606 | bio_offset = 0; | |
1607 | do { | |
1608 | u64 limit = rbd_segment_length(rbd_dev, ofs, size); | |
1609 | unsigned int clone_size; | |
1610 | struct bio *bio_clone; | |
1611 | ||
1612 | BUG_ON(limit > (u64)UINT_MAX); | |
1613 | clone_size = (unsigned int)limit; | |
1614 | dout("bio_chain->bi_vcnt=%hu\n", bio_chain->bi_vcnt); | |
1615 | ||
1616 | kref_get(&coll->kref); | |
1617 | ||
1618 | /* Pass a cloned bio chain via an osd request */ | |
1619 | ||
1620 | bio_clone = bio_chain_clone_range(&bio_chain, | |
1621 | &bio_offset, clone_size, | |
1622 | GFP_ATOMIC); | |
1623 | if (bio_clone) | |
1624 | (void)rbd_do_op(rq, rbd_dev, snapc, | |
1625 | ofs, clone_size, | |
1626 | bio_clone, coll, cur_seg); | |
1627 | else | |
1628 | rbd_coll_end_req_index(rq, coll, cur_seg, | |
1629 | (s32)-ENOMEM, | |
1630 | clone_size); | |
1631 | size -= clone_size; | |
1632 | ofs += clone_size; | |
1633 | ||
1634 | cur_seg++; | |
1635 | } while (size > 0); | |
1636 | kref_put(&coll->kref, rbd_coll_release); | |
1637 | ||
1638 | return 0; | |
1639 | } | |
1640 | ||
602adf40 YS |
1641 | /* |
1642 | * block device queue callback | |
1643 | */ | |
1644 | static void rbd_rq_fn(struct request_queue *q) | |
1645 | { | |
1646 | struct rbd_device *rbd_dev = q->queuedata; | |
b395e8b5 | 1647 | bool read_only = rbd_dev->mapping.read_only; |
602adf40 | 1648 | struct request *rq; |
602adf40 | 1649 | |
00f1f36f | 1650 | while ((rq = blk_fetch_request(q))) { |
b395e8b5 AE |
1651 | struct ceph_snap_context *snapc = NULL; |
1652 | unsigned int size = 0; | |
8295cda7 | 1653 | int result; |
602adf40 | 1654 | |
602adf40 YS |
1655 | dout("fetched request\n"); |
1656 | ||
b395e8b5 AE |
1657 | /* Filter out block requests we don't understand */ |
1658 | ||
602adf40 YS |
1659 | if ((rq->cmd_type != REQ_TYPE_FS)) { |
1660 | __blk_end_request_all(rq, 0); | |
00f1f36f | 1661 | continue; |
602adf40 | 1662 | } |
b395e8b5 | 1663 | spin_unlock_irq(q->queue_lock); |
602adf40 | 1664 | |
a7b4c65f AE |
1665 | /* Write requests need a reference to the snapshot context */ |
1666 | ||
1667 | if (rq_data_dir(rq) == WRITE) { | |
1668 | result = -EROFS; | |
1669 | if (read_only) /* Can't write to a read-only device */ | |
1670 | goto out_end_request; | |
1671 | ||
1672 | /* | |
1673 | * Note that each osd request will take its | |
1674 | * own reference to the snapshot context | |
1675 | * supplied. The reference we take here | |
1676 | * just guarantees the one we provide stays | |
1677 | * valid. | |
1678 | */ | |
1679 | down_read(&rbd_dev->header_rwsem); | |
b395e8b5 | 1680 | snapc = ceph_get_snap_context(rbd_dev->header.snapc); |
a7b4c65f | 1681 | up_read(&rbd_dev->header_rwsem); |
b395e8b5 | 1682 | rbd_assert(snapc != NULL); |
a7b4c65f | 1683 | } else if (!atomic_read(&rbd_dev->exists)) { |
0d7dbfce | 1684 | rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP); |
d1d25646 | 1685 | dout("request for non-existent snapshot"); |
b395e8b5 AE |
1686 | result = -ENXIO; |
1687 | goto out_end_request; | |
e88a36ec JD |
1688 | } |
1689 | ||
f7760dad | 1690 | size = blk_rq_bytes(rq); |
b395e8b5 AE |
1691 | result = rbd_dev_do_request(rq, rbd_dev, snapc, |
1692 | blk_rq_pos(rq) * SECTOR_SIZE, | |
1693 | size, rq->bio); | |
1694 | out_end_request: | |
a7b4c65f AE |
1695 | if (snapc) |
1696 | ceph_put_snap_context(snapc); | |
8295cda7 AE |
1697 | spin_lock_irq(q->queue_lock); |
1698 | if (!size || result < 0) | |
1699 | __blk_end_request_all(rq, result); | |
602adf40 YS |
1700 | } |
1701 | } | |
1702 | ||
1703 | /* | |
1704 | * a queue callback. Makes sure that we don't create a bio that spans across | |
1705 | * multiple osd objects. One exception would be with a single page bios, | |
f7760dad | 1706 | * which we handle later at bio_chain_clone_range() |
602adf40 YS |
1707 | */ |
1708 | static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd, | |
1709 | struct bio_vec *bvec) | |
1710 | { | |
1711 | struct rbd_device *rbd_dev = q->queuedata; | |
e5cfeed2 AE |
1712 | sector_t sector_offset; |
1713 | sector_t sectors_per_obj; | |
1714 | sector_t obj_sector_offset; | |
1715 | int ret; | |
1716 | ||
1717 | /* | |
1718 | * Find how far into its rbd object the partition-relative | |
1719 | * bio start sector is to offset relative to the enclosing | |
1720 | * device. | |
1721 | */ | |
1722 | sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector; | |
1723 | sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT); | |
1724 | obj_sector_offset = sector_offset & (sectors_per_obj - 1); | |
1725 | ||
1726 | /* | |
1727 | * Compute the number of bytes from that offset to the end | |
1728 | * of the object. Account for what's already used by the bio. | |
1729 | */ | |
1730 | ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT; | |
1731 | if (ret > bmd->bi_size) | |
1732 | ret -= bmd->bi_size; | |
1733 | else | |
1734 | ret = 0; | |
1735 | ||
1736 | /* | |
1737 | * Don't send back more than was asked for. And if the bio | |
1738 | * was empty, let the whole thing through because: "Note | |
1739 | * that a block device *must* allow a single page to be | |
1740 | * added to an empty bio." | |
1741 | */ | |
1742 | rbd_assert(bvec->bv_len <= PAGE_SIZE); | |
1743 | if (ret > (int) bvec->bv_len || !bmd->bi_size) | |
1744 | ret = (int) bvec->bv_len; | |
1745 | ||
1746 | return ret; | |
602adf40 YS |
1747 | } |
1748 | ||
1749 | static void rbd_free_disk(struct rbd_device *rbd_dev) | |
1750 | { | |
1751 | struct gendisk *disk = rbd_dev->disk; | |
1752 | ||
1753 | if (!disk) | |
1754 | return; | |
1755 | ||
602adf40 YS |
1756 | if (disk->flags & GENHD_FL_UP) |
1757 | del_gendisk(disk); | |
1758 | if (disk->queue) | |
1759 | blk_cleanup_queue(disk->queue); | |
1760 | put_disk(disk); | |
1761 | } | |
1762 | ||
1763 | /* | |
4156d998 AE |
1764 | * Read the complete header for the given rbd device. |
1765 | * | |
1766 | * Returns a pointer to a dynamically-allocated buffer containing | |
1767 | * the complete and validated header. Caller can pass the address | |
1768 | * of a variable that will be filled in with the version of the | |
1769 | * header object at the time it was read. | |
1770 | * | |
1771 | * Returns a pointer-coded errno if a failure occurs. | |
602adf40 | 1772 | */ |
4156d998 AE |
1773 | static struct rbd_image_header_ondisk * |
1774 | rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version) | |
602adf40 | 1775 | { |
4156d998 | 1776 | struct rbd_image_header_ondisk *ondisk = NULL; |
50f7c4c9 | 1777 | u32 snap_count = 0; |
4156d998 AE |
1778 | u64 names_size = 0; |
1779 | u32 want_count; | |
1780 | int ret; | |
602adf40 | 1781 | |
00f1f36f | 1782 | /* |
4156d998 AE |
1783 | * The complete header will include an array of its 64-bit |
1784 | * snapshot ids, followed by the names of those snapshots as | |
1785 | * a contiguous block of NUL-terminated strings. Note that | |
1786 | * the number of snapshots could change by the time we read | |
1787 | * it in, in which case we re-read it. | |
00f1f36f | 1788 | */ |
4156d998 AE |
1789 | do { |
1790 | size_t size; | |
1791 | ||
1792 | kfree(ondisk); | |
1793 | ||
1794 | size = sizeof (*ondisk); | |
1795 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); | |
1796 | size += names_size; | |
1797 | ondisk = kmalloc(size, GFP_KERNEL); | |
1798 | if (!ondisk) | |
1799 | return ERR_PTR(-ENOMEM); | |
1800 | ||
4775618d | 1801 | ret = rbd_req_sync_read(rbd_dev, rbd_dev->header_name, |
4156d998 AE |
1802 | 0, size, |
1803 | (char *) ondisk, version); | |
1804 | ||
1805 | if (ret < 0) | |
1806 | goto out_err; | |
1807 | if (WARN_ON((size_t) ret < size)) { | |
1808 | ret = -ENXIO; | |
06ecc6cb AE |
1809 | rbd_warn(rbd_dev, "short header read (want %zd got %d)", |
1810 | size, ret); | |
4156d998 AE |
1811 | goto out_err; |
1812 | } | |
1813 | if (!rbd_dev_ondisk_valid(ondisk)) { | |
1814 | ret = -ENXIO; | |
06ecc6cb | 1815 | rbd_warn(rbd_dev, "invalid header"); |
4156d998 | 1816 | goto out_err; |
81e759fb | 1817 | } |
602adf40 | 1818 | |
4156d998 AE |
1819 | names_size = le64_to_cpu(ondisk->snap_names_len); |
1820 | want_count = snap_count; | |
1821 | snap_count = le32_to_cpu(ondisk->snap_count); | |
1822 | } while (snap_count != want_count); | |
00f1f36f | 1823 | |
4156d998 | 1824 | return ondisk; |
00f1f36f | 1825 | |
4156d998 AE |
1826 | out_err: |
1827 | kfree(ondisk); | |
1828 | ||
1829 | return ERR_PTR(ret); | |
1830 | } | |
1831 | ||
1832 | /* | |
1833 | * reload the ondisk the header | |
1834 | */ | |
1835 | static int rbd_read_header(struct rbd_device *rbd_dev, | |
1836 | struct rbd_image_header *header) | |
1837 | { | |
1838 | struct rbd_image_header_ondisk *ondisk; | |
1839 | u64 ver = 0; | |
1840 | int ret; | |
602adf40 | 1841 | |
4156d998 AE |
1842 | ondisk = rbd_dev_v1_header_read(rbd_dev, &ver); |
1843 | if (IS_ERR(ondisk)) | |
1844 | return PTR_ERR(ondisk); | |
1845 | ret = rbd_header_from_disk(header, ondisk); | |
1846 | if (ret >= 0) | |
1847 | header->obj_version = ver; | |
1848 | kfree(ondisk); | |
1849 | ||
1850 | return ret; | |
602adf40 YS |
1851 | } |
1852 | ||
41f38c2b | 1853 | static void rbd_remove_all_snaps(struct rbd_device *rbd_dev) |
dfc5606d YS |
1854 | { |
1855 | struct rbd_snap *snap; | |
a0593290 | 1856 | struct rbd_snap *next; |
dfc5606d | 1857 | |
a0593290 | 1858 | list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node) |
41f38c2b | 1859 | rbd_remove_snap_dev(snap); |
dfc5606d YS |
1860 | } |
1861 | ||
9478554a AE |
1862 | static void rbd_update_mapping_size(struct rbd_device *rbd_dev) |
1863 | { | |
1864 | sector_t size; | |
1865 | ||
0d7dbfce | 1866 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP) |
9478554a AE |
1867 | return; |
1868 | ||
1869 | size = (sector_t) rbd_dev->header.image_size / SECTOR_SIZE; | |
1870 | dout("setting size to %llu sectors", (unsigned long long) size); | |
1871 | rbd_dev->mapping.size = (u64) size; | |
1872 | set_capacity(rbd_dev->disk, size); | |
1873 | } | |
1874 | ||
602adf40 YS |
1875 | /* |
1876 | * only read the first part of the ondisk header, without the snaps info | |
1877 | */ | |
117973fb | 1878 | static int rbd_dev_v1_refresh(struct rbd_device *rbd_dev, u64 *hver) |
602adf40 YS |
1879 | { |
1880 | int ret; | |
1881 | struct rbd_image_header h; | |
602adf40 YS |
1882 | |
1883 | ret = rbd_read_header(rbd_dev, &h); | |
1884 | if (ret < 0) | |
1885 | return ret; | |
1886 | ||
a51aa0c0 JD |
1887 | down_write(&rbd_dev->header_rwsem); |
1888 | ||
9478554a AE |
1889 | /* Update image size, and check for resize of mapped image */ |
1890 | rbd_dev->header.image_size = h.image_size; | |
1891 | rbd_update_mapping_size(rbd_dev); | |
9db4b3e3 | 1892 | |
849b4260 | 1893 | /* rbd_dev->header.object_prefix shouldn't change */ |
602adf40 | 1894 | kfree(rbd_dev->header.snap_sizes); |
849b4260 | 1895 | kfree(rbd_dev->header.snap_names); |
d1d25646 JD |
1896 | /* osd requests may still refer to snapc */ |
1897 | ceph_put_snap_context(rbd_dev->header.snapc); | |
602adf40 | 1898 | |
b813623a AE |
1899 | if (hver) |
1900 | *hver = h.obj_version; | |
a71b891b | 1901 | rbd_dev->header.obj_version = h.obj_version; |
93a24e08 | 1902 | rbd_dev->header.image_size = h.image_size; |
602adf40 YS |
1903 | rbd_dev->header.snapc = h.snapc; |
1904 | rbd_dev->header.snap_names = h.snap_names; | |
1905 | rbd_dev->header.snap_sizes = h.snap_sizes; | |
849b4260 AE |
1906 | /* Free the extra copy of the object prefix */ |
1907 | WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix)); | |
1908 | kfree(h.object_prefix); | |
1909 | ||
304f6808 AE |
1910 | ret = rbd_dev_snaps_update(rbd_dev); |
1911 | if (!ret) | |
1912 | ret = rbd_dev_snaps_register(rbd_dev); | |
dfc5606d | 1913 | |
c666601a | 1914 | up_write(&rbd_dev->header_rwsem); |
602adf40 | 1915 | |
dfc5606d | 1916 | return ret; |
602adf40 YS |
1917 | } |
1918 | ||
117973fb | 1919 | static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver) |
1fe5e993 AE |
1920 | { |
1921 | int ret; | |
1922 | ||
117973fb | 1923 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
1fe5e993 | 1924 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
117973fb AE |
1925 | if (rbd_dev->image_format == 1) |
1926 | ret = rbd_dev_v1_refresh(rbd_dev, hver); | |
1927 | else | |
1928 | ret = rbd_dev_v2_refresh(rbd_dev, hver); | |
1fe5e993 AE |
1929 | mutex_unlock(&ctl_mutex); |
1930 | ||
1931 | return ret; | |
1932 | } | |
1933 | ||
602adf40 YS |
1934 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
1935 | { | |
1936 | struct gendisk *disk; | |
1937 | struct request_queue *q; | |
593a9e7b | 1938 | u64 segment_size; |
602adf40 | 1939 | |
602adf40 | 1940 | /* create gendisk info */ |
602adf40 YS |
1941 | disk = alloc_disk(RBD_MINORS_PER_MAJOR); |
1942 | if (!disk) | |
1fcdb8aa | 1943 | return -ENOMEM; |
602adf40 | 1944 | |
f0f8cef5 | 1945 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
de71a297 | 1946 | rbd_dev->dev_id); |
602adf40 YS |
1947 | disk->major = rbd_dev->major; |
1948 | disk->first_minor = 0; | |
1949 | disk->fops = &rbd_bd_ops; | |
1950 | disk->private_data = rbd_dev; | |
1951 | ||
1952 | /* init rq */ | |
602adf40 YS |
1953 | q = blk_init_queue(rbd_rq_fn, &rbd_dev->lock); |
1954 | if (!q) | |
1955 | goto out_disk; | |
029bcbd8 | 1956 | |
593a9e7b AE |
1957 | /* We use the default size, but let's be explicit about it. */ |
1958 | blk_queue_physical_block_size(q, SECTOR_SIZE); | |
1959 | ||
029bcbd8 | 1960 | /* set io sizes to object size */ |
593a9e7b AE |
1961 | segment_size = rbd_obj_bytes(&rbd_dev->header); |
1962 | blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE); | |
1963 | blk_queue_max_segment_size(q, segment_size); | |
1964 | blk_queue_io_min(q, segment_size); | |
1965 | blk_queue_io_opt(q, segment_size); | |
029bcbd8 | 1966 | |
602adf40 YS |
1967 | blk_queue_merge_bvec(q, rbd_merge_bvec); |
1968 | disk->queue = q; | |
1969 | ||
1970 | q->queuedata = rbd_dev; | |
1971 | ||
1972 | rbd_dev->disk = disk; | |
602adf40 | 1973 | |
12f02944 AE |
1974 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
1975 | ||
602adf40 | 1976 | return 0; |
602adf40 YS |
1977 | out_disk: |
1978 | put_disk(disk); | |
1fcdb8aa AE |
1979 | |
1980 | return -ENOMEM; | |
602adf40 YS |
1981 | } |
1982 | ||
dfc5606d YS |
1983 | /* |
1984 | sysfs | |
1985 | */ | |
1986 | ||
593a9e7b AE |
1987 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
1988 | { | |
1989 | return container_of(dev, struct rbd_device, dev); | |
1990 | } | |
1991 | ||
dfc5606d YS |
1992 | static ssize_t rbd_size_show(struct device *dev, |
1993 | struct device_attribute *attr, char *buf) | |
1994 | { | |
593a9e7b | 1995 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
a51aa0c0 JD |
1996 | sector_t size; |
1997 | ||
1998 | down_read(&rbd_dev->header_rwsem); | |
1999 | size = get_capacity(rbd_dev->disk); | |
2000 | up_read(&rbd_dev->header_rwsem); | |
dfc5606d | 2001 | |
a51aa0c0 | 2002 | return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE); |
dfc5606d YS |
2003 | } |
2004 | ||
34b13184 AE |
2005 | /* |
2006 | * Note this shows the features for whatever's mapped, which is not | |
2007 | * necessarily the base image. | |
2008 | */ | |
2009 | static ssize_t rbd_features_show(struct device *dev, | |
2010 | struct device_attribute *attr, char *buf) | |
2011 | { | |
2012 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
2013 | ||
2014 | return sprintf(buf, "0x%016llx\n", | |
2015 | (unsigned long long) rbd_dev->mapping.features); | |
2016 | } | |
2017 | ||
dfc5606d YS |
2018 | static ssize_t rbd_major_show(struct device *dev, |
2019 | struct device_attribute *attr, char *buf) | |
2020 | { | |
593a9e7b | 2021 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
602adf40 | 2022 | |
dfc5606d YS |
2023 | return sprintf(buf, "%d\n", rbd_dev->major); |
2024 | } | |
2025 | ||
2026 | static ssize_t rbd_client_id_show(struct device *dev, | |
2027 | struct device_attribute *attr, char *buf) | |
602adf40 | 2028 | { |
593a9e7b | 2029 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
dfc5606d | 2030 | |
1dbb4399 AE |
2031 | return sprintf(buf, "client%lld\n", |
2032 | ceph_client_id(rbd_dev->rbd_client->client)); | |
602adf40 YS |
2033 | } |
2034 | ||
dfc5606d YS |
2035 | static ssize_t rbd_pool_show(struct device *dev, |
2036 | struct device_attribute *attr, char *buf) | |
602adf40 | 2037 | { |
593a9e7b | 2038 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
dfc5606d | 2039 | |
0d7dbfce | 2040 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_name); |
dfc5606d YS |
2041 | } |
2042 | ||
9bb2f334 AE |
2043 | static ssize_t rbd_pool_id_show(struct device *dev, |
2044 | struct device_attribute *attr, char *buf) | |
2045 | { | |
2046 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
2047 | ||
0d7dbfce AE |
2048 | return sprintf(buf, "%llu\n", |
2049 | (unsigned long long) rbd_dev->spec->pool_id); | |
9bb2f334 AE |
2050 | } |
2051 | ||
dfc5606d YS |
2052 | static ssize_t rbd_name_show(struct device *dev, |
2053 | struct device_attribute *attr, char *buf) | |
2054 | { | |
593a9e7b | 2055 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
dfc5606d | 2056 | |
a92ffdf8 AE |
2057 | if (rbd_dev->spec->image_name) |
2058 | return sprintf(buf, "%s\n", rbd_dev->spec->image_name); | |
2059 | ||
2060 | return sprintf(buf, "(unknown)\n"); | |
dfc5606d YS |
2061 | } |
2062 | ||
589d30e0 AE |
2063 | static ssize_t rbd_image_id_show(struct device *dev, |
2064 | struct device_attribute *attr, char *buf) | |
2065 | { | |
2066 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
2067 | ||
0d7dbfce | 2068 | return sprintf(buf, "%s\n", rbd_dev->spec->image_id); |
589d30e0 AE |
2069 | } |
2070 | ||
34b13184 AE |
2071 | /* |
2072 | * Shows the name of the currently-mapped snapshot (or | |
2073 | * RBD_SNAP_HEAD_NAME for the base image). | |
2074 | */ | |
dfc5606d YS |
2075 | static ssize_t rbd_snap_show(struct device *dev, |
2076 | struct device_attribute *attr, | |
2077 | char *buf) | |
2078 | { | |
593a9e7b | 2079 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
dfc5606d | 2080 | |
0d7dbfce | 2081 | return sprintf(buf, "%s\n", rbd_dev->spec->snap_name); |
dfc5606d YS |
2082 | } |
2083 | ||
86b00e0d AE |
2084 | /* |
2085 | * For an rbd v2 image, shows the pool id, image id, and snapshot id | |
2086 | * for the parent image. If there is no parent, simply shows | |
2087 | * "(no parent image)". | |
2088 | */ | |
2089 | static ssize_t rbd_parent_show(struct device *dev, | |
2090 | struct device_attribute *attr, | |
2091 | char *buf) | |
2092 | { | |
2093 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
2094 | struct rbd_spec *spec = rbd_dev->parent_spec; | |
2095 | int count; | |
2096 | char *bufp = buf; | |
2097 | ||
2098 | if (!spec) | |
2099 | return sprintf(buf, "(no parent image)\n"); | |
2100 | ||
2101 | count = sprintf(bufp, "pool_id %llu\npool_name %s\n", | |
2102 | (unsigned long long) spec->pool_id, spec->pool_name); | |
2103 | if (count < 0) | |
2104 | return count; | |
2105 | bufp += count; | |
2106 | ||
2107 | count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id, | |
2108 | spec->image_name ? spec->image_name : "(unknown)"); | |
2109 | if (count < 0) | |
2110 | return count; | |
2111 | bufp += count; | |
2112 | ||
2113 | count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n", | |
2114 | (unsigned long long) spec->snap_id, spec->snap_name); | |
2115 | if (count < 0) | |
2116 | return count; | |
2117 | bufp += count; | |
2118 | ||
2119 | count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap); | |
2120 | if (count < 0) | |
2121 | return count; | |
2122 | bufp += count; | |
2123 | ||
2124 | return (ssize_t) (bufp - buf); | |
2125 | } | |
2126 | ||
dfc5606d YS |
2127 | static ssize_t rbd_image_refresh(struct device *dev, |
2128 | struct device_attribute *attr, | |
2129 | const char *buf, | |
2130 | size_t size) | |
2131 | { | |
593a9e7b | 2132 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
b813623a | 2133 | int ret; |
602adf40 | 2134 | |
117973fb | 2135 | ret = rbd_dev_refresh(rbd_dev, NULL); |
b813623a AE |
2136 | |
2137 | return ret < 0 ? ret : size; | |
dfc5606d | 2138 | } |
602adf40 | 2139 | |
dfc5606d | 2140 | static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL); |
34b13184 | 2141 | static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL); |
dfc5606d YS |
2142 | static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL); |
2143 | static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL); | |
2144 | static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL); | |
9bb2f334 | 2145 | static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL); |
dfc5606d | 2146 | static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL); |
589d30e0 | 2147 | static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL); |
dfc5606d YS |
2148 | static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh); |
2149 | static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL); | |
86b00e0d | 2150 | static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL); |
dfc5606d YS |
2151 | |
2152 | static struct attribute *rbd_attrs[] = { | |
2153 | &dev_attr_size.attr, | |
34b13184 | 2154 | &dev_attr_features.attr, |
dfc5606d YS |
2155 | &dev_attr_major.attr, |
2156 | &dev_attr_client_id.attr, | |
2157 | &dev_attr_pool.attr, | |
9bb2f334 | 2158 | &dev_attr_pool_id.attr, |
dfc5606d | 2159 | &dev_attr_name.attr, |
589d30e0 | 2160 | &dev_attr_image_id.attr, |
dfc5606d | 2161 | &dev_attr_current_snap.attr, |
86b00e0d | 2162 | &dev_attr_parent.attr, |
dfc5606d | 2163 | &dev_attr_refresh.attr, |
dfc5606d YS |
2164 | NULL |
2165 | }; | |
2166 | ||
2167 | static struct attribute_group rbd_attr_group = { | |
2168 | .attrs = rbd_attrs, | |
2169 | }; | |
2170 | ||
2171 | static const struct attribute_group *rbd_attr_groups[] = { | |
2172 | &rbd_attr_group, | |
2173 | NULL | |
2174 | }; | |
2175 | ||
2176 | static void rbd_sysfs_dev_release(struct device *dev) | |
2177 | { | |
2178 | } | |
2179 | ||
2180 | static struct device_type rbd_device_type = { | |
2181 | .name = "rbd", | |
2182 | .groups = rbd_attr_groups, | |
2183 | .release = rbd_sysfs_dev_release, | |
2184 | }; | |
2185 | ||
2186 | ||
2187 | /* | |
2188 | sysfs - snapshots | |
2189 | */ | |
2190 | ||
2191 | static ssize_t rbd_snap_size_show(struct device *dev, | |
2192 | struct device_attribute *attr, | |
2193 | char *buf) | |
2194 | { | |
2195 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); | |
2196 | ||
3591538f | 2197 | return sprintf(buf, "%llu\n", (unsigned long long)snap->size); |
dfc5606d YS |
2198 | } |
2199 | ||
2200 | static ssize_t rbd_snap_id_show(struct device *dev, | |
2201 | struct device_attribute *attr, | |
2202 | char *buf) | |
2203 | { | |
2204 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); | |
2205 | ||
3591538f | 2206 | return sprintf(buf, "%llu\n", (unsigned long long)snap->id); |
dfc5606d YS |
2207 | } |
2208 | ||
34b13184 AE |
2209 | static ssize_t rbd_snap_features_show(struct device *dev, |
2210 | struct device_attribute *attr, | |
2211 | char *buf) | |
2212 | { | |
2213 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); | |
2214 | ||
2215 | return sprintf(buf, "0x%016llx\n", | |
2216 | (unsigned long long) snap->features); | |
2217 | } | |
2218 | ||
dfc5606d YS |
2219 | static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL); |
2220 | static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL); | |
34b13184 | 2221 | static DEVICE_ATTR(snap_features, S_IRUGO, rbd_snap_features_show, NULL); |
dfc5606d YS |
2222 | |
2223 | static struct attribute *rbd_snap_attrs[] = { | |
2224 | &dev_attr_snap_size.attr, | |
2225 | &dev_attr_snap_id.attr, | |
34b13184 | 2226 | &dev_attr_snap_features.attr, |
dfc5606d YS |
2227 | NULL, |
2228 | }; | |
2229 | ||
2230 | static struct attribute_group rbd_snap_attr_group = { | |
2231 | .attrs = rbd_snap_attrs, | |
2232 | }; | |
2233 | ||
2234 | static void rbd_snap_dev_release(struct device *dev) | |
2235 | { | |
2236 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); | |
2237 | kfree(snap->name); | |
2238 | kfree(snap); | |
2239 | } | |
2240 | ||
2241 | static const struct attribute_group *rbd_snap_attr_groups[] = { | |
2242 | &rbd_snap_attr_group, | |
2243 | NULL | |
2244 | }; | |
2245 | ||
2246 | static struct device_type rbd_snap_device_type = { | |
2247 | .groups = rbd_snap_attr_groups, | |
2248 | .release = rbd_snap_dev_release, | |
2249 | }; | |
2250 | ||
8b8fb99c AE |
2251 | static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec) |
2252 | { | |
2253 | kref_get(&spec->kref); | |
2254 | ||
2255 | return spec; | |
2256 | } | |
2257 | ||
2258 | static void rbd_spec_free(struct kref *kref); | |
2259 | static void rbd_spec_put(struct rbd_spec *spec) | |
2260 | { | |
2261 | if (spec) | |
2262 | kref_put(&spec->kref, rbd_spec_free); | |
2263 | } | |
2264 | ||
2265 | static struct rbd_spec *rbd_spec_alloc(void) | |
2266 | { | |
2267 | struct rbd_spec *spec; | |
2268 | ||
2269 | spec = kzalloc(sizeof (*spec), GFP_KERNEL); | |
2270 | if (!spec) | |
2271 | return NULL; | |
2272 | kref_init(&spec->kref); | |
2273 | ||
2274 | rbd_spec_put(rbd_spec_get(spec)); /* TEMPORARY */ | |
2275 | ||
2276 | return spec; | |
2277 | } | |
2278 | ||
2279 | static void rbd_spec_free(struct kref *kref) | |
2280 | { | |
2281 | struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref); | |
2282 | ||
2283 | kfree(spec->pool_name); | |
2284 | kfree(spec->image_id); | |
2285 | kfree(spec->image_name); | |
2286 | kfree(spec->snap_name); | |
2287 | kfree(spec); | |
2288 | } | |
2289 | ||
c53d5893 AE |
2290 | struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, |
2291 | struct rbd_spec *spec) | |
2292 | { | |
2293 | struct rbd_device *rbd_dev; | |
2294 | ||
2295 | rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL); | |
2296 | if (!rbd_dev) | |
2297 | return NULL; | |
2298 | ||
2299 | spin_lock_init(&rbd_dev->lock); | |
d78b650a | 2300 | atomic_set(&rbd_dev->exists, 0); |
c53d5893 AE |
2301 | INIT_LIST_HEAD(&rbd_dev->node); |
2302 | INIT_LIST_HEAD(&rbd_dev->snaps); | |
2303 | init_rwsem(&rbd_dev->header_rwsem); | |
2304 | ||
2305 | rbd_dev->spec = spec; | |
2306 | rbd_dev->rbd_client = rbdc; | |
2307 | ||
2308 | return rbd_dev; | |
2309 | } | |
2310 | ||
2311 | static void rbd_dev_destroy(struct rbd_device *rbd_dev) | |
2312 | { | |
86b00e0d | 2313 | rbd_spec_put(rbd_dev->parent_spec); |
c53d5893 AE |
2314 | kfree(rbd_dev->header_name); |
2315 | rbd_put_client(rbd_dev->rbd_client); | |
2316 | rbd_spec_put(rbd_dev->spec); | |
2317 | kfree(rbd_dev); | |
2318 | } | |
2319 | ||
304f6808 AE |
2320 | static bool rbd_snap_registered(struct rbd_snap *snap) |
2321 | { | |
2322 | bool ret = snap->dev.type == &rbd_snap_device_type; | |
2323 | bool reg = device_is_registered(&snap->dev); | |
2324 | ||
2325 | rbd_assert(!ret ^ reg); | |
2326 | ||
2327 | return ret; | |
2328 | } | |
2329 | ||
41f38c2b | 2330 | static void rbd_remove_snap_dev(struct rbd_snap *snap) |
dfc5606d YS |
2331 | { |
2332 | list_del(&snap->node); | |
304f6808 AE |
2333 | if (device_is_registered(&snap->dev)) |
2334 | device_unregister(&snap->dev); | |
dfc5606d YS |
2335 | } |
2336 | ||
14e7085d | 2337 | static int rbd_register_snap_dev(struct rbd_snap *snap, |
dfc5606d YS |
2338 | struct device *parent) |
2339 | { | |
2340 | struct device *dev = &snap->dev; | |
2341 | int ret; | |
2342 | ||
2343 | dev->type = &rbd_snap_device_type; | |
2344 | dev->parent = parent; | |
2345 | dev->release = rbd_snap_dev_release; | |
d4b125e9 | 2346 | dev_set_name(dev, "%s%s", RBD_SNAP_DEV_NAME_PREFIX, snap->name); |
304f6808 AE |
2347 | dout("%s: registering device for snapshot %s\n", __func__, snap->name); |
2348 | ||
dfc5606d YS |
2349 | ret = device_register(dev); |
2350 | ||
2351 | return ret; | |
2352 | } | |
2353 | ||
4e891e0a | 2354 | static struct rbd_snap *__rbd_add_snap_dev(struct rbd_device *rbd_dev, |
c8d18425 | 2355 | const char *snap_name, |
34b13184 AE |
2356 | u64 snap_id, u64 snap_size, |
2357 | u64 snap_features) | |
dfc5606d | 2358 | { |
4e891e0a | 2359 | struct rbd_snap *snap; |
dfc5606d | 2360 | int ret; |
4e891e0a AE |
2361 | |
2362 | snap = kzalloc(sizeof (*snap), GFP_KERNEL); | |
dfc5606d | 2363 | if (!snap) |
4e891e0a AE |
2364 | return ERR_PTR(-ENOMEM); |
2365 | ||
2366 | ret = -ENOMEM; | |
c8d18425 | 2367 | snap->name = kstrdup(snap_name, GFP_KERNEL); |
4e891e0a AE |
2368 | if (!snap->name) |
2369 | goto err; | |
2370 | ||
c8d18425 AE |
2371 | snap->id = snap_id; |
2372 | snap->size = snap_size; | |
34b13184 | 2373 | snap->features = snap_features; |
4e891e0a AE |
2374 | |
2375 | return snap; | |
2376 | ||
dfc5606d YS |
2377 | err: |
2378 | kfree(snap->name); | |
2379 | kfree(snap); | |
4e891e0a AE |
2380 | |
2381 | return ERR_PTR(ret); | |
dfc5606d YS |
2382 | } |
2383 | ||
cd892126 AE |
2384 | static char *rbd_dev_v1_snap_info(struct rbd_device *rbd_dev, u32 which, |
2385 | u64 *snap_size, u64 *snap_features) | |
2386 | { | |
2387 | char *snap_name; | |
2388 | ||
2389 | rbd_assert(which < rbd_dev->header.snapc->num_snaps); | |
2390 | ||
2391 | *snap_size = rbd_dev->header.snap_sizes[which]; | |
2392 | *snap_features = 0; /* No features for v1 */ | |
2393 | ||
2394 | /* Skip over names until we find the one we are looking for */ | |
2395 | ||
2396 | snap_name = rbd_dev->header.snap_names; | |
2397 | while (which--) | |
2398 | snap_name += strlen(snap_name) + 1; | |
2399 | ||
2400 | return snap_name; | |
2401 | } | |
2402 | ||
9d475de5 AE |
2403 | /* |
2404 | * Get the size and object order for an image snapshot, or if | |
2405 | * snap_id is CEPH_NOSNAP, gets this information for the base | |
2406 | * image. | |
2407 | */ | |
2408 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, | |
2409 | u8 *order, u64 *snap_size) | |
2410 | { | |
2411 | __le64 snapid = cpu_to_le64(snap_id); | |
2412 | int ret; | |
2413 | struct { | |
2414 | u8 order; | |
2415 | __le64 size; | |
2416 | } __attribute__ ((packed)) size_buf = { 0 }; | |
2417 | ||
2418 | ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name, | |
2419 | "rbd", "get_size", | |
2420 | (char *) &snapid, sizeof (snapid), | |
2421 | (char *) &size_buf, sizeof (size_buf), | |
2422 | CEPH_OSD_FLAG_READ, NULL); | |
2423 | dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret); | |
2424 | if (ret < 0) | |
2425 | return ret; | |
2426 | ||
2427 | *order = size_buf.order; | |
2428 | *snap_size = le64_to_cpu(size_buf.size); | |
2429 | ||
2430 | dout(" snap_id 0x%016llx order = %u, snap_size = %llu\n", | |
2431 | (unsigned long long) snap_id, (unsigned int) *order, | |
2432 | (unsigned long long) *snap_size); | |
2433 | ||
2434 | return 0; | |
2435 | } | |
2436 | ||
2437 | static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev) | |
2438 | { | |
2439 | return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP, | |
2440 | &rbd_dev->header.obj_order, | |
2441 | &rbd_dev->header.image_size); | |
2442 | } | |
2443 | ||
1e130199 AE |
2444 | static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev) |
2445 | { | |
2446 | void *reply_buf; | |
2447 | int ret; | |
2448 | void *p; | |
2449 | ||
2450 | reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL); | |
2451 | if (!reply_buf) | |
2452 | return -ENOMEM; | |
2453 | ||
2454 | ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name, | |
2455 | "rbd", "get_object_prefix", | |
2456 | NULL, 0, | |
2457 | reply_buf, RBD_OBJ_PREFIX_LEN_MAX, | |
2458 | CEPH_OSD_FLAG_READ, NULL); | |
2459 | dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret); | |
2460 | if (ret < 0) | |
2461 | goto out; | |
a0ea3a40 | 2462 | ret = 0; /* rbd_req_sync_exec() can return positive */ |
1e130199 AE |
2463 | |
2464 | p = reply_buf; | |
2465 | rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p, | |
2466 | p + RBD_OBJ_PREFIX_LEN_MAX, | |
2467 | NULL, GFP_NOIO); | |
2468 | ||
2469 | if (IS_ERR(rbd_dev->header.object_prefix)) { | |
2470 | ret = PTR_ERR(rbd_dev->header.object_prefix); | |
2471 | rbd_dev->header.object_prefix = NULL; | |
2472 | } else { | |
2473 | dout(" object_prefix = %s\n", rbd_dev->header.object_prefix); | |
2474 | } | |
2475 | ||
2476 | out: | |
2477 | kfree(reply_buf); | |
2478 | ||
2479 | return ret; | |
2480 | } | |
2481 | ||
b1b5402a AE |
2482 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
2483 | u64 *snap_features) | |
2484 | { | |
2485 | __le64 snapid = cpu_to_le64(snap_id); | |
2486 | struct { | |
2487 | __le64 features; | |
2488 | __le64 incompat; | |
2489 | } features_buf = { 0 }; | |
d889140c | 2490 | u64 incompat; |
b1b5402a AE |
2491 | int ret; |
2492 | ||
2493 | ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name, | |
2494 | "rbd", "get_features", | |
2495 | (char *) &snapid, sizeof (snapid), | |
2496 | (char *) &features_buf, sizeof (features_buf), | |
2497 | CEPH_OSD_FLAG_READ, NULL); | |
2498 | dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret); | |
2499 | if (ret < 0) | |
2500 | return ret; | |
d889140c AE |
2501 | |
2502 | incompat = le64_to_cpu(features_buf.incompat); | |
2503 | if (incompat & ~RBD_FEATURES_ALL) | |
b8f5c6ed | 2504 | return -ENXIO; |
d889140c | 2505 | |
b1b5402a AE |
2506 | *snap_features = le64_to_cpu(features_buf.features); |
2507 | ||
2508 | dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n", | |
2509 | (unsigned long long) snap_id, | |
2510 | (unsigned long long) *snap_features, | |
2511 | (unsigned long long) le64_to_cpu(features_buf.incompat)); | |
2512 | ||
2513 | return 0; | |
2514 | } | |
2515 | ||
2516 | static int rbd_dev_v2_features(struct rbd_device *rbd_dev) | |
2517 | { | |
2518 | return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP, | |
2519 | &rbd_dev->header.features); | |
2520 | } | |
2521 | ||
86b00e0d AE |
2522 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev) |
2523 | { | |
2524 | struct rbd_spec *parent_spec; | |
2525 | size_t size; | |
2526 | void *reply_buf = NULL; | |
2527 | __le64 snapid; | |
2528 | void *p; | |
2529 | void *end; | |
2530 | char *image_id; | |
2531 | u64 overlap; | |
86b00e0d AE |
2532 | int ret; |
2533 | ||
2534 | parent_spec = rbd_spec_alloc(); | |
2535 | if (!parent_spec) | |
2536 | return -ENOMEM; | |
2537 | ||
2538 | size = sizeof (__le64) + /* pool_id */ | |
2539 | sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */ | |
2540 | sizeof (__le64) + /* snap_id */ | |
2541 | sizeof (__le64); /* overlap */ | |
2542 | reply_buf = kmalloc(size, GFP_KERNEL); | |
2543 | if (!reply_buf) { | |
2544 | ret = -ENOMEM; | |
2545 | goto out_err; | |
2546 | } | |
2547 | ||
2548 | snapid = cpu_to_le64(CEPH_NOSNAP); | |
2549 | ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name, | |
2550 | "rbd", "get_parent", | |
2551 | (char *) &snapid, sizeof (snapid), | |
2552 | (char *) reply_buf, size, | |
2553 | CEPH_OSD_FLAG_READ, NULL); | |
2554 | dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret); | |
2555 | if (ret < 0) | |
2556 | goto out_err; | |
2557 | ||
2558 | ret = -ERANGE; | |
2559 | p = reply_buf; | |
2560 | end = (char *) reply_buf + size; | |
2561 | ceph_decode_64_safe(&p, end, parent_spec->pool_id, out_err); | |
2562 | if (parent_spec->pool_id == CEPH_NOPOOL) | |
2563 | goto out; /* No parent? No problem. */ | |
2564 | ||
979ed480 | 2565 | image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
86b00e0d AE |
2566 | if (IS_ERR(image_id)) { |
2567 | ret = PTR_ERR(image_id); | |
2568 | goto out_err; | |
2569 | } | |
2570 | parent_spec->image_id = image_id; | |
2571 | ceph_decode_64_safe(&p, end, parent_spec->snap_id, out_err); | |
2572 | ceph_decode_64_safe(&p, end, overlap, out_err); | |
2573 | ||
2574 | rbd_dev->parent_overlap = overlap; | |
2575 | rbd_dev->parent_spec = parent_spec; | |
2576 | parent_spec = NULL; /* rbd_dev now owns this */ | |
2577 | out: | |
2578 | ret = 0; | |
2579 | out_err: | |
2580 | kfree(reply_buf); | |
2581 | rbd_spec_put(parent_spec); | |
2582 | ||
2583 | return ret; | |
2584 | } | |
2585 | ||
9e15b77d AE |
2586 | static char *rbd_dev_image_name(struct rbd_device *rbd_dev) |
2587 | { | |
2588 | size_t image_id_size; | |
2589 | char *image_id; | |
2590 | void *p; | |
2591 | void *end; | |
2592 | size_t size; | |
2593 | void *reply_buf = NULL; | |
2594 | size_t len = 0; | |
2595 | char *image_name = NULL; | |
2596 | int ret; | |
2597 | ||
2598 | rbd_assert(!rbd_dev->spec->image_name); | |
2599 | ||
69e7a02f AE |
2600 | len = strlen(rbd_dev->spec->image_id); |
2601 | image_id_size = sizeof (__le32) + len; | |
9e15b77d AE |
2602 | image_id = kmalloc(image_id_size, GFP_KERNEL); |
2603 | if (!image_id) | |
2604 | return NULL; | |
2605 | ||
2606 | p = image_id; | |
2607 | end = (char *) image_id + image_id_size; | |
69e7a02f | 2608 | ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32) len); |
9e15b77d AE |
2609 | |
2610 | size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX; | |
2611 | reply_buf = kmalloc(size, GFP_KERNEL); | |
2612 | if (!reply_buf) | |
2613 | goto out; | |
2614 | ||
2615 | ret = rbd_req_sync_exec(rbd_dev, RBD_DIRECTORY, | |
2616 | "rbd", "dir_get_name", | |
2617 | image_id, image_id_size, | |
2618 | (char *) reply_buf, size, | |
2619 | CEPH_OSD_FLAG_READ, NULL); | |
2620 | if (ret < 0) | |
2621 | goto out; | |
2622 | p = reply_buf; | |
2623 | end = (char *) reply_buf + size; | |
2624 | image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL); | |
2625 | if (IS_ERR(image_name)) | |
2626 | image_name = NULL; | |
2627 | else | |
2628 | dout("%s: name is %s len is %zd\n", __func__, image_name, len); | |
2629 | out: | |
2630 | kfree(reply_buf); | |
2631 | kfree(image_id); | |
2632 | ||
2633 | return image_name; | |
2634 | } | |
2635 | ||
2636 | /* | |
2637 | * When a parent image gets probed, we only have the pool, image, | |
2638 | * and snapshot ids but not the names of any of them. This call | |
2639 | * is made later to fill in those names. It has to be done after | |
2640 | * rbd_dev_snaps_update() has completed because some of the | |
2641 | * information (in particular, snapshot name) is not available | |
2642 | * until then. | |
2643 | */ | |
2644 | static int rbd_dev_probe_update_spec(struct rbd_device *rbd_dev) | |
2645 | { | |
2646 | struct ceph_osd_client *osdc; | |
2647 | const char *name; | |
2648 | void *reply_buf = NULL; | |
2649 | int ret; | |
2650 | ||
2651 | if (rbd_dev->spec->pool_name) | |
2652 | return 0; /* Already have the names */ | |
2653 | ||
2654 | /* Look up the pool name */ | |
2655 | ||
2656 | osdc = &rbd_dev->rbd_client->client->osdc; | |
2657 | name = ceph_pg_pool_name_by_id(osdc->osdmap, rbd_dev->spec->pool_id); | |
935dc89f AE |
2658 | if (!name) { |
2659 | rbd_warn(rbd_dev, "there is no pool with id %llu", | |
2660 | rbd_dev->spec->pool_id); /* Really a BUG() */ | |
2661 | return -EIO; | |
2662 | } | |
9e15b77d AE |
2663 | |
2664 | rbd_dev->spec->pool_name = kstrdup(name, GFP_KERNEL); | |
2665 | if (!rbd_dev->spec->pool_name) | |
2666 | return -ENOMEM; | |
2667 | ||
2668 | /* Fetch the image name; tolerate failure here */ | |
2669 | ||
2670 | name = rbd_dev_image_name(rbd_dev); | |
69e7a02f | 2671 | if (name) |
9e15b77d | 2672 | rbd_dev->spec->image_name = (char *) name; |
69e7a02f | 2673 | else |
06ecc6cb | 2674 | rbd_warn(rbd_dev, "unable to get image name"); |
9e15b77d AE |
2675 | |
2676 | /* Look up the snapshot name. */ | |
2677 | ||
2678 | name = rbd_snap_name(rbd_dev, rbd_dev->spec->snap_id); | |
2679 | if (!name) { | |
935dc89f AE |
2680 | rbd_warn(rbd_dev, "no snapshot with id %llu", |
2681 | rbd_dev->spec->snap_id); /* Really a BUG() */ | |
9e15b77d AE |
2682 | ret = -EIO; |
2683 | goto out_err; | |
2684 | } | |
2685 | rbd_dev->spec->snap_name = kstrdup(name, GFP_KERNEL); | |
2686 | if(!rbd_dev->spec->snap_name) | |
2687 | goto out_err; | |
2688 | ||
2689 | return 0; | |
2690 | out_err: | |
2691 | kfree(reply_buf); | |
2692 | kfree(rbd_dev->spec->pool_name); | |
2693 | rbd_dev->spec->pool_name = NULL; | |
2694 | ||
2695 | return ret; | |
2696 | } | |
2697 | ||
6e14b1a6 | 2698 | static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev, u64 *ver) |
35d489f9 AE |
2699 | { |
2700 | size_t size; | |
2701 | int ret; | |
2702 | void *reply_buf; | |
2703 | void *p; | |
2704 | void *end; | |
2705 | u64 seq; | |
2706 | u32 snap_count; | |
2707 | struct ceph_snap_context *snapc; | |
2708 | u32 i; | |
2709 | ||
2710 | /* | |
2711 | * We'll need room for the seq value (maximum snapshot id), | |
2712 | * snapshot count, and array of that many snapshot ids. | |
2713 | * For now we have a fixed upper limit on the number we're | |
2714 | * prepared to receive. | |
2715 | */ | |
2716 | size = sizeof (__le64) + sizeof (__le32) + | |
2717 | RBD_MAX_SNAP_COUNT * sizeof (__le64); | |
2718 | reply_buf = kzalloc(size, GFP_KERNEL); | |
2719 | if (!reply_buf) | |
2720 | return -ENOMEM; | |
2721 | ||
2722 | ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name, | |
2723 | "rbd", "get_snapcontext", | |
2724 | NULL, 0, | |
2725 | reply_buf, size, | |
6e14b1a6 | 2726 | CEPH_OSD_FLAG_READ, ver); |
35d489f9 AE |
2727 | dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret); |
2728 | if (ret < 0) | |
2729 | goto out; | |
2730 | ||
2731 | ret = -ERANGE; | |
2732 | p = reply_buf; | |
2733 | end = (char *) reply_buf + size; | |
2734 | ceph_decode_64_safe(&p, end, seq, out); | |
2735 | ceph_decode_32_safe(&p, end, snap_count, out); | |
2736 | ||
2737 | /* | |
2738 | * Make sure the reported number of snapshot ids wouldn't go | |
2739 | * beyond the end of our buffer. But before checking that, | |
2740 | * make sure the computed size of the snapshot context we | |
2741 | * allocate is representable in a size_t. | |
2742 | */ | |
2743 | if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context)) | |
2744 | / sizeof (u64)) { | |
2745 | ret = -EINVAL; | |
2746 | goto out; | |
2747 | } | |
2748 | if (!ceph_has_room(&p, end, snap_count * sizeof (__le64))) | |
2749 | goto out; | |
2750 | ||
2751 | size = sizeof (struct ceph_snap_context) + | |
2752 | snap_count * sizeof (snapc->snaps[0]); | |
2753 | snapc = kmalloc(size, GFP_KERNEL); | |
2754 | if (!snapc) { | |
2755 | ret = -ENOMEM; | |
2756 | goto out; | |
2757 | } | |
2758 | ||
2759 | atomic_set(&snapc->nref, 1); | |
2760 | snapc->seq = seq; | |
2761 | snapc->num_snaps = snap_count; | |
2762 | for (i = 0; i < snap_count; i++) | |
2763 | snapc->snaps[i] = ceph_decode_64(&p); | |
2764 | ||
2765 | rbd_dev->header.snapc = snapc; | |
2766 | ||
2767 | dout(" snap context seq = %llu, snap_count = %u\n", | |
2768 | (unsigned long long) seq, (unsigned int) snap_count); | |
2769 | ||
2770 | out: | |
2771 | kfree(reply_buf); | |
2772 | ||
2773 | return 0; | |
2774 | } | |
2775 | ||
b8b1e2db AE |
2776 | static char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, u32 which) |
2777 | { | |
2778 | size_t size; | |
2779 | void *reply_buf; | |
2780 | __le64 snap_id; | |
2781 | int ret; | |
2782 | void *p; | |
2783 | void *end; | |
b8b1e2db AE |
2784 | char *snap_name; |
2785 | ||
2786 | size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN; | |
2787 | reply_buf = kmalloc(size, GFP_KERNEL); | |
2788 | if (!reply_buf) | |
2789 | return ERR_PTR(-ENOMEM); | |
2790 | ||
2791 | snap_id = cpu_to_le64(rbd_dev->header.snapc->snaps[which]); | |
2792 | ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name, | |
2793 | "rbd", "get_snapshot_name", | |
2794 | (char *) &snap_id, sizeof (snap_id), | |
2795 | reply_buf, size, | |
2796 | CEPH_OSD_FLAG_READ, NULL); | |
2797 | dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret); | |
2798 | if (ret < 0) | |
2799 | goto out; | |
2800 | ||
2801 | p = reply_buf; | |
2802 | end = (char *) reply_buf + size; | |
e5c35534 | 2803 | snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
b8b1e2db AE |
2804 | if (IS_ERR(snap_name)) { |
2805 | ret = PTR_ERR(snap_name); | |
2806 | goto out; | |
2807 | } else { | |
2808 | dout(" snap_id 0x%016llx snap_name = %s\n", | |
2809 | (unsigned long long) le64_to_cpu(snap_id), snap_name); | |
2810 | } | |
2811 | kfree(reply_buf); | |
2812 | ||
2813 | return snap_name; | |
2814 | out: | |
2815 | kfree(reply_buf); | |
2816 | ||
2817 | return ERR_PTR(ret); | |
2818 | } | |
2819 | ||
2820 | static char *rbd_dev_v2_snap_info(struct rbd_device *rbd_dev, u32 which, | |
2821 | u64 *snap_size, u64 *snap_features) | |
2822 | { | |
2823 | __le64 snap_id; | |
2824 | u8 order; | |
2825 | int ret; | |
2826 | ||
2827 | snap_id = rbd_dev->header.snapc->snaps[which]; | |
2828 | ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, &order, snap_size); | |
2829 | if (ret) | |
2830 | return ERR_PTR(ret); | |
2831 | ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, snap_features); | |
2832 | if (ret) | |
2833 | return ERR_PTR(ret); | |
2834 | ||
2835 | return rbd_dev_v2_snap_name(rbd_dev, which); | |
2836 | } | |
2837 | ||
2838 | static char *rbd_dev_snap_info(struct rbd_device *rbd_dev, u32 which, | |
2839 | u64 *snap_size, u64 *snap_features) | |
2840 | { | |
2841 | if (rbd_dev->image_format == 1) | |
2842 | return rbd_dev_v1_snap_info(rbd_dev, which, | |
2843 | snap_size, snap_features); | |
2844 | if (rbd_dev->image_format == 2) | |
2845 | return rbd_dev_v2_snap_info(rbd_dev, which, | |
2846 | snap_size, snap_features); | |
2847 | return ERR_PTR(-EINVAL); | |
2848 | } | |
2849 | ||
117973fb AE |
2850 | static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver) |
2851 | { | |
2852 | int ret; | |
2853 | __u8 obj_order; | |
2854 | ||
2855 | down_write(&rbd_dev->header_rwsem); | |
2856 | ||
2857 | /* Grab old order first, to see if it changes */ | |
2858 | ||
2859 | obj_order = rbd_dev->header.obj_order, | |
2860 | ret = rbd_dev_v2_image_size(rbd_dev); | |
2861 | if (ret) | |
2862 | goto out; | |
2863 | if (rbd_dev->header.obj_order != obj_order) { | |
2864 | ret = -EIO; | |
2865 | goto out; | |
2866 | } | |
2867 | rbd_update_mapping_size(rbd_dev); | |
2868 | ||
2869 | ret = rbd_dev_v2_snap_context(rbd_dev, hver); | |
2870 | dout("rbd_dev_v2_snap_context returned %d\n", ret); | |
2871 | if (ret) | |
2872 | goto out; | |
2873 | ret = rbd_dev_snaps_update(rbd_dev); | |
2874 | dout("rbd_dev_snaps_update returned %d\n", ret); | |
2875 | if (ret) | |
2876 | goto out; | |
2877 | ret = rbd_dev_snaps_register(rbd_dev); | |
2878 | dout("rbd_dev_snaps_register returned %d\n", ret); | |
2879 | out: | |
2880 | up_write(&rbd_dev->header_rwsem); | |
2881 | ||
2882 | return ret; | |
2883 | } | |
2884 | ||
dfc5606d | 2885 | /* |
35938150 AE |
2886 | * Scan the rbd device's current snapshot list and compare it to the |
2887 | * newly-received snapshot context. Remove any existing snapshots | |
2888 | * not present in the new snapshot context. Add a new snapshot for | |
2889 | * any snaphots in the snapshot context not in the current list. | |
2890 | * And verify there are no changes to snapshots we already know | |
2891 | * about. | |
2892 | * | |
2893 | * Assumes the snapshots in the snapshot context are sorted by | |
2894 | * snapshot id, highest id first. (Snapshots in the rbd_dev's list | |
2895 | * are also maintained in that order.) | |
dfc5606d | 2896 | */ |
304f6808 | 2897 | static int rbd_dev_snaps_update(struct rbd_device *rbd_dev) |
dfc5606d | 2898 | { |
35938150 AE |
2899 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
2900 | const u32 snap_count = snapc->num_snaps; | |
35938150 AE |
2901 | struct list_head *head = &rbd_dev->snaps; |
2902 | struct list_head *links = head->next; | |
2903 | u32 index = 0; | |
dfc5606d | 2904 | |
9fcbb800 | 2905 | dout("%s: snap count is %u\n", __func__, (unsigned int) snap_count); |
35938150 AE |
2906 | while (index < snap_count || links != head) { |
2907 | u64 snap_id; | |
2908 | struct rbd_snap *snap; | |
cd892126 AE |
2909 | char *snap_name; |
2910 | u64 snap_size = 0; | |
2911 | u64 snap_features = 0; | |
dfc5606d | 2912 | |
35938150 AE |
2913 | snap_id = index < snap_count ? snapc->snaps[index] |
2914 | : CEPH_NOSNAP; | |
2915 | snap = links != head ? list_entry(links, struct rbd_snap, node) | |
2916 | : NULL; | |
aafb230e | 2917 | rbd_assert(!snap || snap->id != CEPH_NOSNAP); |
dfc5606d | 2918 | |
35938150 AE |
2919 | if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) { |
2920 | struct list_head *next = links->next; | |
dfc5606d | 2921 | |
35938150 | 2922 | /* Existing snapshot not in the new snap context */ |
dfc5606d | 2923 | |
0d7dbfce | 2924 | if (rbd_dev->spec->snap_id == snap->id) |
d78b650a | 2925 | atomic_set(&rbd_dev->exists, 0); |
41f38c2b | 2926 | rbd_remove_snap_dev(snap); |
9fcbb800 | 2927 | dout("%ssnap id %llu has been removed\n", |
0d7dbfce AE |
2928 | rbd_dev->spec->snap_id == snap->id ? |
2929 | "mapped " : "", | |
9fcbb800 | 2930 | (unsigned long long) snap->id); |
35938150 AE |
2931 | |
2932 | /* Done with this list entry; advance */ | |
2933 | ||
2934 | links = next; | |
dfc5606d YS |
2935 | continue; |
2936 | } | |
35938150 | 2937 | |
b8b1e2db AE |
2938 | snap_name = rbd_dev_snap_info(rbd_dev, index, |
2939 | &snap_size, &snap_features); | |
cd892126 AE |
2940 | if (IS_ERR(snap_name)) |
2941 | return PTR_ERR(snap_name); | |
2942 | ||
9fcbb800 AE |
2943 | dout("entry %u: snap_id = %llu\n", (unsigned int) snap_count, |
2944 | (unsigned long long) snap_id); | |
35938150 AE |
2945 | if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) { |
2946 | struct rbd_snap *new_snap; | |
2947 | ||
2948 | /* We haven't seen this snapshot before */ | |
2949 | ||
c8d18425 | 2950 | new_snap = __rbd_add_snap_dev(rbd_dev, snap_name, |
cd892126 | 2951 | snap_id, snap_size, snap_features); |
9fcbb800 AE |
2952 | if (IS_ERR(new_snap)) { |
2953 | int err = PTR_ERR(new_snap); | |
2954 | ||
2955 | dout(" failed to add dev, error %d\n", err); | |
2956 | ||
2957 | return err; | |
2958 | } | |
35938150 AE |
2959 | |
2960 | /* New goes before existing, or at end of list */ | |
2961 | ||
9fcbb800 | 2962 | dout(" added dev%s\n", snap ? "" : " at end\n"); |
35938150 AE |
2963 | if (snap) |
2964 | list_add_tail(&new_snap->node, &snap->node); | |
2965 | else | |
523f3258 | 2966 | list_add_tail(&new_snap->node, head); |
35938150 AE |
2967 | } else { |
2968 | /* Already have this one */ | |
2969 | ||
9fcbb800 AE |
2970 | dout(" already present\n"); |
2971 | ||
cd892126 | 2972 | rbd_assert(snap->size == snap_size); |
aafb230e | 2973 | rbd_assert(!strcmp(snap->name, snap_name)); |
cd892126 | 2974 | rbd_assert(snap->features == snap_features); |
35938150 AE |
2975 | |
2976 | /* Done with this list entry; advance */ | |
2977 | ||
2978 | links = links->next; | |
dfc5606d | 2979 | } |
35938150 AE |
2980 | |
2981 | /* Advance to the next entry in the snapshot context */ | |
2982 | ||
2983 | index++; | |
dfc5606d | 2984 | } |
9fcbb800 | 2985 | dout("%s: done\n", __func__); |
dfc5606d YS |
2986 | |
2987 | return 0; | |
2988 | } | |
2989 | ||
304f6808 AE |
2990 | /* |
2991 | * Scan the list of snapshots and register the devices for any that | |
2992 | * have not already been registered. | |
2993 | */ | |
2994 | static int rbd_dev_snaps_register(struct rbd_device *rbd_dev) | |
2995 | { | |
2996 | struct rbd_snap *snap; | |
2997 | int ret = 0; | |
2998 | ||
2999 | dout("%s called\n", __func__); | |
86ff77bb AE |
3000 | if (WARN_ON(!device_is_registered(&rbd_dev->dev))) |
3001 | return -EIO; | |
304f6808 AE |
3002 | |
3003 | list_for_each_entry(snap, &rbd_dev->snaps, node) { | |
3004 | if (!rbd_snap_registered(snap)) { | |
3005 | ret = rbd_register_snap_dev(snap, &rbd_dev->dev); | |
3006 | if (ret < 0) | |
3007 | break; | |
3008 | } | |
3009 | } | |
3010 | dout("%s: returning %d\n", __func__, ret); | |
3011 | ||
3012 | return ret; | |
3013 | } | |
3014 | ||
dfc5606d YS |
3015 | static int rbd_bus_add_dev(struct rbd_device *rbd_dev) |
3016 | { | |
dfc5606d | 3017 | struct device *dev; |
cd789ab9 | 3018 | int ret; |
dfc5606d YS |
3019 | |
3020 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); | |
dfc5606d | 3021 | |
cd789ab9 | 3022 | dev = &rbd_dev->dev; |
dfc5606d YS |
3023 | dev->bus = &rbd_bus_type; |
3024 | dev->type = &rbd_device_type; | |
3025 | dev->parent = &rbd_root_dev; | |
3026 | dev->release = rbd_dev_release; | |
de71a297 | 3027 | dev_set_name(dev, "%d", rbd_dev->dev_id); |
dfc5606d | 3028 | ret = device_register(dev); |
dfc5606d | 3029 | |
dfc5606d | 3030 | mutex_unlock(&ctl_mutex); |
cd789ab9 | 3031 | |
dfc5606d | 3032 | return ret; |
602adf40 YS |
3033 | } |
3034 | ||
dfc5606d YS |
3035 | static void rbd_bus_del_dev(struct rbd_device *rbd_dev) |
3036 | { | |
3037 | device_unregister(&rbd_dev->dev); | |
3038 | } | |
3039 | ||
59c2be1e YS |
3040 | static int rbd_init_watch_dev(struct rbd_device *rbd_dev) |
3041 | { | |
3042 | int ret, rc; | |
3043 | ||
3044 | do { | |
0e6f322d | 3045 | ret = rbd_req_sync_watch(rbd_dev); |
59c2be1e | 3046 | if (ret == -ERANGE) { |
117973fb | 3047 | rc = rbd_dev_refresh(rbd_dev, NULL); |
59c2be1e YS |
3048 | if (rc < 0) |
3049 | return rc; | |
3050 | } | |
3051 | } while (ret == -ERANGE); | |
3052 | ||
3053 | return ret; | |
3054 | } | |
3055 | ||
e2839308 | 3056 | static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0); |
1ddbe94e AE |
3057 | |
3058 | /* | |
499afd5b AE |
3059 | * Get a unique rbd identifier for the given new rbd_dev, and add |
3060 | * the rbd_dev to the global list. The minimum rbd id is 1. | |
1ddbe94e | 3061 | */ |
e2839308 | 3062 | static void rbd_dev_id_get(struct rbd_device *rbd_dev) |
b7f23c36 | 3063 | { |
e2839308 | 3064 | rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max); |
499afd5b AE |
3065 | |
3066 | spin_lock(&rbd_dev_list_lock); | |
3067 | list_add_tail(&rbd_dev->node, &rbd_dev_list); | |
3068 | spin_unlock(&rbd_dev_list_lock); | |
e2839308 AE |
3069 | dout("rbd_dev %p given dev id %llu\n", rbd_dev, |
3070 | (unsigned long long) rbd_dev->dev_id); | |
1ddbe94e | 3071 | } |
b7f23c36 | 3072 | |
1ddbe94e | 3073 | /* |
499afd5b AE |
3074 | * Remove an rbd_dev from the global list, and record that its |
3075 | * identifier is no longer in use. | |
1ddbe94e | 3076 | */ |
e2839308 | 3077 | static void rbd_dev_id_put(struct rbd_device *rbd_dev) |
1ddbe94e | 3078 | { |
d184f6bf | 3079 | struct list_head *tmp; |
de71a297 | 3080 | int rbd_id = rbd_dev->dev_id; |
d184f6bf AE |
3081 | int max_id; |
3082 | ||
aafb230e | 3083 | rbd_assert(rbd_id > 0); |
499afd5b | 3084 | |
e2839308 AE |
3085 | dout("rbd_dev %p released dev id %llu\n", rbd_dev, |
3086 | (unsigned long long) rbd_dev->dev_id); | |
499afd5b AE |
3087 | spin_lock(&rbd_dev_list_lock); |
3088 | list_del_init(&rbd_dev->node); | |
d184f6bf AE |
3089 | |
3090 | /* | |
3091 | * If the id being "put" is not the current maximum, there | |
3092 | * is nothing special we need to do. | |
3093 | */ | |
e2839308 | 3094 | if (rbd_id != atomic64_read(&rbd_dev_id_max)) { |
d184f6bf AE |
3095 | spin_unlock(&rbd_dev_list_lock); |
3096 | return; | |
3097 | } | |
3098 | ||
3099 | /* | |
3100 | * We need to update the current maximum id. Search the | |
3101 | * list to find out what it is. We're more likely to find | |
3102 | * the maximum at the end, so search the list backward. | |
3103 | */ | |
3104 | max_id = 0; | |
3105 | list_for_each_prev(tmp, &rbd_dev_list) { | |
3106 | struct rbd_device *rbd_dev; | |
3107 | ||
3108 | rbd_dev = list_entry(tmp, struct rbd_device, node); | |
b213e0b1 AE |
3109 | if (rbd_dev->dev_id > max_id) |
3110 | max_id = rbd_dev->dev_id; | |
d184f6bf | 3111 | } |
499afd5b | 3112 | spin_unlock(&rbd_dev_list_lock); |
b7f23c36 | 3113 | |
1ddbe94e | 3114 | /* |
e2839308 | 3115 | * The max id could have been updated by rbd_dev_id_get(), in |
d184f6bf AE |
3116 | * which case it now accurately reflects the new maximum. |
3117 | * Be careful not to overwrite the maximum value in that | |
3118 | * case. | |
1ddbe94e | 3119 | */ |
e2839308 AE |
3120 | atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id); |
3121 | dout(" max dev id has been reset\n"); | |
b7f23c36 AE |
3122 | } |
3123 | ||
e28fff26 AE |
3124 | /* |
3125 | * Skips over white space at *buf, and updates *buf to point to the | |
3126 | * first found non-space character (if any). Returns the length of | |
593a9e7b AE |
3127 | * the token (string of non-white space characters) found. Note |
3128 | * that *buf must be terminated with '\0'. | |
e28fff26 AE |
3129 | */ |
3130 | static inline size_t next_token(const char **buf) | |
3131 | { | |
3132 | /* | |
3133 | * These are the characters that produce nonzero for | |
3134 | * isspace() in the "C" and "POSIX" locales. | |
3135 | */ | |
3136 | const char *spaces = " \f\n\r\t\v"; | |
3137 | ||
3138 | *buf += strspn(*buf, spaces); /* Find start of token */ | |
3139 | ||
3140 | return strcspn(*buf, spaces); /* Return token length */ | |
3141 | } | |
3142 | ||
3143 | /* | |
3144 | * Finds the next token in *buf, and if the provided token buffer is | |
3145 | * big enough, copies the found token into it. The result, if | |
593a9e7b AE |
3146 | * copied, is guaranteed to be terminated with '\0'. Note that *buf |
3147 | * must be terminated with '\0' on entry. | |
e28fff26 AE |
3148 | * |
3149 | * Returns the length of the token found (not including the '\0'). | |
3150 | * Return value will be 0 if no token is found, and it will be >= | |
3151 | * token_size if the token would not fit. | |
3152 | * | |
593a9e7b | 3153 | * The *buf pointer will be updated to point beyond the end of the |
e28fff26 AE |
3154 | * found token. Note that this occurs even if the token buffer is |
3155 | * too small to hold it. | |
3156 | */ | |
3157 | static inline size_t copy_token(const char **buf, | |
3158 | char *token, | |
3159 | size_t token_size) | |
3160 | { | |
3161 | size_t len; | |
3162 | ||
3163 | len = next_token(buf); | |
3164 | if (len < token_size) { | |
3165 | memcpy(token, *buf, len); | |
3166 | *(token + len) = '\0'; | |
3167 | } | |
3168 | *buf += len; | |
3169 | ||
3170 | return len; | |
3171 | } | |
3172 | ||
ea3352f4 AE |
3173 | /* |
3174 | * Finds the next token in *buf, dynamically allocates a buffer big | |
3175 | * enough to hold a copy of it, and copies the token into the new | |
3176 | * buffer. The copy is guaranteed to be terminated with '\0'. Note | |
3177 | * that a duplicate buffer is created even for a zero-length token. | |
3178 | * | |
3179 | * Returns a pointer to the newly-allocated duplicate, or a null | |
3180 | * pointer if memory for the duplicate was not available. If | |
3181 | * the lenp argument is a non-null pointer, the length of the token | |
3182 | * (not including the '\0') is returned in *lenp. | |
3183 | * | |
3184 | * If successful, the *buf pointer will be updated to point beyond | |
3185 | * the end of the found token. | |
3186 | * | |
3187 | * Note: uses GFP_KERNEL for allocation. | |
3188 | */ | |
3189 | static inline char *dup_token(const char **buf, size_t *lenp) | |
3190 | { | |
3191 | char *dup; | |
3192 | size_t len; | |
3193 | ||
3194 | len = next_token(buf); | |
4caf35f9 | 3195 | dup = kmemdup(*buf, len + 1, GFP_KERNEL); |
ea3352f4 AE |
3196 | if (!dup) |
3197 | return NULL; | |
ea3352f4 AE |
3198 | *(dup + len) = '\0'; |
3199 | *buf += len; | |
3200 | ||
3201 | if (lenp) | |
3202 | *lenp = len; | |
3203 | ||
3204 | return dup; | |
3205 | } | |
3206 | ||
a725f65e | 3207 | /* |
859c31df AE |
3208 | * Parse the options provided for an "rbd add" (i.e., rbd image |
3209 | * mapping) request. These arrive via a write to /sys/bus/rbd/add, | |
3210 | * and the data written is passed here via a NUL-terminated buffer. | |
3211 | * Returns 0 if successful or an error code otherwise. | |
d22f76e7 | 3212 | * |
859c31df AE |
3213 | * The information extracted from these options is recorded in |
3214 | * the other parameters which return dynamically-allocated | |
3215 | * structures: | |
3216 | * ceph_opts | |
3217 | * The address of a pointer that will refer to a ceph options | |
3218 | * structure. Caller must release the returned pointer using | |
3219 | * ceph_destroy_options() when it is no longer needed. | |
3220 | * rbd_opts | |
3221 | * Address of an rbd options pointer. Fully initialized by | |
3222 | * this function; caller must release with kfree(). | |
3223 | * spec | |
3224 | * Address of an rbd image specification pointer. Fully | |
3225 | * initialized by this function based on parsed options. | |
3226 | * Caller must release with rbd_spec_put(). | |
3227 | * | |
3228 | * The options passed take this form: | |
3229 | * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>] | |
3230 | * where: | |
3231 | * <mon_addrs> | |
3232 | * A comma-separated list of one or more monitor addresses. | |
3233 | * A monitor address is an ip address, optionally followed | |
3234 | * by a port number (separated by a colon). | |
3235 | * I.e.: ip1[:port1][,ip2[:port2]...] | |
3236 | * <options> | |
3237 | * A comma-separated list of ceph and/or rbd options. | |
3238 | * <pool_name> | |
3239 | * The name of the rados pool containing the rbd image. | |
3240 | * <image_name> | |
3241 | * The name of the image in that pool to map. | |
3242 | * <snap_id> | |
3243 | * An optional snapshot id. If provided, the mapping will | |
3244 | * present data from the image at the time that snapshot was | |
3245 | * created. The image head is used if no snapshot id is | |
3246 | * provided. Snapshot mappings are always read-only. | |
a725f65e | 3247 | */ |
859c31df | 3248 | static int rbd_add_parse_args(const char *buf, |
dc79b113 | 3249 | struct ceph_options **ceph_opts, |
859c31df AE |
3250 | struct rbd_options **opts, |
3251 | struct rbd_spec **rbd_spec) | |
e28fff26 | 3252 | { |
d22f76e7 | 3253 | size_t len; |
859c31df | 3254 | char *options; |
0ddebc0c AE |
3255 | const char *mon_addrs; |
3256 | size_t mon_addrs_size; | |
859c31df | 3257 | struct rbd_spec *spec = NULL; |
4e9afeba | 3258 | struct rbd_options *rbd_opts = NULL; |
859c31df | 3259 | struct ceph_options *copts; |
dc79b113 | 3260 | int ret; |
e28fff26 AE |
3261 | |
3262 | /* The first four tokens are required */ | |
3263 | ||
7ef3214a | 3264 | len = next_token(&buf); |
4fb5d671 AE |
3265 | if (!len) { |
3266 | rbd_warn(NULL, "no monitor address(es) provided"); | |
3267 | return -EINVAL; | |
3268 | } | |
0ddebc0c | 3269 | mon_addrs = buf; |
f28e565a | 3270 | mon_addrs_size = len + 1; |
7ef3214a | 3271 | buf += len; |
a725f65e | 3272 | |
dc79b113 | 3273 | ret = -EINVAL; |
f28e565a AE |
3274 | options = dup_token(&buf, NULL); |
3275 | if (!options) | |
dc79b113 | 3276 | return -ENOMEM; |
4fb5d671 AE |
3277 | if (!*options) { |
3278 | rbd_warn(NULL, "no options provided"); | |
3279 | goto out_err; | |
3280 | } | |
e28fff26 | 3281 | |
859c31df AE |
3282 | spec = rbd_spec_alloc(); |
3283 | if (!spec) | |
f28e565a | 3284 | goto out_mem; |
859c31df AE |
3285 | |
3286 | spec->pool_name = dup_token(&buf, NULL); | |
3287 | if (!spec->pool_name) | |
3288 | goto out_mem; | |
4fb5d671 AE |
3289 | if (!*spec->pool_name) { |
3290 | rbd_warn(NULL, "no pool name provided"); | |
3291 | goto out_err; | |
3292 | } | |
e28fff26 | 3293 | |
69e7a02f | 3294 | spec->image_name = dup_token(&buf, NULL); |
859c31df | 3295 | if (!spec->image_name) |
f28e565a | 3296 | goto out_mem; |
4fb5d671 AE |
3297 | if (!*spec->image_name) { |
3298 | rbd_warn(NULL, "no image name provided"); | |
3299 | goto out_err; | |
3300 | } | |
d4b125e9 | 3301 | |
f28e565a AE |
3302 | /* |
3303 | * Snapshot name is optional; default is to use "-" | |
3304 | * (indicating the head/no snapshot). | |
3305 | */ | |
3feeb894 | 3306 | len = next_token(&buf); |
820a5f3e | 3307 | if (!len) { |
3feeb894 AE |
3308 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
3309 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; | |
f28e565a | 3310 | } else if (len > RBD_MAX_SNAP_NAME_LEN) { |
dc79b113 | 3311 | ret = -ENAMETOOLONG; |
f28e565a | 3312 | goto out_err; |
849b4260 | 3313 | } |
4caf35f9 | 3314 | spec->snap_name = kmemdup(buf, len + 1, GFP_KERNEL); |
859c31df | 3315 | if (!spec->snap_name) |
f28e565a | 3316 | goto out_mem; |
859c31df | 3317 | *(spec->snap_name + len) = '\0'; |
e5c35534 | 3318 | |
0ddebc0c | 3319 | /* Initialize all rbd options to the defaults */ |
e28fff26 | 3320 | |
4e9afeba AE |
3321 | rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL); |
3322 | if (!rbd_opts) | |
3323 | goto out_mem; | |
3324 | ||
3325 | rbd_opts->read_only = RBD_READ_ONLY_DEFAULT; | |
d22f76e7 | 3326 | |
859c31df | 3327 | copts = ceph_parse_options(options, mon_addrs, |
0ddebc0c | 3328 | mon_addrs + mon_addrs_size - 1, |
4e9afeba | 3329 | parse_rbd_opts_token, rbd_opts); |
859c31df AE |
3330 | if (IS_ERR(copts)) { |
3331 | ret = PTR_ERR(copts); | |
dc79b113 AE |
3332 | goto out_err; |
3333 | } | |
859c31df AE |
3334 | kfree(options); |
3335 | ||
3336 | *ceph_opts = copts; | |
4e9afeba | 3337 | *opts = rbd_opts; |
859c31df | 3338 | *rbd_spec = spec; |
0ddebc0c | 3339 | |
dc79b113 | 3340 | return 0; |
f28e565a | 3341 | out_mem: |
dc79b113 | 3342 | ret = -ENOMEM; |
d22f76e7 | 3343 | out_err: |
859c31df AE |
3344 | kfree(rbd_opts); |
3345 | rbd_spec_put(spec); | |
f28e565a | 3346 | kfree(options); |
d22f76e7 | 3347 | |
dc79b113 | 3348 | return ret; |
a725f65e AE |
3349 | } |
3350 | ||
589d30e0 AE |
3351 | /* |
3352 | * An rbd format 2 image has a unique identifier, distinct from the | |
3353 | * name given to it by the user. Internally, that identifier is | |
3354 | * what's used to specify the names of objects related to the image. | |
3355 | * | |
3356 | * A special "rbd id" object is used to map an rbd image name to its | |
3357 | * id. If that object doesn't exist, then there is no v2 rbd image | |
3358 | * with the supplied name. | |
3359 | * | |
3360 | * This function will record the given rbd_dev's image_id field if | |
3361 | * it can be determined, and in that case will return 0. If any | |
3362 | * errors occur a negative errno will be returned and the rbd_dev's | |
3363 | * image_id field will be unchanged (and should be NULL). | |
3364 | */ | |
3365 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) | |
3366 | { | |
3367 | int ret; | |
3368 | size_t size; | |
3369 | char *object_name; | |
3370 | void *response; | |
3371 | void *p; | |
3372 | ||
2c0d0a10 AE |
3373 | /* |
3374 | * When probing a parent image, the image id is already | |
3375 | * known (and the image name likely is not). There's no | |
3376 | * need to fetch the image id again in this case. | |
3377 | */ | |
3378 | if (rbd_dev->spec->image_id) | |
3379 | return 0; | |
3380 | ||
589d30e0 AE |
3381 | /* |
3382 | * First, see if the format 2 image id file exists, and if | |
3383 | * so, get the image's persistent id from it. | |
3384 | */ | |
69e7a02f | 3385 | size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name); |
589d30e0 AE |
3386 | object_name = kmalloc(size, GFP_NOIO); |
3387 | if (!object_name) | |
3388 | return -ENOMEM; | |
0d7dbfce | 3389 | sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name); |
589d30e0 AE |
3390 | dout("rbd id object name is %s\n", object_name); |
3391 | ||
3392 | /* Response will be an encoded string, which includes a length */ | |
3393 | ||
3394 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; | |
3395 | response = kzalloc(size, GFP_NOIO); | |
3396 | if (!response) { | |
3397 | ret = -ENOMEM; | |
3398 | goto out; | |
3399 | } | |
3400 | ||
3401 | ret = rbd_req_sync_exec(rbd_dev, object_name, | |
3402 | "rbd", "get_id", | |
3403 | NULL, 0, | |
3404 | response, RBD_IMAGE_ID_LEN_MAX, | |
3405 | CEPH_OSD_FLAG_READ, NULL); | |
3406 | dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret); | |
3407 | if (ret < 0) | |
3408 | goto out; | |
a0ea3a40 | 3409 | ret = 0; /* rbd_req_sync_exec() can return positive */ |
589d30e0 AE |
3410 | |
3411 | p = response; | |
0d7dbfce | 3412 | rbd_dev->spec->image_id = ceph_extract_encoded_string(&p, |
589d30e0 | 3413 | p + RBD_IMAGE_ID_LEN_MAX, |
979ed480 | 3414 | NULL, GFP_NOIO); |
0d7dbfce AE |
3415 | if (IS_ERR(rbd_dev->spec->image_id)) { |
3416 | ret = PTR_ERR(rbd_dev->spec->image_id); | |
3417 | rbd_dev->spec->image_id = NULL; | |
589d30e0 | 3418 | } else { |
0d7dbfce | 3419 | dout("image_id is %s\n", rbd_dev->spec->image_id); |
589d30e0 AE |
3420 | } |
3421 | out: | |
3422 | kfree(response); | |
3423 | kfree(object_name); | |
3424 | ||
3425 | return ret; | |
3426 | } | |
3427 | ||
a30b71b9 AE |
3428 | static int rbd_dev_v1_probe(struct rbd_device *rbd_dev) |
3429 | { | |
3430 | int ret; | |
3431 | size_t size; | |
3432 | ||
3433 | /* Version 1 images have no id; empty string is used */ | |
3434 | ||
0d7dbfce AE |
3435 | rbd_dev->spec->image_id = kstrdup("", GFP_KERNEL); |
3436 | if (!rbd_dev->spec->image_id) | |
a30b71b9 | 3437 | return -ENOMEM; |
a30b71b9 AE |
3438 | |
3439 | /* Record the header object name for this rbd image. */ | |
3440 | ||
69e7a02f | 3441 | size = strlen(rbd_dev->spec->image_name) + sizeof (RBD_SUFFIX); |
a30b71b9 AE |
3442 | rbd_dev->header_name = kmalloc(size, GFP_KERNEL); |
3443 | if (!rbd_dev->header_name) { | |
3444 | ret = -ENOMEM; | |
3445 | goto out_err; | |
3446 | } | |
0d7dbfce AE |
3447 | sprintf(rbd_dev->header_name, "%s%s", |
3448 | rbd_dev->spec->image_name, RBD_SUFFIX); | |
a30b71b9 AE |
3449 | |
3450 | /* Populate rbd image metadata */ | |
3451 | ||
3452 | ret = rbd_read_header(rbd_dev, &rbd_dev->header); | |
3453 | if (ret < 0) | |
3454 | goto out_err; | |
86b00e0d AE |
3455 | |
3456 | /* Version 1 images have no parent (no layering) */ | |
3457 | ||
3458 | rbd_dev->parent_spec = NULL; | |
3459 | rbd_dev->parent_overlap = 0; | |
3460 | ||
a30b71b9 AE |
3461 | rbd_dev->image_format = 1; |
3462 | ||
3463 | dout("discovered version 1 image, header name is %s\n", | |
3464 | rbd_dev->header_name); | |
3465 | ||
3466 | return 0; | |
3467 | ||
3468 | out_err: | |
3469 | kfree(rbd_dev->header_name); | |
3470 | rbd_dev->header_name = NULL; | |
0d7dbfce AE |
3471 | kfree(rbd_dev->spec->image_id); |
3472 | rbd_dev->spec->image_id = NULL; | |
a30b71b9 AE |
3473 | |
3474 | return ret; | |
3475 | } | |
3476 | ||
3477 | static int rbd_dev_v2_probe(struct rbd_device *rbd_dev) | |
3478 | { | |
3479 | size_t size; | |
9d475de5 | 3480 | int ret; |
6e14b1a6 | 3481 | u64 ver = 0; |
a30b71b9 AE |
3482 | |
3483 | /* | |
3484 | * Image id was filled in by the caller. Record the header | |
3485 | * object name for this rbd image. | |
3486 | */ | |
979ed480 | 3487 | size = sizeof (RBD_HEADER_PREFIX) + strlen(rbd_dev->spec->image_id); |
a30b71b9 AE |
3488 | rbd_dev->header_name = kmalloc(size, GFP_KERNEL); |
3489 | if (!rbd_dev->header_name) | |
3490 | return -ENOMEM; | |
3491 | sprintf(rbd_dev->header_name, "%s%s", | |
0d7dbfce | 3492 | RBD_HEADER_PREFIX, rbd_dev->spec->image_id); |
9d475de5 AE |
3493 | |
3494 | /* Get the size and object order for the image */ | |
3495 | ||
3496 | ret = rbd_dev_v2_image_size(rbd_dev); | |
1e130199 AE |
3497 | if (ret < 0) |
3498 | goto out_err; | |
3499 | ||
3500 | /* Get the object prefix (a.k.a. block_name) for the image */ | |
3501 | ||
3502 | ret = rbd_dev_v2_object_prefix(rbd_dev); | |
b1b5402a AE |
3503 | if (ret < 0) |
3504 | goto out_err; | |
3505 | ||
d889140c | 3506 | /* Get the and check features for the image */ |
b1b5402a AE |
3507 | |
3508 | ret = rbd_dev_v2_features(rbd_dev); | |
9d475de5 AE |
3509 | if (ret < 0) |
3510 | goto out_err; | |
35d489f9 | 3511 | |
86b00e0d AE |
3512 | /* If the image supports layering, get the parent info */ |
3513 | ||
3514 | if (rbd_dev->header.features & RBD_FEATURE_LAYERING) { | |
3515 | ret = rbd_dev_v2_parent_info(rbd_dev); | |
3516 | if (ret < 0) | |
3517 | goto out_err; | |
3518 | } | |
3519 | ||
6e14b1a6 AE |
3520 | /* crypto and compression type aren't (yet) supported for v2 images */ |
3521 | ||
3522 | rbd_dev->header.crypt_type = 0; | |
3523 | rbd_dev->header.comp_type = 0; | |
35d489f9 | 3524 | |
6e14b1a6 AE |
3525 | /* Get the snapshot context, plus the header version */ |
3526 | ||
3527 | ret = rbd_dev_v2_snap_context(rbd_dev, &ver); | |
35d489f9 AE |
3528 | if (ret) |
3529 | goto out_err; | |
6e14b1a6 AE |
3530 | rbd_dev->header.obj_version = ver; |
3531 | ||
a30b71b9 AE |
3532 | rbd_dev->image_format = 2; |
3533 | ||
3534 | dout("discovered version 2 image, header name is %s\n", | |
3535 | rbd_dev->header_name); | |
3536 | ||
35152979 | 3537 | return 0; |
9d475de5 | 3538 | out_err: |
86b00e0d AE |
3539 | rbd_dev->parent_overlap = 0; |
3540 | rbd_spec_put(rbd_dev->parent_spec); | |
3541 | rbd_dev->parent_spec = NULL; | |
9d475de5 AE |
3542 | kfree(rbd_dev->header_name); |
3543 | rbd_dev->header_name = NULL; | |
1e130199 AE |
3544 | kfree(rbd_dev->header.object_prefix); |
3545 | rbd_dev->header.object_prefix = NULL; | |
9d475de5 AE |
3546 | |
3547 | return ret; | |
a30b71b9 AE |
3548 | } |
3549 | ||
83a06263 AE |
3550 | static int rbd_dev_probe_finish(struct rbd_device *rbd_dev) |
3551 | { | |
3552 | int ret; | |
3553 | ||
3554 | /* no need to lock here, as rbd_dev is not registered yet */ | |
3555 | ret = rbd_dev_snaps_update(rbd_dev); | |
3556 | if (ret) | |
3557 | return ret; | |
3558 | ||
9e15b77d AE |
3559 | ret = rbd_dev_probe_update_spec(rbd_dev); |
3560 | if (ret) | |
3561 | goto err_out_snaps; | |
3562 | ||
83a06263 AE |
3563 | ret = rbd_dev_set_mapping(rbd_dev); |
3564 | if (ret) | |
3565 | goto err_out_snaps; | |
3566 | ||
3567 | /* generate unique id: find highest unique id, add one */ | |
3568 | rbd_dev_id_get(rbd_dev); | |
3569 | ||
3570 | /* Fill in the device name, now that we have its id. */ | |
3571 | BUILD_BUG_ON(DEV_NAME_LEN | |
3572 | < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH); | |
3573 | sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id); | |
3574 | ||
3575 | /* Get our block major device number. */ | |
3576 | ||
3577 | ret = register_blkdev(0, rbd_dev->name); | |
3578 | if (ret < 0) | |
3579 | goto err_out_id; | |
3580 | rbd_dev->major = ret; | |
3581 | ||
3582 | /* Set up the blkdev mapping. */ | |
3583 | ||
3584 | ret = rbd_init_disk(rbd_dev); | |
3585 | if (ret) | |
3586 | goto err_out_blkdev; | |
3587 | ||
3588 | ret = rbd_bus_add_dev(rbd_dev); | |
3589 | if (ret) | |
3590 | goto err_out_disk; | |
3591 | ||
3592 | /* | |
3593 | * At this point cleanup in the event of an error is the job | |
3594 | * of the sysfs code (initiated by rbd_bus_del_dev()). | |
3595 | */ | |
3596 | down_write(&rbd_dev->header_rwsem); | |
3597 | ret = rbd_dev_snaps_register(rbd_dev); | |
3598 | up_write(&rbd_dev->header_rwsem); | |
3599 | if (ret) | |
3600 | goto err_out_bus; | |
3601 | ||
3602 | ret = rbd_init_watch_dev(rbd_dev); | |
3603 | if (ret) | |
3604 | goto err_out_bus; | |
3605 | ||
3606 | /* Everything's ready. Announce the disk to the world. */ | |
3607 | ||
3608 | add_disk(rbd_dev->disk); | |
3609 | ||
3610 | pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name, | |
3611 | (unsigned long long) rbd_dev->mapping.size); | |
3612 | ||
3613 | return ret; | |
3614 | err_out_bus: | |
3615 | /* this will also clean up rest of rbd_dev stuff */ | |
3616 | ||
3617 | rbd_bus_del_dev(rbd_dev); | |
3618 | ||
3619 | return ret; | |
3620 | err_out_disk: | |
3621 | rbd_free_disk(rbd_dev); | |
3622 | err_out_blkdev: | |
3623 | unregister_blkdev(rbd_dev->major, rbd_dev->name); | |
3624 | err_out_id: | |
3625 | rbd_dev_id_put(rbd_dev); | |
3626 | err_out_snaps: | |
3627 | rbd_remove_all_snaps(rbd_dev); | |
3628 | ||
3629 | return ret; | |
3630 | } | |
3631 | ||
a30b71b9 AE |
3632 | /* |
3633 | * Probe for the existence of the header object for the given rbd | |
3634 | * device. For format 2 images this includes determining the image | |
3635 | * id. | |
3636 | */ | |
3637 | static int rbd_dev_probe(struct rbd_device *rbd_dev) | |
3638 | { | |
3639 | int ret; | |
3640 | ||
3641 | /* | |
3642 | * Get the id from the image id object. If it's not a | |
3643 | * format 2 image, we'll get ENOENT back, and we'll assume | |
3644 | * it's a format 1 image. | |
3645 | */ | |
3646 | ret = rbd_dev_image_id(rbd_dev); | |
3647 | if (ret) | |
3648 | ret = rbd_dev_v1_probe(rbd_dev); | |
3649 | else | |
3650 | ret = rbd_dev_v2_probe(rbd_dev); | |
83a06263 | 3651 | if (ret) { |
a30b71b9 AE |
3652 | dout("probe failed, returning %d\n", ret); |
3653 | ||
83a06263 AE |
3654 | return ret; |
3655 | } | |
3656 | ||
3657 | ret = rbd_dev_probe_finish(rbd_dev); | |
3658 | if (ret) | |
3659 | rbd_header_free(&rbd_dev->header); | |
3660 | ||
a30b71b9 AE |
3661 | return ret; |
3662 | } | |
3663 | ||
59c2be1e YS |
3664 | static ssize_t rbd_add(struct bus_type *bus, |
3665 | const char *buf, | |
3666 | size_t count) | |
602adf40 | 3667 | { |
cb8627c7 | 3668 | struct rbd_device *rbd_dev = NULL; |
dc79b113 | 3669 | struct ceph_options *ceph_opts = NULL; |
4e9afeba | 3670 | struct rbd_options *rbd_opts = NULL; |
859c31df | 3671 | struct rbd_spec *spec = NULL; |
9d3997fd | 3672 | struct rbd_client *rbdc; |
27cc2594 AE |
3673 | struct ceph_osd_client *osdc; |
3674 | int rc = -ENOMEM; | |
602adf40 YS |
3675 | |
3676 | if (!try_module_get(THIS_MODULE)) | |
3677 | return -ENODEV; | |
3678 | ||
602adf40 | 3679 | /* parse add command */ |
859c31df | 3680 | rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec); |
dc79b113 | 3681 | if (rc < 0) |
bd4ba655 | 3682 | goto err_out_module; |
78cea76e | 3683 | |
9d3997fd AE |
3684 | rbdc = rbd_get_client(ceph_opts); |
3685 | if (IS_ERR(rbdc)) { | |
3686 | rc = PTR_ERR(rbdc); | |
0ddebc0c | 3687 | goto err_out_args; |
9d3997fd | 3688 | } |
c53d5893 | 3689 | ceph_opts = NULL; /* rbd_dev client now owns this */ |
602adf40 | 3690 | |
602adf40 | 3691 | /* pick the pool */ |
9d3997fd | 3692 | osdc = &rbdc->client->osdc; |
859c31df | 3693 | rc = ceph_pg_poolid_by_name(osdc->osdmap, spec->pool_name); |
602adf40 YS |
3694 | if (rc < 0) |
3695 | goto err_out_client; | |
859c31df AE |
3696 | spec->pool_id = (u64) rc; |
3697 | ||
c53d5893 | 3698 | rbd_dev = rbd_dev_create(rbdc, spec); |
bd4ba655 AE |
3699 | if (!rbd_dev) |
3700 | goto err_out_client; | |
c53d5893 AE |
3701 | rbdc = NULL; /* rbd_dev now owns this */ |
3702 | spec = NULL; /* rbd_dev now owns this */ | |
602adf40 | 3703 | |
bd4ba655 | 3704 | rbd_dev->mapping.read_only = rbd_opts->read_only; |
c53d5893 AE |
3705 | kfree(rbd_opts); |
3706 | rbd_opts = NULL; /* done with this */ | |
bd4ba655 | 3707 | |
a30b71b9 AE |
3708 | rc = rbd_dev_probe(rbd_dev); |
3709 | if (rc < 0) | |
c53d5893 | 3710 | goto err_out_rbd_dev; |
05fd6f6f | 3711 | |
602adf40 | 3712 | return count; |
c53d5893 AE |
3713 | err_out_rbd_dev: |
3714 | rbd_dev_destroy(rbd_dev); | |
bd4ba655 | 3715 | err_out_client: |
9d3997fd | 3716 | rbd_put_client(rbdc); |
0ddebc0c | 3717 | err_out_args: |
78cea76e AE |
3718 | if (ceph_opts) |
3719 | ceph_destroy_options(ceph_opts); | |
4e9afeba | 3720 | kfree(rbd_opts); |
859c31df | 3721 | rbd_spec_put(spec); |
bd4ba655 AE |
3722 | err_out_module: |
3723 | module_put(THIS_MODULE); | |
27cc2594 | 3724 | |
602adf40 | 3725 | dout("Error adding device %s\n", buf); |
27cc2594 AE |
3726 | |
3727 | return (ssize_t) rc; | |
602adf40 YS |
3728 | } |
3729 | ||
de71a297 | 3730 | static struct rbd_device *__rbd_get_dev(unsigned long dev_id) |
602adf40 YS |
3731 | { |
3732 | struct list_head *tmp; | |
3733 | struct rbd_device *rbd_dev; | |
3734 | ||
e124a82f | 3735 | spin_lock(&rbd_dev_list_lock); |
602adf40 YS |
3736 | list_for_each(tmp, &rbd_dev_list) { |
3737 | rbd_dev = list_entry(tmp, struct rbd_device, node); | |
de71a297 | 3738 | if (rbd_dev->dev_id == dev_id) { |
e124a82f | 3739 | spin_unlock(&rbd_dev_list_lock); |
602adf40 | 3740 | return rbd_dev; |
e124a82f | 3741 | } |
602adf40 | 3742 | } |
e124a82f | 3743 | spin_unlock(&rbd_dev_list_lock); |
602adf40 YS |
3744 | return NULL; |
3745 | } | |
3746 | ||
dfc5606d | 3747 | static void rbd_dev_release(struct device *dev) |
602adf40 | 3748 | { |
593a9e7b | 3749 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
602adf40 | 3750 | |
1dbb4399 AE |
3751 | if (rbd_dev->watch_request) { |
3752 | struct ceph_client *client = rbd_dev->rbd_client->client; | |
3753 | ||
3754 | ceph_osdc_unregister_linger_request(&client->osdc, | |
59c2be1e | 3755 | rbd_dev->watch_request); |
1dbb4399 | 3756 | } |
59c2be1e | 3757 | if (rbd_dev->watch_event) |
070c633f | 3758 | rbd_req_sync_unwatch(rbd_dev); |
59c2be1e | 3759 | |
602adf40 YS |
3760 | |
3761 | /* clean up and free blkdev */ | |
3762 | rbd_free_disk(rbd_dev); | |
3763 | unregister_blkdev(rbd_dev->major, rbd_dev->name); | |
32eec68d | 3764 | |
2ac4e75d AE |
3765 | /* release allocated disk header fields */ |
3766 | rbd_header_free(&rbd_dev->header); | |
3767 | ||
32eec68d | 3768 | /* done with the id, and with the rbd_dev */ |
e2839308 | 3769 | rbd_dev_id_put(rbd_dev); |
c53d5893 AE |
3770 | rbd_assert(rbd_dev->rbd_client != NULL); |
3771 | rbd_dev_destroy(rbd_dev); | |
602adf40 YS |
3772 | |
3773 | /* release module ref */ | |
3774 | module_put(THIS_MODULE); | |
602adf40 YS |
3775 | } |
3776 | ||
dfc5606d YS |
3777 | static ssize_t rbd_remove(struct bus_type *bus, |
3778 | const char *buf, | |
3779 | size_t count) | |
602adf40 YS |
3780 | { |
3781 | struct rbd_device *rbd_dev = NULL; | |
3782 | int target_id, rc; | |
3783 | unsigned long ul; | |
3784 | int ret = count; | |
3785 | ||
3786 | rc = strict_strtoul(buf, 10, &ul); | |
3787 | if (rc) | |
3788 | return rc; | |
3789 | ||
3790 | /* convert to int; abort if we lost anything in the conversion */ | |
3791 | target_id = (int) ul; | |
3792 | if (target_id != ul) | |
3793 | return -EINVAL; | |
3794 | ||
3795 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); | |
3796 | ||
3797 | rbd_dev = __rbd_get_dev(target_id); | |
3798 | if (!rbd_dev) { | |
3799 | ret = -ENOENT; | |
3800 | goto done; | |
42382b70 AE |
3801 | } |
3802 | ||
3803 | if (rbd_dev->open_count) { | |
3804 | ret = -EBUSY; | |
3805 | goto done; | |
602adf40 YS |
3806 | } |
3807 | ||
41f38c2b | 3808 | rbd_remove_all_snaps(rbd_dev); |
dfc5606d | 3809 | rbd_bus_del_dev(rbd_dev); |
602adf40 YS |
3810 | |
3811 | done: | |
3812 | mutex_unlock(&ctl_mutex); | |
aafb230e | 3813 | |
602adf40 YS |
3814 | return ret; |
3815 | } | |
3816 | ||
602adf40 YS |
3817 | /* |
3818 | * create control files in sysfs | |
dfc5606d | 3819 | * /sys/bus/rbd/... |
602adf40 YS |
3820 | */ |
3821 | static int rbd_sysfs_init(void) | |
3822 | { | |
dfc5606d | 3823 | int ret; |
602adf40 | 3824 | |
fed4c143 | 3825 | ret = device_register(&rbd_root_dev); |
21079786 | 3826 | if (ret < 0) |
dfc5606d | 3827 | return ret; |
602adf40 | 3828 | |
fed4c143 AE |
3829 | ret = bus_register(&rbd_bus_type); |
3830 | if (ret < 0) | |
3831 | device_unregister(&rbd_root_dev); | |
602adf40 | 3832 | |
602adf40 YS |
3833 | return ret; |
3834 | } | |
3835 | ||
3836 | static void rbd_sysfs_cleanup(void) | |
3837 | { | |
dfc5606d | 3838 | bus_unregister(&rbd_bus_type); |
fed4c143 | 3839 | device_unregister(&rbd_root_dev); |
602adf40 YS |
3840 | } |
3841 | ||
3842 | int __init rbd_init(void) | |
3843 | { | |
3844 | int rc; | |
3845 | ||
3846 | rc = rbd_sysfs_init(); | |
3847 | if (rc) | |
3848 | return rc; | |
f0f8cef5 | 3849 | pr_info("loaded " RBD_DRV_NAME_LONG "\n"); |
602adf40 YS |
3850 | return 0; |
3851 | } | |
3852 | ||
3853 | void __exit rbd_exit(void) | |
3854 | { | |
3855 | rbd_sysfs_cleanup(); | |
3856 | } | |
3857 | ||
3858 | module_init(rbd_init); | |
3859 | module_exit(rbd_exit); | |
3860 | ||
3861 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); | |
3862 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); | |
3863 | MODULE_DESCRIPTION("rados block device"); | |
3864 | ||
3865 | /* following authorship retained from original osdblk.c */ | |
3866 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); | |
3867 | ||
3868 | MODULE_LICENSE("GPL"); |