1
2 /*
3 rbd.c -- Export ceph rados objects as a Linux block device
4
5
6 based on drivers/block/osdblk.c:
7
8 Copyright 2009 Red Hat, Inc.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING. If not, write to
21 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23
24
25 For usage instructions, please refer to:
26
27 Documentation/ABI/testing/sysfs-bus-rbd
28
29 */
30
31 #include <linux/ceph/libceph.h>
32 #include <linux/ceph/osd_client.h>
33 #include <linux/ceph/mon_client.h>
34 #include <linux/ceph/decode.h>
35 #include <linux/parser.h>
36 #include <linux/bsearch.h>
37
38 #include <linux/kernel.h>
39 #include <linux/device.h>
40 #include <linux/module.h>
41 #include <linux/blk-mq.h>
42 #include <linux/fs.h>
43 #include <linux/blkdev.h>
44 #include <linux/slab.h>
45 #include <linux/idr.h>
46 #include <linux/workqueue.h>
47
48 #include "rbd_types.h"
49
50 #define RBD_DEBUG /* Activate rbd_assert() calls */
51
52 /*
53 * The basic unit of block I/O is a sector. It is interpreted in a
54 * number of contexts in Linux (blk, bio, genhd), but the default is
55 * universally 512 bytes. These symbols are just slightly more
56 * meaningful than the bare numbers they represent.
57 */
58 #define SECTOR_SHIFT 9
59 #define SECTOR_SIZE (1ULL << SECTOR_SHIFT)
60
61 /*
62 * Increment the given counter and return its updated value.
63 * If the counter is already 0 it will not be incremented.
64 * If the counter is already at its maximum value returns
65 * -EINVAL without updating it.
66 */
atomic_inc_return_safe(atomic_t * v)67 static int atomic_inc_return_safe(atomic_t *v)
68 {
69 unsigned int counter;
70
71 counter = (unsigned int)__atomic_add_unless(v, 1, 0);
72 if (counter <= (unsigned int)INT_MAX)
73 return (int)counter;
74
75 atomic_dec(v);
76
77 return -EINVAL;
78 }
79
80 /* Decrement the counter. Return the resulting value, or -EINVAL */
atomic_dec_return_safe(atomic_t * v)81 static int atomic_dec_return_safe(atomic_t *v)
82 {
83 int counter;
84
85 counter = atomic_dec_return(v);
86 if (counter >= 0)
87 return counter;
88
89 atomic_inc(v);
90
91 return -EINVAL;
92 }
93
94 #define RBD_DRV_NAME "rbd"
95
96 #define RBD_MINORS_PER_MAJOR 256
97 #define RBD_SINGLE_MAJOR_PART_SHIFT 4
98
99 #define RBD_MAX_PARENT_CHAIN_LEN 16
100
101 #define RBD_SNAP_DEV_NAME_PREFIX "snap_"
102 #define RBD_MAX_SNAP_NAME_LEN \
103 (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1))
104
105 #define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
106
107 #define RBD_SNAP_HEAD_NAME "-"
108
109 #define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */
110
111 /* This allows a single page to hold an image name sent by OSD */
112 #define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1)
113 #define RBD_IMAGE_ID_LEN_MAX 64
114
115 #define RBD_OBJ_PREFIX_LEN_MAX 64
116
117 /* Feature bits */
118
119 #define RBD_FEATURE_LAYERING (1<<0)
120 #define RBD_FEATURE_STRIPINGV2 (1<<1)
121 #define RBD_FEATURES_ALL \
122 (RBD_FEATURE_LAYERING | RBD_FEATURE_STRIPINGV2)
123
124 /* Features supported by this (client software) implementation. */
125
126 #define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL)
127
128 /*
129 * An RBD device name will be "rbd#", where the "rbd" comes from
130 * RBD_DRV_NAME above, and # is a unique integer identifier.
131 * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big
132 * enough to hold all possible device names.
133 */
134 #define DEV_NAME_LEN 32
135 #define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1)
136
137 /*
138 * block device image metadata (in-memory version)
139 */
140 struct rbd_image_header {
141 /* These six fields never change for a given rbd image */
142 char *object_prefix;
143 __u8 obj_order;
144 __u8 crypt_type;
145 __u8 comp_type;
146 u64 stripe_unit;
147 u64 stripe_count;
148 u64 features; /* Might be changeable someday? */
149
150 /* The remaining fields need to be updated occasionally */
151 u64 image_size;
152 struct ceph_snap_context *snapc;
153 char *snap_names; /* format 1 only */
154 u64 *snap_sizes; /* format 1 only */
155 };
156
157 /*
158 * An rbd image specification.
159 *
160 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
161 * identify an image. Each rbd_dev structure includes a pointer to
162 * an rbd_spec structure that encapsulates this identity.
163 *
164 * Each of the id's in an rbd_spec has an associated name. For a
165 * user-mapped image, the names are supplied and the id's associated
166 * with them are looked up. For a layered image, a parent image is
167 * defined by the tuple, and the names are looked up.
168 *
169 * An rbd_dev structure contains a parent_spec pointer which is
170 * non-null if the image it represents is a child in a layered
171 * image. This pointer will refer to the rbd_spec structure used
172 * by the parent rbd_dev for its own identity (i.e., the structure
173 * is shared between the parent and child).
174 *
175 * Since these structures are populated once, during the discovery
176 * phase of image construction, they are effectively immutable so
177 * we make no effort to synchronize access to them.
178 *
179 * Note that code herein does not assume the image name is known (it
180 * could be a null pointer).
181 */
182 struct rbd_spec {
183 u64 pool_id;
184 const char *pool_name;
185
186 const char *image_id;
187 const char *image_name;
188
189 u64 snap_id;
190 const char *snap_name;
191
192 struct kref kref;
193 };
194
195 /*
196 * an instance of the client. multiple devices may share an rbd client.
197 */
198 struct rbd_client {
199 struct ceph_client *client;
200 struct kref kref;
201 struct list_head node;
202 };
203
204 struct rbd_img_request;
205 typedef void (*rbd_img_callback_t)(struct rbd_img_request *);
206
207 #define BAD_WHICH U32_MAX /* Good which or bad which, which? */
208
209 struct rbd_obj_request;
210 typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *);
211
212 enum obj_request_type {
213 OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES
214 };
215
216 enum obj_operation_type {
217 OBJ_OP_WRITE,
218 OBJ_OP_READ,
219 OBJ_OP_DISCARD,
220 };
221
222 enum obj_req_flags {
223 OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */
224 OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */
225 OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */
226 OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */
227 };
228
229 struct rbd_obj_request {
230 const char *object_name;
231 u64 offset; /* object start byte */
232 u64 length; /* bytes from offset */
233 unsigned long flags;
234
235 /*
236 * An object request associated with an image will have its
237 * img_data flag set; a standalone object request will not.
238 *
239 * A standalone object request will have which == BAD_WHICH
240 * and a null obj_request pointer.
241 *
242 * An object request initiated in support of a layered image
243 * object (to check for its existence before a write) will
244 * have which == BAD_WHICH and a non-null obj_request pointer.
245 *
246 * Finally, an object request for rbd image data will have
247 * which != BAD_WHICH, and will have a non-null img_request
248 * pointer. The value of which will be in the range
249 * 0..(img_request->obj_request_count-1).
250 */
251 union {
252 struct rbd_obj_request *obj_request; /* STAT op */
253 struct {
254 struct rbd_img_request *img_request;
255 u64 img_offset;
256 /* links for img_request->obj_requests list */
257 struct list_head links;
258 };
259 };
260 u32 which; /* posn image request list */
261
262 enum obj_request_type type;
263 union {
264 struct bio *bio_list;
265 struct {
266 struct page **pages;
267 u32 page_count;
268 };
269 };
270 struct page **copyup_pages;
271 u32 copyup_page_count;
272
273 struct ceph_osd_request *osd_req;
274
275 u64 xferred; /* bytes transferred */
276 int result;
277
278 rbd_obj_callback_t callback;
279 struct completion completion;
280
281 struct kref kref;
282 };
283
284 enum img_req_flags {
285 IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */
286 IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */
287 IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */
288 IMG_REQ_DISCARD, /* discard: normal = 0, discard request = 1 */
289 };
290
291 struct rbd_img_request {
292 struct rbd_device *rbd_dev;
293 u64 offset; /* starting image byte offset */
294 u64 length; /* byte count from offset */
295 unsigned long flags;
296 union {
297 u64 snap_id; /* for reads */
298 struct ceph_snap_context *snapc; /* for writes */
299 };
300 union {
301 struct request *rq; /* block request */
302 struct rbd_obj_request *obj_request; /* obj req initiator */
303 };
304 struct page **copyup_pages;
305 u32 copyup_page_count;
306 spinlock_t completion_lock;/* protects next_completion */
307 u32 next_completion;
308 rbd_img_callback_t callback;
309 u64 xferred;/* aggregate bytes transferred */
310 int result; /* first nonzero obj_request result */
311
312 u32 obj_request_count;
313 struct list_head obj_requests; /* rbd_obj_request structs */
314
315 struct kref kref;
316 };
317
318 #define for_each_obj_request(ireq, oreq) \
319 list_for_each_entry(oreq, &(ireq)->obj_requests, links)
320 #define for_each_obj_request_from(ireq, oreq) \
321 list_for_each_entry_from(oreq, &(ireq)->obj_requests, links)
322 #define for_each_obj_request_safe(ireq, oreq, n) \
323 list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links)
324
325 struct rbd_mapping {
326 u64 size;
327 u64 features;
328 bool read_only;
329 };
330
331 /*
332 * a single device
333 */
334 struct rbd_device {
335 int dev_id; /* blkdev unique id */
336
337 int major; /* blkdev assigned major */
338 int minor;
339 struct gendisk *disk; /* blkdev's gendisk and rq */
340
341 u32 image_format; /* Either 1 or 2 */
342 struct rbd_client *rbd_client;
343
344 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
345
346 spinlock_t lock; /* queue, flags, open_count */
347
348 struct rbd_image_header header;
349 unsigned long flags; /* possibly lock protected */
350 struct rbd_spec *spec;
351
352 char *header_name;
353
354 struct ceph_file_layout layout;
355
356 struct ceph_osd_event *watch_event;
357 struct rbd_obj_request *watch_request;
358
359 struct rbd_spec *parent_spec;
360 u64 parent_overlap;
361 atomic_t parent_ref;
362 struct rbd_device *parent;
363
364 /* Block layer tags. */
365 struct blk_mq_tag_set tag_set;
366
367 /* protects updating the header */
368 struct rw_semaphore header_rwsem;
369
370 struct rbd_mapping mapping;
371
372 struct list_head node;
373
374 /* sysfs related */
375 struct device dev;
376 unsigned long open_count; /* protected by lock */
377 };
378
379 /*
380 * Flag bits for rbd_dev->flags. If atomicity is required,
381 * rbd_dev->lock is used to protect access.
382 *
383 * Currently, only the "removing" flag (which is coupled with the
384 * "open_count" field) requires atomic access.
385 */
386 enum rbd_dev_flags {
387 RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */
388 RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */
389 };
390
391 static DEFINE_MUTEX(client_mutex); /* Serialize client creation */
392
393 static LIST_HEAD(rbd_dev_list); /* devices */
394 static DEFINE_SPINLOCK(rbd_dev_list_lock);
395
396 static LIST_HEAD(rbd_client_list); /* clients */
397 static DEFINE_SPINLOCK(rbd_client_list_lock);
398
399 /* Slab caches for frequently-allocated structures */
400
401 static struct kmem_cache *rbd_img_request_cache;
402 static struct kmem_cache *rbd_obj_request_cache;
403 static struct kmem_cache *rbd_segment_name_cache;
404
405 static int rbd_major;
406 static DEFINE_IDA(rbd_dev_id_ida);
407
408 static struct workqueue_struct *rbd_wq;
409
410 /*
411 * Default to false for now, as single-major requires >= 0.75 version of
412 * userspace rbd utility.
413 */
414 static bool single_major = false;
415 module_param(single_major, bool, S_IRUGO);
416 MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: false)");
417
418 static int rbd_img_request_submit(struct rbd_img_request *img_request);
419
420 static void rbd_dev_device_release(struct device *dev);
421
422 static ssize_t rbd_add(struct bus_type *bus, const char *buf,
423 size_t count);
424 static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
425 size_t count);
426 static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf,
427 size_t count);
428 static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf,
429 size_t count);
430 static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth);
431 static void rbd_spec_put(struct rbd_spec *spec);
432
rbd_dev_id_to_minor(int dev_id)433 static int rbd_dev_id_to_minor(int dev_id)
434 {
435 return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT;
436 }
437
minor_to_rbd_dev_id(int minor)438 static int minor_to_rbd_dev_id(int minor)
439 {
440 return minor >> RBD_SINGLE_MAJOR_PART_SHIFT;
441 }
442
443 static BUS_ATTR(add, S_IWUSR, NULL, rbd_add);
444 static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove);
445 static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major);
446 static BUS_ATTR(remove_single_major, S_IWUSR, NULL, rbd_remove_single_major);
447
448 static struct attribute *rbd_bus_attrs[] = {
449 &bus_attr_add.attr,
450 &bus_attr_remove.attr,
451 &bus_attr_add_single_major.attr,
452 &bus_attr_remove_single_major.attr,
453 NULL,
454 };
455
rbd_bus_is_visible(struct kobject * kobj,struct attribute * attr,int index)456 static umode_t rbd_bus_is_visible(struct kobject *kobj,
457 struct attribute *attr, int index)
458 {
459 if (!single_major &&
460 (attr == &bus_attr_add_single_major.attr ||
461 attr == &bus_attr_remove_single_major.attr))
462 return 0;
463
464 return attr->mode;
465 }
466
467 static const struct attribute_group rbd_bus_group = {
468 .attrs = rbd_bus_attrs,
469 .is_visible = rbd_bus_is_visible,
470 };
471 __ATTRIBUTE_GROUPS(rbd_bus);
472
473 static struct bus_type rbd_bus_type = {
474 .name = "rbd",
475 .bus_groups = rbd_bus_groups,
476 };
477
rbd_root_dev_release(struct device * dev)478 static void rbd_root_dev_release(struct device *dev)
479 {
480 }
481
482 static struct device rbd_root_dev = {
483 .init_name = "rbd",
484 .release = rbd_root_dev_release,
485 };
486
487 static __printf(2, 3)
rbd_warn(struct rbd_device * rbd_dev,const char * fmt,...)488 void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
489 {
490 struct va_format vaf;
491 va_list args;
492
493 va_start(args, fmt);
494 vaf.fmt = fmt;
495 vaf.va = &args;
496
497 if (!rbd_dev)
498 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
499 else if (rbd_dev->disk)
500 printk(KERN_WARNING "%s: %s: %pV\n",
501 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
502 else if (rbd_dev->spec && rbd_dev->spec->image_name)
503 printk(KERN_WARNING "%s: image %s: %pV\n",
504 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
505 else if (rbd_dev->spec && rbd_dev->spec->image_id)
506 printk(KERN_WARNING "%s: id %s: %pV\n",
507 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
508 else /* punt */
509 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
510 RBD_DRV_NAME, rbd_dev, &vaf);
511 va_end(args);
512 }
513
514 #ifdef RBD_DEBUG
515 #define rbd_assert(expr) \
516 if (unlikely(!(expr))) { \
517 printk(KERN_ERR "\nAssertion failure in %s() " \
518 "at line %d:\n\n" \
519 "\trbd_assert(%s);\n\n", \
520 __func__, __LINE__, #expr); \
521 BUG(); \
522 }
523 #else /* !RBD_DEBUG */
524 # define rbd_assert(expr) ((void) 0)
525 #endif /* !RBD_DEBUG */
526
527 static void rbd_osd_copyup_callback(struct rbd_obj_request *obj_request);
528 static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request);
529 static void rbd_img_parent_read(struct rbd_obj_request *obj_request);
530 static void rbd_dev_remove_parent(struct rbd_device *rbd_dev);
531
532 static int rbd_dev_refresh(struct rbd_device *rbd_dev);
533 static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev);
534 static int rbd_dev_header_info(struct rbd_device *rbd_dev);
535 static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev);
536 static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
537 u64 snap_id);
538 static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
539 u8 *order, u64 *snap_size);
540 static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
541 u64 *snap_features);
542 static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name);
543
rbd_open(struct block_device * bdev,fmode_t mode)544 static int rbd_open(struct block_device *bdev, fmode_t mode)
545 {
546 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
547 bool removing = false;
548
549 if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
550 return -EROFS;
551
552 spin_lock_irq(&rbd_dev->lock);
553 if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags))
554 removing = true;
555 else
556 rbd_dev->open_count++;
557 spin_unlock_irq(&rbd_dev->lock);
558 if (removing)
559 return -ENOENT;
560
561 (void) get_device(&rbd_dev->dev);
562
563 return 0;
564 }
565
rbd_release(struct gendisk * disk,fmode_t mode)566 static void rbd_release(struct gendisk *disk, fmode_t mode)
567 {
568 struct rbd_device *rbd_dev = disk->private_data;
569 unsigned long open_count_before;
570
571 spin_lock_irq(&rbd_dev->lock);
572 open_count_before = rbd_dev->open_count--;
573 spin_unlock_irq(&rbd_dev->lock);
574 rbd_assert(open_count_before > 0);
575
576 put_device(&rbd_dev->dev);
577 }
578
rbd_ioctl_set_ro(struct rbd_device * rbd_dev,unsigned long arg)579 static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg)
580 {
581 int ret = 0;
582 int val;
583 bool ro;
584 bool ro_changed = false;
585
586 /* get_user() may sleep, so call it before taking rbd_dev->lock */
587 if (get_user(val, (int __user *)(arg)))
588 return -EFAULT;
589
590 ro = val ? true : false;
591 /* Snapshot doesn't allow to write*/
592 if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro)
593 return -EROFS;
594
595 spin_lock_irq(&rbd_dev->lock);
596 /* prevent others open this device */
597 if (rbd_dev->open_count > 1) {
598 ret = -EBUSY;
599 goto out;
600 }
601
602 if (rbd_dev->mapping.read_only != ro) {
603 rbd_dev->mapping.read_only = ro;
604 ro_changed = true;
605 }
606
607 out:
608 spin_unlock_irq(&rbd_dev->lock);
609 /* set_disk_ro() may sleep, so call it after releasing rbd_dev->lock */
610 if (ret == 0 && ro_changed)
611 set_disk_ro(rbd_dev->disk, ro ? 1 : 0);
612
613 return ret;
614 }
615
rbd_ioctl(struct block_device * bdev,fmode_t mode,unsigned int cmd,unsigned long arg)616 static int rbd_ioctl(struct block_device *bdev, fmode_t mode,
617 unsigned int cmd, unsigned long arg)
618 {
619 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
620 int ret = 0;
621
622 switch (cmd) {
623 case BLKROSET:
624 ret = rbd_ioctl_set_ro(rbd_dev, arg);
625 break;
626 default:
627 ret = -ENOTTY;
628 }
629
630 return ret;
631 }
632
633 #ifdef CONFIG_COMPAT
rbd_compat_ioctl(struct block_device * bdev,fmode_t mode,unsigned int cmd,unsigned long arg)634 static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode,
635 unsigned int cmd, unsigned long arg)
636 {
637 return rbd_ioctl(bdev, mode, cmd, arg);
638 }
639 #endif /* CONFIG_COMPAT */
640
641 static const struct block_device_operations rbd_bd_ops = {
642 .owner = THIS_MODULE,
643 .open = rbd_open,
644 .release = rbd_release,
645 .ioctl = rbd_ioctl,
646 #ifdef CONFIG_COMPAT
647 .compat_ioctl = rbd_compat_ioctl,
648 #endif
649 };
650
651 /*
652 * Initialize an rbd client instance. Success or not, this function
653 * consumes ceph_opts. Caller holds client_mutex.
654 */
rbd_client_create(struct ceph_options * ceph_opts)655 static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
656 {
657 struct rbd_client *rbdc;
658 int ret = -ENOMEM;
659
660 dout("%s:\n", __func__);
661 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
662 if (!rbdc)
663 goto out_opt;
664
665 kref_init(&rbdc->kref);
666 INIT_LIST_HEAD(&rbdc->node);
667
668 rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
669 if (IS_ERR(rbdc->client))
670 goto out_rbdc;
671 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
672
673 ret = ceph_open_session(rbdc->client);
674 if (ret < 0)
675 goto out_client;
676
677 spin_lock(&rbd_client_list_lock);
678 list_add_tail(&rbdc->node, &rbd_client_list);
679 spin_unlock(&rbd_client_list_lock);
680
681 dout("%s: rbdc %p\n", __func__, rbdc);
682
683 return rbdc;
684 out_client:
685 ceph_destroy_client(rbdc->client);
686 out_rbdc:
687 kfree(rbdc);
688 out_opt:
689 if (ceph_opts)
690 ceph_destroy_options(ceph_opts);
691 dout("%s: error %d\n", __func__, ret);
692
693 return ERR_PTR(ret);
694 }
695
__rbd_get_client(struct rbd_client * rbdc)696 static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc)
697 {
698 kref_get(&rbdc->kref);
699
700 return rbdc;
701 }
702
703 /*
704 * Find a ceph client with specific addr and configuration. If
705 * found, bump its reference count.
706 */
rbd_client_find(struct ceph_options * ceph_opts)707 static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
708 {
709 struct rbd_client *client_node;
710 bool found = false;
711
712 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
713 return NULL;
714
715 spin_lock(&rbd_client_list_lock);
716 list_for_each_entry(client_node, &rbd_client_list, node) {
717 if (!ceph_compare_options(ceph_opts, client_node->client)) {
718 __rbd_get_client(client_node);
719
720 found = true;
721 break;
722 }
723 }
724 spin_unlock(&rbd_client_list_lock);
725
726 return found ? client_node : NULL;
727 }
728
729 /*
730 * mount options
731 */
732 enum {
733 Opt_last_int,
734 /* int args above */
735 Opt_last_string,
736 /* string args above */
737 Opt_read_only,
738 Opt_read_write,
739 /* Boolean args above */
740 Opt_last_bool,
741 };
742
743 static match_table_t rbd_opts_tokens = {
744 /* int args above */
745 /* string args above */
746 {Opt_read_only, "read_only"},
747 {Opt_read_only, "ro"}, /* Alternate spelling */
748 {Opt_read_write, "read_write"},
749 {Opt_read_write, "rw"}, /* Alternate spelling */
750 /* Boolean args above */
751 {-1, NULL}
752 };
753
754 struct rbd_options {
755 bool read_only;
756 };
757
758 #define RBD_READ_ONLY_DEFAULT false
759
parse_rbd_opts_token(char * c,void * private)760 static int parse_rbd_opts_token(char *c, void *private)
761 {
762 struct rbd_options *rbd_opts = private;
763 substring_t argstr[MAX_OPT_ARGS];
764 int token, intval, ret;
765
766 token = match_token(c, rbd_opts_tokens, argstr);
767 if (token < 0)
768 return -EINVAL;
769
770 if (token < Opt_last_int) {
771 ret = match_int(&argstr[0], &intval);
772 if (ret < 0) {
773 pr_err("bad mount option arg (not int) "
774 "at '%s'\n", c);
775 return ret;
776 }
777 dout("got int token %d val %d\n", token, intval);
778 } else if (token > Opt_last_int && token < Opt_last_string) {
779 dout("got string token %d val %s\n", token,
780 argstr[0].from);
781 } else if (token > Opt_last_string && token < Opt_last_bool) {
782 dout("got Boolean token %d\n", token);
783 } else {
784 dout("got token %d\n", token);
785 }
786
787 switch (token) {
788 case Opt_read_only:
789 rbd_opts->read_only = true;
790 break;
791 case Opt_read_write:
792 rbd_opts->read_only = false;
793 break;
794 default:
795 rbd_assert(false);
796 break;
797 }
798 return 0;
799 }
800
obj_op_name(enum obj_operation_type op_type)801 static char* obj_op_name(enum obj_operation_type op_type)
802 {
803 switch (op_type) {
804 case OBJ_OP_READ:
805 return "read";
806 case OBJ_OP_WRITE:
807 return "write";
808 case OBJ_OP_DISCARD:
809 return "discard";
810 default:
811 return "???";
812 }
813 }
814
815 /*
816 * Get a ceph client with specific addr and configuration, if one does
817 * not exist create it. Either way, ceph_opts is consumed by this
818 * function.
819 */
rbd_get_client(struct ceph_options * ceph_opts)820 static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
821 {
822 struct rbd_client *rbdc;
823
824 mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING);
825 rbdc = rbd_client_find(ceph_opts);
826 if (rbdc) /* using an existing client */
827 ceph_destroy_options(ceph_opts);
828 else
829 rbdc = rbd_client_create(ceph_opts);
830 mutex_unlock(&client_mutex);
831
832 return rbdc;
833 }
834
835 /*
836 * Destroy ceph client
837 *
838 * Caller must hold rbd_client_list_lock.
839 */
rbd_client_release(struct kref * kref)840 static void rbd_client_release(struct kref *kref)
841 {
842 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
843
844 dout("%s: rbdc %p\n", __func__, rbdc);
845 spin_lock(&rbd_client_list_lock);
846 list_del(&rbdc->node);
847 spin_unlock(&rbd_client_list_lock);
848
849 ceph_destroy_client(rbdc->client);
850 kfree(rbdc);
851 }
852
853 /*
854 * Drop reference to ceph client node. If it's not referenced anymore, release
855 * it.
856 */
rbd_put_client(struct rbd_client * rbdc)857 static void rbd_put_client(struct rbd_client *rbdc)
858 {
859 if (rbdc)
860 kref_put(&rbdc->kref, rbd_client_release);
861 }
862
rbd_image_format_valid(u32 image_format)863 static bool rbd_image_format_valid(u32 image_format)
864 {
865 return image_format == 1 || image_format == 2;
866 }
867
rbd_dev_ondisk_valid(struct rbd_image_header_ondisk * ondisk)868 static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
869 {
870 size_t size;
871 u32 snap_count;
872
873 /* The header has to start with the magic rbd header text */
874 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
875 return false;
876
877 /* The bio layer requires at least sector-sized I/O */
878
879 if (ondisk->options.order < SECTOR_SHIFT)
880 return false;
881
882 /* If we use u64 in a few spots we may be able to loosen this */
883
884 if (ondisk->options.order > 8 * sizeof (int) - 1)
885 return false;
886
887 /*
888 * The size of a snapshot header has to fit in a size_t, and
889 * that limits the number of snapshots.
890 */
891 snap_count = le32_to_cpu(ondisk->snap_count);
892 size = SIZE_MAX - sizeof (struct ceph_snap_context);
893 if (snap_count > size / sizeof (__le64))
894 return false;
895
896 /*
897 * Not only that, but the size of the entire the snapshot
898 * header must also be representable in a size_t.
899 */
900 size -= snap_count * sizeof (__le64);
901 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
902 return false;
903
904 return true;
905 }
906
907 /*
908 * Fill an rbd image header with information from the given format 1
909 * on-disk header.
910 */
rbd_header_from_disk(struct rbd_device * rbd_dev,struct rbd_image_header_ondisk * ondisk)911 static int rbd_header_from_disk(struct rbd_device *rbd_dev,
912 struct rbd_image_header_ondisk *ondisk)
913 {
914 struct rbd_image_header *header = &rbd_dev->header;
915 bool first_time = header->object_prefix == NULL;
916 struct ceph_snap_context *snapc;
917 char *object_prefix = NULL;
918 char *snap_names = NULL;
919 u64 *snap_sizes = NULL;
920 u32 snap_count;
921 size_t size;
922 int ret = -ENOMEM;
923 u32 i;
924
925 /* Allocate this now to avoid having to handle failure below */
926
927 if (first_time) {
928 size_t len;
929
930 len = strnlen(ondisk->object_prefix,
931 sizeof (ondisk->object_prefix));
932 object_prefix = kmalloc(len + 1, GFP_KERNEL);
933 if (!object_prefix)
934 return -ENOMEM;
935 memcpy(object_prefix, ondisk->object_prefix, len);
936 object_prefix[len] = '\0';
937 }
938
939 /* Allocate the snapshot context and fill it in */
940
941 snap_count = le32_to_cpu(ondisk->snap_count);
942 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
943 if (!snapc)
944 goto out_err;
945 snapc->seq = le64_to_cpu(ondisk->snap_seq);
946 if (snap_count) {
947 struct rbd_image_snap_ondisk *snaps;
948 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
949
950 /* We'll keep a copy of the snapshot names... */
951
952 if (snap_names_len > (u64)SIZE_MAX)
953 goto out_2big;
954 snap_names = kmalloc(snap_names_len, GFP_KERNEL);
955 if (!snap_names)
956 goto out_err;
957
958 /* ...as well as the array of their sizes. */
959
960 size = snap_count * sizeof (*header->snap_sizes);
961 snap_sizes = kmalloc(size, GFP_KERNEL);
962 if (!snap_sizes)
963 goto out_err;
964
965 /*
966 * Copy the names, and fill in each snapshot's id
967 * and size.
968 *
969 * Note that rbd_dev_v1_header_info() guarantees the
970 * ondisk buffer we're working with has
971 * snap_names_len bytes beyond the end of the
972 * snapshot id array, this memcpy() is safe.
973 */
974 memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len);
975 snaps = ondisk->snaps;
976 for (i = 0; i < snap_count; i++) {
977 snapc->snaps[i] = le64_to_cpu(snaps[i].id);
978 snap_sizes[i] = le64_to_cpu(snaps[i].image_size);
979 }
980 }
981
982 /* We won't fail any more, fill in the header */
983
984 if (first_time) {
985 header->object_prefix = object_prefix;
986 header->obj_order = ondisk->options.order;
987 header->crypt_type = ondisk->options.crypt_type;
988 header->comp_type = ondisk->options.comp_type;
989 /* The rest aren't used for format 1 images */
990 header->stripe_unit = 0;
991 header->stripe_count = 0;
992 header->features = 0;
993 } else {
994 ceph_put_snap_context(header->snapc);
995 kfree(header->snap_names);
996 kfree(header->snap_sizes);
997 }
998
999 /* The remaining fields always get updated (when we refresh) */
1000
1001 header->image_size = le64_to_cpu(ondisk->image_size);
1002 header->snapc = snapc;
1003 header->snap_names = snap_names;
1004 header->snap_sizes = snap_sizes;
1005
1006 return 0;
1007 out_2big:
1008 ret = -EIO;
1009 out_err:
1010 kfree(snap_sizes);
1011 kfree(snap_names);
1012 ceph_put_snap_context(snapc);
1013 kfree(object_prefix);
1014
1015 return ret;
1016 }
1017
_rbd_dev_v1_snap_name(struct rbd_device * rbd_dev,u32 which)1018 static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which)
1019 {
1020 const char *snap_name;
1021
1022 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
1023
1024 /* Skip over names until we find the one we are looking for */
1025
1026 snap_name = rbd_dev->header.snap_names;
1027 while (which--)
1028 snap_name += strlen(snap_name) + 1;
1029
1030 return kstrdup(snap_name, GFP_KERNEL);
1031 }
1032
1033 /*
1034 * Snapshot id comparison function for use with qsort()/bsearch().
1035 * Note that result is for snapshots in *descending* order.
1036 */
snapid_compare_reverse(const void * s1,const void * s2)1037 static int snapid_compare_reverse(const void *s1, const void *s2)
1038 {
1039 u64 snap_id1 = *(u64 *)s1;
1040 u64 snap_id2 = *(u64 *)s2;
1041
1042 if (snap_id1 < snap_id2)
1043 return 1;
1044 return snap_id1 == snap_id2 ? 0 : -1;
1045 }
1046
1047 /*
1048 * Search a snapshot context to see if the given snapshot id is
1049 * present.
1050 *
1051 * Returns the position of the snapshot id in the array if it's found,
1052 * or BAD_SNAP_INDEX otherwise.
1053 *
1054 * Note: The snapshot array is in kept sorted (by the osd) in
1055 * reverse order, highest snapshot id first.
1056 */
rbd_dev_snap_index(struct rbd_device * rbd_dev,u64 snap_id)1057 static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id)
1058 {
1059 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
1060 u64 *found;
1061
1062 found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps,
1063 sizeof (snap_id), snapid_compare_reverse);
1064
1065 return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX;
1066 }
1067
rbd_dev_v1_snap_name(struct rbd_device * rbd_dev,u64 snap_id)1068 static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev,
1069 u64 snap_id)
1070 {
1071 u32 which;
1072 const char *snap_name;
1073
1074 which = rbd_dev_snap_index(rbd_dev, snap_id);
1075 if (which == BAD_SNAP_INDEX)
1076 return ERR_PTR(-ENOENT);
1077
1078 snap_name = _rbd_dev_v1_snap_name(rbd_dev, which);
1079 return snap_name ? snap_name : ERR_PTR(-ENOMEM);
1080 }
1081
rbd_snap_name(struct rbd_device * rbd_dev,u64 snap_id)1082 static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
1083 {
1084 if (snap_id == CEPH_NOSNAP)
1085 return RBD_SNAP_HEAD_NAME;
1086
1087 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1088 if (rbd_dev->image_format == 1)
1089 return rbd_dev_v1_snap_name(rbd_dev, snap_id);
1090
1091 return rbd_dev_v2_snap_name(rbd_dev, snap_id);
1092 }
1093
rbd_snap_size(struct rbd_device * rbd_dev,u64 snap_id,u64 * snap_size)1094 static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
1095 u64 *snap_size)
1096 {
1097 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1098 if (snap_id == CEPH_NOSNAP) {
1099 *snap_size = rbd_dev->header.image_size;
1100 } else if (rbd_dev->image_format == 1) {
1101 u32 which;
1102
1103 which = rbd_dev_snap_index(rbd_dev, snap_id);
1104 if (which == BAD_SNAP_INDEX)
1105 return -ENOENT;
1106
1107 *snap_size = rbd_dev->header.snap_sizes[which];
1108 } else {
1109 u64 size = 0;
1110 int ret;
1111
1112 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size);
1113 if (ret)
1114 return ret;
1115
1116 *snap_size = size;
1117 }
1118 return 0;
1119 }
1120
rbd_snap_features(struct rbd_device * rbd_dev,u64 snap_id,u64 * snap_features)1121 static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
1122 u64 *snap_features)
1123 {
1124 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1125 if (snap_id == CEPH_NOSNAP) {
1126 *snap_features = rbd_dev->header.features;
1127 } else if (rbd_dev->image_format == 1) {
1128 *snap_features = 0; /* No features for format 1 */
1129 } else {
1130 u64 features = 0;
1131 int ret;
1132
1133 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features);
1134 if (ret)
1135 return ret;
1136
1137 *snap_features = features;
1138 }
1139 return 0;
1140 }
1141
rbd_dev_mapping_set(struct rbd_device * rbd_dev)1142 static int rbd_dev_mapping_set(struct rbd_device *rbd_dev)
1143 {
1144 u64 snap_id = rbd_dev->spec->snap_id;
1145 u64 size = 0;
1146 u64 features = 0;
1147 int ret;
1148
1149 ret = rbd_snap_size(rbd_dev, snap_id, &size);
1150 if (ret)
1151 return ret;
1152 ret = rbd_snap_features(rbd_dev, snap_id, &features);
1153 if (ret)
1154 return ret;
1155
1156 rbd_dev->mapping.size = size;
1157 rbd_dev->mapping.features = features;
1158
1159 return 0;
1160 }
1161
rbd_dev_mapping_clear(struct rbd_device * rbd_dev)1162 static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev)
1163 {
1164 rbd_dev->mapping.size = 0;
1165 rbd_dev->mapping.features = 0;
1166 }
1167
rbd_segment_name_free(const char * name)1168 static void rbd_segment_name_free(const char *name)
1169 {
1170 /* The explicit cast here is needed to drop the const qualifier */
1171
1172 kmem_cache_free(rbd_segment_name_cache, (void *)name);
1173 }
1174
rbd_segment_name(struct rbd_device * rbd_dev,u64 offset)1175 static const char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
1176 {
1177 char *name;
1178 u64 segment;
1179 int ret;
1180 char *name_format;
1181
1182 name = kmem_cache_alloc(rbd_segment_name_cache, GFP_NOIO);
1183 if (!name)
1184 return NULL;
1185 segment = offset >> rbd_dev->header.obj_order;
1186 name_format = "%s.%012llx";
1187 if (rbd_dev->image_format == 2)
1188 name_format = "%s.%016llx";
1189 ret = snprintf(name, CEPH_MAX_OID_NAME_LEN + 1, name_format,
1190 rbd_dev->header.object_prefix, segment);
1191 if (ret < 0 || ret > CEPH_MAX_OID_NAME_LEN) {
1192 pr_err("error formatting segment name for #%llu (%d)\n",
1193 segment, ret);
1194 rbd_segment_name_free(name);
1195 name = NULL;
1196 }
1197
1198 return name;
1199 }
1200
rbd_segment_offset(struct rbd_device * rbd_dev,u64 offset)1201 static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
1202 {
1203 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
1204
1205 return offset & (segment_size - 1);
1206 }
1207
rbd_segment_length(struct rbd_device * rbd_dev,u64 offset,u64 length)1208 static u64 rbd_segment_length(struct rbd_device *rbd_dev,
1209 u64 offset, u64 length)
1210 {
1211 u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
1212
1213 offset &= segment_size - 1;
1214
1215 rbd_assert(length <= U64_MAX - offset);
1216 if (offset + length > segment_size)
1217 length = segment_size - offset;
1218
1219 return length;
1220 }
1221
1222 /*
1223 * returns the size of an object in the image
1224 */
rbd_obj_bytes(struct rbd_image_header * header)1225 static u64 rbd_obj_bytes(struct rbd_image_header *header)
1226 {
1227 return 1 << header->obj_order;
1228 }
1229
1230 /*
1231 * bio helpers
1232 */
1233
bio_chain_put(struct bio * chain)1234 static void bio_chain_put(struct bio *chain)
1235 {
1236 struct bio *tmp;
1237
1238 while (chain) {
1239 tmp = chain;
1240 chain = chain->bi_next;
1241 bio_put(tmp);
1242 }
1243 }
1244
1245 /*
1246 * zeros a bio chain, starting at specific offset
1247 */
zero_bio_chain(struct bio * chain,int start_ofs)1248 static void zero_bio_chain(struct bio *chain, int start_ofs)
1249 {
1250 struct bio_vec bv;
1251 struct bvec_iter iter;
1252 unsigned long flags;
1253 void *buf;
1254 int pos = 0;
1255
1256 while (chain) {
1257 bio_for_each_segment(bv, chain, iter) {
1258 if (pos + bv.bv_len > start_ofs) {
1259 int remainder = max(start_ofs - pos, 0);
1260 buf = bvec_kmap_irq(&bv, &flags);
1261 memset(buf + remainder, 0,
1262 bv.bv_len - remainder);
1263 flush_dcache_page(bv.bv_page);
1264 bvec_kunmap_irq(buf, &flags);
1265 }
1266 pos += bv.bv_len;
1267 }
1268
1269 chain = chain->bi_next;
1270 }
1271 }
1272
1273 /*
1274 * similar to zero_bio_chain(), zeros data defined by a page array,
1275 * starting at the given byte offset from the start of the array and
1276 * continuing up to the given end offset. The pages array is
1277 * assumed to be big enough to hold all bytes up to the end.
1278 */
zero_pages(struct page ** pages,u64 offset,u64 end)1279 static void zero_pages(struct page **pages, u64 offset, u64 end)
1280 {
1281 struct page **page = &pages[offset >> PAGE_SHIFT];
1282
1283 rbd_assert(end > offset);
1284 rbd_assert(end - offset <= (u64)SIZE_MAX);
1285 while (offset < end) {
1286 size_t page_offset;
1287 size_t length;
1288 unsigned long flags;
1289 void *kaddr;
1290
1291 page_offset = offset & ~PAGE_MASK;
1292 length = min_t(size_t, PAGE_SIZE - page_offset, end - offset);
1293 local_irq_save(flags);
1294 kaddr = kmap_atomic(*page);
1295 memset(kaddr + page_offset, 0, length);
1296 flush_dcache_page(*page);
1297 kunmap_atomic(kaddr);
1298 local_irq_restore(flags);
1299
1300 offset += length;
1301 page++;
1302 }
1303 }
1304
1305 /*
1306 * Clone a portion of a bio, starting at the given byte offset
1307 * and continuing for the number of bytes indicated.
1308 */
bio_clone_range(struct bio * bio_src,unsigned int offset,unsigned int len,gfp_t gfpmask)1309 static struct bio *bio_clone_range(struct bio *bio_src,
1310 unsigned int offset,
1311 unsigned int len,
1312 gfp_t gfpmask)
1313 {
1314 struct bio *bio;
1315
1316 bio = bio_clone(bio_src, gfpmask);
1317 if (!bio)
1318 return NULL; /* ENOMEM */
1319
1320 bio_advance(bio, offset);
1321 bio->bi_iter.bi_size = len;
1322
1323 return bio;
1324 }
1325
1326 /*
1327 * Clone a portion of a bio chain, starting at the given byte offset
1328 * into the first bio in the source chain and continuing for the
1329 * number of bytes indicated. The result is another bio chain of
1330 * exactly the given length, or a null pointer on error.
1331 *
1332 * The bio_src and offset parameters are both in-out. On entry they
1333 * refer to the first source bio and the offset into that bio where
1334 * the start of data to be cloned is located.
1335 *
1336 * On return, bio_src is updated to refer to the bio in the source
1337 * chain that contains first un-cloned byte, and *offset will
1338 * contain the offset of that byte within that bio.
1339 */
bio_chain_clone_range(struct bio ** bio_src,unsigned int * offset,unsigned int len,gfp_t gfpmask)1340 static struct bio *bio_chain_clone_range(struct bio **bio_src,
1341 unsigned int *offset,
1342 unsigned int len,
1343 gfp_t gfpmask)
1344 {
1345 struct bio *bi = *bio_src;
1346 unsigned int off = *offset;
1347 struct bio *chain = NULL;
1348 struct bio **end;
1349
1350 /* Build up a chain of clone bios up to the limit */
1351
1352 if (!bi || off >= bi->bi_iter.bi_size || !len)
1353 return NULL; /* Nothing to clone */
1354
1355 end = &chain;
1356 while (len) {
1357 unsigned int bi_size;
1358 struct bio *bio;
1359
1360 if (!bi) {
1361 rbd_warn(NULL, "bio_chain exhausted with %u left", len);
1362 goto out_err; /* EINVAL; ran out of bio's */
1363 }
1364 bi_size = min_t(unsigned int, bi->bi_iter.bi_size - off, len);
1365 bio = bio_clone_range(bi, off, bi_size, gfpmask);
1366 if (!bio)
1367 goto out_err; /* ENOMEM */
1368
1369 *end = bio;
1370 end = &bio->bi_next;
1371
1372 off += bi_size;
1373 if (off == bi->bi_iter.bi_size) {
1374 bi = bi->bi_next;
1375 off = 0;
1376 }
1377 len -= bi_size;
1378 }
1379 *bio_src = bi;
1380 *offset = off;
1381
1382 return chain;
1383 out_err:
1384 bio_chain_put(chain);
1385
1386 return NULL;
1387 }
1388
1389 /*
1390 * The default/initial value for all object request flags is 0. For
1391 * each flag, once its value is set to 1 it is never reset to 0
1392 * again.
1393 */
obj_request_img_data_set(struct rbd_obj_request * obj_request)1394 static void obj_request_img_data_set(struct rbd_obj_request *obj_request)
1395 {
1396 if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) {
1397 struct rbd_device *rbd_dev;
1398
1399 rbd_dev = obj_request->img_request->rbd_dev;
1400 rbd_warn(rbd_dev, "obj_request %p already marked img_data",
1401 obj_request);
1402 }
1403 }
1404
obj_request_img_data_test(struct rbd_obj_request * obj_request)1405 static bool obj_request_img_data_test(struct rbd_obj_request *obj_request)
1406 {
1407 smp_mb();
1408 return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0;
1409 }
1410
obj_request_done_set(struct rbd_obj_request * obj_request)1411 static void obj_request_done_set(struct rbd_obj_request *obj_request)
1412 {
1413 if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) {
1414 struct rbd_device *rbd_dev = NULL;
1415
1416 if (obj_request_img_data_test(obj_request))
1417 rbd_dev = obj_request->img_request->rbd_dev;
1418 rbd_warn(rbd_dev, "obj_request %p already marked done",
1419 obj_request);
1420 }
1421 }
1422
obj_request_done_test(struct rbd_obj_request * obj_request)1423 static bool obj_request_done_test(struct rbd_obj_request *obj_request)
1424 {
1425 smp_mb();
1426 return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0;
1427 }
1428
1429 /*
1430 * This sets the KNOWN flag after (possibly) setting the EXISTS
1431 * flag. The latter is set based on the "exists" value provided.
1432 *
1433 * Note that for our purposes once an object exists it never goes
1434 * away again. It's possible that the response from two existence
1435 * checks are separated by the creation of the target object, and
1436 * the first ("doesn't exist") response arrives *after* the second
1437 * ("does exist"). In that case we ignore the second one.
1438 */
obj_request_existence_set(struct rbd_obj_request * obj_request,bool exists)1439 static void obj_request_existence_set(struct rbd_obj_request *obj_request,
1440 bool exists)
1441 {
1442 if (exists)
1443 set_bit(OBJ_REQ_EXISTS, &obj_request->flags);
1444 set_bit(OBJ_REQ_KNOWN, &obj_request->flags);
1445 smp_mb();
1446 }
1447
obj_request_known_test(struct rbd_obj_request * obj_request)1448 static bool obj_request_known_test(struct rbd_obj_request *obj_request)
1449 {
1450 smp_mb();
1451 return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0;
1452 }
1453
obj_request_exists_test(struct rbd_obj_request * obj_request)1454 static bool obj_request_exists_test(struct rbd_obj_request *obj_request)
1455 {
1456 smp_mb();
1457 return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0;
1458 }
1459
obj_request_overlaps_parent(struct rbd_obj_request * obj_request)1460 static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request)
1461 {
1462 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
1463
1464 return obj_request->img_offset <
1465 round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header));
1466 }
1467
rbd_obj_request_get(struct rbd_obj_request * obj_request)1468 static void rbd_obj_request_get(struct rbd_obj_request *obj_request)
1469 {
1470 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1471 atomic_read(&obj_request->kref.refcount));
1472 kref_get(&obj_request->kref);
1473 }
1474
1475 static void rbd_obj_request_destroy(struct kref *kref);
rbd_obj_request_put(struct rbd_obj_request * obj_request)1476 static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
1477 {
1478 rbd_assert(obj_request != NULL);
1479 dout("%s: obj %p (was %d)\n", __func__, obj_request,
1480 atomic_read(&obj_request->kref.refcount));
1481 kref_put(&obj_request->kref, rbd_obj_request_destroy);
1482 }
1483
rbd_img_request_get(struct rbd_img_request * img_request)1484 static void rbd_img_request_get(struct rbd_img_request *img_request)
1485 {
1486 dout("%s: img %p (was %d)\n", __func__, img_request,
1487 atomic_read(&img_request->kref.refcount));
1488 kref_get(&img_request->kref);
1489 }
1490
1491 static bool img_request_child_test(struct rbd_img_request *img_request);
1492 static void rbd_parent_request_destroy(struct kref *kref);
1493 static void rbd_img_request_destroy(struct kref *kref);
rbd_img_request_put(struct rbd_img_request * img_request)1494 static void rbd_img_request_put(struct rbd_img_request *img_request)
1495 {
1496 rbd_assert(img_request != NULL);
1497 dout("%s: img %p (was %d)\n", __func__, img_request,
1498 atomic_read(&img_request->kref.refcount));
1499 if (img_request_child_test(img_request))
1500 kref_put(&img_request->kref, rbd_parent_request_destroy);
1501 else
1502 kref_put(&img_request->kref, rbd_img_request_destroy);
1503 }
1504
rbd_img_obj_request_add(struct rbd_img_request * img_request,struct rbd_obj_request * obj_request)1505 static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
1506 struct rbd_obj_request *obj_request)
1507 {
1508 rbd_assert(obj_request->img_request == NULL);
1509
1510 /* Image request now owns object's original reference */
1511 obj_request->img_request = img_request;
1512 obj_request->which = img_request->obj_request_count;
1513 rbd_assert(!obj_request_img_data_test(obj_request));
1514 obj_request_img_data_set(obj_request);
1515 rbd_assert(obj_request->which != BAD_WHICH);
1516 img_request->obj_request_count++;
1517 list_add_tail(&obj_request->links, &img_request->obj_requests);
1518 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1519 obj_request->which);
1520 }
1521
rbd_img_obj_request_del(struct rbd_img_request * img_request,struct rbd_obj_request * obj_request)1522 static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
1523 struct rbd_obj_request *obj_request)
1524 {
1525 rbd_assert(obj_request->which != BAD_WHICH);
1526
1527 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1528 obj_request->which);
1529 list_del(&obj_request->links);
1530 rbd_assert(img_request->obj_request_count > 0);
1531 img_request->obj_request_count--;
1532 rbd_assert(obj_request->which == img_request->obj_request_count);
1533 obj_request->which = BAD_WHICH;
1534 rbd_assert(obj_request_img_data_test(obj_request));
1535 rbd_assert(obj_request->img_request == img_request);
1536 obj_request->img_request = NULL;
1537 obj_request->callback = NULL;
1538 rbd_obj_request_put(obj_request);
1539 }
1540
obj_request_type_valid(enum obj_request_type type)1541 static bool obj_request_type_valid(enum obj_request_type type)
1542 {
1543 switch (type) {
1544 case OBJ_REQUEST_NODATA:
1545 case OBJ_REQUEST_BIO:
1546 case OBJ_REQUEST_PAGES:
1547 return true;
1548 default:
1549 return false;
1550 }
1551 }
1552
rbd_obj_request_submit(struct ceph_osd_client * osdc,struct rbd_obj_request * obj_request)1553 static int rbd_obj_request_submit(struct ceph_osd_client *osdc,
1554 struct rbd_obj_request *obj_request)
1555 {
1556 dout("%s %p\n", __func__, obj_request);
1557 return ceph_osdc_start_request(osdc, obj_request->osd_req, false);
1558 }
1559
rbd_obj_request_end(struct rbd_obj_request * obj_request)1560 static void rbd_obj_request_end(struct rbd_obj_request *obj_request)
1561 {
1562 dout("%s %p\n", __func__, obj_request);
1563 ceph_osdc_cancel_request(obj_request->osd_req);
1564 }
1565
1566 /*
1567 * Wait for an object request to complete. If interrupted, cancel the
1568 * underlying osd request.
1569 */
rbd_obj_request_wait(struct rbd_obj_request * obj_request)1570 static int rbd_obj_request_wait(struct rbd_obj_request *obj_request)
1571 {
1572 int ret;
1573
1574 dout("%s %p\n", __func__, obj_request);
1575
1576 ret = wait_for_completion_interruptible(&obj_request->completion);
1577 if (ret < 0) {
1578 dout("%s %p interrupted\n", __func__, obj_request);
1579 rbd_obj_request_end(obj_request);
1580 return ret;
1581 }
1582
1583 dout("%s %p done\n", __func__, obj_request);
1584 return 0;
1585 }
1586
rbd_img_request_complete(struct rbd_img_request * img_request)1587 static void rbd_img_request_complete(struct rbd_img_request *img_request)
1588 {
1589
1590 dout("%s: img %p\n", __func__, img_request);
1591
1592 /*
1593 * If no error occurred, compute the aggregate transfer
1594 * count for the image request. We could instead use
1595 * atomic64_cmpxchg() to update it as each object request
1596 * completes; not clear which way is better off hand.
1597 */
1598 if (!img_request->result) {
1599 struct rbd_obj_request *obj_request;
1600 u64 xferred = 0;
1601
1602 for_each_obj_request(img_request, obj_request)
1603 xferred += obj_request->xferred;
1604 img_request->xferred = xferred;
1605 }
1606
1607 if (img_request->callback)
1608 img_request->callback(img_request);
1609 else
1610 rbd_img_request_put(img_request);
1611 }
1612
1613 /*
1614 * The default/initial value for all image request flags is 0. Each
1615 * is conditionally set to 1 at image request initialization time
1616 * and currently never change thereafter.
1617 */
img_request_write_set(struct rbd_img_request * img_request)1618 static void img_request_write_set(struct rbd_img_request *img_request)
1619 {
1620 set_bit(IMG_REQ_WRITE, &img_request->flags);
1621 smp_mb();
1622 }
1623
img_request_write_test(struct rbd_img_request * img_request)1624 static bool img_request_write_test(struct rbd_img_request *img_request)
1625 {
1626 smp_mb();
1627 return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0;
1628 }
1629
1630 /*
1631 * Set the discard flag when the img_request is an discard request
1632 */
img_request_discard_set(struct rbd_img_request * img_request)1633 static void img_request_discard_set(struct rbd_img_request *img_request)
1634 {
1635 set_bit(IMG_REQ_DISCARD, &img_request->flags);
1636 smp_mb();
1637 }
1638
img_request_discard_test(struct rbd_img_request * img_request)1639 static bool img_request_discard_test(struct rbd_img_request *img_request)
1640 {
1641 smp_mb();
1642 return test_bit(IMG_REQ_DISCARD, &img_request->flags) != 0;
1643 }
1644
img_request_child_set(struct rbd_img_request * img_request)1645 static void img_request_child_set(struct rbd_img_request *img_request)
1646 {
1647 set_bit(IMG_REQ_CHILD, &img_request->flags);
1648 smp_mb();
1649 }
1650
img_request_child_clear(struct rbd_img_request * img_request)1651 static void img_request_child_clear(struct rbd_img_request *img_request)
1652 {
1653 clear_bit(IMG_REQ_CHILD, &img_request->flags);
1654 smp_mb();
1655 }
1656
img_request_child_test(struct rbd_img_request * img_request)1657 static bool img_request_child_test(struct rbd_img_request *img_request)
1658 {
1659 smp_mb();
1660 return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0;
1661 }
1662
img_request_layered_set(struct rbd_img_request * img_request)1663 static void img_request_layered_set(struct rbd_img_request *img_request)
1664 {
1665 set_bit(IMG_REQ_LAYERED, &img_request->flags);
1666 smp_mb();
1667 }
1668
img_request_layered_clear(struct rbd_img_request * img_request)1669 static void img_request_layered_clear(struct rbd_img_request *img_request)
1670 {
1671 clear_bit(IMG_REQ_LAYERED, &img_request->flags);
1672 smp_mb();
1673 }
1674
img_request_layered_test(struct rbd_img_request * img_request)1675 static bool img_request_layered_test(struct rbd_img_request *img_request)
1676 {
1677 smp_mb();
1678 return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
1679 }
1680
1681 static enum obj_operation_type
rbd_img_request_op_type(struct rbd_img_request * img_request)1682 rbd_img_request_op_type(struct rbd_img_request *img_request)
1683 {
1684 if (img_request_write_test(img_request))
1685 return OBJ_OP_WRITE;
1686 else if (img_request_discard_test(img_request))
1687 return OBJ_OP_DISCARD;
1688 else
1689 return OBJ_OP_READ;
1690 }
1691
1692 static void
rbd_img_obj_request_read_callback(struct rbd_obj_request * obj_request)1693 rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request)
1694 {
1695 u64 xferred = obj_request->xferred;
1696 u64 length = obj_request->length;
1697
1698 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1699 obj_request, obj_request->img_request, obj_request->result,
1700 xferred, length);
1701 /*
1702 * ENOENT means a hole in the image. We zero-fill the entire
1703 * length of the request. A short read also implies zero-fill
1704 * to the end of the request. An error requires the whole
1705 * length of the request to be reported finished with an error
1706 * to the block layer. In each case we update the xferred
1707 * count to indicate the whole request was satisfied.
1708 */
1709 rbd_assert(obj_request->type != OBJ_REQUEST_NODATA);
1710 if (obj_request->result == -ENOENT) {
1711 if (obj_request->type == OBJ_REQUEST_BIO)
1712 zero_bio_chain(obj_request->bio_list, 0);
1713 else
1714 zero_pages(obj_request->pages, 0, length);
1715 obj_request->result = 0;
1716 } else if (xferred < length && !obj_request->result) {
1717 if (obj_request->type == OBJ_REQUEST_BIO)
1718 zero_bio_chain(obj_request->bio_list, xferred);
1719 else
1720 zero_pages(obj_request->pages, xferred, length);
1721 }
1722 obj_request->xferred = length;
1723 obj_request_done_set(obj_request);
1724 }
1725
rbd_obj_request_complete(struct rbd_obj_request * obj_request)1726 static void rbd_obj_request_complete(struct rbd_obj_request *obj_request)
1727 {
1728 dout("%s: obj %p cb %p\n", __func__, obj_request,
1729 obj_request->callback);
1730 if (obj_request->callback)
1731 obj_request->callback(obj_request);
1732 else
1733 complete_all(&obj_request->completion);
1734 }
1735
rbd_osd_trivial_callback(struct rbd_obj_request * obj_request)1736 static void rbd_osd_trivial_callback(struct rbd_obj_request *obj_request)
1737 {
1738 dout("%s: obj %p\n", __func__, obj_request);
1739 obj_request_done_set(obj_request);
1740 }
1741
rbd_osd_read_callback(struct rbd_obj_request * obj_request)1742 static void rbd_osd_read_callback(struct rbd_obj_request *obj_request)
1743 {
1744 struct rbd_img_request *img_request = NULL;
1745 struct rbd_device *rbd_dev = NULL;
1746 bool layered = false;
1747
1748 if (obj_request_img_data_test(obj_request)) {
1749 img_request = obj_request->img_request;
1750 layered = img_request && img_request_layered_test(img_request);
1751 rbd_dev = img_request->rbd_dev;
1752 }
1753
1754 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1755 obj_request, img_request, obj_request->result,
1756 obj_request->xferred, obj_request->length);
1757 if (layered && obj_request->result == -ENOENT &&
1758 obj_request->img_offset < rbd_dev->parent_overlap)
1759 rbd_img_parent_read(obj_request);
1760 else if (img_request)
1761 rbd_img_obj_request_read_callback(obj_request);
1762 else
1763 obj_request_done_set(obj_request);
1764 }
1765
rbd_osd_write_callback(struct rbd_obj_request * obj_request)1766 static void rbd_osd_write_callback(struct rbd_obj_request *obj_request)
1767 {
1768 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1769 obj_request->result, obj_request->length);
1770 /*
1771 * There is no such thing as a successful short write. Set
1772 * it to our originally-requested length.
1773 */
1774 obj_request->xferred = obj_request->length;
1775 obj_request_done_set(obj_request);
1776 }
1777
rbd_osd_discard_callback(struct rbd_obj_request * obj_request)1778 static void rbd_osd_discard_callback(struct rbd_obj_request *obj_request)
1779 {
1780 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1781 obj_request->result, obj_request->length);
1782 /*
1783 * There is no such thing as a successful short discard. Set
1784 * it to our originally-requested length.
1785 */
1786 obj_request->xferred = obj_request->length;
1787 /* discarding a non-existent object is not a problem */
1788 if (obj_request->result == -ENOENT)
1789 obj_request->result = 0;
1790 obj_request_done_set(obj_request);
1791 }
1792
1793 /*
1794 * For a simple stat call there's nothing to do. We'll do more if
1795 * this is part of a write sequence for a layered image.
1796 */
rbd_osd_stat_callback(struct rbd_obj_request * obj_request)1797 static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request)
1798 {
1799 dout("%s: obj %p\n", __func__, obj_request);
1800 obj_request_done_set(obj_request);
1801 }
1802
rbd_osd_call_callback(struct rbd_obj_request * obj_request)1803 static void rbd_osd_call_callback(struct rbd_obj_request *obj_request)
1804 {
1805 dout("%s: obj %p\n", __func__, obj_request);
1806
1807 if (obj_request_img_data_test(obj_request))
1808 rbd_osd_copyup_callback(obj_request);
1809 else
1810 obj_request_done_set(obj_request);
1811 }
1812
rbd_osd_req_callback(struct ceph_osd_request * osd_req,struct ceph_msg * msg)1813 static void rbd_osd_req_callback(struct ceph_osd_request *osd_req,
1814 struct ceph_msg *msg)
1815 {
1816 struct rbd_obj_request *obj_request = osd_req->r_priv;
1817 u16 opcode;
1818
1819 dout("%s: osd_req %p msg %p\n", __func__, osd_req, msg);
1820 rbd_assert(osd_req == obj_request->osd_req);
1821 if (obj_request_img_data_test(obj_request)) {
1822 rbd_assert(obj_request->img_request);
1823 rbd_assert(obj_request->which != BAD_WHICH);
1824 } else {
1825 rbd_assert(obj_request->which == BAD_WHICH);
1826 }
1827
1828 if (osd_req->r_result < 0)
1829 obj_request->result = osd_req->r_result;
1830
1831 rbd_assert(osd_req->r_num_ops <= CEPH_OSD_MAX_OP);
1832
1833 /*
1834 * We support a 64-bit length, but ultimately it has to be
1835 * passed to the block layer, which just supports a 32-bit
1836 * length field.
1837 */
1838 obj_request->xferred = osd_req->r_reply_op_len[0];
1839 rbd_assert(obj_request->xferred < (u64)UINT_MAX);
1840
1841 opcode = osd_req->r_ops[0].op;
1842 switch (opcode) {
1843 case CEPH_OSD_OP_READ:
1844 rbd_osd_read_callback(obj_request);
1845 break;
1846 case CEPH_OSD_OP_SETALLOCHINT:
1847 rbd_assert(osd_req->r_ops[1].op == CEPH_OSD_OP_WRITE);
1848 /* fall through */
1849 case CEPH_OSD_OP_WRITE:
1850 rbd_osd_write_callback(obj_request);
1851 break;
1852 case CEPH_OSD_OP_STAT:
1853 rbd_osd_stat_callback(obj_request);
1854 break;
1855 case CEPH_OSD_OP_DELETE:
1856 case CEPH_OSD_OP_TRUNCATE:
1857 case CEPH_OSD_OP_ZERO:
1858 rbd_osd_discard_callback(obj_request);
1859 break;
1860 case CEPH_OSD_OP_CALL:
1861 rbd_osd_call_callback(obj_request);
1862 break;
1863 case CEPH_OSD_OP_NOTIFY_ACK:
1864 case CEPH_OSD_OP_WATCH:
1865 rbd_osd_trivial_callback(obj_request);
1866 break;
1867 default:
1868 rbd_warn(NULL, "%s: unsupported op %hu",
1869 obj_request->object_name, (unsigned short) opcode);
1870 break;
1871 }
1872
1873 if (obj_request_done_test(obj_request))
1874 rbd_obj_request_complete(obj_request);
1875 }
1876
rbd_osd_req_format_read(struct rbd_obj_request * obj_request)1877 static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request)
1878 {
1879 struct rbd_img_request *img_request = obj_request->img_request;
1880 struct ceph_osd_request *osd_req = obj_request->osd_req;
1881 u64 snap_id;
1882
1883 rbd_assert(osd_req != NULL);
1884
1885 snap_id = img_request ? img_request->snap_id : CEPH_NOSNAP;
1886 ceph_osdc_build_request(osd_req, obj_request->offset,
1887 NULL, snap_id, NULL);
1888 }
1889
rbd_osd_req_format_write(struct rbd_obj_request * obj_request)1890 static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request)
1891 {
1892 struct rbd_img_request *img_request = obj_request->img_request;
1893 struct ceph_osd_request *osd_req = obj_request->osd_req;
1894 struct ceph_snap_context *snapc;
1895 struct timespec mtime = CURRENT_TIME;
1896
1897 rbd_assert(osd_req != NULL);
1898
1899 snapc = img_request ? img_request->snapc : NULL;
1900 ceph_osdc_build_request(osd_req, obj_request->offset,
1901 snapc, CEPH_NOSNAP, &mtime);
1902 }
1903
1904 /*
1905 * Create an osd request. A read request has one osd op (read).
1906 * A write request has either one (watch) or two (hint+write) osd ops.
1907 * (All rbd data writes are prefixed with an allocation hint op, but
1908 * technically osd watch is a write request, hence this distinction.)
1909 */
rbd_osd_req_create(struct rbd_device * rbd_dev,enum obj_operation_type op_type,unsigned int num_ops,struct rbd_obj_request * obj_request)1910 static struct ceph_osd_request *rbd_osd_req_create(
1911 struct rbd_device *rbd_dev,
1912 enum obj_operation_type op_type,
1913 unsigned int num_ops,
1914 struct rbd_obj_request *obj_request)
1915 {
1916 struct ceph_snap_context *snapc = NULL;
1917 struct ceph_osd_client *osdc;
1918 struct ceph_osd_request *osd_req;
1919
1920 if (obj_request_img_data_test(obj_request) &&
1921 (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_WRITE)) {
1922 struct rbd_img_request *img_request = obj_request->img_request;
1923 if (op_type == OBJ_OP_WRITE) {
1924 rbd_assert(img_request_write_test(img_request));
1925 } else {
1926 rbd_assert(img_request_discard_test(img_request));
1927 }
1928 snapc = img_request->snapc;
1929 }
1930
1931 rbd_assert(num_ops == 1 || ((op_type == OBJ_OP_WRITE) && num_ops == 2));
1932
1933 /* Allocate and initialize the request, for the num_ops ops */
1934
1935 osdc = &rbd_dev->rbd_client->client->osdc;
1936 osd_req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false,
1937 GFP_NOIO);
1938 if (!osd_req)
1939 return NULL; /* ENOMEM */
1940
1941 if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD)
1942 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
1943 else
1944 osd_req->r_flags = CEPH_OSD_FLAG_READ;
1945
1946 osd_req->r_callback = rbd_osd_req_callback;
1947 osd_req->r_priv = obj_request;
1948
1949 osd_req->r_base_oloc.pool = ceph_file_layout_pg_pool(rbd_dev->layout);
1950 ceph_oid_set_name(&osd_req->r_base_oid, obj_request->object_name);
1951
1952 return osd_req;
1953 }
1954
1955 /*
1956 * Create a copyup osd request based on the information in the object
1957 * request supplied. A copyup request has two or three osd ops, a
1958 * copyup method call, potentially a hint op, and a write or truncate
1959 * or zero op.
1960 */
1961 static struct ceph_osd_request *
rbd_osd_req_create_copyup(struct rbd_obj_request * obj_request)1962 rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request)
1963 {
1964 struct rbd_img_request *img_request;
1965 struct ceph_snap_context *snapc;
1966 struct rbd_device *rbd_dev;
1967 struct ceph_osd_client *osdc;
1968 struct ceph_osd_request *osd_req;
1969 int num_osd_ops = 3;
1970
1971 rbd_assert(obj_request_img_data_test(obj_request));
1972 img_request = obj_request->img_request;
1973 rbd_assert(img_request);
1974 rbd_assert(img_request_write_test(img_request) ||
1975 img_request_discard_test(img_request));
1976
1977 if (img_request_discard_test(img_request))
1978 num_osd_ops = 2;
1979
1980 /* Allocate and initialize the request, for all the ops */
1981
1982 snapc = img_request->snapc;
1983 rbd_dev = img_request->rbd_dev;
1984 osdc = &rbd_dev->rbd_client->client->osdc;
1985 osd_req = ceph_osdc_alloc_request(osdc, snapc, num_osd_ops,
1986 false, GFP_NOIO);
1987 if (!osd_req)
1988 return NULL; /* ENOMEM */
1989
1990 osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
1991 osd_req->r_callback = rbd_osd_req_callback;
1992 osd_req->r_priv = obj_request;
1993
1994 osd_req->r_base_oloc.pool = ceph_file_layout_pg_pool(rbd_dev->layout);
1995 ceph_oid_set_name(&osd_req->r_base_oid, obj_request->object_name);
1996
1997 return osd_req;
1998 }
1999
2000
rbd_osd_req_destroy(struct ceph_osd_request * osd_req)2001 static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req)
2002 {
2003 ceph_osdc_put_request(osd_req);
2004 }
2005
2006 /* object_name is assumed to be a non-null pointer and NUL-terminated */
2007
rbd_obj_request_create(const char * object_name,u64 offset,u64 length,enum obj_request_type type)2008 static struct rbd_obj_request *rbd_obj_request_create(const char *object_name,
2009 u64 offset, u64 length,
2010 enum obj_request_type type)
2011 {
2012 struct rbd_obj_request *obj_request;
2013 size_t size;
2014 char *name;
2015
2016 rbd_assert(obj_request_type_valid(type));
2017
2018 size = strlen(object_name) + 1;
2019 name = kmalloc(size, GFP_NOIO);
2020 if (!name)
2021 return NULL;
2022
2023 obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO);
2024 if (!obj_request) {
2025 kfree(name);
2026 return NULL;
2027 }
2028
2029 obj_request->object_name = memcpy(name, object_name, size);
2030 obj_request->offset = offset;
2031 obj_request->length = length;
2032 obj_request->flags = 0;
2033 obj_request->which = BAD_WHICH;
2034 obj_request->type = type;
2035 INIT_LIST_HEAD(&obj_request->links);
2036 init_completion(&obj_request->completion);
2037 kref_init(&obj_request->kref);
2038
2039 dout("%s: \"%s\" %llu/%llu %d -> obj %p\n", __func__, object_name,
2040 offset, length, (int)type, obj_request);
2041
2042 return obj_request;
2043 }
2044
rbd_obj_request_destroy(struct kref * kref)2045 static void rbd_obj_request_destroy(struct kref *kref)
2046 {
2047 struct rbd_obj_request *obj_request;
2048
2049 obj_request = container_of(kref, struct rbd_obj_request, kref);
2050
2051 dout("%s: obj %p\n", __func__, obj_request);
2052
2053 rbd_assert(obj_request->img_request == NULL);
2054 rbd_assert(obj_request->which == BAD_WHICH);
2055
2056 if (obj_request->osd_req)
2057 rbd_osd_req_destroy(obj_request->osd_req);
2058
2059 rbd_assert(obj_request_type_valid(obj_request->type));
2060 switch (obj_request->type) {
2061 case OBJ_REQUEST_NODATA:
2062 break; /* Nothing to do */
2063 case OBJ_REQUEST_BIO:
2064 if (obj_request->bio_list)
2065 bio_chain_put(obj_request->bio_list);
2066 break;
2067 case OBJ_REQUEST_PAGES:
2068 if (obj_request->pages)
2069 ceph_release_page_vector(obj_request->pages,
2070 obj_request->page_count);
2071 break;
2072 }
2073
2074 kfree(obj_request->object_name);
2075 obj_request->object_name = NULL;
2076 kmem_cache_free(rbd_obj_request_cache, obj_request);
2077 }
2078
2079 /* It's OK to call this for a device with no parent */
2080
2081 static void rbd_spec_put(struct rbd_spec *spec);
rbd_dev_unparent(struct rbd_device * rbd_dev)2082 static void rbd_dev_unparent(struct rbd_device *rbd_dev)
2083 {
2084 rbd_dev_remove_parent(rbd_dev);
2085 rbd_spec_put(rbd_dev->parent_spec);
2086 rbd_dev->parent_spec = NULL;
2087 rbd_dev->parent_overlap = 0;
2088 }
2089
2090 /*
2091 * Parent image reference counting is used to determine when an
2092 * image's parent fields can be safely torn down--after there are no
2093 * more in-flight requests to the parent image. When the last
2094 * reference is dropped, cleaning them up is safe.
2095 */
rbd_dev_parent_put(struct rbd_device * rbd_dev)2096 static void rbd_dev_parent_put(struct rbd_device *rbd_dev)
2097 {
2098 int counter;
2099
2100 if (!rbd_dev->parent_spec)
2101 return;
2102
2103 counter = atomic_dec_return_safe(&rbd_dev->parent_ref);
2104 if (counter > 0)
2105 return;
2106
2107 /* Last reference; clean up parent data structures */
2108
2109 if (!counter)
2110 rbd_dev_unparent(rbd_dev);
2111 else
2112 rbd_warn(rbd_dev, "parent reference underflow");
2113 }
2114
2115 /*
2116 * If an image has a non-zero parent overlap, get a reference to its
2117 * parent.
2118 *
2119 * Returns true if the rbd device has a parent with a non-zero
2120 * overlap and a reference for it was successfully taken, or
2121 * false otherwise.
2122 */
rbd_dev_parent_get(struct rbd_device * rbd_dev)2123 static bool rbd_dev_parent_get(struct rbd_device *rbd_dev)
2124 {
2125 int counter = 0;
2126
2127 if (!rbd_dev->parent_spec)
2128 return false;
2129
2130 down_read(&rbd_dev->header_rwsem);
2131 if (rbd_dev->parent_overlap)
2132 counter = atomic_inc_return_safe(&rbd_dev->parent_ref);
2133 up_read(&rbd_dev->header_rwsem);
2134
2135 if (counter < 0)
2136 rbd_warn(rbd_dev, "parent reference overflow");
2137
2138 return counter > 0;
2139 }
2140
2141 /*
2142 * Caller is responsible for filling in the list of object requests
2143 * that comprises the image request, and the Linux request pointer
2144 * (if there is one).
2145 */
rbd_img_request_create(struct rbd_device * rbd_dev,u64 offset,u64 length,enum obj_operation_type op_type,struct ceph_snap_context * snapc)2146 static struct rbd_img_request *rbd_img_request_create(
2147 struct rbd_device *rbd_dev,
2148 u64 offset, u64 length,
2149 enum obj_operation_type op_type,
2150 struct ceph_snap_context *snapc)
2151 {
2152 struct rbd_img_request *img_request;
2153
2154 img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_NOIO);
2155 if (!img_request)
2156 return NULL;
2157
2158 img_request->rq = NULL;
2159 img_request->rbd_dev = rbd_dev;
2160 img_request->offset = offset;
2161 img_request->length = length;
2162 img_request->flags = 0;
2163 if (op_type == OBJ_OP_DISCARD) {
2164 img_request_discard_set(img_request);
2165 img_request->snapc = snapc;
2166 } else if (op_type == OBJ_OP_WRITE) {
2167 img_request_write_set(img_request);
2168 img_request->snapc = snapc;
2169 } else {
2170 img_request->snap_id = rbd_dev->spec->snap_id;
2171 }
2172 if (rbd_dev_parent_get(rbd_dev))
2173 img_request_layered_set(img_request);
2174 spin_lock_init(&img_request->completion_lock);
2175 img_request->next_completion = 0;
2176 img_request->callback = NULL;
2177 img_request->result = 0;
2178 img_request->obj_request_count = 0;
2179 INIT_LIST_HEAD(&img_request->obj_requests);
2180 kref_init(&img_request->kref);
2181
2182 dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev,
2183 obj_op_name(op_type), offset, length, img_request);
2184
2185 return img_request;
2186 }
2187
rbd_img_request_destroy(struct kref * kref)2188 static void rbd_img_request_destroy(struct kref *kref)
2189 {
2190 struct rbd_img_request *img_request;
2191 struct rbd_obj_request *obj_request;
2192 struct rbd_obj_request *next_obj_request;
2193
2194 img_request = container_of(kref, struct rbd_img_request, kref);
2195
2196 dout("%s: img %p\n", __func__, img_request);
2197
2198 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
2199 rbd_img_obj_request_del(img_request, obj_request);
2200 rbd_assert(img_request->obj_request_count == 0);
2201
2202 if (img_request_layered_test(img_request)) {
2203 img_request_layered_clear(img_request);
2204 rbd_dev_parent_put(img_request->rbd_dev);
2205 }
2206
2207 if (img_request_write_test(img_request) ||
2208 img_request_discard_test(img_request))
2209 ceph_put_snap_context(img_request->snapc);
2210
2211 kmem_cache_free(rbd_img_request_cache, img_request);
2212 }
2213
rbd_parent_request_create(struct rbd_obj_request * obj_request,u64 img_offset,u64 length)2214 static struct rbd_img_request *rbd_parent_request_create(
2215 struct rbd_obj_request *obj_request,
2216 u64 img_offset, u64 length)
2217 {
2218 struct rbd_img_request *parent_request;
2219 struct rbd_device *rbd_dev;
2220
2221 rbd_assert(obj_request->img_request);
2222 rbd_dev = obj_request->img_request->rbd_dev;
2223
2224 parent_request = rbd_img_request_create(rbd_dev->parent, img_offset,
2225 length, OBJ_OP_READ, NULL);
2226 if (!parent_request)
2227 return NULL;
2228
2229 img_request_child_set(parent_request);
2230 rbd_obj_request_get(obj_request);
2231 parent_request->obj_request = obj_request;
2232
2233 return parent_request;
2234 }
2235
rbd_parent_request_destroy(struct kref * kref)2236 static void rbd_parent_request_destroy(struct kref *kref)
2237 {
2238 struct rbd_img_request *parent_request;
2239 struct rbd_obj_request *orig_request;
2240
2241 parent_request = container_of(kref, struct rbd_img_request, kref);
2242 orig_request = parent_request->obj_request;
2243
2244 parent_request->obj_request = NULL;
2245 rbd_obj_request_put(orig_request);
2246 img_request_child_clear(parent_request);
2247
2248 rbd_img_request_destroy(kref);
2249 }
2250
rbd_img_obj_end_request(struct rbd_obj_request * obj_request)2251 static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request)
2252 {
2253 struct rbd_img_request *img_request;
2254 unsigned int xferred;
2255 int result;
2256 bool more;
2257
2258 rbd_assert(obj_request_img_data_test(obj_request));
2259 img_request = obj_request->img_request;
2260
2261 rbd_assert(obj_request->xferred <= (u64)UINT_MAX);
2262 xferred = (unsigned int)obj_request->xferred;
2263 result = obj_request->result;
2264 if (result) {
2265 struct rbd_device *rbd_dev = img_request->rbd_dev;
2266 enum obj_operation_type op_type;
2267
2268 if (img_request_discard_test(img_request))
2269 op_type = OBJ_OP_DISCARD;
2270 else if (img_request_write_test(img_request))
2271 op_type = OBJ_OP_WRITE;
2272 else
2273 op_type = OBJ_OP_READ;
2274
2275 rbd_warn(rbd_dev, "%s %llx at %llx (%llx)",
2276 obj_op_name(op_type), obj_request->length,
2277 obj_request->img_offset, obj_request->offset);
2278 rbd_warn(rbd_dev, " result %d xferred %x",
2279 result, xferred);
2280 if (!img_request->result)
2281 img_request->result = result;
2282 /*
2283 * Need to end I/O on the entire obj_request worth of
2284 * bytes in case of error.
2285 */
2286 xferred = obj_request->length;
2287 }
2288
2289 /* Image object requests don't own their page array */
2290
2291 if (obj_request->type == OBJ_REQUEST_PAGES) {
2292 obj_request->pages = NULL;
2293 obj_request->page_count = 0;
2294 }
2295
2296 if (img_request_child_test(img_request)) {
2297 rbd_assert(img_request->obj_request != NULL);
2298 more = obj_request->which < img_request->obj_request_count - 1;
2299 } else {
2300 rbd_assert(img_request->rq != NULL);
2301
2302 more = blk_update_request(img_request->rq, result, xferred);
2303 if (!more)
2304 __blk_mq_end_request(img_request->rq, result);
2305 }
2306
2307 return more;
2308 }
2309
rbd_img_obj_callback(struct rbd_obj_request * obj_request)2310 static void rbd_img_obj_callback(struct rbd_obj_request *obj_request)
2311 {
2312 struct rbd_img_request *img_request;
2313 u32 which = obj_request->which;
2314 bool more = true;
2315
2316 rbd_assert(obj_request_img_data_test(obj_request));
2317 img_request = obj_request->img_request;
2318
2319 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
2320 rbd_assert(img_request != NULL);
2321 rbd_assert(img_request->obj_request_count > 0);
2322 rbd_assert(which != BAD_WHICH);
2323 rbd_assert(which < img_request->obj_request_count);
2324
2325 spin_lock_irq(&img_request->completion_lock);
2326 if (which != img_request->next_completion)
2327 goto out;
2328
2329 for_each_obj_request_from(img_request, obj_request) {
2330 rbd_assert(more);
2331 rbd_assert(which < img_request->obj_request_count);
2332
2333 if (!obj_request_done_test(obj_request))
2334 break;
2335 more = rbd_img_obj_end_request(obj_request);
2336 which++;
2337 }
2338
2339 rbd_assert(more ^ (which == img_request->obj_request_count));
2340 img_request->next_completion = which;
2341 out:
2342 spin_unlock_irq(&img_request->completion_lock);
2343 rbd_img_request_put(img_request);
2344
2345 if (!more)
2346 rbd_img_request_complete(img_request);
2347 }
2348
2349 /*
2350 * Add individual osd ops to the given ceph_osd_request and prepare
2351 * them for submission. num_ops is the current number of
2352 * osd operations already to the object request.
2353 */
rbd_img_obj_request_fill(struct rbd_obj_request * obj_request,struct ceph_osd_request * osd_request,enum obj_operation_type op_type,unsigned int num_ops)2354 static void rbd_img_obj_request_fill(struct rbd_obj_request *obj_request,
2355 struct ceph_osd_request *osd_request,
2356 enum obj_operation_type op_type,
2357 unsigned int num_ops)
2358 {
2359 struct rbd_img_request *img_request = obj_request->img_request;
2360 struct rbd_device *rbd_dev = img_request->rbd_dev;
2361 u64 object_size = rbd_obj_bytes(&rbd_dev->header);
2362 u64 offset = obj_request->offset;
2363 u64 length = obj_request->length;
2364 u64 img_end;
2365 u16 opcode;
2366
2367 if (op_type == OBJ_OP_DISCARD) {
2368 if (!offset && length == object_size &&
2369 (!img_request_layered_test(img_request) ||
2370 !obj_request_overlaps_parent(obj_request))) {
2371 opcode = CEPH_OSD_OP_DELETE;
2372 } else if ((offset + length == object_size)) {
2373 opcode = CEPH_OSD_OP_TRUNCATE;
2374 } else {
2375 down_read(&rbd_dev->header_rwsem);
2376 img_end = rbd_dev->header.image_size;
2377 up_read(&rbd_dev->header_rwsem);
2378
2379 if (obj_request->img_offset + length == img_end)
2380 opcode = CEPH_OSD_OP_TRUNCATE;
2381 else
2382 opcode = CEPH_OSD_OP_ZERO;
2383 }
2384 } else if (op_type == OBJ_OP_WRITE) {
2385 opcode = CEPH_OSD_OP_WRITE;
2386 osd_req_op_alloc_hint_init(osd_request, num_ops,
2387 object_size, object_size);
2388 num_ops++;
2389 } else {
2390 opcode = CEPH_OSD_OP_READ;
2391 }
2392
2393 if (opcode == CEPH_OSD_OP_DELETE)
2394 osd_req_op_init(osd_request, num_ops, opcode);
2395 else
2396 osd_req_op_extent_init(osd_request, num_ops, opcode,
2397 offset, length, 0, 0);
2398
2399 if (obj_request->type == OBJ_REQUEST_BIO)
2400 osd_req_op_extent_osd_data_bio(osd_request, num_ops,
2401 obj_request->bio_list, length);
2402 else if (obj_request->type == OBJ_REQUEST_PAGES)
2403 osd_req_op_extent_osd_data_pages(osd_request, num_ops,
2404 obj_request->pages, length,
2405 offset & ~PAGE_MASK, false, false);
2406
2407 /* Discards are also writes */
2408 if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD)
2409 rbd_osd_req_format_write(obj_request);
2410 else
2411 rbd_osd_req_format_read(obj_request);
2412 }
2413
2414 /*
2415 * Split up an image request into one or more object requests, each
2416 * to a different object. The "type" parameter indicates whether
2417 * "data_desc" is the pointer to the head of a list of bio
2418 * structures, or the base of a page array. In either case this
2419 * function assumes data_desc describes memory sufficient to hold
2420 * all data described by the image request.
2421 */
rbd_img_request_fill(struct rbd_img_request * img_request,enum obj_request_type type,void * data_desc)2422 static int rbd_img_request_fill(struct rbd_img_request *img_request,
2423 enum obj_request_type type,
2424 void *data_desc)
2425 {
2426 struct rbd_device *rbd_dev = img_request->rbd_dev;
2427 struct rbd_obj_request *obj_request = NULL;
2428 struct rbd_obj_request *next_obj_request;
2429 struct bio *bio_list = NULL;
2430 unsigned int bio_offset = 0;
2431 struct page **pages = NULL;
2432 enum obj_operation_type op_type;
2433 u64 img_offset;
2434 u64 resid;
2435
2436 dout("%s: img %p type %d data_desc %p\n", __func__, img_request,
2437 (int)type, data_desc);
2438
2439 img_offset = img_request->offset;
2440 resid = img_request->length;
2441 rbd_assert(resid > 0);
2442 op_type = rbd_img_request_op_type(img_request);
2443
2444 if (type == OBJ_REQUEST_BIO) {
2445 bio_list = data_desc;
2446 rbd_assert(img_offset ==
2447 bio_list->bi_iter.bi_sector << SECTOR_SHIFT);
2448 } else if (type == OBJ_REQUEST_PAGES) {
2449 pages = data_desc;
2450 }
2451
2452 while (resid) {
2453 struct ceph_osd_request *osd_req;
2454 const char *object_name;
2455 u64 offset;
2456 u64 length;
2457
2458 object_name = rbd_segment_name(rbd_dev, img_offset);
2459 if (!object_name)
2460 goto out_unwind;
2461 offset = rbd_segment_offset(rbd_dev, img_offset);
2462 length = rbd_segment_length(rbd_dev, img_offset, resid);
2463 obj_request = rbd_obj_request_create(object_name,
2464 offset, length, type);
2465 /* object request has its own copy of the object name */
2466 rbd_segment_name_free(object_name);
2467 if (!obj_request)
2468 goto out_unwind;
2469
2470 /*
2471 * set obj_request->img_request before creating the
2472 * osd_request so that it gets the right snapc
2473 */
2474 rbd_img_obj_request_add(img_request, obj_request);
2475
2476 if (type == OBJ_REQUEST_BIO) {
2477 unsigned int clone_size;
2478
2479 rbd_assert(length <= (u64)UINT_MAX);
2480 clone_size = (unsigned int)length;
2481 obj_request->bio_list =
2482 bio_chain_clone_range(&bio_list,
2483 &bio_offset,
2484 clone_size,
2485 GFP_NOIO);
2486 if (!obj_request->bio_list)
2487 goto out_unwind;
2488 } else if (type == OBJ_REQUEST_PAGES) {
2489 unsigned int page_count;
2490
2491 obj_request->pages = pages;
2492 page_count = (u32)calc_pages_for(offset, length);
2493 obj_request->page_count = page_count;
2494 if ((offset + length) & ~PAGE_MASK)
2495 page_count--; /* more on last page */
2496 pages += page_count;
2497 }
2498
2499 osd_req = rbd_osd_req_create(rbd_dev, op_type,
2500 (op_type == OBJ_OP_WRITE) ? 2 : 1,
2501 obj_request);
2502 if (!osd_req)
2503 goto out_unwind;
2504
2505 obj_request->osd_req = osd_req;
2506 obj_request->callback = rbd_img_obj_callback;
2507 obj_request->img_offset = img_offset;
2508
2509 rbd_img_obj_request_fill(obj_request, osd_req, op_type, 0);
2510
2511 rbd_img_request_get(img_request);
2512
2513 img_offset += length;
2514 resid -= length;
2515 }
2516
2517 return 0;
2518
2519 out_unwind:
2520 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
2521 rbd_img_obj_request_del(img_request, obj_request);
2522
2523 return -ENOMEM;
2524 }
2525
2526 static void
rbd_osd_copyup_callback(struct rbd_obj_request * obj_request)2527 rbd_osd_copyup_callback(struct rbd_obj_request *obj_request)
2528 {
2529 struct rbd_img_request *img_request;
2530 struct rbd_device *rbd_dev;
2531 struct page **pages;
2532 u32 page_count;
2533
2534 dout("%s: obj %p\n", __func__, obj_request);
2535
2536 rbd_assert(obj_request->type == OBJ_REQUEST_BIO ||
2537 obj_request->type == OBJ_REQUEST_NODATA);
2538 rbd_assert(obj_request_img_data_test(obj_request));
2539 img_request = obj_request->img_request;
2540 rbd_assert(img_request);
2541
2542 rbd_dev = img_request->rbd_dev;
2543 rbd_assert(rbd_dev);
2544
2545 pages = obj_request->copyup_pages;
2546 rbd_assert(pages != NULL);
2547 obj_request->copyup_pages = NULL;
2548 page_count = obj_request->copyup_page_count;
2549 rbd_assert(page_count);
2550 obj_request->copyup_page_count = 0;
2551 ceph_release_page_vector(pages, page_count);
2552
2553 /*
2554 * We want the transfer count to reflect the size of the
2555 * original write request. There is no such thing as a
2556 * successful short write, so if the request was successful
2557 * we can just set it to the originally-requested length.
2558 */
2559 if (!obj_request->result)
2560 obj_request->xferred = obj_request->length;
2561
2562 obj_request_done_set(obj_request);
2563 }
2564
2565 static void
rbd_img_obj_parent_read_full_callback(struct rbd_img_request * img_request)2566 rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request)
2567 {
2568 struct rbd_obj_request *orig_request;
2569 struct ceph_osd_request *osd_req;
2570 struct ceph_osd_client *osdc;
2571 struct rbd_device *rbd_dev;
2572 struct page **pages;
2573 enum obj_operation_type op_type;
2574 u32 page_count;
2575 int img_result;
2576 u64 parent_length;
2577
2578 rbd_assert(img_request_child_test(img_request));
2579
2580 /* First get what we need from the image request */
2581
2582 pages = img_request->copyup_pages;
2583 rbd_assert(pages != NULL);
2584 img_request->copyup_pages = NULL;
2585 page_count = img_request->copyup_page_count;
2586 rbd_assert(page_count);
2587 img_request->copyup_page_count = 0;
2588
2589 orig_request = img_request->obj_request;
2590 rbd_assert(orig_request != NULL);
2591 rbd_assert(obj_request_type_valid(orig_request->type));
2592 img_result = img_request->result;
2593 parent_length = img_request->length;
2594 rbd_assert(parent_length == img_request->xferred);
2595 rbd_img_request_put(img_request);
2596
2597 rbd_assert(orig_request->img_request);
2598 rbd_dev = orig_request->img_request->rbd_dev;
2599 rbd_assert(rbd_dev);
2600
2601 /*
2602 * If the overlap has become 0 (most likely because the
2603 * image has been flattened) we need to free the pages
2604 * and re-submit the original write request.
2605 */
2606 if (!rbd_dev->parent_overlap) {
2607 struct ceph_osd_client *osdc;
2608
2609 ceph_release_page_vector(pages, page_count);
2610 osdc = &rbd_dev->rbd_client->client->osdc;
2611 img_result = rbd_obj_request_submit(osdc, orig_request);
2612 if (!img_result)
2613 return;
2614 }
2615
2616 if (img_result)
2617 goto out_err;
2618
2619 /*
2620 * The original osd request is of no use to use any more.
2621 * We need a new one that can hold the three ops in a copyup
2622 * request. Allocate the new copyup osd request for the
2623 * original request, and release the old one.
2624 */
2625 img_result = -ENOMEM;
2626 osd_req = rbd_osd_req_create_copyup(orig_request);
2627 if (!osd_req)
2628 goto out_err;
2629 rbd_osd_req_destroy(orig_request->osd_req);
2630 orig_request->osd_req = osd_req;
2631 orig_request->copyup_pages = pages;
2632 orig_request->copyup_page_count = page_count;
2633
2634 /* Initialize the copyup op */
2635
2636 osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup");
2637 osd_req_op_cls_request_data_pages(osd_req, 0, pages, parent_length, 0,
2638 false, false);
2639
2640 /* Add the other op(s) */
2641
2642 op_type = rbd_img_request_op_type(orig_request->img_request);
2643 rbd_img_obj_request_fill(orig_request, osd_req, op_type, 1);
2644
2645 /* All set, send it off. */
2646
2647 osdc = &rbd_dev->rbd_client->client->osdc;
2648 img_result = rbd_obj_request_submit(osdc, orig_request);
2649 if (!img_result)
2650 return;
2651 out_err:
2652 /* Record the error code and complete the request */
2653
2654 orig_request->result = img_result;
2655 orig_request->xferred = 0;
2656 obj_request_done_set(orig_request);
2657 rbd_obj_request_complete(orig_request);
2658 }
2659
2660 /*
2661 * Read from the parent image the range of data that covers the
2662 * entire target of the given object request. This is used for
2663 * satisfying a layered image write request when the target of an
2664 * object request from the image request does not exist.
2665 *
2666 * A page array big enough to hold the returned data is allocated
2667 * and supplied to rbd_img_request_fill() as the "data descriptor."
2668 * When the read completes, this page array will be transferred to
2669 * the original object request for the copyup operation.
2670 *
2671 * If an error occurs, record it as the result of the original
2672 * object request and mark it done so it gets completed.
2673 */
rbd_img_obj_parent_read_full(struct rbd_obj_request * obj_request)2674 static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request)
2675 {
2676 struct rbd_img_request *img_request = NULL;
2677 struct rbd_img_request *parent_request = NULL;
2678 struct rbd_device *rbd_dev;
2679 u64 img_offset;
2680 u64 length;
2681 struct page **pages = NULL;
2682 u32 page_count;
2683 int result;
2684
2685 rbd_assert(obj_request_img_data_test(obj_request));
2686 rbd_assert(obj_request_type_valid(obj_request->type));
2687
2688 img_request = obj_request->img_request;
2689 rbd_assert(img_request != NULL);
2690 rbd_dev = img_request->rbd_dev;
2691 rbd_assert(rbd_dev->parent != NULL);
2692
2693 /*
2694 * Determine the byte range covered by the object in the
2695 * child image to which the original request was to be sent.
2696 */
2697 img_offset = obj_request->img_offset - obj_request->offset;
2698 length = (u64)1 << rbd_dev->header.obj_order;
2699
2700 /*
2701 * There is no defined parent data beyond the parent
2702 * overlap, so limit what we read at that boundary if
2703 * necessary.
2704 */
2705 if (img_offset + length > rbd_dev->parent_overlap) {
2706 rbd_assert(img_offset < rbd_dev->parent_overlap);
2707 length = rbd_dev->parent_overlap - img_offset;
2708 }
2709
2710 /*
2711 * Allocate a page array big enough to receive the data read
2712 * from the parent.
2713 */
2714 page_count = (u32)calc_pages_for(0, length);
2715 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2716 if (IS_ERR(pages)) {
2717 result = PTR_ERR(pages);
2718 pages = NULL;
2719 goto out_err;
2720 }
2721
2722 result = -ENOMEM;
2723 parent_request = rbd_parent_request_create(obj_request,
2724 img_offset, length);
2725 if (!parent_request)
2726 goto out_err;
2727
2728 result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages);
2729 if (result)
2730 goto out_err;
2731 parent_request->copyup_pages = pages;
2732 parent_request->copyup_page_count = page_count;
2733
2734 parent_request->callback = rbd_img_obj_parent_read_full_callback;
2735 result = rbd_img_request_submit(parent_request);
2736 if (!result)
2737 return 0;
2738
2739 parent_request->copyup_pages = NULL;
2740 parent_request->copyup_page_count = 0;
2741 parent_request->obj_request = NULL;
2742 rbd_obj_request_put(obj_request);
2743 out_err:
2744 if (pages)
2745 ceph_release_page_vector(pages, page_count);
2746 if (parent_request)
2747 rbd_img_request_put(parent_request);
2748 obj_request->result = result;
2749 obj_request->xferred = 0;
2750 obj_request_done_set(obj_request);
2751
2752 return result;
2753 }
2754
rbd_img_obj_exists_callback(struct rbd_obj_request * obj_request)2755 static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request)
2756 {
2757 struct rbd_obj_request *orig_request;
2758 struct rbd_device *rbd_dev;
2759 int result;
2760
2761 rbd_assert(!obj_request_img_data_test(obj_request));
2762
2763 /*
2764 * All we need from the object request is the original
2765 * request and the result of the STAT op. Grab those, then
2766 * we're done with the request.
2767 */
2768 orig_request = obj_request->obj_request;
2769 obj_request->obj_request = NULL;
2770 rbd_obj_request_put(orig_request);
2771 rbd_assert(orig_request);
2772 rbd_assert(orig_request->img_request);
2773
2774 result = obj_request->result;
2775 obj_request->result = 0;
2776
2777 dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__,
2778 obj_request, orig_request, result,
2779 obj_request->xferred, obj_request->length);
2780 rbd_obj_request_put(obj_request);
2781
2782 /*
2783 * If the overlap has become 0 (most likely because the
2784 * image has been flattened) we need to free the pages
2785 * and re-submit the original write request.
2786 */
2787 rbd_dev = orig_request->img_request->rbd_dev;
2788 if (!rbd_dev->parent_overlap) {
2789 struct ceph_osd_client *osdc;
2790
2791 osdc = &rbd_dev->rbd_client->client->osdc;
2792 result = rbd_obj_request_submit(osdc, orig_request);
2793 if (!result)
2794 return;
2795 }
2796
2797 /*
2798 * Our only purpose here is to determine whether the object
2799 * exists, and we don't want to treat the non-existence as
2800 * an error. If something else comes back, transfer the
2801 * error to the original request and complete it now.
2802 */
2803 if (!result) {
2804 obj_request_existence_set(orig_request, true);
2805 } else if (result == -ENOENT) {
2806 obj_request_existence_set(orig_request, false);
2807 } else if (result) {
2808 orig_request->result = result;
2809 goto out;
2810 }
2811
2812 /*
2813 * Resubmit the original request now that we have recorded
2814 * whether the target object exists.
2815 */
2816 orig_request->result = rbd_img_obj_request_submit(orig_request);
2817 out:
2818 if (orig_request->result)
2819 rbd_obj_request_complete(orig_request);
2820 }
2821
rbd_img_obj_exists_submit(struct rbd_obj_request * obj_request)2822 static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request)
2823 {
2824 struct rbd_obj_request *stat_request;
2825 struct rbd_device *rbd_dev;
2826 struct ceph_osd_client *osdc;
2827 struct page **pages = NULL;
2828 u32 page_count;
2829 size_t size;
2830 int ret;
2831
2832 /*
2833 * The response data for a STAT call consists of:
2834 * le64 length;
2835 * struct {
2836 * le32 tv_sec;
2837 * le32 tv_nsec;
2838 * } mtime;
2839 */
2840 size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32);
2841 page_count = (u32)calc_pages_for(0, size);
2842 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
2843 if (IS_ERR(pages))
2844 return PTR_ERR(pages);
2845
2846 ret = -ENOMEM;
2847 stat_request = rbd_obj_request_create(obj_request->object_name, 0, 0,
2848 OBJ_REQUEST_PAGES);
2849 if (!stat_request)
2850 goto out;
2851
2852 rbd_obj_request_get(obj_request);
2853 stat_request->obj_request = obj_request;
2854 stat_request->pages = pages;
2855 stat_request->page_count = page_count;
2856
2857 rbd_assert(obj_request->img_request);
2858 rbd_dev = obj_request->img_request->rbd_dev;
2859 stat_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1,
2860 stat_request);
2861 if (!stat_request->osd_req)
2862 goto out;
2863 stat_request->callback = rbd_img_obj_exists_callback;
2864
2865 osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT);
2866 osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0,
2867 false, false);
2868 rbd_osd_req_format_read(stat_request);
2869
2870 osdc = &rbd_dev->rbd_client->client->osdc;
2871 ret = rbd_obj_request_submit(osdc, stat_request);
2872 out:
2873 if (ret)
2874 rbd_obj_request_put(obj_request);
2875
2876 return ret;
2877 }
2878
img_obj_request_simple(struct rbd_obj_request * obj_request)2879 static bool img_obj_request_simple(struct rbd_obj_request *obj_request)
2880 {
2881 struct rbd_img_request *img_request;
2882 struct rbd_device *rbd_dev;
2883
2884 rbd_assert(obj_request_img_data_test(obj_request));
2885
2886 img_request = obj_request->img_request;
2887 rbd_assert(img_request);
2888 rbd_dev = img_request->rbd_dev;
2889
2890 /* Reads */
2891 if (!img_request_write_test(img_request) &&
2892 !img_request_discard_test(img_request))
2893 return true;
2894
2895 /* Non-layered writes */
2896 if (!img_request_layered_test(img_request))
2897 return true;
2898
2899 /*
2900 * Layered writes outside of the parent overlap range don't
2901 * share any data with the parent.
2902 */
2903 if (!obj_request_overlaps_parent(obj_request))
2904 return true;
2905
2906 /*
2907 * Entire-object layered writes - we will overwrite whatever
2908 * parent data there is anyway.
2909 */
2910 if (!obj_request->offset &&
2911 obj_request->length == rbd_obj_bytes(&rbd_dev->header))
2912 return true;
2913
2914 /*
2915 * If the object is known to already exist, its parent data has
2916 * already been copied.
2917 */
2918 if (obj_request_known_test(obj_request) &&
2919 obj_request_exists_test(obj_request))
2920 return true;
2921
2922 return false;
2923 }
2924
rbd_img_obj_request_submit(struct rbd_obj_request * obj_request)2925 static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request)
2926 {
2927 if (img_obj_request_simple(obj_request)) {
2928 struct rbd_device *rbd_dev;
2929 struct ceph_osd_client *osdc;
2930
2931 rbd_dev = obj_request->img_request->rbd_dev;
2932 osdc = &rbd_dev->rbd_client->client->osdc;
2933
2934 return rbd_obj_request_submit(osdc, obj_request);
2935 }
2936
2937 /*
2938 * It's a layered write. The target object might exist but
2939 * we may not know that yet. If we know it doesn't exist,
2940 * start by reading the data for the full target object from
2941 * the parent so we can use it for a copyup to the target.
2942 */
2943 if (obj_request_known_test(obj_request))
2944 return rbd_img_obj_parent_read_full(obj_request);
2945
2946 /* We don't know whether the target exists. Go find out. */
2947
2948 return rbd_img_obj_exists_submit(obj_request);
2949 }
2950
rbd_img_request_submit(struct rbd_img_request * img_request)2951 static int rbd_img_request_submit(struct rbd_img_request *img_request)
2952 {
2953 struct rbd_obj_request *obj_request;
2954 struct rbd_obj_request *next_obj_request;
2955
2956 dout("%s: img %p\n", __func__, img_request);
2957 for_each_obj_request_safe(img_request, obj_request, next_obj_request) {
2958 int ret;
2959
2960 ret = rbd_img_obj_request_submit(obj_request);
2961 if (ret)
2962 return ret;
2963 }
2964
2965 return 0;
2966 }
2967
rbd_img_parent_read_callback(struct rbd_img_request * img_request)2968 static void rbd_img_parent_read_callback(struct rbd_img_request *img_request)
2969 {
2970 struct rbd_obj_request *obj_request;
2971 struct rbd_device *rbd_dev;
2972 u64 obj_end;
2973 u64 img_xferred;
2974 int img_result;
2975
2976 rbd_assert(img_request_child_test(img_request));
2977
2978 /* First get what we need from the image request and release it */
2979
2980 obj_request = img_request->obj_request;
2981 img_xferred = img_request->xferred;
2982 img_result = img_request->result;
2983 rbd_img_request_put(img_request);
2984
2985 /*
2986 * If the overlap has become 0 (most likely because the
2987 * image has been flattened) we need to re-submit the
2988 * original request.
2989 */
2990 rbd_assert(obj_request);
2991 rbd_assert(obj_request->img_request);
2992 rbd_dev = obj_request->img_request->rbd_dev;
2993 if (!rbd_dev->parent_overlap) {
2994 struct ceph_osd_client *osdc;
2995
2996 osdc = &rbd_dev->rbd_client->client->osdc;
2997 img_result = rbd_obj_request_submit(osdc, obj_request);
2998 if (!img_result)
2999 return;
3000 }
3001
3002 obj_request->result = img_result;
3003 if (obj_request->result)
3004 goto out;
3005
3006 /*
3007 * We need to zero anything beyond the parent overlap
3008 * boundary. Since rbd_img_obj_request_read_callback()
3009 * will zero anything beyond the end of a short read, an
3010 * easy way to do this is to pretend the data from the
3011 * parent came up short--ending at the overlap boundary.
3012 */
3013 rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length);
3014 obj_end = obj_request->img_offset + obj_request->length;
3015 if (obj_end > rbd_dev->parent_overlap) {
3016 u64 xferred = 0;
3017
3018 if (obj_request->img_offset < rbd_dev->parent_overlap)
3019 xferred = rbd_dev->parent_overlap -
3020 obj_request->img_offset;
3021
3022 obj_request->xferred = min(img_xferred, xferred);
3023 } else {
3024 obj_request->xferred = img_xferred;
3025 }
3026 out:
3027 rbd_img_obj_request_read_callback(obj_request);
3028 rbd_obj_request_complete(obj_request);
3029 }
3030
rbd_img_parent_read(struct rbd_obj_request * obj_request)3031 static void rbd_img_parent_read(struct rbd_obj_request *obj_request)
3032 {
3033 struct rbd_img_request *img_request;
3034 int result;
3035
3036 rbd_assert(obj_request_img_data_test(obj_request));
3037 rbd_assert(obj_request->img_request != NULL);
3038 rbd_assert(obj_request->result == (s32) -ENOENT);
3039 rbd_assert(obj_request_type_valid(obj_request->type));
3040
3041 /* rbd_read_finish(obj_request, obj_request->length); */
3042 img_request = rbd_parent_request_create(obj_request,
3043 obj_request->img_offset,
3044 obj_request->length);
3045 result = -ENOMEM;
3046 if (!img_request)
3047 goto out_err;
3048
3049 if (obj_request->type == OBJ_REQUEST_BIO)
3050 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
3051 obj_request->bio_list);
3052 else
3053 result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES,
3054 obj_request->pages);
3055 if (result)
3056 goto out_err;
3057
3058 img_request->callback = rbd_img_parent_read_callback;
3059 result = rbd_img_request_submit(img_request);
3060 if (result)
3061 goto out_err;
3062
3063 return;
3064 out_err:
3065 if (img_request)
3066 rbd_img_request_put(img_request);
3067 obj_request->result = result;
3068 obj_request->xferred = 0;
3069 obj_request_done_set(obj_request);
3070 }
3071
rbd_obj_notify_ack_sync(struct rbd_device * rbd_dev,u64 notify_id)3072 static int rbd_obj_notify_ack_sync(struct rbd_device *rbd_dev, u64 notify_id)
3073 {
3074 struct rbd_obj_request *obj_request;
3075 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3076 int ret;
3077
3078 obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
3079 OBJ_REQUEST_NODATA);
3080 if (!obj_request)
3081 return -ENOMEM;
3082
3083 ret = -ENOMEM;
3084 obj_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1,
3085 obj_request);
3086 if (!obj_request->osd_req)
3087 goto out;
3088
3089 osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_NOTIFY_ACK,
3090 notify_id, 0, 0);
3091 rbd_osd_req_format_read(obj_request);
3092
3093 ret = rbd_obj_request_submit(osdc, obj_request);
3094 if (ret)
3095 goto out;
3096 ret = rbd_obj_request_wait(obj_request);
3097 out:
3098 rbd_obj_request_put(obj_request);
3099
3100 return ret;
3101 }
3102
rbd_watch_cb(u64 ver,u64 notify_id,u8 opcode,void * data)3103 static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
3104 {
3105 struct rbd_device *rbd_dev = (struct rbd_device *)data;
3106 int ret;
3107
3108 if (!rbd_dev)
3109 return;
3110
3111 dout("%s: \"%s\" notify_id %llu opcode %u\n", __func__,
3112 rbd_dev->header_name, (unsigned long long)notify_id,
3113 (unsigned int)opcode);
3114
3115 /*
3116 * Until adequate refresh error handling is in place, there is
3117 * not much we can do here, except warn.
3118 *
3119 * See http://tracker.ceph.com/issues/5040
3120 */
3121 ret = rbd_dev_refresh(rbd_dev);
3122 if (ret)
3123 rbd_warn(rbd_dev, "refresh failed: %d", ret);
3124
3125 ret = rbd_obj_notify_ack_sync(rbd_dev, notify_id);
3126 if (ret)
3127 rbd_warn(rbd_dev, "notify_ack ret %d", ret);
3128 }
3129
3130 /*
3131 * Send a (un)watch request and wait for the ack. Return a request
3132 * with a ref held on success or error.
3133 */
rbd_obj_watch_request_helper(struct rbd_device * rbd_dev,bool watch)3134 static struct rbd_obj_request *rbd_obj_watch_request_helper(
3135 struct rbd_device *rbd_dev,
3136 bool watch)
3137 {
3138 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3139 struct rbd_obj_request *obj_request;
3140 int ret;
3141
3142 obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
3143 OBJ_REQUEST_NODATA);
3144 if (!obj_request)
3145 return ERR_PTR(-ENOMEM);
3146
3147 obj_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_WRITE, 1,
3148 obj_request);
3149 if (!obj_request->osd_req) {
3150 ret = -ENOMEM;
3151 goto out;
3152 }
3153
3154 osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_WATCH,
3155 rbd_dev->watch_event->cookie, 0, watch);
3156 rbd_osd_req_format_write(obj_request);
3157
3158 if (watch)
3159 ceph_osdc_set_request_linger(osdc, obj_request->osd_req);
3160
3161 ret = rbd_obj_request_submit(osdc, obj_request);
3162 if (ret)
3163 goto out;
3164
3165 ret = rbd_obj_request_wait(obj_request);
3166 if (ret)
3167 goto out;
3168
3169 ret = obj_request->result;
3170 if (ret) {
3171 if (watch)
3172 rbd_obj_request_end(obj_request);
3173 goto out;
3174 }
3175
3176 return obj_request;
3177
3178 out:
3179 rbd_obj_request_put(obj_request);
3180 return ERR_PTR(ret);
3181 }
3182
3183 /*
3184 * Initiate a watch request, synchronously.
3185 */
rbd_dev_header_watch_sync(struct rbd_device * rbd_dev)3186 static int rbd_dev_header_watch_sync(struct rbd_device *rbd_dev)
3187 {
3188 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3189 struct rbd_obj_request *obj_request;
3190 int ret;
3191
3192 rbd_assert(!rbd_dev->watch_event);
3193 rbd_assert(!rbd_dev->watch_request);
3194
3195 ret = ceph_osdc_create_event(osdc, rbd_watch_cb, rbd_dev,
3196 &rbd_dev->watch_event);
3197 if (ret < 0)
3198 return ret;
3199
3200 obj_request = rbd_obj_watch_request_helper(rbd_dev, true);
3201 if (IS_ERR(obj_request)) {
3202 ceph_osdc_cancel_event(rbd_dev->watch_event);
3203 rbd_dev->watch_event = NULL;
3204 return PTR_ERR(obj_request);
3205 }
3206
3207 /*
3208 * A watch request is set to linger, so the underlying osd
3209 * request won't go away until we unregister it. We retain
3210 * a pointer to the object request during that time (in
3211 * rbd_dev->watch_request), so we'll keep a reference to it.
3212 * We'll drop that reference after we've unregistered it in
3213 * rbd_dev_header_unwatch_sync().
3214 */
3215 rbd_dev->watch_request = obj_request;
3216
3217 return 0;
3218 }
3219
3220 /*
3221 * Tear down a watch request, synchronously.
3222 */
rbd_dev_header_unwatch_sync(struct rbd_device * rbd_dev)3223 static void rbd_dev_header_unwatch_sync(struct rbd_device *rbd_dev)
3224 {
3225 struct rbd_obj_request *obj_request;
3226
3227 rbd_assert(rbd_dev->watch_event);
3228 rbd_assert(rbd_dev->watch_request);
3229
3230 rbd_obj_request_end(rbd_dev->watch_request);
3231 rbd_obj_request_put(rbd_dev->watch_request);
3232 rbd_dev->watch_request = NULL;
3233
3234 obj_request = rbd_obj_watch_request_helper(rbd_dev, false);
3235 if (!IS_ERR(obj_request))
3236 rbd_obj_request_put(obj_request);
3237 else
3238 rbd_warn(rbd_dev, "unable to tear down watch request (%ld)",
3239 PTR_ERR(obj_request));
3240
3241 ceph_osdc_cancel_event(rbd_dev->watch_event);
3242 rbd_dev->watch_event = NULL;
3243 }
3244
3245 /*
3246 * Synchronous osd object method call. Returns the number of bytes
3247 * returned in the outbound buffer, or a negative error code.
3248 */
rbd_obj_method_sync(struct rbd_device * rbd_dev,const char * object_name,const char * class_name,const char * method_name,const void * outbound,size_t outbound_size,void * inbound,size_t inbound_size)3249 static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
3250 const char *object_name,
3251 const char *class_name,
3252 const char *method_name,
3253 const void *outbound,
3254 size_t outbound_size,
3255 void *inbound,
3256 size_t inbound_size)
3257 {
3258 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3259 struct rbd_obj_request *obj_request;
3260 struct page **pages;
3261 u32 page_count;
3262 int ret;
3263
3264 /*
3265 * Method calls are ultimately read operations. The result
3266 * should placed into the inbound buffer provided. They
3267 * also supply outbound data--parameters for the object
3268 * method. Currently if this is present it will be a
3269 * snapshot id.
3270 */
3271 page_count = (u32)calc_pages_for(0, inbound_size);
3272 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
3273 if (IS_ERR(pages))
3274 return PTR_ERR(pages);
3275
3276 ret = -ENOMEM;
3277 obj_request = rbd_obj_request_create(object_name, 0, inbound_size,
3278 OBJ_REQUEST_PAGES);
3279 if (!obj_request)
3280 goto out;
3281
3282 obj_request->pages = pages;
3283 obj_request->page_count = page_count;
3284
3285 obj_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1,
3286 obj_request);
3287 if (!obj_request->osd_req)
3288 goto out;
3289
3290 osd_req_op_cls_init(obj_request->osd_req, 0, CEPH_OSD_OP_CALL,
3291 class_name, method_name);
3292 if (outbound_size) {
3293 struct ceph_pagelist *pagelist;
3294
3295 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
3296 if (!pagelist)
3297 goto out;
3298
3299 ceph_pagelist_init(pagelist);
3300 ceph_pagelist_append(pagelist, outbound, outbound_size);
3301 osd_req_op_cls_request_data_pagelist(obj_request->osd_req, 0,
3302 pagelist);
3303 }
3304 osd_req_op_cls_response_data_pages(obj_request->osd_req, 0,
3305 obj_request->pages, inbound_size,
3306 0, false, false);
3307 rbd_osd_req_format_read(obj_request);
3308
3309 ret = rbd_obj_request_submit(osdc, obj_request);
3310 if (ret)
3311 goto out;
3312 ret = rbd_obj_request_wait(obj_request);
3313 if (ret)
3314 goto out;
3315
3316 ret = obj_request->result;
3317 if (ret < 0)
3318 goto out;
3319
3320 rbd_assert(obj_request->xferred < (u64)INT_MAX);
3321 ret = (int)obj_request->xferred;
3322 ceph_copy_from_page_vector(pages, inbound, 0, obj_request->xferred);
3323 out:
3324 if (obj_request)
3325 rbd_obj_request_put(obj_request);
3326 else
3327 ceph_release_page_vector(pages, page_count);
3328
3329 return ret;
3330 }
3331
rbd_queue_workfn(struct work_struct * work)3332 static void rbd_queue_workfn(struct work_struct *work)
3333 {
3334 struct request *rq = blk_mq_rq_from_pdu(work);
3335 struct rbd_device *rbd_dev = rq->q->queuedata;
3336 struct rbd_img_request *img_request;
3337 struct ceph_snap_context *snapc = NULL;
3338 u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT;
3339 u64 length = blk_rq_bytes(rq);
3340 enum obj_operation_type op_type;
3341 u64 mapping_size;
3342 int result;
3343
3344 if (rq->cmd_type != REQ_TYPE_FS) {
3345 dout("%s: non-fs request type %d\n", __func__,
3346 (int) rq->cmd_type);
3347 result = -EIO;
3348 goto err;
3349 }
3350
3351 if (rq->cmd_flags & REQ_DISCARD)
3352 op_type = OBJ_OP_DISCARD;
3353 else if (rq->cmd_flags & REQ_WRITE)
3354 op_type = OBJ_OP_WRITE;
3355 else
3356 op_type = OBJ_OP_READ;
3357
3358 /* Ignore/skip any zero-length requests */
3359
3360 if (!length) {
3361 dout("%s: zero-length request\n", __func__);
3362 result = 0;
3363 goto err_rq;
3364 }
3365
3366 /* Only reads are allowed to a read-only device */
3367
3368 if (op_type != OBJ_OP_READ) {
3369 if (rbd_dev->mapping.read_only) {
3370 result = -EROFS;
3371 goto err_rq;
3372 }
3373 rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP);
3374 }
3375
3376 /*
3377 * Quit early if the mapped snapshot no longer exists. It's
3378 * still possible the snapshot will have disappeared by the
3379 * time our request arrives at the osd, but there's no sense in
3380 * sending it if we already know.
3381 */
3382 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) {
3383 dout("request for non-existent snapshot");
3384 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
3385 result = -ENXIO;
3386 goto err_rq;
3387 }
3388
3389 if (offset && length > U64_MAX - offset + 1) {
3390 rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset,
3391 length);
3392 result = -EINVAL;
3393 goto err_rq; /* Shouldn't happen */
3394 }
3395
3396 blk_mq_start_request(rq);
3397
3398 down_read(&rbd_dev->header_rwsem);
3399 mapping_size = rbd_dev->mapping.size;
3400 if (op_type != OBJ_OP_READ) {
3401 snapc = rbd_dev->header.snapc;
3402 ceph_get_snap_context(snapc);
3403 }
3404 up_read(&rbd_dev->header_rwsem);
3405
3406 if (offset + length > mapping_size) {
3407 rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset,
3408 length, mapping_size);
3409 result = -EIO;
3410 goto err_rq;
3411 }
3412
3413 img_request = rbd_img_request_create(rbd_dev, offset, length, op_type,
3414 snapc);
3415 if (!img_request) {
3416 result = -ENOMEM;
3417 goto err_rq;
3418 }
3419 img_request->rq = rq;
3420 snapc = NULL; /* img_request consumes a ref */
3421
3422 if (op_type == OBJ_OP_DISCARD)
3423 result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA,
3424 NULL);
3425 else
3426 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
3427 rq->bio);
3428 if (result)
3429 goto err_img_request;
3430
3431 result = rbd_img_request_submit(img_request);
3432 if (result)
3433 goto err_img_request;
3434
3435 return;
3436
3437 err_img_request:
3438 rbd_img_request_put(img_request);
3439 err_rq:
3440 if (result)
3441 rbd_warn(rbd_dev, "%s %llx at %llx result %d",
3442 obj_op_name(op_type), length, offset, result);
3443 ceph_put_snap_context(snapc);
3444 err:
3445 blk_mq_end_request(rq, result);
3446 }
3447
rbd_queue_rq(struct blk_mq_hw_ctx * hctx,const struct blk_mq_queue_data * bd)3448 static int rbd_queue_rq(struct blk_mq_hw_ctx *hctx,
3449 const struct blk_mq_queue_data *bd)
3450 {
3451 struct request *rq = bd->rq;
3452 struct work_struct *work = blk_mq_rq_to_pdu(rq);
3453
3454 queue_work(rbd_wq, work);
3455 return BLK_MQ_RQ_QUEUE_OK;
3456 }
3457
3458 /*
3459 * a queue callback. Makes sure that we don't create a bio that spans across
3460 * multiple osd objects. One exception would be with a single page bios,
3461 * which we handle later at bio_chain_clone_range()
3462 */
rbd_merge_bvec(struct request_queue * q,struct bvec_merge_data * bmd,struct bio_vec * bvec)3463 static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
3464 struct bio_vec *bvec)
3465 {
3466 struct rbd_device *rbd_dev = q->queuedata;
3467 sector_t sector_offset;
3468 sector_t sectors_per_obj;
3469 sector_t obj_sector_offset;
3470 int ret;
3471
3472 /*
3473 * Find how far into its rbd object the partition-relative
3474 * bio start sector is to offset relative to the enclosing
3475 * device.
3476 */
3477 sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector;
3478 sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
3479 obj_sector_offset = sector_offset & (sectors_per_obj - 1);
3480
3481 /*
3482 * Compute the number of bytes from that offset to the end
3483 * of the object. Account for what's already used by the bio.
3484 */
3485 ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT;
3486 if (ret > bmd->bi_size)
3487 ret -= bmd->bi_size;
3488 else
3489 ret = 0;
3490
3491 /*
3492 * Don't send back more than was asked for. And if the bio
3493 * was empty, let the whole thing through because: "Note
3494 * that a block device *must* allow a single page to be
3495 * added to an empty bio."
3496 */
3497 rbd_assert(bvec->bv_len <= PAGE_SIZE);
3498 if (ret > (int) bvec->bv_len || !bmd->bi_size)
3499 ret = (int) bvec->bv_len;
3500
3501 return ret;
3502 }
3503
rbd_free_disk(struct rbd_device * rbd_dev)3504 static void rbd_free_disk(struct rbd_device *rbd_dev)
3505 {
3506 struct gendisk *disk = rbd_dev->disk;
3507
3508 if (!disk)
3509 return;
3510
3511 rbd_dev->disk = NULL;
3512 if (disk->flags & GENHD_FL_UP) {
3513 del_gendisk(disk);
3514 if (disk->queue)
3515 blk_cleanup_queue(disk->queue);
3516 blk_mq_free_tag_set(&rbd_dev->tag_set);
3517 }
3518 put_disk(disk);
3519 }
3520
rbd_obj_read_sync(struct rbd_device * rbd_dev,const char * object_name,u64 offset,u64 length,void * buf)3521 static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
3522 const char *object_name,
3523 u64 offset, u64 length, void *buf)
3524
3525 {
3526 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3527 struct rbd_obj_request *obj_request;
3528 struct page **pages = NULL;
3529 u32 page_count;
3530 size_t size;
3531 int ret;
3532
3533 page_count = (u32) calc_pages_for(offset, length);
3534 pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
3535 if (IS_ERR(pages))
3536 return PTR_ERR(pages);
3537
3538 ret = -ENOMEM;
3539 obj_request = rbd_obj_request_create(object_name, offset, length,
3540 OBJ_REQUEST_PAGES);
3541 if (!obj_request)
3542 goto out;
3543
3544 obj_request->pages = pages;
3545 obj_request->page_count = page_count;
3546
3547 obj_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1,
3548 obj_request);
3549 if (!obj_request->osd_req)
3550 goto out;
3551
3552 osd_req_op_extent_init(obj_request->osd_req, 0, CEPH_OSD_OP_READ,
3553 offset, length, 0, 0);
3554 osd_req_op_extent_osd_data_pages(obj_request->osd_req, 0,
3555 obj_request->pages,
3556 obj_request->length,
3557 obj_request->offset & ~PAGE_MASK,
3558 false, false);
3559 rbd_osd_req_format_read(obj_request);
3560
3561 ret = rbd_obj_request_submit(osdc, obj_request);
3562 if (ret)
3563 goto out;
3564 ret = rbd_obj_request_wait(obj_request);
3565 if (ret)
3566 goto out;
3567
3568 ret = obj_request->result;
3569 if (ret < 0)
3570 goto out;
3571
3572 rbd_assert(obj_request->xferred <= (u64) SIZE_MAX);
3573 size = (size_t) obj_request->xferred;
3574 ceph_copy_from_page_vector(pages, buf, 0, size);
3575 rbd_assert(size <= (size_t)INT_MAX);
3576 ret = (int)size;
3577 out:
3578 if (obj_request)
3579 rbd_obj_request_put(obj_request);
3580 else
3581 ceph_release_page_vector(pages, page_count);
3582
3583 return ret;
3584 }
3585
3586 /*
3587 * Read the complete header for the given rbd device. On successful
3588 * return, the rbd_dev->header field will contain up-to-date
3589 * information about the image.
3590 */
rbd_dev_v1_header_info(struct rbd_device * rbd_dev)3591 static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev)
3592 {
3593 struct rbd_image_header_ondisk *ondisk = NULL;
3594 u32 snap_count = 0;
3595 u64 names_size = 0;
3596 u32 want_count;
3597 int ret;
3598
3599 /*
3600 * The complete header will include an array of its 64-bit
3601 * snapshot ids, followed by the names of those snapshots as
3602 * a contiguous block of NUL-terminated strings. Note that
3603 * the number of snapshots could change by the time we read
3604 * it in, in which case we re-read it.
3605 */
3606 do {
3607 size_t size;
3608
3609 kfree(ondisk);
3610
3611 size = sizeof (*ondisk);
3612 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
3613 size += names_size;
3614 ondisk = kmalloc(size, GFP_KERNEL);
3615 if (!ondisk)
3616 return -ENOMEM;
3617
3618 ret = rbd_obj_read_sync(rbd_dev, rbd_dev->header_name,
3619 0, size, ondisk);
3620 if (ret < 0)
3621 goto out;
3622 if ((size_t)ret < size) {
3623 ret = -ENXIO;
3624 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
3625 size, ret);
3626 goto out;
3627 }
3628 if (!rbd_dev_ondisk_valid(ondisk)) {
3629 ret = -ENXIO;
3630 rbd_warn(rbd_dev, "invalid header");
3631 goto out;
3632 }
3633
3634 names_size = le64_to_cpu(ondisk->snap_names_len);
3635 want_count = snap_count;
3636 snap_count = le32_to_cpu(ondisk->snap_count);
3637 } while (snap_count != want_count);
3638
3639 ret = rbd_header_from_disk(rbd_dev, ondisk);
3640 out:
3641 kfree(ondisk);
3642
3643 return ret;
3644 }
3645
3646 /*
3647 * Clear the rbd device's EXISTS flag if the snapshot it's mapped to
3648 * has disappeared from the (just updated) snapshot context.
3649 */
rbd_exists_validate(struct rbd_device * rbd_dev)3650 static void rbd_exists_validate(struct rbd_device *rbd_dev)
3651 {
3652 u64 snap_id;
3653
3654 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags))
3655 return;
3656
3657 snap_id = rbd_dev->spec->snap_id;
3658 if (snap_id == CEPH_NOSNAP)
3659 return;
3660
3661 if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX)
3662 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
3663 }
3664
rbd_dev_update_size(struct rbd_device * rbd_dev)3665 static void rbd_dev_update_size(struct rbd_device *rbd_dev)
3666 {
3667 sector_t size;
3668 bool removing;
3669
3670 /*
3671 * Don't hold the lock while doing disk operations,
3672 * or lock ordering will conflict with the bdev mutex via:
3673 * rbd_add() -> blkdev_get() -> rbd_open()
3674 */
3675 spin_lock_irq(&rbd_dev->lock);
3676 removing = test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags);
3677 spin_unlock_irq(&rbd_dev->lock);
3678 /*
3679 * If the device is being removed, rbd_dev->disk has
3680 * been destroyed, so don't try to update its size
3681 */
3682 if (!removing) {
3683 size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE;
3684 dout("setting size to %llu sectors", (unsigned long long)size);
3685 set_capacity(rbd_dev->disk, size);
3686 revalidate_disk(rbd_dev->disk);
3687 }
3688 }
3689
rbd_dev_refresh(struct rbd_device * rbd_dev)3690 static int rbd_dev_refresh(struct rbd_device *rbd_dev)
3691 {
3692 u64 mapping_size;
3693 int ret;
3694
3695 down_write(&rbd_dev->header_rwsem);
3696 mapping_size = rbd_dev->mapping.size;
3697
3698 ret = rbd_dev_header_info(rbd_dev);
3699 if (ret)
3700 goto out;
3701
3702 /*
3703 * If there is a parent, see if it has disappeared due to the
3704 * mapped image getting flattened.
3705 */
3706 if (rbd_dev->parent) {
3707 ret = rbd_dev_v2_parent_info(rbd_dev);
3708 if (ret)
3709 goto out;
3710 }
3711
3712 if (rbd_dev->spec->snap_id == CEPH_NOSNAP) {
3713 rbd_dev->mapping.size = rbd_dev->header.image_size;
3714 } else {
3715 /* validate mapped snapshot's EXISTS flag */
3716 rbd_exists_validate(rbd_dev);
3717 }
3718
3719 out:
3720 up_write(&rbd_dev->header_rwsem);
3721 if (!ret && mapping_size != rbd_dev->mapping.size)
3722 rbd_dev_update_size(rbd_dev);
3723
3724 return ret;
3725 }
3726
rbd_init_request(void * data,struct request * rq,unsigned int hctx_idx,unsigned int request_idx,unsigned int numa_node)3727 static int rbd_init_request(void *data, struct request *rq,
3728 unsigned int hctx_idx, unsigned int request_idx,
3729 unsigned int numa_node)
3730 {
3731 struct work_struct *work = blk_mq_rq_to_pdu(rq);
3732
3733 INIT_WORK(work, rbd_queue_workfn);
3734 return 0;
3735 }
3736
3737 static struct blk_mq_ops rbd_mq_ops = {
3738 .queue_rq = rbd_queue_rq,
3739 .map_queue = blk_mq_map_queue,
3740 .init_request = rbd_init_request,
3741 };
3742
rbd_init_disk(struct rbd_device * rbd_dev)3743 static int rbd_init_disk(struct rbd_device *rbd_dev)
3744 {
3745 struct gendisk *disk;
3746 struct request_queue *q;
3747 u64 segment_size;
3748 int err;
3749
3750 /* create gendisk info */
3751 disk = alloc_disk(single_major ?
3752 (1 << RBD_SINGLE_MAJOR_PART_SHIFT) :
3753 RBD_MINORS_PER_MAJOR);
3754 if (!disk)
3755 return -ENOMEM;
3756
3757 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
3758 rbd_dev->dev_id);
3759 disk->major = rbd_dev->major;
3760 disk->first_minor = rbd_dev->minor;
3761 if (single_major)
3762 disk->flags |= GENHD_FL_EXT_DEVT;
3763 disk->fops = &rbd_bd_ops;
3764 disk->private_data = rbd_dev;
3765
3766 memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set));
3767 rbd_dev->tag_set.ops = &rbd_mq_ops;
3768 rbd_dev->tag_set.queue_depth = BLKDEV_MAX_RQ;
3769 rbd_dev->tag_set.numa_node = NUMA_NO_NODE;
3770 rbd_dev->tag_set.flags =
3771 BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
3772 rbd_dev->tag_set.nr_hw_queues = 1;
3773 rbd_dev->tag_set.cmd_size = sizeof(struct work_struct);
3774
3775 err = blk_mq_alloc_tag_set(&rbd_dev->tag_set);
3776 if (err)
3777 goto out_disk;
3778
3779 q = blk_mq_init_queue(&rbd_dev->tag_set);
3780 if (IS_ERR(q)) {
3781 err = PTR_ERR(q);
3782 goto out_tag_set;
3783 }
3784
3785 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
3786 /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */
3787
3788 /* set io sizes to object size */
3789 segment_size = rbd_obj_bytes(&rbd_dev->header);
3790 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
3791 blk_queue_max_segment_size(q, segment_size);
3792 blk_queue_io_min(q, segment_size);
3793 blk_queue_io_opt(q, segment_size);
3794
3795 /* enable the discard support */
3796 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
3797 q->limits.discard_granularity = segment_size;
3798 q->limits.discard_alignment = segment_size;
3799 q->limits.max_discard_sectors = segment_size / SECTOR_SIZE;
3800 q->limits.discard_zeroes_data = 1;
3801
3802 blk_queue_merge_bvec(q, rbd_merge_bvec);
3803 if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC))
3804 q->backing_dev_info.capabilities |= BDI_CAP_STABLE_WRITES;
3805
3806 disk->queue = q;
3807
3808 q->queuedata = rbd_dev;
3809
3810 rbd_dev->disk = disk;
3811
3812 return 0;
3813 out_tag_set:
3814 blk_mq_free_tag_set(&rbd_dev->tag_set);
3815 out_disk:
3816 put_disk(disk);
3817 return err;
3818 }
3819
3820 /*
3821 sysfs
3822 */
3823
dev_to_rbd_dev(struct device * dev)3824 static struct rbd_device *dev_to_rbd_dev(struct device *dev)
3825 {
3826 return container_of(dev, struct rbd_device, dev);
3827 }
3828
rbd_size_show(struct device * dev,struct device_attribute * attr,char * buf)3829 static ssize_t rbd_size_show(struct device *dev,
3830 struct device_attribute *attr, char *buf)
3831 {
3832 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3833
3834 return sprintf(buf, "%llu\n",
3835 (unsigned long long)rbd_dev->mapping.size);
3836 }
3837
3838 /*
3839 * Note this shows the features for whatever's mapped, which is not
3840 * necessarily the base image.
3841 */
rbd_features_show(struct device * dev,struct device_attribute * attr,char * buf)3842 static ssize_t rbd_features_show(struct device *dev,
3843 struct device_attribute *attr, char *buf)
3844 {
3845 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3846
3847 return sprintf(buf, "0x%016llx\n",
3848 (unsigned long long)rbd_dev->mapping.features);
3849 }
3850
rbd_major_show(struct device * dev,struct device_attribute * attr,char * buf)3851 static ssize_t rbd_major_show(struct device *dev,
3852 struct device_attribute *attr, char *buf)
3853 {
3854 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3855
3856 if (rbd_dev->major)
3857 return sprintf(buf, "%d\n", rbd_dev->major);
3858
3859 return sprintf(buf, "(none)\n");
3860 }
3861
rbd_minor_show(struct device * dev,struct device_attribute * attr,char * buf)3862 static ssize_t rbd_minor_show(struct device *dev,
3863 struct device_attribute *attr, char *buf)
3864 {
3865 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3866
3867 return sprintf(buf, "%d\n", rbd_dev->minor);
3868 }
3869
rbd_client_id_show(struct device * dev,struct device_attribute * attr,char * buf)3870 static ssize_t rbd_client_id_show(struct device *dev,
3871 struct device_attribute *attr, char *buf)
3872 {
3873 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3874
3875 return sprintf(buf, "client%lld\n",
3876 ceph_client_id(rbd_dev->rbd_client->client));
3877 }
3878
rbd_pool_show(struct device * dev,struct device_attribute * attr,char * buf)3879 static ssize_t rbd_pool_show(struct device *dev,
3880 struct device_attribute *attr, char *buf)
3881 {
3882 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3883
3884 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
3885 }
3886
rbd_pool_id_show(struct device * dev,struct device_attribute * attr,char * buf)3887 static ssize_t rbd_pool_id_show(struct device *dev,
3888 struct device_attribute *attr, char *buf)
3889 {
3890 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3891
3892 return sprintf(buf, "%llu\n",
3893 (unsigned long long) rbd_dev->spec->pool_id);
3894 }
3895
rbd_name_show(struct device * dev,struct device_attribute * attr,char * buf)3896 static ssize_t rbd_name_show(struct device *dev,
3897 struct device_attribute *attr, char *buf)
3898 {
3899 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3900
3901 if (rbd_dev->spec->image_name)
3902 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
3903
3904 return sprintf(buf, "(unknown)\n");
3905 }
3906
rbd_image_id_show(struct device * dev,struct device_attribute * attr,char * buf)3907 static ssize_t rbd_image_id_show(struct device *dev,
3908 struct device_attribute *attr, char *buf)
3909 {
3910 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3911
3912 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
3913 }
3914
3915 /*
3916 * Shows the name of the currently-mapped snapshot (or
3917 * RBD_SNAP_HEAD_NAME for the base image).
3918 */
rbd_snap_show(struct device * dev,struct device_attribute * attr,char * buf)3919 static ssize_t rbd_snap_show(struct device *dev,
3920 struct device_attribute *attr,
3921 char *buf)
3922 {
3923 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3924
3925 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
3926 }
3927
3928 /*
3929 * For a v2 image, shows the chain of parent images, separated by empty
3930 * lines. For v1 images or if there is no parent, shows "(no parent
3931 * image)".
3932 */
rbd_parent_show(struct device * dev,struct device_attribute * attr,char * buf)3933 static ssize_t rbd_parent_show(struct device *dev,
3934 struct device_attribute *attr,
3935 char *buf)
3936 {
3937 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3938 ssize_t count = 0;
3939
3940 if (!rbd_dev->parent)
3941 return sprintf(buf, "(no parent image)\n");
3942
3943 for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) {
3944 struct rbd_spec *spec = rbd_dev->parent_spec;
3945
3946 count += sprintf(&buf[count], "%s"
3947 "pool_id %llu\npool_name %s\n"
3948 "image_id %s\nimage_name %s\n"
3949 "snap_id %llu\nsnap_name %s\n"
3950 "overlap %llu\n",
3951 !count ? "" : "\n", /* first? */
3952 spec->pool_id, spec->pool_name,
3953 spec->image_id, spec->image_name ?: "(unknown)",
3954 spec->snap_id, spec->snap_name,
3955 rbd_dev->parent_overlap);
3956 }
3957
3958 return count;
3959 }
3960
rbd_image_refresh(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)3961 static ssize_t rbd_image_refresh(struct device *dev,
3962 struct device_attribute *attr,
3963 const char *buf,
3964 size_t size)
3965 {
3966 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
3967 int ret;
3968
3969 ret = rbd_dev_refresh(rbd_dev);
3970 if (ret)
3971 return ret;
3972
3973 return size;
3974 }
3975
3976 static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
3977 static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
3978 static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
3979 static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL);
3980 static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
3981 static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
3982 static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
3983 static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
3984 static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
3985 static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
3986 static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
3987 static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
3988
3989 static struct attribute *rbd_attrs[] = {
3990 &dev_attr_size.attr,
3991 &dev_attr_features.attr,
3992 &dev_attr_major.attr,
3993 &dev_attr_minor.attr,
3994 &dev_attr_client_id.attr,
3995 &dev_attr_pool.attr,
3996 &dev_attr_pool_id.attr,
3997 &dev_attr_name.attr,
3998 &dev_attr_image_id.attr,
3999 &dev_attr_current_snap.attr,
4000 &dev_attr_parent.attr,
4001 &dev_attr_refresh.attr,
4002 NULL
4003 };
4004
4005 static struct attribute_group rbd_attr_group = {
4006 .attrs = rbd_attrs,
4007 };
4008
4009 static const struct attribute_group *rbd_attr_groups[] = {
4010 &rbd_attr_group,
4011 NULL
4012 };
4013
rbd_sysfs_dev_release(struct device * dev)4014 static void rbd_sysfs_dev_release(struct device *dev)
4015 {
4016 }
4017
4018 static struct device_type rbd_device_type = {
4019 .name = "rbd",
4020 .groups = rbd_attr_groups,
4021 .release = rbd_sysfs_dev_release,
4022 };
4023
rbd_spec_get(struct rbd_spec * spec)4024 static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
4025 {
4026 kref_get(&spec->kref);
4027
4028 return spec;
4029 }
4030
4031 static void rbd_spec_free(struct kref *kref);
rbd_spec_put(struct rbd_spec * spec)4032 static void rbd_spec_put(struct rbd_spec *spec)
4033 {
4034 if (spec)
4035 kref_put(&spec->kref, rbd_spec_free);
4036 }
4037
rbd_spec_alloc(void)4038 static struct rbd_spec *rbd_spec_alloc(void)
4039 {
4040 struct rbd_spec *spec;
4041
4042 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
4043 if (!spec)
4044 return NULL;
4045
4046 spec->pool_id = CEPH_NOPOOL;
4047 spec->snap_id = CEPH_NOSNAP;
4048 kref_init(&spec->kref);
4049
4050 return spec;
4051 }
4052
rbd_spec_free(struct kref * kref)4053 static void rbd_spec_free(struct kref *kref)
4054 {
4055 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
4056
4057 kfree(spec->pool_name);
4058 kfree(spec->image_id);
4059 kfree(spec->image_name);
4060 kfree(spec->snap_name);
4061 kfree(spec);
4062 }
4063
rbd_dev_create(struct rbd_client * rbdc,struct rbd_spec * spec)4064 static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
4065 struct rbd_spec *spec)
4066 {
4067 struct rbd_device *rbd_dev;
4068
4069 rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL);
4070 if (!rbd_dev)
4071 return NULL;
4072
4073 spin_lock_init(&rbd_dev->lock);
4074 rbd_dev->flags = 0;
4075 atomic_set(&rbd_dev->parent_ref, 0);
4076 INIT_LIST_HEAD(&rbd_dev->node);
4077 init_rwsem(&rbd_dev->header_rwsem);
4078
4079 rbd_dev->spec = spec;
4080 rbd_dev->rbd_client = rbdc;
4081
4082 /* Initialize the layout used for all rbd requests */
4083
4084 rbd_dev->layout.fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
4085 rbd_dev->layout.fl_stripe_count = cpu_to_le32(1);
4086 rbd_dev->layout.fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
4087 rbd_dev->layout.fl_pg_pool = cpu_to_le32((u32) spec->pool_id);
4088
4089 return rbd_dev;
4090 }
4091
rbd_dev_destroy(struct rbd_device * rbd_dev)4092 static void rbd_dev_destroy(struct rbd_device *rbd_dev)
4093 {
4094 rbd_put_client(rbd_dev->rbd_client);
4095 rbd_spec_put(rbd_dev->spec);
4096 kfree(rbd_dev);
4097 }
4098
4099 /*
4100 * Get the size and object order for an image snapshot, or if
4101 * snap_id is CEPH_NOSNAP, gets this information for the base
4102 * image.
4103 */
_rbd_dev_v2_snap_size(struct rbd_device * rbd_dev,u64 snap_id,u8 * order,u64 * snap_size)4104 static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
4105 u8 *order, u64 *snap_size)
4106 {
4107 __le64 snapid = cpu_to_le64(snap_id);
4108 int ret;
4109 struct {
4110 u8 order;
4111 __le64 size;
4112 } __attribute__ ((packed)) size_buf = { 0 };
4113
4114 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
4115 "rbd", "get_size",
4116 &snapid, sizeof (snapid),
4117 &size_buf, sizeof (size_buf));
4118 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
4119 if (ret < 0)
4120 return ret;
4121 if (ret < sizeof (size_buf))
4122 return -ERANGE;
4123
4124 if (order) {
4125 *order = size_buf.order;
4126 dout(" order %u", (unsigned int)*order);
4127 }
4128 *snap_size = le64_to_cpu(size_buf.size);
4129
4130 dout(" snap_id 0x%016llx snap_size = %llu\n",
4131 (unsigned long long)snap_id,
4132 (unsigned long long)*snap_size);
4133
4134 return 0;
4135 }
4136
rbd_dev_v2_image_size(struct rbd_device * rbd_dev)4137 static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
4138 {
4139 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
4140 &rbd_dev->header.obj_order,
4141 &rbd_dev->header.image_size);
4142 }
4143
rbd_dev_v2_object_prefix(struct rbd_device * rbd_dev)4144 static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
4145 {
4146 void *reply_buf;
4147 int ret;
4148 void *p;
4149
4150 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
4151 if (!reply_buf)
4152 return -ENOMEM;
4153
4154 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
4155 "rbd", "get_object_prefix", NULL, 0,
4156 reply_buf, RBD_OBJ_PREFIX_LEN_MAX);
4157 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
4158 if (ret < 0)
4159 goto out;
4160
4161 p = reply_buf;
4162 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
4163 p + ret, NULL, GFP_NOIO);
4164 ret = 0;
4165
4166 if (IS_ERR(rbd_dev->header.object_prefix)) {
4167 ret = PTR_ERR(rbd_dev->header.object_prefix);
4168 rbd_dev->header.object_prefix = NULL;
4169 } else {
4170 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
4171 }
4172 out:
4173 kfree(reply_buf);
4174
4175 return ret;
4176 }
4177
_rbd_dev_v2_snap_features(struct rbd_device * rbd_dev,u64 snap_id,u64 * snap_features)4178 static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
4179 u64 *snap_features)
4180 {
4181 __le64 snapid = cpu_to_le64(snap_id);
4182 struct {
4183 __le64 features;
4184 __le64 incompat;
4185 } __attribute__ ((packed)) features_buf = { 0 };
4186 u64 incompat;
4187 int ret;
4188
4189 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
4190 "rbd", "get_features",
4191 &snapid, sizeof (snapid),
4192 &features_buf, sizeof (features_buf));
4193 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
4194 if (ret < 0)
4195 return ret;
4196 if (ret < sizeof (features_buf))
4197 return -ERANGE;
4198
4199 incompat = le64_to_cpu(features_buf.incompat);
4200 if (incompat & ~RBD_FEATURES_SUPPORTED)
4201 return -ENXIO;
4202
4203 *snap_features = le64_to_cpu(features_buf.features);
4204
4205 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
4206 (unsigned long long)snap_id,
4207 (unsigned long long)*snap_features,
4208 (unsigned long long)le64_to_cpu(features_buf.incompat));
4209
4210 return 0;
4211 }
4212
rbd_dev_v2_features(struct rbd_device * rbd_dev)4213 static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
4214 {
4215 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
4216 &rbd_dev->header.features);
4217 }
4218
rbd_dev_v2_parent_info(struct rbd_device * rbd_dev)4219 static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
4220 {
4221 struct rbd_spec *parent_spec;
4222 size_t size;
4223 void *reply_buf = NULL;
4224 __le64 snapid;
4225 void *p;
4226 void *end;
4227 u64 pool_id;
4228 char *image_id;
4229 u64 snap_id;
4230 u64 overlap;
4231 int ret;
4232
4233 parent_spec = rbd_spec_alloc();
4234 if (!parent_spec)
4235 return -ENOMEM;
4236
4237 size = sizeof (__le64) + /* pool_id */
4238 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
4239 sizeof (__le64) + /* snap_id */
4240 sizeof (__le64); /* overlap */
4241 reply_buf = kmalloc(size, GFP_KERNEL);
4242 if (!reply_buf) {
4243 ret = -ENOMEM;
4244 goto out_err;
4245 }
4246
4247 snapid = cpu_to_le64(rbd_dev->spec->snap_id);
4248 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
4249 "rbd", "get_parent",
4250 &snapid, sizeof (snapid),
4251 reply_buf, size);
4252 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
4253 if (ret < 0)
4254 goto out_err;
4255
4256 p = reply_buf;
4257 end = reply_buf + ret;
4258 ret = -ERANGE;
4259 ceph_decode_64_safe(&p, end, pool_id, out_err);
4260 if (pool_id == CEPH_NOPOOL) {
4261 /*
4262 * Either the parent never existed, or we have
4263 * record of it but the image got flattened so it no
4264 * longer has a parent. When the parent of a
4265 * layered image disappears we immediately set the
4266 * overlap to 0. The effect of this is that all new
4267 * requests will be treated as if the image had no
4268 * parent.
4269 */
4270 if (rbd_dev->parent_overlap) {
4271 rbd_dev->parent_overlap = 0;
4272 rbd_dev_parent_put(rbd_dev);
4273 pr_info("%s: clone image has been flattened\n",
4274 rbd_dev->disk->disk_name);
4275 }
4276
4277 goto out; /* No parent? No problem. */
4278 }
4279
4280 /* The ceph file layout needs to fit pool id in 32 bits */
4281
4282 ret = -EIO;
4283 if (pool_id > (u64)U32_MAX) {
4284 rbd_warn(NULL, "parent pool id too large (%llu > %u)",
4285 (unsigned long long)pool_id, U32_MAX);
4286 goto out_err;
4287 }
4288
4289 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
4290 if (IS_ERR(image_id)) {
4291 ret = PTR_ERR(image_id);
4292 goto out_err;
4293 }
4294 ceph_decode_64_safe(&p, end, snap_id, out_err);
4295 ceph_decode_64_safe(&p, end, overlap, out_err);
4296
4297 /*
4298 * The parent won't change (except when the clone is
4299 * flattened, already handled that). So we only need to
4300 * record the parent spec we have not already done so.
4301 */
4302 if (!rbd_dev->parent_spec) {
4303 parent_spec->pool_id = pool_id;
4304 parent_spec->image_id = image_id;
4305 parent_spec->snap_id = snap_id;
4306 rbd_dev->parent_spec = parent_spec;
4307 parent_spec = NULL; /* rbd_dev now owns this */
4308 } else {
4309 kfree(image_id);
4310 }
4311
4312 /*
4313 * We always update the parent overlap. If it's zero we issue
4314 * a warning, as we will proceed as if there was no parent.
4315 */
4316 if (!overlap) {
4317 if (parent_spec) {
4318 /* refresh, careful to warn just once */
4319 if (rbd_dev->parent_overlap)
4320 rbd_warn(rbd_dev,
4321 "clone now standalone (overlap became 0)");
4322 } else {
4323 /* initial probe */
4324 rbd_warn(rbd_dev, "clone is standalone (overlap 0)");
4325 }
4326 }
4327 rbd_dev->parent_overlap = overlap;
4328
4329 out:
4330 ret = 0;
4331 out_err:
4332 kfree(reply_buf);
4333 rbd_spec_put(parent_spec);
4334
4335 return ret;
4336 }
4337
rbd_dev_v2_striping_info(struct rbd_device * rbd_dev)4338 static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
4339 {
4340 struct {
4341 __le64 stripe_unit;
4342 __le64 stripe_count;
4343 } __attribute__ ((packed)) striping_info_buf = { 0 };
4344 size_t size = sizeof (striping_info_buf);
4345 void *p;
4346 u64 obj_size;
4347 u64 stripe_unit;
4348 u64 stripe_count;
4349 int ret;
4350
4351 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
4352 "rbd", "get_stripe_unit_count", NULL, 0,
4353 (char *)&striping_info_buf, size);
4354 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
4355 if (ret < 0)
4356 return ret;
4357 if (ret < size)
4358 return -ERANGE;
4359
4360 /*
4361 * We don't actually support the "fancy striping" feature
4362 * (STRIPINGV2) yet, but if the striping sizes are the
4363 * defaults the behavior is the same as before. So find
4364 * out, and only fail if the image has non-default values.
4365 */
4366 ret = -EINVAL;
4367 obj_size = (u64)1 << rbd_dev->header.obj_order;
4368 p = &striping_info_buf;
4369 stripe_unit = ceph_decode_64(&p);
4370 if (stripe_unit != obj_size) {
4371 rbd_warn(rbd_dev, "unsupported stripe unit "
4372 "(got %llu want %llu)",
4373 stripe_unit, obj_size);
4374 return -EINVAL;
4375 }
4376 stripe_count = ceph_decode_64(&p);
4377 if (stripe_count != 1) {
4378 rbd_warn(rbd_dev, "unsupported stripe count "
4379 "(got %llu want 1)", stripe_count);
4380 return -EINVAL;
4381 }
4382 rbd_dev->header.stripe_unit = stripe_unit;
4383 rbd_dev->header.stripe_count = stripe_count;
4384
4385 return 0;
4386 }
4387
rbd_dev_image_name(struct rbd_device * rbd_dev)4388 static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
4389 {
4390 size_t image_id_size;
4391 char *image_id;
4392 void *p;
4393 void *end;
4394 size_t size;
4395 void *reply_buf = NULL;
4396 size_t len = 0;
4397 char *image_name = NULL;
4398 int ret;
4399
4400 rbd_assert(!rbd_dev->spec->image_name);
4401
4402 len = strlen(rbd_dev->spec->image_id);
4403 image_id_size = sizeof (__le32) + len;
4404 image_id = kmalloc(image_id_size, GFP_KERNEL);
4405 if (!image_id)
4406 return NULL;
4407
4408 p = image_id;
4409 end = image_id + image_id_size;
4410 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
4411
4412 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
4413 reply_buf = kmalloc(size, GFP_KERNEL);
4414 if (!reply_buf)
4415 goto out;
4416
4417 ret = rbd_obj_method_sync(rbd_dev, RBD_DIRECTORY,
4418 "rbd", "dir_get_name",
4419 image_id, image_id_size,
4420 reply_buf, size);
4421 if (ret < 0)
4422 goto out;
4423 p = reply_buf;
4424 end = reply_buf + ret;
4425
4426 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
4427 if (IS_ERR(image_name))
4428 image_name = NULL;
4429 else
4430 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
4431 out:
4432 kfree(reply_buf);
4433 kfree(image_id);
4434
4435 return image_name;
4436 }
4437
rbd_v1_snap_id_by_name(struct rbd_device * rbd_dev,const char * name)4438 static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
4439 {
4440 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
4441 const char *snap_name;
4442 u32 which = 0;
4443
4444 /* Skip over names until we find the one we are looking for */
4445
4446 snap_name = rbd_dev->header.snap_names;
4447 while (which < snapc->num_snaps) {
4448 if (!strcmp(name, snap_name))
4449 return snapc->snaps[which];
4450 snap_name += strlen(snap_name) + 1;
4451 which++;
4452 }
4453 return CEPH_NOSNAP;
4454 }
4455
rbd_v2_snap_id_by_name(struct rbd_device * rbd_dev,const char * name)4456 static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
4457 {
4458 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
4459 u32 which;
4460 bool found = false;
4461 u64 snap_id;
4462
4463 for (which = 0; !found && which < snapc->num_snaps; which++) {
4464 const char *snap_name;
4465
4466 snap_id = snapc->snaps[which];
4467 snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id);
4468 if (IS_ERR(snap_name)) {
4469 /* ignore no-longer existing snapshots */
4470 if (PTR_ERR(snap_name) == -ENOENT)
4471 continue;
4472 else
4473 break;
4474 }
4475 found = !strcmp(name, snap_name);
4476 kfree(snap_name);
4477 }
4478 return found ? snap_id : CEPH_NOSNAP;
4479 }
4480
4481 /*
4482 * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
4483 * no snapshot by that name is found, or if an error occurs.
4484 */
rbd_snap_id_by_name(struct rbd_device * rbd_dev,const char * name)4485 static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
4486 {
4487 if (rbd_dev->image_format == 1)
4488 return rbd_v1_snap_id_by_name(rbd_dev, name);
4489
4490 return rbd_v2_snap_id_by_name(rbd_dev, name);
4491 }
4492
4493 /*
4494 * An image being mapped will have everything but the snap id.
4495 */
rbd_spec_fill_snap_id(struct rbd_device * rbd_dev)4496 static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev)
4497 {
4498 struct rbd_spec *spec = rbd_dev->spec;
4499
4500 rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name);
4501 rbd_assert(spec->image_id && spec->image_name);
4502 rbd_assert(spec->snap_name);
4503
4504 if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
4505 u64 snap_id;
4506
4507 snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
4508 if (snap_id == CEPH_NOSNAP)
4509 return -ENOENT;
4510
4511 spec->snap_id = snap_id;
4512 } else {
4513 spec->snap_id = CEPH_NOSNAP;
4514 }
4515
4516 return 0;
4517 }
4518
4519 /*
4520 * A parent image will have all ids but none of the names.
4521 *
4522 * All names in an rbd spec are dynamically allocated. It's OK if we
4523 * can't figure out the name for an image id.
4524 */
rbd_spec_fill_names(struct rbd_device * rbd_dev)4525 static int rbd_spec_fill_names(struct rbd_device *rbd_dev)
4526 {
4527 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
4528 struct rbd_spec *spec = rbd_dev->spec;
4529 const char *pool_name;
4530 const char *image_name;
4531 const char *snap_name;
4532 int ret;
4533
4534 rbd_assert(spec->pool_id != CEPH_NOPOOL);
4535 rbd_assert(spec->image_id);
4536 rbd_assert(spec->snap_id != CEPH_NOSNAP);
4537
4538 /* Get the pool name; we have to make our own copy of this */
4539
4540 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id);
4541 if (!pool_name) {
4542 rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id);
4543 return -EIO;
4544 }
4545 pool_name = kstrdup(pool_name, GFP_KERNEL);
4546 if (!pool_name)
4547 return -ENOMEM;
4548
4549 /* Fetch the image name; tolerate failure here */
4550
4551 image_name = rbd_dev_image_name(rbd_dev);
4552 if (!image_name)
4553 rbd_warn(rbd_dev, "unable to get image name");
4554
4555 /* Fetch the snapshot name */
4556
4557 snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
4558 if (IS_ERR(snap_name)) {
4559 ret = PTR_ERR(snap_name);
4560 goto out_err;
4561 }
4562
4563 spec->pool_name = pool_name;
4564 spec->image_name = image_name;
4565 spec->snap_name = snap_name;
4566
4567 return 0;
4568
4569 out_err:
4570 kfree(image_name);
4571 kfree(pool_name);
4572 return ret;
4573 }
4574
rbd_dev_v2_snap_context(struct rbd_device * rbd_dev)4575 static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
4576 {
4577 size_t size;
4578 int ret;
4579 void *reply_buf;
4580 void *p;
4581 void *end;
4582 u64 seq;
4583 u32 snap_count;
4584 struct ceph_snap_context *snapc;
4585 u32 i;
4586
4587 /*
4588 * We'll need room for the seq value (maximum snapshot id),
4589 * snapshot count, and array of that many snapshot ids.
4590 * For now we have a fixed upper limit on the number we're
4591 * prepared to receive.
4592 */
4593 size = sizeof (__le64) + sizeof (__le32) +
4594 RBD_MAX_SNAP_COUNT * sizeof (__le64);
4595 reply_buf = kzalloc(size, GFP_KERNEL);
4596 if (!reply_buf)
4597 return -ENOMEM;
4598
4599 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
4600 "rbd", "get_snapcontext", NULL, 0,
4601 reply_buf, size);
4602 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
4603 if (ret < 0)
4604 goto out;
4605
4606 p = reply_buf;
4607 end = reply_buf + ret;
4608 ret = -ERANGE;
4609 ceph_decode_64_safe(&p, end, seq, out);
4610 ceph_decode_32_safe(&p, end, snap_count, out);
4611
4612 /*
4613 * Make sure the reported number of snapshot ids wouldn't go
4614 * beyond the end of our buffer. But before checking that,
4615 * make sure the computed size of the snapshot context we
4616 * allocate is representable in a size_t.
4617 */
4618 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
4619 / sizeof (u64)) {
4620 ret = -EINVAL;
4621 goto out;
4622 }
4623 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
4624 goto out;
4625 ret = 0;
4626
4627 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
4628 if (!snapc) {
4629 ret = -ENOMEM;
4630 goto out;
4631 }
4632 snapc->seq = seq;
4633 for (i = 0; i < snap_count; i++)
4634 snapc->snaps[i] = ceph_decode_64(&p);
4635
4636 ceph_put_snap_context(rbd_dev->header.snapc);
4637 rbd_dev->header.snapc = snapc;
4638
4639 dout(" snap context seq = %llu, snap_count = %u\n",
4640 (unsigned long long)seq, (unsigned int)snap_count);
4641 out:
4642 kfree(reply_buf);
4643
4644 return ret;
4645 }
4646
rbd_dev_v2_snap_name(struct rbd_device * rbd_dev,u64 snap_id)4647 static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
4648 u64 snap_id)
4649 {
4650 size_t size;
4651 void *reply_buf;
4652 __le64 snapid;
4653 int ret;
4654 void *p;
4655 void *end;
4656 char *snap_name;
4657
4658 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
4659 reply_buf = kmalloc(size, GFP_KERNEL);
4660 if (!reply_buf)
4661 return ERR_PTR(-ENOMEM);
4662
4663 snapid = cpu_to_le64(snap_id);
4664 ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
4665 "rbd", "get_snapshot_name",
4666 &snapid, sizeof (snapid),
4667 reply_buf, size);
4668 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
4669 if (ret < 0) {
4670 snap_name = ERR_PTR(ret);
4671 goto out;
4672 }
4673
4674 p = reply_buf;
4675 end = reply_buf + ret;
4676 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
4677 if (IS_ERR(snap_name))
4678 goto out;
4679
4680 dout(" snap_id 0x%016llx snap_name = %s\n",
4681 (unsigned long long)snap_id, snap_name);
4682 out:
4683 kfree(reply_buf);
4684
4685 return snap_name;
4686 }
4687
rbd_dev_v2_header_info(struct rbd_device * rbd_dev)4688 static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev)
4689 {
4690 bool first_time = rbd_dev->header.object_prefix == NULL;
4691 int ret;
4692
4693 ret = rbd_dev_v2_image_size(rbd_dev);
4694 if (ret)
4695 return ret;
4696
4697 if (first_time) {
4698 ret = rbd_dev_v2_header_onetime(rbd_dev);
4699 if (ret)
4700 return ret;
4701 }
4702
4703 ret = rbd_dev_v2_snap_context(rbd_dev);
4704 dout("rbd_dev_v2_snap_context returned %d\n", ret);
4705
4706 return ret;
4707 }
4708
rbd_dev_header_info(struct rbd_device * rbd_dev)4709 static int rbd_dev_header_info(struct rbd_device *rbd_dev)
4710 {
4711 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
4712
4713 if (rbd_dev->image_format == 1)
4714 return rbd_dev_v1_header_info(rbd_dev);
4715
4716 return rbd_dev_v2_header_info(rbd_dev);
4717 }
4718
rbd_bus_add_dev(struct rbd_device * rbd_dev)4719 static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
4720 {
4721 struct device *dev;
4722 int ret;
4723
4724 dev = &rbd_dev->dev;
4725 dev->bus = &rbd_bus_type;
4726 dev->type = &rbd_device_type;
4727 dev->parent = &rbd_root_dev;
4728 dev->release = rbd_dev_device_release;
4729 dev_set_name(dev, "%d", rbd_dev->dev_id);
4730 ret = device_register(dev);
4731
4732 return ret;
4733 }
4734
rbd_bus_del_dev(struct rbd_device * rbd_dev)4735 static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
4736 {
4737 device_unregister(&rbd_dev->dev);
4738 }
4739
4740 /*
4741 * Get a unique rbd identifier for the given new rbd_dev, and add
4742 * the rbd_dev to the global list.
4743 */
rbd_dev_id_get(struct rbd_device * rbd_dev)4744 static int rbd_dev_id_get(struct rbd_device *rbd_dev)
4745 {
4746 int new_dev_id;
4747
4748 new_dev_id = ida_simple_get(&rbd_dev_id_ida,
4749 0, minor_to_rbd_dev_id(1 << MINORBITS),
4750 GFP_KERNEL);
4751 if (new_dev_id < 0)
4752 return new_dev_id;
4753
4754 rbd_dev->dev_id = new_dev_id;
4755
4756 spin_lock(&rbd_dev_list_lock);
4757 list_add_tail(&rbd_dev->node, &rbd_dev_list);
4758 spin_unlock(&rbd_dev_list_lock);
4759
4760 dout("rbd_dev %p given dev id %d\n", rbd_dev, rbd_dev->dev_id);
4761
4762 return 0;
4763 }
4764
4765 /*
4766 * Remove an rbd_dev from the global list, and record that its
4767 * identifier is no longer in use.
4768 */
rbd_dev_id_put(struct rbd_device * rbd_dev)4769 static void rbd_dev_id_put(struct rbd_device *rbd_dev)
4770 {
4771 spin_lock(&rbd_dev_list_lock);
4772 list_del_init(&rbd_dev->node);
4773 spin_unlock(&rbd_dev_list_lock);
4774
4775 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4776
4777 dout("rbd_dev %p released dev id %d\n", rbd_dev, rbd_dev->dev_id);
4778 }
4779
4780 /*
4781 * Skips over white space at *buf, and updates *buf to point to the
4782 * first found non-space character (if any). Returns the length of
4783 * the token (string of non-white space characters) found. Note
4784 * that *buf must be terminated with '\0'.
4785 */
next_token(const char ** buf)4786 static inline size_t next_token(const char **buf)
4787 {
4788 /*
4789 * These are the characters that produce nonzero for
4790 * isspace() in the "C" and "POSIX" locales.
4791 */
4792 const char *spaces = " \f\n\r\t\v";
4793
4794 *buf += strspn(*buf, spaces); /* Find start of token */
4795
4796 return strcspn(*buf, spaces); /* Return token length */
4797 }
4798
4799 /*
4800 * Finds the next token in *buf, dynamically allocates a buffer big
4801 * enough to hold a copy of it, and copies the token into the new
4802 * buffer. The copy is guaranteed to be terminated with '\0'. Note
4803 * that a duplicate buffer is created even for a zero-length token.
4804 *
4805 * Returns a pointer to the newly-allocated duplicate, or a null
4806 * pointer if memory for the duplicate was not available. If
4807 * the lenp argument is a non-null pointer, the length of the token
4808 * (not including the '\0') is returned in *lenp.
4809 *
4810 * If successful, the *buf pointer will be updated to point beyond
4811 * the end of the found token.
4812 *
4813 * Note: uses GFP_KERNEL for allocation.
4814 */
dup_token(const char ** buf,size_t * lenp)4815 static inline char *dup_token(const char **buf, size_t *lenp)
4816 {
4817 char *dup;
4818 size_t len;
4819
4820 len = next_token(buf);
4821 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
4822 if (!dup)
4823 return NULL;
4824 *(dup + len) = '\0';
4825 *buf += len;
4826
4827 if (lenp)
4828 *lenp = len;
4829
4830 return dup;
4831 }
4832
4833 /*
4834 * Parse the options provided for an "rbd add" (i.e., rbd image
4835 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
4836 * and the data written is passed here via a NUL-terminated buffer.
4837 * Returns 0 if successful or an error code otherwise.
4838 *
4839 * The information extracted from these options is recorded in
4840 * the other parameters which return dynamically-allocated
4841 * structures:
4842 * ceph_opts
4843 * The address of a pointer that will refer to a ceph options
4844 * structure. Caller must release the returned pointer using
4845 * ceph_destroy_options() when it is no longer needed.
4846 * rbd_opts
4847 * Address of an rbd options pointer. Fully initialized by
4848 * this function; caller must release with kfree().
4849 * spec
4850 * Address of an rbd image specification pointer. Fully
4851 * initialized by this function based on parsed options.
4852 * Caller must release with rbd_spec_put().
4853 *
4854 * The options passed take this form:
4855 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
4856 * where:
4857 * <mon_addrs>
4858 * A comma-separated list of one or more monitor addresses.
4859 * A monitor address is an ip address, optionally followed
4860 * by a port number (separated by a colon).
4861 * I.e.: ip1[:port1][,ip2[:port2]...]
4862 * <options>
4863 * A comma-separated list of ceph and/or rbd options.
4864 * <pool_name>
4865 * The name of the rados pool containing the rbd image.
4866 * <image_name>
4867 * The name of the image in that pool to map.
4868 * <snap_id>
4869 * An optional snapshot id. If provided, the mapping will
4870 * present data from the image at the time that snapshot was
4871 * created. The image head is used if no snapshot id is
4872 * provided. Snapshot mappings are always read-only.
4873 */
rbd_add_parse_args(const char * buf,struct ceph_options ** ceph_opts,struct rbd_options ** opts,struct rbd_spec ** rbd_spec)4874 static int rbd_add_parse_args(const char *buf,
4875 struct ceph_options **ceph_opts,
4876 struct rbd_options **opts,
4877 struct rbd_spec **rbd_spec)
4878 {
4879 size_t len;
4880 char *options;
4881 const char *mon_addrs;
4882 char *snap_name;
4883 size_t mon_addrs_size;
4884 struct rbd_spec *spec = NULL;
4885 struct rbd_options *rbd_opts = NULL;
4886 struct ceph_options *copts;
4887 int ret;
4888
4889 /* The first four tokens are required */
4890
4891 len = next_token(&buf);
4892 if (!len) {
4893 rbd_warn(NULL, "no monitor address(es) provided");
4894 return -EINVAL;
4895 }
4896 mon_addrs = buf;
4897 mon_addrs_size = len + 1;
4898 buf += len;
4899
4900 ret = -EINVAL;
4901 options = dup_token(&buf, NULL);
4902 if (!options)
4903 return -ENOMEM;
4904 if (!*options) {
4905 rbd_warn(NULL, "no options provided");
4906 goto out_err;
4907 }
4908
4909 spec = rbd_spec_alloc();
4910 if (!spec)
4911 goto out_mem;
4912
4913 spec->pool_name = dup_token(&buf, NULL);
4914 if (!spec->pool_name)
4915 goto out_mem;
4916 if (!*spec->pool_name) {
4917 rbd_warn(NULL, "no pool name provided");
4918 goto out_err;
4919 }
4920
4921 spec->image_name = dup_token(&buf, NULL);
4922 if (!spec->image_name)
4923 goto out_mem;
4924 if (!*spec->image_name) {
4925 rbd_warn(NULL, "no image name provided");
4926 goto out_err;
4927 }
4928
4929 /*
4930 * Snapshot name is optional; default is to use "-"
4931 * (indicating the head/no snapshot).
4932 */
4933 len = next_token(&buf);
4934 if (!len) {
4935 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
4936 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
4937 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
4938 ret = -ENAMETOOLONG;
4939 goto out_err;
4940 }
4941 snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
4942 if (!snap_name)
4943 goto out_mem;
4944 *(snap_name + len) = '\0';
4945 spec->snap_name = snap_name;
4946
4947 /* Initialize all rbd options to the defaults */
4948
4949 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
4950 if (!rbd_opts)
4951 goto out_mem;
4952
4953 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
4954
4955 copts = ceph_parse_options(options, mon_addrs,
4956 mon_addrs + mon_addrs_size - 1,
4957 parse_rbd_opts_token, rbd_opts);
4958 if (IS_ERR(copts)) {
4959 ret = PTR_ERR(copts);
4960 goto out_err;
4961 }
4962 kfree(options);
4963
4964 *ceph_opts = copts;
4965 *opts = rbd_opts;
4966 *rbd_spec = spec;
4967
4968 return 0;
4969 out_mem:
4970 ret = -ENOMEM;
4971 out_err:
4972 kfree(rbd_opts);
4973 rbd_spec_put(spec);
4974 kfree(options);
4975
4976 return ret;
4977 }
4978
4979 /*
4980 * Return pool id (>= 0) or a negative error code.
4981 */
rbd_add_get_pool_id(struct rbd_client * rbdc,const char * pool_name)4982 static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name)
4983 {
4984 u64 newest_epoch;
4985 unsigned long timeout = rbdc->client->options->mount_timeout * HZ;
4986 int tries = 0;
4987 int ret;
4988
4989 again:
4990 ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name);
4991 if (ret == -ENOENT && tries++ < 1) {
4992 ret = ceph_monc_do_get_version(&rbdc->client->monc, "osdmap",
4993 &newest_epoch);
4994 if (ret < 0)
4995 return ret;
4996
4997 if (rbdc->client->osdc.osdmap->epoch < newest_epoch) {
4998 ceph_monc_request_next_osdmap(&rbdc->client->monc);
4999 (void) ceph_monc_wait_osdmap(&rbdc->client->monc,
5000 newest_epoch, timeout);
5001 goto again;
5002 } else {
5003 /* the osdmap we have is new enough */
5004 return -ENOENT;
5005 }
5006 }
5007
5008 return ret;
5009 }
5010
5011 /*
5012 * An rbd format 2 image has a unique identifier, distinct from the
5013 * name given to it by the user. Internally, that identifier is
5014 * what's used to specify the names of objects related to the image.
5015 *
5016 * A special "rbd id" object is used to map an rbd image name to its
5017 * id. If that object doesn't exist, then there is no v2 rbd image
5018 * with the supplied name.
5019 *
5020 * This function will record the given rbd_dev's image_id field if
5021 * it can be determined, and in that case will return 0. If any
5022 * errors occur a negative errno will be returned and the rbd_dev's
5023 * image_id field will be unchanged (and should be NULL).
5024 */
rbd_dev_image_id(struct rbd_device * rbd_dev)5025 static int rbd_dev_image_id(struct rbd_device *rbd_dev)
5026 {
5027 int ret;
5028 size_t size;
5029 char *object_name;
5030 void *response;
5031 char *image_id;
5032
5033 /*
5034 * When probing a parent image, the image id is already
5035 * known (and the image name likely is not). There's no
5036 * need to fetch the image id again in this case. We
5037 * do still need to set the image format though.
5038 */
5039 if (rbd_dev->spec->image_id) {
5040 rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1;
5041
5042 return 0;
5043 }
5044
5045 /*
5046 * First, see if the format 2 image id file exists, and if
5047 * so, get the image's persistent id from it.
5048 */
5049 size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
5050 object_name = kmalloc(size, GFP_NOIO);
5051 if (!object_name)
5052 return -ENOMEM;
5053 sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
5054 dout("rbd id object name is %s\n", object_name);
5055
5056 /* Response will be an encoded string, which includes a length */
5057
5058 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
5059 response = kzalloc(size, GFP_NOIO);
5060 if (!response) {
5061 ret = -ENOMEM;
5062 goto out;
5063 }
5064
5065 /* If it doesn't exist we'll assume it's a format 1 image */
5066
5067 ret = rbd_obj_method_sync(rbd_dev, object_name,
5068 "rbd", "get_id", NULL, 0,
5069 response, RBD_IMAGE_ID_LEN_MAX);
5070 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
5071 if (ret == -ENOENT) {
5072 image_id = kstrdup("", GFP_KERNEL);
5073 ret = image_id ? 0 : -ENOMEM;
5074 if (!ret)
5075 rbd_dev->image_format = 1;
5076 } else if (ret >= 0) {
5077 void *p = response;
5078
5079 image_id = ceph_extract_encoded_string(&p, p + ret,
5080 NULL, GFP_NOIO);
5081 ret = PTR_ERR_OR_ZERO(image_id);
5082 if (!ret)
5083 rbd_dev->image_format = 2;
5084 }
5085
5086 if (!ret) {
5087 rbd_dev->spec->image_id = image_id;
5088 dout("image_id is %s\n", image_id);
5089 }
5090 out:
5091 kfree(response);
5092 kfree(object_name);
5093
5094 return ret;
5095 }
5096
5097 /*
5098 * Undo whatever state changes are made by v1 or v2 header info
5099 * call.
5100 */
rbd_dev_unprobe(struct rbd_device * rbd_dev)5101 static void rbd_dev_unprobe(struct rbd_device *rbd_dev)
5102 {
5103 struct rbd_image_header *header;
5104
5105 rbd_dev_parent_put(rbd_dev);
5106
5107 /* Free dynamic fields from the header, then zero it out */
5108
5109 header = &rbd_dev->header;
5110 ceph_put_snap_context(header->snapc);
5111 kfree(header->snap_sizes);
5112 kfree(header->snap_names);
5113 kfree(header->object_prefix);
5114 memset(header, 0, sizeof (*header));
5115 }
5116
rbd_dev_v2_header_onetime(struct rbd_device * rbd_dev)5117 static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev)
5118 {
5119 int ret;
5120
5121 ret = rbd_dev_v2_object_prefix(rbd_dev);
5122 if (ret)
5123 goto out_err;
5124
5125 /*
5126 * Get the and check features for the image. Currently the
5127 * features are assumed to never change.
5128 */
5129 ret = rbd_dev_v2_features(rbd_dev);
5130 if (ret)
5131 goto out_err;
5132
5133 /* If the image supports fancy striping, get its parameters */
5134
5135 if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
5136 ret = rbd_dev_v2_striping_info(rbd_dev);
5137 if (ret < 0)
5138 goto out_err;
5139 }
5140 /* No support for crypto and compression type format 2 images */
5141
5142 return 0;
5143 out_err:
5144 rbd_dev->header.features = 0;
5145 kfree(rbd_dev->header.object_prefix);
5146 rbd_dev->header.object_prefix = NULL;
5147
5148 return ret;
5149 }
5150
5151 /*
5152 * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() ->
5153 * rbd_dev_image_probe() recursion depth, which means it's also the
5154 * length of the already discovered part of the parent chain.
5155 */
rbd_dev_probe_parent(struct rbd_device * rbd_dev,int depth)5156 static int rbd_dev_probe_parent(struct rbd_device *rbd_dev, int depth)
5157 {
5158 struct rbd_device *parent = NULL;
5159 int ret;
5160
5161 if (!rbd_dev->parent_spec)
5162 return 0;
5163
5164 if (++depth > RBD_MAX_PARENT_CHAIN_LEN) {
5165 pr_info("parent chain is too long (%d)\n", depth);
5166 ret = -EINVAL;
5167 goto out_err;
5168 }
5169
5170 parent = rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec);
5171 if (!parent) {
5172 ret = -ENOMEM;
5173 goto out_err;
5174 }
5175
5176 /*
5177 * Images related by parent/child relationships always share
5178 * rbd_client and spec/parent_spec, so bump their refcounts.
5179 */
5180 __rbd_get_client(rbd_dev->rbd_client);
5181 rbd_spec_get(rbd_dev->parent_spec);
5182
5183 ret = rbd_dev_image_probe(parent, depth);
5184 if (ret < 0)
5185 goto out_err;
5186
5187 rbd_dev->parent = parent;
5188 atomic_set(&rbd_dev->parent_ref, 1);
5189 return 0;
5190
5191 out_err:
5192 rbd_dev_unparent(rbd_dev);
5193 if (parent)
5194 rbd_dev_destroy(parent);
5195 return ret;
5196 }
5197
rbd_dev_device_setup(struct rbd_device * rbd_dev)5198 static int rbd_dev_device_setup(struct rbd_device *rbd_dev)
5199 {
5200 int ret;
5201
5202 /* Get an id and fill in device name. */
5203
5204 ret = rbd_dev_id_get(rbd_dev);
5205 if (ret)
5206 return ret;
5207
5208 BUILD_BUG_ON(DEV_NAME_LEN
5209 < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
5210 sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
5211
5212 /* Record our major and minor device numbers. */
5213
5214 if (!single_major) {
5215 ret = register_blkdev(0, rbd_dev->name);
5216 if (ret < 0)
5217 goto err_out_id;
5218
5219 rbd_dev->major = ret;
5220 rbd_dev->minor = 0;
5221 } else {
5222 rbd_dev->major = rbd_major;
5223 rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id);
5224 }
5225
5226 /* Set up the blkdev mapping. */
5227
5228 ret = rbd_init_disk(rbd_dev);
5229 if (ret)
5230 goto err_out_blkdev;
5231
5232 ret = rbd_dev_mapping_set(rbd_dev);
5233 if (ret)
5234 goto err_out_disk;
5235
5236 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
5237 set_disk_ro(rbd_dev->disk, rbd_dev->mapping.read_only);
5238
5239 ret = rbd_bus_add_dev(rbd_dev);
5240 if (ret)
5241 goto err_out_mapping;
5242
5243 /* Everything's ready. Announce the disk to the world. */
5244
5245 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
5246 add_disk(rbd_dev->disk);
5247
5248 pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
5249 (unsigned long long) rbd_dev->mapping.size);
5250
5251 return ret;
5252
5253 err_out_mapping:
5254 rbd_dev_mapping_clear(rbd_dev);
5255 err_out_disk:
5256 rbd_free_disk(rbd_dev);
5257 err_out_blkdev:
5258 if (!single_major)
5259 unregister_blkdev(rbd_dev->major, rbd_dev->name);
5260 err_out_id:
5261 rbd_dev_id_put(rbd_dev);
5262 rbd_dev_mapping_clear(rbd_dev);
5263
5264 return ret;
5265 }
5266
rbd_dev_header_name(struct rbd_device * rbd_dev)5267 static int rbd_dev_header_name(struct rbd_device *rbd_dev)
5268 {
5269 struct rbd_spec *spec = rbd_dev->spec;
5270 size_t size;
5271
5272 /* Record the header object name for this rbd image. */
5273
5274 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
5275
5276 if (rbd_dev->image_format == 1)
5277 size = strlen(spec->image_name) + sizeof (RBD_SUFFIX);
5278 else
5279 size = sizeof (RBD_HEADER_PREFIX) + strlen(spec->image_id);
5280
5281 rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
5282 if (!rbd_dev->header_name)
5283 return -ENOMEM;
5284
5285 if (rbd_dev->image_format == 1)
5286 sprintf(rbd_dev->header_name, "%s%s",
5287 spec->image_name, RBD_SUFFIX);
5288 else
5289 sprintf(rbd_dev->header_name, "%s%s",
5290 RBD_HEADER_PREFIX, spec->image_id);
5291 return 0;
5292 }
5293
rbd_dev_image_release(struct rbd_device * rbd_dev)5294 static void rbd_dev_image_release(struct rbd_device *rbd_dev)
5295 {
5296 rbd_dev_unprobe(rbd_dev);
5297 kfree(rbd_dev->header_name);
5298 rbd_dev->header_name = NULL;
5299 rbd_dev->image_format = 0;
5300 kfree(rbd_dev->spec->image_id);
5301 rbd_dev->spec->image_id = NULL;
5302
5303 rbd_dev_destroy(rbd_dev);
5304 }
5305
5306 /*
5307 * Probe for the existence of the header object for the given rbd
5308 * device. If this image is the one being mapped (i.e., not a
5309 * parent), initiate a watch on its header object before using that
5310 * object to get detailed information about the rbd image.
5311 */
rbd_dev_image_probe(struct rbd_device * rbd_dev,int depth)5312 static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth)
5313 {
5314 int ret;
5315
5316 /*
5317 * Get the id from the image id object. Unless there's an
5318 * error, rbd_dev->spec->image_id will be filled in with
5319 * a dynamically-allocated string, and rbd_dev->image_format
5320 * will be set to either 1 or 2.
5321 */
5322 ret = rbd_dev_image_id(rbd_dev);
5323 if (ret)
5324 return ret;
5325
5326 ret = rbd_dev_header_name(rbd_dev);
5327 if (ret)
5328 goto err_out_format;
5329
5330 if (!depth) {
5331 ret = rbd_dev_header_watch_sync(rbd_dev);
5332 if (ret) {
5333 if (ret == -ENOENT)
5334 pr_info("image %s/%s does not exist\n",
5335 rbd_dev->spec->pool_name,
5336 rbd_dev->spec->image_name);
5337 goto out_header_name;
5338 }
5339 }
5340
5341 ret = rbd_dev_header_info(rbd_dev);
5342 if (ret)
5343 goto err_out_watch;
5344
5345 /*
5346 * If this image is the one being mapped, we have pool name and
5347 * id, image name and id, and snap name - need to fill snap id.
5348 * Otherwise this is a parent image, identified by pool, image
5349 * and snap ids - need to fill in names for those ids.
5350 */
5351 if (!depth)
5352 ret = rbd_spec_fill_snap_id(rbd_dev);
5353 else
5354 ret = rbd_spec_fill_names(rbd_dev);
5355 if (ret) {
5356 if (ret == -ENOENT)
5357 pr_info("snap %s/%s@%s does not exist\n",
5358 rbd_dev->spec->pool_name,
5359 rbd_dev->spec->image_name,
5360 rbd_dev->spec->snap_name);
5361 goto err_out_probe;
5362 }
5363
5364 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
5365 ret = rbd_dev_v2_parent_info(rbd_dev);
5366 if (ret)
5367 goto err_out_probe;
5368
5369 /*
5370 * Need to warn users if this image is the one being
5371 * mapped and has a parent.
5372 */
5373 if (!depth && rbd_dev->parent_spec)
5374 rbd_warn(rbd_dev,
5375 "WARNING: kernel layering is EXPERIMENTAL!");
5376 }
5377
5378 ret = rbd_dev_probe_parent(rbd_dev, depth);
5379 if (ret)
5380 goto err_out_probe;
5381
5382 dout("discovered format %u image, header name is %s\n",
5383 rbd_dev->image_format, rbd_dev->header_name);
5384 return 0;
5385
5386 err_out_probe:
5387 rbd_dev_unprobe(rbd_dev);
5388 err_out_watch:
5389 if (!depth)
5390 rbd_dev_header_unwatch_sync(rbd_dev);
5391 out_header_name:
5392 kfree(rbd_dev->header_name);
5393 rbd_dev->header_name = NULL;
5394 err_out_format:
5395 rbd_dev->image_format = 0;
5396 kfree(rbd_dev->spec->image_id);
5397 rbd_dev->spec->image_id = NULL;
5398 return ret;
5399 }
5400
do_rbd_add(struct bus_type * bus,const char * buf,size_t count)5401 static ssize_t do_rbd_add(struct bus_type *bus,
5402 const char *buf,
5403 size_t count)
5404 {
5405 struct rbd_device *rbd_dev = NULL;
5406 struct ceph_options *ceph_opts = NULL;
5407 struct rbd_options *rbd_opts = NULL;
5408 struct rbd_spec *spec = NULL;
5409 struct rbd_client *rbdc;
5410 bool read_only;
5411 int rc = -ENOMEM;
5412
5413 if (!try_module_get(THIS_MODULE))
5414 return -ENODEV;
5415
5416 /* parse add command */
5417 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
5418 if (rc < 0)
5419 goto err_out_module;
5420 read_only = rbd_opts->read_only;
5421 kfree(rbd_opts);
5422 rbd_opts = NULL; /* done with this */
5423
5424 rbdc = rbd_get_client(ceph_opts);
5425 if (IS_ERR(rbdc)) {
5426 rc = PTR_ERR(rbdc);
5427 goto err_out_args;
5428 }
5429
5430 /* pick the pool */
5431 rc = rbd_add_get_pool_id(rbdc, spec->pool_name);
5432 if (rc < 0) {
5433 if (rc == -ENOENT)
5434 pr_info("pool %s does not exist\n", spec->pool_name);
5435 goto err_out_client;
5436 }
5437 spec->pool_id = (u64)rc;
5438
5439 /* The ceph file layout needs to fit pool id in 32 bits */
5440
5441 if (spec->pool_id > (u64)U32_MAX) {
5442 rbd_warn(NULL, "pool id too large (%llu > %u)",
5443 (unsigned long long)spec->pool_id, U32_MAX);
5444 rc = -EIO;
5445 goto err_out_client;
5446 }
5447
5448 rbd_dev = rbd_dev_create(rbdc, spec);
5449 if (!rbd_dev)
5450 goto err_out_client;
5451 rbdc = NULL; /* rbd_dev now owns this */
5452 spec = NULL; /* rbd_dev now owns this */
5453
5454 rc = rbd_dev_image_probe(rbd_dev, 0);
5455 if (rc < 0)
5456 goto err_out_rbd_dev;
5457
5458 /* If we are mapping a snapshot it must be marked read-only */
5459
5460 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
5461 read_only = true;
5462 rbd_dev->mapping.read_only = read_only;
5463
5464 rc = rbd_dev_device_setup(rbd_dev);
5465 if (rc) {
5466 /*
5467 * rbd_dev_header_unwatch_sync() can't be moved into
5468 * rbd_dev_image_release() without refactoring, see
5469 * commit 1f3ef78861ac.
5470 */
5471 rbd_dev_header_unwatch_sync(rbd_dev);
5472 rbd_dev_image_release(rbd_dev);
5473 goto err_out_module;
5474 }
5475
5476 return count;
5477
5478 err_out_rbd_dev:
5479 rbd_dev_destroy(rbd_dev);
5480 err_out_client:
5481 rbd_put_client(rbdc);
5482 err_out_args:
5483 rbd_spec_put(spec);
5484 err_out_module:
5485 module_put(THIS_MODULE);
5486
5487 dout("Error adding device %s\n", buf);
5488
5489 return (ssize_t)rc;
5490 }
5491
rbd_add(struct bus_type * bus,const char * buf,size_t count)5492 static ssize_t rbd_add(struct bus_type *bus,
5493 const char *buf,
5494 size_t count)
5495 {
5496 if (single_major)
5497 return -EINVAL;
5498
5499 return do_rbd_add(bus, buf, count);
5500 }
5501
rbd_add_single_major(struct bus_type * bus,const char * buf,size_t count)5502 static ssize_t rbd_add_single_major(struct bus_type *bus,
5503 const char *buf,
5504 size_t count)
5505 {
5506 return do_rbd_add(bus, buf, count);
5507 }
5508
rbd_dev_device_release(struct device * dev)5509 static void rbd_dev_device_release(struct device *dev)
5510 {
5511 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
5512
5513 rbd_free_disk(rbd_dev);
5514 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
5515 rbd_dev_mapping_clear(rbd_dev);
5516 if (!single_major)
5517 unregister_blkdev(rbd_dev->major, rbd_dev->name);
5518 rbd_dev_id_put(rbd_dev);
5519 rbd_dev_mapping_clear(rbd_dev);
5520 }
5521
rbd_dev_remove_parent(struct rbd_device * rbd_dev)5522 static void rbd_dev_remove_parent(struct rbd_device *rbd_dev)
5523 {
5524 while (rbd_dev->parent) {
5525 struct rbd_device *first = rbd_dev;
5526 struct rbd_device *second = first->parent;
5527 struct rbd_device *third;
5528
5529 /*
5530 * Follow to the parent with no grandparent and
5531 * remove it.
5532 */
5533 while (second && (third = second->parent)) {
5534 first = second;
5535 second = third;
5536 }
5537 rbd_assert(second);
5538 rbd_dev_image_release(second);
5539 first->parent = NULL;
5540 first->parent_overlap = 0;
5541
5542 rbd_assert(first->parent_spec);
5543 rbd_spec_put(first->parent_spec);
5544 first->parent_spec = NULL;
5545 }
5546 }
5547
do_rbd_remove(struct bus_type * bus,const char * buf,size_t count)5548 static ssize_t do_rbd_remove(struct bus_type *bus,
5549 const char *buf,
5550 size_t count)
5551 {
5552 struct rbd_device *rbd_dev = NULL;
5553 struct list_head *tmp;
5554 int dev_id;
5555 unsigned long ul;
5556 bool already = false;
5557 int ret;
5558
5559 ret = kstrtoul(buf, 10, &ul);
5560 if (ret)
5561 return ret;
5562
5563 /* convert to int; abort if we lost anything in the conversion */
5564 dev_id = (int)ul;
5565 if (dev_id != ul)
5566 return -EINVAL;
5567
5568 ret = -ENOENT;
5569 spin_lock(&rbd_dev_list_lock);
5570 list_for_each(tmp, &rbd_dev_list) {
5571 rbd_dev = list_entry(tmp, struct rbd_device, node);
5572 if (rbd_dev->dev_id == dev_id) {
5573 ret = 0;
5574 break;
5575 }
5576 }
5577 if (!ret) {
5578 spin_lock_irq(&rbd_dev->lock);
5579 if (rbd_dev->open_count)
5580 ret = -EBUSY;
5581 else
5582 already = test_and_set_bit(RBD_DEV_FLAG_REMOVING,
5583 &rbd_dev->flags);
5584 spin_unlock_irq(&rbd_dev->lock);
5585 }
5586 spin_unlock(&rbd_dev_list_lock);
5587 if (ret < 0 || already)
5588 return ret;
5589
5590 rbd_dev_header_unwatch_sync(rbd_dev);
5591 /*
5592 * flush remaining watch callbacks - these must be complete
5593 * before the osd_client is shutdown
5594 */
5595 dout("%s: flushing notifies", __func__);
5596 ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc);
5597
5598 /*
5599 * Don't free anything from rbd_dev->disk until after all
5600 * notifies are completely processed. Otherwise
5601 * rbd_bus_del_dev() will race with rbd_watch_cb(), resulting
5602 * in a potential use after free of rbd_dev->disk or rbd_dev.
5603 */
5604 rbd_bus_del_dev(rbd_dev);
5605 rbd_dev_image_release(rbd_dev);
5606 module_put(THIS_MODULE);
5607
5608 return count;
5609 }
5610
rbd_remove(struct bus_type * bus,const char * buf,size_t count)5611 static ssize_t rbd_remove(struct bus_type *bus,
5612 const char *buf,
5613 size_t count)
5614 {
5615 if (single_major)
5616 return -EINVAL;
5617
5618 return do_rbd_remove(bus, buf, count);
5619 }
5620
rbd_remove_single_major(struct bus_type * bus,const char * buf,size_t count)5621 static ssize_t rbd_remove_single_major(struct bus_type *bus,
5622 const char *buf,
5623 size_t count)
5624 {
5625 return do_rbd_remove(bus, buf, count);
5626 }
5627
5628 /*
5629 * create control files in sysfs
5630 * /sys/bus/rbd/...
5631 */
rbd_sysfs_init(void)5632 static int rbd_sysfs_init(void)
5633 {
5634 int ret;
5635
5636 ret = device_register(&rbd_root_dev);
5637 if (ret < 0)
5638 return ret;
5639
5640 ret = bus_register(&rbd_bus_type);
5641 if (ret < 0)
5642 device_unregister(&rbd_root_dev);
5643
5644 return ret;
5645 }
5646
rbd_sysfs_cleanup(void)5647 static void rbd_sysfs_cleanup(void)
5648 {
5649 bus_unregister(&rbd_bus_type);
5650 device_unregister(&rbd_root_dev);
5651 }
5652
rbd_slab_init(void)5653 static int rbd_slab_init(void)
5654 {
5655 rbd_assert(!rbd_img_request_cache);
5656 rbd_img_request_cache = kmem_cache_create("rbd_img_request",
5657 sizeof (struct rbd_img_request),
5658 __alignof__(struct rbd_img_request),
5659 0, NULL);
5660 if (!rbd_img_request_cache)
5661 return -ENOMEM;
5662
5663 rbd_assert(!rbd_obj_request_cache);
5664 rbd_obj_request_cache = kmem_cache_create("rbd_obj_request",
5665 sizeof (struct rbd_obj_request),
5666 __alignof__(struct rbd_obj_request),
5667 0, NULL);
5668 if (!rbd_obj_request_cache)
5669 goto out_err;
5670
5671 rbd_assert(!rbd_segment_name_cache);
5672 rbd_segment_name_cache = kmem_cache_create("rbd_segment_name",
5673 CEPH_MAX_OID_NAME_LEN + 1, 1, 0, NULL);
5674 if (rbd_segment_name_cache)
5675 return 0;
5676 out_err:
5677 if (rbd_obj_request_cache) {
5678 kmem_cache_destroy(rbd_obj_request_cache);
5679 rbd_obj_request_cache = NULL;
5680 }
5681
5682 kmem_cache_destroy(rbd_img_request_cache);
5683 rbd_img_request_cache = NULL;
5684
5685 return -ENOMEM;
5686 }
5687
rbd_slab_exit(void)5688 static void rbd_slab_exit(void)
5689 {
5690 rbd_assert(rbd_segment_name_cache);
5691 kmem_cache_destroy(rbd_segment_name_cache);
5692 rbd_segment_name_cache = NULL;
5693
5694 rbd_assert(rbd_obj_request_cache);
5695 kmem_cache_destroy(rbd_obj_request_cache);
5696 rbd_obj_request_cache = NULL;
5697
5698 rbd_assert(rbd_img_request_cache);
5699 kmem_cache_destroy(rbd_img_request_cache);
5700 rbd_img_request_cache = NULL;
5701 }
5702
rbd_init(void)5703 static int __init rbd_init(void)
5704 {
5705 int rc;
5706
5707 if (!libceph_compatible(NULL)) {
5708 rbd_warn(NULL, "libceph incompatibility (quitting)");
5709 return -EINVAL;
5710 }
5711
5712 rc = rbd_slab_init();
5713 if (rc)
5714 return rc;
5715
5716 /*
5717 * The number of active work items is limited by the number of
5718 * rbd devices * queue depth, so leave @max_active at default.
5719 */
5720 rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0);
5721 if (!rbd_wq) {
5722 rc = -ENOMEM;
5723 goto err_out_slab;
5724 }
5725
5726 if (single_major) {
5727 rbd_major = register_blkdev(0, RBD_DRV_NAME);
5728 if (rbd_major < 0) {
5729 rc = rbd_major;
5730 goto err_out_wq;
5731 }
5732 }
5733
5734 rc = rbd_sysfs_init();
5735 if (rc)
5736 goto err_out_blkdev;
5737
5738 if (single_major)
5739 pr_info("loaded (major %d)\n", rbd_major);
5740 else
5741 pr_info("loaded\n");
5742
5743 return 0;
5744
5745 err_out_blkdev:
5746 if (single_major)
5747 unregister_blkdev(rbd_major, RBD_DRV_NAME);
5748 err_out_wq:
5749 destroy_workqueue(rbd_wq);
5750 err_out_slab:
5751 rbd_slab_exit();
5752 return rc;
5753 }
5754
rbd_exit(void)5755 static void __exit rbd_exit(void)
5756 {
5757 ida_destroy(&rbd_dev_id_ida);
5758 rbd_sysfs_cleanup();
5759 if (single_major)
5760 unregister_blkdev(rbd_major, RBD_DRV_NAME);
5761 destroy_workqueue(rbd_wq);
5762 rbd_slab_exit();
5763 }
5764
5765 module_init(rbd_init);
5766 module_exit(rbd_exit);
5767
5768 MODULE_AUTHOR("Alex Elder <elder@inktank.com>");
5769 MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
5770 MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
5771 /* following authorship retained from original osdblk.c */
5772 MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
5773
5774 MODULE_DESCRIPTION("RADOS Block Device (RBD) driver");
5775 MODULE_LICENSE("GPL");
5776