]>
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 | ||
f0f8cef5 AE |
55 | #define RBD_DRV_NAME "rbd" |
56 | #define RBD_DRV_NAME_LONG "rbd (rados block device)" | |
602adf40 YS |
57 | |
58 | #define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */ | |
59 | ||
d4b125e9 AE |
60 | #define RBD_SNAP_DEV_NAME_PREFIX "snap_" |
61 | #define RBD_MAX_SNAP_NAME_LEN \ | |
62 | (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1)) | |
63 | ||
35d489f9 | 64 | #define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */ |
602adf40 YS |
65 | |
66 | #define RBD_SNAP_HEAD_NAME "-" | |
67 | ||
9e15b77d AE |
68 | /* This allows a single page to hold an image name sent by OSD */ |
69 | #define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1) | |
1e130199 | 70 | #define RBD_IMAGE_ID_LEN_MAX 64 |
9e15b77d | 71 | |
1e130199 | 72 | #define RBD_OBJ_PREFIX_LEN_MAX 64 |
589d30e0 | 73 | |
d889140c AE |
74 | /* Feature bits */ |
75 | ||
5cbf6f12 AE |
76 | #define RBD_FEATURE_LAYERING (1<<0) |
77 | #define RBD_FEATURE_STRIPINGV2 (1<<1) | |
78 | #define RBD_FEATURES_ALL \ | |
79 | (RBD_FEATURE_LAYERING | RBD_FEATURE_STRIPINGV2) | |
d889140c AE |
80 | |
81 | /* Features supported by this (client software) implementation. */ | |
82 | ||
5cbf6f12 | 83 | #define RBD_FEATURES_SUPPORTED (0) |
d889140c | 84 | |
81a89793 AE |
85 | /* |
86 | * An RBD device name will be "rbd#", where the "rbd" comes from | |
87 | * RBD_DRV_NAME above, and # is a unique integer identifier. | |
88 | * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big | |
89 | * enough to hold all possible device names. | |
90 | */ | |
602adf40 | 91 | #define DEV_NAME_LEN 32 |
81a89793 | 92 | #define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1) |
602adf40 YS |
93 | |
94 | /* | |
95 | * block device image metadata (in-memory version) | |
96 | */ | |
97 | struct rbd_image_header { | |
f84344f3 | 98 | /* These four fields never change for a given rbd image */ |
849b4260 | 99 | char *object_prefix; |
34b13184 | 100 | u64 features; |
602adf40 YS |
101 | __u8 obj_order; |
102 | __u8 crypt_type; | |
103 | __u8 comp_type; | |
602adf40 | 104 | |
f84344f3 AE |
105 | /* The remaining fields need to be updated occasionally */ |
106 | u64 image_size; | |
107 | struct ceph_snap_context *snapc; | |
602adf40 YS |
108 | char *snap_names; |
109 | u64 *snap_sizes; | |
59c2be1e YS |
110 | |
111 | u64 obj_version; | |
112 | }; | |
113 | ||
0d7dbfce AE |
114 | /* |
115 | * An rbd image specification. | |
116 | * | |
117 | * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely | |
c66c6e0c AE |
118 | * identify an image. Each rbd_dev structure includes a pointer to |
119 | * an rbd_spec structure that encapsulates this identity. | |
120 | * | |
121 | * Each of the id's in an rbd_spec has an associated name. For a | |
122 | * user-mapped image, the names are supplied and the id's associated | |
123 | * with them are looked up. For a layered image, a parent image is | |
124 | * defined by the tuple, and the names are looked up. | |
125 | * | |
126 | * An rbd_dev structure contains a parent_spec pointer which is | |
127 | * non-null if the image it represents is a child in a layered | |
128 | * image. This pointer will refer to the rbd_spec structure used | |
129 | * by the parent rbd_dev for its own identity (i.e., the structure | |
130 | * is shared between the parent and child). | |
131 | * | |
132 | * Since these structures are populated once, during the discovery | |
133 | * phase of image construction, they are effectively immutable so | |
134 | * we make no effort to synchronize access to them. | |
135 | * | |
136 | * Note that code herein does not assume the image name is known (it | |
137 | * could be a null pointer). | |
0d7dbfce AE |
138 | */ |
139 | struct rbd_spec { | |
140 | u64 pool_id; | |
141 | char *pool_name; | |
142 | ||
143 | char *image_id; | |
0d7dbfce | 144 | char *image_name; |
0d7dbfce AE |
145 | |
146 | u64 snap_id; | |
147 | char *snap_name; | |
148 | ||
149 | struct kref kref; | |
150 | }; | |
151 | ||
602adf40 | 152 | /* |
f0f8cef5 | 153 | * an instance of the client. multiple devices may share an rbd client. |
602adf40 YS |
154 | */ |
155 | struct rbd_client { | |
156 | struct ceph_client *client; | |
157 | struct kref kref; | |
158 | struct list_head node; | |
159 | }; | |
160 | ||
bf0d5f50 AE |
161 | struct rbd_img_request; |
162 | typedef void (*rbd_img_callback_t)(struct rbd_img_request *); | |
163 | ||
164 | #define BAD_WHICH U32_MAX /* Good which or bad which, which? */ | |
165 | ||
166 | struct rbd_obj_request; | |
167 | typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *); | |
168 | ||
9969ebc5 AE |
169 | enum obj_request_type { |
170 | OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES | |
171 | }; | |
bf0d5f50 | 172 | |
926f9b3f AE |
173 | enum obj_req_flags { |
174 | OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */ | |
6365d33a | 175 | OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */ |
5679c59f AE |
176 | OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */ |
177 | OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */ | |
926f9b3f AE |
178 | }; |
179 | ||
bf0d5f50 AE |
180 | struct rbd_obj_request { |
181 | const char *object_name; | |
182 | u64 offset; /* object start byte */ | |
183 | u64 length; /* bytes from offset */ | |
926f9b3f | 184 | unsigned long flags; |
bf0d5f50 | 185 | |
c5b5ef6c AE |
186 | /* |
187 | * An object request associated with an image will have its | |
188 | * img_data flag set; a standalone object request will not. | |
189 | * | |
190 | * A standalone object request will have which == BAD_WHICH | |
191 | * and a null obj_request pointer. | |
192 | * | |
193 | * An object request initiated in support of a layered image | |
194 | * object (to check for its existence before a write) will | |
195 | * have which == BAD_WHICH and a non-null obj_request pointer. | |
196 | * | |
197 | * Finally, an object request for rbd image data will have | |
198 | * which != BAD_WHICH, and will have a non-null img_request | |
199 | * pointer. The value of which will be in the range | |
200 | * 0..(img_request->obj_request_count-1). | |
201 | */ | |
202 | union { | |
203 | struct rbd_obj_request *obj_request; /* STAT op */ | |
204 | struct { | |
205 | struct rbd_img_request *img_request; | |
206 | u64 img_offset; | |
207 | /* links for img_request->obj_requests list */ | |
208 | struct list_head links; | |
209 | }; | |
210 | }; | |
bf0d5f50 AE |
211 | u32 which; /* posn image request list */ |
212 | ||
213 | enum obj_request_type type; | |
788e2df3 AE |
214 | union { |
215 | struct bio *bio_list; | |
216 | struct { | |
217 | struct page **pages; | |
218 | u32 page_count; | |
219 | }; | |
220 | }; | |
bf0d5f50 AE |
221 | |
222 | struct ceph_osd_request *osd_req; | |
223 | ||
224 | u64 xferred; /* bytes transferred */ | |
225 | u64 version; | |
1b83bef2 | 226 | int result; |
bf0d5f50 AE |
227 | |
228 | rbd_obj_callback_t callback; | |
788e2df3 | 229 | struct completion completion; |
bf0d5f50 AE |
230 | |
231 | struct kref kref; | |
232 | }; | |
233 | ||
0c425248 | 234 | enum img_req_flags { |
9849e986 AE |
235 | IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */ |
236 | IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */ | |
d0b2e944 | 237 | IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */ |
0c425248 AE |
238 | }; |
239 | ||
bf0d5f50 | 240 | struct rbd_img_request { |
bf0d5f50 AE |
241 | struct rbd_device *rbd_dev; |
242 | u64 offset; /* starting image byte offset */ | |
243 | u64 length; /* byte count from offset */ | |
0c425248 | 244 | unsigned long flags; |
bf0d5f50 | 245 | union { |
9849e986 | 246 | u64 snap_id; /* for reads */ |
bf0d5f50 | 247 | struct ceph_snap_context *snapc; /* for writes */ |
9849e986 AE |
248 | }; |
249 | union { | |
250 | struct request *rq; /* block request */ | |
251 | struct rbd_obj_request *obj_request; /* obj req initiator */ | |
bf0d5f50 AE |
252 | }; |
253 | spinlock_t completion_lock;/* protects next_completion */ | |
254 | u32 next_completion; | |
255 | rbd_img_callback_t callback; | |
55f27e09 | 256 | u64 xferred;/* aggregate bytes transferred */ |
a5a337d4 | 257 | int result; /* first nonzero obj_request result */ |
bf0d5f50 AE |
258 | |
259 | u32 obj_request_count; | |
260 | struct list_head obj_requests; /* rbd_obj_request structs */ | |
261 | ||
262 | struct kref kref; | |
263 | }; | |
264 | ||
265 | #define for_each_obj_request(ireq, oreq) \ | |
ef06f4d3 | 266 | list_for_each_entry(oreq, &(ireq)->obj_requests, links) |
bf0d5f50 | 267 | #define for_each_obj_request_from(ireq, oreq) \ |
ef06f4d3 | 268 | list_for_each_entry_from(oreq, &(ireq)->obj_requests, links) |
bf0d5f50 | 269 | #define for_each_obj_request_safe(ireq, oreq, n) \ |
ef06f4d3 | 270 | list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links) |
bf0d5f50 | 271 | |
dfc5606d YS |
272 | struct rbd_snap { |
273 | struct device dev; | |
274 | const char *name; | |
3591538f | 275 | u64 size; |
dfc5606d YS |
276 | struct list_head node; |
277 | u64 id; | |
34b13184 | 278 | u64 features; |
dfc5606d YS |
279 | }; |
280 | ||
f84344f3 | 281 | struct rbd_mapping { |
99c1f08f | 282 | u64 size; |
34b13184 | 283 | u64 features; |
f84344f3 AE |
284 | bool read_only; |
285 | }; | |
286 | ||
602adf40 YS |
287 | /* |
288 | * a single device | |
289 | */ | |
290 | struct rbd_device { | |
de71a297 | 291 | int dev_id; /* blkdev unique id */ |
602adf40 YS |
292 | |
293 | int major; /* blkdev assigned major */ | |
294 | struct gendisk *disk; /* blkdev's gendisk and rq */ | |
602adf40 | 295 | |
a30b71b9 | 296 | u32 image_format; /* Either 1 or 2 */ |
602adf40 YS |
297 | struct rbd_client *rbd_client; |
298 | ||
299 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ | |
300 | ||
b82d167b | 301 | spinlock_t lock; /* queue, flags, open_count */ |
602adf40 YS |
302 | |
303 | struct rbd_image_header header; | |
b82d167b | 304 | unsigned long flags; /* possibly lock protected */ |
0d7dbfce | 305 | struct rbd_spec *spec; |
602adf40 | 306 | |
0d7dbfce | 307 | char *header_name; |
971f839a | 308 | |
0903e875 AE |
309 | struct ceph_file_layout layout; |
310 | ||
59c2be1e | 311 | struct ceph_osd_event *watch_event; |
975241af | 312 | struct rbd_obj_request *watch_request; |
59c2be1e | 313 | |
86b00e0d AE |
314 | struct rbd_spec *parent_spec; |
315 | u64 parent_overlap; | |
2f82ee54 | 316 | struct rbd_device *parent; |
86b00e0d | 317 | |
c666601a JD |
318 | /* protects updating the header */ |
319 | struct rw_semaphore header_rwsem; | |
f84344f3 AE |
320 | |
321 | struct rbd_mapping mapping; | |
602adf40 YS |
322 | |
323 | struct list_head node; | |
dfc5606d YS |
324 | |
325 | /* list of snapshots */ | |
326 | struct list_head snaps; | |
327 | ||
328 | /* sysfs related */ | |
329 | struct device dev; | |
b82d167b | 330 | unsigned long open_count; /* protected by lock */ |
dfc5606d YS |
331 | }; |
332 | ||
b82d167b AE |
333 | /* |
334 | * Flag bits for rbd_dev->flags. If atomicity is required, | |
335 | * rbd_dev->lock is used to protect access. | |
336 | * | |
337 | * Currently, only the "removing" flag (which is coupled with the | |
338 | * "open_count" field) requires atomic access. | |
339 | */ | |
6d292906 AE |
340 | enum rbd_dev_flags { |
341 | RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */ | |
b82d167b | 342 | RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */ |
6d292906 AE |
343 | }; |
344 | ||
602adf40 | 345 | static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */ |
e124a82f | 346 | |
602adf40 | 347 | static LIST_HEAD(rbd_dev_list); /* devices */ |
e124a82f AE |
348 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
349 | ||
432b8587 AE |
350 | static LIST_HEAD(rbd_client_list); /* clients */ |
351 | static DEFINE_SPINLOCK(rbd_client_list_lock); | |
602adf40 | 352 | |
304f6808 AE |
353 | static int rbd_dev_snaps_update(struct rbd_device *rbd_dev); |
354 | static int rbd_dev_snaps_register(struct rbd_device *rbd_dev); | |
355 | ||
dfc5606d | 356 | static void rbd_dev_release(struct device *dev); |
41f38c2b | 357 | static void rbd_remove_snap_dev(struct rbd_snap *snap); |
dfc5606d | 358 | |
f0f8cef5 AE |
359 | static ssize_t rbd_add(struct bus_type *bus, const char *buf, |
360 | size_t count); | |
361 | static ssize_t rbd_remove(struct bus_type *bus, const char *buf, | |
362 | size_t count); | |
2f82ee54 | 363 | static int rbd_dev_probe(struct rbd_device *rbd_dev); |
f0f8cef5 AE |
364 | |
365 | static struct bus_attribute rbd_bus_attrs[] = { | |
366 | __ATTR(add, S_IWUSR, NULL, rbd_add), | |
367 | __ATTR(remove, S_IWUSR, NULL, rbd_remove), | |
368 | __ATTR_NULL | |
369 | }; | |
370 | ||
371 | static struct bus_type rbd_bus_type = { | |
372 | .name = "rbd", | |
373 | .bus_attrs = rbd_bus_attrs, | |
374 | }; | |
375 | ||
376 | static void rbd_root_dev_release(struct device *dev) | |
377 | { | |
378 | } | |
379 | ||
380 | static struct device rbd_root_dev = { | |
381 | .init_name = "rbd", | |
382 | .release = rbd_root_dev_release, | |
383 | }; | |
384 | ||
06ecc6cb AE |
385 | static __printf(2, 3) |
386 | void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...) | |
387 | { | |
388 | struct va_format vaf; | |
389 | va_list args; | |
390 | ||
391 | va_start(args, fmt); | |
392 | vaf.fmt = fmt; | |
393 | vaf.va = &args; | |
394 | ||
395 | if (!rbd_dev) | |
396 | printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf); | |
397 | else if (rbd_dev->disk) | |
398 | printk(KERN_WARNING "%s: %s: %pV\n", | |
399 | RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf); | |
400 | else if (rbd_dev->spec && rbd_dev->spec->image_name) | |
401 | printk(KERN_WARNING "%s: image %s: %pV\n", | |
402 | RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf); | |
403 | else if (rbd_dev->spec && rbd_dev->spec->image_id) | |
404 | printk(KERN_WARNING "%s: id %s: %pV\n", | |
405 | RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf); | |
406 | else /* punt */ | |
407 | printk(KERN_WARNING "%s: rbd_dev %p: %pV\n", | |
408 | RBD_DRV_NAME, rbd_dev, &vaf); | |
409 | va_end(args); | |
410 | } | |
411 | ||
aafb230e AE |
412 | #ifdef RBD_DEBUG |
413 | #define rbd_assert(expr) \ | |
414 | if (unlikely(!(expr))) { \ | |
415 | printk(KERN_ERR "\nAssertion failure in %s() " \ | |
416 | "at line %d:\n\n" \ | |
417 | "\trbd_assert(%s);\n\n", \ | |
418 | __func__, __LINE__, #expr); \ | |
419 | BUG(); \ | |
420 | } | |
421 | #else /* !RBD_DEBUG */ | |
422 | # define rbd_assert(expr) ((void) 0) | |
423 | #endif /* !RBD_DEBUG */ | |
dfc5606d | 424 | |
8b3e1a56 AE |
425 | static void rbd_img_parent_read(struct rbd_obj_request *obj_request); |
426 | ||
117973fb AE |
427 | static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver); |
428 | static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver); | |
59c2be1e | 429 | |
602adf40 YS |
430 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
431 | { | |
f0f8cef5 | 432 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
b82d167b | 433 | bool removing = false; |
602adf40 | 434 | |
f84344f3 | 435 | if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only) |
602adf40 YS |
436 | return -EROFS; |
437 | ||
a14ea269 | 438 | spin_lock_irq(&rbd_dev->lock); |
b82d167b AE |
439 | if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) |
440 | removing = true; | |
441 | else | |
442 | rbd_dev->open_count++; | |
a14ea269 | 443 | spin_unlock_irq(&rbd_dev->lock); |
b82d167b AE |
444 | if (removing) |
445 | return -ENOENT; | |
446 | ||
42382b70 | 447 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
c3e946ce | 448 | (void) get_device(&rbd_dev->dev); |
f84344f3 | 449 | set_device_ro(bdev, rbd_dev->mapping.read_only); |
42382b70 | 450 | mutex_unlock(&ctl_mutex); |
340c7a2b | 451 | |
602adf40 YS |
452 | return 0; |
453 | } | |
454 | ||
dfc5606d YS |
455 | static int rbd_release(struct gendisk *disk, fmode_t mode) |
456 | { | |
457 | struct rbd_device *rbd_dev = disk->private_data; | |
b82d167b AE |
458 | unsigned long open_count_before; |
459 | ||
a14ea269 | 460 | spin_lock_irq(&rbd_dev->lock); |
b82d167b | 461 | open_count_before = rbd_dev->open_count--; |
a14ea269 | 462 | spin_unlock_irq(&rbd_dev->lock); |
b82d167b | 463 | rbd_assert(open_count_before > 0); |
dfc5606d | 464 | |
42382b70 | 465 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
c3e946ce | 466 | put_device(&rbd_dev->dev); |
42382b70 | 467 | mutex_unlock(&ctl_mutex); |
dfc5606d YS |
468 | |
469 | return 0; | |
470 | } | |
471 | ||
602adf40 YS |
472 | static const struct block_device_operations rbd_bd_ops = { |
473 | .owner = THIS_MODULE, | |
474 | .open = rbd_open, | |
dfc5606d | 475 | .release = rbd_release, |
602adf40 YS |
476 | }; |
477 | ||
478 | /* | |
479 | * Initialize an rbd client instance. | |
43ae4701 | 480 | * We own *ceph_opts. |
602adf40 | 481 | */ |
f8c38929 | 482 | static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts) |
602adf40 YS |
483 | { |
484 | struct rbd_client *rbdc; | |
485 | int ret = -ENOMEM; | |
486 | ||
37206ee5 | 487 | dout("%s:\n", __func__); |
602adf40 YS |
488 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); |
489 | if (!rbdc) | |
490 | goto out_opt; | |
491 | ||
492 | kref_init(&rbdc->kref); | |
493 | INIT_LIST_HEAD(&rbdc->node); | |
494 | ||
bc534d86 AE |
495 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
496 | ||
43ae4701 | 497 | rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0); |
602adf40 | 498 | if (IS_ERR(rbdc->client)) |
bc534d86 | 499 | goto out_mutex; |
43ae4701 | 500 | ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */ |
602adf40 YS |
501 | |
502 | ret = ceph_open_session(rbdc->client); | |
503 | if (ret < 0) | |
504 | goto out_err; | |
505 | ||
432b8587 | 506 | spin_lock(&rbd_client_list_lock); |
602adf40 | 507 | list_add_tail(&rbdc->node, &rbd_client_list); |
432b8587 | 508 | spin_unlock(&rbd_client_list_lock); |
602adf40 | 509 | |
bc534d86 | 510 | mutex_unlock(&ctl_mutex); |
37206ee5 | 511 | dout("%s: rbdc %p\n", __func__, rbdc); |
bc534d86 | 512 | |
602adf40 YS |
513 | return rbdc; |
514 | ||
515 | out_err: | |
516 | ceph_destroy_client(rbdc->client); | |
bc534d86 AE |
517 | out_mutex: |
518 | mutex_unlock(&ctl_mutex); | |
602adf40 YS |
519 | kfree(rbdc); |
520 | out_opt: | |
43ae4701 AE |
521 | if (ceph_opts) |
522 | ceph_destroy_options(ceph_opts); | |
37206ee5 AE |
523 | dout("%s: error %d\n", __func__, ret); |
524 | ||
28f259b7 | 525 | return ERR_PTR(ret); |
602adf40 YS |
526 | } |
527 | ||
2f82ee54 AE |
528 | static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc) |
529 | { | |
530 | kref_get(&rbdc->kref); | |
531 | ||
532 | return rbdc; | |
533 | } | |
534 | ||
602adf40 | 535 | /* |
1f7ba331 AE |
536 | * Find a ceph client with specific addr and configuration. If |
537 | * found, bump its reference count. | |
602adf40 | 538 | */ |
1f7ba331 | 539 | static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts) |
602adf40 YS |
540 | { |
541 | struct rbd_client *client_node; | |
1f7ba331 | 542 | bool found = false; |
602adf40 | 543 | |
43ae4701 | 544 | if (ceph_opts->flags & CEPH_OPT_NOSHARE) |
602adf40 YS |
545 | return NULL; |
546 | ||
1f7ba331 AE |
547 | spin_lock(&rbd_client_list_lock); |
548 | list_for_each_entry(client_node, &rbd_client_list, node) { | |
549 | if (!ceph_compare_options(ceph_opts, client_node->client)) { | |
2f82ee54 AE |
550 | __rbd_get_client(client_node); |
551 | ||
1f7ba331 AE |
552 | found = true; |
553 | break; | |
554 | } | |
555 | } | |
556 | spin_unlock(&rbd_client_list_lock); | |
557 | ||
558 | return found ? client_node : NULL; | |
602adf40 YS |
559 | } |
560 | ||
59c2be1e YS |
561 | /* |
562 | * mount options | |
563 | */ | |
564 | enum { | |
59c2be1e YS |
565 | Opt_last_int, |
566 | /* int args above */ | |
567 | Opt_last_string, | |
568 | /* string args above */ | |
cc0538b6 AE |
569 | Opt_read_only, |
570 | Opt_read_write, | |
571 | /* Boolean args above */ | |
572 | Opt_last_bool, | |
59c2be1e YS |
573 | }; |
574 | ||
43ae4701 | 575 | static match_table_t rbd_opts_tokens = { |
59c2be1e YS |
576 | /* int args above */ |
577 | /* string args above */ | |
be466c1c | 578 | {Opt_read_only, "read_only"}, |
cc0538b6 AE |
579 | {Opt_read_only, "ro"}, /* Alternate spelling */ |
580 | {Opt_read_write, "read_write"}, | |
581 | {Opt_read_write, "rw"}, /* Alternate spelling */ | |
582 | /* Boolean args above */ | |
59c2be1e YS |
583 | {-1, NULL} |
584 | }; | |
585 | ||
98571b5a AE |
586 | struct rbd_options { |
587 | bool read_only; | |
588 | }; | |
589 | ||
590 | #define RBD_READ_ONLY_DEFAULT false | |
591 | ||
59c2be1e YS |
592 | static int parse_rbd_opts_token(char *c, void *private) |
593 | { | |
43ae4701 | 594 | struct rbd_options *rbd_opts = private; |
59c2be1e YS |
595 | substring_t argstr[MAX_OPT_ARGS]; |
596 | int token, intval, ret; | |
597 | ||
43ae4701 | 598 | token = match_token(c, rbd_opts_tokens, argstr); |
59c2be1e YS |
599 | if (token < 0) |
600 | return -EINVAL; | |
601 | ||
602 | if (token < Opt_last_int) { | |
603 | ret = match_int(&argstr[0], &intval); | |
604 | if (ret < 0) { | |
605 | pr_err("bad mount option arg (not int) " | |
606 | "at '%s'\n", c); | |
607 | return ret; | |
608 | } | |
609 | dout("got int token %d val %d\n", token, intval); | |
610 | } else if (token > Opt_last_int && token < Opt_last_string) { | |
611 | dout("got string token %d val %s\n", token, | |
612 | argstr[0].from); | |
cc0538b6 AE |
613 | } else if (token > Opt_last_string && token < Opt_last_bool) { |
614 | dout("got Boolean token %d\n", token); | |
59c2be1e YS |
615 | } else { |
616 | dout("got token %d\n", token); | |
617 | } | |
618 | ||
619 | switch (token) { | |
cc0538b6 AE |
620 | case Opt_read_only: |
621 | rbd_opts->read_only = true; | |
622 | break; | |
623 | case Opt_read_write: | |
624 | rbd_opts->read_only = false; | |
625 | break; | |
59c2be1e | 626 | default: |
aafb230e AE |
627 | rbd_assert(false); |
628 | break; | |
59c2be1e YS |
629 | } |
630 | return 0; | |
631 | } | |
632 | ||
602adf40 YS |
633 | /* |
634 | * Get a ceph client with specific addr and configuration, if one does | |
635 | * not exist create it. | |
636 | */ | |
9d3997fd | 637 | static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts) |
602adf40 | 638 | { |
f8c38929 | 639 | struct rbd_client *rbdc; |
59c2be1e | 640 | |
1f7ba331 | 641 | rbdc = rbd_client_find(ceph_opts); |
9d3997fd | 642 | if (rbdc) /* using an existing client */ |
43ae4701 | 643 | ceph_destroy_options(ceph_opts); |
9d3997fd | 644 | else |
f8c38929 | 645 | rbdc = rbd_client_create(ceph_opts); |
602adf40 | 646 | |
9d3997fd | 647 | return rbdc; |
602adf40 YS |
648 | } |
649 | ||
650 | /* | |
651 | * Destroy ceph client | |
d23a4b3f | 652 | * |
432b8587 | 653 | * Caller must hold rbd_client_list_lock. |
602adf40 YS |
654 | */ |
655 | static void rbd_client_release(struct kref *kref) | |
656 | { | |
657 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); | |
658 | ||
37206ee5 | 659 | dout("%s: rbdc %p\n", __func__, rbdc); |
cd9d9f5d | 660 | spin_lock(&rbd_client_list_lock); |
602adf40 | 661 | list_del(&rbdc->node); |
cd9d9f5d | 662 | spin_unlock(&rbd_client_list_lock); |
602adf40 YS |
663 | |
664 | ceph_destroy_client(rbdc->client); | |
665 | kfree(rbdc); | |
666 | } | |
667 | ||
668 | /* | |
669 | * Drop reference to ceph client node. If it's not referenced anymore, release | |
670 | * it. | |
671 | */ | |
9d3997fd | 672 | static void rbd_put_client(struct rbd_client *rbdc) |
602adf40 | 673 | { |
c53d5893 AE |
674 | if (rbdc) |
675 | kref_put(&rbdc->kref, rbd_client_release); | |
602adf40 YS |
676 | } |
677 | ||
a30b71b9 AE |
678 | static bool rbd_image_format_valid(u32 image_format) |
679 | { | |
680 | return image_format == 1 || image_format == 2; | |
681 | } | |
682 | ||
8e94af8e AE |
683 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
684 | { | |
103a150f AE |
685 | size_t size; |
686 | u32 snap_count; | |
687 | ||
688 | /* The header has to start with the magic rbd header text */ | |
689 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) | |
690 | return false; | |
691 | ||
db2388b6 AE |
692 | /* The bio layer requires at least sector-sized I/O */ |
693 | ||
694 | if (ondisk->options.order < SECTOR_SHIFT) | |
695 | return false; | |
696 | ||
697 | /* If we use u64 in a few spots we may be able to loosen this */ | |
698 | ||
699 | if (ondisk->options.order > 8 * sizeof (int) - 1) | |
700 | return false; | |
701 | ||
103a150f AE |
702 | /* |
703 | * The size of a snapshot header has to fit in a size_t, and | |
704 | * that limits the number of snapshots. | |
705 | */ | |
706 | snap_count = le32_to_cpu(ondisk->snap_count); | |
707 | size = SIZE_MAX - sizeof (struct ceph_snap_context); | |
708 | if (snap_count > size / sizeof (__le64)) | |
709 | return false; | |
710 | ||
711 | /* | |
712 | * Not only that, but the size of the entire the snapshot | |
713 | * header must also be representable in a size_t. | |
714 | */ | |
715 | size -= snap_count * sizeof (__le64); | |
716 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) | |
717 | return false; | |
718 | ||
719 | return true; | |
8e94af8e AE |
720 | } |
721 | ||
602adf40 YS |
722 | /* |
723 | * Create a new header structure, translate header format from the on-disk | |
724 | * header. | |
725 | */ | |
726 | static int rbd_header_from_disk(struct rbd_image_header *header, | |
4156d998 | 727 | struct rbd_image_header_ondisk *ondisk) |
602adf40 | 728 | { |
ccece235 | 729 | u32 snap_count; |
58c17b0e | 730 | size_t len; |
d2bb24e5 | 731 | size_t size; |
621901d6 | 732 | u32 i; |
602adf40 | 733 | |
6a52325f AE |
734 | memset(header, 0, sizeof (*header)); |
735 | ||
103a150f AE |
736 | snap_count = le32_to_cpu(ondisk->snap_count); |
737 | ||
58c17b0e AE |
738 | len = strnlen(ondisk->object_prefix, sizeof (ondisk->object_prefix)); |
739 | header->object_prefix = kmalloc(len + 1, GFP_KERNEL); | |
6a52325f | 740 | if (!header->object_prefix) |
602adf40 | 741 | return -ENOMEM; |
58c17b0e AE |
742 | memcpy(header->object_prefix, ondisk->object_prefix, len); |
743 | header->object_prefix[len] = '\0'; | |
00f1f36f | 744 | |
602adf40 | 745 | if (snap_count) { |
f785cc1d AE |
746 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
747 | ||
621901d6 AE |
748 | /* Save a copy of the snapshot names */ |
749 | ||
f785cc1d AE |
750 | if (snap_names_len > (u64) SIZE_MAX) |
751 | return -EIO; | |
752 | header->snap_names = kmalloc(snap_names_len, GFP_KERNEL); | |
602adf40 | 753 | if (!header->snap_names) |
6a52325f | 754 | goto out_err; |
f785cc1d AE |
755 | /* |
756 | * Note that rbd_dev_v1_header_read() guarantees | |
757 | * the ondisk buffer we're working with has | |
758 | * snap_names_len bytes beyond the end of the | |
759 | * snapshot id array, this memcpy() is safe. | |
760 | */ | |
761 | memcpy(header->snap_names, &ondisk->snaps[snap_count], | |
762 | snap_names_len); | |
6a52325f | 763 | |
621901d6 AE |
764 | /* Record each snapshot's size */ |
765 | ||
d2bb24e5 AE |
766 | size = snap_count * sizeof (*header->snap_sizes); |
767 | header->snap_sizes = kmalloc(size, GFP_KERNEL); | |
602adf40 | 768 | if (!header->snap_sizes) |
6a52325f | 769 | goto out_err; |
621901d6 AE |
770 | for (i = 0; i < snap_count; i++) |
771 | header->snap_sizes[i] = | |
772 | le64_to_cpu(ondisk->snaps[i].image_size); | |
602adf40 | 773 | } else { |
ccece235 | 774 | WARN_ON(ondisk->snap_names_len); |
602adf40 YS |
775 | header->snap_names = NULL; |
776 | header->snap_sizes = NULL; | |
777 | } | |
849b4260 | 778 | |
34b13184 | 779 | header->features = 0; /* No features support in v1 images */ |
602adf40 YS |
780 | header->obj_order = ondisk->options.order; |
781 | header->crypt_type = ondisk->options.crypt_type; | |
782 | header->comp_type = ondisk->options.comp_type; | |
6a52325f | 783 | |
621901d6 AE |
784 | /* Allocate and fill in the snapshot context */ |
785 | ||
f84344f3 | 786 | header->image_size = le64_to_cpu(ondisk->image_size); |
6a52325f AE |
787 | size = sizeof (struct ceph_snap_context); |
788 | size += snap_count * sizeof (header->snapc->snaps[0]); | |
789 | header->snapc = kzalloc(size, GFP_KERNEL); | |
790 | if (!header->snapc) | |
791 | goto out_err; | |
602adf40 YS |
792 | |
793 | atomic_set(&header->snapc->nref, 1); | |
505cbb9b | 794 | header->snapc->seq = le64_to_cpu(ondisk->snap_seq); |
602adf40 | 795 | header->snapc->num_snaps = snap_count; |
621901d6 AE |
796 | for (i = 0; i < snap_count; i++) |
797 | header->snapc->snaps[i] = | |
798 | le64_to_cpu(ondisk->snaps[i].id); | |
602adf40 YS |
799 | |
800 | return 0; | |
801 | ||
6a52325f | 802 | out_err: |
849b4260 | 803 | kfree(header->snap_sizes); |
ccece235 | 804 | header->snap_sizes = NULL; |
602adf40 | 805 | kfree(header->snap_names); |
ccece235 | 806 | header->snap_names = NULL; |
6a52325f AE |
807 | kfree(header->object_prefix); |
808 | header->object_prefix = NULL; | |
ccece235 | 809 | |
00f1f36f | 810 | return -ENOMEM; |
602adf40 YS |
811 | } |
812 | ||
9e15b77d AE |
813 | static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id) |
814 | { | |
815 | struct rbd_snap *snap; | |
816 | ||
817 | if (snap_id == CEPH_NOSNAP) | |
818 | return RBD_SNAP_HEAD_NAME; | |
819 | ||
820 | list_for_each_entry(snap, &rbd_dev->snaps, node) | |
821 | if (snap_id == snap->id) | |
822 | return snap->name; | |
823 | ||
824 | return NULL; | |
825 | } | |
826 | ||
8836b995 | 827 | static int snap_by_name(struct rbd_device *rbd_dev, const char *snap_name) |
602adf40 | 828 | { |
602adf40 | 829 | |
e86924a8 | 830 | struct rbd_snap *snap; |
602adf40 | 831 | |
e86924a8 AE |
832 | list_for_each_entry(snap, &rbd_dev->snaps, node) { |
833 | if (!strcmp(snap_name, snap->name)) { | |
0d7dbfce | 834 | rbd_dev->spec->snap_id = snap->id; |
e86924a8 | 835 | rbd_dev->mapping.size = snap->size; |
34b13184 | 836 | rbd_dev->mapping.features = snap->features; |
602adf40 | 837 | |
e86924a8 | 838 | return 0; |
00f1f36f | 839 | } |
00f1f36f | 840 | } |
e86924a8 | 841 | |
00f1f36f | 842 | return -ENOENT; |
602adf40 YS |
843 | } |
844 | ||
819d52bf | 845 | static int rbd_dev_set_mapping(struct rbd_device *rbd_dev) |
602adf40 | 846 | { |
78dc447d | 847 | int ret; |
602adf40 | 848 | |
0d7dbfce | 849 | if (!memcmp(rbd_dev->spec->snap_name, RBD_SNAP_HEAD_NAME, |
cc9d734c | 850 | sizeof (RBD_SNAP_HEAD_NAME))) { |
0d7dbfce | 851 | rbd_dev->spec->snap_id = CEPH_NOSNAP; |
99c1f08f | 852 | rbd_dev->mapping.size = rbd_dev->header.image_size; |
34b13184 | 853 | rbd_dev->mapping.features = rbd_dev->header.features; |
e86924a8 | 854 | ret = 0; |
602adf40 | 855 | } else { |
0d7dbfce | 856 | ret = snap_by_name(rbd_dev, rbd_dev->spec->snap_name); |
602adf40 YS |
857 | if (ret < 0) |
858 | goto done; | |
f84344f3 | 859 | rbd_dev->mapping.read_only = true; |
602adf40 | 860 | } |
6d292906 AE |
861 | set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
862 | ||
602adf40 | 863 | done: |
602adf40 YS |
864 | return ret; |
865 | } | |
866 | ||
867 | static void rbd_header_free(struct rbd_image_header *header) | |
868 | { | |
849b4260 | 869 | kfree(header->object_prefix); |
d78fd7ae | 870 | header->object_prefix = NULL; |
602adf40 | 871 | kfree(header->snap_sizes); |
d78fd7ae | 872 | header->snap_sizes = NULL; |
849b4260 | 873 | kfree(header->snap_names); |
d78fd7ae | 874 | header->snap_names = NULL; |
d1d25646 | 875 | ceph_put_snap_context(header->snapc); |
d78fd7ae | 876 | header->snapc = NULL; |
602adf40 YS |
877 | } |
878 | ||
98571b5a | 879 | static const char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset) |
602adf40 | 880 | { |
65ccfe21 AE |
881 | char *name; |
882 | u64 segment; | |
883 | int ret; | |
602adf40 | 884 | |
2fd82b9e | 885 | name = kmalloc(MAX_OBJ_NAME_SIZE + 1, GFP_NOIO); |
65ccfe21 AE |
886 | if (!name) |
887 | return NULL; | |
888 | segment = offset >> rbd_dev->header.obj_order; | |
2fd82b9e | 889 | ret = snprintf(name, MAX_OBJ_NAME_SIZE + 1, "%s.%012llx", |
65ccfe21 | 890 | rbd_dev->header.object_prefix, segment); |
2fd82b9e | 891 | if (ret < 0 || ret > MAX_OBJ_NAME_SIZE) { |
65ccfe21 AE |
892 | pr_err("error formatting segment name for #%llu (%d)\n", |
893 | segment, ret); | |
894 | kfree(name); | |
895 | name = NULL; | |
896 | } | |
602adf40 | 897 | |
65ccfe21 AE |
898 | return name; |
899 | } | |
602adf40 | 900 | |
65ccfe21 AE |
901 | static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset) |
902 | { | |
903 | u64 segment_size = (u64) 1 << rbd_dev->header.obj_order; | |
602adf40 | 904 | |
65ccfe21 AE |
905 | return offset & (segment_size - 1); |
906 | } | |
907 | ||
908 | static u64 rbd_segment_length(struct rbd_device *rbd_dev, | |
909 | u64 offset, u64 length) | |
910 | { | |
911 | u64 segment_size = (u64) 1 << rbd_dev->header.obj_order; | |
912 | ||
913 | offset &= segment_size - 1; | |
914 | ||
aafb230e | 915 | rbd_assert(length <= U64_MAX - offset); |
65ccfe21 AE |
916 | if (offset + length > segment_size) |
917 | length = segment_size - offset; | |
918 | ||
919 | return length; | |
602adf40 YS |
920 | } |
921 | ||
029bcbd8 JD |
922 | /* |
923 | * returns the size of an object in the image | |
924 | */ | |
925 | static u64 rbd_obj_bytes(struct rbd_image_header *header) | |
926 | { | |
927 | return 1 << header->obj_order; | |
928 | } | |
929 | ||
602adf40 YS |
930 | /* |
931 | * bio helpers | |
932 | */ | |
933 | ||
934 | static void bio_chain_put(struct bio *chain) | |
935 | { | |
936 | struct bio *tmp; | |
937 | ||
938 | while (chain) { | |
939 | tmp = chain; | |
940 | chain = chain->bi_next; | |
941 | bio_put(tmp); | |
942 | } | |
943 | } | |
944 | ||
945 | /* | |
946 | * zeros a bio chain, starting at specific offset | |
947 | */ | |
948 | static void zero_bio_chain(struct bio *chain, int start_ofs) | |
949 | { | |
950 | struct bio_vec *bv; | |
951 | unsigned long flags; | |
952 | void *buf; | |
953 | int i; | |
954 | int pos = 0; | |
955 | ||
956 | while (chain) { | |
957 | bio_for_each_segment(bv, chain, i) { | |
958 | if (pos + bv->bv_len > start_ofs) { | |
959 | int remainder = max(start_ofs - pos, 0); | |
960 | buf = bvec_kmap_irq(bv, &flags); | |
961 | memset(buf + remainder, 0, | |
962 | bv->bv_len - remainder); | |
85b5aaa6 | 963 | bvec_kunmap_irq(buf, &flags); |
602adf40 YS |
964 | } |
965 | pos += bv->bv_len; | |
966 | } | |
967 | ||
968 | chain = chain->bi_next; | |
969 | } | |
970 | } | |
971 | ||
972 | /* | |
f7760dad AE |
973 | * Clone a portion of a bio, starting at the given byte offset |
974 | * and continuing for the number of bytes indicated. | |
602adf40 | 975 | */ |
f7760dad AE |
976 | static struct bio *bio_clone_range(struct bio *bio_src, |
977 | unsigned int offset, | |
978 | unsigned int len, | |
979 | gfp_t gfpmask) | |
602adf40 | 980 | { |
f7760dad AE |
981 | struct bio_vec *bv; |
982 | unsigned int resid; | |
983 | unsigned short idx; | |
984 | unsigned int voff; | |
985 | unsigned short end_idx; | |
986 | unsigned short vcnt; | |
987 | struct bio *bio; | |
988 | ||
989 | /* Handle the easy case for the caller */ | |
990 | ||
991 | if (!offset && len == bio_src->bi_size) | |
992 | return bio_clone(bio_src, gfpmask); | |
993 | ||
994 | if (WARN_ON_ONCE(!len)) | |
995 | return NULL; | |
996 | if (WARN_ON_ONCE(len > bio_src->bi_size)) | |
997 | return NULL; | |
998 | if (WARN_ON_ONCE(offset > bio_src->bi_size - len)) | |
999 | return NULL; | |
1000 | ||
1001 | /* Find first affected segment... */ | |
1002 | ||
1003 | resid = offset; | |
1004 | __bio_for_each_segment(bv, bio_src, idx, 0) { | |
1005 | if (resid < bv->bv_len) | |
1006 | break; | |
1007 | resid -= bv->bv_len; | |
602adf40 | 1008 | } |
f7760dad | 1009 | voff = resid; |
602adf40 | 1010 | |
f7760dad | 1011 | /* ...and the last affected segment */ |
602adf40 | 1012 | |
f7760dad AE |
1013 | resid += len; |
1014 | __bio_for_each_segment(bv, bio_src, end_idx, idx) { | |
1015 | if (resid <= bv->bv_len) | |
1016 | break; | |
1017 | resid -= bv->bv_len; | |
1018 | } | |
1019 | vcnt = end_idx - idx + 1; | |
1020 | ||
1021 | /* Build the clone */ | |
1022 | ||
1023 | bio = bio_alloc(gfpmask, (unsigned int) vcnt); | |
1024 | if (!bio) | |
1025 | return NULL; /* ENOMEM */ | |
602adf40 | 1026 | |
f7760dad AE |
1027 | bio->bi_bdev = bio_src->bi_bdev; |
1028 | bio->bi_sector = bio_src->bi_sector + (offset >> SECTOR_SHIFT); | |
1029 | bio->bi_rw = bio_src->bi_rw; | |
1030 | bio->bi_flags |= 1 << BIO_CLONED; | |
1031 | ||
1032 | /* | |
1033 | * Copy over our part of the bio_vec, then update the first | |
1034 | * and last (or only) entries. | |
1035 | */ | |
1036 | memcpy(&bio->bi_io_vec[0], &bio_src->bi_io_vec[idx], | |
1037 | vcnt * sizeof (struct bio_vec)); | |
1038 | bio->bi_io_vec[0].bv_offset += voff; | |
1039 | if (vcnt > 1) { | |
1040 | bio->bi_io_vec[0].bv_len -= voff; | |
1041 | bio->bi_io_vec[vcnt - 1].bv_len = resid; | |
1042 | } else { | |
1043 | bio->bi_io_vec[0].bv_len = len; | |
602adf40 YS |
1044 | } |
1045 | ||
f7760dad AE |
1046 | bio->bi_vcnt = vcnt; |
1047 | bio->bi_size = len; | |
1048 | bio->bi_idx = 0; | |
1049 | ||
1050 | return bio; | |
1051 | } | |
1052 | ||
1053 | /* | |
1054 | * Clone a portion of a bio chain, starting at the given byte offset | |
1055 | * into the first bio in the source chain and continuing for the | |
1056 | * number of bytes indicated. The result is another bio chain of | |
1057 | * exactly the given length, or a null pointer on error. | |
1058 | * | |
1059 | * The bio_src and offset parameters are both in-out. On entry they | |
1060 | * refer to the first source bio and the offset into that bio where | |
1061 | * the start of data to be cloned is located. | |
1062 | * | |
1063 | * On return, bio_src is updated to refer to the bio in the source | |
1064 | * chain that contains first un-cloned byte, and *offset will | |
1065 | * contain the offset of that byte within that bio. | |
1066 | */ | |
1067 | static struct bio *bio_chain_clone_range(struct bio **bio_src, | |
1068 | unsigned int *offset, | |
1069 | unsigned int len, | |
1070 | gfp_t gfpmask) | |
1071 | { | |
1072 | struct bio *bi = *bio_src; | |
1073 | unsigned int off = *offset; | |
1074 | struct bio *chain = NULL; | |
1075 | struct bio **end; | |
1076 | ||
1077 | /* Build up a chain of clone bios up to the limit */ | |
1078 | ||
1079 | if (!bi || off >= bi->bi_size || !len) | |
1080 | return NULL; /* Nothing to clone */ | |
602adf40 | 1081 | |
f7760dad AE |
1082 | end = &chain; |
1083 | while (len) { | |
1084 | unsigned int bi_size; | |
1085 | struct bio *bio; | |
1086 | ||
f5400b7a AE |
1087 | if (!bi) { |
1088 | rbd_warn(NULL, "bio_chain exhausted with %u left", len); | |
f7760dad | 1089 | goto out_err; /* EINVAL; ran out of bio's */ |
f5400b7a | 1090 | } |
f7760dad AE |
1091 | bi_size = min_t(unsigned int, bi->bi_size - off, len); |
1092 | bio = bio_clone_range(bi, off, bi_size, gfpmask); | |
1093 | if (!bio) | |
1094 | goto out_err; /* ENOMEM */ | |
1095 | ||
1096 | *end = bio; | |
1097 | end = &bio->bi_next; | |
602adf40 | 1098 | |
f7760dad AE |
1099 | off += bi_size; |
1100 | if (off == bi->bi_size) { | |
1101 | bi = bi->bi_next; | |
1102 | off = 0; | |
1103 | } | |
1104 | len -= bi_size; | |
1105 | } | |
1106 | *bio_src = bi; | |
1107 | *offset = off; | |
1108 | ||
1109 | return chain; | |
1110 | out_err: | |
1111 | bio_chain_put(chain); | |
602adf40 | 1112 | |
602adf40 YS |
1113 | return NULL; |
1114 | } | |
1115 | ||
926f9b3f AE |
1116 | /* |
1117 | * The default/initial value for all object request flags is 0. For | |
1118 | * each flag, once its value is set to 1 it is never reset to 0 | |
1119 | * again. | |
1120 | */ | |
57acbaa7 | 1121 | static void obj_request_img_data_set(struct rbd_obj_request *obj_request) |
926f9b3f | 1122 | { |
57acbaa7 | 1123 | if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) { |
926f9b3f AE |
1124 | struct rbd_device *rbd_dev; |
1125 | ||
57acbaa7 AE |
1126 | rbd_dev = obj_request->img_request->rbd_dev; |
1127 | rbd_warn(rbd_dev, "obj_request %p already marked img_data\n", | |
926f9b3f AE |
1128 | obj_request); |
1129 | } | |
1130 | } | |
1131 | ||
57acbaa7 | 1132 | static bool obj_request_img_data_test(struct rbd_obj_request *obj_request) |
926f9b3f AE |
1133 | { |
1134 | smp_mb(); | |
57acbaa7 | 1135 | return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0; |
926f9b3f AE |
1136 | } |
1137 | ||
57acbaa7 | 1138 | static void obj_request_done_set(struct rbd_obj_request *obj_request) |
6365d33a | 1139 | { |
57acbaa7 AE |
1140 | if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) { |
1141 | struct rbd_device *rbd_dev = NULL; | |
6365d33a | 1142 | |
57acbaa7 AE |
1143 | if (obj_request_img_data_test(obj_request)) |
1144 | rbd_dev = obj_request->img_request->rbd_dev; | |
1145 | rbd_warn(rbd_dev, "obj_request %p already marked done\n", | |
6365d33a AE |
1146 | obj_request); |
1147 | } | |
1148 | } | |
1149 | ||
57acbaa7 | 1150 | static bool obj_request_done_test(struct rbd_obj_request *obj_request) |
6365d33a AE |
1151 | { |
1152 | smp_mb(); | |
57acbaa7 | 1153 | return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0; |
6365d33a AE |
1154 | } |
1155 | ||
5679c59f AE |
1156 | /* |
1157 | * This sets the KNOWN flag after (possibly) setting the EXISTS | |
1158 | * flag. The latter is set based on the "exists" value provided. | |
1159 | * | |
1160 | * Note that for our purposes once an object exists it never goes | |
1161 | * away again. It's possible that the response from two existence | |
1162 | * checks are separated by the creation of the target object, and | |
1163 | * the first ("doesn't exist") response arrives *after* the second | |
1164 | * ("does exist"). In that case we ignore the second one. | |
1165 | */ | |
1166 | static void obj_request_existence_set(struct rbd_obj_request *obj_request, | |
1167 | bool exists) | |
1168 | { | |
1169 | if (exists) | |
1170 | set_bit(OBJ_REQ_EXISTS, &obj_request->flags); | |
1171 | set_bit(OBJ_REQ_KNOWN, &obj_request->flags); | |
1172 | smp_mb(); | |
1173 | } | |
1174 | ||
1175 | static bool obj_request_known_test(struct rbd_obj_request *obj_request) | |
1176 | { | |
1177 | smp_mb(); | |
1178 | return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0; | |
1179 | } | |
1180 | ||
1181 | static bool obj_request_exists_test(struct rbd_obj_request *obj_request) | |
1182 | { | |
1183 | smp_mb(); | |
1184 | return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0; | |
1185 | } | |
1186 | ||
bf0d5f50 AE |
1187 | static void rbd_obj_request_get(struct rbd_obj_request *obj_request) |
1188 | { | |
37206ee5 AE |
1189 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
1190 | atomic_read(&obj_request->kref.refcount)); | |
bf0d5f50 AE |
1191 | kref_get(&obj_request->kref); |
1192 | } | |
1193 | ||
1194 | static void rbd_obj_request_destroy(struct kref *kref); | |
1195 | static void rbd_obj_request_put(struct rbd_obj_request *obj_request) | |
1196 | { | |
1197 | rbd_assert(obj_request != NULL); | |
37206ee5 AE |
1198 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
1199 | atomic_read(&obj_request->kref.refcount)); | |
bf0d5f50 AE |
1200 | kref_put(&obj_request->kref, rbd_obj_request_destroy); |
1201 | } | |
1202 | ||
1203 | static void rbd_img_request_get(struct rbd_img_request *img_request) | |
1204 | { | |
37206ee5 AE |
1205 | dout("%s: img %p (was %d)\n", __func__, img_request, |
1206 | atomic_read(&img_request->kref.refcount)); | |
bf0d5f50 AE |
1207 | kref_get(&img_request->kref); |
1208 | } | |
1209 | ||
1210 | static void rbd_img_request_destroy(struct kref *kref); | |
1211 | static void rbd_img_request_put(struct rbd_img_request *img_request) | |
1212 | { | |
1213 | rbd_assert(img_request != NULL); | |
37206ee5 AE |
1214 | dout("%s: img %p (was %d)\n", __func__, img_request, |
1215 | atomic_read(&img_request->kref.refcount)); | |
bf0d5f50 AE |
1216 | kref_put(&img_request->kref, rbd_img_request_destroy); |
1217 | } | |
1218 | ||
1219 | static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request, | |
1220 | struct rbd_obj_request *obj_request) | |
1221 | { | |
25dcf954 AE |
1222 | rbd_assert(obj_request->img_request == NULL); |
1223 | ||
b155e86c | 1224 | /* Image request now owns object's original reference */ |
bf0d5f50 | 1225 | obj_request->img_request = img_request; |
25dcf954 | 1226 | obj_request->which = img_request->obj_request_count; |
6365d33a AE |
1227 | rbd_assert(!obj_request_img_data_test(obj_request)); |
1228 | obj_request_img_data_set(obj_request); | |
bf0d5f50 | 1229 | rbd_assert(obj_request->which != BAD_WHICH); |
25dcf954 AE |
1230 | img_request->obj_request_count++; |
1231 | list_add_tail(&obj_request->links, &img_request->obj_requests); | |
37206ee5 AE |
1232 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
1233 | obj_request->which); | |
bf0d5f50 AE |
1234 | } |
1235 | ||
1236 | static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request, | |
1237 | struct rbd_obj_request *obj_request) | |
1238 | { | |
1239 | rbd_assert(obj_request->which != BAD_WHICH); | |
25dcf954 | 1240 | |
37206ee5 AE |
1241 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
1242 | obj_request->which); | |
bf0d5f50 | 1243 | list_del(&obj_request->links); |
25dcf954 AE |
1244 | rbd_assert(img_request->obj_request_count > 0); |
1245 | img_request->obj_request_count--; | |
1246 | rbd_assert(obj_request->which == img_request->obj_request_count); | |
1247 | obj_request->which = BAD_WHICH; | |
6365d33a | 1248 | rbd_assert(obj_request_img_data_test(obj_request)); |
bf0d5f50 | 1249 | rbd_assert(obj_request->img_request == img_request); |
bf0d5f50 | 1250 | obj_request->img_request = NULL; |
25dcf954 | 1251 | obj_request->callback = NULL; |
bf0d5f50 AE |
1252 | rbd_obj_request_put(obj_request); |
1253 | } | |
1254 | ||
1255 | static bool obj_request_type_valid(enum obj_request_type type) | |
1256 | { | |
1257 | switch (type) { | |
9969ebc5 | 1258 | case OBJ_REQUEST_NODATA: |
bf0d5f50 | 1259 | case OBJ_REQUEST_BIO: |
788e2df3 | 1260 | case OBJ_REQUEST_PAGES: |
bf0d5f50 AE |
1261 | return true; |
1262 | default: | |
1263 | return false; | |
1264 | } | |
1265 | } | |
1266 | ||
bf0d5f50 AE |
1267 | static int rbd_obj_request_submit(struct ceph_osd_client *osdc, |
1268 | struct rbd_obj_request *obj_request) | |
1269 | { | |
37206ee5 AE |
1270 | dout("%s: osdc %p obj %p\n", __func__, osdc, obj_request); |
1271 | ||
bf0d5f50 AE |
1272 | return ceph_osdc_start_request(osdc, obj_request->osd_req, false); |
1273 | } | |
1274 | ||
1275 | static void rbd_img_request_complete(struct rbd_img_request *img_request) | |
1276 | { | |
55f27e09 | 1277 | |
37206ee5 | 1278 | dout("%s: img %p\n", __func__, img_request); |
55f27e09 AE |
1279 | |
1280 | /* | |
1281 | * If no error occurred, compute the aggregate transfer | |
1282 | * count for the image request. We could instead use | |
1283 | * atomic64_cmpxchg() to update it as each object request | |
1284 | * completes; not clear which way is better off hand. | |
1285 | */ | |
1286 | if (!img_request->result) { | |
1287 | struct rbd_obj_request *obj_request; | |
1288 | u64 xferred = 0; | |
1289 | ||
1290 | for_each_obj_request(img_request, obj_request) | |
1291 | xferred += obj_request->xferred; | |
1292 | img_request->xferred = xferred; | |
1293 | } | |
1294 | ||
bf0d5f50 AE |
1295 | if (img_request->callback) |
1296 | img_request->callback(img_request); | |
1297 | else | |
1298 | rbd_img_request_put(img_request); | |
1299 | } | |
1300 | ||
788e2df3 AE |
1301 | /* Caller is responsible for rbd_obj_request_destroy(obj_request) */ |
1302 | ||
1303 | static int rbd_obj_request_wait(struct rbd_obj_request *obj_request) | |
1304 | { | |
37206ee5 AE |
1305 | dout("%s: obj %p\n", __func__, obj_request); |
1306 | ||
788e2df3 AE |
1307 | return wait_for_completion_interruptible(&obj_request->completion); |
1308 | } | |
1309 | ||
0c425248 AE |
1310 | /* |
1311 | * The default/initial value for all image request flags is 0. Each | |
1312 | * is conditionally set to 1 at image request initialization time | |
1313 | * and currently never change thereafter. | |
1314 | */ | |
1315 | static void img_request_write_set(struct rbd_img_request *img_request) | |
1316 | { | |
1317 | set_bit(IMG_REQ_WRITE, &img_request->flags); | |
1318 | smp_mb(); | |
1319 | } | |
1320 | ||
1321 | static bool img_request_write_test(struct rbd_img_request *img_request) | |
1322 | { | |
1323 | smp_mb(); | |
1324 | return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0; | |
1325 | } | |
1326 | ||
9849e986 AE |
1327 | static void img_request_child_set(struct rbd_img_request *img_request) |
1328 | { | |
1329 | set_bit(IMG_REQ_CHILD, &img_request->flags); | |
1330 | smp_mb(); | |
1331 | } | |
1332 | ||
1333 | static bool img_request_child_test(struct rbd_img_request *img_request) | |
1334 | { | |
1335 | smp_mb(); | |
1336 | return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0; | |
1337 | } | |
1338 | ||
d0b2e944 AE |
1339 | static void img_request_layered_set(struct rbd_img_request *img_request) |
1340 | { | |
1341 | set_bit(IMG_REQ_LAYERED, &img_request->flags); | |
1342 | smp_mb(); | |
1343 | } | |
1344 | ||
1345 | static bool img_request_layered_test(struct rbd_img_request *img_request) | |
1346 | { | |
1347 | smp_mb(); | |
1348 | return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0; | |
1349 | } | |
1350 | ||
6e2a4505 AE |
1351 | static void |
1352 | rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request) | |
1353 | { | |
1354 | dout("%s: obj %p img %p result %d %llu/%llu\n", __func__, | |
1355 | obj_request, obj_request->img_request, obj_request->result, | |
1356 | obj_request->xferred, obj_request->length); | |
1357 | /* | |
1358 | * ENOENT means a hole in the image. We zero-fill the | |
1359 | * entire length of the request. A short read also implies | |
1360 | * zero-fill to the end of the request. Either way we | |
1361 | * update the xferred count to indicate the whole request | |
1362 | * was satisfied. | |
1363 | */ | |
1364 | BUG_ON(obj_request->type != OBJ_REQUEST_BIO); | |
1365 | if (obj_request->result == -ENOENT) { | |
1366 | zero_bio_chain(obj_request->bio_list, 0); | |
1367 | obj_request->result = 0; | |
1368 | obj_request->xferred = obj_request->length; | |
1369 | } else if (obj_request->xferred < obj_request->length && | |
1370 | !obj_request->result) { | |
1371 | zero_bio_chain(obj_request->bio_list, obj_request->xferred); | |
1372 | obj_request->xferred = obj_request->length; | |
1373 | } | |
1374 | obj_request_done_set(obj_request); | |
1375 | } | |
1376 | ||
bf0d5f50 AE |
1377 | static void rbd_obj_request_complete(struct rbd_obj_request *obj_request) |
1378 | { | |
37206ee5 AE |
1379 | dout("%s: obj %p cb %p\n", __func__, obj_request, |
1380 | obj_request->callback); | |
bf0d5f50 AE |
1381 | if (obj_request->callback) |
1382 | obj_request->callback(obj_request); | |
788e2df3 AE |
1383 | else |
1384 | complete_all(&obj_request->completion); | |
bf0d5f50 AE |
1385 | } |
1386 | ||
c47f9371 | 1387 | static void rbd_osd_trivial_callback(struct rbd_obj_request *obj_request) |
39bf2c5d AE |
1388 | { |
1389 | dout("%s: obj %p\n", __func__, obj_request); | |
1390 | obj_request_done_set(obj_request); | |
1391 | } | |
1392 | ||
c47f9371 | 1393 | static void rbd_osd_read_callback(struct rbd_obj_request *obj_request) |
bf0d5f50 | 1394 | { |
57acbaa7 AE |
1395 | struct rbd_img_request *img_request = NULL; |
1396 | bool layered = false; | |
1397 | ||
1398 | if (obj_request_img_data_test(obj_request)) { | |
1399 | img_request = obj_request->img_request; | |
1400 | layered = img_request && img_request_layered_test(img_request); | |
1401 | } else { | |
1402 | img_request = NULL; | |
1403 | layered = false; | |
1404 | } | |
8b3e1a56 AE |
1405 | |
1406 | dout("%s: obj %p img %p result %d %llu/%llu\n", __func__, | |
1407 | obj_request, img_request, obj_request->result, | |
1408 | obj_request->xferred, obj_request->length); | |
1409 | if (layered && obj_request->result == -ENOENT) | |
1410 | rbd_img_parent_read(obj_request); | |
1411 | else if (img_request) | |
6e2a4505 AE |
1412 | rbd_img_obj_request_read_callback(obj_request); |
1413 | else | |
1414 | obj_request_done_set(obj_request); | |
bf0d5f50 AE |
1415 | } |
1416 | ||
c47f9371 | 1417 | static void rbd_osd_write_callback(struct rbd_obj_request *obj_request) |
bf0d5f50 | 1418 | { |
1b83bef2 SW |
1419 | dout("%s: obj %p result %d %llu\n", __func__, obj_request, |
1420 | obj_request->result, obj_request->length); | |
1421 | /* | |
8b3e1a56 AE |
1422 | * There is no such thing as a successful short write. Set |
1423 | * it to our originally-requested length. | |
1b83bef2 SW |
1424 | */ |
1425 | obj_request->xferred = obj_request->length; | |
07741308 | 1426 | obj_request_done_set(obj_request); |
bf0d5f50 AE |
1427 | } |
1428 | ||
fbfab539 AE |
1429 | /* |
1430 | * For a simple stat call there's nothing to do. We'll do more if | |
1431 | * this is part of a write sequence for a layered image. | |
1432 | */ | |
c47f9371 | 1433 | static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request) |
fbfab539 | 1434 | { |
37206ee5 | 1435 | dout("%s: obj %p\n", __func__, obj_request); |
fbfab539 AE |
1436 | obj_request_done_set(obj_request); |
1437 | } | |
1438 | ||
bf0d5f50 AE |
1439 | static void rbd_osd_req_callback(struct ceph_osd_request *osd_req, |
1440 | struct ceph_msg *msg) | |
1441 | { | |
1442 | struct rbd_obj_request *obj_request = osd_req->r_priv; | |
bf0d5f50 AE |
1443 | u16 opcode; |
1444 | ||
37206ee5 | 1445 | dout("%s: osd_req %p msg %p\n", __func__, osd_req, msg); |
bf0d5f50 | 1446 | rbd_assert(osd_req == obj_request->osd_req); |
57acbaa7 AE |
1447 | if (obj_request_img_data_test(obj_request)) { |
1448 | rbd_assert(obj_request->img_request); | |
1449 | rbd_assert(obj_request->which != BAD_WHICH); | |
1450 | } else { | |
1451 | rbd_assert(obj_request->which == BAD_WHICH); | |
1452 | } | |
bf0d5f50 | 1453 | |
1b83bef2 SW |
1454 | if (osd_req->r_result < 0) |
1455 | obj_request->result = osd_req->r_result; | |
bf0d5f50 AE |
1456 | obj_request->version = le64_to_cpu(osd_req->r_reassert_version.version); |
1457 | ||
1b83bef2 | 1458 | WARN_ON(osd_req->r_num_ops != 1); /* For now */ |
bf0d5f50 | 1459 | |
c47f9371 AE |
1460 | /* |
1461 | * We support a 64-bit length, but ultimately it has to be | |
1462 | * passed to blk_end_request(), which takes an unsigned int. | |
1463 | */ | |
1b83bef2 | 1464 | obj_request->xferred = osd_req->r_reply_op_len[0]; |
8b3e1a56 | 1465 | rbd_assert(obj_request->xferred < (u64)UINT_MAX); |
79528734 | 1466 | opcode = osd_req->r_ops[0].op; |
bf0d5f50 AE |
1467 | switch (opcode) { |
1468 | case CEPH_OSD_OP_READ: | |
c47f9371 | 1469 | rbd_osd_read_callback(obj_request); |
bf0d5f50 AE |
1470 | break; |
1471 | case CEPH_OSD_OP_WRITE: | |
c47f9371 | 1472 | rbd_osd_write_callback(obj_request); |
bf0d5f50 | 1473 | break; |
fbfab539 | 1474 | case CEPH_OSD_OP_STAT: |
c47f9371 | 1475 | rbd_osd_stat_callback(obj_request); |
fbfab539 | 1476 | break; |
36be9a76 | 1477 | case CEPH_OSD_OP_CALL: |
b8d70035 | 1478 | case CEPH_OSD_OP_NOTIFY_ACK: |
9969ebc5 | 1479 | case CEPH_OSD_OP_WATCH: |
c47f9371 | 1480 | rbd_osd_trivial_callback(obj_request); |
9969ebc5 | 1481 | break; |
bf0d5f50 AE |
1482 | default: |
1483 | rbd_warn(NULL, "%s: unsupported op %hu\n", | |
1484 | obj_request->object_name, (unsigned short) opcode); | |
1485 | break; | |
1486 | } | |
1487 | ||
07741308 | 1488 | if (obj_request_done_test(obj_request)) |
bf0d5f50 AE |
1489 | rbd_obj_request_complete(obj_request); |
1490 | } | |
1491 | ||
2fa12320 | 1492 | static void rbd_osd_req_format(struct rbd_obj_request *obj_request, |
79528734 | 1493 | bool write_request) |
430c28c3 AE |
1494 | { |
1495 | struct rbd_img_request *img_request = obj_request->img_request; | |
8c042b0d | 1496 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
430c28c3 AE |
1497 | struct ceph_snap_context *snapc = NULL; |
1498 | u64 snap_id = CEPH_NOSNAP; | |
1499 | struct timespec *mtime = NULL; | |
1500 | struct timespec now; | |
1501 | ||
8c042b0d | 1502 | rbd_assert(osd_req != NULL); |
430c28c3 AE |
1503 | |
1504 | if (write_request) { | |
1505 | now = CURRENT_TIME; | |
1506 | mtime = &now; | |
1507 | if (img_request) | |
1508 | snapc = img_request->snapc; | |
2fa12320 AE |
1509 | } else if (img_request) { |
1510 | snap_id = img_request->snap_id; | |
8c042b0d AE |
1511 | } |
1512 | ceph_osdc_build_request(osd_req, obj_request->offset, | |
79528734 | 1513 | snapc, snap_id, mtime); |
430c28c3 AE |
1514 | } |
1515 | ||
bf0d5f50 AE |
1516 | static struct ceph_osd_request *rbd_osd_req_create( |
1517 | struct rbd_device *rbd_dev, | |
1518 | bool write_request, | |
430c28c3 | 1519 | struct rbd_obj_request *obj_request) |
bf0d5f50 | 1520 | { |
bf0d5f50 AE |
1521 | struct ceph_snap_context *snapc = NULL; |
1522 | struct ceph_osd_client *osdc; | |
1523 | struct ceph_osd_request *osd_req; | |
bf0d5f50 | 1524 | |
6365d33a AE |
1525 | if (obj_request_img_data_test(obj_request)) { |
1526 | struct rbd_img_request *img_request = obj_request->img_request; | |
1527 | ||
0c425248 AE |
1528 | rbd_assert(write_request == |
1529 | img_request_write_test(img_request)); | |
1530 | if (write_request) | |
bf0d5f50 | 1531 | snapc = img_request->snapc; |
bf0d5f50 AE |
1532 | } |
1533 | ||
1534 | /* Allocate and initialize the request, for the single op */ | |
1535 | ||
1536 | osdc = &rbd_dev->rbd_client->client->osdc; | |
1537 | osd_req = ceph_osdc_alloc_request(osdc, snapc, 1, false, GFP_ATOMIC); | |
1538 | if (!osd_req) | |
1539 | return NULL; /* ENOMEM */ | |
bf0d5f50 | 1540 | |
430c28c3 | 1541 | if (write_request) |
bf0d5f50 | 1542 | osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK; |
430c28c3 | 1543 | else |
bf0d5f50 | 1544 | osd_req->r_flags = CEPH_OSD_FLAG_READ; |
bf0d5f50 AE |
1545 | |
1546 | osd_req->r_callback = rbd_osd_req_callback; | |
1547 | osd_req->r_priv = obj_request; | |
1548 | ||
1549 | osd_req->r_oid_len = strlen(obj_request->object_name); | |
1550 | rbd_assert(osd_req->r_oid_len < sizeof (osd_req->r_oid)); | |
1551 | memcpy(osd_req->r_oid, obj_request->object_name, osd_req->r_oid_len); | |
1552 | ||
1553 | osd_req->r_file_layout = rbd_dev->layout; /* struct */ | |
1554 | ||
bf0d5f50 AE |
1555 | return osd_req; |
1556 | } | |
1557 | ||
1558 | static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req) | |
1559 | { | |
1560 | ceph_osdc_put_request(osd_req); | |
1561 | } | |
1562 | ||
1563 | /* object_name is assumed to be a non-null pointer and NUL-terminated */ | |
1564 | ||
1565 | static struct rbd_obj_request *rbd_obj_request_create(const char *object_name, | |
1566 | u64 offset, u64 length, | |
1567 | enum obj_request_type type) | |
1568 | { | |
1569 | struct rbd_obj_request *obj_request; | |
1570 | size_t size; | |
1571 | char *name; | |
1572 | ||
1573 | rbd_assert(obj_request_type_valid(type)); | |
1574 | ||
1575 | size = strlen(object_name) + 1; | |
1576 | obj_request = kzalloc(sizeof (*obj_request) + size, GFP_KERNEL); | |
1577 | if (!obj_request) | |
1578 | return NULL; | |
1579 | ||
1580 | name = (char *)(obj_request + 1); | |
1581 | obj_request->object_name = memcpy(name, object_name, size); | |
1582 | obj_request->offset = offset; | |
1583 | obj_request->length = length; | |
926f9b3f | 1584 | obj_request->flags = 0; |
bf0d5f50 AE |
1585 | obj_request->which = BAD_WHICH; |
1586 | obj_request->type = type; | |
1587 | INIT_LIST_HEAD(&obj_request->links); | |
788e2df3 | 1588 | init_completion(&obj_request->completion); |
bf0d5f50 AE |
1589 | kref_init(&obj_request->kref); |
1590 | ||
37206ee5 AE |
1591 | dout("%s: \"%s\" %llu/%llu %d -> obj %p\n", __func__, object_name, |
1592 | offset, length, (int)type, obj_request); | |
1593 | ||
bf0d5f50 AE |
1594 | return obj_request; |
1595 | } | |
1596 | ||
1597 | static void rbd_obj_request_destroy(struct kref *kref) | |
1598 | { | |
1599 | struct rbd_obj_request *obj_request; | |
1600 | ||
1601 | obj_request = container_of(kref, struct rbd_obj_request, kref); | |
1602 | ||
37206ee5 AE |
1603 | dout("%s: obj %p\n", __func__, obj_request); |
1604 | ||
bf0d5f50 AE |
1605 | rbd_assert(obj_request->img_request == NULL); |
1606 | rbd_assert(obj_request->which == BAD_WHICH); | |
1607 | ||
1608 | if (obj_request->osd_req) | |
1609 | rbd_osd_req_destroy(obj_request->osd_req); | |
1610 | ||
1611 | rbd_assert(obj_request_type_valid(obj_request->type)); | |
1612 | switch (obj_request->type) { | |
9969ebc5 AE |
1613 | case OBJ_REQUEST_NODATA: |
1614 | break; /* Nothing to do */ | |
bf0d5f50 AE |
1615 | case OBJ_REQUEST_BIO: |
1616 | if (obj_request->bio_list) | |
1617 | bio_chain_put(obj_request->bio_list); | |
1618 | break; | |
788e2df3 AE |
1619 | case OBJ_REQUEST_PAGES: |
1620 | if (obj_request->pages) | |
1621 | ceph_release_page_vector(obj_request->pages, | |
1622 | obj_request->page_count); | |
1623 | break; | |
bf0d5f50 AE |
1624 | } |
1625 | ||
1626 | kfree(obj_request); | |
1627 | } | |
1628 | ||
1629 | /* | |
1630 | * Caller is responsible for filling in the list of object requests | |
1631 | * that comprises the image request, and the Linux request pointer | |
1632 | * (if there is one). | |
1633 | */ | |
cc344fa1 AE |
1634 | static struct rbd_img_request *rbd_img_request_create( |
1635 | struct rbd_device *rbd_dev, | |
bf0d5f50 | 1636 | u64 offset, u64 length, |
9849e986 AE |
1637 | bool write_request, |
1638 | bool child_request) | |
bf0d5f50 AE |
1639 | { |
1640 | struct rbd_img_request *img_request; | |
1641 | struct ceph_snap_context *snapc = NULL; | |
1642 | ||
1643 | img_request = kmalloc(sizeof (*img_request), GFP_ATOMIC); | |
1644 | if (!img_request) | |
1645 | return NULL; | |
1646 | ||
1647 | if (write_request) { | |
1648 | down_read(&rbd_dev->header_rwsem); | |
1649 | snapc = ceph_get_snap_context(rbd_dev->header.snapc); | |
1650 | up_read(&rbd_dev->header_rwsem); | |
1651 | if (WARN_ON(!snapc)) { | |
1652 | kfree(img_request); | |
1653 | return NULL; /* Shouldn't happen */ | |
1654 | } | |
0c425248 | 1655 | |
bf0d5f50 AE |
1656 | } |
1657 | ||
1658 | img_request->rq = NULL; | |
1659 | img_request->rbd_dev = rbd_dev; | |
1660 | img_request->offset = offset; | |
1661 | img_request->length = length; | |
0c425248 AE |
1662 | img_request->flags = 0; |
1663 | if (write_request) { | |
1664 | img_request_write_set(img_request); | |
bf0d5f50 | 1665 | img_request->snapc = snapc; |
0c425248 | 1666 | } else { |
bf0d5f50 | 1667 | img_request->snap_id = rbd_dev->spec->snap_id; |
0c425248 | 1668 | } |
9849e986 AE |
1669 | if (child_request) |
1670 | img_request_child_set(img_request); | |
d0b2e944 AE |
1671 | if (rbd_dev->parent_spec) |
1672 | img_request_layered_set(img_request); | |
bf0d5f50 AE |
1673 | spin_lock_init(&img_request->completion_lock); |
1674 | img_request->next_completion = 0; | |
1675 | img_request->callback = NULL; | |
a5a337d4 | 1676 | img_request->result = 0; |
bf0d5f50 AE |
1677 | img_request->obj_request_count = 0; |
1678 | INIT_LIST_HEAD(&img_request->obj_requests); | |
1679 | kref_init(&img_request->kref); | |
1680 | ||
1681 | rbd_img_request_get(img_request); /* Avoid a warning */ | |
1682 | rbd_img_request_put(img_request); /* TEMPORARY */ | |
1683 | ||
37206ee5 AE |
1684 | dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev, |
1685 | write_request ? "write" : "read", offset, length, | |
1686 | img_request); | |
1687 | ||
bf0d5f50 AE |
1688 | return img_request; |
1689 | } | |
1690 | ||
1691 | static void rbd_img_request_destroy(struct kref *kref) | |
1692 | { | |
1693 | struct rbd_img_request *img_request; | |
1694 | struct rbd_obj_request *obj_request; | |
1695 | struct rbd_obj_request *next_obj_request; | |
1696 | ||
1697 | img_request = container_of(kref, struct rbd_img_request, kref); | |
1698 | ||
37206ee5 AE |
1699 | dout("%s: img %p\n", __func__, img_request); |
1700 | ||
bf0d5f50 AE |
1701 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
1702 | rbd_img_obj_request_del(img_request, obj_request); | |
25dcf954 | 1703 | rbd_assert(img_request->obj_request_count == 0); |
bf0d5f50 | 1704 | |
0c425248 | 1705 | if (img_request_write_test(img_request)) |
bf0d5f50 AE |
1706 | ceph_put_snap_context(img_request->snapc); |
1707 | ||
8b3e1a56 AE |
1708 | if (img_request_child_test(img_request)) |
1709 | rbd_obj_request_put(img_request->obj_request); | |
1710 | ||
bf0d5f50 AE |
1711 | kfree(img_request); |
1712 | } | |
1713 | ||
1217857f AE |
1714 | static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request) |
1715 | { | |
6365d33a | 1716 | struct rbd_img_request *img_request; |
1217857f AE |
1717 | unsigned int xferred; |
1718 | int result; | |
8b3e1a56 | 1719 | bool more; |
1217857f | 1720 | |
6365d33a AE |
1721 | rbd_assert(obj_request_img_data_test(obj_request)); |
1722 | img_request = obj_request->img_request; | |
1723 | ||
1217857f AE |
1724 | rbd_assert(obj_request->xferred <= (u64)UINT_MAX); |
1725 | xferred = (unsigned int)obj_request->xferred; | |
1726 | result = obj_request->result; | |
1727 | if (result) { | |
1728 | struct rbd_device *rbd_dev = img_request->rbd_dev; | |
1729 | ||
1730 | rbd_warn(rbd_dev, "%s %llx at %llx (%llx)\n", | |
1731 | img_request_write_test(img_request) ? "write" : "read", | |
1732 | obj_request->length, obj_request->img_offset, | |
1733 | obj_request->offset); | |
1734 | rbd_warn(rbd_dev, " result %d xferred %x\n", | |
1735 | result, xferred); | |
1736 | if (!img_request->result) | |
1737 | img_request->result = result; | |
1738 | } | |
1739 | ||
8b3e1a56 AE |
1740 | if (img_request_child_test(img_request)) { |
1741 | rbd_assert(img_request->obj_request != NULL); | |
1742 | more = obj_request->which < img_request->obj_request_count - 1; | |
1743 | } else { | |
1744 | rbd_assert(img_request->rq != NULL); | |
1745 | more = blk_end_request(img_request->rq, result, xferred); | |
1746 | } | |
1747 | ||
1748 | return more; | |
1217857f AE |
1749 | } |
1750 | ||
2169238d AE |
1751 | static void rbd_img_obj_callback(struct rbd_obj_request *obj_request) |
1752 | { | |
1753 | struct rbd_img_request *img_request; | |
1754 | u32 which = obj_request->which; | |
1755 | bool more = true; | |
1756 | ||
6365d33a | 1757 | rbd_assert(obj_request_img_data_test(obj_request)); |
2169238d AE |
1758 | img_request = obj_request->img_request; |
1759 | ||
1760 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); | |
1761 | rbd_assert(img_request != NULL); | |
2169238d AE |
1762 | rbd_assert(img_request->obj_request_count > 0); |
1763 | rbd_assert(which != BAD_WHICH); | |
1764 | rbd_assert(which < img_request->obj_request_count); | |
1765 | rbd_assert(which >= img_request->next_completion); | |
1766 | ||
1767 | spin_lock_irq(&img_request->completion_lock); | |
1768 | if (which != img_request->next_completion) | |
1769 | goto out; | |
1770 | ||
1771 | for_each_obj_request_from(img_request, obj_request) { | |
2169238d AE |
1772 | rbd_assert(more); |
1773 | rbd_assert(which < img_request->obj_request_count); | |
1774 | ||
1775 | if (!obj_request_done_test(obj_request)) | |
1776 | break; | |
1217857f | 1777 | more = rbd_img_obj_end_request(obj_request); |
2169238d AE |
1778 | which++; |
1779 | } | |
1780 | ||
1781 | rbd_assert(more ^ (which == img_request->obj_request_count)); | |
1782 | img_request->next_completion = which; | |
1783 | out: | |
1784 | spin_unlock_irq(&img_request->completion_lock); | |
1785 | ||
1786 | if (!more) | |
1787 | rbd_img_request_complete(img_request); | |
1788 | } | |
1789 | ||
bf0d5f50 AE |
1790 | static int rbd_img_request_fill_bio(struct rbd_img_request *img_request, |
1791 | struct bio *bio_list) | |
1792 | { | |
1793 | struct rbd_device *rbd_dev = img_request->rbd_dev; | |
1794 | struct rbd_obj_request *obj_request = NULL; | |
1795 | struct rbd_obj_request *next_obj_request; | |
0c425248 | 1796 | bool write_request = img_request_write_test(img_request); |
bf0d5f50 | 1797 | unsigned int bio_offset; |
7da22d29 | 1798 | u64 img_offset; |
bf0d5f50 AE |
1799 | u64 resid; |
1800 | u16 opcode; | |
1801 | ||
37206ee5 AE |
1802 | dout("%s: img %p bio %p\n", __func__, img_request, bio_list); |
1803 | ||
430c28c3 | 1804 | opcode = write_request ? CEPH_OSD_OP_WRITE : CEPH_OSD_OP_READ; |
bf0d5f50 | 1805 | bio_offset = 0; |
7da22d29 AE |
1806 | img_offset = img_request->offset; |
1807 | rbd_assert(img_offset == bio_list->bi_sector << SECTOR_SHIFT); | |
bf0d5f50 | 1808 | resid = img_request->length; |
4dda41d3 | 1809 | rbd_assert(resid > 0); |
bf0d5f50 | 1810 | while (resid) { |
2fa12320 | 1811 | struct ceph_osd_request *osd_req; |
bf0d5f50 AE |
1812 | const char *object_name; |
1813 | unsigned int clone_size; | |
bf0d5f50 AE |
1814 | u64 offset; |
1815 | u64 length; | |
1816 | ||
7da22d29 | 1817 | object_name = rbd_segment_name(rbd_dev, img_offset); |
bf0d5f50 AE |
1818 | if (!object_name) |
1819 | goto out_unwind; | |
7da22d29 AE |
1820 | offset = rbd_segment_offset(rbd_dev, img_offset); |
1821 | length = rbd_segment_length(rbd_dev, img_offset, resid); | |
bf0d5f50 AE |
1822 | obj_request = rbd_obj_request_create(object_name, |
1823 | offset, length, | |
1824 | OBJ_REQUEST_BIO); | |
1825 | kfree(object_name); /* object request has its own copy */ | |
1826 | if (!obj_request) | |
1827 | goto out_unwind; | |
1828 | ||
1829 | rbd_assert(length <= (u64) UINT_MAX); | |
1830 | clone_size = (unsigned int) length; | |
1831 | obj_request->bio_list = bio_chain_clone_range(&bio_list, | |
1832 | &bio_offset, clone_size, | |
1833 | GFP_ATOMIC); | |
1834 | if (!obj_request->bio_list) | |
1835 | goto out_partial; | |
1836 | ||
2fa12320 AE |
1837 | osd_req = rbd_osd_req_create(rbd_dev, write_request, |
1838 | obj_request); | |
1839 | if (!osd_req) | |
bf0d5f50 | 1840 | goto out_partial; |
2fa12320 | 1841 | obj_request->osd_req = osd_req; |
2169238d | 1842 | obj_request->callback = rbd_img_obj_callback; |
430c28c3 | 1843 | |
2fa12320 AE |
1844 | osd_req_op_extent_init(osd_req, 0, opcode, offset, length, |
1845 | 0, 0); | |
406e2c9f | 1846 | osd_req_op_extent_osd_data_bio(osd_req, 0, |
a4ce40a9 | 1847 | obj_request->bio_list, obj_request->length); |
2fa12320 | 1848 | rbd_osd_req_format(obj_request, write_request); |
430c28c3 | 1849 | |
7da22d29 | 1850 | obj_request->img_offset = img_offset; |
bf0d5f50 AE |
1851 | rbd_img_obj_request_add(img_request, obj_request); |
1852 | ||
7da22d29 | 1853 | img_offset += length; |
bf0d5f50 AE |
1854 | resid -= length; |
1855 | } | |
1856 | ||
1857 | return 0; | |
1858 | ||
1859 | out_partial: | |
1860 | rbd_obj_request_put(obj_request); | |
1861 | out_unwind: | |
1862 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) | |
1863 | rbd_obj_request_put(obj_request); | |
1864 | ||
1865 | return -ENOMEM; | |
1866 | } | |
1867 | ||
c5b5ef6c AE |
1868 | static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request) |
1869 | { | |
1870 | struct rbd_device *rbd_dev; | |
1871 | struct ceph_osd_client *osdc; | |
1872 | struct rbd_obj_request *orig_request; | |
1873 | int result; | |
1874 | ||
1875 | rbd_assert(!obj_request_img_data_test(obj_request)); | |
1876 | ||
1877 | /* | |
1878 | * All we need from the object request is the original | |
1879 | * request and the result of the STAT op. Grab those, then | |
1880 | * we're done with the request. | |
1881 | */ | |
1882 | orig_request = obj_request->obj_request; | |
1883 | obj_request->obj_request = NULL; | |
1884 | rbd_assert(orig_request); | |
1885 | rbd_assert(orig_request->img_request); | |
1886 | ||
1887 | result = obj_request->result; | |
1888 | obj_request->result = 0; | |
1889 | ||
1890 | dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__, | |
1891 | obj_request, orig_request, result, | |
1892 | obj_request->xferred, obj_request->length); | |
1893 | rbd_obj_request_put(obj_request); | |
1894 | ||
1895 | rbd_assert(orig_request); | |
1896 | rbd_assert(orig_request->img_request); | |
1897 | rbd_dev = orig_request->img_request->rbd_dev; | |
1898 | osdc = &rbd_dev->rbd_client->client->osdc; | |
1899 | ||
1900 | /* | |
1901 | * Our only purpose here is to determine whether the object | |
1902 | * exists, and we don't want to treat the non-existence as | |
1903 | * an error. If something else comes back, transfer the | |
1904 | * error to the original request and complete it now. | |
1905 | */ | |
1906 | if (!result) { | |
1907 | obj_request_existence_set(orig_request, true); | |
1908 | } else if (result == -ENOENT) { | |
1909 | obj_request_existence_set(orig_request, false); | |
1910 | } else if (result) { | |
1911 | orig_request->result = result; | |
1912 | goto out_err; | |
1913 | } | |
1914 | ||
1915 | /* | |
1916 | * Resubmit the original request now that we have recorded | |
1917 | * whether the target object exists. | |
1918 | */ | |
1919 | orig_request->result = rbd_obj_request_submit(osdc, orig_request); | |
1920 | out_err: | |
1921 | if (orig_request->result) | |
1922 | rbd_obj_request_complete(orig_request); | |
1923 | rbd_obj_request_put(orig_request); | |
1924 | } | |
1925 | ||
1926 | static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request) | |
1927 | { | |
1928 | struct rbd_obj_request *stat_request; | |
1929 | struct rbd_device *rbd_dev; | |
1930 | struct ceph_osd_client *osdc; | |
1931 | struct page **pages = NULL; | |
1932 | u32 page_count; | |
1933 | size_t size; | |
1934 | int ret; | |
1935 | ||
1936 | /* | |
1937 | * The response data for a STAT call consists of: | |
1938 | * le64 length; | |
1939 | * struct { | |
1940 | * le32 tv_sec; | |
1941 | * le32 tv_nsec; | |
1942 | * } mtime; | |
1943 | */ | |
1944 | size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32); | |
1945 | page_count = (u32)calc_pages_for(0, size); | |
1946 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); | |
1947 | if (IS_ERR(pages)) | |
1948 | return PTR_ERR(pages); | |
1949 | ||
1950 | ret = -ENOMEM; | |
1951 | stat_request = rbd_obj_request_create(obj_request->object_name, 0, 0, | |
1952 | OBJ_REQUEST_PAGES); | |
1953 | if (!stat_request) | |
1954 | goto out; | |
1955 | ||
1956 | rbd_obj_request_get(obj_request); | |
1957 | stat_request->obj_request = obj_request; | |
1958 | stat_request->pages = pages; | |
1959 | stat_request->page_count = page_count; | |
1960 | ||
1961 | rbd_assert(obj_request->img_request); | |
1962 | rbd_dev = obj_request->img_request->rbd_dev; | |
1963 | stat_request->osd_req = rbd_osd_req_create(rbd_dev, false, | |
1964 | stat_request); | |
1965 | if (!stat_request->osd_req) | |
1966 | goto out; | |
1967 | stat_request->callback = rbd_img_obj_exists_callback; | |
1968 | ||
1969 | osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT); | |
1970 | osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0, | |
1971 | false, false); | |
1972 | rbd_osd_req_format(stat_request, false); | |
1973 | ||
1974 | osdc = &rbd_dev->rbd_client->client->osdc; | |
1975 | ret = rbd_obj_request_submit(osdc, stat_request); | |
1976 | out: | |
1977 | if (ret) | |
1978 | rbd_obj_request_put(obj_request); | |
1979 | ||
1980 | return ret; | |
1981 | } | |
1982 | ||
bf0d5f50 AE |
1983 | static int rbd_img_request_submit(struct rbd_img_request *img_request) |
1984 | { | |
1985 | struct rbd_device *rbd_dev = img_request->rbd_dev; | |
1986 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; | |
1987 | struct rbd_obj_request *obj_request; | |
46faeed4 | 1988 | struct rbd_obj_request *next_obj_request; |
c5b5ef6c AE |
1989 | bool write_request = img_request_write_test(img_request); |
1990 | bool layered = img_request_layered_test(img_request); | |
bf0d5f50 | 1991 | |
37206ee5 | 1992 | dout("%s: img %p\n", __func__, img_request); |
46faeed4 | 1993 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) { |
c5b5ef6c AE |
1994 | bool known; |
1995 | bool object_exists; | |
bf0d5f50 AE |
1996 | int ret; |
1997 | ||
c5b5ef6c AE |
1998 | /* |
1999 | * We need to know whether the target object exists | |
2000 | * for a layered write. Issue an existence check | |
2001 | * first if we need to. | |
2002 | */ | |
2003 | known = obj_request_known_test(obj_request); | |
2004 | object_exists = known && obj_request_exists_test(obj_request); | |
2005 | if (!write_request || !layered || object_exists) | |
2006 | ret = rbd_obj_request_submit(osdc, obj_request); | |
2007 | else | |
2008 | ret = rbd_img_obj_exists_submit(obj_request); | |
bf0d5f50 AE |
2009 | if (ret) |
2010 | return ret; | |
bf0d5f50 AE |
2011 | } |
2012 | ||
2013 | return 0; | |
2014 | } | |
8b3e1a56 AE |
2015 | |
2016 | static void rbd_img_parent_read_callback(struct rbd_img_request *img_request) | |
2017 | { | |
2018 | struct rbd_obj_request *obj_request; | |
2019 | ||
2020 | rbd_assert(img_request_child_test(img_request)); | |
2021 | ||
2022 | obj_request = img_request->obj_request; | |
2023 | rbd_assert(obj_request != NULL); | |
2024 | obj_request->result = img_request->result; | |
2025 | obj_request->xferred = img_request->xferred; | |
2026 | ||
2027 | rbd_img_obj_request_read_callback(obj_request); | |
2028 | rbd_obj_request_complete(obj_request); | |
2029 | } | |
2030 | ||
2031 | static void rbd_img_parent_read(struct rbd_obj_request *obj_request) | |
2032 | { | |
2033 | struct rbd_device *rbd_dev; | |
2034 | struct rbd_img_request *img_request; | |
2035 | int result; | |
2036 | ||
2037 | rbd_assert(obj_request_img_data_test(obj_request)); | |
2038 | rbd_assert(obj_request->img_request != NULL); | |
2039 | rbd_assert(obj_request->result == (s32) -ENOENT); | |
2040 | rbd_assert(obj_request->type == OBJ_REQUEST_BIO); | |
2041 | ||
2042 | rbd_dev = obj_request->img_request->rbd_dev; | |
2043 | rbd_assert(rbd_dev->parent != NULL); | |
2044 | /* rbd_read_finish(obj_request, obj_request->length); */ | |
2045 | img_request = rbd_img_request_create(rbd_dev->parent, | |
2046 | obj_request->img_offset, | |
2047 | obj_request->length, | |
2048 | false, true); | |
2049 | result = -ENOMEM; | |
2050 | if (!img_request) | |
2051 | goto out_err; | |
2052 | ||
2053 | rbd_obj_request_get(obj_request); | |
2054 | img_request->obj_request = obj_request; | |
2055 | ||
2056 | result = rbd_img_request_fill_bio(img_request, obj_request->bio_list); | |
2057 | if (result) | |
2058 | goto out_err; | |
2059 | ||
2060 | img_request->callback = rbd_img_parent_read_callback; | |
2061 | result = rbd_img_request_submit(img_request); | |
2062 | if (result) | |
2063 | goto out_err; | |
2064 | ||
2065 | return; | |
2066 | out_err: | |
2067 | if (img_request) | |
2068 | rbd_img_request_put(img_request); | |
2069 | obj_request->result = result; | |
2070 | obj_request->xferred = 0; | |
2071 | obj_request_done_set(obj_request); | |
2072 | } | |
bf0d5f50 | 2073 | |
cf81b60e | 2074 | static int rbd_obj_notify_ack(struct rbd_device *rbd_dev, |
b8d70035 AE |
2075 | u64 ver, u64 notify_id) |
2076 | { | |
2077 | struct rbd_obj_request *obj_request; | |
2169238d | 2078 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
b8d70035 AE |
2079 | int ret; |
2080 | ||
2081 | obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0, | |
2082 | OBJ_REQUEST_NODATA); | |
2083 | if (!obj_request) | |
2084 | return -ENOMEM; | |
2085 | ||
2086 | ret = -ENOMEM; | |
430c28c3 | 2087 | obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request); |
b8d70035 AE |
2088 | if (!obj_request->osd_req) |
2089 | goto out; | |
2169238d | 2090 | obj_request->callback = rbd_obj_request_put; |
b8d70035 | 2091 | |
c99d2d4a AE |
2092 | osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_NOTIFY_ACK, |
2093 | notify_id, ver, 0); | |
2fa12320 | 2094 | rbd_osd_req_format(obj_request, false); |
430c28c3 | 2095 | |
b8d70035 | 2096 | ret = rbd_obj_request_submit(osdc, obj_request); |
b8d70035 | 2097 | out: |
cf81b60e AE |
2098 | if (ret) |
2099 | rbd_obj_request_put(obj_request); | |
b8d70035 AE |
2100 | |
2101 | return ret; | |
2102 | } | |
2103 | ||
2104 | static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data) | |
2105 | { | |
2106 | struct rbd_device *rbd_dev = (struct rbd_device *)data; | |
2107 | u64 hver; | |
2108 | int rc; | |
2109 | ||
2110 | if (!rbd_dev) | |
2111 | return; | |
2112 | ||
37206ee5 | 2113 | dout("%s: \"%s\" notify_id %llu opcode %u\n", __func__, |
b8d70035 AE |
2114 | rbd_dev->header_name, (unsigned long long) notify_id, |
2115 | (unsigned int) opcode); | |
2116 | rc = rbd_dev_refresh(rbd_dev, &hver); | |
2117 | if (rc) | |
2118 | rbd_warn(rbd_dev, "got notification but failed to " | |
2119 | " update snaps: %d\n", rc); | |
2120 | ||
cf81b60e | 2121 | rbd_obj_notify_ack(rbd_dev, hver, notify_id); |
b8d70035 AE |
2122 | } |
2123 | ||
9969ebc5 AE |
2124 | /* |
2125 | * Request sync osd watch/unwatch. The value of "start" determines | |
2126 | * whether a watch request is being initiated or torn down. | |
2127 | */ | |
2128 | static int rbd_dev_header_watch_sync(struct rbd_device *rbd_dev, int start) | |
2129 | { | |
2130 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; | |
2131 | struct rbd_obj_request *obj_request; | |
9969ebc5 AE |
2132 | int ret; |
2133 | ||
2134 | rbd_assert(start ^ !!rbd_dev->watch_event); | |
2135 | rbd_assert(start ^ !!rbd_dev->watch_request); | |
2136 | ||
2137 | if (start) { | |
3c663bbd | 2138 | ret = ceph_osdc_create_event(osdc, rbd_watch_cb, rbd_dev, |
9969ebc5 AE |
2139 | &rbd_dev->watch_event); |
2140 | if (ret < 0) | |
2141 | return ret; | |
8eb87565 | 2142 | rbd_assert(rbd_dev->watch_event != NULL); |
9969ebc5 AE |
2143 | } |
2144 | ||
2145 | ret = -ENOMEM; | |
2146 | obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0, | |
2147 | OBJ_REQUEST_NODATA); | |
2148 | if (!obj_request) | |
2149 | goto out_cancel; | |
2150 | ||
430c28c3 AE |
2151 | obj_request->osd_req = rbd_osd_req_create(rbd_dev, true, obj_request); |
2152 | if (!obj_request->osd_req) | |
2153 | goto out_cancel; | |
2154 | ||
8eb87565 | 2155 | if (start) |
975241af | 2156 | ceph_osdc_set_request_linger(osdc, obj_request->osd_req); |
8eb87565 | 2157 | else |
6977c3f9 | 2158 | ceph_osdc_unregister_linger_request(osdc, |
975241af | 2159 | rbd_dev->watch_request->osd_req); |
2169238d AE |
2160 | |
2161 | osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_WATCH, | |
2162 | rbd_dev->watch_event->cookie, | |
2163 | rbd_dev->header.obj_version, start); | |
2164 | rbd_osd_req_format(obj_request, true); | |
2165 | ||
9969ebc5 AE |
2166 | ret = rbd_obj_request_submit(osdc, obj_request); |
2167 | if (ret) | |
2168 | goto out_cancel; | |
2169 | ret = rbd_obj_request_wait(obj_request); | |
2170 | if (ret) | |
2171 | goto out_cancel; | |
9969ebc5 AE |
2172 | ret = obj_request->result; |
2173 | if (ret) | |
2174 | goto out_cancel; | |
2175 | ||
8eb87565 AE |
2176 | /* |
2177 | * A watch request is set to linger, so the underlying osd | |
2178 | * request won't go away until we unregister it. We retain | |
2179 | * a pointer to the object request during that time (in | |
2180 | * rbd_dev->watch_request), so we'll keep a reference to | |
2181 | * it. We'll drop that reference (below) after we've | |
2182 | * unregistered it. | |
2183 | */ | |
2184 | if (start) { | |
2185 | rbd_dev->watch_request = obj_request; | |
2186 | ||
2187 | return 0; | |
2188 | } | |
2189 | ||
2190 | /* We have successfully torn down the watch request */ | |
2191 | ||
2192 | rbd_obj_request_put(rbd_dev->watch_request); | |
2193 | rbd_dev->watch_request = NULL; | |
9969ebc5 AE |
2194 | out_cancel: |
2195 | /* Cancel the event if we're tearing down, or on error */ | |
2196 | ceph_osdc_cancel_event(rbd_dev->watch_event); | |
2197 | rbd_dev->watch_event = NULL; | |
9969ebc5 AE |
2198 | if (obj_request) |
2199 | rbd_obj_request_put(obj_request); | |
2200 | ||
2201 | return ret; | |
2202 | } | |
2203 | ||
36be9a76 AE |
2204 | /* |
2205 | * Synchronous osd object method call | |
2206 | */ | |
2207 | static int rbd_obj_method_sync(struct rbd_device *rbd_dev, | |
2208 | const char *object_name, | |
2209 | const char *class_name, | |
2210 | const char *method_name, | |
2211 | const char *outbound, | |
2212 | size_t outbound_size, | |
2213 | char *inbound, | |
2214 | size_t inbound_size, | |
2215 | u64 *version) | |
2216 | { | |
2169238d | 2217 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
36be9a76 | 2218 | struct rbd_obj_request *obj_request; |
36be9a76 AE |
2219 | struct page **pages; |
2220 | u32 page_count; | |
2221 | int ret; | |
2222 | ||
2223 | /* | |
6010a451 AE |
2224 | * Method calls are ultimately read operations. The result |
2225 | * should placed into the inbound buffer provided. They | |
2226 | * also supply outbound data--parameters for the object | |
2227 | * method. Currently if this is present it will be a | |
2228 | * snapshot id. | |
36be9a76 AE |
2229 | */ |
2230 | page_count = (u32) calc_pages_for(0, inbound_size); | |
2231 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); | |
2232 | if (IS_ERR(pages)) | |
2233 | return PTR_ERR(pages); | |
2234 | ||
2235 | ret = -ENOMEM; | |
6010a451 | 2236 | obj_request = rbd_obj_request_create(object_name, 0, inbound_size, |
36be9a76 AE |
2237 | OBJ_REQUEST_PAGES); |
2238 | if (!obj_request) | |
2239 | goto out; | |
2240 | ||
2241 | obj_request->pages = pages; | |
2242 | obj_request->page_count = page_count; | |
2243 | ||
430c28c3 | 2244 | obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request); |
36be9a76 AE |
2245 | if (!obj_request->osd_req) |
2246 | goto out; | |
2247 | ||
c99d2d4a | 2248 | osd_req_op_cls_init(obj_request->osd_req, 0, CEPH_OSD_OP_CALL, |
04017e29 AE |
2249 | class_name, method_name); |
2250 | if (outbound_size) { | |
2251 | struct ceph_pagelist *pagelist; | |
2252 | ||
2253 | pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS); | |
2254 | if (!pagelist) | |
2255 | goto out; | |
2256 | ||
2257 | ceph_pagelist_init(pagelist); | |
2258 | ceph_pagelist_append(pagelist, outbound, outbound_size); | |
2259 | osd_req_op_cls_request_data_pagelist(obj_request->osd_req, 0, | |
2260 | pagelist); | |
2261 | } | |
a4ce40a9 AE |
2262 | osd_req_op_cls_response_data_pages(obj_request->osd_req, 0, |
2263 | obj_request->pages, inbound_size, | |
44cd188d | 2264 | 0, false, false); |
2fa12320 | 2265 | rbd_osd_req_format(obj_request, false); |
430c28c3 | 2266 | |
36be9a76 AE |
2267 | ret = rbd_obj_request_submit(osdc, obj_request); |
2268 | if (ret) | |
2269 | goto out; | |
2270 | ret = rbd_obj_request_wait(obj_request); | |
2271 | if (ret) | |
2272 | goto out; | |
2273 | ||
2274 | ret = obj_request->result; | |
2275 | if (ret < 0) | |
2276 | goto out; | |
23ed6e13 | 2277 | ret = 0; |
903bb32e | 2278 | ceph_copy_from_page_vector(pages, inbound, 0, obj_request->xferred); |
36be9a76 AE |
2279 | if (version) |
2280 | *version = obj_request->version; | |
2281 | out: | |
2282 | if (obj_request) | |
2283 | rbd_obj_request_put(obj_request); | |
2284 | else | |
2285 | ceph_release_page_vector(pages, page_count); | |
2286 | ||
2287 | return ret; | |
2288 | } | |
2289 | ||
bf0d5f50 | 2290 | static void rbd_request_fn(struct request_queue *q) |
cc344fa1 | 2291 | __releases(q->queue_lock) __acquires(q->queue_lock) |
bf0d5f50 AE |
2292 | { |
2293 | struct rbd_device *rbd_dev = q->queuedata; | |
2294 | bool read_only = rbd_dev->mapping.read_only; | |
2295 | struct request *rq; | |
2296 | int result; | |
2297 | ||
2298 | while ((rq = blk_fetch_request(q))) { | |
2299 | bool write_request = rq_data_dir(rq) == WRITE; | |
2300 | struct rbd_img_request *img_request; | |
2301 | u64 offset; | |
2302 | u64 length; | |
2303 | ||
2304 | /* Ignore any non-FS requests that filter through. */ | |
2305 | ||
2306 | if (rq->cmd_type != REQ_TYPE_FS) { | |
4dda41d3 AE |
2307 | dout("%s: non-fs request type %d\n", __func__, |
2308 | (int) rq->cmd_type); | |
2309 | __blk_end_request_all(rq, 0); | |
2310 | continue; | |
2311 | } | |
2312 | ||
2313 | /* Ignore/skip any zero-length requests */ | |
2314 | ||
2315 | offset = (u64) blk_rq_pos(rq) << SECTOR_SHIFT; | |
2316 | length = (u64) blk_rq_bytes(rq); | |
2317 | ||
2318 | if (!length) { | |
2319 | dout("%s: zero-length request\n", __func__); | |
bf0d5f50 AE |
2320 | __blk_end_request_all(rq, 0); |
2321 | continue; | |
2322 | } | |
2323 | ||
2324 | spin_unlock_irq(q->queue_lock); | |
2325 | ||
2326 | /* Disallow writes to a read-only device */ | |
2327 | ||
2328 | if (write_request) { | |
2329 | result = -EROFS; | |
2330 | if (read_only) | |
2331 | goto end_request; | |
2332 | rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP); | |
2333 | } | |
2334 | ||
6d292906 AE |
2335 | /* |
2336 | * Quit early if the mapped snapshot no longer | |
2337 | * exists. It's still possible the snapshot will | |
2338 | * have disappeared by the time our request arrives | |
2339 | * at the osd, but there's no sense in sending it if | |
2340 | * we already know. | |
2341 | */ | |
2342 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) { | |
bf0d5f50 AE |
2343 | dout("request for non-existent snapshot"); |
2344 | rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP); | |
2345 | result = -ENXIO; | |
2346 | goto end_request; | |
2347 | } | |
2348 | ||
bf0d5f50 AE |
2349 | result = -EINVAL; |
2350 | if (WARN_ON(offset && length > U64_MAX - offset + 1)) | |
2351 | goto end_request; /* Shouldn't happen */ | |
2352 | ||
2353 | result = -ENOMEM; | |
2354 | img_request = rbd_img_request_create(rbd_dev, offset, length, | |
9849e986 | 2355 | write_request, false); |
bf0d5f50 AE |
2356 | if (!img_request) |
2357 | goto end_request; | |
2358 | ||
2359 | img_request->rq = rq; | |
2360 | ||
2361 | result = rbd_img_request_fill_bio(img_request, rq->bio); | |
2362 | if (!result) | |
2363 | result = rbd_img_request_submit(img_request); | |
2364 | if (result) | |
2365 | rbd_img_request_put(img_request); | |
2366 | end_request: | |
2367 | spin_lock_irq(q->queue_lock); | |
2368 | if (result < 0) { | |
7da22d29 AE |
2369 | rbd_warn(rbd_dev, "%s %llx at %llx result %d\n", |
2370 | write_request ? "write" : "read", | |
2371 | length, offset, result); | |
2372 | ||
bf0d5f50 AE |
2373 | __blk_end_request_all(rq, result); |
2374 | } | |
2375 | } | |
2376 | } | |
2377 | ||
602adf40 YS |
2378 | /* |
2379 | * a queue callback. Makes sure that we don't create a bio that spans across | |
2380 | * multiple osd objects. One exception would be with a single page bios, | |
f7760dad | 2381 | * which we handle later at bio_chain_clone_range() |
602adf40 YS |
2382 | */ |
2383 | static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd, | |
2384 | struct bio_vec *bvec) | |
2385 | { | |
2386 | struct rbd_device *rbd_dev = q->queuedata; | |
e5cfeed2 AE |
2387 | sector_t sector_offset; |
2388 | sector_t sectors_per_obj; | |
2389 | sector_t obj_sector_offset; | |
2390 | int ret; | |
2391 | ||
2392 | /* | |
2393 | * Find how far into its rbd object the partition-relative | |
2394 | * bio start sector is to offset relative to the enclosing | |
2395 | * device. | |
2396 | */ | |
2397 | sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector; | |
2398 | sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT); | |
2399 | obj_sector_offset = sector_offset & (sectors_per_obj - 1); | |
2400 | ||
2401 | /* | |
2402 | * Compute the number of bytes from that offset to the end | |
2403 | * of the object. Account for what's already used by the bio. | |
2404 | */ | |
2405 | ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT; | |
2406 | if (ret > bmd->bi_size) | |
2407 | ret -= bmd->bi_size; | |
2408 | else | |
2409 | ret = 0; | |
2410 | ||
2411 | /* | |
2412 | * Don't send back more than was asked for. And if the bio | |
2413 | * was empty, let the whole thing through because: "Note | |
2414 | * that a block device *must* allow a single page to be | |
2415 | * added to an empty bio." | |
2416 | */ | |
2417 | rbd_assert(bvec->bv_len <= PAGE_SIZE); | |
2418 | if (ret > (int) bvec->bv_len || !bmd->bi_size) | |
2419 | ret = (int) bvec->bv_len; | |
2420 | ||
2421 | return ret; | |
602adf40 YS |
2422 | } |
2423 | ||
2424 | static void rbd_free_disk(struct rbd_device *rbd_dev) | |
2425 | { | |
2426 | struct gendisk *disk = rbd_dev->disk; | |
2427 | ||
2428 | if (!disk) | |
2429 | return; | |
2430 | ||
602adf40 YS |
2431 | if (disk->flags & GENHD_FL_UP) |
2432 | del_gendisk(disk); | |
2433 | if (disk->queue) | |
2434 | blk_cleanup_queue(disk->queue); | |
2435 | put_disk(disk); | |
2436 | } | |
2437 | ||
788e2df3 AE |
2438 | static int rbd_obj_read_sync(struct rbd_device *rbd_dev, |
2439 | const char *object_name, | |
2440 | u64 offset, u64 length, | |
2441 | char *buf, u64 *version) | |
2442 | ||
2443 | { | |
2169238d | 2444 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
788e2df3 | 2445 | struct rbd_obj_request *obj_request; |
788e2df3 AE |
2446 | struct page **pages = NULL; |
2447 | u32 page_count; | |
1ceae7ef | 2448 | size_t size; |
788e2df3 AE |
2449 | int ret; |
2450 | ||
2451 | page_count = (u32) calc_pages_for(offset, length); | |
2452 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); | |
2453 | if (IS_ERR(pages)) | |
2454 | ret = PTR_ERR(pages); | |
2455 | ||
2456 | ret = -ENOMEM; | |
2457 | obj_request = rbd_obj_request_create(object_name, offset, length, | |
36be9a76 | 2458 | OBJ_REQUEST_PAGES); |
788e2df3 AE |
2459 | if (!obj_request) |
2460 | goto out; | |
2461 | ||
2462 | obj_request->pages = pages; | |
2463 | obj_request->page_count = page_count; | |
2464 | ||
430c28c3 | 2465 | obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request); |
788e2df3 AE |
2466 | if (!obj_request->osd_req) |
2467 | goto out; | |
2468 | ||
c99d2d4a AE |
2469 | osd_req_op_extent_init(obj_request->osd_req, 0, CEPH_OSD_OP_READ, |
2470 | offset, length, 0, 0); | |
406e2c9f | 2471 | osd_req_op_extent_osd_data_pages(obj_request->osd_req, 0, |
a4ce40a9 | 2472 | obj_request->pages, |
44cd188d AE |
2473 | obj_request->length, |
2474 | obj_request->offset & ~PAGE_MASK, | |
2475 | false, false); | |
2fa12320 | 2476 | rbd_osd_req_format(obj_request, false); |
430c28c3 | 2477 | |
788e2df3 AE |
2478 | ret = rbd_obj_request_submit(osdc, obj_request); |
2479 | if (ret) | |
2480 | goto out; | |
2481 | ret = rbd_obj_request_wait(obj_request); | |
2482 | if (ret) | |
2483 | goto out; | |
2484 | ||
2485 | ret = obj_request->result; | |
2486 | if (ret < 0) | |
2487 | goto out; | |
1ceae7ef AE |
2488 | |
2489 | rbd_assert(obj_request->xferred <= (u64) SIZE_MAX); | |
2490 | size = (size_t) obj_request->xferred; | |
903bb32e | 2491 | ceph_copy_from_page_vector(pages, buf, 0, size); |
23ed6e13 AE |
2492 | rbd_assert(size <= (size_t) INT_MAX); |
2493 | ret = (int) size; | |
788e2df3 AE |
2494 | if (version) |
2495 | *version = obj_request->version; | |
2496 | out: | |
2497 | if (obj_request) | |
2498 | rbd_obj_request_put(obj_request); | |
2499 | else | |
2500 | ceph_release_page_vector(pages, page_count); | |
2501 | ||
2502 | return ret; | |
2503 | } | |
2504 | ||
602adf40 | 2505 | /* |
4156d998 AE |
2506 | * Read the complete header for the given rbd device. |
2507 | * | |
2508 | * Returns a pointer to a dynamically-allocated buffer containing | |
2509 | * the complete and validated header. Caller can pass the address | |
2510 | * of a variable that will be filled in with the version of the | |
2511 | * header object at the time it was read. | |
2512 | * | |
2513 | * Returns a pointer-coded errno if a failure occurs. | |
602adf40 | 2514 | */ |
4156d998 AE |
2515 | static struct rbd_image_header_ondisk * |
2516 | rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version) | |
602adf40 | 2517 | { |
4156d998 | 2518 | struct rbd_image_header_ondisk *ondisk = NULL; |
50f7c4c9 | 2519 | u32 snap_count = 0; |
4156d998 AE |
2520 | u64 names_size = 0; |
2521 | u32 want_count; | |
2522 | int ret; | |
602adf40 | 2523 | |
00f1f36f | 2524 | /* |
4156d998 AE |
2525 | * The complete header will include an array of its 64-bit |
2526 | * snapshot ids, followed by the names of those snapshots as | |
2527 | * a contiguous block of NUL-terminated strings. Note that | |
2528 | * the number of snapshots could change by the time we read | |
2529 | * it in, in which case we re-read it. | |
00f1f36f | 2530 | */ |
4156d998 AE |
2531 | do { |
2532 | size_t size; | |
2533 | ||
2534 | kfree(ondisk); | |
2535 | ||
2536 | size = sizeof (*ondisk); | |
2537 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); | |
2538 | size += names_size; | |
2539 | ondisk = kmalloc(size, GFP_KERNEL); | |
2540 | if (!ondisk) | |
2541 | return ERR_PTR(-ENOMEM); | |
2542 | ||
788e2df3 | 2543 | ret = rbd_obj_read_sync(rbd_dev, rbd_dev->header_name, |
4156d998 AE |
2544 | 0, size, |
2545 | (char *) ondisk, version); | |
4156d998 AE |
2546 | if (ret < 0) |
2547 | goto out_err; | |
2548 | if (WARN_ON((size_t) ret < size)) { | |
2549 | ret = -ENXIO; | |
06ecc6cb AE |
2550 | rbd_warn(rbd_dev, "short header read (want %zd got %d)", |
2551 | size, ret); | |
4156d998 AE |
2552 | goto out_err; |
2553 | } | |
2554 | if (!rbd_dev_ondisk_valid(ondisk)) { | |
2555 | ret = -ENXIO; | |
06ecc6cb | 2556 | rbd_warn(rbd_dev, "invalid header"); |
4156d998 | 2557 | goto out_err; |
81e759fb | 2558 | } |
602adf40 | 2559 | |
4156d998 AE |
2560 | names_size = le64_to_cpu(ondisk->snap_names_len); |
2561 | want_count = snap_count; | |
2562 | snap_count = le32_to_cpu(ondisk->snap_count); | |
2563 | } while (snap_count != want_count); | |
00f1f36f | 2564 | |
4156d998 | 2565 | return ondisk; |
00f1f36f | 2566 | |
4156d998 AE |
2567 | out_err: |
2568 | kfree(ondisk); | |
2569 | ||
2570 | return ERR_PTR(ret); | |
2571 | } | |
2572 | ||
2573 | /* | |
2574 | * reload the ondisk the header | |
2575 | */ | |
2576 | static int rbd_read_header(struct rbd_device *rbd_dev, | |
2577 | struct rbd_image_header *header) | |
2578 | { | |
2579 | struct rbd_image_header_ondisk *ondisk; | |
2580 | u64 ver = 0; | |
2581 | int ret; | |
602adf40 | 2582 | |
4156d998 AE |
2583 | ondisk = rbd_dev_v1_header_read(rbd_dev, &ver); |
2584 | if (IS_ERR(ondisk)) | |
2585 | return PTR_ERR(ondisk); | |
2586 | ret = rbd_header_from_disk(header, ondisk); | |
2587 | if (ret >= 0) | |
2588 | header->obj_version = ver; | |
2589 | kfree(ondisk); | |
2590 | ||
2591 | return ret; | |
602adf40 YS |
2592 | } |
2593 | ||
41f38c2b | 2594 | static void rbd_remove_all_snaps(struct rbd_device *rbd_dev) |
dfc5606d YS |
2595 | { |
2596 | struct rbd_snap *snap; | |
a0593290 | 2597 | struct rbd_snap *next; |
dfc5606d | 2598 | |
a0593290 | 2599 | list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node) |
41f38c2b | 2600 | rbd_remove_snap_dev(snap); |
dfc5606d YS |
2601 | } |
2602 | ||
9478554a AE |
2603 | static void rbd_update_mapping_size(struct rbd_device *rbd_dev) |
2604 | { | |
2605 | sector_t size; | |
2606 | ||
0d7dbfce | 2607 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP) |
9478554a AE |
2608 | return; |
2609 | ||
2610 | size = (sector_t) rbd_dev->header.image_size / SECTOR_SIZE; | |
2611 | dout("setting size to %llu sectors", (unsigned long long) size); | |
2612 | rbd_dev->mapping.size = (u64) size; | |
2613 | set_capacity(rbd_dev->disk, size); | |
2614 | } | |
2615 | ||
602adf40 YS |
2616 | /* |
2617 | * only read the first part of the ondisk header, without the snaps info | |
2618 | */ | |
117973fb | 2619 | static int rbd_dev_v1_refresh(struct rbd_device *rbd_dev, u64 *hver) |
602adf40 YS |
2620 | { |
2621 | int ret; | |
2622 | struct rbd_image_header h; | |
602adf40 YS |
2623 | |
2624 | ret = rbd_read_header(rbd_dev, &h); | |
2625 | if (ret < 0) | |
2626 | return ret; | |
2627 | ||
a51aa0c0 JD |
2628 | down_write(&rbd_dev->header_rwsem); |
2629 | ||
9478554a AE |
2630 | /* Update image size, and check for resize of mapped image */ |
2631 | rbd_dev->header.image_size = h.image_size; | |
2632 | rbd_update_mapping_size(rbd_dev); | |
9db4b3e3 | 2633 | |
849b4260 | 2634 | /* rbd_dev->header.object_prefix shouldn't change */ |
602adf40 | 2635 | kfree(rbd_dev->header.snap_sizes); |
849b4260 | 2636 | kfree(rbd_dev->header.snap_names); |
d1d25646 JD |
2637 | /* osd requests may still refer to snapc */ |
2638 | ceph_put_snap_context(rbd_dev->header.snapc); | |
602adf40 | 2639 | |
b813623a AE |
2640 | if (hver) |
2641 | *hver = h.obj_version; | |
a71b891b | 2642 | rbd_dev->header.obj_version = h.obj_version; |
93a24e08 | 2643 | rbd_dev->header.image_size = h.image_size; |
602adf40 YS |
2644 | rbd_dev->header.snapc = h.snapc; |
2645 | rbd_dev->header.snap_names = h.snap_names; | |
2646 | rbd_dev->header.snap_sizes = h.snap_sizes; | |
849b4260 AE |
2647 | /* Free the extra copy of the object prefix */ |
2648 | WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix)); | |
2649 | kfree(h.object_prefix); | |
2650 | ||
304f6808 AE |
2651 | ret = rbd_dev_snaps_update(rbd_dev); |
2652 | if (!ret) | |
2653 | ret = rbd_dev_snaps_register(rbd_dev); | |
dfc5606d | 2654 | |
c666601a | 2655 | up_write(&rbd_dev->header_rwsem); |
602adf40 | 2656 | |
dfc5606d | 2657 | return ret; |
602adf40 YS |
2658 | } |
2659 | ||
117973fb | 2660 | static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver) |
1fe5e993 AE |
2661 | { |
2662 | int ret; | |
2663 | ||
117973fb | 2664 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
1fe5e993 | 2665 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); |
117973fb AE |
2666 | if (rbd_dev->image_format == 1) |
2667 | ret = rbd_dev_v1_refresh(rbd_dev, hver); | |
2668 | else | |
2669 | ret = rbd_dev_v2_refresh(rbd_dev, hver); | |
1fe5e993 AE |
2670 | mutex_unlock(&ctl_mutex); |
2671 | ||
2672 | return ret; | |
2673 | } | |
2674 | ||
602adf40 YS |
2675 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
2676 | { | |
2677 | struct gendisk *disk; | |
2678 | struct request_queue *q; | |
593a9e7b | 2679 | u64 segment_size; |
602adf40 | 2680 | |
602adf40 | 2681 | /* create gendisk info */ |
602adf40 YS |
2682 | disk = alloc_disk(RBD_MINORS_PER_MAJOR); |
2683 | if (!disk) | |
1fcdb8aa | 2684 | return -ENOMEM; |
602adf40 | 2685 | |
f0f8cef5 | 2686 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
de71a297 | 2687 | rbd_dev->dev_id); |
602adf40 YS |
2688 | disk->major = rbd_dev->major; |
2689 | disk->first_minor = 0; | |
2690 | disk->fops = &rbd_bd_ops; | |
2691 | disk->private_data = rbd_dev; | |
2692 | ||
bf0d5f50 | 2693 | q = blk_init_queue(rbd_request_fn, &rbd_dev->lock); |
602adf40 YS |
2694 | if (!q) |
2695 | goto out_disk; | |
029bcbd8 | 2696 | |
593a9e7b AE |
2697 | /* We use the default size, but let's be explicit about it. */ |
2698 | blk_queue_physical_block_size(q, SECTOR_SIZE); | |
2699 | ||
029bcbd8 | 2700 | /* set io sizes to object size */ |
593a9e7b AE |
2701 | segment_size = rbd_obj_bytes(&rbd_dev->header); |
2702 | blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE); | |
2703 | blk_queue_max_segment_size(q, segment_size); | |
2704 | blk_queue_io_min(q, segment_size); | |
2705 | blk_queue_io_opt(q, segment_size); | |
029bcbd8 | 2706 | |
602adf40 YS |
2707 | blk_queue_merge_bvec(q, rbd_merge_bvec); |
2708 | disk->queue = q; | |
2709 | ||
2710 | q->queuedata = rbd_dev; | |
2711 | ||
2712 | rbd_dev->disk = disk; | |
602adf40 | 2713 | |
12f02944 AE |
2714 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
2715 | ||
602adf40 | 2716 | return 0; |
602adf40 YS |
2717 | out_disk: |
2718 | put_disk(disk); | |
1fcdb8aa AE |
2719 | |
2720 | return -ENOMEM; | |
602adf40 YS |
2721 | } |
2722 | ||
dfc5606d YS |
2723 | /* |
2724 | sysfs | |
2725 | */ | |
2726 | ||
593a9e7b AE |
2727 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
2728 | { | |
2729 | return container_of(dev, struct rbd_device, dev); | |
2730 | } | |
2731 | ||
dfc5606d YS |
2732 | static ssize_t rbd_size_show(struct device *dev, |
2733 | struct device_attribute *attr, char *buf) | |
2734 | { | |
593a9e7b | 2735 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
a51aa0c0 JD |
2736 | sector_t size; |
2737 | ||
2738 | down_read(&rbd_dev->header_rwsem); | |
2739 | size = get_capacity(rbd_dev->disk); | |
2740 | up_read(&rbd_dev->header_rwsem); | |
dfc5606d | 2741 | |
a51aa0c0 | 2742 | return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE); |
dfc5606d YS |
2743 | } |
2744 | ||
34b13184 AE |
2745 | /* |
2746 | * Note this shows the features for whatever's mapped, which is not | |
2747 | * necessarily the base image. | |
2748 | */ | |
2749 | static ssize_t rbd_features_show(struct device *dev, | |
2750 | struct device_attribute *attr, char *buf) | |
2751 | { | |
2752 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
2753 | ||
2754 | return sprintf(buf, "0x%016llx\n", | |
2755 | (unsigned long long) rbd_dev->mapping.features); | |
2756 | } | |
2757 | ||
dfc5606d YS |
2758 | static ssize_t rbd_major_show(struct device *dev, |
2759 | struct device_attribute *attr, char *buf) | |
2760 | { | |
593a9e7b | 2761 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
602adf40 | 2762 | |
dfc5606d YS |
2763 | return sprintf(buf, "%d\n", rbd_dev->major); |
2764 | } | |
2765 | ||
2766 | static ssize_t rbd_client_id_show(struct device *dev, | |
2767 | struct device_attribute *attr, char *buf) | |
602adf40 | 2768 | { |
593a9e7b | 2769 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
dfc5606d | 2770 | |
1dbb4399 AE |
2771 | return sprintf(buf, "client%lld\n", |
2772 | ceph_client_id(rbd_dev->rbd_client->client)); | |
602adf40 YS |
2773 | } |
2774 | ||
dfc5606d YS |
2775 | static ssize_t rbd_pool_show(struct device *dev, |
2776 | struct device_attribute *attr, char *buf) | |
602adf40 | 2777 | { |
593a9e7b | 2778 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
dfc5606d | 2779 | |
0d7dbfce | 2780 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_name); |
dfc5606d YS |
2781 | } |
2782 | ||
9bb2f334 AE |
2783 | static ssize_t rbd_pool_id_show(struct device *dev, |
2784 | struct device_attribute *attr, char *buf) | |
2785 | { | |
2786 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
2787 | ||
0d7dbfce AE |
2788 | return sprintf(buf, "%llu\n", |
2789 | (unsigned long long) rbd_dev->spec->pool_id); | |
9bb2f334 AE |
2790 | } |
2791 | ||
dfc5606d YS |
2792 | static ssize_t rbd_name_show(struct device *dev, |
2793 | struct device_attribute *attr, char *buf) | |
2794 | { | |
593a9e7b | 2795 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
dfc5606d | 2796 | |
a92ffdf8 AE |
2797 | if (rbd_dev->spec->image_name) |
2798 | return sprintf(buf, "%s\n", rbd_dev->spec->image_name); | |
2799 | ||
2800 | return sprintf(buf, "(unknown)\n"); | |
dfc5606d YS |
2801 | } |
2802 | ||
589d30e0 AE |
2803 | static ssize_t rbd_image_id_show(struct device *dev, |
2804 | struct device_attribute *attr, char *buf) | |
2805 | { | |
2806 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
2807 | ||
0d7dbfce | 2808 | return sprintf(buf, "%s\n", rbd_dev->spec->image_id); |
589d30e0 AE |
2809 | } |
2810 | ||
34b13184 AE |
2811 | /* |
2812 | * Shows the name of the currently-mapped snapshot (or | |
2813 | * RBD_SNAP_HEAD_NAME for the base image). | |
2814 | */ | |
dfc5606d YS |
2815 | static ssize_t rbd_snap_show(struct device *dev, |
2816 | struct device_attribute *attr, | |
2817 | char *buf) | |
2818 | { | |
593a9e7b | 2819 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
dfc5606d | 2820 | |
0d7dbfce | 2821 | return sprintf(buf, "%s\n", rbd_dev->spec->snap_name); |
dfc5606d YS |
2822 | } |
2823 | ||
86b00e0d AE |
2824 | /* |
2825 | * For an rbd v2 image, shows the pool id, image id, and snapshot id | |
2826 | * for the parent image. If there is no parent, simply shows | |
2827 | * "(no parent image)". | |
2828 | */ | |
2829 | static ssize_t rbd_parent_show(struct device *dev, | |
2830 | struct device_attribute *attr, | |
2831 | char *buf) | |
2832 | { | |
2833 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); | |
2834 | struct rbd_spec *spec = rbd_dev->parent_spec; | |
2835 | int count; | |
2836 | char *bufp = buf; | |
2837 | ||
2838 | if (!spec) | |
2839 | return sprintf(buf, "(no parent image)\n"); | |
2840 | ||
2841 | count = sprintf(bufp, "pool_id %llu\npool_name %s\n", | |
2842 | (unsigned long long) spec->pool_id, spec->pool_name); | |
2843 | if (count < 0) | |
2844 | return count; | |
2845 | bufp += count; | |
2846 | ||
2847 | count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id, | |
2848 | spec->image_name ? spec->image_name : "(unknown)"); | |
2849 | if (count < 0) | |
2850 | return count; | |
2851 | bufp += count; | |
2852 | ||
2853 | count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n", | |
2854 | (unsigned long long) spec->snap_id, spec->snap_name); | |
2855 | if (count < 0) | |
2856 | return count; | |
2857 | bufp += count; | |
2858 | ||
2859 | count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap); | |
2860 | if (count < 0) | |
2861 | return count; | |
2862 | bufp += count; | |
2863 | ||
2864 | return (ssize_t) (bufp - buf); | |
2865 | } | |
2866 | ||
dfc5606d YS |
2867 | static ssize_t rbd_image_refresh(struct device *dev, |
2868 | struct device_attribute *attr, | |
2869 | const char *buf, | |
2870 | size_t size) | |
2871 | { | |
593a9e7b | 2872 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
b813623a | 2873 | int ret; |
602adf40 | 2874 | |
117973fb | 2875 | ret = rbd_dev_refresh(rbd_dev, NULL); |
b813623a AE |
2876 | |
2877 | return ret < 0 ? ret : size; | |
dfc5606d | 2878 | } |
602adf40 | 2879 | |
dfc5606d | 2880 | static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL); |
34b13184 | 2881 | static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL); |
dfc5606d YS |
2882 | static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL); |
2883 | static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL); | |
2884 | static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL); | |
9bb2f334 | 2885 | static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL); |
dfc5606d | 2886 | static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL); |
589d30e0 | 2887 | static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL); |
dfc5606d YS |
2888 | static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh); |
2889 | static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL); | |
86b00e0d | 2890 | static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL); |
dfc5606d YS |
2891 | |
2892 | static struct attribute *rbd_attrs[] = { | |
2893 | &dev_attr_size.attr, | |
34b13184 | 2894 | &dev_attr_features.attr, |
dfc5606d YS |
2895 | &dev_attr_major.attr, |
2896 | &dev_attr_client_id.attr, | |
2897 | &dev_attr_pool.attr, | |
9bb2f334 | 2898 | &dev_attr_pool_id.attr, |
dfc5606d | 2899 | &dev_attr_name.attr, |
589d30e0 | 2900 | &dev_attr_image_id.attr, |
dfc5606d | 2901 | &dev_attr_current_snap.attr, |
86b00e0d | 2902 | &dev_attr_parent.attr, |
dfc5606d | 2903 | &dev_attr_refresh.attr, |
dfc5606d YS |
2904 | NULL |
2905 | }; | |
2906 | ||
2907 | static struct attribute_group rbd_attr_group = { | |
2908 | .attrs = rbd_attrs, | |
2909 | }; | |
2910 | ||
2911 | static const struct attribute_group *rbd_attr_groups[] = { | |
2912 | &rbd_attr_group, | |
2913 | NULL | |
2914 | }; | |
2915 | ||
2916 | static void rbd_sysfs_dev_release(struct device *dev) | |
2917 | { | |
2918 | } | |
2919 | ||
2920 | static struct device_type rbd_device_type = { | |
2921 | .name = "rbd", | |
2922 | .groups = rbd_attr_groups, | |
2923 | .release = rbd_sysfs_dev_release, | |
2924 | }; | |
2925 | ||
2926 | ||
2927 | /* | |
2928 | sysfs - snapshots | |
2929 | */ | |
2930 | ||
2931 | static ssize_t rbd_snap_size_show(struct device *dev, | |
2932 | struct device_attribute *attr, | |
2933 | char *buf) | |
2934 | { | |
2935 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); | |
2936 | ||
3591538f | 2937 | return sprintf(buf, "%llu\n", (unsigned long long)snap->size); |
dfc5606d YS |
2938 | } |
2939 | ||
2940 | static ssize_t rbd_snap_id_show(struct device *dev, | |
2941 | struct device_attribute *attr, | |
2942 | char *buf) | |
2943 | { | |
2944 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); | |
2945 | ||
3591538f | 2946 | return sprintf(buf, "%llu\n", (unsigned long long)snap->id); |
dfc5606d YS |
2947 | } |
2948 | ||
34b13184 AE |
2949 | static ssize_t rbd_snap_features_show(struct device *dev, |
2950 | struct device_attribute *attr, | |
2951 | char *buf) | |
2952 | { | |
2953 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); | |
2954 | ||
2955 | return sprintf(buf, "0x%016llx\n", | |
2956 | (unsigned long long) snap->features); | |
2957 | } | |
2958 | ||
dfc5606d YS |
2959 | static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL); |
2960 | static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL); | |
34b13184 | 2961 | static DEVICE_ATTR(snap_features, S_IRUGO, rbd_snap_features_show, NULL); |
dfc5606d YS |
2962 | |
2963 | static struct attribute *rbd_snap_attrs[] = { | |
2964 | &dev_attr_snap_size.attr, | |
2965 | &dev_attr_snap_id.attr, | |
34b13184 | 2966 | &dev_attr_snap_features.attr, |
dfc5606d YS |
2967 | NULL, |
2968 | }; | |
2969 | ||
2970 | static struct attribute_group rbd_snap_attr_group = { | |
2971 | .attrs = rbd_snap_attrs, | |
2972 | }; | |
2973 | ||
2974 | static void rbd_snap_dev_release(struct device *dev) | |
2975 | { | |
2976 | struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev); | |
2977 | kfree(snap->name); | |
2978 | kfree(snap); | |
2979 | } | |
2980 | ||
2981 | static const struct attribute_group *rbd_snap_attr_groups[] = { | |
2982 | &rbd_snap_attr_group, | |
2983 | NULL | |
2984 | }; | |
2985 | ||
2986 | static struct device_type rbd_snap_device_type = { | |
2987 | .groups = rbd_snap_attr_groups, | |
2988 | .release = rbd_snap_dev_release, | |
2989 | }; | |
2990 | ||
8b8fb99c AE |
2991 | static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec) |
2992 | { | |
2993 | kref_get(&spec->kref); | |
2994 | ||
2995 | return spec; | |
2996 | } | |
2997 | ||
2998 | static void rbd_spec_free(struct kref *kref); | |
2999 | static void rbd_spec_put(struct rbd_spec *spec) | |
3000 | { | |
3001 | if (spec) | |
3002 | kref_put(&spec->kref, rbd_spec_free); | |
3003 | } | |
3004 | ||
3005 | static struct rbd_spec *rbd_spec_alloc(void) | |
3006 | { | |
3007 | struct rbd_spec *spec; | |
3008 | ||
3009 | spec = kzalloc(sizeof (*spec), GFP_KERNEL); | |
3010 | if (!spec) | |
3011 | return NULL; | |
3012 | kref_init(&spec->kref); | |
3013 | ||
8b8fb99c AE |
3014 | return spec; |
3015 | } | |
3016 | ||
3017 | static void rbd_spec_free(struct kref *kref) | |
3018 | { | |
3019 | struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref); | |
3020 | ||
3021 | kfree(spec->pool_name); | |
3022 | kfree(spec->image_id); | |
3023 | kfree(spec->image_name); | |
3024 | kfree(spec->snap_name); | |
3025 | kfree(spec); | |
3026 | } | |
3027 | ||
cc344fa1 | 3028 | static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, |
c53d5893 AE |
3029 | struct rbd_spec *spec) |
3030 | { | |
3031 | struct rbd_device *rbd_dev; | |
3032 | ||
3033 | rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL); | |
3034 | if (!rbd_dev) | |
3035 | return NULL; | |
3036 | ||
3037 | spin_lock_init(&rbd_dev->lock); | |
6d292906 | 3038 | rbd_dev->flags = 0; |
c53d5893 AE |
3039 | INIT_LIST_HEAD(&rbd_dev->node); |
3040 | INIT_LIST_HEAD(&rbd_dev->snaps); | |
3041 | init_rwsem(&rbd_dev->header_rwsem); | |
3042 | ||
3043 | rbd_dev->spec = spec; | |
3044 | rbd_dev->rbd_client = rbdc; | |
3045 | ||
0903e875 AE |
3046 | /* Initialize the layout used for all rbd requests */ |
3047 | ||
3048 | rbd_dev->layout.fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER); | |
3049 | rbd_dev->layout.fl_stripe_count = cpu_to_le32(1); | |
3050 | rbd_dev->layout.fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER); | |
3051 | rbd_dev->layout.fl_pg_pool = cpu_to_le32((u32) spec->pool_id); | |
3052 | ||
c53d5893 AE |
3053 | return rbd_dev; |
3054 | } | |
3055 | ||
3056 | static void rbd_dev_destroy(struct rbd_device *rbd_dev) | |
3057 | { | |
86b00e0d | 3058 | rbd_spec_put(rbd_dev->parent_spec); |
c53d5893 AE |
3059 | kfree(rbd_dev->header_name); |
3060 | rbd_put_client(rbd_dev->rbd_client); | |
3061 | rbd_spec_put(rbd_dev->spec); | |
3062 | kfree(rbd_dev); | |
3063 | } | |
3064 | ||
304f6808 AE |
3065 | static bool rbd_snap_registered(struct rbd_snap *snap) |
3066 | { | |
3067 | bool ret = snap->dev.type == &rbd_snap_device_type; | |
3068 | bool reg = device_is_registered(&snap->dev); | |
3069 | ||
3070 | rbd_assert(!ret ^ reg); | |
3071 | ||
3072 | return ret; | |
3073 | } | |
3074 | ||
41f38c2b | 3075 | static void rbd_remove_snap_dev(struct rbd_snap *snap) |
dfc5606d YS |
3076 | { |
3077 | list_del(&snap->node); | |
304f6808 AE |
3078 | if (device_is_registered(&snap->dev)) |
3079 | device_unregister(&snap->dev); | |
dfc5606d YS |
3080 | } |
3081 | ||
14e7085d | 3082 | static int rbd_register_snap_dev(struct rbd_snap *snap, |
dfc5606d YS |
3083 | struct device *parent) |
3084 | { | |
3085 | struct device *dev = &snap->dev; | |
3086 | int ret; | |
3087 | ||
3088 | dev->type = &rbd_snap_device_type; | |
3089 | dev->parent = parent; | |
3090 | dev->release = rbd_snap_dev_release; | |
d4b125e9 | 3091 | dev_set_name(dev, "%s%s", RBD_SNAP_DEV_NAME_PREFIX, snap->name); |
304f6808 AE |
3092 | dout("%s: registering device for snapshot %s\n", __func__, snap->name); |
3093 | ||
dfc5606d YS |
3094 | ret = device_register(dev); |
3095 | ||
3096 | return ret; | |
3097 | } | |
3098 | ||
4e891e0a | 3099 | static struct rbd_snap *__rbd_add_snap_dev(struct rbd_device *rbd_dev, |
c8d18425 | 3100 | const char *snap_name, |
34b13184 AE |
3101 | u64 snap_id, u64 snap_size, |
3102 | u64 snap_features) | |
dfc5606d | 3103 | { |
4e891e0a | 3104 | struct rbd_snap *snap; |
dfc5606d | 3105 | int ret; |
4e891e0a AE |
3106 | |
3107 | snap = kzalloc(sizeof (*snap), GFP_KERNEL); | |
dfc5606d | 3108 | if (!snap) |
4e891e0a AE |
3109 | return ERR_PTR(-ENOMEM); |
3110 | ||
3111 | ret = -ENOMEM; | |
c8d18425 | 3112 | snap->name = kstrdup(snap_name, GFP_KERNEL); |
4e891e0a AE |
3113 | if (!snap->name) |
3114 | goto err; | |
3115 | ||
c8d18425 AE |
3116 | snap->id = snap_id; |
3117 | snap->size = snap_size; | |
34b13184 | 3118 | snap->features = snap_features; |
4e891e0a AE |
3119 | |
3120 | return snap; | |
3121 | ||
dfc5606d YS |
3122 | err: |
3123 | kfree(snap->name); | |
3124 | kfree(snap); | |
4e891e0a AE |
3125 | |
3126 | return ERR_PTR(ret); | |
dfc5606d YS |
3127 | } |
3128 | ||
cd892126 AE |
3129 | static char *rbd_dev_v1_snap_info(struct rbd_device *rbd_dev, u32 which, |
3130 | u64 *snap_size, u64 *snap_features) | |
3131 | { | |
3132 | char *snap_name; | |
3133 | ||
3134 | rbd_assert(which < rbd_dev->header.snapc->num_snaps); | |
3135 | ||
3136 | *snap_size = rbd_dev->header.snap_sizes[which]; | |
3137 | *snap_features = 0; /* No features for v1 */ | |
3138 | ||
3139 | /* Skip over names until we find the one we are looking for */ | |
3140 | ||
3141 | snap_name = rbd_dev->header.snap_names; | |
3142 | while (which--) | |
3143 | snap_name += strlen(snap_name) + 1; | |
3144 | ||
3145 | return snap_name; | |
3146 | } | |
3147 | ||
9d475de5 AE |
3148 | /* |
3149 | * Get the size and object order for an image snapshot, or if | |
3150 | * snap_id is CEPH_NOSNAP, gets this information for the base | |
3151 | * image. | |
3152 | */ | |
3153 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, | |
3154 | u8 *order, u64 *snap_size) | |
3155 | { | |
3156 | __le64 snapid = cpu_to_le64(snap_id); | |
3157 | int ret; | |
3158 | struct { | |
3159 | u8 order; | |
3160 | __le64 size; | |
3161 | } __attribute__ ((packed)) size_buf = { 0 }; | |
3162 | ||
36be9a76 | 3163 | ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name, |
9d475de5 AE |
3164 | "rbd", "get_size", |
3165 | (char *) &snapid, sizeof (snapid), | |
07b2391f | 3166 | (char *) &size_buf, sizeof (size_buf), NULL); |
36be9a76 | 3167 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
9d475de5 AE |
3168 | if (ret < 0) |
3169 | return ret; | |
3170 | ||
3171 | *order = size_buf.order; | |
3172 | *snap_size = le64_to_cpu(size_buf.size); | |
3173 | ||
3174 | dout(" snap_id 0x%016llx order = %u, snap_size = %llu\n", | |
3175 | (unsigned long long) snap_id, (unsigned int) *order, | |
3176 | (unsigned long long) *snap_size); | |
3177 | ||
3178 | return 0; | |
3179 | } | |
3180 | ||
3181 | static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev) | |
3182 | { | |
3183 | return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP, | |
3184 | &rbd_dev->header.obj_order, | |
3185 | &rbd_dev->header.image_size); | |
3186 | } | |
3187 | ||
1e130199 AE |
3188 | static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev) |
3189 | { | |
3190 | void *reply_buf; | |
3191 | int ret; | |
3192 | void *p; | |
3193 | ||
3194 | reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL); | |
3195 | if (!reply_buf) | |
3196 | return -ENOMEM; | |
3197 | ||
36be9a76 | 3198 | ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name, |
1e130199 AE |
3199 | "rbd", "get_object_prefix", |
3200 | NULL, 0, | |
07b2391f | 3201 | reply_buf, RBD_OBJ_PREFIX_LEN_MAX, NULL); |
36be9a76 | 3202 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
1e130199 AE |
3203 | if (ret < 0) |
3204 | goto out; | |
3205 | ||
3206 | p = reply_buf; | |
3207 | rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p, | |
3208 | p + RBD_OBJ_PREFIX_LEN_MAX, | |
3209 | NULL, GFP_NOIO); | |
3210 | ||
3211 | if (IS_ERR(rbd_dev->header.object_prefix)) { | |
3212 | ret = PTR_ERR(rbd_dev->header.object_prefix); | |
3213 | rbd_dev->header.object_prefix = NULL; | |
3214 | } else { | |
3215 | dout(" object_prefix = %s\n", rbd_dev->header.object_prefix); | |
3216 | } | |
3217 | ||
3218 | out: | |
3219 | kfree(reply_buf); | |
3220 | ||
3221 | return ret; | |
3222 | } | |
3223 | ||
b1b5402a AE |
3224 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
3225 | u64 *snap_features) | |
3226 | { | |
3227 | __le64 snapid = cpu_to_le64(snap_id); | |
3228 | struct { | |
3229 | __le64 features; | |
3230 | __le64 incompat; | |
3231 | } features_buf = { 0 }; | |
d889140c | 3232 | u64 incompat; |
b1b5402a AE |
3233 | int ret; |
3234 | ||
36be9a76 | 3235 | ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name, |
b1b5402a AE |
3236 | "rbd", "get_features", |
3237 | (char *) &snapid, sizeof (snapid), | |
3238 | (char *) &features_buf, sizeof (features_buf), | |
07b2391f | 3239 | NULL); |
36be9a76 | 3240 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
b1b5402a AE |
3241 | if (ret < 0) |
3242 | return ret; | |
d889140c AE |
3243 | |
3244 | incompat = le64_to_cpu(features_buf.incompat); | |
5cbf6f12 | 3245 | if (incompat & ~RBD_FEATURES_SUPPORTED) |
b8f5c6ed | 3246 | return -ENXIO; |
d889140c | 3247 | |
b1b5402a AE |
3248 | *snap_features = le64_to_cpu(features_buf.features); |
3249 | ||
3250 | dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n", | |
3251 | (unsigned long long) snap_id, | |
3252 | (unsigned long long) *snap_features, | |
3253 | (unsigned long long) le64_to_cpu(features_buf.incompat)); | |
3254 | ||
3255 | return 0; | |
3256 | } | |
3257 | ||
3258 | static int rbd_dev_v2_features(struct rbd_device *rbd_dev) | |
3259 | { | |
3260 | return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP, | |
3261 | &rbd_dev->header.features); | |
3262 | } | |
3263 | ||
86b00e0d AE |
3264 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev) |
3265 | { | |
3266 | struct rbd_spec *parent_spec; | |
3267 | size_t size; | |
3268 | void *reply_buf = NULL; | |
3269 | __le64 snapid; | |
3270 | void *p; | |
3271 | void *end; | |
3272 | char *image_id; | |
3273 | u64 overlap; | |
86b00e0d AE |
3274 | int ret; |
3275 | ||
3276 | parent_spec = rbd_spec_alloc(); | |
3277 | if (!parent_spec) | |
3278 | return -ENOMEM; | |
3279 | ||
3280 | size = sizeof (__le64) + /* pool_id */ | |
3281 | sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */ | |
3282 | sizeof (__le64) + /* snap_id */ | |
3283 | sizeof (__le64); /* overlap */ | |
3284 | reply_buf = kmalloc(size, GFP_KERNEL); | |
3285 | if (!reply_buf) { | |
3286 | ret = -ENOMEM; | |
3287 | goto out_err; | |
3288 | } | |
3289 | ||
3290 | snapid = cpu_to_le64(CEPH_NOSNAP); | |
36be9a76 | 3291 | ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name, |
86b00e0d AE |
3292 | "rbd", "get_parent", |
3293 | (char *) &snapid, sizeof (snapid), | |
07b2391f | 3294 | (char *) reply_buf, size, NULL); |
36be9a76 | 3295 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
86b00e0d AE |
3296 | if (ret < 0) |
3297 | goto out_err; | |
3298 | ||
3299 | ret = -ERANGE; | |
3300 | p = reply_buf; | |
3301 | end = (char *) reply_buf + size; | |
3302 | ceph_decode_64_safe(&p, end, parent_spec->pool_id, out_err); | |
3303 | if (parent_spec->pool_id == CEPH_NOPOOL) | |
3304 | goto out; /* No parent? No problem. */ | |
3305 | ||
0903e875 AE |
3306 | /* The ceph file layout needs to fit pool id in 32 bits */ |
3307 | ||
3308 | ret = -EIO; | |
3309 | if (WARN_ON(parent_spec->pool_id > (u64) U32_MAX)) | |
3310 | goto out; | |
3311 | ||
979ed480 | 3312 | image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
86b00e0d AE |
3313 | if (IS_ERR(image_id)) { |
3314 | ret = PTR_ERR(image_id); | |
3315 | goto out_err; | |
3316 | } | |
3317 | parent_spec->image_id = image_id; | |
3318 | ceph_decode_64_safe(&p, end, parent_spec->snap_id, out_err); | |
3319 | ceph_decode_64_safe(&p, end, overlap, out_err); | |
3320 | ||
3321 | rbd_dev->parent_overlap = overlap; | |
3322 | rbd_dev->parent_spec = parent_spec; | |
3323 | parent_spec = NULL; /* rbd_dev now owns this */ | |
3324 | out: | |
3325 | ret = 0; | |
3326 | out_err: | |
3327 | kfree(reply_buf); | |
3328 | rbd_spec_put(parent_spec); | |
3329 | ||
3330 | return ret; | |
3331 | } | |
3332 | ||
9e15b77d AE |
3333 | static char *rbd_dev_image_name(struct rbd_device *rbd_dev) |
3334 | { | |
3335 | size_t image_id_size; | |
3336 | char *image_id; | |
3337 | void *p; | |
3338 | void *end; | |
3339 | size_t size; | |
3340 | void *reply_buf = NULL; | |
3341 | size_t len = 0; | |
3342 | char *image_name = NULL; | |
3343 | int ret; | |
3344 | ||
3345 | rbd_assert(!rbd_dev->spec->image_name); | |
3346 | ||
69e7a02f AE |
3347 | len = strlen(rbd_dev->spec->image_id); |
3348 | image_id_size = sizeof (__le32) + len; | |
9e15b77d AE |
3349 | image_id = kmalloc(image_id_size, GFP_KERNEL); |
3350 | if (!image_id) | |
3351 | return NULL; | |
3352 | ||
3353 | p = image_id; | |
3354 | end = (char *) image_id + image_id_size; | |
69e7a02f | 3355 | ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32) len); |
9e15b77d AE |
3356 | |
3357 | size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX; | |
3358 | reply_buf = kmalloc(size, GFP_KERNEL); | |
3359 | if (!reply_buf) | |
3360 | goto out; | |
3361 | ||
36be9a76 | 3362 | ret = rbd_obj_method_sync(rbd_dev, RBD_DIRECTORY, |
9e15b77d AE |
3363 | "rbd", "dir_get_name", |
3364 | image_id, image_id_size, | |
07b2391f | 3365 | (char *) reply_buf, size, NULL); |
9e15b77d AE |
3366 | if (ret < 0) |
3367 | goto out; | |
3368 | p = reply_buf; | |
3369 | end = (char *) reply_buf + size; | |
3370 | image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL); | |
3371 | if (IS_ERR(image_name)) | |
3372 | image_name = NULL; | |
3373 | else | |
3374 | dout("%s: name is %s len is %zd\n", __func__, image_name, len); | |
3375 | out: | |
3376 | kfree(reply_buf); | |
3377 | kfree(image_id); | |
3378 | ||
3379 | return image_name; | |
3380 | } | |
3381 | ||
3382 | /* | |
3383 | * When a parent image gets probed, we only have the pool, image, | |
3384 | * and snapshot ids but not the names of any of them. This call | |
3385 | * is made later to fill in those names. It has to be done after | |
3386 | * rbd_dev_snaps_update() has completed because some of the | |
3387 | * information (in particular, snapshot name) is not available | |
3388 | * until then. | |
3389 | */ | |
3390 | static int rbd_dev_probe_update_spec(struct rbd_device *rbd_dev) | |
3391 | { | |
3392 | struct ceph_osd_client *osdc; | |
3393 | const char *name; | |
3394 | void *reply_buf = NULL; | |
3395 | int ret; | |
3396 | ||
3397 | if (rbd_dev->spec->pool_name) | |
3398 | return 0; /* Already have the names */ | |
3399 | ||
3400 | /* Look up the pool name */ | |
3401 | ||
3402 | osdc = &rbd_dev->rbd_client->client->osdc; | |
3403 | name = ceph_pg_pool_name_by_id(osdc->osdmap, rbd_dev->spec->pool_id); | |
935dc89f AE |
3404 | if (!name) { |
3405 | rbd_warn(rbd_dev, "there is no pool with id %llu", | |
3406 | rbd_dev->spec->pool_id); /* Really a BUG() */ | |
3407 | return -EIO; | |
3408 | } | |
9e15b77d AE |
3409 | |
3410 | rbd_dev->spec->pool_name = kstrdup(name, GFP_KERNEL); | |
3411 | if (!rbd_dev->spec->pool_name) | |
3412 | return -ENOMEM; | |
3413 | ||
3414 | /* Fetch the image name; tolerate failure here */ | |
3415 | ||
3416 | name = rbd_dev_image_name(rbd_dev); | |
69e7a02f | 3417 | if (name) |
9e15b77d | 3418 | rbd_dev->spec->image_name = (char *) name; |
69e7a02f | 3419 | else |
06ecc6cb | 3420 | rbd_warn(rbd_dev, "unable to get image name"); |
9e15b77d AE |
3421 | |
3422 | /* Look up the snapshot name. */ | |
3423 | ||
3424 | name = rbd_snap_name(rbd_dev, rbd_dev->spec->snap_id); | |
3425 | if (!name) { | |
935dc89f AE |
3426 | rbd_warn(rbd_dev, "no snapshot with id %llu", |
3427 | rbd_dev->spec->snap_id); /* Really a BUG() */ | |
9e15b77d AE |
3428 | ret = -EIO; |
3429 | goto out_err; | |
3430 | } | |
3431 | rbd_dev->spec->snap_name = kstrdup(name, GFP_KERNEL); | |
3432 | if(!rbd_dev->spec->snap_name) | |
3433 | goto out_err; | |
3434 | ||
3435 | return 0; | |
3436 | out_err: | |
3437 | kfree(reply_buf); | |
3438 | kfree(rbd_dev->spec->pool_name); | |
3439 | rbd_dev->spec->pool_name = NULL; | |
3440 | ||
3441 | return ret; | |
3442 | } | |
3443 | ||
6e14b1a6 | 3444 | static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev, u64 *ver) |
35d489f9 AE |
3445 | { |
3446 | size_t size; | |
3447 | int ret; | |
3448 | void *reply_buf; | |
3449 | void *p; | |
3450 | void *end; | |
3451 | u64 seq; | |
3452 | u32 snap_count; | |
3453 | struct ceph_snap_context *snapc; | |
3454 | u32 i; | |
3455 | ||
3456 | /* | |
3457 | * We'll need room for the seq value (maximum snapshot id), | |
3458 | * snapshot count, and array of that many snapshot ids. | |
3459 | * For now we have a fixed upper limit on the number we're | |
3460 | * prepared to receive. | |
3461 | */ | |
3462 | size = sizeof (__le64) + sizeof (__le32) + | |
3463 | RBD_MAX_SNAP_COUNT * sizeof (__le64); | |
3464 | reply_buf = kzalloc(size, GFP_KERNEL); | |
3465 | if (!reply_buf) | |
3466 | return -ENOMEM; | |
3467 | ||
36be9a76 | 3468 | ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name, |
35d489f9 AE |
3469 | "rbd", "get_snapcontext", |
3470 | NULL, 0, | |
07b2391f | 3471 | reply_buf, size, ver); |
36be9a76 | 3472 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
35d489f9 AE |
3473 | if (ret < 0) |
3474 | goto out; | |
3475 | ||
3476 | ret = -ERANGE; | |
3477 | p = reply_buf; | |
3478 | end = (char *) reply_buf + size; | |
3479 | ceph_decode_64_safe(&p, end, seq, out); | |
3480 | ceph_decode_32_safe(&p, end, snap_count, out); | |
3481 | ||
3482 | /* | |
3483 | * Make sure the reported number of snapshot ids wouldn't go | |
3484 | * beyond the end of our buffer. But before checking that, | |
3485 | * make sure the computed size of the snapshot context we | |
3486 | * allocate is representable in a size_t. | |
3487 | */ | |
3488 | if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context)) | |
3489 | / sizeof (u64)) { | |
3490 | ret = -EINVAL; | |
3491 | goto out; | |
3492 | } | |
3493 | if (!ceph_has_room(&p, end, snap_count * sizeof (__le64))) | |
3494 | goto out; | |
3495 | ||
3496 | size = sizeof (struct ceph_snap_context) + | |
3497 | snap_count * sizeof (snapc->snaps[0]); | |
3498 | snapc = kmalloc(size, GFP_KERNEL); | |
3499 | if (!snapc) { | |
3500 | ret = -ENOMEM; | |
3501 | goto out; | |
3502 | } | |
3503 | ||
3504 | atomic_set(&snapc->nref, 1); | |
3505 | snapc->seq = seq; | |
3506 | snapc->num_snaps = snap_count; | |
3507 | for (i = 0; i < snap_count; i++) | |
3508 | snapc->snaps[i] = ceph_decode_64(&p); | |
3509 | ||
3510 | rbd_dev->header.snapc = snapc; | |
3511 | ||
3512 | dout(" snap context seq = %llu, snap_count = %u\n", | |
3513 | (unsigned long long) seq, (unsigned int) snap_count); | |
3514 | ||
3515 | out: | |
3516 | kfree(reply_buf); | |
3517 | ||
3518 | return 0; | |
3519 | } | |
3520 | ||
b8b1e2db AE |
3521 | static char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, u32 which) |
3522 | { | |
3523 | size_t size; | |
3524 | void *reply_buf; | |
3525 | __le64 snap_id; | |
3526 | int ret; | |
3527 | void *p; | |
3528 | void *end; | |
b8b1e2db AE |
3529 | char *snap_name; |
3530 | ||
3531 | size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN; | |
3532 | reply_buf = kmalloc(size, GFP_KERNEL); | |
3533 | if (!reply_buf) | |
3534 | return ERR_PTR(-ENOMEM); | |
3535 | ||
3536 | snap_id = cpu_to_le64(rbd_dev->header.snapc->snaps[which]); | |
36be9a76 | 3537 | ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name, |
b8b1e2db AE |
3538 | "rbd", "get_snapshot_name", |
3539 | (char *) &snap_id, sizeof (snap_id), | |
07b2391f | 3540 | reply_buf, size, NULL); |
36be9a76 | 3541 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
b8b1e2db AE |
3542 | if (ret < 0) |
3543 | goto out; | |
3544 | ||
3545 | p = reply_buf; | |
3546 | end = (char *) reply_buf + size; | |
e5c35534 | 3547 | snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
b8b1e2db AE |
3548 | if (IS_ERR(snap_name)) { |
3549 | ret = PTR_ERR(snap_name); | |
3550 | goto out; | |
3551 | } else { | |
3552 | dout(" snap_id 0x%016llx snap_name = %s\n", | |
3553 | (unsigned long long) le64_to_cpu(snap_id), snap_name); | |
3554 | } | |
3555 | kfree(reply_buf); | |
3556 | ||
3557 | return snap_name; | |
3558 | out: | |
3559 | kfree(reply_buf); | |
3560 | ||
3561 | return ERR_PTR(ret); | |
3562 | } | |
3563 | ||
3564 | static char *rbd_dev_v2_snap_info(struct rbd_device *rbd_dev, u32 which, | |
3565 | u64 *snap_size, u64 *snap_features) | |
3566 | { | |
e0b49868 | 3567 | u64 snap_id; |
b8b1e2db AE |
3568 | u8 order; |
3569 | int ret; | |
3570 | ||
3571 | snap_id = rbd_dev->header.snapc->snaps[which]; | |
3572 | ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, &order, snap_size); | |
3573 | if (ret) | |
3574 | return ERR_PTR(ret); | |
3575 | ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, snap_features); | |
3576 | if (ret) | |
3577 | return ERR_PTR(ret); | |
3578 | ||
3579 | return rbd_dev_v2_snap_name(rbd_dev, which); | |
3580 | } | |
3581 | ||
3582 | static char *rbd_dev_snap_info(struct rbd_device *rbd_dev, u32 which, | |
3583 | u64 *snap_size, u64 *snap_features) | |
3584 | { | |
3585 | if (rbd_dev->image_format == 1) | |
3586 | return rbd_dev_v1_snap_info(rbd_dev, which, | |
3587 | snap_size, snap_features); | |
3588 | if (rbd_dev->image_format == 2) | |
3589 | return rbd_dev_v2_snap_info(rbd_dev, which, | |
3590 | snap_size, snap_features); | |
3591 | return ERR_PTR(-EINVAL); | |
3592 | } | |
3593 | ||
117973fb AE |
3594 | static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver) |
3595 | { | |
3596 | int ret; | |
3597 | __u8 obj_order; | |
3598 | ||
3599 | down_write(&rbd_dev->header_rwsem); | |
3600 | ||
3601 | /* Grab old order first, to see if it changes */ | |
3602 | ||
3603 | obj_order = rbd_dev->header.obj_order, | |
3604 | ret = rbd_dev_v2_image_size(rbd_dev); | |
3605 | if (ret) | |
3606 | goto out; | |
3607 | if (rbd_dev->header.obj_order != obj_order) { | |
3608 | ret = -EIO; | |
3609 | goto out; | |
3610 | } | |
3611 | rbd_update_mapping_size(rbd_dev); | |
3612 | ||
3613 | ret = rbd_dev_v2_snap_context(rbd_dev, hver); | |
3614 | dout("rbd_dev_v2_snap_context returned %d\n", ret); | |
3615 | if (ret) | |
3616 | goto out; | |
3617 | ret = rbd_dev_snaps_update(rbd_dev); | |
3618 | dout("rbd_dev_snaps_update returned %d\n", ret); | |
3619 | if (ret) | |
3620 | goto out; | |
3621 | ret = rbd_dev_snaps_register(rbd_dev); | |
3622 | dout("rbd_dev_snaps_register returned %d\n", ret); | |
3623 | out: | |
3624 | up_write(&rbd_dev->header_rwsem); | |
3625 | ||
3626 | return ret; | |
3627 | } | |
3628 | ||
dfc5606d | 3629 | /* |
35938150 AE |
3630 | * Scan the rbd device's current snapshot list and compare it to the |
3631 | * newly-received snapshot context. Remove any existing snapshots | |
3632 | * not present in the new snapshot context. Add a new snapshot for | |
3633 | * any snaphots in the snapshot context not in the current list. | |
3634 | * And verify there are no changes to snapshots we already know | |
3635 | * about. | |
3636 | * | |
3637 | * Assumes the snapshots in the snapshot context are sorted by | |
3638 | * snapshot id, highest id first. (Snapshots in the rbd_dev's list | |
3639 | * are also maintained in that order.) | |
dfc5606d | 3640 | */ |
304f6808 | 3641 | static int rbd_dev_snaps_update(struct rbd_device *rbd_dev) |
dfc5606d | 3642 | { |
35938150 AE |
3643 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
3644 | const u32 snap_count = snapc->num_snaps; | |
35938150 AE |
3645 | struct list_head *head = &rbd_dev->snaps; |
3646 | struct list_head *links = head->next; | |
3647 | u32 index = 0; | |
dfc5606d | 3648 | |
9fcbb800 | 3649 | dout("%s: snap count is %u\n", __func__, (unsigned int) snap_count); |
35938150 AE |
3650 | while (index < snap_count || links != head) { |
3651 | u64 snap_id; | |
3652 | struct rbd_snap *snap; | |
cd892126 AE |
3653 | char *snap_name; |
3654 | u64 snap_size = 0; | |
3655 | u64 snap_features = 0; | |
dfc5606d | 3656 | |
35938150 AE |
3657 | snap_id = index < snap_count ? snapc->snaps[index] |
3658 | : CEPH_NOSNAP; | |
3659 | snap = links != head ? list_entry(links, struct rbd_snap, node) | |
3660 | : NULL; | |
aafb230e | 3661 | rbd_assert(!snap || snap->id != CEPH_NOSNAP); |
dfc5606d | 3662 | |
35938150 AE |
3663 | if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) { |
3664 | struct list_head *next = links->next; | |
dfc5606d | 3665 | |
6d292906 AE |
3666 | /* |
3667 | * A previously-existing snapshot is not in | |
3668 | * the new snap context. | |
3669 | * | |
3670 | * If the now missing snapshot is the one the | |
3671 | * image is mapped to, clear its exists flag | |
3672 | * so we can avoid sending any more requests | |
3673 | * to it. | |
3674 | */ | |
0d7dbfce | 3675 | if (rbd_dev->spec->snap_id == snap->id) |
6d292906 | 3676 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
41f38c2b | 3677 | rbd_remove_snap_dev(snap); |
9fcbb800 | 3678 | dout("%ssnap id %llu has been removed\n", |
0d7dbfce AE |
3679 | rbd_dev->spec->snap_id == snap->id ? |
3680 | "mapped " : "", | |
9fcbb800 | 3681 | (unsigned long long) snap->id); |
35938150 AE |
3682 | |
3683 | /* Done with this list entry; advance */ | |
3684 | ||
3685 | links = next; | |
dfc5606d YS |
3686 | continue; |
3687 | } | |
35938150 | 3688 | |
b8b1e2db AE |
3689 | snap_name = rbd_dev_snap_info(rbd_dev, index, |
3690 | &snap_size, &snap_features); | |
cd892126 AE |
3691 | if (IS_ERR(snap_name)) |
3692 | return PTR_ERR(snap_name); | |
3693 | ||
9fcbb800 AE |
3694 | dout("entry %u: snap_id = %llu\n", (unsigned int) snap_count, |
3695 | (unsigned long long) snap_id); | |
35938150 AE |
3696 | if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) { |
3697 | struct rbd_snap *new_snap; | |
3698 | ||
3699 | /* We haven't seen this snapshot before */ | |
3700 | ||
c8d18425 | 3701 | new_snap = __rbd_add_snap_dev(rbd_dev, snap_name, |
cd892126 | 3702 | snap_id, snap_size, snap_features); |
9fcbb800 AE |
3703 | if (IS_ERR(new_snap)) { |
3704 | int err = PTR_ERR(new_snap); | |
3705 | ||
3706 | dout(" failed to add dev, error %d\n", err); | |
3707 | ||
3708 | return err; | |
3709 | } | |
35938150 AE |
3710 | |
3711 | /* New goes before existing, or at end of list */ | |
3712 | ||
9fcbb800 | 3713 | dout(" added dev%s\n", snap ? "" : " at end\n"); |
35938150 AE |
3714 | if (snap) |
3715 | list_add_tail(&new_snap->node, &snap->node); | |
3716 | else | |
523f3258 | 3717 | list_add_tail(&new_snap->node, head); |
35938150 AE |
3718 | } else { |
3719 | /* Already have this one */ | |
3720 | ||
9fcbb800 AE |
3721 | dout(" already present\n"); |
3722 | ||
cd892126 | 3723 | rbd_assert(snap->size == snap_size); |
aafb230e | 3724 | rbd_assert(!strcmp(snap->name, snap_name)); |
cd892126 | 3725 | rbd_assert(snap->features == snap_features); |
35938150 AE |
3726 | |
3727 | /* Done with this list entry; advance */ | |
3728 | ||
3729 | links = links->next; | |
dfc5606d | 3730 | } |
35938150 AE |
3731 | |
3732 | /* Advance to the next entry in the snapshot context */ | |
3733 | ||
3734 | index++; | |
dfc5606d | 3735 | } |
9fcbb800 | 3736 | dout("%s: done\n", __func__); |
dfc5606d YS |
3737 | |
3738 | return 0; | |
3739 | } | |
3740 | ||
304f6808 AE |
3741 | /* |
3742 | * Scan the list of snapshots and register the devices for any that | |
3743 | * have not already been registered. | |
3744 | */ | |
3745 | static int rbd_dev_snaps_register(struct rbd_device *rbd_dev) | |
3746 | { | |
3747 | struct rbd_snap *snap; | |
3748 | int ret = 0; | |
3749 | ||
37206ee5 | 3750 | dout("%s:\n", __func__); |
86ff77bb AE |
3751 | if (WARN_ON(!device_is_registered(&rbd_dev->dev))) |
3752 | return -EIO; | |
304f6808 AE |
3753 | |
3754 | list_for_each_entry(snap, &rbd_dev->snaps, node) { | |
3755 | if (!rbd_snap_registered(snap)) { | |
3756 | ret = rbd_register_snap_dev(snap, &rbd_dev->dev); | |
3757 | if (ret < 0) | |
3758 | break; | |
3759 | } | |
3760 | } | |
3761 | dout("%s: returning %d\n", __func__, ret); | |
3762 | ||
3763 | return ret; | |
3764 | } | |
3765 | ||
dfc5606d YS |
3766 | static int rbd_bus_add_dev(struct rbd_device *rbd_dev) |
3767 | { | |
dfc5606d | 3768 | struct device *dev; |
cd789ab9 | 3769 | int ret; |
dfc5606d YS |
3770 | |
3771 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); | |
dfc5606d | 3772 | |
cd789ab9 | 3773 | dev = &rbd_dev->dev; |
dfc5606d YS |
3774 | dev->bus = &rbd_bus_type; |
3775 | dev->type = &rbd_device_type; | |
3776 | dev->parent = &rbd_root_dev; | |
3777 | dev->release = rbd_dev_release; | |
de71a297 | 3778 | dev_set_name(dev, "%d", rbd_dev->dev_id); |
dfc5606d | 3779 | ret = device_register(dev); |
dfc5606d | 3780 | |
dfc5606d | 3781 | mutex_unlock(&ctl_mutex); |
cd789ab9 | 3782 | |
dfc5606d | 3783 | return ret; |
602adf40 YS |
3784 | } |
3785 | ||
dfc5606d YS |
3786 | static void rbd_bus_del_dev(struct rbd_device *rbd_dev) |
3787 | { | |
3788 | device_unregister(&rbd_dev->dev); | |
3789 | } | |
3790 | ||
e2839308 | 3791 | static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0); |
1ddbe94e AE |
3792 | |
3793 | /* | |
499afd5b AE |
3794 | * Get a unique rbd identifier for the given new rbd_dev, and add |
3795 | * the rbd_dev to the global list. The minimum rbd id is 1. | |
1ddbe94e | 3796 | */ |
e2839308 | 3797 | static void rbd_dev_id_get(struct rbd_device *rbd_dev) |
b7f23c36 | 3798 | { |
e2839308 | 3799 | rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max); |
499afd5b AE |
3800 | |
3801 | spin_lock(&rbd_dev_list_lock); | |
3802 | list_add_tail(&rbd_dev->node, &rbd_dev_list); | |
3803 | spin_unlock(&rbd_dev_list_lock); | |
e2839308 AE |
3804 | dout("rbd_dev %p given dev id %llu\n", rbd_dev, |
3805 | (unsigned long long) rbd_dev->dev_id); | |
1ddbe94e | 3806 | } |
b7f23c36 | 3807 | |
1ddbe94e | 3808 | /* |
499afd5b AE |
3809 | * Remove an rbd_dev from the global list, and record that its |
3810 | * identifier is no longer in use. | |
1ddbe94e | 3811 | */ |
e2839308 | 3812 | static void rbd_dev_id_put(struct rbd_device *rbd_dev) |
1ddbe94e | 3813 | { |
d184f6bf | 3814 | struct list_head *tmp; |
de71a297 | 3815 | int rbd_id = rbd_dev->dev_id; |
d184f6bf AE |
3816 | int max_id; |
3817 | ||
aafb230e | 3818 | rbd_assert(rbd_id > 0); |
499afd5b | 3819 | |
e2839308 AE |
3820 | dout("rbd_dev %p released dev id %llu\n", rbd_dev, |
3821 | (unsigned long long) rbd_dev->dev_id); | |
499afd5b AE |
3822 | spin_lock(&rbd_dev_list_lock); |
3823 | list_del_init(&rbd_dev->node); | |
d184f6bf AE |
3824 | |
3825 | /* | |
3826 | * If the id being "put" is not the current maximum, there | |
3827 | * is nothing special we need to do. | |
3828 | */ | |
e2839308 | 3829 | if (rbd_id != atomic64_read(&rbd_dev_id_max)) { |
d184f6bf AE |
3830 | spin_unlock(&rbd_dev_list_lock); |
3831 | return; | |
3832 | } | |
3833 | ||
3834 | /* | |
3835 | * We need to update the current maximum id. Search the | |
3836 | * list to find out what it is. We're more likely to find | |
3837 | * the maximum at the end, so search the list backward. | |
3838 | */ | |
3839 | max_id = 0; | |
3840 | list_for_each_prev(tmp, &rbd_dev_list) { | |
3841 | struct rbd_device *rbd_dev; | |
3842 | ||
3843 | rbd_dev = list_entry(tmp, struct rbd_device, node); | |
b213e0b1 AE |
3844 | if (rbd_dev->dev_id > max_id) |
3845 | max_id = rbd_dev->dev_id; | |
d184f6bf | 3846 | } |
499afd5b | 3847 | spin_unlock(&rbd_dev_list_lock); |
b7f23c36 | 3848 | |
1ddbe94e | 3849 | /* |
e2839308 | 3850 | * The max id could have been updated by rbd_dev_id_get(), in |
d184f6bf AE |
3851 | * which case it now accurately reflects the new maximum. |
3852 | * Be careful not to overwrite the maximum value in that | |
3853 | * case. | |
1ddbe94e | 3854 | */ |
e2839308 AE |
3855 | atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id); |
3856 | dout(" max dev id has been reset\n"); | |
b7f23c36 AE |
3857 | } |
3858 | ||
e28fff26 AE |
3859 | /* |
3860 | * Skips over white space at *buf, and updates *buf to point to the | |
3861 | * first found non-space character (if any). Returns the length of | |
593a9e7b AE |
3862 | * the token (string of non-white space characters) found. Note |
3863 | * that *buf must be terminated with '\0'. | |
e28fff26 AE |
3864 | */ |
3865 | static inline size_t next_token(const char **buf) | |
3866 | { | |
3867 | /* | |
3868 | * These are the characters that produce nonzero for | |
3869 | * isspace() in the "C" and "POSIX" locales. | |
3870 | */ | |
3871 | const char *spaces = " \f\n\r\t\v"; | |
3872 | ||
3873 | *buf += strspn(*buf, spaces); /* Find start of token */ | |
3874 | ||
3875 | return strcspn(*buf, spaces); /* Return token length */ | |
3876 | } | |
3877 | ||
3878 | /* | |
3879 | * Finds the next token in *buf, and if the provided token buffer is | |
3880 | * big enough, copies the found token into it. The result, if | |
593a9e7b AE |
3881 | * copied, is guaranteed to be terminated with '\0'. Note that *buf |
3882 | * must be terminated with '\0' on entry. | |
e28fff26 AE |
3883 | * |
3884 | * Returns the length of the token found (not including the '\0'). | |
3885 | * Return value will be 0 if no token is found, and it will be >= | |
3886 | * token_size if the token would not fit. | |
3887 | * | |
593a9e7b | 3888 | * The *buf pointer will be updated to point beyond the end of the |
e28fff26 AE |
3889 | * found token. Note that this occurs even if the token buffer is |
3890 | * too small to hold it. | |
3891 | */ | |
3892 | static inline size_t copy_token(const char **buf, | |
3893 | char *token, | |
3894 | size_t token_size) | |
3895 | { | |
3896 | size_t len; | |
3897 | ||
3898 | len = next_token(buf); | |
3899 | if (len < token_size) { | |
3900 | memcpy(token, *buf, len); | |
3901 | *(token + len) = '\0'; | |
3902 | } | |
3903 | *buf += len; | |
3904 | ||
3905 | return len; | |
3906 | } | |
3907 | ||
ea3352f4 AE |
3908 | /* |
3909 | * Finds the next token in *buf, dynamically allocates a buffer big | |
3910 | * enough to hold a copy of it, and copies the token into the new | |
3911 | * buffer. The copy is guaranteed to be terminated with '\0'. Note | |
3912 | * that a duplicate buffer is created even for a zero-length token. | |
3913 | * | |
3914 | * Returns a pointer to the newly-allocated duplicate, or a null | |
3915 | * pointer if memory for the duplicate was not available. If | |
3916 | * the lenp argument is a non-null pointer, the length of the token | |
3917 | * (not including the '\0') is returned in *lenp. | |
3918 | * | |
3919 | * If successful, the *buf pointer will be updated to point beyond | |
3920 | * the end of the found token. | |
3921 | * | |
3922 | * Note: uses GFP_KERNEL for allocation. | |
3923 | */ | |
3924 | static inline char *dup_token(const char **buf, size_t *lenp) | |
3925 | { | |
3926 | char *dup; | |
3927 | size_t len; | |
3928 | ||
3929 | len = next_token(buf); | |
4caf35f9 | 3930 | dup = kmemdup(*buf, len + 1, GFP_KERNEL); |
ea3352f4 AE |
3931 | if (!dup) |
3932 | return NULL; | |
ea3352f4 AE |
3933 | *(dup + len) = '\0'; |
3934 | *buf += len; | |
3935 | ||
3936 | if (lenp) | |
3937 | *lenp = len; | |
3938 | ||
3939 | return dup; | |
3940 | } | |
3941 | ||
a725f65e | 3942 | /* |
859c31df AE |
3943 | * Parse the options provided for an "rbd add" (i.e., rbd image |
3944 | * mapping) request. These arrive via a write to /sys/bus/rbd/add, | |
3945 | * and the data written is passed here via a NUL-terminated buffer. | |
3946 | * Returns 0 if successful or an error code otherwise. | |
d22f76e7 | 3947 | * |
859c31df AE |
3948 | * The information extracted from these options is recorded in |
3949 | * the other parameters which return dynamically-allocated | |
3950 | * structures: | |
3951 | * ceph_opts | |
3952 | * The address of a pointer that will refer to a ceph options | |
3953 | * structure. Caller must release the returned pointer using | |
3954 | * ceph_destroy_options() when it is no longer needed. | |
3955 | * rbd_opts | |
3956 | * Address of an rbd options pointer. Fully initialized by | |
3957 | * this function; caller must release with kfree(). | |
3958 | * spec | |
3959 | * Address of an rbd image specification pointer. Fully | |
3960 | * initialized by this function based on parsed options. | |
3961 | * Caller must release with rbd_spec_put(). | |
3962 | * | |
3963 | * The options passed take this form: | |
3964 | * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>] | |
3965 | * where: | |
3966 | * <mon_addrs> | |
3967 | * A comma-separated list of one or more monitor addresses. | |
3968 | * A monitor address is an ip address, optionally followed | |
3969 | * by a port number (separated by a colon). | |
3970 | * I.e.: ip1[:port1][,ip2[:port2]...] | |
3971 | * <options> | |
3972 | * A comma-separated list of ceph and/or rbd options. | |
3973 | * <pool_name> | |
3974 | * The name of the rados pool containing the rbd image. | |
3975 | * <image_name> | |
3976 | * The name of the image in that pool to map. | |
3977 | * <snap_id> | |
3978 | * An optional snapshot id. If provided, the mapping will | |
3979 | * present data from the image at the time that snapshot was | |
3980 | * created. The image head is used if no snapshot id is | |
3981 | * provided. Snapshot mappings are always read-only. | |
a725f65e | 3982 | */ |
859c31df | 3983 | static int rbd_add_parse_args(const char *buf, |
dc79b113 | 3984 | struct ceph_options **ceph_opts, |
859c31df AE |
3985 | struct rbd_options **opts, |
3986 | struct rbd_spec **rbd_spec) | |
e28fff26 | 3987 | { |
d22f76e7 | 3988 | size_t len; |
859c31df | 3989 | char *options; |
0ddebc0c AE |
3990 | const char *mon_addrs; |
3991 | size_t mon_addrs_size; | |
859c31df | 3992 | struct rbd_spec *spec = NULL; |
4e9afeba | 3993 | struct rbd_options *rbd_opts = NULL; |
859c31df | 3994 | struct ceph_options *copts; |
dc79b113 | 3995 | int ret; |
e28fff26 AE |
3996 | |
3997 | /* The first four tokens are required */ | |
3998 | ||
7ef3214a | 3999 | len = next_token(&buf); |
4fb5d671 AE |
4000 | if (!len) { |
4001 | rbd_warn(NULL, "no monitor address(es) provided"); | |
4002 | return -EINVAL; | |
4003 | } | |
0ddebc0c | 4004 | mon_addrs = buf; |
f28e565a | 4005 | mon_addrs_size = len + 1; |
7ef3214a | 4006 | buf += len; |
a725f65e | 4007 | |
dc79b113 | 4008 | ret = -EINVAL; |
f28e565a AE |
4009 | options = dup_token(&buf, NULL); |
4010 | if (!options) | |
dc79b113 | 4011 | return -ENOMEM; |
4fb5d671 AE |
4012 | if (!*options) { |
4013 | rbd_warn(NULL, "no options provided"); | |
4014 | goto out_err; | |
4015 | } | |
e28fff26 | 4016 | |
859c31df AE |
4017 | spec = rbd_spec_alloc(); |
4018 | if (!spec) | |
f28e565a | 4019 | goto out_mem; |
859c31df AE |
4020 | |
4021 | spec->pool_name = dup_token(&buf, NULL); | |
4022 | if (!spec->pool_name) | |
4023 | goto out_mem; | |
4fb5d671 AE |
4024 | if (!*spec->pool_name) { |
4025 | rbd_warn(NULL, "no pool name provided"); | |
4026 | goto out_err; | |
4027 | } | |
e28fff26 | 4028 | |
69e7a02f | 4029 | spec->image_name = dup_token(&buf, NULL); |
859c31df | 4030 | if (!spec->image_name) |
f28e565a | 4031 | goto out_mem; |
4fb5d671 AE |
4032 | if (!*spec->image_name) { |
4033 | rbd_warn(NULL, "no image name provided"); | |
4034 | goto out_err; | |
4035 | } | |
d4b125e9 | 4036 | |
f28e565a AE |
4037 | /* |
4038 | * Snapshot name is optional; default is to use "-" | |
4039 | * (indicating the head/no snapshot). | |
4040 | */ | |
3feeb894 | 4041 | len = next_token(&buf); |
820a5f3e | 4042 | if (!len) { |
3feeb894 AE |
4043 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
4044 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; | |
f28e565a | 4045 | } else if (len > RBD_MAX_SNAP_NAME_LEN) { |
dc79b113 | 4046 | ret = -ENAMETOOLONG; |
f28e565a | 4047 | goto out_err; |
849b4260 | 4048 | } |
4caf35f9 | 4049 | spec->snap_name = kmemdup(buf, len + 1, GFP_KERNEL); |
859c31df | 4050 | if (!spec->snap_name) |
f28e565a | 4051 | goto out_mem; |
859c31df | 4052 | *(spec->snap_name + len) = '\0'; |
e5c35534 | 4053 | |
0ddebc0c | 4054 | /* Initialize all rbd options to the defaults */ |
e28fff26 | 4055 | |
4e9afeba AE |
4056 | rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL); |
4057 | if (!rbd_opts) | |
4058 | goto out_mem; | |
4059 | ||
4060 | rbd_opts->read_only = RBD_READ_ONLY_DEFAULT; | |
d22f76e7 | 4061 | |
859c31df | 4062 | copts = ceph_parse_options(options, mon_addrs, |
0ddebc0c | 4063 | mon_addrs + mon_addrs_size - 1, |
4e9afeba | 4064 | parse_rbd_opts_token, rbd_opts); |
859c31df AE |
4065 | if (IS_ERR(copts)) { |
4066 | ret = PTR_ERR(copts); | |
dc79b113 AE |
4067 | goto out_err; |
4068 | } | |
859c31df AE |
4069 | kfree(options); |
4070 | ||
4071 | *ceph_opts = copts; | |
4e9afeba | 4072 | *opts = rbd_opts; |
859c31df | 4073 | *rbd_spec = spec; |
0ddebc0c | 4074 | |
dc79b113 | 4075 | return 0; |
f28e565a | 4076 | out_mem: |
dc79b113 | 4077 | ret = -ENOMEM; |
d22f76e7 | 4078 | out_err: |
859c31df AE |
4079 | kfree(rbd_opts); |
4080 | rbd_spec_put(spec); | |
f28e565a | 4081 | kfree(options); |
d22f76e7 | 4082 | |
dc79b113 | 4083 | return ret; |
a725f65e AE |
4084 | } |
4085 | ||
589d30e0 AE |
4086 | /* |
4087 | * An rbd format 2 image has a unique identifier, distinct from the | |
4088 | * name given to it by the user. Internally, that identifier is | |
4089 | * what's used to specify the names of objects related to the image. | |
4090 | * | |
4091 | * A special "rbd id" object is used to map an rbd image name to its | |
4092 | * id. If that object doesn't exist, then there is no v2 rbd image | |
4093 | * with the supplied name. | |
4094 | * | |
4095 | * This function will record the given rbd_dev's image_id field if | |
4096 | * it can be determined, and in that case will return 0. If any | |
4097 | * errors occur a negative errno will be returned and the rbd_dev's | |
4098 | * image_id field will be unchanged (and should be NULL). | |
4099 | */ | |
4100 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) | |
4101 | { | |
4102 | int ret; | |
4103 | size_t size; | |
4104 | char *object_name; | |
4105 | void *response; | |
4106 | void *p; | |
4107 | ||
2f82ee54 AE |
4108 | /* If we already have it we don't need to look it up */ |
4109 | ||
4110 | if (rbd_dev->spec->image_id) | |
4111 | return 0; | |
4112 | ||
2c0d0a10 AE |
4113 | /* |
4114 | * When probing a parent image, the image id is already | |
4115 | * known (and the image name likely is not). There's no | |
4116 | * need to fetch the image id again in this case. | |
4117 | */ | |
4118 | if (rbd_dev->spec->image_id) | |
4119 | return 0; | |
4120 | ||
589d30e0 AE |
4121 | /* |
4122 | * First, see if the format 2 image id file exists, and if | |
4123 | * so, get the image's persistent id from it. | |
4124 | */ | |
69e7a02f | 4125 | size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name); |
589d30e0 AE |
4126 | object_name = kmalloc(size, GFP_NOIO); |
4127 | if (!object_name) | |
4128 | return -ENOMEM; | |
0d7dbfce | 4129 | sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name); |
589d30e0 AE |
4130 | dout("rbd id object name is %s\n", object_name); |
4131 | ||
4132 | /* Response will be an encoded string, which includes a length */ | |
4133 | ||
4134 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; | |
4135 | response = kzalloc(size, GFP_NOIO); | |
4136 | if (!response) { | |
4137 | ret = -ENOMEM; | |
4138 | goto out; | |
4139 | } | |
4140 | ||
36be9a76 | 4141 | ret = rbd_obj_method_sync(rbd_dev, object_name, |
589d30e0 AE |
4142 | "rbd", "get_id", |
4143 | NULL, 0, | |
07b2391f | 4144 | response, RBD_IMAGE_ID_LEN_MAX, NULL); |
36be9a76 | 4145 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
589d30e0 AE |
4146 | if (ret < 0) |
4147 | goto out; | |
4148 | ||
4149 | p = response; | |
0d7dbfce | 4150 | rbd_dev->spec->image_id = ceph_extract_encoded_string(&p, |
589d30e0 | 4151 | p + RBD_IMAGE_ID_LEN_MAX, |
979ed480 | 4152 | NULL, GFP_NOIO); |
0d7dbfce AE |
4153 | if (IS_ERR(rbd_dev->spec->image_id)) { |
4154 | ret = PTR_ERR(rbd_dev->spec->image_id); | |
4155 | rbd_dev->spec->image_id = NULL; | |
589d30e0 | 4156 | } else { |
0d7dbfce | 4157 | dout("image_id is %s\n", rbd_dev->spec->image_id); |
589d30e0 AE |
4158 | } |
4159 | out: | |
4160 | kfree(response); | |
4161 | kfree(object_name); | |
4162 | ||
4163 | return ret; | |
4164 | } | |
4165 | ||
a30b71b9 AE |
4166 | static int rbd_dev_v1_probe(struct rbd_device *rbd_dev) |
4167 | { | |
4168 | int ret; | |
4169 | size_t size; | |
4170 | ||
4171 | /* Version 1 images have no id; empty string is used */ | |
4172 | ||
0d7dbfce AE |
4173 | rbd_dev->spec->image_id = kstrdup("", GFP_KERNEL); |
4174 | if (!rbd_dev->spec->image_id) | |
a30b71b9 | 4175 | return -ENOMEM; |
a30b71b9 AE |
4176 | |
4177 | /* Record the header object name for this rbd image. */ | |
4178 | ||
69e7a02f | 4179 | size = strlen(rbd_dev->spec->image_name) + sizeof (RBD_SUFFIX); |
a30b71b9 AE |
4180 | rbd_dev->header_name = kmalloc(size, GFP_KERNEL); |
4181 | if (!rbd_dev->header_name) { | |
4182 | ret = -ENOMEM; | |
4183 | goto out_err; | |
4184 | } | |
0d7dbfce AE |
4185 | sprintf(rbd_dev->header_name, "%s%s", |
4186 | rbd_dev->spec->image_name, RBD_SUFFIX); | |
a30b71b9 AE |
4187 | |
4188 | /* Populate rbd image metadata */ | |
4189 | ||
4190 | ret = rbd_read_header(rbd_dev, &rbd_dev->header); | |
4191 | if (ret < 0) | |
4192 | goto out_err; | |
86b00e0d AE |
4193 | |
4194 | /* Version 1 images have no parent (no layering) */ | |
4195 | ||
4196 | rbd_dev->parent_spec = NULL; | |
4197 | rbd_dev->parent_overlap = 0; | |
4198 | ||
a30b71b9 AE |
4199 | rbd_dev->image_format = 1; |
4200 | ||
4201 | dout("discovered version 1 image, header name is %s\n", | |
4202 | rbd_dev->header_name); | |
4203 | ||
4204 | return 0; | |
4205 | ||
4206 | out_err: | |
4207 | kfree(rbd_dev->header_name); | |
4208 | rbd_dev->header_name = NULL; | |
0d7dbfce AE |
4209 | kfree(rbd_dev->spec->image_id); |
4210 | rbd_dev->spec->image_id = NULL; | |
a30b71b9 AE |
4211 | |
4212 | return ret; | |
4213 | } | |
4214 | ||
4215 | static int rbd_dev_v2_probe(struct rbd_device *rbd_dev) | |
4216 | { | |
4217 | size_t size; | |
9d475de5 | 4218 | int ret; |
6e14b1a6 | 4219 | u64 ver = 0; |
a30b71b9 AE |
4220 | |
4221 | /* | |
4222 | * Image id was filled in by the caller. Record the header | |
4223 | * object name for this rbd image. | |
4224 | */ | |
979ed480 | 4225 | size = sizeof (RBD_HEADER_PREFIX) + strlen(rbd_dev->spec->image_id); |
a30b71b9 AE |
4226 | rbd_dev->header_name = kmalloc(size, GFP_KERNEL); |
4227 | if (!rbd_dev->header_name) | |
4228 | return -ENOMEM; | |
4229 | sprintf(rbd_dev->header_name, "%s%s", | |
0d7dbfce | 4230 | RBD_HEADER_PREFIX, rbd_dev->spec->image_id); |
9d475de5 AE |
4231 | |
4232 | /* Get the size and object order for the image */ | |
4233 | ||
4234 | ret = rbd_dev_v2_image_size(rbd_dev); | |
1e130199 AE |
4235 | if (ret < 0) |
4236 | goto out_err; | |
4237 | ||
4238 | /* Get the object prefix (a.k.a. block_name) for the image */ | |
4239 | ||
4240 | ret = rbd_dev_v2_object_prefix(rbd_dev); | |
b1b5402a AE |
4241 | if (ret < 0) |
4242 | goto out_err; | |
4243 | ||
d889140c | 4244 | /* Get the and check features for the image */ |
b1b5402a AE |
4245 | |
4246 | ret = rbd_dev_v2_features(rbd_dev); | |
9d475de5 AE |
4247 | if (ret < 0) |
4248 | goto out_err; | |
35d489f9 | 4249 | |
86b00e0d AE |
4250 | /* If the image supports layering, get the parent info */ |
4251 | ||
4252 | if (rbd_dev->header.features & RBD_FEATURE_LAYERING) { | |
4253 | ret = rbd_dev_v2_parent_info(rbd_dev); | |
4254 | if (ret < 0) | |
4255 | goto out_err; | |
4256 | } | |
4257 | ||
6e14b1a6 AE |
4258 | /* crypto and compression type aren't (yet) supported for v2 images */ |
4259 | ||
4260 | rbd_dev->header.crypt_type = 0; | |
4261 | rbd_dev->header.comp_type = 0; | |
35d489f9 | 4262 | |
6e14b1a6 AE |
4263 | /* Get the snapshot context, plus the header version */ |
4264 | ||
4265 | ret = rbd_dev_v2_snap_context(rbd_dev, &ver); | |
35d489f9 AE |
4266 | if (ret) |
4267 | goto out_err; | |
6e14b1a6 AE |
4268 | rbd_dev->header.obj_version = ver; |
4269 | ||
a30b71b9 AE |
4270 | rbd_dev->image_format = 2; |
4271 | ||
4272 | dout("discovered version 2 image, header name is %s\n", | |
4273 | rbd_dev->header_name); | |
4274 | ||
35152979 | 4275 | return 0; |
9d475de5 | 4276 | out_err: |
86b00e0d AE |
4277 | rbd_dev->parent_overlap = 0; |
4278 | rbd_spec_put(rbd_dev->parent_spec); | |
4279 | rbd_dev->parent_spec = NULL; | |
9d475de5 AE |
4280 | kfree(rbd_dev->header_name); |
4281 | rbd_dev->header_name = NULL; | |
1e130199 AE |
4282 | kfree(rbd_dev->header.object_prefix); |
4283 | rbd_dev->header.object_prefix = NULL; | |
9d475de5 AE |
4284 | |
4285 | return ret; | |
a30b71b9 AE |
4286 | } |
4287 | ||
83a06263 AE |
4288 | static int rbd_dev_probe_finish(struct rbd_device *rbd_dev) |
4289 | { | |
2f82ee54 AE |
4290 | struct rbd_device *parent = NULL; |
4291 | struct rbd_spec *parent_spec = NULL; | |
4292 | struct rbd_client *rbdc = NULL; | |
83a06263 AE |
4293 | int ret; |
4294 | ||
4295 | /* no need to lock here, as rbd_dev is not registered yet */ | |
4296 | ret = rbd_dev_snaps_update(rbd_dev); | |
4297 | if (ret) | |
4298 | return ret; | |
4299 | ||
9e15b77d AE |
4300 | ret = rbd_dev_probe_update_spec(rbd_dev); |
4301 | if (ret) | |
4302 | goto err_out_snaps; | |
4303 | ||
83a06263 AE |
4304 | ret = rbd_dev_set_mapping(rbd_dev); |
4305 | if (ret) | |
4306 | goto err_out_snaps; | |
4307 | ||
4308 | /* generate unique id: find highest unique id, add one */ | |
4309 | rbd_dev_id_get(rbd_dev); | |
4310 | ||
4311 | /* Fill in the device name, now that we have its id. */ | |
4312 | BUILD_BUG_ON(DEV_NAME_LEN | |
4313 | < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH); | |
4314 | sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id); | |
4315 | ||
4316 | /* Get our block major device number. */ | |
4317 | ||
4318 | ret = register_blkdev(0, rbd_dev->name); | |
4319 | if (ret < 0) | |
4320 | goto err_out_id; | |
4321 | rbd_dev->major = ret; | |
4322 | ||
4323 | /* Set up the blkdev mapping. */ | |
4324 | ||
4325 | ret = rbd_init_disk(rbd_dev); | |
4326 | if (ret) | |
4327 | goto err_out_blkdev; | |
4328 | ||
4329 | ret = rbd_bus_add_dev(rbd_dev); | |
4330 | if (ret) | |
4331 | goto err_out_disk; | |
4332 | ||
4333 | /* | |
4334 | * At this point cleanup in the event of an error is the job | |
4335 | * of the sysfs code (initiated by rbd_bus_del_dev()). | |
4336 | */ | |
2f82ee54 AE |
4337 | /* Probe the parent if there is one */ |
4338 | ||
4339 | if (rbd_dev->parent_spec) { | |
4340 | /* | |
4341 | * We need to pass a reference to the client and the | |
4342 | * parent spec when creating the parent rbd_dev. | |
4343 | * Images related by parent/child relationships | |
4344 | * always share both. | |
4345 | */ | |
4346 | parent_spec = rbd_spec_get(rbd_dev->parent_spec); | |
4347 | rbdc = __rbd_get_client(rbd_dev->rbd_client); | |
4348 | ||
4349 | parent = rbd_dev_create(rbdc, parent_spec); | |
4350 | if (!parent) { | |
4351 | ret = -ENOMEM; | |
4352 | goto err_out_spec; | |
4353 | } | |
4354 | rbdc = NULL; /* parent now owns reference */ | |
4355 | parent_spec = NULL; /* parent now owns reference */ | |
4356 | ret = rbd_dev_probe(parent); | |
4357 | if (ret < 0) | |
4358 | goto err_out_parent; | |
4359 | rbd_dev->parent = parent; | |
4360 | } | |
4361 | ||
83a06263 AE |
4362 | down_write(&rbd_dev->header_rwsem); |
4363 | ret = rbd_dev_snaps_register(rbd_dev); | |
4364 | up_write(&rbd_dev->header_rwsem); | |
4365 | if (ret) | |
4366 | goto err_out_bus; | |
4367 | ||
9969ebc5 | 4368 | ret = rbd_dev_header_watch_sync(rbd_dev, 1); |
83a06263 AE |
4369 | if (ret) |
4370 | goto err_out_bus; | |
4371 | ||
4372 | /* Everything's ready. Announce the disk to the world. */ | |
4373 | ||
4374 | add_disk(rbd_dev->disk); | |
4375 | ||
4376 | pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name, | |
4377 | (unsigned long long) rbd_dev->mapping.size); | |
4378 | ||
4379 | return ret; | |
2f82ee54 AE |
4380 | |
4381 | err_out_parent: | |
4382 | rbd_dev_destroy(parent); | |
4383 | err_out_spec: | |
4384 | rbd_spec_put(parent_spec); | |
4385 | rbd_put_client(rbdc); | |
83a06263 AE |
4386 | err_out_bus: |
4387 | /* this will also clean up rest of rbd_dev stuff */ | |
4388 | ||
4389 | rbd_bus_del_dev(rbd_dev); | |
4390 | ||
4391 | return ret; | |
4392 | err_out_disk: | |
4393 | rbd_free_disk(rbd_dev); | |
4394 | err_out_blkdev: | |
4395 | unregister_blkdev(rbd_dev->major, rbd_dev->name); | |
4396 | err_out_id: | |
4397 | rbd_dev_id_put(rbd_dev); | |
4398 | err_out_snaps: | |
4399 | rbd_remove_all_snaps(rbd_dev); | |
4400 | ||
4401 | return ret; | |
4402 | } | |
4403 | ||
a30b71b9 AE |
4404 | /* |
4405 | * Probe for the existence of the header object for the given rbd | |
4406 | * device. For format 2 images this includes determining the image | |
4407 | * id. | |
4408 | */ | |
4409 | static int rbd_dev_probe(struct rbd_device *rbd_dev) | |
4410 | { | |
4411 | int ret; | |
4412 | ||
4413 | /* | |
4414 | * Get the id from the image id object. If it's not a | |
4415 | * format 2 image, we'll get ENOENT back, and we'll assume | |
4416 | * it's a format 1 image. | |
4417 | */ | |
4418 | ret = rbd_dev_image_id(rbd_dev); | |
4419 | if (ret) | |
4420 | ret = rbd_dev_v1_probe(rbd_dev); | |
4421 | else | |
4422 | ret = rbd_dev_v2_probe(rbd_dev); | |
83a06263 | 4423 | if (ret) { |
a30b71b9 AE |
4424 | dout("probe failed, returning %d\n", ret); |
4425 | ||
83a06263 AE |
4426 | return ret; |
4427 | } | |
4428 | ||
4429 | ret = rbd_dev_probe_finish(rbd_dev); | |
4430 | if (ret) | |
4431 | rbd_header_free(&rbd_dev->header); | |
4432 | ||
a30b71b9 AE |
4433 | return ret; |
4434 | } | |
4435 | ||
59c2be1e YS |
4436 | static ssize_t rbd_add(struct bus_type *bus, |
4437 | const char *buf, | |
4438 | size_t count) | |
602adf40 | 4439 | { |
cb8627c7 | 4440 | struct rbd_device *rbd_dev = NULL; |
dc79b113 | 4441 | struct ceph_options *ceph_opts = NULL; |
4e9afeba | 4442 | struct rbd_options *rbd_opts = NULL; |
859c31df | 4443 | struct rbd_spec *spec = NULL; |
9d3997fd | 4444 | struct rbd_client *rbdc; |
27cc2594 AE |
4445 | struct ceph_osd_client *osdc; |
4446 | int rc = -ENOMEM; | |
602adf40 YS |
4447 | |
4448 | if (!try_module_get(THIS_MODULE)) | |
4449 | return -ENODEV; | |
4450 | ||
602adf40 | 4451 | /* parse add command */ |
859c31df | 4452 | rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec); |
dc79b113 | 4453 | if (rc < 0) |
bd4ba655 | 4454 | goto err_out_module; |
78cea76e | 4455 | |
9d3997fd AE |
4456 | rbdc = rbd_get_client(ceph_opts); |
4457 | if (IS_ERR(rbdc)) { | |
4458 | rc = PTR_ERR(rbdc); | |
0ddebc0c | 4459 | goto err_out_args; |
9d3997fd | 4460 | } |
c53d5893 | 4461 | ceph_opts = NULL; /* rbd_dev client now owns this */ |
602adf40 | 4462 | |
602adf40 | 4463 | /* pick the pool */ |
9d3997fd | 4464 | osdc = &rbdc->client->osdc; |
859c31df | 4465 | rc = ceph_pg_poolid_by_name(osdc->osdmap, spec->pool_name); |
602adf40 YS |
4466 | if (rc < 0) |
4467 | goto err_out_client; | |
859c31df AE |
4468 | spec->pool_id = (u64) rc; |
4469 | ||
0903e875 AE |
4470 | /* The ceph file layout needs to fit pool id in 32 bits */ |
4471 | ||
4472 | if (WARN_ON(spec->pool_id > (u64) U32_MAX)) { | |
4473 | rc = -EIO; | |
4474 | goto err_out_client; | |
4475 | } | |
4476 | ||
c53d5893 | 4477 | rbd_dev = rbd_dev_create(rbdc, spec); |
bd4ba655 AE |
4478 | if (!rbd_dev) |
4479 | goto err_out_client; | |
c53d5893 AE |
4480 | rbdc = NULL; /* rbd_dev now owns this */ |
4481 | spec = NULL; /* rbd_dev now owns this */ | |
602adf40 | 4482 | |
bd4ba655 | 4483 | rbd_dev->mapping.read_only = rbd_opts->read_only; |
c53d5893 AE |
4484 | kfree(rbd_opts); |
4485 | rbd_opts = NULL; /* done with this */ | |
bd4ba655 | 4486 | |
a30b71b9 AE |
4487 | rc = rbd_dev_probe(rbd_dev); |
4488 | if (rc < 0) | |
c53d5893 | 4489 | goto err_out_rbd_dev; |
05fd6f6f | 4490 | |
602adf40 | 4491 | return count; |
c53d5893 AE |
4492 | err_out_rbd_dev: |
4493 | rbd_dev_destroy(rbd_dev); | |
bd4ba655 | 4494 | err_out_client: |
9d3997fd | 4495 | rbd_put_client(rbdc); |
0ddebc0c | 4496 | err_out_args: |
78cea76e AE |
4497 | if (ceph_opts) |
4498 | ceph_destroy_options(ceph_opts); | |
4e9afeba | 4499 | kfree(rbd_opts); |
859c31df | 4500 | rbd_spec_put(spec); |
bd4ba655 AE |
4501 | err_out_module: |
4502 | module_put(THIS_MODULE); | |
27cc2594 | 4503 | |
602adf40 | 4504 | dout("Error adding device %s\n", buf); |
27cc2594 AE |
4505 | |
4506 | return (ssize_t) rc; | |
602adf40 YS |
4507 | } |
4508 | ||
de71a297 | 4509 | static struct rbd_device *__rbd_get_dev(unsigned long dev_id) |
602adf40 YS |
4510 | { |
4511 | struct list_head *tmp; | |
4512 | struct rbd_device *rbd_dev; | |
4513 | ||
e124a82f | 4514 | spin_lock(&rbd_dev_list_lock); |
602adf40 YS |
4515 | list_for_each(tmp, &rbd_dev_list) { |
4516 | rbd_dev = list_entry(tmp, struct rbd_device, node); | |
de71a297 | 4517 | if (rbd_dev->dev_id == dev_id) { |
e124a82f | 4518 | spin_unlock(&rbd_dev_list_lock); |
602adf40 | 4519 | return rbd_dev; |
e124a82f | 4520 | } |
602adf40 | 4521 | } |
e124a82f | 4522 | spin_unlock(&rbd_dev_list_lock); |
602adf40 YS |
4523 | return NULL; |
4524 | } | |
4525 | ||
dfc5606d | 4526 | static void rbd_dev_release(struct device *dev) |
602adf40 | 4527 | { |
593a9e7b | 4528 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
602adf40 | 4529 | |
59c2be1e | 4530 | if (rbd_dev->watch_event) |
9969ebc5 | 4531 | rbd_dev_header_watch_sync(rbd_dev, 0); |
602adf40 YS |
4532 | |
4533 | /* clean up and free blkdev */ | |
4534 | rbd_free_disk(rbd_dev); | |
4535 | unregister_blkdev(rbd_dev->major, rbd_dev->name); | |
32eec68d | 4536 | |
2ac4e75d AE |
4537 | /* release allocated disk header fields */ |
4538 | rbd_header_free(&rbd_dev->header); | |
4539 | ||
32eec68d | 4540 | /* done with the id, and with the rbd_dev */ |
e2839308 | 4541 | rbd_dev_id_put(rbd_dev); |
c53d5893 AE |
4542 | rbd_assert(rbd_dev->rbd_client != NULL); |
4543 | rbd_dev_destroy(rbd_dev); | |
602adf40 YS |
4544 | |
4545 | /* release module ref */ | |
4546 | module_put(THIS_MODULE); | |
602adf40 YS |
4547 | } |
4548 | ||
2f82ee54 AE |
4549 | static void __rbd_remove(struct rbd_device *rbd_dev) |
4550 | { | |
4551 | rbd_remove_all_snaps(rbd_dev); | |
4552 | rbd_bus_del_dev(rbd_dev); | |
4553 | } | |
4554 | ||
dfc5606d YS |
4555 | static ssize_t rbd_remove(struct bus_type *bus, |
4556 | const char *buf, | |
4557 | size_t count) | |
602adf40 YS |
4558 | { |
4559 | struct rbd_device *rbd_dev = NULL; | |
4560 | int target_id, rc; | |
4561 | unsigned long ul; | |
4562 | int ret = count; | |
4563 | ||
4564 | rc = strict_strtoul(buf, 10, &ul); | |
4565 | if (rc) | |
4566 | return rc; | |
4567 | ||
4568 | /* convert to int; abort if we lost anything in the conversion */ | |
4569 | target_id = (int) ul; | |
4570 | if (target_id != ul) | |
4571 | return -EINVAL; | |
4572 | ||
4573 | mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); | |
4574 | ||
4575 | rbd_dev = __rbd_get_dev(target_id); | |
4576 | if (!rbd_dev) { | |
4577 | ret = -ENOENT; | |
4578 | goto done; | |
42382b70 AE |
4579 | } |
4580 | ||
a14ea269 | 4581 | spin_lock_irq(&rbd_dev->lock); |
b82d167b | 4582 | if (rbd_dev->open_count) |
42382b70 | 4583 | ret = -EBUSY; |
b82d167b AE |
4584 | else |
4585 | set_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags); | |
a14ea269 | 4586 | spin_unlock_irq(&rbd_dev->lock); |
b82d167b | 4587 | if (ret < 0) |
42382b70 | 4588 | goto done; |
602adf40 | 4589 | |
2f82ee54 AE |
4590 | while (rbd_dev->parent_spec) { |
4591 | struct rbd_device *first = rbd_dev; | |
4592 | struct rbd_device *second = first->parent; | |
4593 | struct rbd_device *third; | |
4594 | ||
4595 | /* | |
4596 | * Follow to the parent with no grandparent and | |
4597 | * remove it. | |
4598 | */ | |
4599 | while (second && (third = second->parent)) { | |
4600 | first = second; | |
4601 | second = third; | |
4602 | } | |
4603 | __rbd_remove(second); | |
4604 | rbd_spec_put(first->parent_spec); | |
4605 | first->parent_spec = NULL; | |
4606 | first->parent_overlap = 0; | |
4607 | first->parent = NULL; | |
4608 | } | |
4609 | __rbd_remove(rbd_dev); | |
602adf40 YS |
4610 | |
4611 | done: | |
4612 | mutex_unlock(&ctl_mutex); | |
aafb230e | 4613 | |
602adf40 YS |
4614 | return ret; |
4615 | } | |
4616 | ||
602adf40 YS |
4617 | /* |
4618 | * create control files in sysfs | |
dfc5606d | 4619 | * /sys/bus/rbd/... |
602adf40 YS |
4620 | */ |
4621 | static int rbd_sysfs_init(void) | |
4622 | { | |
dfc5606d | 4623 | int ret; |
602adf40 | 4624 | |
fed4c143 | 4625 | ret = device_register(&rbd_root_dev); |
21079786 | 4626 | if (ret < 0) |
dfc5606d | 4627 | return ret; |
602adf40 | 4628 | |
fed4c143 AE |
4629 | ret = bus_register(&rbd_bus_type); |
4630 | if (ret < 0) | |
4631 | device_unregister(&rbd_root_dev); | |
602adf40 | 4632 | |
602adf40 YS |
4633 | return ret; |
4634 | } | |
4635 | ||
4636 | static void rbd_sysfs_cleanup(void) | |
4637 | { | |
dfc5606d | 4638 | bus_unregister(&rbd_bus_type); |
fed4c143 | 4639 | device_unregister(&rbd_root_dev); |
602adf40 YS |
4640 | } |
4641 | ||
cc344fa1 | 4642 | static int __init rbd_init(void) |
602adf40 YS |
4643 | { |
4644 | int rc; | |
4645 | ||
1e32d34c AE |
4646 | if (!libceph_compatible(NULL)) { |
4647 | rbd_warn(NULL, "libceph incompatibility (quitting)"); | |
4648 | ||
4649 | return -EINVAL; | |
4650 | } | |
602adf40 YS |
4651 | rc = rbd_sysfs_init(); |
4652 | if (rc) | |
4653 | return rc; | |
f0f8cef5 | 4654 | pr_info("loaded " RBD_DRV_NAME_LONG "\n"); |
602adf40 YS |
4655 | return 0; |
4656 | } | |
4657 | ||
cc344fa1 | 4658 | static void __exit rbd_exit(void) |
602adf40 YS |
4659 | { |
4660 | rbd_sysfs_cleanup(); | |
4661 | } | |
4662 | ||
4663 | module_init(rbd_init); | |
4664 | module_exit(rbd_exit); | |
4665 | ||
4666 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); | |
4667 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); | |
4668 | MODULE_DESCRIPTION("rados block device"); | |
4669 | ||
4670 | /* following authorship retained from original osdblk.c */ | |
4671 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); | |
4672 | ||
4673 | MODULE_LICENSE("GPL"); |