3 rbd.c -- Export ceph rados objects as a Linux block device
6 based on drivers/block/osdblk.c:
8 Copyright 2009 Red Hat, Inc.
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.
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.
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.
25 For usage instructions, please refer to:
27 Documentation/ABI/testing/sysfs-bus-rbd
31 #include <linux/ceph/libceph.h>
32 #include <linux/ceph/osd_client.h>
33 #include <linux/ceph/mon_client.h>
34 #include <linux/ceph/cls_lock_client.h>
35 #include <linux/ceph/decode.h>
36 #include <linux/parser.h>
37 #include <linux/bsearch.h>
39 #include <linux/kernel.h>
40 #include <linux/device.h>
41 #include <linux/module.h>
42 #include <linux/blk-mq.h>
44 #include <linux/blkdev.h>
45 #include <linux/slab.h>
46 #include <linux/idr.h>
47 #include <linux/workqueue.h>
49 #include "rbd_types.h"
51 #define RBD_DEBUG /* Activate rbd_assert() calls */
54 * The basic unit of block I/O is a sector. It is interpreted in a
55 * number of contexts in Linux (blk, bio, genhd), but the default is
56 * universally 512 bytes. These symbols are just slightly more
57 * meaningful than the bare numbers they represent.
59 #define SECTOR_SHIFT 9
60 #define SECTOR_SIZE (1ULL << SECTOR_SHIFT)
63 * Increment the given counter and return its updated value.
64 * If the counter is already 0 it will not be incremented.
65 * If the counter is already at its maximum value returns
66 * -EINVAL without updating it.
68 static int atomic_inc_return_safe(atomic_t
*v
)
72 counter
= (unsigned int)__atomic_add_unless(v
, 1, 0);
73 if (counter
<= (unsigned int)INT_MAX
)
81 /* Decrement the counter. Return the resulting value, or -EINVAL */
82 static int atomic_dec_return_safe(atomic_t
*v
)
86 counter
= atomic_dec_return(v
);
95 #define RBD_DRV_NAME "rbd"
97 #define RBD_MINORS_PER_MAJOR 256
98 #define RBD_SINGLE_MAJOR_PART_SHIFT 4
100 #define RBD_MAX_PARENT_CHAIN_LEN 16
102 #define RBD_SNAP_DEV_NAME_PREFIX "snap_"
103 #define RBD_MAX_SNAP_NAME_LEN \
104 (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1))
106 #define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
108 #define RBD_SNAP_HEAD_NAME "-"
110 #define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */
112 /* This allows a single page to hold an image name sent by OSD */
113 #define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1)
114 #define RBD_IMAGE_ID_LEN_MAX 64
116 #define RBD_OBJ_PREFIX_LEN_MAX 64
118 #define RBD_NOTIFY_TIMEOUT 5 /* seconds */
119 #define RBD_RETRY_DELAY msecs_to_jiffies(1000)
123 #define RBD_FEATURE_LAYERING (1ULL<<0)
124 #define RBD_FEATURE_STRIPINGV2 (1ULL<<1)
125 #define RBD_FEATURE_EXCLUSIVE_LOCK (1ULL<<2)
126 #define RBD_FEATURE_DATA_POOL (1ULL<<7)
127 #define RBD_FEATURE_OPERATIONS (1ULL<<8)
129 #define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \
130 RBD_FEATURE_STRIPINGV2 | \
131 RBD_FEATURE_EXCLUSIVE_LOCK | \
132 RBD_FEATURE_DATA_POOL | \
133 RBD_FEATURE_OPERATIONS)
135 /* Features supported by this (client software) implementation. */
137 #define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL)
140 * An RBD device name will be "rbd#", where the "rbd" comes from
141 * RBD_DRV_NAME above, and # is a unique integer identifier.
143 #define DEV_NAME_LEN 32
146 * block device image metadata (in-memory version)
148 struct rbd_image_header
{
149 /* These six fields never change for a given rbd image */
155 u64 features
; /* Might be changeable someday? */
157 /* The remaining fields need to be updated occasionally */
159 struct ceph_snap_context
*snapc
;
160 char *snap_names
; /* format 1 only */
161 u64
*snap_sizes
; /* format 1 only */
165 * An rbd image specification.
167 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
168 * identify an image. Each rbd_dev structure includes a pointer to
169 * an rbd_spec structure that encapsulates this identity.
171 * Each of the id's in an rbd_spec has an associated name. For a
172 * user-mapped image, the names are supplied and the id's associated
173 * with them are looked up. For a layered image, a parent image is
174 * defined by the tuple, and the names are looked up.
176 * An rbd_dev structure contains a parent_spec pointer which is
177 * non-null if the image it represents is a child in a layered
178 * image. This pointer will refer to the rbd_spec structure used
179 * by the parent rbd_dev for its own identity (i.e., the structure
180 * is shared between the parent and child).
182 * Since these structures are populated once, during the discovery
183 * phase of image construction, they are effectively immutable so
184 * we make no effort to synchronize access to them.
186 * Note that code herein does not assume the image name is known (it
187 * could be a null pointer).
191 const char *pool_name
;
193 const char *image_id
;
194 const char *image_name
;
197 const char *snap_name
;
203 * an instance of the client. multiple devices may share an rbd client.
206 struct ceph_client
*client
;
208 struct list_head node
;
211 struct rbd_img_request
;
212 typedef void (*rbd_img_callback_t
)(struct rbd_img_request
*);
214 #define BAD_WHICH U32_MAX /* Good which or bad which, which? */
216 struct rbd_obj_request
;
217 typedef void (*rbd_obj_callback_t
)(struct rbd_obj_request
*);
219 enum obj_request_type
{
220 OBJ_REQUEST_NODATA
, OBJ_REQUEST_BIO
, OBJ_REQUEST_PAGES
223 enum obj_operation_type
{
230 OBJ_REQ_DONE
, /* completion flag: not done = 0, done = 1 */
231 OBJ_REQ_IMG_DATA
, /* object usage: standalone = 0, image = 1 */
232 OBJ_REQ_KNOWN
, /* EXISTS flag valid: no = 0, yes = 1 */
233 OBJ_REQ_EXISTS
, /* target exists: no = 0, yes = 1 */
236 struct rbd_obj_request
{
238 u64 offset
; /* object start byte */
239 u64 length
; /* bytes from offset */
243 * An object request associated with an image will have its
244 * img_data flag set; a standalone object request will not.
246 * A standalone object request will have which == BAD_WHICH
247 * and a null obj_request pointer.
249 * An object request initiated in support of a layered image
250 * object (to check for its existence before a write) will
251 * have which == BAD_WHICH and a non-null obj_request pointer.
253 * Finally, an object request for rbd image data will have
254 * which != BAD_WHICH, and will have a non-null img_request
255 * pointer. The value of which will be in the range
256 * 0..(img_request->obj_request_count-1).
259 struct rbd_obj_request
*obj_request
; /* STAT op */
261 struct rbd_img_request
*img_request
;
263 /* links for img_request->obj_requests list */
264 struct list_head links
;
267 u32 which
; /* posn image request list */
269 enum obj_request_type type
;
271 struct bio
*bio_list
;
277 struct page
**copyup_pages
;
278 u32 copyup_page_count
;
280 struct ceph_osd_request
*osd_req
;
282 u64 xferred
; /* bytes transferred */
285 rbd_obj_callback_t callback
;
291 IMG_REQ_WRITE
, /* I/O direction: read = 0, write = 1 */
292 IMG_REQ_CHILD
, /* initiator: block = 0, child image = 1 */
293 IMG_REQ_LAYERED
, /* ENOENT handling: normal = 0, layered = 1 */
294 IMG_REQ_DISCARD
, /* discard: normal = 0, discard request = 1 */
297 struct rbd_img_request
{
298 struct rbd_device
*rbd_dev
;
299 u64 offset
; /* starting image byte offset */
300 u64 length
; /* byte count from offset */
303 u64 snap_id
; /* for reads */
304 struct ceph_snap_context
*snapc
; /* for writes */
307 struct request
*rq
; /* block request */
308 struct rbd_obj_request
*obj_request
; /* obj req initiator */
310 struct page
**copyup_pages
;
311 u32 copyup_page_count
;
312 spinlock_t completion_lock
;/* protects next_completion */
314 rbd_img_callback_t callback
;
315 u64 xferred
;/* aggregate bytes transferred */
316 int result
; /* first nonzero obj_request result */
318 u32 obj_request_count
;
319 struct list_head obj_requests
; /* rbd_obj_request structs */
324 #define for_each_obj_request(ireq, oreq) \
325 list_for_each_entry(oreq, &(ireq)->obj_requests, links)
326 #define for_each_obj_request_from(ireq, oreq) \
327 list_for_each_entry_from(oreq, &(ireq)->obj_requests, links)
328 #define for_each_obj_request_safe(ireq, oreq, n) \
329 list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links)
331 enum rbd_watch_state
{
332 RBD_WATCH_STATE_UNREGISTERED
,
333 RBD_WATCH_STATE_REGISTERED
,
334 RBD_WATCH_STATE_ERROR
,
337 enum rbd_lock_state
{
338 RBD_LOCK_STATE_UNLOCKED
,
339 RBD_LOCK_STATE_LOCKED
,
340 RBD_LOCK_STATE_RELEASING
,
343 /* WatchNotify::ClientId */
344 struct rbd_client_id
{
358 int dev_id
; /* blkdev unique id */
360 int major
; /* blkdev assigned major */
362 struct gendisk
*disk
; /* blkdev's gendisk and rq */
364 u32 image_format
; /* Either 1 or 2 */
365 struct rbd_client
*rbd_client
;
367 char name
[DEV_NAME_LEN
]; /* blkdev name, e.g. rbd3 */
369 spinlock_t lock
; /* queue, flags, open_count */
371 struct rbd_image_header header
;
372 unsigned long flags
; /* possibly lock protected */
373 struct rbd_spec
*spec
;
374 struct rbd_options
*opts
;
375 char *config_info
; /* add{,_single_major} string */
377 struct ceph_object_id header_oid
;
378 struct ceph_object_locator header_oloc
;
380 struct ceph_file_layout layout
; /* used for all rbd requests */
382 struct mutex watch_mutex
;
383 enum rbd_watch_state watch_state
;
384 struct ceph_osd_linger_request
*watch_handle
;
386 struct delayed_work watch_dwork
;
388 struct rw_semaphore lock_rwsem
;
389 enum rbd_lock_state lock_state
;
390 char lock_cookie
[32];
391 struct rbd_client_id owner_cid
;
392 struct work_struct acquired_lock_work
;
393 struct work_struct released_lock_work
;
394 struct delayed_work lock_dwork
;
395 struct work_struct unlock_work
;
396 wait_queue_head_t lock_waitq
;
398 struct workqueue_struct
*task_wq
;
400 struct rbd_spec
*parent_spec
;
403 struct rbd_device
*parent
;
405 /* Block layer tags. */
406 struct blk_mq_tag_set tag_set
;
408 /* protects updating the header */
409 struct rw_semaphore header_rwsem
;
411 struct rbd_mapping mapping
;
413 struct list_head node
;
417 unsigned long open_count
; /* protected by lock */
421 * Flag bits for rbd_dev->flags:
422 * - REMOVING (which is coupled with rbd_dev->open_count) is protected
424 * - BLACKLISTED is protected by rbd_dev->lock_rwsem
427 RBD_DEV_FLAG_EXISTS
, /* mapped snapshot has not been deleted */
428 RBD_DEV_FLAG_REMOVING
, /* this mapping is being removed */
429 RBD_DEV_FLAG_BLACKLISTED
, /* our ceph_client is blacklisted */
432 static DEFINE_MUTEX(client_mutex
); /* Serialize client creation */
434 static LIST_HEAD(rbd_dev_list
); /* devices */
435 static DEFINE_SPINLOCK(rbd_dev_list_lock
);
437 static LIST_HEAD(rbd_client_list
); /* clients */
438 static DEFINE_SPINLOCK(rbd_client_list_lock
);
440 /* Slab caches for frequently-allocated structures */
442 static struct kmem_cache
*rbd_img_request_cache
;
443 static struct kmem_cache
*rbd_obj_request_cache
;
445 static struct bio_set
*rbd_bio_clone
;
447 static int rbd_major
;
448 static DEFINE_IDA(rbd_dev_id_ida
);
450 static struct workqueue_struct
*rbd_wq
;
453 * single-major requires >= 0.75 version of userspace rbd utility.
455 static bool single_major
= true;
456 module_param(single_major
, bool, S_IRUGO
);
457 MODULE_PARM_DESC(single_major
, "Use a single major number for all rbd devices (default: true)");
459 static int rbd_img_request_submit(struct rbd_img_request
*img_request
);
461 static ssize_t
rbd_add(struct bus_type
*bus
, const char *buf
,
463 static ssize_t
rbd_remove(struct bus_type
*bus
, const char *buf
,
465 static ssize_t
rbd_add_single_major(struct bus_type
*bus
, const char *buf
,
467 static ssize_t
rbd_remove_single_major(struct bus_type
*bus
, const char *buf
,
469 static int rbd_dev_image_probe(struct rbd_device
*rbd_dev
, int depth
);
470 static void rbd_spec_put(struct rbd_spec
*spec
);
472 static int rbd_dev_id_to_minor(int dev_id
)
474 return dev_id
<< RBD_SINGLE_MAJOR_PART_SHIFT
;
477 static int minor_to_rbd_dev_id(int minor
)
479 return minor
>> RBD_SINGLE_MAJOR_PART_SHIFT
;
482 static bool __rbd_is_lock_owner(struct rbd_device
*rbd_dev
)
484 return rbd_dev
->lock_state
== RBD_LOCK_STATE_LOCKED
||
485 rbd_dev
->lock_state
== RBD_LOCK_STATE_RELEASING
;
488 static bool rbd_is_lock_owner(struct rbd_device
*rbd_dev
)
492 down_read(&rbd_dev
->lock_rwsem
);
493 is_lock_owner
= __rbd_is_lock_owner(rbd_dev
);
494 up_read(&rbd_dev
->lock_rwsem
);
495 return is_lock_owner
;
498 static ssize_t
rbd_supported_features_show(struct bus_type
*bus
, char *buf
)
500 return sprintf(buf
, "0x%llx\n", RBD_FEATURES_SUPPORTED
);
503 static BUS_ATTR(add
, S_IWUSR
, NULL
, rbd_add
);
504 static BUS_ATTR(remove
, S_IWUSR
, NULL
, rbd_remove
);
505 static BUS_ATTR(add_single_major
, S_IWUSR
, NULL
, rbd_add_single_major
);
506 static BUS_ATTR(remove_single_major
, S_IWUSR
, NULL
, rbd_remove_single_major
);
507 static BUS_ATTR(supported_features
, S_IRUGO
, rbd_supported_features_show
, NULL
);
509 static struct attribute
*rbd_bus_attrs
[] = {
511 &bus_attr_remove
.attr
,
512 &bus_attr_add_single_major
.attr
,
513 &bus_attr_remove_single_major
.attr
,
514 &bus_attr_supported_features
.attr
,
518 static umode_t
rbd_bus_is_visible(struct kobject
*kobj
,
519 struct attribute
*attr
, int index
)
522 (attr
== &bus_attr_add_single_major
.attr
||
523 attr
== &bus_attr_remove_single_major
.attr
))
529 static const struct attribute_group rbd_bus_group
= {
530 .attrs
= rbd_bus_attrs
,
531 .is_visible
= rbd_bus_is_visible
,
533 __ATTRIBUTE_GROUPS(rbd_bus
);
535 static struct bus_type rbd_bus_type
= {
537 .bus_groups
= rbd_bus_groups
,
540 static void rbd_root_dev_release(struct device
*dev
)
544 static struct device rbd_root_dev
= {
546 .release
= rbd_root_dev_release
,
549 static __printf(2, 3)
550 void rbd_warn(struct rbd_device
*rbd_dev
, const char *fmt
, ...)
552 struct va_format vaf
;
560 printk(KERN_WARNING
"%s: %pV\n", RBD_DRV_NAME
, &vaf
);
561 else if (rbd_dev
->disk
)
562 printk(KERN_WARNING
"%s: %s: %pV\n",
563 RBD_DRV_NAME
, rbd_dev
->disk
->disk_name
, &vaf
);
564 else if (rbd_dev
->spec
&& rbd_dev
->spec
->image_name
)
565 printk(KERN_WARNING
"%s: image %s: %pV\n",
566 RBD_DRV_NAME
, rbd_dev
->spec
->image_name
, &vaf
);
567 else if (rbd_dev
->spec
&& rbd_dev
->spec
->image_id
)
568 printk(KERN_WARNING
"%s: id %s: %pV\n",
569 RBD_DRV_NAME
, rbd_dev
->spec
->image_id
, &vaf
);
571 printk(KERN_WARNING
"%s: rbd_dev %p: %pV\n",
572 RBD_DRV_NAME
, rbd_dev
, &vaf
);
577 #define rbd_assert(expr) \
578 if (unlikely(!(expr))) { \
579 printk(KERN_ERR "\nAssertion failure in %s() " \
581 "\trbd_assert(%s);\n\n", \
582 __func__, __LINE__, #expr); \
585 #else /* !RBD_DEBUG */
586 # define rbd_assert(expr) ((void) 0)
587 #endif /* !RBD_DEBUG */
589 static void rbd_osd_copyup_callback(struct rbd_obj_request
*obj_request
);
590 static int rbd_img_obj_request_submit(struct rbd_obj_request
*obj_request
);
591 static void rbd_img_parent_read(struct rbd_obj_request
*obj_request
);
592 static void rbd_dev_remove_parent(struct rbd_device
*rbd_dev
);
594 static int rbd_dev_refresh(struct rbd_device
*rbd_dev
);
595 static int rbd_dev_v2_header_onetime(struct rbd_device
*rbd_dev
);
596 static int rbd_dev_header_info(struct rbd_device
*rbd_dev
);
597 static int rbd_dev_v2_parent_info(struct rbd_device
*rbd_dev
);
598 static const char *rbd_dev_v2_snap_name(struct rbd_device
*rbd_dev
,
600 static int _rbd_dev_v2_snap_size(struct rbd_device
*rbd_dev
, u64 snap_id
,
601 u8
*order
, u64
*snap_size
);
602 static int _rbd_dev_v2_snap_features(struct rbd_device
*rbd_dev
, u64 snap_id
,
605 static int rbd_open(struct block_device
*bdev
, fmode_t mode
)
607 struct rbd_device
*rbd_dev
= bdev
->bd_disk
->private_data
;
608 bool removing
= false;
610 spin_lock_irq(&rbd_dev
->lock
);
611 if (test_bit(RBD_DEV_FLAG_REMOVING
, &rbd_dev
->flags
))
614 rbd_dev
->open_count
++;
615 spin_unlock_irq(&rbd_dev
->lock
);
619 (void) get_device(&rbd_dev
->dev
);
624 static void rbd_release(struct gendisk
*disk
, fmode_t mode
)
626 struct rbd_device
*rbd_dev
= disk
->private_data
;
627 unsigned long open_count_before
;
629 spin_lock_irq(&rbd_dev
->lock
);
630 open_count_before
= rbd_dev
->open_count
--;
631 spin_unlock_irq(&rbd_dev
->lock
);
632 rbd_assert(open_count_before
> 0);
634 put_device(&rbd_dev
->dev
);
637 static int rbd_ioctl_set_ro(struct rbd_device
*rbd_dev
, unsigned long arg
)
641 if (get_user(ro
, (int __user
*)arg
))
644 /* Snapshots can't be marked read-write */
645 if (rbd_dev
->spec
->snap_id
!= CEPH_NOSNAP
&& !ro
)
648 /* Let blkdev_roset() handle it */
652 static int rbd_ioctl(struct block_device
*bdev
, fmode_t mode
,
653 unsigned int cmd
, unsigned long arg
)
655 struct rbd_device
*rbd_dev
= bdev
->bd_disk
->private_data
;
660 ret
= rbd_ioctl_set_ro(rbd_dev
, arg
);
670 static int rbd_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
671 unsigned int cmd
, unsigned long arg
)
673 return rbd_ioctl(bdev
, mode
, cmd
, arg
);
675 #endif /* CONFIG_COMPAT */
677 static const struct block_device_operations rbd_bd_ops
= {
678 .owner
= THIS_MODULE
,
680 .release
= rbd_release
,
683 .compat_ioctl
= rbd_compat_ioctl
,
688 * Initialize an rbd client instance. Success or not, this function
689 * consumes ceph_opts. Caller holds client_mutex.
691 static struct rbd_client
*rbd_client_create(struct ceph_options
*ceph_opts
)
693 struct rbd_client
*rbdc
;
696 dout("%s:\n", __func__
);
697 rbdc
= kmalloc(sizeof(struct rbd_client
), GFP_KERNEL
);
701 kref_init(&rbdc
->kref
);
702 INIT_LIST_HEAD(&rbdc
->node
);
704 rbdc
->client
= ceph_create_client(ceph_opts
, rbdc
);
705 if (IS_ERR(rbdc
->client
))
707 ceph_opts
= NULL
; /* Now rbdc->client is responsible for ceph_opts */
709 ret
= ceph_open_session(rbdc
->client
);
713 spin_lock(&rbd_client_list_lock
);
714 list_add_tail(&rbdc
->node
, &rbd_client_list
);
715 spin_unlock(&rbd_client_list_lock
);
717 dout("%s: rbdc %p\n", __func__
, rbdc
);
721 ceph_destroy_client(rbdc
->client
);
726 ceph_destroy_options(ceph_opts
);
727 dout("%s: error %d\n", __func__
, ret
);
732 static struct rbd_client
*__rbd_get_client(struct rbd_client
*rbdc
)
734 kref_get(&rbdc
->kref
);
740 * Find a ceph client with specific addr and configuration. If
741 * found, bump its reference count.
743 static struct rbd_client
*rbd_client_find(struct ceph_options
*ceph_opts
)
745 struct rbd_client
*client_node
;
748 if (ceph_opts
->flags
& CEPH_OPT_NOSHARE
)
751 spin_lock(&rbd_client_list_lock
);
752 list_for_each_entry(client_node
, &rbd_client_list
, node
) {
753 if (!ceph_compare_options(ceph_opts
, client_node
->client
)) {
754 __rbd_get_client(client_node
);
760 spin_unlock(&rbd_client_list_lock
);
762 return found
? client_node
: NULL
;
766 * (Per device) rbd map options
773 /* string args above */
781 static match_table_t rbd_opts_tokens
= {
782 {Opt_queue_depth
, "queue_depth=%d"},
784 /* string args above */
785 {Opt_read_only
, "read_only"},
786 {Opt_read_only
, "ro"}, /* Alternate spelling */
787 {Opt_read_write
, "read_write"},
788 {Opt_read_write
, "rw"}, /* Alternate spelling */
789 {Opt_lock_on_read
, "lock_on_read"},
790 {Opt_exclusive
, "exclusive"},
801 #define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ
802 #define RBD_READ_ONLY_DEFAULT false
803 #define RBD_LOCK_ON_READ_DEFAULT false
804 #define RBD_EXCLUSIVE_DEFAULT false
806 static int parse_rbd_opts_token(char *c
, void *private)
808 struct rbd_options
*rbd_opts
= private;
809 substring_t argstr
[MAX_OPT_ARGS
];
810 int token
, intval
, ret
;
812 token
= match_token(c
, rbd_opts_tokens
, argstr
);
813 if (token
< Opt_last_int
) {
814 ret
= match_int(&argstr
[0], &intval
);
816 pr_err("bad mount option arg (not int) at '%s'\n", c
);
819 dout("got int token %d val %d\n", token
, intval
);
820 } else if (token
> Opt_last_int
&& token
< Opt_last_string
) {
821 dout("got string token %d val %s\n", token
, argstr
[0].from
);
823 dout("got token %d\n", token
);
827 case Opt_queue_depth
:
829 pr_err("queue_depth out of range\n");
832 rbd_opts
->queue_depth
= intval
;
835 rbd_opts
->read_only
= true;
838 rbd_opts
->read_only
= false;
840 case Opt_lock_on_read
:
841 rbd_opts
->lock_on_read
= true;
844 rbd_opts
->exclusive
= true;
847 /* libceph prints "bad option" msg */
854 static char* obj_op_name(enum obj_operation_type op_type
)
869 * Get a ceph client with specific addr and configuration, if one does
870 * not exist create it. Either way, ceph_opts is consumed by this
873 static struct rbd_client
*rbd_get_client(struct ceph_options
*ceph_opts
)
875 struct rbd_client
*rbdc
;
877 mutex_lock_nested(&client_mutex
, SINGLE_DEPTH_NESTING
);
878 rbdc
= rbd_client_find(ceph_opts
);
879 if (rbdc
) /* using an existing client */
880 ceph_destroy_options(ceph_opts
);
882 rbdc
= rbd_client_create(ceph_opts
);
883 mutex_unlock(&client_mutex
);
889 * Destroy ceph client
891 * Caller must hold rbd_client_list_lock.
893 static void rbd_client_release(struct kref
*kref
)
895 struct rbd_client
*rbdc
= container_of(kref
, struct rbd_client
, kref
);
897 dout("%s: rbdc %p\n", __func__
, rbdc
);
898 spin_lock(&rbd_client_list_lock
);
899 list_del(&rbdc
->node
);
900 spin_unlock(&rbd_client_list_lock
);
902 ceph_destroy_client(rbdc
->client
);
907 * Drop reference to ceph client node. If it's not referenced anymore, release
910 static void rbd_put_client(struct rbd_client
*rbdc
)
913 kref_put(&rbdc
->kref
, rbd_client_release
);
916 static bool rbd_image_format_valid(u32 image_format
)
918 return image_format
== 1 || image_format
== 2;
921 static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk
*ondisk
)
926 /* The header has to start with the magic rbd header text */
927 if (memcmp(&ondisk
->text
, RBD_HEADER_TEXT
, sizeof (RBD_HEADER_TEXT
)))
930 /* The bio layer requires at least sector-sized I/O */
932 if (ondisk
->options
.order
< SECTOR_SHIFT
)
935 /* If we use u64 in a few spots we may be able to loosen this */
937 if (ondisk
->options
.order
> 8 * sizeof (int) - 1)
941 * The size of a snapshot header has to fit in a size_t, and
942 * that limits the number of snapshots.
944 snap_count
= le32_to_cpu(ondisk
->snap_count
);
945 size
= SIZE_MAX
- sizeof (struct ceph_snap_context
);
946 if (snap_count
> size
/ sizeof (__le64
))
950 * Not only that, but the size of the entire the snapshot
951 * header must also be representable in a size_t.
953 size
-= snap_count
* sizeof (__le64
);
954 if ((u64
) size
< le64_to_cpu(ondisk
->snap_names_len
))
961 * returns the size of an object in the image
963 static u32
rbd_obj_bytes(struct rbd_image_header
*header
)
965 return 1U << header
->obj_order
;
968 static void rbd_init_layout(struct rbd_device
*rbd_dev
)
970 if (rbd_dev
->header
.stripe_unit
== 0 ||
971 rbd_dev
->header
.stripe_count
== 0) {
972 rbd_dev
->header
.stripe_unit
= rbd_obj_bytes(&rbd_dev
->header
);
973 rbd_dev
->header
.stripe_count
= 1;
976 rbd_dev
->layout
.stripe_unit
= rbd_dev
->header
.stripe_unit
;
977 rbd_dev
->layout
.stripe_count
= rbd_dev
->header
.stripe_count
;
978 rbd_dev
->layout
.object_size
= rbd_obj_bytes(&rbd_dev
->header
);
979 rbd_dev
->layout
.pool_id
= rbd_dev
->header
.data_pool_id
== CEPH_NOPOOL
?
980 rbd_dev
->spec
->pool_id
: rbd_dev
->header
.data_pool_id
;
981 RCU_INIT_POINTER(rbd_dev
->layout
.pool_ns
, NULL
);
985 * Fill an rbd image header with information from the given format 1
988 static int rbd_header_from_disk(struct rbd_device
*rbd_dev
,
989 struct rbd_image_header_ondisk
*ondisk
)
991 struct rbd_image_header
*header
= &rbd_dev
->header
;
992 bool first_time
= header
->object_prefix
== NULL
;
993 struct ceph_snap_context
*snapc
;
994 char *object_prefix
= NULL
;
995 char *snap_names
= NULL
;
996 u64
*snap_sizes
= NULL
;
1001 /* Allocate this now to avoid having to handle failure below */
1004 object_prefix
= kstrndup(ondisk
->object_prefix
,
1005 sizeof(ondisk
->object_prefix
),
1011 /* Allocate the snapshot context and fill it in */
1013 snap_count
= le32_to_cpu(ondisk
->snap_count
);
1014 snapc
= ceph_create_snap_context(snap_count
, GFP_KERNEL
);
1017 snapc
->seq
= le64_to_cpu(ondisk
->snap_seq
);
1019 struct rbd_image_snap_ondisk
*snaps
;
1020 u64 snap_names_len
= le64_to_cpu(ondisk
->snap_names_len
);
1022 /* We'll keep a copy of the snapshot names... */
1024 if (snap_names_len
> (u64
)SIZE_MAX
)
1026 snap_names
= kmalloc(snap_names_len
, GFP_KERNEL
);
1030 /* ...as well as the array of their sizes. */
1031 snap_sizes
= kmalloc_array(snap_count
,
1032 sizeof(*header
->snap_sizes
),
1038 * Copy the names, and fill in each snapshot's id
1041 * Note that rbd_dev_v1_header_info() guarantees the
1042 * ondisk buffer we're working with has
1043 * snap_names_len bytes beyond the end of the
1044 * snapshot id array, this memcpy() is safe.
1046 memcpy(snap_names
, &ondisk
->snaps
[snap_count
], snap_names_len
);
1047 snaps
= ondisk
->snaps
;
1048 for (i
= 0; i
< snap_count
; i
++) {
1049 snapc
->snaps
[i
] = le64_to_cpu(snaps
[i
].id
);
1050 snap_sizes
[i
] = le64_to_cpu(snaps
[i
].image_size
);
1054 /* We won't fail any more, fill in the header */
1057 header
->object_prefix
= object_prefix
;
1058 header
->obj_order
= ondisk
->options
.order
;
1059 rbd_init_layout(rbd_dev
);
1061 ceph_put_snap_context(header
->snapc
);
1062 kfree(header
->snap_names
);
1063 kfree(header
->snap_sizes
);
1066 /* The remaining fields always get updated (when we refresh) */
1068 header
->image_size
= le64_to_cpu(ondisk
->image_size
);
1069 header
->snapc
= snapc
;
1070 header
->snap_names
= snap_names
;
1071 header
->snap_sizes
= snap_sizes
;
1079 ceph_put_snap_context(snapc
);
1080 kfree(object_prefix
);
1085 static const char *_rbd_dev_v1_snap_name(struct rbd_device
*rbd_dev
, u32 which
)
1087 const char *snap_name
;
1089 rbd_assert(which
< rbd_dev
->header
.snapc
->num_snaps
);
1091 /* Skip over names until we find the one we are looking for */
1093 snap_name
= rbd_dev
->header
.snap_names
;
1095 snap_name
+= strlen(snap_name
) + 1;
1097 return kstrdup(snap_name
, GFP_KERNEL
);
1101 * Snapshot id comparison function for use with qsort()/bsearch().
1102 * Note that result is for snapshots in *descending* order.
1104 static int snapid_compare_reverse(const void *s1
, const void *s2
)
1106 u64 snap_id1
= *(u64
*)s1
;
1107 u64 snap_id2
= *(u64
*)s2
;
1109 if (snap_id1
< snap_id2
)
1111 return snap_id1
== snap_id2
? 0 : -1;
1115 * Search a snapshot context to see if the given snapshot id is
1118 * Returns the position of the snapshot id in the array if it's found,
1119 * or BAD_SNAP_INDEX otherwise.
1121 * Note: The snapshot array is in kept sorted (by the osd) in
1122 * reverse order, highest snapshot id first.
1124 static u32
rbd_dev_snap_index(struct rbd_device
*rbd_dev
, u64 snap_id
)
1126 struct ceph_snap_context
*snapc
= rbd_dev
->header
.snapc
;
1129 found
= bsearch(&snap_id
, &snapc
->snaps
, snapc
->num_snaps
,
1130 sizeof (snap_id
), snapid_compare_reverse
);
1132 return found
? (u32
)(found
- &snapc
->snaps
[0]) : BAD_SNAP_INDEX
;
1135 static const char *rbd_dev_v1_snap_name(struct rbd_device
*rbd_dev
,
1139 const char *snap_name
;
1141 which
= rbd_dev_snap_index(rbd_dev
, snap_id
);
1142 if (which
== BAD_SNAP_INDEX
)
1143 return ERR_PTR(-ENOENT
);
1145 snap_name
= _rbd_dev_v1_snap_name(rbd_dev
, which
);
1146 return snap_name
? snap_name
: ERR_PTR(-ENOMEM
);
1149 static const char *rbd_snap_name(struct rbd_device
*rbd_dev
, u64 snap_id
)
1151 if (snap_id
== CEPH_NOSNAP
)
1152 return RBD_SNAP_HEAD_NAME
;
1154 rbd_assert(rbd_image_format_valid(rbd_dev
->image_format
));
1155 if (rbd_dev
->image_format
== 1)
1156 return rbd_dev_v1_snap_name(rbd_dev
, snap_id
);
1158 return rbd_dev_v2_snap_name(rbd_dev
, snap_id
);
1161 static int rbd_snap_size(struct rbd_device
*rbd_dev
, u64 snap_id
,
1164 rbd_assert(rbd_image_format_valid(rbd_dev
->image_format
));
1165 if (snap_id
== CEPH_NOSNAP
) {
1166 *snap_size
= rbd_dev
->header
.image_size
;
1167 } else if (rbd_dev
->image_format
== 1) {
1170 which
= rbd_dev_snap_index(rbd_dev
, snap_id
);
1171 if (which
== BAD_SNAP_INDEX
)
1174 *snap_size
= rbd_dev
->header
.snap_sizes
[which
];
1179 ret
= _rbd_dev_v2_snap_size(rbd_dev
, snap_id
, NULL
, &size
);
1188 static int rbd_snap_features(struct rbd_device
*rbd_dev
, u64 snap_id
,
1191 rbd_assert(rbd_image_format_valid(rbd_dev
->image_format
));
1192 if (snap_id
== CEPH_NOSNAP
) {
1193 *snap_features
= rbd_dev
->header
.features
;
1194 } else if (rbd_dev
->image_format
== 1) {
1195 *snap_features
= 0; /* No features for format 1 */
1200 ret
= _rbd_dev_v2_snap_features(rbd_dev
, snap_id
, &features
);
1204 *snap_features
= features
;
1209 static int rbd_dev_mapping_set(struct rbd_device
*rbd_dev
)
1211 u64 snap_id
= rbd_dev
->spec
->snap_id
;
1216 ret
= rbd_snap_size(rbd_dev
, snap_id
, &size
);
1219 ret
= rbd_snap_features(rbd_dev
, snap_id
, &features
);
1223 rbd_dev
->mapping
.size
= size
;
1224 rbd_dev
->mapping
.features
= features
;
1229 static void rbd_dev_mapping_clear(struct rbd_device
*rbd_dev
)
1231 rbd_dev
->mapping
.size
= 0;
1232 rbd_dev
->mapping
.features
= 0;
1235 static u64
rbd_segment_offset(struct rbd_device
*rbd_dev
, u64 offset
)
1237 u64 segment_size
= rbd_obj_bytes(&rbd_dev
->header
);
1239 return offset
& (segment_size
- 1);
1242 static u64
rbd_segment_length(struct rbd_device
*rbd_dev
,
1243 u64 offset
, u64 length
)
1245 u64 segment_size
= rbd_obj_bytes(&rbd_dev
->header
);
1247 offset
&= segment_size
- 1;
1249 rbd_assert(length
<= U64_MAX
- offset
);
1250 if (offset
+ length
> segment_size
)
1251 length
= segment_size
- offset
;
1260 static void bio_chain_put(struct bio
*chain
)
1266 chain
= chain
->bi_next
;
1272 * zeros a bio chain, starting at specific offset
1274 static void zero_bio_chain(struct bio
*chain
, int start_ofs
)
1277 struct bvec_iter iter
;
1278 unsigned long flags
;
1283 bio_for_each_segment(bv
, chain
, iter
) {
1284 if (pos
+ bv
.bv_len
> start_ofs
) {
1285 int remainder
= max(start_ofs
- pos
, 0);
1286 buf
= bvec_kmap_irq(&bv
, &flags
);
1287 memset(buf
+ remainder
, 0,
1288 bv
.bv_len
- remainder
);
1289 flush_dcache_page(bv
.bv_page
);
1290 bvec_kunmap_irq(buf
, &flags
);
1295 chain
= chain
->bi_next
;
1300 * similar to zero_bio_chain(), zeros data defined by a page array,
1301 * starting at the given byte offset from the start of the array and
1302 * continuing up to the given end offset. The pages array is
1303 * assumed to be big enough to hold all bytes up to the end.
1305 static void zero_pages(struct page
**pages
, u64 offset
, u64 end
)
1307 struct page
**page
= &pages
[offset
>> PAGE_SHIFT
];
1309 rbd_assert(end
> offset
);
1310 rbd_assert(end
- offset
<= (u64
)SIZE_MAX
);
1311 while (offset
< end
) {
1314 unsigned long flags
;
1317 page_offset
= offset
& ~PAGE_MASK
;
1318 length
= min_t(size_t, PAGE_SIZE
- page_offset
, end
- offset
);
1319 local_irq_save(flags
);
1320 kaddr
= kmap_atomic(*page
);
1321 memset(kaddr
+ page_offset
, 0, length
);
1322 flush_dcache_page(*page
);
1323 kunmap_atomic(kaddr
);
1324 local_irq_restore(flags
);
1332 * Clone a portion of a bio, starting at the given byte offset
1333 * and continuing for the number of bytes indicated.
1335 static struct bio
*bio_clone_range(struct bio
*bio_src
,
1336 unsigned int offset
,
1342 bio
= bio_clone_fast(bio_src
, gfpmask
, rbd_bio_clone
);
1344 return NULL
; /* ENOMEM */
1346 bio_advance(bio
, offset
);
1347 bio
->bi_iter
.bi_size
= len
;
1353 * Clone a portion of a bio chain, starting at the given byte offset
1354 * into the first bio in the source chain and continuing for the
1355 * number of bytes indicated. The result is another bio chain of
1356 * exactly the given length, or a null pointer on error.
1358 * The bio_src and offset parameters are both in-out. On entry they
1359 * refer to the first source bio and the offset into that bio where
1360 * the start of data to be cloned is located.
1362 * On return, bio_src is updated to refer to the bio in the source
1363 * chain that contains first un-cloned byte, and *offset will
1364 * contain the offset of that byte within that bio.
1366 static struct bio
*bio_chain_clone_range(struct bio
**bio_src
,
1367 unsigned int *offset
,
1371 struct bio
*bi
= *bio_src
;
1372 unsigned int off
= *offset
;
1373 struct bio
*chain
= NULL
;
1376 /* Build up a chain of clone bios up to the limit */
1378 if (!bi
|| off
>= bi
->bi_iter
.bi_size
|| !len
)
1379 return NULL
; /* Nothing to clone */
1383 unsigned int bi_size
;
1387 rbd_warn(NULL
, "bio_chain exhausted with %u left", len
);
1388 goto out_err
; /* EINVAL; ran out of bio's */
1390 bi_size
= min_t(unsigned int, bi
->bi_iter
.bi_size
- off
, len
);
1391 bio
= bio_clone_range(bi
, off
, bi_size
, gfpmask
);
1393 goto out_err
; /* ENOMEM */
1396 end
= &bio
->bi_next
;
1399 if (off
== bi
->bi_iter
.bi_size
) {
1410 bio_chain_put(chain
);
1416 * The default/initial value for all object request flags is 0. For
1417 * each flag, once its value is set to 1 it is never reset to 0
1420 static void obj_request_img_data_set(struct rbd_obj_request
*obj_request
)
1422 if (test_and_set_bit(OBJ_REQ_IMG_DATA
, &obj_request
->flags
)) {
1423 struct rbd_device
*rbd_dev
;
1425 rbd_dev
= obj_request
->img_request
->rbd_dev
;
1426 rbd_warn(rbd_dev
, "obj_request %p already marked img_data",
1431 static bool obj_request_img_data_test(struct rbd_obj_request
*obj_request
)
1434 return test_bit(OBJ_REQ_IMG_DATA
, &obj_request
->flags
) != 0;
1437 static void obj_request_done_set(struct rbd_obj_request
*obj_request
)
1439 if (test_and_set_bit(OBJ_REQ_DONE
, &obj_request
->flags
)) {
1440 struct rbd_device
*rbd_dev
= NULL
;
1442 if (obj_request_img_data_test(obj_request
))
1443 rbd_dev
= obj_request
->img_request
->rbd_dev
;
1444 rbd_warn(rbd_dev
, "obj_request %p already marked done",
1449 static bool obj_request_done_test(struct rbd_obj_request
*obj_request
)
1452 return test_bit(OBJ_REQ_DONE
, &obj_request
->flags
) != 0;
1456 * This sets the KNOWN flag after (possibly) setting the EXISTS
1457 * flag. The latter is set based on the "exists" value provided.
1459 * Note that for our purposes once an object exists it never goes
1460 * away again. It's possible that the response from two existence
1461 * checks are separated by the creation of the target object, and
1462 * the first ("doesn't exist") response arrives *after* the second
1463 * ("does exist"). In that case we ignore the second one.
1465 static void obj_request_existence_set(struct rbd_obj_request
*obj_request
,
1469 set_bit(OBJ_REQ_EXISTS
, &obj_request
->flags
);
1470 set_bit(OBJ_REQ_KNOWN
, &obj_request
->flags
);
1474 static bool obj_request_known_test(struct rbd_obj_request
*obj_request
)
1477 return test_bit(OBJ_REQ_KNOWN
, &obj_request
->flags
) != 0;
1480 static bool obj_request_exists_test(struct rbd_obj_request
*obj_request
)
1483 return test_bit(OBJ_REQ_EXISTS
, &obj_request
->flags
) != 0;
1486 static bool obj_request_overlaps_parent(struct rbd_obj_request
*obj_request
)
1488 struct rbd_device
*rbd_dev
= obj_request
->img_request
->rbd_dev
;
1490 return obj_request
->img_offset
<
1491 round_up(rbd_dev
->parent_overlap
, rbd_obj_bytes(&rbd_dev
->header
));
1494 static void rbd_obj_request_get(struct rbd_obj_request
*obj_request
)
1496 dout("%s: obj %p (was %d)\n", __func__
, obj_request
,
1497 kref_read(&obj_request
->kref
));
1498 kref_get(&obj_request
->kref
);
1501 static void rbd_obj_request_destroy(struct kref
*kref
);
1502 static void rbd_obj_request_put(struct rbd_obj_request
*obj_request
)
1504 rbd_assert(obj_request
!= NULL
);
1505 dout("%s: obj %p (was %d)\n", __func__
, obj_request
,
1506 kref_read(&obj_request
->kref
));
1507 kref_put(&obj_request
->kref
, rbd_obj_request_destroy
);
1510 static void rbd_img_request_get(struct rbd_img_request
*img_request
)
1512 dout("%s: img %p (was %d)\n", __func__
, img_request
,
1513 kref_read(&img_request
->kref
));
1514 kref_get(&img_request
->kref
);
1517 static bool img_request_child_test(struct rbd_img_request
*img_request
);
1518 static void rbd_parent_request_destroy(struct kref
*kref
);
1519 static void rbd_img_request_destroy(struct kref
*kref
);
1520 static void rbd_img_request_put(struct rbd_img_request
*img_request
)
1522 rbd_assert(img_request
!= NULL
);
1523 dout("%s: img %p (was %d)\n", __func__
, img_request
,
1524 kref_read(&img_request
->kref
));
1525 if (img_request_child_test(img_request
))
1526 kref_put(&img_request
->kref
, rbd_parent_request_destroy
);
1528 kref_put(&img_request
->kref
, rbd_img_request_destroy
);
1531 static inline void rbd_img_obj_request_add(struct rbd_img_request
*img_request
,
1532 struct rbd_obj_request
*obj_request
)
1534 rbd_assert(obj_request
->img_request
== NULL
);
1536 /* Image request now owns object's original reference */
1537 obj_request
->img_request
= img_request
;
1538 obj_request
->which
= img_request
->obj_request_count
;
1539 rbd_assert(!obj_request_img_data_test(obj_request
));
1540 obj_request_img_data_set(obj_request
);
1541 rbd_assert(obj_request
->which
!= BAD_WHICH
);
1542 img_request
->obj_request_count
++;
1543 list_add_tail(&obj_request
->links
, &img_request
->obj_requests
);
1544 dout("%s: img %p obj %p w=%u\n", __func__
, img_request
, obj_request
,
1545 obj_request
->which
);
1548 static inline void rbd_img_obj_request_del(struct rbd_img_request
*img_request
,
1549 struct rbd_obj_request
*obj_request
)
1551 rbd_assert(obj_request
->which
!= BAD_WHICH
);
1553 dout("%s: img %p obj %p w=%u\n", __func__
, img_request
, obj_request
,
1554 obj_request
->which
);
1555 list_del(&obj_request
->links
);
1556 rbd_assert(img_request
->obj_request_count
> 0);
1557 img_request
->obj_request_count
--;
1558 rbd_assert(obj_request
->which
== img_request
->obj_request_count
);
1559 obj_request
->which
= BAD_WHICH
;
1560 rbd_assert(obj_request_img_data_test(obj_request
));
1561 rbd_assert(obj_request
->img_request
== img_request
);
1562 obj_request
->img_request
= NULL
;
1563 obj_request
->callback
= NULL
;
1564 rbd_obj_request_put(obj_request
);
1567 static bool obj_request_type_valid(enum obj_request_type type
)
1570 case OBJ_REQUEST_NODATA
:
1571 case OBJ_REQUEST_BIO
:
1572 case OBJ_REQUEST_PAGES
:
1579 static void rbd_img_obj_callback(struct rbd_obj_request
*obj_request
);
1581 static void rbd_obj_request_submit(struct rbd_obj_request
*obj_request
)
1583 struct ceph_osd_request
*osd_req
= obj_request
->osd_req
;
1585 dout("%s %p object_no %016llx %llu~%llu osd_req %p\n", __func__
,
1586 obj_request
, obj_request
->object_no
, obj_request
->offset
,
1587 obj_request
->length
, osd_req
);
1588 if (obj_request_img_data_test(obj_request
)) {
1589 WARN_ON(obj_request
->callback
!= rbd_img_obj_callback
);
1590 rbd_img_request_get(obj_request
->img_request
);
1592 ceph_osdc_start_request(osd_req
->r_osdc
, osd_req
, false);
1595 static void rbd_img_request_complete(struct rbd_img_request
*img_request
)
1598 dout("%s: img %p\n", __func__
, img_request
);
1601 * If no error occurred, compute the aggregate transfer
1602 * count for the image request. We could instead use
1603 * atomic64_cmpxchg() to update it as each object request
1604 * completes; not clear which way is better off hand.
1606 if (!img_request
->result
) {
1607 struct rbd_obj_request
*obj_request
;
1610 for_each_obj_request(img_request
, obj_request
)
1611 xferred
+= obj_request
->xferred
;
1612 img_request
->xferred
= xferred
;
1615 if (img_request
->callback
)
1616 img_request
->callback(img_request
);
1618 rbd_img_request_put(img_request
);
1622 * The default/initial value for all image request flags is 0. Each
1623 * is conditionally set to 1 at image request initialization time
1624 * and currently never change thereafter.
1626 static void img_request_write_set(struct rbd_img_request
*img_request
)
1628 set_bit(IMG_REQ_WRITE
, &img_request
->flags
);
1632 static bool img_request_write_test(struct rbd_img_request
*img_request
)
1635 return test_bit(IMG_REQ_WRITE
, &img_request
->flags
) != 0;
1639 * Set the discard flag when the img_request is an discard request
1641 static void img_request_discard_set(struct rbd_img_request
*img_request
)
1643 set_bit(IMG_REQ_DISCARD
, &img_request
->flags
);
1647 static bool img_request_discard_test(struct rbd_img_request
*img_request
)
1650 return test_bit(IMG_REQ_DISCARD
, &img_request
->flags
) != 0;
1653 static void img_request_child_set(struct rbd_img_request
*img_request
)
1655 set_bit(IMG_REQ_CHILD
, &img_request
->flags
);
1659 static void img_request_child_clear(struct rbd_img_request
*img_request
)
1661 clear_bit(IMG_REQ_CHILD
, &img_request
->flags
);
1665 static bool img_request_child_test(struct rbd_img_request
*img_request
)
1668 return test_bit(IMG_REQ_CHILD
, &img_request
->flags
) != 0;
1671 static void img_request_layered_set(struct rbd_img_request
*img_request
)
1673 set_bit(IMG_REQ_LAYERED
, &img_request
->flags
);
1677 static void img_request_layered_clear(struct rbd_img_request
*img_request
)
1679 clear_bit(IMG_REQ_LAYERED
, &img_request
->flags
);
1683 static bool img_request_layered_test(struct rbd_img_request
*img_request
)
1686 return test_bit(IMG_REQ_LAYERED
, &img_request
->flags
) != 0;
1689 static enum obj_operation_type
1690 rbd_img_request_op_type(struct rbd_img_request
*img_request
)
1692 if (img_request_write_test(img_request
))
1693 return OBJ_OP_WRITE
;
1694 else if (img_request_discard_test(img_request
))
1695 return OBJ_OP_DISCARD
;
1701 rbd_img_obj_request_read_callback(struct rbd_obj_request
*obj_request
)
1703 u64 xferred
= obj_request
->xferred
;
1704 u64 length
= obj_request
->length
;
1706 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__
,
1707 obj_request
, obj_request
->img_request
, obj_request
->result
,
1710 * ENOENT means a hole in the image. We zero-fill the entire
1711 * length of the request. A short read also implies zero-fill
1712 * to the end of the request. An error requires the whole
1713 * length of the request to be reported finished with an error
1714 * to the block layer. In each case we update the xferred
1715 * count to indicate the whole request was satisfied.
1717 rbd_assert(obj_request
->type
!= OBJ_REQUEST_NODATA
);
1718 if (obj_request
->result
== -ENOENT
) {
1719 if (obj_request
->type
== OBJ_REQUEST_BIO
)
1720 zero_bio_chain(obj_request
->bio_list
, 0);
1722 zero_pages(obj_request
->pages
, 0, length
);
1723 obj_request
->result
= 0;
1724 } else if (xferred
< length
&& !obj_request
->result
) {
1725 if (obj_request
->type
== OBJ_REQUEST_BIO
)
1726 zero_bio_chain(obj_request
->bio_list
, xferred
);
1728 zero_pages(obj_request
->pages
, xferred
, length
);
1730 obj_request
->xferred
= length
;
1731 obj_request_done_set(obj_request
);
1734 static void rbd_obj_request_complete(struct rbd_obj_request
*obj_request
)
1736 dout("%s: obj %p cb %p\n", __func__
, obj_request
,
1737 obj_request
->callback
);
1738 obj_request
->callback(obj_request
);
1741 static void rbd_obj_request_error(struct rbd_obj_request
*obj_request
, int err
)
1743 obj_request
->result
= err
;
1744 obj_request
->xferred
= 0;
1746 * kludge - mirror rbd_obj_request_submit() to match a put in
1747 * rbd_img_obj_callback()
1749 if (obj_request_img_data_test(obj_request
)) {
1750 WARN_ON(obj_request
->callback
!= rbd_img_obj_callback
);
1751 rbd_img_request_get(obj_request
->img_request
);
1753 obj_request_done_set(obj_request
);
1754 rbd_obj_request_complete(obj_request
);
1757 static void rbd_osd_read_callback(struct rbd_obj_request
*obj_request
)
1759 struct rbd_img_request
*img_request
= NULL
;
1760 struct rbd_device
*rbd_dev
= NULL
;
1761 bool layered
= false;
1763 if (obj_request_img_data_test(obj_request
)) {
1764 img_request
= obj_request
->img_request
;
1765 layered
= img_request
&& img_request_layered_test(img_request
);
1766 rbd_dev
= img_request
->rbd_dev
;
1769 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__
,
1770 obj_request
, img_request
, obj_request
->result
,
1771 obj_request
->xferred
, obj_request
->length
);
1772 if (layered
&& obj_request
->result
== -ENOENT
&&
1773 obj_request
->img_offset
< rbd_dev
->parent_overlap
)
1774 rbd_img_parent_read(obj_request
);
1775 else if (img_request
)
1776 rbd_img_obj_request_read_callback(obj_request
);
1778 obj_request_done_set(obj_request
);
1781 static void rbd_osd_write_callback(struct rbd_obj_request
*obj_request
)
1783 dout("%s: obj %p result %d %llu\n", __func__
, obj_request
,
1784 obj_request
->result
, obj_request
->length
);
1786 * There is no such thing as a successful short write. Set
1787 * it to our originally-requested length.
1789 obj_request
->xferred
= obj_request
->length
;
1790 obj_request_done_set(obj_request
);
1793 static void rbd_osd_discard_callback(struct rbd_obj_request
*obj_request
)
1795 dout("%s: obj %p result %d %llu\n", __func__
, obj_request
,
1796 obj_request
->result
, obj_request
->length
);
1798 * There is no such thing as a successful short discard. Set
1799 * it to our originally-requested length.
1801 obj_request
->xferred
= obj_request
->length
;
1802 /* discarding a non-existent object is not a problem */
1803 if (obj_request
->result
== -ENOENT
)
1804 obj_request
->result
= 0;
1805 obj_request_done_set(obj_request
);
1809 * For a simple stat call there's nothing to do. We'll do more if
1810 * this is part of a write sequence for a layered image.
1812 static void rbd_osd_stat_callback(struct rbd_obj_request
*obj_request
)
1814 dout("%s: obj %p\n", __func__
, obj_request
);
1815 obj_request_done_set(obj_request
);
1818 static void rbd_osd_call_callback(struct rbd_obj_request
*obj_request
)
1820 dout("%s: obj %p\n", __func__
, obj_request
);
1822 if (obj_request_img_data_test(obj_request
))
1823 rbd_osd_copyup_callback(obj_request
);
1825 obj_request_done_set(obj_request
);
1828 static void rbd_osd_req_callback(struct ceph_osd_request
*osd_req
)
1830 struct rbd_obj_request
*obj_request
= osd_req
->r_priv
;
1833 dout("%s: osd_req %p\n", __func__
, osd_req
);
1834 rbd_assert(osd_req
== obj_request
->osd_req
);
1835 if (obj_request_img_data_test(obj_request
)) {
1836 rbd_assert(obj_request
->img_request
);
1837 rbd_assert(obj_request
->which
!= BAD_WHICH
);
1839 rbd_assert(obj_request
->which
== BAD_WHICH
);
1842 if (osd_req
->r_result
< 0)
1843 obj_request
->result
= osd_req
->r_result
;
1846 * We support a 64-bit length, but ultimately it has to be
1847 * passed to the block layer, which just supports a 32-bit
1850 obj_request
->xferred
= osd_req
->r_ops
[0].outdata_len
;
1851 rbd_assert(obj_request
->xferred
< (u64
)UINT_MAX
);
1853 opcode
= osd_req
->r_ops
[0].op
;
1855 case CEPH_OSD_OP_READ
:
1856 rbd_osd_read_callback(obj_request
);
1858 case CEPH_OSD_OP_SETALLOCHINT
:
1859 rbd_assert(osd_req
->r_ops
[1].op
== CEPH_OSD_OP_WRITE
||
1860 osd_req
->r_ops
[1].op
== CEPH_OSD_OP_WRITEFULL
);
1862 case CEPH_OSD_OP_WRITE
:
1863 case CEPH_OSD_OP_WRITEFULL
:
1864 rbd_osd_write_callback(obj_request
);
1866 case CEPH_OSD_OP_STAT
:
1867 rbd_osd_stat_callback(obj_request
);
1869 case CEPH_OSD_OP_DELETE
:
1870 case CEPH_OSD_OP_TRUNCATE
:
1871 case CEPH_OSD_OP_ZERO
:
1872 rbd_osd_discard_callback(obj_request
);
1874 case CEPH_OSD_OP_CALL
:
1875 rbd_osd_call_callback(obj_request
);
1878 rbd_warn(NULL
, "unexpected OSD op: object_no %016llx opcode %d",
1879 obj_request
->object_no
, opcode
);
1883 if (obj_request_done_test(obj_request
))
1884 rbd_obj_request_complete(obj_request
);
1887 static void rbd_osd_req_format_read(struct rbd_obj_request
*obj_request
)
1889 struct ceph_osd_request
*osd_req
= obj_request
->osd_req
;
1891 rbd_assert(obj_request_img_data_test(obj_request
));
1892 osd_req
->r_snapid
= obj_request
->img_request
->snap_id
;
1895 static void rbd_osd_req_format_write(struct rbd_obj_request
*obj_request
)
1897 struct ceph_osd_request
*osd_req
= obj_request
->osd_req
;
1899 ktime_get_real_ts(&osd_req
->r_mtime
);
1900 osd_req
->r_data_offset
= obj_request
->offset
;
1903 static struct ceph_osd_request
*
1904 __rbd_osd_req_create(struct rbd_device
*rbd_dev
,
1905 struct ceph_snap_context
*snapc
,
1906 int num_ops
, unsigned int flags
,
1907 struct rbd_obj_request
*obj_request
)
1909 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
1910 struct ceph_osd_request
*req
;
1911 const char *name_format
= rbd_dev
->image_format
== 1 ?
1912 RBD_V1_DATA_FORMAT
: RBD_V2_DATA_FORMAT
;
1914 req
= ceph_osdc_alloc_request(osdc
, snapc
, num_ops
, false, GFP_NOIO
);
1918 req
->r_flags
= flags
;
1919 req
->r_callback
= rbd_osd_req_callback
;
1920 req
->r_priv
= obj_request
;
1922 req
->r_base_oloc
.pool
= rbd_dev
->layout
.pool_id
;
1923 if (ceph_oid_aprintf(&req
->r_base_oid
, GFP_NOIO
, name_format
,
1924 rbd_dev
->header
.object_prefix
, obj_request
->object_no
))
1927 if (ceph_osdc_alloc_messages(req
, GFP_NOIO
))
1933 ceph_osdc_put_request(req
);
1938 * Create an osd request. A read request has one osd op (read).
1939 * A write request has either one (watch) or two (hint+write) osd ops.
1940 * (All rbd data writes are prefixed with an allocation hint op, but
1941 * technically osd watch is a write request, hence this distinction.)
1943 static struct ceph_osd_request
*rbd_osd_req_create(
1944 struct rbd_device
*rbd_dev
,
1945 enum obj_operation_type op_type
,
1946 unsigned int num_ops
,
1947 struct rbd_obj_request
*obj_request
)
1949 struct ceph_snap_context
*snapc
= NULL
;
1951 if (obj_request_img_data_test(obj_request
) &&
1952 (op_type
== OBJ_OP_DISCARD
|| op_type
== OBJ_OP_WRITE
)) {
1953 struct rbd_img_request
*img_request
= obj_request
->img_request
;
1954 if (op_type
== OBJ_OP_WRITE
) {
1955 rbd_assert(img_request_write_test(img_request
));
1957 rbd_assert(img_request_discard_test(img_request
));
1959 snapc
= img_request
->snapc
;
1962 rbd_assert(num_ops
== 1 || ((op_type
== OBJ_OP_WRITE
) && num_ops
== 2));
1964 return __rbd_osd_req_create(rbd_dev
, snapc
, num_ops
,
1965 (op_type
== OBJ_OP_WRITE
|| op_type
== OBJ_OP_DISCARD
) ?
1966 CEPH_OSD_FLAG_WRITE
: CEPH_OSD_FLAG_READ
, obj_request
);
1970 * Create a copyup osd request based on the information in the object
1971 * request supplied. A copyup request has two or three osd ops, a
1972 * copyup method call, potentially a hint op, and a write or truncate
1975 static struct ceph_osd_request
*
1976 rbd_osd_req_create_copyup(struct rbd_obj_request
*obj_request
)
1978 struct rbd_img_request
*img_request
;
1979 int num_osd_ops
= 3;
1981 rbd_assert(obj_request_img_data_test(obj_request
));
1982 img_request
= obj_request
->img_request
;
1983 rbd_assert(img_request
);
1984 rbd_assert(img_request_write_test(img_request
) ||
1985 img_request_discard_test(img_request
));
1987 if (img_request_discard_test(img_request
))
1990 return __rbd_osd_req_create(img_request
->rbd_dev
,
1991 img_request
->snapc
, num_osd_ops
,
1992 CEPH_OSD_FLAG_WRITE
, obj_request
);
1995 static void rbd_osd_req_destroy(struct ceph_osd_request
*osd_req
)
1997 ceph_osdc_put_request(osd_req
);
2000 static struct rbd_obj_request
*
2001 rbd_obj_request_create(enum obj_request_type type
)
2003 struct rbd_obj_request
*obj_request
;
2005 rbd_assert(obj_request_type_valid(type
));
2007 obj_request
= kmem_cache_zalloc(rbd_obj_request_cache
, GFP_NOIO
);
2011 obj_request
->which
= BAD_WHICH
;
2012 obj_request
->type
= type
;
2013 INIT_LIST_HEAD(&obj_request
->links
);
2014 kref_init(&obj_request
->kref
);
2016 dout("%s %p\n", __func__
, obj_request
);
2020 static void rbd_obj_request_destroy(struct kref
*kref
)
2022 struct rbd_obj_request
*obj_request
;
2024 obj_request
= container_of(kref
, struct rbd_obj_request
, kref
);
2026 dout("%s: obj %p\n", __func__
, obj_request
);
2028 rbd_assert(obj_request
->img_request
== NULL
);
2029 rbd_assert(obj_request
->which
== BAD_WHICH
);
2031 if (obj_request
->osd_req
)
2032 rbd_osd_req_destroy(obj_request
->osd_req
);
2034 rbd_assert(obj_request_type_valid(obj_request
->type
));
2035 switch (obj_request
->type
) {
2036 case OBJ_REQUEST_NODATA
:
2037 break; /* Nothing to do */
2038 case OBJ_REQUEST_BIO
:
2039 if (obj_request
->bio_list
)
2040 bio_chain_put(obj_request
->bio_list
);
2042 case OBJ_REQUEST_PAGES
:
2043 /* img_data requests don't own their page array */
2044 if (obj_request
->pages
&&
2045 !obj_request_img_data_test(obj_request
))
2046 ceph_release_page_vector(obj_request
->pages
,
2047 obj_request
->page_count
);
2051 kmem_cache_free(rbd_obj_request_cache
, obj_request
);
2054 /* It's OK to call this for a device with no parent */
2056 static void rbd_spec_put(struct rbd_spec
*spec
);
2057 static void rbd_dev_unparent(struct rbd_device
*rbd_dev
)
2059 rbd_dev_remove_parent(rbd_dev
);
2060 rbd_spec_put(rbd_dev
->parent_spec
);
2061 rbd_dev
->parent_spec
= NULL
;
2062 rbd_dev
->parent_overlap
= 0;
2066 * Parent image reference counting is used to determine when an
2067 * image's parent fields can be safely torn down--after there are no
2068 * more in-flight requests to the parent image. When the last
2069 * reference is dropped, cleaning them up is safe.
2071 static void rbd_dev_parent_put(struct rbd_device
*rbd_dev
)
2075 if (!rbd_dev
->parent_spec
)
2078 counter
= atomic_dec_return_safe(&rbd_dev
->parent_ref
);
2082 /* Last reference; clean up parent data structures */
2085 rbd_dev_unparent(rbd_dev
);
2087 rbd_warn(rbd_dev
, "parent reference underflow");
2091 * If an image has a non-zero parent overlap, get a reference to its
2094 * Returns true if the rbd device has a parent with a non-zero
2095 * overlap and a reference for it was successfully taken, or
2098 static bool rbd_dev_parent_get(struct rbd_device
*rbd_dev
)
2102 if (!rbd_dev
->parent_spec
)
2105 down_read(&rbd_dev
->header_rwsem
);
2106 if (rbd_dev
->parent_overlap
)
2107 counter
= atomic_inc_return_safe(&rbd_dev
->parent_ref
);
2108 up_read(&rbd_dev
->header_rwsem
);
2111 rbd_warn(rbd_dev
, "parent reference overflow");
2117 * Caller is responsible for filling in the list of object requests
2118 * that comprises the image request, and the Linux request pointer
2119 * (if there is one).
2121 static struct rbd_img_request
*rbd_img_request_create(
2122 struct rbd_device
*rbd_dev
,
2123 u64 offset
, u64 length
,
2124 enum obj_operation_type op_type
,
2125 struct ceph_snap_context
*snapc
)
2127 struct rbd_img_request
*img_request
;
2129 img_request
= kmem_cache_zalloc(rbd_img_request_cache
, GFP_NOIO
);
2133 img_request
->rbd_dev
= rbd_dev
;
2134 img_request
->offset
= offset
;
2135 img_request
->length
= length
;
2136 if (op_type
== OBJ_OP_DISCARD
) {
2137 img_request_discard_set(img_request
);
2138 img_request
->snapc
= snapc
;
2139 } else if (op_type
== OBJ_OP_WRITE
) {
2140 img_request_write_set(img_request
);
2141 img_request
->snapc
= snapc
;
2143 img_request
->snap_id
= rbd_dev
->spec
->snap_id
;
2145 if (rbd_dev_parent_get(rbd_dev
))
2146 img_request_layered_set(img_request
);
2148 spin_lock_init(&img_request
->completion_lock
);
2149 INIT_LIST_HEAD(&img_request
->obj_requests
);
2150 kref_init(&img_request
->kref
);
2152 dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__
, rbd_dev
,
2153 obj_op_name(op_type
), offset
, length
, img_request
);
2158 static void rbd_img_request_destroy(struct kref
*kref
)
2160 struct rbd_img_request
*img_request
;
2161 struct rbd_obj_request
*obj_request
;
2162 struct rbd_obj_request
*next_obj_request
;
2164 img_request
= container_of(kref
, struct rbd_img_request
, kref
);
2166 dout("%s: img %p\n", __func__
, img_request
);
2168 for_each_obj_request_safe(img_request
, obj_request
, next_obj_request
)
2169 rbd_img_obj_request_del(img_request
, obj_request
);
2170 rbd_assert(img_request
->obj_request_count
== 0);
2172 if (img_request_layered_test(img_request
)) {
2173 img_request_layered_clear(img_request
);
2174 rbd_dev_parent_put(img_request
->rbd_dev
);
2177 if (img_request_write_test(img_request
) ||
2178 img_request_discard_test(img_request
))
2179 ceph_put_snap_context(img_request
->snapc
);
2181 kmem_cache_free(rbd_img_request_cache
, img_request
);
2184 static struct rbd_img_request
*rbd_parent_request_create(
2185 struct rbd_obj_request
*obj_request
,
2186 u64 img_offset
, u64 length
)
2188 struct rbd_img_request
*parent_request
;
2189 struct rbd_device
*rbd_dev
;
2191 rbd_assert(obj_request
->img_request
);
2192 rbd_dev
= obj_request
->img_request
->rbd_dev
;
2194 parent_request
= rbd_img_request_create(rbd_dev
->parent
, img_offset
,
2195 length
, OBJ_OP_READ
, NULL
);
2196 if (!parent_request
)
2199 img_request_child_set(parent_request
);
2200 rbd_obj_request_get(obj_request
);
2201 parent_request
->obj_request
= obj_request
;
2203 return parent_request
;
2206 static void rbd_parent_request_destroy(struct kref
*kref
)
2208 struct rbd_img_request
*parent_request
;
2209 struct rbd_obj_request
*orig_request
;
2211 parent_request
= container_of(kref
, struct rbd_img_request
, kref
);
2212 orig_request
= parent_request
->obj_request
;
2214 parent_request
->obj_request
= NULL
;
2215 rbd_obj_request_put(orig_request
);
2216 img_request_child_clear(parent_request
);
2218 rbd_img_request_destroy(kref
);
2221 static bool rbd_img_obj_end_request(struct rbd_obj_request
*obj_request
)
2223 struct rbd_img_request
*img_request
;
2224 unsigned int xferred
;
2228 rbd_assert(obj_request_img_data_test(obj_request
));
2229 img_request
= obj_request
->img_request
;
2231 rbd_assert(obj_request
->xferred
<= (u64
)UINT_MAX
);
2232 xferred
= (unsigned int)obj_request
->xferred
;
2233 result
= obj_request
->result
;
2235 struct rbd_device
*rbd_dev
= img_request
->rbd_dev
;
2236 enum obj_operation_type op_type
;
2238 if (img_request_discard_test(img_request
))
2239 op_type
= OBJ_OP_DISCARD
;
2240 else if (img_request_write_test(img_request
))
2241 op_type
= OBJ_OP_WRITE
;
2243 op_type
= OBJ_OP_READ
;
2245 rbd_warn(rbd_dev
, "%s %llx at %llx (%llx)",
2246 obj_op_name(op_type
), obj_request
->length
,
2247 obj_request
->img_offset
, obj_request
->offset
);
2248 rbd_warn(rbd_dev
, " result %d xferred %x",
2250 if (!img_request
->result
)
2251 img_request
->result
= result
;
2253 * Need to end I/O on the entire obj_request worth of
2254 * bytes in case of error.
2256 xferred
= obj_request
->length
;
2259 if (img_request_child_test(img_request
)) {
2260 rbd_assert(img_request
->obj_request
!= NULL
);
2261 more
= obj_request
->which
< img_request
->obj_request_count
- 1;
2263 blk_status_t status
= errno_to_blk_status(result
);
2265 rbd_assert(img_request
->rq
!= NULL
);
2267 more
= blk_update_request(img_request
->rq
, status
, xferred
);
2269 __blk_mq_end_request(img_request
->rq
, status
);
2275 static void rbd_img_obj_callback(struct rbd_obj_request
*obj_request
)
2277 struct rbd_img_request
*img_request
;
2278 u32 which
= obj_request
->which
;
2281 rbd_assert(obj_request_img_data_test(obj_request
));
2282 img_request
= obj_request
->img_request
;
2284 dout("%s: img %p obj %p\n", __func__
, img_request
, obj_request
);
2285 rbd_assert(img_request
!= NULL
);
2286 rbd_assert(img_request
->obj_request_count
> 0);
2287 rbd_assert(which
!= BAD_WHICH
);
2288 rbd_assert(which
< img_request
->obj_request_count
);
2290 spin_lock_irq(&img_request
->completion_lock
);
2291 if (which
!= img_request
->next_completion
)
2294 for_each_obj_request_from(img_request
, obj_request
) {
2296 rbd_assert(which
< img_request
->obj_request_count
);
2298 if (!obj_request_done_test(obj_request
))
2300 more
= rbd_img_obj_end_request(obj_request
);
2304 rbd_assert(more
^ (which
== img_request
->obj_request_count
));
2305 img_request
->next_completion
= which
;
2307 spin_unlock_irq(&img_request
->completion_lock
);
2308 rbd_img_request_put(img_request
);
2311 rbd_img_request_complete(img_request
);
2315 * Add individual osd ops to the given ceph_osd_request and prepare
2316 * them for submission. num_ops is the current number of
2317 * osd operations already to the object request.
2319 static void rbd_img_obj_request_fill(struct rbd_obj_request
*obj_request
,
2320 struct ceph_osd_request
*osd_request
,
2321 enum obj_operation_type op_type
,
2322 unsigned int num_ops
)
2324 struct rbd_img_request
*img_request
= obj_request
->img_request
;
2325 struct rbd_device
*rbd_dev
= img_request
->rbd_dev
;
2326 u64 object_size
= rbd_obj_bytes(&rbd_dev
->header
);
2327 u64 offset
= obj_request
->offset
;
2328 u64 length
= obj_request
->length
;
2332 if (op_type
== OBJ_OP_DISCARD
) {
2333 if (!offset
&& length
== object_size
&&
2334 (!img_request_layered_test(img_request
) ||
2335 !obj_request_overlaps_parent(obj_request
))) {
2336 opcode
= CEPH_OSD_OP_DELETE
;
2337 } else if ((offset
+ length
== object_size
)) {
2338 opcode
= CEPH_OSD_OP_TRUNCATE
;
2340 down_read(&rbd_dev
->header_rwsem
);
2341 img_end
= rbd_dev
->header
.image_size
;
2342 up_read(&rbd_dev
->header_rwsem
);
2344 if (obj_request
->img_offset
+ length
== img_end
)
2345 opcode
= CEPH_OSD_OP_TRUNCATE
;
2347 opcode
= CEPH_OSD_OP_ZERO
;
2349 } else if (op_type
== OBJ_OP_WRITE
) {
2350 if (!offset
&& length
== object_size
)
2351 opcode
= CEPH_OSD_OP_WRITEFULL
;
2353 opcode
= CEPH_OSD_OP_WRITE
;
2354 osd_req_op_alloc_hint_init(osd_request
, num_ops
,
2355 object_size
, object_size
);
2358 opcode
= CEPH_OSD_OP_READ
;
2361 if (opcode
== CEPH_OSD_OP_DELETE
)
2362 osd_req_op_init(osd_request
, num_ops
, opcode
, 0);
2364 osd_req_op_extent_init(osd_request
, num_ops
, opcode
,
2365 offset
, length
, 0, 0);
2367 if (obj_request
->type
== OBJ_REQUEST_BIO
)
2368 osd_req_op_extent_osd_data_bio(osd_request
, num_ops
,
2369 obj_request
->bio_list
, length
);
2370 else if (obj_request
->type
== OBJ_REQUEST_PAGES
)
2371 osd_req_op_extent_osd_data_pages(osd_request
, num_ops
,
2372 obj_request
->pages
, length
,
2373 offset
& ~PAGE_MASK
, false, false);
2375 /* Discards are also writes */
2376 if (op_type
== OBJ_OP_WRITE
|| op_type
== OBJ_OP_DISCARD
)
2377 rbd_osd_req_format_write(obj_request
);
2379 rbd_osd_req_format_read(obj_request
);
2383 * Split up an image request into one or more object requests, each
2384 * to a different object. The "type" parameter indicates whether
2385 * "data_desc" is the pointer to the head of a list of bio
2386 * structures, or the base of a page array. In either case this
2387 * function assumes data_desc describes memory sufficient to hold
2388 * all data described by the image request.
2390 static int rbd_img_request_fill(struct rbd_img_request
*img_request
,
2391 enum obj_request_type type
,
2394 struct rbd_device
*rbd_dev
= img_request
->rbd_dev
;
2395 struct rbd_obj_request
*obj_request
= NULL
;
2396 struct rbd_obj_request
*next_obj_request
;
2397 struct bio
*bio_list
= NULL
;
2398 unsigned int bio_offset
= 0;
2399 struct page
**pages
= NULL
;
2400 enum obj_operation_type op_type
;
2404 dout("%s: img %p type %d data_desc %p\n", __func__
, img_request
,
2405 (int)type
, data_desc
);
2407 img_offset
= img_request
->offset
;
2408 resid
= img_request
->length
;
2409 rbd_assert(resid
> 0);
2410 op_type
= rbd_img_request_op_type(img_request
);
2412 if (type
== OBJ_REQUEST_BIO
) {
2413 bio_list
= data_desc
;
2414 rbd_assert(img_offset
==
2415 bio_list
->bi_iter
.bi_sector
<< SECTOR_SHIFT
);
2416 } else if (type
== OBJ_REQUEST_PAGES
) {
2421 struct ceph_osd_request
*osd_req
;
2422 u64 object_no
= img_offset
>> rbd_dev
->header
.obj_order
;
2423 u64 offset
= rbd_segment_offset(rbd_dev
, img_offset
);
2424 u64 length
= rbd_segment_length(rbd_dev
, img_offset
, resid
);
2426 obj_request
= rbd_obj_request_create(type
);
2430 obj_request
->object_no
= object_no
;
2431 obj_request
->offset
= offset
;
2432 obj_request
->length
= length
;
2435 * set obj_request->img_request before creating the
2436 * osd_request so that it gets the right snapc
2438 rbd_img_obj_request_add(img_request
, obj_request
);
2440 if (type
== OBJ_REQUEST_BIO
) {
2441 unsigned int clone_size
;
2443 rbd_assert(length
<= (u64
)UINT_MAX
);
2444 clone_size
= (unsigned int)length
;
2445 obj_request
->bio_list
=
2446 bio_chain_clone_range(&bio_list
,
2450 if (!obj_request
->bio_list
)
2452 } else if (type
== OBJ_REQUEST_PAGES
) {
2453 unsigned int page_count
;
2455 obj_request
->pages
= pages
;
2456 page_count
= (u32
)calc_pages_for(offset
, length
);
2457 obj_request
->page_count
= page_count
;
2458 if ((offset
+ length
) & ~PAGE_MASK
)
2459 page_count
--; /* more on last page */
2460 pages
+= page_count
;
2463 osd_req
= rbd_osd_req_create(rbd_dev
, op_type
,
2464 (op_type
== OBJ_OP_WRITE
) ? 2 : 1,
2469 obj_request
->osd_req
= osd_req
;
2470 obj_request
->callback
= rbd_img_obj_callback
;
2471 obj_request
->img_offset
= img_offset
;
2473 rbd_img_obj_request_fill(obj_request
, osd_req
, op_type
, 0);
2475 img_offset
+= length
;
2482 for_each_obj_request_safe(img_request
, obj_request
, next_obj_request
)
2483 rbd_img_obj_request_del(img_request
, obj_request
);
2489 rbd_osd_copyup_callback(struct rbd_obj_request
*obj_request
)
2491 struct rbd_img_request
*img_request
;
2492 struct rbd_device
*rbd_dev
;
2493 struct page
**pages
;
2496 dout("%s: obj %p\n", __func__
, obj_request
);
2498 rbd_assert(obj_request
->type
== OBJ_REQUEST_BIO
||
2499 obj_request
->type
== OBJ_REQUEST_NODATA
);
2500 rbd_assert(obj_request_img_data_test(obj_request
));
2501 img_request
= obj_request
->img_request
;
2502 rbd_assert(img_request
);
2504 rbd_dev
= img_request
->rbd_dev
;
2505 rbd_assert(rbd_dev
);
2507 pages
= obj_request
->copyup_pages
;
2508 rbd_assert(pages
!= NULL
);
2509 obj_request
->copyup_pages
= NULL
;
2510 page_count
= obj_request
->copyup_page_count
;
2511 rbd_assert(page_count
);
2512 obj_request
->copyup_page_count
= 0;
2513 ceph_release_page_vector(pages
, page_count
);
2516 * We want the transfer count to reflect the size of the
2517 * original write request. There is no such thing as a
2518 * successful short write, so if the request was successful
2519 * we can just set it to the originally-requested length.
2521 if (!obj_request
->result
)
2522 obj_request
->xferred
= obj_request
->length
;
2524 obj_request_done_set(obj_request
);
2528 rbd_img_obj_parent_read_full_callback(struct rbd_img_request
*img_request
)
2530 struct rbd_obj_request
*orig_request
;
2531 struct ceph_osd_request
*osd_req
;
2532 struct rbd_device
*rbd_dev
;
2533 struct page
**pages
;
2534 enum obj_operation_type op_type
;
2539 rbd_assert(img_request_child_test(img_request
));
2541 /* First get what we need from the image request */
2543 pages
= img_request
->copyup_pages
;
2544 rbd_assert(pages
!= NULL
);
2545 img_request
->copyup_pages
= NULL
;
2546 page_count
= img_request
->copyup_page_count
;
2547 rbd_assert(page_count
);
2548 img_request
->copyup_page_count
= 0;
2550 orig_request
= img_request
->obj_request
;
2551 rbd_assert(orig_request
!= NULL
);
2552 rbd_assert(obj_request_type_valid(orig_request
->type
));
2553 img_result
= img_request
->result
;
2554 parent_length
= img_request
->length
;
2555 rbd_assert(img_result
|| parent_length
== img_request
->xferred
);
2556 rbd_img_request_put(img_request
);
2558 rbd_assert(orig_request
->img_request
);
2559 rbd_dev
= orig_request
->img_request
->rbd_dev
;
2560 rbd_assert(rbd_dev
);
2563 * If the overlap has become 0 (most likely because the
2564 * image has been flattened) we need to free the pages
2565 * and re-submit the original write request.
2567 if (!rbd_dev
->parent_overlap
) {
2568 ceph_release_page_vector(pages
, page_count
);
2569 rbd_obj_request_submit(orig_request
);
2577 * The original osd request is of no use to use any more.
2578 * We need a new one that can hold the three ops in a copyup
2579 * request. Allocate the new copyup osd request for the
2580 * original request, and release the old one.
2582 img_result
= -ENOMEM
;
2583 osd_req
= rbd_osd_req_create_copyup(orig_request
);
2586 rbd_osd_req_destroy(orig_request
->osd_req
);
2587 orig_request
->osd_req
= osd_req
;
2588 orig_request
->copyup_pages
= pages
;
2589 orig_request
->copyup_page_count
= page_count
;
2591 /* Initialize the copyup op */
2593 osd_req_op_cls_init(osd_req
, 0, CEPH_OSD_OP_CALL
, "rbd", "copyup");
2594 osd_req_op_cls_request_data_pages(osd_req
, 0, pages
, parent_length
, 0,
2597 /* Add the other op(s) */
2599 op_type
= rbd_img_request_op_type(orig_request
->img_request
);
2600 rbd_img_obj_request_fill(orig_request
, osd_req
, op_type
, 1);
2602 /* All set, send it off. */
2604 rbd_obj_request_submit(orig_request
);
2608 ceph_release_page_vector(pages
, page_count
);
2609 rbd_obj_request_error(orig_request
, img_result
);
2613 * Read from the parent image the range of data that covers the
2614 * entire target of the given object request. This is used for
2615 * satisfying a layered image write request when the target of an
2616 * object request from the image request does not exist.
2618 * A page array big enough to hold the returned data is allocated
2619 * and supplied to rbd_img_request_fill() as the "data descriptor."
2620 * When the read completes, this page array will be transferred to
2621 * the original object request for the copyup operation.
2623 * If an error occurs, it is recorded as the result of the original
2624 * object request in rbd_img_obj_exists_callback().
2626 static int rbd_img_obj_parent_read_full(struct rbd_obj_request
*obj_request
)
2628 struct rbd_device
*rbd_dev
= obj_request
->img_request
->rbd_dev
;
2629 struct rbd_img_request
*parent_request
= NULL
;
2632 struct page
**pages
= NULL
;
2636 rbd_assert(rbd_dev
->parent
!= NULL
);
2639 * Determine the byte range covered by the object in the
2640 * child image to which the original request was to be sent.
2642 img_offset
= obj_request
->img_offset
- obj_request
->offset
;
2643 length
= rbd_obj_bytes(&rbd_dev
->header
);
2646 * There is no defined parent data beyond the parent
2647 * overlap, so limit what we read at that boundary if
2650 if (img_offset
+ length
> rbd_dev
->parent_overlap
) {
2651 rbd_assert(img_offset
< rbd_dev
->parent_overlap
);
2652 length
= rbd_dev
->parent_overlap
- img_offset
;
2656 * Allocate a page array big enough to receive the data read
2659 page_count
= (u32
)calc_pages_for(0, length
);
2660 pages
= ceph_alloc_page_vector(page_count
, GFP_NOIO
);
2661 if (IS_ERR(pages
)) {
2662 result
= PTR_ERR(pages
);
2668 parent_request
= rbd_parent_request_create(obj_request
,
2669 img_offset
, length
);
2670 if (!parent_request
)
2673 result
= rbd_img_request_fill(parent_request
, OBJ_REQUEST_PAGES
, pages
);
2677 parent_request
->copyup_pages
= pages
;
2678 parent_request
->copyup_page_count
= page_count
;
2679 parent_request
->callback
= rbd_img_obj_parent_read_full_callback
;
2681 result
= rbd_img_request_submit(parent_request
);
2685 parent_request
->copyup_pages
= NULL
;
2686 parent_request
->copyup_page_count
= 0;
2689 ceph_release_page_vector(pages
, page_count
);
2691 rbd_img_request_put(parent_request
);
2695 static void rbd_img_obj_exists_callback(struct rbd_obj_request
*obj_request
)
2697 struct rbd_obj_request
*orig_request
;
2698 struct rbd_device
*rbd_dev
;
2701 rbd_assert(!obj_request_img_data_test(obj_request
));
2704 * All we need from the object request is the original
2705 * request and the result of the STAT op. Grab those, then
2706 * we're done with the request.
2708 orig_request
= obj_request
->obj_request
;
2709 obj_request
->obj_request
= NULL
;
2710 rbd_obj_request_put(orig_request
);
2711 rbd_assert(orig_request
);
2712 rbd_assert(orig_request
->img_request
);
2714 result
= obj_request
->result
;
2715 obj_request
->result
= 0;
2717 dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__
,
2718 obj_request
, orig_request
, result
,
2719 obj_request
->xferred
, obj_request
->length
);
2720 rbd_obj_request_put(obj_request
);
2723 * If the overlap has become 0 (most likely because the
2724 * image has been flattened) we need to re-submit the
2727 rbd_dev
= orig_request
->img_request
->rbd_dev
;
2728 if (!rbd_dev
->parent_overlap
) {
2729 rbd_obj_request_submit(orig_request
);
2734 * Our only purpose here is to determine whether the object
2735 * exists, and we don't want to treat the non-existence as
2736 * an error. If something else comes back, transfer the
2737 * error to the original request and complete it now.
2740 obj_request_existence_set(orig_request
, true);
2741 } else if (result
== -ENOENT
) {
2742 obj_request_existence_set(orig_request
, false);
2744 goto fail_orig_request
;
2748 * Resubmit the original request now that we have recorded
2749 * whether the target object exists.
2751 result
= rbd_img_obj_request_submit(orig_request
);
2753 goto fail_orig_request
;
2758 rbd_obj_request_error(orig_request
, result
);
2761 static int rbd_img_obj_exists_submit(struct rbd_obj_request
*obj_request
)
2763 struct rbd_device
*rbd_dev
= obj_request
->img_request
->rbd_dev
;
2764 struct rbd_obj_request
*stat_request
;
2765 struct page
**pages
;
2770 stat_request
= rbd_obj_request_create(OBJ_REQUEST_PAGES
);
2774 stat_request
->object_no
= obj_request
->object_no
;
2776 stat_request
->osd_req
= rbd_osd_req_create(rbd_dev
, OBJ_OP_READ
, 1,
2778 if (!stat_request
->osd_req
) {
2780 goto fail_stat_request
;
2784 * The response data for a STAT call consists of:
2791 size
= sizeof (__le64
) + sizeof (__le32
) + sizeof (__le32
);
2792 page_count
= (u32
)calc_pages_for(0, size
);
2793 pages
= ceph_alloc_page_vector(page_count
, GFP_NOIO
);
2794 if (IS_ERR(pages
)) {
2795 ret
= PTR_ERR(pages
);
2796 goto fail_stat_request
;
2799 osd_req_op_init(stat_request
->osd_req
, 0, CEPH_OSD_OP_STAT
, 0);
2800 osd_req_op_raw_data_in_pages(stat_request
->osd_req
, 0, pages
, size
, 0,
2803 rbd_obj_request_get(obj_request
);
2804 stat_request
->obj_request
= obj_request
;
2805 stat_request
->pages
= pages
;
2806 stat_request
->page_count
= page_count
;
2807 stat_request
->callback
= rbd_img_obj_exists_callback
;
2809 rbd_obj_request_submit(stat_request
);
2813 rbd_obj_request_put(stat_request
);
2817 static bool img_obj_request_simple(struct rbd_obj_request
*obj_request
)
2819 struct rbd_img_request
*img_request
= obj_request
->img_request
;
2820 struct rbd_device
*rbd_dev
= img_request
->rbd_dev
;
2823 if (!img_request_write_test(img_request
) &&
2824 !img_request_discard_test(img_request
))
2827 /* Non-layered writes */
2828 if (!img_request_layered_test(img_request
))
2832 * Layered writes outside of the parent overlap range don't
2833 * share any data with the parent.
2835 if (!obj_request_overlaps_parent(obj_request
))
2839 * Entire-object layered writes - we will overwrite whatever
2840 * parent data there is anyway.
2842 if (!obj_request
->offset
&&
2843 obj_request
->length
== rbd_obj_bytes(&rbd_dev
->header
))
2847 * If the object is known to already exist, its parent data has
2848 * already been copied.
2850 if (obj_request_known_test(obj_request
) &&
2851 obj_request_exists_test(obj_request
))
2857 static int rbd_img_obj_request_submit(struct rbd_obj_request
*obj_request
)
2859 rbd_assert(obj_request_img_data_test(obj_request
));
2860 rbd_assert(obj_request_type_valid(obj_request
->type
));
2861 rbd_assert(obj_request
->img_request
);
2863 if (img_obj_request_simple(obj_request
)) {
2864 rbd_obj_request_submit(obj_request
);
2869 * It's a layered write. The target object might exist but
2870 * we may not know that yet. If we know it doesn't exist,
2871 * start by reading the data for the full target object from
2872 * the parent so we can use it for a copyup to the target.
2874 if (obj_request_known_test(obj_request
))
2875 return rbd_img_obj_parent_read_full(obj_request
);
2877 /* We don't know whether the target exists. Go find out. */
2879 return rbd_img_obj_exists_submit(obj_request
);
2882 static int rbd_img_request_submit(struct rbd_img_request
*img_request
)
2884 struct rbd_obj_request
*obj_request
;
2885 struct rbd_obj_request
*next_obj_request
;
2888 dout("%s: img %p\n", __func__
, img_request
);
2890 rbd_img_request_get(img_request
);
2891 for_each_obj_request_safe(img_request
, obj_request
, next_obj_request
) {
2892 ret
= rbd_img_obj_request_submit(obj_request
);
2898 rbd_img_request_put(img_request
);
2902 static void rbd_img_parent_read_callback(struct rbd_img_request
*img_request
)
2904 struct rbd_obj_request
*obj_request
;
2905 struct rbd_device
*rbd_dev
;
2910 rbd_assert(img_request_child_test(img_request
));
2912 /* First get what we need from the image request and release it */
2914 obj_request
= img_request
->obj_request
;
2915 img_xferred
= img_request
->xferred
;
2916 img_result
= img_request
->result
;
2917 rbd_img_request_put(img_request
);
2920 * If the overlap has become 0 (most likely because the
2921 * image has been flattened) we need to re-submit the
2924 rbd_assert(obj_request
);
2925 rbd_assert(obj_request
->img_request
);
2926 rbd_dev
= obj_request
->img_request
->rbd_dev
;
2927 if (!rbd_dev
->parent_overlap
) {
2928 rbd_obj_request_submit(obj_request
);
2932 obj_request
->result
= img_result
;
2933 if (obj_request
->result
)
2937 * We need to zero anything beyond the parent overlap
2938 * boundary. Since rbd_img_obj_request_read_callback()
2939 * will zero anything beyond the end of a short read, an
2940 * easy way to do this is to pretend the data from the
2941 * parent came up short--ending at the overlap boundary.
2943 rbd_assert(obj_request
->img_offset
< U64_MAX
- obj_request
->length
);
2944 obj_end
= obj_request
->img_offset
+ obj_request
->length
;
2945 if (obj_end
> rbd_dev
->parent_overlap
) {
2948 if (obj_request
->img_offset
< rbd_dev
->parent_overlap
)
2949 xferred
= rbd_dev
->parent_overlap
-
2950 obj_request
->img_offset
;
2952 obj_request
->xferred
= min(img_xferred
, xferred
);
2954 obj_request
->xferred
= img_xferred
;
2957 rbd_img_obj_request_read_callback(obj_request
);
2958 rbd_obj_request_complete(obj_request
);
2961 static void rbd_img_parent_read(struct rbd_obj_request
*obj_request
)
2963 struct rbd_img_request
*img_request
;
2966 rbd_assert(obj_request_img_data_test(obj_request
));
2967 rbd_assert(obj_request
->img_request
!= NULL
);
2968 rbd_assert(obj_request
->result
== (s32
) -ENOENT
);
2969 rbd_assert(obj_request_type_valid(obj_request
->type
));
2971 /* rbd_read_finish(obj_request, obj_request->length); */
2972 img_request
= rbd_parent_request_create(obj_request
,
2973 obj_request
->img_offset
,
2974 obj_request
->length
);
2979 if (obj_request
->type
== OBJ_REQUEST_BIO
)
2980 result
= rbd_img_request_fill(img_request
, OBJ_REQUEST_BIO
,
2981 obj_request
->bio_list
);
2983 result
= rbd_img_request_fill(img_request
, OBJ_REQUEST_PAGES
,
2984 obj_request
->pages
);
2988 img_request
->callback
= rbd_img_parent_read_callback
;
2989 result
= rbd_img_request_submit(img_request
);
2996 rbd_img_request_put(img_request
);
2997 obj_request
->result
= result
;
2998 obj_request
->xferred
= 0;
2999 obj_request_done_set(obj_request
);
3002 static const struct rbd_client_id rbd_empty_cid
;
3004 static bool rbd_cid_equal(const struct rbd_client_id
*lhs
,
3005 const struct rbd_client_id
*rhs
)
3007 return lhs
->gid
== rhs
->gid
&& lhs
->handle
== rhs
->handle
;
3010 static struct rbd_client_id
rbd_get_cid(struct rbd_device
*rbd_dev
)
3012 struct rbd_client_id cid
;
3014 mutex_lock(&rbd_dev
->watch_mutex
);
3015 cid
.gid
= ceph_client_gid(rbd_dev
->rbd_client
->client
);
3016 cid
.handle
= rbd_dev
->watch_cookie
;
3017 mutex_unlock(&rbd_dev
->watch_mutex
);
3022 * lock_rwsem must be held for write
3024 static void rbd_set_owner_cid(struct rbd_device
*rbd_dev
,
3025 const struct rbd_client_id
*cid
)
3027 dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__
, rbd_dev
,
3028 rbd_dev
->owner_cid
.gid
, rbd_dev
->owner_cid
.handle
,
3029 cid
->gid
, cid
->handle
);
3030 rbd_dev
->owner_cid
= *cid
; /* struct */
3033 static void format_lock_cookie(struct rbd_device
*rbd_dev
, char *buf
)
3035 mutex_lock(&rbd_dev
->watch_mutex
);
3036 sprintf(buf
, "%s %llu", RBD_LOCK_COOKIE_PREFIX
, rbd_dev
->watch_cookie
);
3037 mutex_unlock(&rbd_dev
->watch_mutex
);
3040 static void __rbd_lock(struct rbd_device
*rbd_dev
, const char *cookie
)
3042 struct rbd_client_id cid
= rbd_get_cid(rbd_dev
);
3044 strcpy(rbd_dev
->lock_cookie
, cookie
);
3045 rbd_set_owner_cid(rbd_dev
, &cid
);
3046 queue_work(rbd_dev
->task_wq
, &rbd_dev
->acquired_lock_work
);
3050 * lock_rwsem must be held for write
3052 static int rbd_lock(struct rbd_device
*rbd_dev
)
3054 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
3058 WARN_ON(__rbd_is_lock_owner(rbd_dev
) ||
3059 rbd_dev
->lock_cookie
[0] != '\0');
3061 format_lock_cookie(rbd_dev
, cookie
);
3062 ret
= ceph_cls_lock(osdc
, &rbd_dev
->header_oid
, &rbd_dev
->header_oloc
,
3063 RBD_LOCK_NAME
, CEPH_CLS_LOCK_EXCLUSIVE
, cookie
,
3064 RBD_LOCK_TAG
, "", 0);
3068 rbd_dev
->lock_state
= RBD_LOCK_STATE_LOCKED
;
3069 __rbd_lock(rbd_dev
, cookie
);
3074 * lock_rwsem must be held for write
3076 static void rbd_unlock(struct rbd_device
*rbd_dev
)
3078 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
3081 WARN_ON(!__rbd_is_lock_owner(rbd_dev
) ||
3082 rbd_dev
->lock_cookie
[0] == '\0');
3084 ret
= ceph_cls_unlock(osdc
, &rbd_dev
->header_oid
, &rbd_dev
->header_oloc
,
3085 RBD_LOCK_NAME
, rbd_dev
->lock_cookie
);
3086 if (ret
&& ret
!= -ENOENT
)
3087 rbd_warn(rbd_dev
, "failed to unlock: %d", ret
);
3089 /* treat errors as the image is unlocked */
3090 rbd_dev
->lock_state
= RBD_LOCK_STATE_UNLOCKED
;
3091 rbd_dev
->lock_cookie
[0] = '\0';
3092 rbd_set_owner_cid(rbd_dev
, &rbd_empty_cid
);
3093 queue_work(rbd_dev
->task_wq
, &rbd_dev
->released_lock_work
);
3096 static int __rbd_notify_op_lock(struct rbd_device
*rbd_dev
,
3097 enum rbd_notify_op notify_op
,
3098 struct page
***preply_pages
,
3101 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
3102 struct rbd_client_id cid
= rbd_get_cid(rbd_dev
);
3103 int buf_size
= 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN
;
3107 dout("%s rbd_dev %p notify_op %d\n", __func__
, rbd_dev
, notify_op
);
3109 /* encode *LockPayload NotifyMessage (op + ClientId) */
3110 ceph_start_encoding(&p
, 2, 1, buf_size
- CEPH_ENCODING_START_BLK_LEN
);
3111 ceph_encode_32(&p
, notify_op
);
3112 ceph_encode_64(&p
, cid
.gid
);
3113 ceph_encode_64(&p
, cid
.handle
);
3115 return ceph_osdc_notify(osdc
, &rbd_dev
->header_oid
,
3116 &rbd_dev
->header_oloc
, buf
, buf_size
,
3117 RBD_NOTIFY_TIMEOUT
, preply_pages
, preply_len
);
3120 static void rbd_notify_op_lock(struct rbd_device
*rbd_dev
,
3121 enum rbd_notify_op notify_op
)
3123 struct page
**reply_pages
;
3126 __rbd_notify_op_lock(rbd_dev
, notify_op
, &reply_pages
, &reply_len
);
3127 ceph_release_page_vector(reply_pages
, calc_pages_for(0, reply_len
));
3130 static void rbd_notify_acquired_lock(struct work_struct
*work
)
3132 struct rbd_device
*rbd_dev
= container_of(work
, struct rbd_device
,
3133 acquired_lock_work
);
3135 rbd_notify_op_lock(rbd_dev
, RBD_NOTIFY_OP_ACQUIRED_LOCK
);
3138 static void rbd_notify_released_lock(struct work_struct
*work
)
3140 struct rbd_device
*rbd_dev
= container_of(work
, struct rbd_device
,
3141 released_lock_work
);
3143 rbd_notify_op_lock(rbd_dev
, RBD_NOTIFY_OP_RELEASED_LOCK
);
3146 static int rbd_request_lock(struct rbd_device
*rbd_dev
)
3148 struct page
**reply_pages
;
3150 bool lock_owner_responded
= false;
3153 dout("%s rbd_dev %p\n", __func__
, rbd_dev
);
3155 ret
= __rbd_notify_op_lock(rbd_dev
, RBD_NOTIFY_OP_REQUEST_LOCK
,
3156 &reply_pages
, &reply_len
);
3157 if (ret
&& ret
!= -ETIMEDOUT
) {
3158 rbd_warn(rbd_dev
, "failed to request lock: %d", ret
);
3162 if (reply_len
> 0 && reply_len
<= PAGE_SIZE
) {
3163 void *p
= page_address(reply_pages
[0]);
3164 void *const end
= p
+ reply_len
;
3167 ceph_decode_32_safe(&p
, end
, n
, e_inval
); /* num_acks */
3172 ceph_decode_need(&p
, end
, 8 + 8, e_inval
);
3173 p
+= 8 + 8; /* skip gid and cookie */
3175 ceph_decode_32_safe(&p
, end
, len
, e_inval
);
3179 if (lock_owner_responded
) {
3181 "duplicate lock owners detected");
3186 lock_owner_responded
= true;
3187 ret
= ceph_start_decoding(&p
, end
, 1, "ResponseMessage",
3191 "failed to decode ResponseMessage: %d",
3196 ret
= ceph_decode_32(&p
);
3200 if (!lock_owner_responded
) {
3201 rbd_warn(rbd_dev
, "no lock owners detected");
3206 ceph_release_page_vector(reply_pages
, calc_pages_for(0, reply_len
));
3214 static void wake_requests(struct rbd_device
*rbd_dev
, bool wake_all
)
3216 dout("%s rbd_dev %p wake_all %d\n", __func__
, rbd_dev
, wake_all
);
3218 cancel_delayed_work(&rbd_dev
->lock_dwork
);
3220 wake_up_all(&rbd_dev
->lock_waitq
);
3222 wake_up(&rbd_dev
->lock_waitq
);
3225 static int get_lock_owner_info(struct rbd_device
*rbd_dev
,
3226 struct ceph_locker
**lockers
, u32
*num_lockers
)
3228 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
3233 dout("%s rbd_dev %p\n", __func__
, rbd_dev
);
3235 ret
= ceph_cls_lock_info(osdc
, &rbd_dev
->header_oid
,
3236 &rbd_dev
->header_oloc
, RBD_LOCK_NAME
,
3237 &lock_type
, &lock_tag
, lockers
, num_lockers
);
3241 if (*num_lockers
== 0) {
3242 dout("%s rbd_dev %p no lockers detected\n", __func__
, rbd_dev
);
3246 if (strcmp(lock_tag
, RBD_LOCK_TAG
)) {
3247 rbd_warn(rbd_dev
, "locked by external mechanism, tag %s",
3253 if (lock_type
== CEPH_CLS_LOCK_SHARED
) {
3254 rbd_warn(rbd_dev
, "shared lock type detected");
3259 if (strncmp((*lockers
)[0].id
.cookie
, RBD_LOCK_COOKIE_PREFIX
,
3260 strlen(RBD_LOCK_COOKIE_PREFIX
))) {
3261 rbd_warn(rbd_dev
, "locked by external mechanism, cookie %s",
3262 (*lockers
)[0].id
.cookie
);
3272 static int find_watcher(struct rbd_device
*rbd_dev
,
3273 const struct ceph_locker
*locker
)
3275 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
3276 struct ceph_watch_item
*watchers
;
3282 ret
= ceph_osdc_list_watchers(osdc
, &rbd_dev
->header_oid
,
3283 &rbd_dev
->header_oloc
, &watchers
,
3288 sscanf(locker
->id
.cookie
, RBD_LOCK_COOKIE_PREFIX
" %llu", &cookie
);
3289 for (i
= 0; i
< num_watchers
; i
++) {
3290 if (!memcmp(&watchers
[i
].addr
, &locker
->info
.addr
,
3291 sizeof(locker
->info
.addr
)) &&
3292 watchers
[i
].cookie
== cookie
) {
3293 struct rbd_client_id cid
= {
3294 .gid
= le64_to_cpu(watchers
[i
].name
.num
),
3298 dout("%s rbd_dev %p found cid %llu-%llu\n", __func__
,
3299 rbd_dev
, cid
.gid
, cid
.handle
);
3300 rbd_set_owner_cid(rbd_dev
, &cid
);
3306 dout("%s rbd_dev %p no watchers\n", __func__
, rbd_dev
);
3314 * lock_rwsem must be held for write
3316 static int rbd_try_lock(struct rbd_device
*rbd_dev
)
3318 struct ceph_client
*client
= rbd_dev
->rbd_client
->client
;
3319 struct ceph_locker
*lockers
;
3324 ret
= rbd_lock(rbd_dev
);
3328 /* determine if the current lock holder is still alive */
3329 ret
= get_lock_owner_info(rbd_dev
, &lockers
, &num_lockers
);
3333 if (num_lockers
== 0)
3336 ret
= find_watcher(rbd_dev
, lockers
);
3339 ret
= 0; /* have to request lock */
3343 rbd_warn(rbd_dev
, "%s%llu seems dead, breaking lock",
3344 ENTITY_NAME(lockers
[0].id
.name
));
3346 ret
= ceph_monc_blacklist_add(&client
->monc
,
3347 &lockers
[0].info
.addr
);
3349 rbd_warn(rbd_dev
, "blacklist of %s%llu failed: %d",
3350 ENTITY_NAME(lockers
[0].id
.name
), ret
);
3354 ret
= ceph_cls_break_lock(&client
->osdc
, &rbd_dev
->header_oid
,
3355 &rbd_dev
->header_oloc
, RBD_LOCK_NAME
,
3356 lockers
[0].id
.cookie
,
3357 &lockers
[0].id
.name
);
3358 if (ret
&& ret
!= -ENOENT
)
3362 ceph_free_lockers(lockers
, num_lockers
);
3366 ceph_free_lockers(lockers
, num_lockers
);
3371 * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED
3373 static enum rbd_lock_state
rbd_try_acquire_lock(struct rbd_device
*rbd_dev
,
3376 enum rbd_lock_state lock_state
;
3378 down_read(&rbd_dev
->lock_rwsem
);
3379 dout("%s rbd_dev %p read lock_state %d\n", __func__
, rbd_dev
,
3380 rbd_dev
->lock_state
);
3381 if (__rbd_is_lock_owner(rbd_dev
)) {
3382 lock_state
= rbd_dev
->lock_state
;
3383 up_read(&rbd_dev
->lock_rwsem
);
3387 up_read(&rbd_dev
->lock_rwsem
);
3388 down_write(&rbd_dev
->lock_rwsem
);
3389 dout("%s rbd_dev %p write lock_state %d\n", __func__
, rbd_dev
,
3390 rbd_dev
->lock_state
);
3391 if (!__rbd_is_lock_owner(rbd_dev
)) {
3392 *pret
= rbd_try_lock(rbd_dev
);
3394 rbd_warn(rbd_dev
, "failed to acquire lock: %d", *pret
);
3397 lock_state
= rbd_dev
->lock_state
;
3398 up_write(&rbd_dev
->lock_rwsem
);
3402 static void rbd_acquire_lock(struct work_struct
*work
)
3404 struct rbd_device
*rbd_dev
= container_of(to_delayed_work(work
),
3405 struct rbd_device
, lock_dwork
);
3406 enum rbd_lock_state lock_state
;
3409 dout("%s rbd_dev %p\n", __func__
, rbd_dev
);
3411 lock_state
= rbd_try_acquire_lock(rbd_dev
, &ret
);
3412 if (lock_state
!= RBD_LOCK_STATE_UNLOCKED
|| ret
== -EBLACKLISTED
) {
3413 if (lock_state
== RBD_LOCK_STATE_LOCKED
)
3414 wake_requests(rbd_dev
, true);
3415 dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__
,
3416 rbd_dev
, lock_state
, ret
);
3420 ret
= rbd_request_lock(rbd_dev
);
3421 if (ret
== -ETIMEDOUT
) {
3422 goto again
; /* treat this as a dead client */
3423 } else if (ret
== -EROFS
) {
3424 rbd_warn(rbd_dev
, "peer will not release lock");
3426 * If this is rbd_add_acquire_lock(), we want to fail
3427 * immediately -- reuse BLACKLISTED flag. Otherwise we
3430 if (!(rbd_dev
->disk
->flags
& GENHD_FL_UP
)) {
3431 set_bit(RBD_DEV_FLAG_BLACKLISTED
, &rbd_dev
->flags
);
3432 /* wake "rbd map --exclusive" process */
3433 wake_requests(rbd_dev
, false);
3435 } else if (ret
< 0) {
3436 rbd_warn(rbd_dev
, "error requesting lock: %d", ret
);
3437 mod_delayed_work(rbd_dev
->task_wq
, &rbd_dev
->lock_dwork
,
3441 * lock owner acked, but resend if we don't see them
3444 dout("%s rbd_dev %p requeueing lock_dwork\n", __func__
,
3446 mod_delayed_work(rbd_dev
->task_wq
, &rbd_dev
->lock_dwork
,
3447 msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT
* MSEC_PER_SEC
));
3452 * lock_rwsem must be held for write
3454 static bool rbd_release_lock(struct rbd_device
*rbd_dev
)
3456 dout("%s rbd_dev %p read lock_state %d\n", __func__
, rbd_dev
,
3457 rbd_dev
->lock_state
);
3458 if (rbd_dev
->lock_state
!= RBD_LOCK_STATE_LOCKED
)
3461 rbd_dev
->lock_state
= RBD_LOCK_STATE_RELEASING
;
3462 downgrade_write(&rbd_dev
->lock_rwsem
);
3464 * Ensure that all in-flight IO is flushed.
3466 * FIXME: ceph_osdc_sync() flushes the entire OSD client, which
3467 * may be shared with other devices.
3469 ceph_osdc_sync(&rbd_dev
->rbd_client
->client
->osdc
);
3470 up_read(&rbd_dev
->lock_rwsem
);
3472 down_write(&rbd_dev
->lock_rwsem
);
3473 dout("%s rbd_dev %p write lock_state %d\n", __func__
, rbd_dev
,
3474 rbd_dev
->lock_state
);
3475 if (rbd_dev
->lock_state
!= RBD_LOCK_STATE_RELEASING
)
3478 rbd_unlock(rbd_dev
);
3480 * Give others a chance to grab the lock - we would re-acquire
3481 * almost immediately if we got new IO during ceph_osdc_sync()
3482 * otherwise. We need to ack our own notifications, so this
3483 * lock_dwork will be requeued from rbd_wait_state_locked()
3484 * after wake_requests() in rbd_handle_released_lock().
3486 cancel_delayed_work(&rbd_dev
->lock_dwork
);
3490 static void rbd_release_lock_work(struct work_struct
*work
)
3492 struct rbd_device
*rbd_dev
= container_of(work
, struct rbd_device
,
3495 down_write(&rbd_dev
->lock_rwsem
);
3496 rbd_release_lock(rbd_dev
);
3497 up_write(&rbd_dev
->lock_rwsem
);
3500 static void rbd_handle_acquired_lock(struct rbd_device
*rbd_dev
, u8 struct_v
,
3503 struct rbd_client_id cid
= { 0 };
3505 if (struct_v
>= 2) {
3506 cid
.gid
= ceph_decode_64(p
);
3507 cid
.handle
= ceph_decode_64(p
);
3510 dout("%s rbd_dev %p cid %llu-%llu\n", __func__
, rbd_dev
, cid
.gid
,
3512 if (!rbd_cid_equal(&cid
, &rbd_empty_cid
)) {
3513 down_write(&rbd_dev
->lock_rwsem
);
3514 if (rbd_cid_equal(&cid
, &rbd_dev
->owner_cid
)) {
3516 * we already know that the remote client is
3519 up_write(&rbd_dev
->lock_rwsem
);
3523 rbd_set_owner_cid(rbd_dev
, &cid
);
3524 downgrade_write(&rbd_dev
->lock_rwsem
);
3526 down_read(&rbd_dev
->lock_rwsem
);
3529 if (!__rbd_is_lock_owner(rbd_dev
))
3530 wake_requests(rbd_dev
, false);
3531 up_read(&rbd_dev
->lock_rwsem
);
3534 static void rbd_handle_released_lock(struct rbd_device
*rbd_dev
, u8 struct_v
,
3537 struct rbd_client_id cid
= { 0 };
3539 if (struct_v
>= 2) {
3540 cid
.gid
= ceph_decode_64(p
);
3541 cid
.handle
= ceph_decode_64(p
);
3544 dout("%s rbd_dev %p cid %llu-%llu\n", __func__
, rbd_dev
, cid
.gid
,
3546 if (!rbd_cid_equal(&cid
, &rbd_empty_cid
)) {
3547 down_write(&rbd_dev
->lock_rwsem
);
3548 if (!rbd_cid_equal(&cid
, &rbd_dev
->owner_cid
)) {
3549 dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n",
3550 __func__
, rbd_dev
, cid
.gid
, cid
.handle
,
3551 rbd_dev
->owner_cid
.gid
, rbd_dev
->owner_cid
.handle
);
3552 up_write(&rbd_dev
->lock_rwsem
);
3556 rbd_set_owner_cid(rbd_dev
, &rbd_empty_cid
);
3557 downgrade_write(&rbd_dev
->lock_rwsem
);
3559 down_read(&rbd_dev
->lock_rwsem
);
3562 if (!__rbd_is_lock_owner(rbd_dev
))
3563 wake_requests(rbd_dev
, false);
3564 up_read(&rbd_dev
->lock_rwsem
);
3568 * Returns result for ResponseMessage to be encoded (<= 0), or 1 if no
3569 * ResponseMessage is needed.
3571 static int rbd_handle_request_lock(struct rbd_device
*rbd_dev
, u8 struct_v
,
3574 struct rbd_client_id my_cid
= rbd_get_cid(rbd_dev
);
3575 struct rbd_client_id cid
= { 0 };
3578 if (struct_v
>= 2) {
3579 cid
.gid
= ceph_decode_64(p
);
3580 cid
.handle
= ceph_decode_64(p
);
3583 dout("%s rbd_dev %p cid %llu-%llu\n", __func__
, rbd_dev
, cid
.gid
,
3585 if (rbd_cid_equal(&cid
, &my_cid
))
3588 down_read(&rbd_dev
->lock_rwsem
);
3589 if (__rbd_is_lock_owner(rbd_dev
)) {
3590 if (rbd_dev
->lock_state
== RBD_LOCK_STATE_LOCKED
&&
3591 rbd_cid_equal(&rbd_dev
->owner_cid
, &rbd_empty_cid
))
3595 * encode ResponseMessage(0) so the peer can detect
3600 if (rbd_dev
->lock_state
== RBD_LOCK_STATE_LOCKED
) {
3601 if (!rbd_dev
->opts
->exclusive
) {
3602 dout("%s rbd_dev %p queueing unlock_work\n",
3604 queue_work(rbd_dev
->task_wq
,
3605 &rbd_dev
->unlock_work
);
3607 /* refuse to release the lock */
3614 up_read(&rbd_dev
->lock_rwsem
);
3618 static void __rbd_acknowledge_notify(struct rbd_device
*rbd_dev
,
3619 u64 notify_id
, u64 cookie
, s32
*result
)
3621 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
3622 int buf_size
= 4 + CEPH_ENCODING_START_BLK_LEN
;
3629 /* encode ResponseMessage */
3630 ceph_start_encoding(&p
, 1, 1,
3631 buf_size
- CEPH_ENCODING_START_BLK_LEN
);
3632 ceph_encode_32(&p
, *result
);
3637 ret
= ceph_osdc_notify_ack(osdc
, &rbd_dev
->header_oid
,
3638 &rbd_dev
->header_oloc
, notify_id
, cookie
,
3641 rbd_warn(rbd_dev
, "acknowledge_notify failed: %d", ret
);
3644 static void rbd_acknowledge_notify(struct rbd_device
*rbd_dev
, u64 notify_id
,
3647 dout("%s rbd_dev %p\n", __func__
, rbd_dev
);
3648 __rbd_acknowledge_notify(rbd_dev
, notify_id
, cookie
, NULL
);
3651 static void rbd_acknowledge_notify_result(struct rbd_device
*rbd_dev
,
3652 u64 notify_id
, u64 cookie
, s32 result
)
3654 dout("%s rbd_dev %p result %d\n", __func__
, rbd_dev
, result
);
3655 __rbd_acknowledge_notify(rbd_dev
, notify_id
, cookie
, &result
);
3658 static void rbd_watch_cb(void *arg
, u64 notify_id
, u64 cookie
,
3659 u64 notifier_id
, void *data
, size_t data_len
)
3661 struct rbd_device
*rbd_dev
= arg
;
3663 void *const end
= p
+ data_len
;
3669 dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n",
3670 __func__
, rbd_dev
, cookie
, notify_id
, data_len
);
3672 ret
= ceph_start_decoding(&p
, end
, 1, "NotifyMessage",
3675 rbd_warn(rbd_dev
, "failed to decode NotifyMessage: %d",
3680 notify_op
= ceph_decode_32(&p
);
3682 /* legacy notification for header updates */
3683 notify_op
= RBD_NOTIFY_OP_HEADER_UPDATE
;
3687 dout("%s rbd_dev %p notify_op %u\n", __func__
, rbd_dev
, notify_op
);
3688 switch (notify_op
) {
3689 case RBD_NOTIFY_OP_ACQUIRED_LOCK
:
3690 rbd_handle_acquired_lock(rbd_dev
, struct_v
, &p
);
3691 rbd_acknowledge_notify(rbd_dev
, notify_id
, cookie
);
3693 case RBD_NOTIFY_OP_RELEASED_LOCK
:
3694 rbd_handle_released_lock(rbd_dev
, struct_v
, &p
);
3695 rbd_acknowledge_notify(rbd_dev
, notify_id
, cookie
);
3697 case RBD_NOTIFY_OP_REQUEST_LOCK
:
3698 ret
= rbd_handle_request_lock(rbd_dev
, struct_v
, &p
);
3700 rbd_acknowledge_notify_result(rbd_dev
, notify_id
,
3703 rbd_acknowledge_notify(rbd_dev
, notify_id
, cookie
);
3705 case RBD_NOTIFY_OP_HEADER_UPDATE
:
3706 ret
= rbd_dev_refresh(rbd_dev
);
3708 rbd_warn(rbd_dev
, "refresh failed: %d", ret
);
3710 rbd_acknowledge_notify(rbd_dev
, notify_id
, cookie
);
3713 if (rbd_is_lock_owner(rbd_dev
))
3714 rbd_acknowledge_notify_result(rbd_dev
, notify_id
,
3715 cookie
, -EOPNOTSUPP
);
3717 rbd_acknowledge_notify(rbd_dev
, notify_id
, cookie
);
3722 static void __rbd_unregister_watch(struct rbd_device
*rbd_dev
);
3724 static void rbd_watch_errcb(void *arg
, u64 cookie
, int err
)
3726 struct rbd_device
*rbd_dev
= arg
;
3728 rbd_warn(rbd_dev
, "encountered watch error: %d", err
);
3730 down_write(&rbd_dev
->lock_rwsem
);
3731 rbd_set_owner_cid(rbd_dev
, &rbd_empty_cid
);
3732 up_write(&rbd_dev
->lock_rwsem
);
3734 mutex_lock(&rbd_dev
->watch_mutex
);
3735 if (rbd_dev
->watch_state
== RBD_WATCH_STATE_REGISTERED
) {
3736 __rbd_unregister_watch(rbd_dev
);
3737 rbd_dev
->watch_state
= RBD_WATCH_STATE_ERROR
;
3739 queue_delayed_work(rbd_dev
->task_wq
, &rbd_dev
->watch_dwork
, 0);
3741 mutex_unlock(&rbd_dev
->watch_mutex
);
3745 * watch_mutex must be locked
3747 static int __rbd_register_watch(struct rbd_device
*rbd_dev
)
3749 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
3750 struct ceph_osd_linger_request
*handle
;
3752 rbd_assert(!rbd_dev
->watch_handle
);
3753 dout("%s rbd_dev %p\n", __func__
, rbd_dev
);
3755 handle
= ceph_osdc_watch(osdc
, &rbd_dev
->header_oid
,
3756 &rbd_dev
->header_oloc
, rbd_watch_cb
,
3757 rbd_watch_errcb
, rbd_dev
);
3759 return PTR_ERR(handle
);
3761 rbd_dev
->watch_handle
= handle
;
3766 * watch_mutex must be locked
3768 static void __rbd_unregister_watch(struct rbd_device
*rbd_dev
)
3770 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
3773 rbd_assert(rbd_dev
->watch_handle
);
3774 dout("%s rbd_dev %p\n", __func__
, rbd_dev
);
3776 ret
= ceph_osdc_unwatch(osdc
, rbd_dev
->watch_handle
);
3778 rbd_warn(rbd_dev
, "failed to unwatch: %d", ret
);
3780 rbd_dev
->watch_handle
= NULL
;
3783 static int rbd_register_watch(struct rbd_device
*rbd_dev
)
3787 mutex_lock(&rbd_dev
->watch_mutex
);
3788 rbd_assert(rbd_dev
->watch_state
== RBD_WATCH_STATE_UNREGISTERED
);
3789 ret
= __rbd_register_watch(rbd_dev
);
3793 rbd_dev
->watch_state
= RBD_WATCH_STATE_REGISTERED
;
3794 rbd_dev
->watch_cookie
= rbd_dev
->watch_handle
->linger_id
;
3797 mutex_unlock(&rbd_dev
->watch_mutex
);
3801 static void cancel_tasks_sync(struct rbd_device
*rbd_dev
)
3803 dout("%s rbd_dev %p\n", __func__
, rbd_dev
);
3805 cancel_delayed_work_sync(&rbd_dev
->watch_dwork
);
3806 cancel_work_sync(&rbd_dev
->acquired_lock_work
);
3807 cancel_work_sync(&rbd_dev
->released_lock_work
);
3808 cancel_delayed_work_sync(&rbd_dev
->lock_dwork
);
3809 cancel_work_sync(&rbd_dev
->unlock_work
);
3812 static void rbd_unregister_watch(struct rbd_device
*rbd_dev
)
3814 WARN_ON(waitqueue_active(&rbd_dev
->lock_waitq
));
3815 cancel_tasks_sync(rbd_dev
);
3817 mutex_lock(&rbd_dev
->watch_mutex
);
3818 if (rbd_dev
->watch_state
== RBD_WATCH_STATE_REGISTERED
)
3819 __rbd_unregister_watch(rbd_dev
);
3820 rbd_dev
->watch_state
= RBD_WATCH_STATE_UNREGISTERED
;
3821 mutex_unlock(&rbd_dev
->watch_mutex
);
3823 ceph_osdc_flush_notifies(&rbd_dev
->rbd_client
->client
->osdc
);
3827 * lock_rwsem must be held for write
3829 static void rbd_reacquire_lock(struct rbd_device
*rbd_dev
)
3831 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
3835 WARN_ON(rbd_dev
->lock_state
!= RBD_LOCK_STATE_LOCKED
);
3837 format_lock_cookie(rbd_dev
, cookie
);
3838 ret
= ceph_cls_set_cookie(osdc
, &rbd_dev
->header_oid
,
3839 &rbd_dev
->header_oloc
, RBD_LOCK_NAME
,
3840 CEPH_CLS_LOCK_EXCLUSIVE
, rbd_dev
->lock_cookie
,
3841 RBD_LOCK_TAG
, cookie
);
3843 if (ret
!= -EOPNOTSUPP
)
3844 rbd_warn(rbd_dev
, "failed to update lock cookie: %d",
3848 * Lock cookie cannot be updated on older OSDs, so do
3849 * a manual release and queue an acquire.
3851 if (rbd_release_lock(rbd_dev
))
3852 queue_delayed_work(rbd_dev
->task_wq
,
3853 &rbd_dev
->lock_dwork
, 0);
3855 __rbd_lock(rbd_dev
, cookie
);
3859 static void rbd_reregister_watch(struct work_struct
*work
)
3861 struct rbd_device
*rbd_dev
= container_of(to_delayed_work(work
),
3862 struct rbd_device
, watch_dwork
);
3865 dout("%s rbd_dev %p\n", __func__
, rbd_dev
);
3867 mutex_lock(&rbd_dev
->watch_mutex
);
3868 if (rbd_dev
->watch_state
!= RBD_WATCH_STATE_ERROR
) {
3869 mutex_unlock(&rbd_dev
->watch_mutex
);
3873 ret
= __rbd_register_watch(rbd_dev
);
3875 rbd_warn(rbd_dev
, "failed to reregister watch: %d", ret
);
3876 if (ret
== -EBLACKLISTED
|| ret
== -ENOENT
) {
3877 set_bit(RBD_DEV_FLAG_BLACKLISTED
, &rbd_dev
->flags
);
3878 wake_requests(rbd_dev
, true);
3880 queue_delayed_work(rbd_dev
->task_wq
,
3881 &rbd_dev
->watch_dwork
,
3884 mutex_unlock(&rbd_dev
->watch_mutex
);
3888 rbd_dev
->watch_state
= RBD_WATCH_STATE_REGISTERED
;
3889 rbd_dev
->watch_cookie
= rbd_dev
->watch_handle
->linger_id
;
3890 mutex_unlock(&rbd_dev
->watch_mutex
);
3892 down_write(&rbd_dev
->lock_rwsem
);
3893 if (rbd_dev
->lock_state
== RBD_LOCK_STATE_LOCKED
)
3894 rbd_reacquire_lock(rbd_dev
);
3895 up_write(&rbd_dev
->lock_rwsem
);
3897 ret
= rbd_dev_refresh(rbd_dev
);
3899 rbd_warn(rbd_dev
, "reregisteration refresh failed: %d", ret
);
3903 * Synchronous osd object method call. Returns the number of bytes
3904 * returned in the outbound buffer, or a negative error code.
3906 static int rbd_obj_method_sync(struct rbd_device
*rbd_dev
,
3907 struct ceph_object_id
*oid
,
3908 struct ceph_object_locator
*oloc
,
3909 const char *method_name
,
3910 const void *outbound
,
3911 size_t outbound_size
,
3913 size_t inbound_size
)
3915 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
3916 struct page
*req_page
= NULL
;
3917 struct page
*reply_page
;
3921 * Method calls are ultimately read operations. The result
3922 * should placed into the inbound buffer provided. They
3923 * also supply outbound data--parameters for the object
3924 * method. Currently if this is present it will be a
3928 if (outbound_size
> PAGE_SIZE
)
3931 req_page
= alloc_page(GFP_KERNEL
);
3935 memcpy(page_address(req_page
), outbound
, outbound_size
);
3938 reply_page
= alloc_page(GFP_KERNEL
);
3941 __free_page(req_page
);
3945 ret
= ceph_osdc_call(osdc
, oid
, oloc
, RBD_DRV_NAME
, method_name
,
3946 CEPH_OSD_FLAG_READ
, req_page
, outbound_size
,
3947 reply_page
, &inbound_size
);
3949 memcpy(inbound
, page_address(reply_page
), inbound_size
);
3954 __free_page(req_page
);
3955 __free_page(reply_page
);
3960 * lock_rwsem must be held for read
3962 static void rbd_wait_state_locked(struct rbd_device
*rbd_dev
)
3968 * Note the use of mod_delayed_work() in rbd_acquire_lock()
3969 * and cancel_delayed_work() in wake_requests().
3971 dout("%s rbd_dev %p queueing lock_dwork\n", __func__
, rbd_dev
);
3972 queue_delayed_work(rbd_dev
->task_wq
, &rbd_dev
->lock_dwork
, 0);
3973 prepare_to_wait_exclusive(&rbd_dev
->lock_waitq
, &wait
,
3974 TASK_UNINTERRUPTIBLE
);
3975 up_read(&rbd_dev
->lock_rwsem
);
3977 down_read(&rbd_dev
->lock_rwsem
);
3978 } while (rbd_dev
->lock_state
!= RBD_LOCK_STATE_LOCKED
&&
3979 !test_bit(RBD_DEV_FLAG_BLACKLISTED
, &rbd_dev
->flags
));
3981 finish_wait(&rbd_dev
->lock_waitq
, &wait
);
3984 static void rbd_queue_workfn(struct work_struct
*work
)
3986 struct request
*rq
= blk_mq_rq_from_pdu(work
);
3987 struct rbd_device
*rbd_dev
= rq
->q
->queuedata
;
3988 struct rbd_img_request
*img_request
;
3989 struct ceph_snap_context
*snapc
= NULL
;
3990 u64 offset
= (u64
)blk_rq_pos(rq
) << SECTOR_SHIFT
;
3991 u64 length
= blk_rq_bytes(rq
);
3992 enum obj_operation_type op_type
;
3994 bool must_be_locked
;
3997 switch (req_op(rq
)) {
3998 case REQ_OP_DISCARD
:
3999 case REQ_OP_WRITE_ZEROES
:
4000 op_type
= OBJ_OP_DISCARD
;
4003 op_type
= OBJ_OP_WRITE
;
4006 op_type
= OBJ_OP_READ
;
4009 dout("%s: non-fs request type %d\n", __func__
, req_op(rq
));
4014 /* Ignore/skip any zero-length requests */
4017 dout("%s: zero-length request\n", __func__
);
4022 rbd_assert(op_type
== OBJ_OP_READ
||
4023 rbd_dev
->spec
->snap_id
== CEPH_NOSNAP
);
4026 * Quit early if the mapped snapshot no longer exists. It's
4027 * still possible the snapshot will have disappeared by the
4028 * time our request arrives at the osd, but there's no sense in
4029 * sending it if we already know.
4031 if (!test_bit(RBD_DEV_FLAG_EXISTS
, &rbd_dev
->flags
)) {
4032 dout("request for non-existent snapshot");
4033 rbd_assert(rbd_dev
->spec
->snap_id
!= CEPH_NOSNAP
);
4038 if (offset
&& length
> U64_MAX
- offset
+ 1) {
4039 rbd_warn(rbd_dev
, "bad request range (%llu~%llu)", offset
,
4042 goto err_rq
; /* Shouldn't happen */
4045 blk_mq_start_request(rq
);
4047 down_read(&rbd_dev
->header_rwsem
);
4048 mapping_size
= rbd_dev
->mapping
.size
;
4049 if (op_type
!= OBJ_OP_READ
) {
4050 snapc
= rbd_dev
->header
.snapc
;
4051 ceph_get_snap_context(snapc
);
4053 up_read(&rbd_dev
->header_rwsem
);
4055 if (offset
+ length
> mapping_size
) {
4056 rbd_warn(rbd_dev
, "beyond EOD (%llu~%llu > %llu)", offset
,
4057 length
, mapping_size
);
4063 (rbd_dev
->header
.features
& RBD_FEATURE_EXCLUSIVE_LOCK
) &&
4064 (op_type
!= OBJ_OP_READ
|| rbd_dev
->opts
->lock_on_read
);
4065 if (must_be_locked
) {
4066 down_read(&rbd_dev
->lock_rwsem
);
4067 if (rbd_dev
->lock_state
!= RBD_LOCK_STATE_LOCKED
&&
4068 !test_bit(RBD_DEV_FLAG_BLACKLISTED
, &rbd_dev
->flags
)) {
4069 if (rbd_dev
->opts
->exclusive
) {
4070 rbd_warn(rbd_dev
, "exclusive lock required");
4074 rbd_wait_state_locked(rbd_dev
);
4076 if (test_bit(RBD_DEV_FLAG_BLACKLISTED
, &rbd_dev
->flags
)) {
4077 result
= -EBLACKLISTED
;
4082 img_request
= rbd_img_request_create(rbd_dev
, offset
, length
, op_type
,
4088 img_request
->rq
= rq
;
4089 snapc
= NULL
; /* img_request consumes a ref */
4091 if (op_type
== OBJ_OP_DISCARD
)
4092 result
= rbd_img_request_fill(img_request
, OBJ_REQUEST_NODATA
,
4095 result
= rbd_img_request_fill(img_request
, OBJ_REQUEST_BIO
,
4098 goto err_img_request
;
4100 result
= rbd_img_request_submit(img_request
);
4102 goto err_img_request
;
4105 up_read(&rbd_dev
->lock_rwsem
);
4109 rbd_img_request_put(img_request
);
4112 up_read(&rbd_dev
->lock_rwsem
);
4115 rbd_warn(rbd_dev
, "%s %llx at %llx result %d",
4116 obj_op_name(op_type
), length
, offset
, result
);
4117 ceph_put_snap_context(snapc
);
4119 blk_mq_end_request(rq
, errno_to_blk_status(result
));
4122 static blk_status_t
rbd_queue_rq(struct blk_mq_hw_ctx
*hctx
,
4123 const struct blk_mq_queue_data
*bd
)
4125 struct request
*rq
= bd
->rq
;
4126 struct work_struct
*work
= blk_mq_rq_to_pdu(rq
);
4128 queue_work(rbd_wq
, work
);
4132 static void rbd_free_disk(struct rbd_device
*rbd_dev
)
4134 blk_cleanup_queue(rbd_dev
->disk
->queue
);
4135 blk_mq_free_tag_set(&rbd_dev
->tag_set
);
4136 put_disk(rbd_dev
->disk
);
4137 rbd_dev
->disk
= NULL
;
4140 static int rbd_obj_read_sync(struct rbd_device
*rbd_dev
,
4141 struct ceph_object_id
*oid
,
4142 struct ceph_object_locator
*oloc
,
4143 void *buf
, int buf_len
)
4146 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
4147 struct ceph_osd_request
*req
;
4148 struct page
**pages
;
4149 int num_pages
= calc_pages_for(0, buf_len
);
4152 req
= ceph_osdc_alloc_request(osdc
, NULL
, 1, false, GFP_KERNEL
);
4156 ceph_oid_copy(&req
->r_base_oid
, oid
);
4157 ceph_oloc_copy(&req
->r_base_oloc
, oloc
);
4158 req
->r_flags
= CEPH_OSD_FLAG_READ
;
4160 ret
= ceph_osdc_alloc_messages(req
, GFP_KERNEL
);
4164 pages
= ceph_alloc_page_vector(num_pages
, GFP_KERNEL
);
4165 if (IS_ERR(pages
)) {
4166 ret
= PTR_ERR(pages
);
4170 osd_req_op_extent_init(req
, 0, CEPH_OSD_OP_READ
, 0, buf_len
, 0, 0);
4171 osd_req_op_extent_osd_data_pages(req
, 0, pages
, buf_len
, 0, false,
4174 ceph_osdc_start_request(osdc
, req
, false);
4175 ret
= ceph_osdc_wait_request(osdc
, req
);
4177 ceph_copy_from_page_vector(pages
, buf
, 0, ret
);
4180 ceph_osdc_put_request(req
);
4185 * Read the complete header for the given rbd device. On successful
4186 * return, the rbd_dev->header field will contain up-to-date
4187 * information about the image.
4189 static int rbd_dev_v1_header_info(struct rbd_device
*rbd_dev
)
4191 struct rbd_image_header_ondisk
*ondisk
= NULL
;
4198 * The complete header will include an array of its 64-bit
4199 * snapshot ids, followed by the names of those snapshots as
4200 * a contiguous block of NUL-terminated strings. Note that
4201 * the number of snapshots could change by the time we read
4202 * it in, in which case we re-read it.
4209 size
= sizeof (*ondisk
);
4210 size
+= snap_count
* sizeof (struct rbd_image_snap_ondisk
);
4212 ondisk
= kmalloc(size
, GFP_KERNEL
);
4216 ret
= rbd_obj_read_sync(rbd_dev
, &rbd_dev
->header_oid
,
4217 &rbd_dev
->header_oloc
, ondisk
, size
);
4220 if ((size_t)ret
< size
) {
4222 rbd_warn(rbd_dev
, "short header read (want %zd got %d)",
4226 if (!rbd_dev_ondisk_valid(ondisk
)) {
4228 rbd_warn(rbd_dev
, "invalid header");
4232 names_size
= le64_to_cpu(ondisk
->snap_names_len
);
4233 want_count
= snap_count
;
4234 snap_count
= le32_to_cpu(ondisk
->snap_count
);
4235 } while (snap_count
!= want_count
);
4237 ret
= rbd_header_from_disk(rbd_dev
, ondisk
);
4245 * Clear the rbd device's EXISTS flag if the snapshot it's mapped to
4246 * has disappeared from the (just updated) snapshot context.
4248 static void rbd_exists_validate(struct rbd_device
*rbd_dev
)
4252 if (!test_bit(RBD_DEV_FLAG_EXISTS
, &rbd_dev
->flags
))
4255 snap_id
= rbd_dev
->spec
->snap_id
;
4256 if (snap_id
== CEPH_NOSNAP
)
4259 if (rbd_dev_snap_index(rbd_dev
, snap_id
) == BAD_SNAP_INDEX
)
4260 clear_bit(RBD_DEV_FLAG_EXISTS
, &rbd_dev
->flags
);
4263 static void rbd_dev_update_size(struct rbd_device
*rbd_dev
)
4268 * If EXISTS is not set, rbd_dev->disk may be NULL, so don't
4269 * try to update its size. If REMOVING is set, updating size
4270 * is just useless work since the device can't be opened.
4272 if (test_bit(RBD_DEV_FLAG_EXISTS
, &rbd_dev
->flags
) &&
4273 !test_bit(RBD_DEV_FLAG_REMOVING
, &rbd_dev
->flags
)) {
4274 size
= (sector_t
)rbd_dev
->mapping
.size
/ SECTOR_SIZE
;
4275 dout("setting size to %llu sectors", (unsigned long long)size
);
4276 set_capacity(rbd_dev
->disk
, size
);
4277 revalidate_disk(rbd_dev
->disk
);
4281 static int rbd_dev_refresh(struct rbd_device
*rbd_dev
)
4286 down_write(&rbd_dev
->header_rwsem
);
4287 mapping_size
= rbd_dev
->mapping
.size
;
4289 ret
= rbd_dev_header_info(rbd_dev
);
4294 * If there is a parent, see if it has disappeared due to the
4295 * mapped image getting flattened.
4297 if (rbd_dev
->parent
) {
4298 ret
= rbd_dev_v2_parent_info(rbd_dev
);
4303 if (rbd_dev
->spec
->snap_id
== CEPH_NOSNAP
) {
4304 rbd_dev
->mapping
.size
= rbd_dev
->header
.image_size
;
4306 /* validate mapped snapshot's EXISTS flag */
4307 rbd_exists_validate(rbd_dev
);
4311 up_write(&rbd_dev
->header_rwsem
);
4312 if (!ret
&& mapping_size
!= rbd_dev
->mapping
.size
)
4313 rbd_dev_update_size(rbd_dev
);
4318 static int rbd_init_request(struct blk_mq_tag_set
*set
, struct request
*rq
,
4319 unsigned int hctx_idx
, unsigned int numa_node
)
4321 struct work_struct
*work
= blk_mq_rq_to_pdu(rq
);
4323 INIT_WORK(work
, rbd_queue_workfn
);
4327 static const struct blk_mq_ops rbd_mq_ops
= {
4328 .queue_rq
= rbd_queue_rq
,
4329 .init_request
= rbd_init_request
,
4332 static int rbd_init_disk(struct rbd_device
*rbd_dev
)
4334 struct gendisk
*disk
;
4335 struct request_queue
*q
;
4339 /* create gendisk info */
4340 disk
= alloc_disk(single_major
?
4341 (1 << RBD_SINGLE_MAJOR_PART_SHIFT
) :
4342 RBD_MINORS_PER_MAJOR
);
4346 snprintf(disk
->disk_name
, sizeof(disk
->disk_name
), RBD_DRV_NAME
"%d",
4348 disk
->major
= rbd_dev
->major
;
4349 disk
->first_minor
= rbd_dev
->minor
;
4351 disk
->flags
|= GENHD_FL_EXT_DEVT
;
4352 disk
->fops
= &rbd_bd_ops
;
4353 disk
->private_data
= rbd_dev
;
4355 memset(&rbd_dev
->tag_set
, 0, sizeof(rbd_dev
->tag_set
));
4356 rbd_dev
->tag_set
.ops
= &rbd_mq_ops
;
4357 rbd_dev
->tag_set
.queue_depth
= rbd_dev
->opts
->queue_depth
;
4358 rbd_dev
->tag_set
.numa_node
= NUMA_NO_NODE
;
4359 rbd_dev
->tag_set
.flags
= BLK_MQ_F_SHOULD_MERGE
| BLK_MQ_F_SG_MERGE
;
4360 rbd_dev
->tag_set
.nr_hw_queues
= 1;
4361 rbd_dev
->tag_set
.cmd_size
= sizeof(struct work_struct
);
4363 err
= blk_mq_alloc_tag_set(&rbd_dev
->tag_set
);
4367 q
= blk_mq_init_queue(&rbd_dev
->tag_set
);
4373 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, q
);
4374 /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */
4376 /* set io sizes to object size */
4377 segment_size
= rbd_obj_bytes(&rbd_dev
->header
);
4378 blk_queue_max_hw_sectors(q
, segment_size
/ SECTOR_SIZE
);
4379 q
->limits
.max_sectors
= queue_max_hw_sectors(q
);
4380 blk_queue_max_segments(q
, USHRT_MAX
);
4381 blk_queue_max_segment_size(q
, segment_size
);
4382 blk_queue_io_min(q
, segment_size
);
4383 blk_queue_io_opt(q
, segment_size
);
4385 /* enable the discard support */
4386 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, q
);
4387 q
->limits
.discard_granularity
= segment_size
;
4388 blk_queue_max_discard_sectors(q
, segment_size
/ SECTOR_SIZE
);
4389 blk_queue_max_write_zeroes_sectors(q
, segment_size
/ SECTOR_SIZE
);
4391 if (!ceph_test_opt(rbd_dev
->rbd_client
->client
, NOCRC
))
4392 q
->backing_dev_info
->capabilities
|= BDI_CAP_STABLE_WRITES
;
4395 * disk_release() expects a queue ref from add_disk() and will
4396 * put it. Hold an extra ref until add_disk() is called.
4398 WARN_ON(!blk_get_queue(q
));
4400 q
->queuedata
= rbd_dev
;
4402 rbd_dev
->disk
= disk
;
4406 blk_mq_free_tag_set(&rbd_dev
->tag_set
);
4416 static struct rbd_device
*dev_to_rbd_dev(struct device
*dev
)
4418 return container_of(dev
, struct rbd_device
, dev
);
4421 static ssize_t
rbd_size_show(struct device
*dev
,
4422 struct device_attribute
*attr
, char *buf
)
4424 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4426 return sprintf(buf
, "%llu\n",
4427 (unsigned long long)rbd_dev
->mapping
.size
);
4431 * Note this shows the features for whatever's mapped, which is not
4432 * necessarily the base image.
4434 static ssize_t
rbd_features_show(struct device
*dev
,
4435 struct device_attribute
*attr
, char *buf
)
4437 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4439 return sprintf(buf
, "0x%016llx\n",
4440 (unsigned long long)rbd_dev
->mapping
.features
);
4443 static ssize_t
rbd_major_show(struct device
*dev
,
4444 struct device_attribute
*attr
, char *buf
)
4446 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4449 return sprintf(buf
, "%d\n", rbd_dev
->major
);
4451 return sprintf(buf
, "(none)\n");
4454 static ssize_t
rbd_minor_show(struct device
*dev
,
4455 struct device_attribute
*attr
, char *buf
)
4457 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4459 return sprintf(buf
, "%d\n", rbd_dev
->minor
);
4462 static ssize_t
rbd_client_addr_show(struct device
*dev
,
4463 struct device_attribute
*attr
, char *buf
)
4465 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4466 struct ceph_entity_addr
*client_addr
=
4467 ceph_client_addr(rbd_dev
->rbd_client
->client
);
4469 return sprintf(buf
, "%pISpc/%u\n", &client_addr
->in_addr
,
4470 le32_to_cpu(client_addr
->nonce
));
4473 static ssize_t
rbd_client_id_show(struct device
*dev
,
4474 struct device_attribute
*attr
, char *buf
)
4476 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4478 return sprintf(buf
, "client%lld\n",
4479 ceph_client_gid(rbd_dev
->rbd_client
->client
));
4482 static ssize_t
rbd_cluster_fsid_show(struct device
*dev
,
4483 struct device_attribute
*attr
, char *buf
)
4485 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4487 return sprintf(buf
, "%pU\n", &rbd_dev
->rbd_client
->client
->fsid
);
4490 static ssize_t
rbd_config_info_show(struct device
*dev
,
4491 struct device_attribute
*attr
, char *buf
)
4493 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4495 return sprintf(buf
, "%s\n", rbd_dev
->config_info
);
4498 static ssize_t
rbd_pool_show(struct device
*dev
,
4499 struct device_attribute
*attr
, char *buf
)
4501 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4503 return sprintf(buf
, "%s\n", rbd_dev
->spec
->pool_name
);
4506 static ssize_t
rbd_pool_id_show(struct device
*dev
,
4507 struct device_attribute
*attr
, char *buf
)
4509 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4511 return sprintf(buf
, "%llu\n",
4512 (unsigned long long) rbd_dev
->spec
->pool_id
);
4515 static ssize_t
rbd_name_show(struct device
*dev
,
4516 struct device_attribute
*attr
, char *buf
)
4518 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4520 if (rbd_dev
->spec
->image_name
)
4521 return sprintf(buf
, "%s\n", rbd_dev
->spec
->image_name
);
4523 return sprintf(buf
, "(unknown)\n");
4526 static ssize_t
rbd_image_id_show(struct device
*dev
,
4527 struct device_attribute
*attr
, char *buf
)
4529 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4531 return sprintf(buf
, "%s\n", rbd_dev
->spec
->image_id
);
4535 * Shows the name of the currently-mapped snapshot (or
4536 * RBD_SNAP_HEAD_NAME for the base image).
4538 static ssize_t
rbd_snap_show(struct device
*dev
,
4539 struct device_attribute
*attr
,
4542 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4544 return sprintf(buf
, "%s\n", rbd_dev
->spec
->snap_name
);
4547 static ssize_t
rbd_snap_id_show(struct device
*dev
,
4548 struct device_attribute
*attr
, char *buf
)
4550 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4552 return sprintf(buf
, "%llu\n", rbd_dev
->spec
->snap_id
);
4556 * For a v2 image, shows the chain of parent images, separated by empty
4557 * lines. For v1 images or if there is no parent, shows "(no parent
4560 static ssize_t
rbd_parent_show(struct device
*dev
,
4561 struct device_attribute
*attr
,
4564 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4567 if (!rbd_dev
->parent
)
4568 return sprintf(buf
, "(no parent image)\n");
4570 for ( ; rbd_dev
->parent
; rbd_dev
= rbd_dev
->parent
) {
4571 struct rbd_spec
*spec
= rbd_dev
->parent_spec
;
4573 count
+= sprintf(&buf
[count
], "%s"
4574 "pool_id %llu\npool_name %s\n"
4575 "image_id %s\nimage_name %s\n"
4576 "snap_id %llu\nsnap_name %s\n"
4578 !count
? "" : "\n", /* first? */
4579 spec
->pool_id
, spec
->pool_name
,
4580 spec
->image_id
, spec
->image_name
?: "(unknown)",
4581 spec
->snap_id
, spec
->snap_name
,
4582 rbd_dev
->parent_overlap
);
4588 static ssize_t
rbd_image_refresh(struct device
*dev
,
4589 struct device_attribute
*attr
,
4593 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4596 ret
= rbd_dev_refresh(rbd_dev
);
4603 static DEVICE_ATTR(size
, S_IRUGO
, rbd_size_show
, NULL
);
4604 static DEVICE_ATTR(features
, S_IRUGO
, rbd_features_show
, NULL
);
4605 static DEVICE_ATTR(major
, S_IRUGO
, rbd_major_show
, NULL
);
4606 static DEVICE_ATTR(minor
, S_IRUGO
, rbd_minor_show
, NULL
);
4607 static DEVICE_ATTR(client_addr
, S_IRUGO
, rbd_client_addr_show
, NULL
);
4608 static DEVICE_ATTR(client_id
, S_IRUGO
, rbd_client_id_show
, NULL
);
4609 static DEVICE_ATTR(cluster_fsid
, S_IRUGO
, rbd_cluster_fsid_show
, NULL
);
4610 static DEVICE_ATTR(config_info
, S_IRUSR
, rbd_config_info_show
, NULL
);
4611 static DEVICE_ATTR(pool
, S_IRUGO
, rbd_pool_show
, NULL
);
4612 static DEVICE_ATTR(pool_id
, S_IRUGO
, rbd_pool_id_show
, NULL
);
4613 static DEVICE_ATTR(name
, S_IRUGO
, rbd_name_show
, NULL
);
4614 static DEVICE_ATTR(image_id
, S_IRUGO
, rbd_image_id_show
, NULL
);
4615 static DEVICE_ATTR(refresh
, S_IWUSR
, NULL
, rbd_image_refresh
);
4616 static DEVICE_ATTR(current_snap
, S_IRUGO
, rbd_snap_show
, NULL
);
4617 static DEVICE_ATTR(snap_id
, S_IRUGO
, rbd_snap_id_show
, NULL
);
4618 static DEVICE_ATTR(parent
, S_IRUGO
, rbd_parent_show
, NULL
);
4620 static struct attribute
*rbd_attrs
[] = {
4621 &dev_attr_size
.attr
,
4622 &dev_attr_features
.attr
,
4623 &dev_attr_major
.attr
,
4624 &dev_attr_minor
.attr
,
4625 &dev_attr_client_addr
.attr
,
4626 &dev_attr_client_id
.attr
,
4627 &dev_attr_cluster_fsid
.attr
,
4628 &dev_attr_config_info
.attr
,
4629 &dev_attr_pool
.attr
,
4630 &dev_attr_pool_id
.attr
,
4631 &dev_attr_name
.attr
,
4632 &dev_attr_image_id
.attr
,
4633 &dev_attr_current_snap
.attr
,
4634 &dev_attr_snap_id
.attr
,
4635 &dev_attr_parent
.attr
,
4636 &dev_attr_refresh
.attr
,
4640 static struct attribute_group rbd_attr_group
= {
4644 static const struct attribute_group
*rbd_attr_groups
[] = {
4649 static void rbd_dev_release(struct device
*dev
);
4651 static const struct device_type rbd_device_type
= {
4653 .groups
= rbd_attr_groups
,
4654 .release
= rbd_dev_release
,
4657 static struct rbd_spec
*rbd_spec_get(struct rbd_spec
*spec
)
4659 kref_get(&spec
->kref
);
4664 static void rbd_spec_free(struct kref
*kref
);
4665 static void rbd_spec_put(struct rbd_spec
*spec
)
4668 kref_put(&spec
->kref
, rbd_spec_free
);
4671 static struct rbd_spec
*rbd_spec_alloc(void)
4673 struct rbd_spec
*spec
;
4675 spec
= kzalloc(sizeof (*spec
), GFP_KERNEL
);
4679 spec
->pool_id
= CEPH_NOPOOL
;
4680 spec
->snap_id
= CEPH_NOSNAP
;
4681 kref_init(&spec
->kref
);
4686 static void rbd_spec_free(struct kref
*kref
)
4688 struct rbd_spec
*spec
= container_of(kref
, struct rbd_spec
, kref
);
4690 kfree(spec
->pool_name
);
4691 kfree(spec
->image_id
);
4692 kfree(spec
->image_name
);
4693 kfree(spec
->snap_name
);
4697 static void rbd_dev_free(struct rbd_device
*rbd_dev
)
4699 WARN_ON(rbd_dev
->watch_state
!= RBD_WATCH_STATE_UNREGISTERED
);
4700 WARN_ON(rbd_dev
->lock_state
!= RBD_LOCK_STATE_UNLOCKED
);
4702 ceph_oid_destroy(&rbd_dev
->header_oid
);
4703 ceph_oloc_destroy(&rbd_dev
->header_oloc
);
4704 kfree(rbd_dev
->config_info
);
4706 rbd_put_client(rbd_dev
->rbd_client
);
4707 rbd_spec_put(rbd_dev
->spec
);
4708 kfree(rbd_dev
->opts
);
4712 static void rbd_dev_release(struct device
*dev
)
4714 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4715 bool need_put
= !!rbd_dev
->opts
;
4718 destroy_workqueue(rbd_dev
->task_wq
);
4719 ida_simple_remove(&rbd_dev_id_ida
, rbd_dev
->dev_id
);
4722 rbd_dev_free(rbd_dev
);
4725 * This is racy, but way better than putting module outside of
4726 * the release callback. The race window is pretty small, so
4727 * doing something similar to dm (dm-builtin.c) is overkill.
4730 module_put(THIS_MODULE
);
4733 static struct rbd_device
*__rbd_dev_create(struct rbd_client
*rbdc
,
4734 struct rbd_spec
*spec
)
4736 struct rbd_device
*rbd_dev
;
4738 rbd_dev
= kzalloc(sizeof(*rbd_dev
), GFP_KERNEL
);
4742 spin_lock_init(&rbd_dev
->lock
);
4743 INIT_LIST_HEAD(&rbd_dev
->node
);
4744 init_rwsem(&rbd_dev
->header_rwsem
);
4746 rbd_dev
->header
.data_pool_id
= CEPH_NOPOOL
;
4747 ceph_oid_init(&rbd_dev
->header_oid
);
4748 rbd_dev
->header_oloc
.pool
= spec
->pool_id
;
4750 mutex_init(&rbd_dev
->watch_mutex
);
4751 rbd_dev
->watch_state
= RBD_WATCH_STATE_UNREGISTERED
;
4752 INIT_DELAYED_WORK(&rbd_dev
->watch_dwork
, rbd_reregister_watch
);
4754 init_rwsem(&rbd_dev
->lock_rwsem
);
4755 rbd_dev
->lock_state
= RBD_LOCK_STATE_UNLOCKED
;
4756 INIT_WORK(&rbd_dev
->acquired_lock_work
, rbd_notify_acquired_lock
);
4757 INIT_WORK(&rbd_dev
->released_lock_work
, rbd_notify_released_lock
);
4758 INIT_DELAYED_WORK(&rbd_dev
->lock_dwork
, rbd_acquire_lock
);
4759 INIT_WORK(&rbd_dev
->unlock_work
, rbd_release_lock_work
);
4760 init_waitqueue_head(&rbd_dev
->lock_waitq
);
4762 rbd_dev
->dev
.bus
= &rbd_bus_type
;
4763 rbd_dev
->dev
.type
= &rbd_device_type
;
4764 rbd_dev
->dev
.parent
= &rbd_root_dev
;
4765 device_initialize(&rbd_dev
->dev
);
4767 rbd_dev
->rbd_client
= rbdc
;
4768 rbd_dev
->spec
= spec
;
4774 * Create a mapping rbd_dev.
4776 static struct rbd_device
*rbd_dev_create(struct rbd_client
*rbdc
,
4777 struct rbd_spec
*spec
,
4778 struct rbd_options
*opts
)
4780 struct rbd_device
*rbd_dev
;
4782 rbd_dev
= __rbd_dev_create(rbdc
, spec
);
4786 rbd_dev
->opts
= opts
;
4788 /* get an id and fill in device name */
4789 rbd_dev
->dev_id
= ida_simple_get(&rbd_dev_id_ida
, 0,
4790 minor_to_rbd_dev_id(1 << MINORBITS
),
4792 if (rbd_dev
->dev_id
< 0)
4795 sprintf(rbd_dev
->name
, RBD_DRV_NAME
"%d", rbd_dev
->dev_id
);
4796 rbd_dev
->task_wq
= alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM
,
4798 if (!rbd_dev
->task_wq
)
4801 /* we have a ref from do_rbd_add() */
4802 __module_get(THIS_MODULE
);
4804 dout("%s rbd_dev %p dev_id %d\n", __func__
, rbd_dev
, rbd_dev
->dev_id
);
4808 ida_simple_remove(&rbd_dev_id_ida
, rbd_dev
->dev_id
);
4810 rbd_dev_free(rbd_dev
);
4814 static void rbd_dev_destroy(struct rbd_device
*rbd_dev
)
4817 put_device(&rbd_dev
->dev
);
4821 * Get the size and object order for an image snapshot, or if
4822 * snap_id is CEPH_NOSNAP, gets this information for the base
4825 static int _rbd_dev_v2_snap_size(struct rbd_device
*rbd_dev
, u64 snap_id
,
4826 u8
*order
, u64
*snap_size
)
4828 __le64 snapid
= cpu_to_le64(snap_id
);
4833 } __attribute__ ((packed
)) size_buf
= { 0 };
4835 ret
= rbd_obj_method_sync(rbd_dev
, &rbd_dev
->header_oid
,
4836 &rbd_dev
->header_oloc
, "get_size",
4837 &snapid
, sizeof(snapid
),
4838 &size_buf
, sizeof(size_buf
));
4839 dout("%s: rbd_obj_method_sync returned %d\n", __func__
, ret
);
4842 if (ret
< sizeof (size_buf
))
4846 *order
= size_buf
.order
;
4847 dout(" order %u", (unsigned int)*order
);
4849 *snap_size
= le64_to_cpu(size_buf
.size
);
4851 dout(" snap_id 0x%016llx snap_size = %llu\n",
4852 (unsigned long long)snap_id
,
4853 (unsigned long long)*snap_size
);
4858 static int rbd_dev_v2_image_size(struct rbd_device
*rbd_dev
)
4860 return _rbd_dev_v2_snap_size(rbd_dev
, CEPH_NOSNAP
,
4861 &rbd_dev
->header
.obj_order
,
4862 &rbd_dev
->header
.image_size
);
4865 static int rbd_dev_v2_object_prefix(struct rbd_device
*rbd_dev
)
4871 reply_buf
= kzalloc(RBD_OBJ_PREFIX_LEN_MAX
, GFP_KERNEL
);
4875 ret
= rbd_obj_method_sync(rbd_dev
, &rbd_dev
->header_oid
,
4876 &rbd_dev
->header_oloc
, "get_object_prefix",
4877 NULL
, 0, reply_buf
, RBD_OBJ_PREFIX_LEN_MAX
);
4878 dout("%s: rbd_obj_method_sync returned %d\n", __func__
, ret
);
4883 rbd_dev
->header
.object_prefix
= ceph_extract_encoded_string(&p
,
4884 p
+ ret
, NULL
, GFP_NOIO
);
4887 if (IS_ERR(rbd_dev
->header
.object_prefix
)) {
4888 ret
= PTR_ERR(rbd_dev
->header
.object_prefix
);
4889 rbd_dev
->header
.object_prefix
= NULL
;
4891 dout(" object_prefix = %s\n", rbd_dev
->header
.object_prefix
);
4899 static int _rbd_dev_v2_snap_features(struct rbd_device
*rbd_dev
, u64 snap_id
,
4902 __le64 snapid
= cpu_to_le64(snap_id
);
4906 } __attribute__ ((packed
)) features_buf
= { 0 };
4910 ret
= rbd_obj_method_sync(rbd_dev
, &rbd_dev
->header_oid
,
4911 &rbd_dev
->header_oloc
, "get_features",
4912 &snapid
, sizeof(snapid
),
4913 &features_buf
, sizeof(features_buf
));
4914 dout("%s: rbd_obj_method_sync returned %d\n", __func__
, ret
);
4917 if (ret
< sizeof (features_buf
))
4920 unsup
= le64_to_cpu(features_buf
.incompat
) & ~RBD_FEATURES_SUPPORTED
;
4922 rbd_warn(rbd_dev
, "image uses unsupported features: 0x%llx",
4927 *snap_features
= le64_to_cpu(features_buf
.features
);
4929 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
4930 (unsigned long long)snap_id
,
4931 (unsigned long long)*snap_features
,
4932 (unsigned long long)le64_to_cpu(features_buf
.incompat
));
4937 static int rbd_dev_v2_features(struct rbd_device
*rbd_dev
)
4939 return _rbd_dev_v2_snap_features(rbd_dev
, CEPH_NOSNAP
,
4940 &rbd_dev
->header
.features
);
4943 static int rbd_dev_v2_parent_info(struct rbd_device
*rbd_dev
)
4945 struct rbd_spec
*parent_spec
;
4947 void *reply_buf
= NULL
;
4957 parent_spec
= rbd_spec_alloc();
4961 size
= sizeof (__le64
) + /* pool_id */
4962 sizeof (__le32
) + RBD_IMAGE_ID_LEN_MAX
+ /* image_id */
4963 sizeof (__le64
) + /* snap_id */
4964 sizeof (__le64
); /* overlap */
4965 reply_buf
= kmalloc(size
, GFP_KERNEL
);
4971 snapid
= cpu_to_le64(rbd_dev
->spec
->snap_id
);
4972 ret
= rbd_obj_method_sync(rbd_dev
, &rbd_dev
->header_oid
,
4973 &rbd_dev
->header_oloc
, "get_parent",
4974 &snapid
, sizeof(snapid
), reply_buf
, size
);
4975 dout("%s: rbd_obj_method_sync returned %d\n", __func__
, ret
);
4980 end
= reply_buf
+ ret
;
4982 ceph_decode_64_safe(&p
, end
, pool_id
, out_err
);
4983 if (pool_id
== CEPH_NOPOOL
) {
4985 * Either the parent never existed, or we have
4986 * record of it but the image got flattened so it no
4987 * longer has a parent. When the parent of a
4988 * layered image disappears we immediately set the
4989 * overlap to 0. The effect of this is that all new
4990 * requests will be treated as if the image had no
4993 if (rbd_dev
->parent_overlap
) {
4994 rbd_dev
->parent_overlap
= 0;
4995 rbd_dev_parent_put(rbd_dev
);
4996 pr_info("%s: clone image has been flattened\n",
4997 rbd_dev
->disk
->disk_name
);
5000 goto out
; /* No parent? No problem. */
5003 /* The ceph file layout needs to fit pool id in 32 bits */
5006 if (pool_id
> (u64
)U32_MAX
) {
5007 rbd_warn(NULL
, "parent pool id too large (%llu > %u)",
5008 (unsigned long long)pool_id
, U32_MAX
);
5012 image_id
= ceph_extract_encoded_string(&p
, end
, NULL
, GFP_KERNEL
);
5013 if (IS_ERR(image_id
)) {
5014 ret
= PTR_ERR(image_id
);
5017 ceph_decode_64_safe(&p
, end
, snap_id
, out_err
);
5018 ceph_decode_64_safe(&p
, end
, overlap
, out_err
);
5021 * The parent won't change (except when the clone is
5022 * flattened, already handled that). So we only need to
5023 * record the parent spec we have not already done so.
5025 if (!rbd_dev
->parent_spec
) {
5026 parent_spec
->pool_id
= pool_id
;
5027 parent_spec
->image_id
= image_id
;
5028 parent_spec
->snap_id
= snap_id
;
5029 rbd_dev
->parent_spec
= parent_spec
;
5030 parent_spec
= NULL
; /* rbd_dev now owns this */
5036 * We always update the parent overlap. If it's zero we issue
5037 * a warning, as we will proceed as if there was no parent.
5041 /* refresh, careful to warn just once */
5042 if (rbd_dev
->parent_overlap
)
5044 "clone now standalone (overlap became 0)");
5047 rbd_warn(rbd_dev
, "clone is standalone (overlap 0)");
5050 rbd_dev
->parent_overlap
= overlap
;
5056 rbd_spec_put(parent_spec
);
5061 static int rbd_dev_v2_striping_info(struct rbd_device
*rbd_dev
)
5065 __le64 stripe_count
;
5066 } __attribute__ ((packed
)) striping_info_buf
= { 0 };
5067 size_t size
= sizeof (striping_info_buf
);
5074 ret
= rbd_obj_method_sync(rbd_dev
, &rbd_dev
->header_oid
,
5075 &rbd_dev
->header_oloc
, "get_stripe_unit_count",
5076 NULL
, 0, &striping_info_buf
, size
);
5077 dout("%s: rbd_obj_method_sync returned %d\n", __func__
, ret
);
5084 * We don't actually support the "fancy striping" feature
5085 * (STRIPINGV2) yet, but if the striping sizes are the
5086 * defaults the behavior is the same as before. So find
5087 * out, and only fail if the image has non-default values.
5090 obj_size
= rbd_obj_bytes(&rbd_dev
->header
);
5091 p
= &striping_info_buf
;
5092 stripe_unit
= ceph_decode_64(&p
);
5093 if (stripe_unit
!= obj_size
) {
5094 rbd_warn(rbd_dev
, "unsupported stripe unit "
5095 "(got %llu want %llu)",
5096 stripe_unit
, obj_size
);
5099 stripe_count
= ceph_decode_64(&p
);
5100 if (stripe_count
!= 1) {
5101 rbd_warn(rbd_dev
, "unsupported stripe count "
5102 "(got %llu want 1)", stripe_count
);
5105 rbd_dev
->header
.stripe_unit
= stripe_unit
;
5106 rbd_dev
->header
.stripe_count
= stripe_count
;
5111 static int rbd_dev_v2_data_pool(struct rbd_device
*rbd_dev
)
5113 __le64 data_pool_id
;
5116 ret
= rbd_obj_method_sync(rbd_dev
, &rbd_dev
->header_oid
,
5117 &rbd_dev
->header_oloc
, "get_data_pool",
5118 NULL
, 0, &data_pool_id
, sizeof(data_pool_id
));
5121 if (ret
< sizeof(data_pool_id
))
5124 rbd_dev
->header
.data_pool_id
= le64_to_cpu(data_pool_id
);
5125 WARN_ON(rbd_dev
->header
.data_pool_id
== CEPH_NOPOOL
);
5129 static char *rbd_dev_image_name(struct rbd_device
*rbd_dev
)
5131 CEPH_DEFINE_OID_ONSTACK(oid
);
5132 size_t image_id_size
;
5137 void *reply_buf
= NULL
;
5139 char *image_name
= NULL
;
5142 rbd_assert(!rbd_dev
->spec
->image_name
);
5144 len
= strlen(rbd_dev
->spec
->image_id
);
5145 image_id_size
= sizeof (__le32
) + len
;
5146 image_id
= kmalloc(image_id_size
, GFP_KERNEL
);
5151 end
= image_id
+ image_id_size
;
5152 ceph_encode_string(&p
, end
, rbd_dev
->spec
->image_id
, (u32
)len
);
5154 size
= sizeof (__le32
) + RBD_IMAGE_NAME_LEN_MAX
;
5155 reply_buf
= kmalloc(size
, GFP_KERNEL
);
5159 ceph_oid_printf(&oid
, "%s", RBD_DIRECTORY
);
5160 ret
= rbd_obj_method_sync(rbd_dev
, &oid
, &rbd_dev
->header_oloc
,
5161 "dir_get_name", image_id
, image_id_size
,
5166 end
= reply_buf
+ ret
;
5168 image_name
= ceph_extract_encoded_string(&p
, end
, &len
, GFP_KERNEL
);
5169 if (IS_ERR(image_name
))
5172 dout("%s: name is %s len is %zd\n", __func__
, image_name
, len
);
5180 static u64
rbd_v1_snap_id_by_name(struct rbd_device
*rbd_dev
, const char *name
)
5182 struct ceph_snap_context
*snapc
= rbd_dev
->header
.snapc
;
5183 const char *snap_name
;
5186 /* Skip over names until we find the one we are looking for */
5188 snap_name
= rbd_dev
->header
.snap_names
;
5189 while (which
< snapc
->num_snaps
) {
5190 if (!strcmp(name
, snap_name
))
5191 return snapc
->snaps
[which
];
5192 snap_name
+= strlen(snap_name
) + 1;
5198 static u64
rbd_v2_snap_id_by_name(struct rbd_device
*rbd_dev
, const char *name
)
5200 struct ceph_snap_context
*snapc
= rbd_dev
->header
.snapc
;
5205 for (which
= 0; !found
&& which
< snapc
->num_snaps
; which
++) {
5206 const char *snap_name
;
5208 snap_id
= snapc
->snaps
[which
];
5209 snap_name
= rbd_dev_v2_snap_name(rbd_dev
, snap_id
);
5210 if (IS_ERR(snap_name
)) {
5211 /* ignore no-longer existing snapshots */
5212 if (PTR_ERR(snap_name
) == -ENOENT
)
5217 found
= !strcmp(name
, snap_name
);
5220 return found
? snap_id
: CEPH_NOSNAP
;
5224 * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
5225 * no snapshot by that name is found, or if an error occurs.
5227 static u64
rbd_snap_id_by_name(struct rbd_device
*rbd_dev
, const char *name
)
5229 if (rbd_dev
->image_format
== 1)
5230 return rbd_v1_snap_id_by_name(rbd_dev
, name
);
5232 return rbd_v2_snap_id_by_name(rbd_dev
, name
);
5236 * An image being mapped will have everything but the snap id.
5238 static int rbd_spec_fill_snap_id(struct rbd_device
*rbd_dev
)
5240 struct rbd_spec
*spec
= rbd_dev
->spec
;
5242 rbd_assert(spec
->pool_id
!= CEPH_NOPOOL
&& spec
->pool_name
);
5243 rbd_assert(spec
->image_id
&& spec
->image_name
);
5244 rbd_assert(spec
->snap_name
);
5246 if (strcmp(spec
->snap_name
, RBD_SNAP_HEAD_NAME
)) {
5249 snap_id
= rbd_snap_id_by_name(rbd_dev
, spec
->snap_name
);
5250 if (snap_id
== CEPH_NOSNAP
)
5253 spec
->snap_id
= snap_id
;
5255 spec
->snap_id
= CEPH_NOSNAP
;
5262 * A parent image will have all ids but none of the names.
5264 * All names in an rbd spec are dynamically allocated. It's OK if we
5265 * can't figure out the name for an image id.
5267 static int rbd_spec_fill_names(struct rbd_device
*rbd_dev
)
5269 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
5270 struct rbd_spec
*spec
= rbd_dev
->spec
;
5271 const char *pool_name
;
5272 const char *image_name
;
5273 const char *snap_name
;
5276 rbd_assert(spec
->pool_id
!= CEPH_NOPOOL
);
5277 rbd_assert(spec
->image_id
);
5278 rbd_assert(spec
->snap_id
!= CEPH_NOSNAP
);
5280 /* Get the pool name; we have to make our own copy of this */
5282 pool_name
= ceph_pg_pool_name_by_id(osdc
->osdmap
, spec
->pool_id
);
5284 rbd_warn(rbd_dev
, "no pool with id %llu", spec
->pool_id
);
5287 pool_name
= kstrdup(pool_name
, GFP_KERNEL
);
5291 /* Fetch the image name; tolerate failure here */
5293 image_name
= rbd_dev_image_name(rbd_dev
);
5295 rbd_warn(rbd_dev
, "unable to get image name");
5297 /* Fetch the snapshot name */
5299 snap_name
= rbd_snap_name(rbd_dev
, spec
->snap_id
);
5300 if (IS_ERR(snap_name
)) {
5301 ret
= PTR_ERR(snap_name
);
5305 spec
->pool_name
= pool_name
;
5306 spec
->image_name
= image_name
;
5307 spec
->snap_name
= snap_name
;
5317 static int rbd_dev_v2_snap_context(struct rbd_device
*rbd_dev
)
5326 struct ceph_snap_context
*snapc
;
5330 * We'll need room for the seq value (maximum snapshot id),
5331 * snapshot count, and array of that many snapshot ids.
5332 * For now we have a fixed upper limit on the number we're
5333 * prepared to receive.
5335 size
= sizeof (__le64
) + sizeof (__le32
) +
5336 RBD_MAX_SNAP_COUNT
* sizeof (__le64
);
5337 reply_buf
= kzalloc(size
, GFP_KERNEL
);
5341 ret
= rbd_obj_method_sync(rbd_dev
, &rbd_dev
->header_oid
,
5342 &rbd_dev
->header_oloc
, "get_snapcontext",
5343 NULL
, 0, reply_buf
, size
);
5344 dout("%s: rbd_obj_method_sync returned %d\n", __func__
, ret
);
5349 end
= reply_buf
+ ret
;
5351 ceph_decode_64_safe(&p
, end
, seq
, out
);
5352 ceph_decode_32_safe(&p
, end
, snap_count
, out
);
5355 * Make sure the reported number of snapshot ids wouldn't go
5356 * beyond the end of our buffer. But before checking that,
5357 * make sure the computed size of the snapshot context we
5358 * allocate is representable in a size_t.
5360 if (snap_count
> (SIZE_MAX
- sizeof (struct ceph_snap_context
))
5365 if (!ceph_has_room(&p
, end
, snap_count
* sizeof (__le64
)))
5369 snapc
= ceph_create_snap_context(snap_count
, GFP_KERNEL
);
5375 for (i
= 0; i
< snap_count
; i
++)
5376 snapc
->snaps
[i
] = ceph_decode_64(&p
);
5378 ceph_put_snap_context(rbd_dev
->header
.snapc
);
5379 rbd_dev
->header
.snapc
= snapc
;
5381 dout(" snap context seq = %llu, snap_count = %u\n",
5382 (unsigned long long)seq
, (unsigned int)snap_count
);
5389 static const char *rbd_dev_v2_snap_name(struct rbd_device
*rbd_dev
,
5400 size
= sizeof (__le32
) + RBD_MAX_SNAP_NAME_LEN
;
5401 reply_buf
= kmalloc(size
, GFP_KERNEL
);
5403 return ERR_PTR(-ENOMEM
);
5405 snapid
= cpu_to_le64(snap_id
);
5406 ret
= rbd_obj_method_sync(rbd_dev
, &rbd_dev
->header_oid
,
5407 &rbd_dev
->header_oloc
, "get_snapshot_name",
5408 &snapid
, sizeof(snapid
), reply_buf
, size
);
5409 dout("%s: rbd_obj_method_sync returned %d\n", __func__
, ret
);
5411 snap_name
= ERR_PTR(ret
);
5416 end
= reply_buf
+ ret
;
5417 snap_name
= ceph_extract_encoded_string(&p
, end
, NULL
, GFP_KERNEL
);
5418 if (IS_ERR(snap_name
))
5421 dout(" snap_id 0x%016llx snap_name = %s\n",
5422 (unsigned long long)snap_id
, snap_name
);
5429 static int rbd_dev_v2_header_info(struct rbd_device
*rbd_dev
)
5431 bool first_time
= rbd_dev
->header
.object_prefix
== NULL
;
5434 ret
= rbd_dev_v2_image_size(rbd_dev
);
5439 ret
= rbd_dev_v2_header_onetime(rbd_dev
);
5444 ret
= rbd_dev_v2_snap_context(rbd_dev
);
5445 if (ret
&& first_time
) {
5446 kfree(rbd_dev
->header
.object_prefix
);
5447 rbd_dev
->header
.object_prefix
= NULL
;
5453 static int rbd_dev_header_info(struct rbd_device
*rbd_dev
)
5455 rbd_assert(rbd_image_format_valid(rbd_dev
->image_format
));
5457 if (rbd_dev
->image_format
== 1)
5458 return rbd_dev_v1_header_info(rbd_dev
);
5460 return rbd_dev_v2_header_info(rbd_dev
);
5464 * Skips over white space at *buf, and updates *buf to point to the
5465 * first found non-space character (if any). Returns the length of
5466 * the token (string of non-white space characters) found. Note
5467 * that *buf must be terminated with '\0'.
5469 static inline size_t next_token(const char **buf
)
5472 * These are the characters that produce nonzero for
5473 * isspace() in the "C" and "POSIX" locales.
5475 const char *spaces
= " \f\n\r\t\v";
5477 *buf
+= strspn(*buf
, spaces
); /* Find start of token */
5479 return strcspn(*buf
, spaces
); /* Return token length */
5483 * Finds the next token in *buf, dynamically allocates a buffer big
5484 * enough to hold a copy of it, and copies the token into the new
5485 * buffer. The copy is guaranteed to be terminated with '\0'. Note
5486 * that a duplicate buffer is created even for a zero-length token.
5488 * Returns a pointer to the newly-allocated duplicate, or a null
5489 * pointer if memory for the duplicate was not available. If
5490 * the lenp argument is a non-null pointer, the length of the token
5491 * (not including the '\0') is returned in *lenp.
5493 * If successful, the *buf pointer will be updated to point beyond
5494 * the end of the found token.
5496 * Note: uses GFP_KERNEL for allocation.
5498 static inline char *dup_token(const char **buf
, size_t *lenp
)
5503 len
= next_token(buf
);
5504 dup
= kmemdup(*buf
, len
+ 1, GFP_KERNEL
);
5507 *(dup
+ len
) = '\0';
5517 * Parse the options provided for an "rbd add" (i.e., rbd image
5518 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
5519 * and the data written is passed here via a NUL-terminated buffer.
5520 * Returns 0 if successful or an error code otherwise.
5522 * The information extracted from these options is recorded in
5523 * the other parameters which return dynamically-allocated
5526 * The address of a pointer that will refer to a ceph options
5527 * structure. Caller must release the returned pointer using
5528 * ceph_destroy_options() when it is no longer needed.
5530 * Address of an rbd options pointer. Fully initialized by
5531 * this function; caller must release with kfree().
5533 * Address of an rbd image specification pointer. Fully
5534 * initialized by this function based on parsed options.
5535 * Caller must release with rbd_spec_put().
5537 * The options passed take this form:
5538 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
5541 * A comma-separated list of one or more monitor addresses.
5542 * A monitor address is an ip address, optionally followed
5543 * by a port number (separated by a colon).
5544 * I.e.: ip1[:port1][,ip2[:port2]...]
5546 * A comma-separated list of ceph and/or rbd options.
5548 * The name of the rados pool containing the rbd image.
5550 * The name of the image in that pool to map.
5552 * An optional snapshot id. If provided, the mapping will
5553 * present data from the image at the time that snapshot was
5554 * created. The image head is used if no snapshot id is
5555 * provided. Snapshot mappings are always read-only.
5557 static int rbd_add_parse_args(const char *buf
,
5558 struct ceph_options
**ceph_opts
,
5559 struct rbd_options
**opts
,
5560 struct rbd_spec
**rbd_spec
)
5564 const char *mon_addrs
;
5566 size_t mon_addrs_size
;
5567 struct rbd_spec
*spec
= NULL
;
5568 struct rbd_options
*rbd_opts
= NULL
;
5569 struct ceph_options
*copts
;
5572 /* The first four tokens are required */
5574 len
= next_token(&buf
);
5576 rbd_warn(NULL
, "no monitor address(es) provided");
5580 mon_addrs_size
= len
+ 1;
5584 options
= dup_token(&buf
, NULL
);
5588 rbd_warn(NULL
, "no options provided");
5592 spec
= rbd_spec_alloc();
5596 spec
->pool_name
= dup_token(&buf
, NULL
);
5597 if (!spec
->pool_name
)
5599 if (!*spec
->pool_name
) {
5600 rbd_warn(NULL
, "no pool name provided");
5604 spec
->image_name
= dup_token(&buf
, NULL
);
5605 if (!spec
->image_name
)
5607 if (!*spec
->image_name
) {
5608 rbd_warn(NULL
, "no image name provided");
5613 * Snapshot name is optional; default is to use "-"
5614 * (indicating the head/no snapshot).
5616 len
= next_token(&buf
);
5618 buf
= RBD_SNAP_HEAD_NAME
; /* No snapshot supplied */
5619 len
= sizeof (RBD_SNAP_HEAD_NAME
) - 1;
5620 } else if (len
> RBD_MAX_SNAP_NAME_LEN
) {
5621 ret
= -ENAMETOOLONG
;
5624 snap_name
= kmemdup(buf
, len
+ 1, GFP_KERNEL
);
5627 *(snap_name
+ len
) = '\0';
5628 spec
->snap_name
= snap_name
;
5630 /* Initialize all rbd options to the defaults */
5632 rbd_opts
= kzalloc(sizeof (*rbd_opts
), GFP_KERNEL
);
5636 rbd_opts
->read_only
= RBD_READ_ONLY_DEFAULT
;
5637 rbd_opts
->queue_depth
= RBD_QUEUE_DEPTH_DEFAULT
;
5638 rbd_opts
->lock_on_read
= RBD_LOCK_ON_READ_DEFAULT
;
5639 rbd_opts
->exclusive
= RBD_EXCLUSIVE_DEFAULT
;
5641 copts
= ceph_parse_options(options
, mon_addrs
,
5642 mon_addrs
+ mon_addrs_size
- 1,
5643 parse_rbd_opts_token
, rbd_opts
);
5644 if (IS_ERR(copts
)) {
5645 ret
= PTR_ERR(copts
);
5666 * Return pool id (>= 0) or a negative error code.
5668 static int rbd_add_get_pool_id(struct rbd_client
*rbdc
, const char *pool_name
)
5670 struct ceph_options
*opts
= rbdc
->client
->options
;
5676 ret
= ceph_pg_poolid_by_name(rbdc
->client
->osdc
.osdmap
, pool_name
);
5677 if (ret
== -ENOENT
&& tries
++ < 1) {
5678 ret
= ceph_monc_get_version(&rbdc
->client
->monc
, "osdmap",
5683 if (rbdc
->client
->osdc
.osdmap
->epoch
< newest_epoch
) {
5684 ceph_osdc_maybe_request_map(&rbdc
->client
->osdc
);
5685 (void) ceph_monc_wait_osdmap(&rbdc
->client
->monc
,
5687 opts
->mount_timeout
);
5690 /* the osdmap we have is new enough */
5698 static void rbd_dev_image_unlock(struct rbd_device
*rbd_dev
)
5700 down_write(&rbd_dev
->lock_rwsem
);
5701 if (__rbd_is_lock_owner(rbd_dev
))
5702 rbd_unlock(rbd_dev
);
5703 up_write(&rbd_dev
->lock_rwsem
);
5706 static int rbd_add_acquire_lock(struct rbd_device
*rbd_dev
)
5708 if (!(rbd_dev
->header
.features
& RBD_FEATURE_EXCLUSIVE_LOCK
)) {
5709 rbd_warn(rbd_dev
, "exclusive-lock feature is not enabled");
5713 /* FIXME: "rbd map --exclusive" should be in interruptible */
5714 down_read(&rbd_dev
->lock_rwsem
);
5715 rbd_wait_state_locked(rbd_dev
);
5716 up_read(&rbd_dev
->lock_rwsem
);
5717 if (test_bit(RBD_DEV_FLAG_BLACKLISTED
, &rbd_dev
->flags
)) {
5718 rbd_warn(rbd_dev
, "failed to acquire exclusive lock");
5726 * An rbd format 2 image has a unique identifier, distinct from the
5727 * name given to it by the user. Internally, that identifier is
5728 * what's used to specify the names of objects related to the image.
5730 * A special "rbd id" object is used to map an rbd image name to its
5731 * id. If that object doesn't exist, then there is no v2 rbd image
5732 * with the supplied name.
5734 * This function will record the given rbd_dev's image_id field if
5735 * it can be determined, and in that case will return 0. If any
5736 * errors occur a negative errno will be returned and the rbd_dev's
5737 * image_id field will be unchanged (and should be NULL).
5739 static int rbd_dev_image_id(struct rbd_device
*rbd_dev
)
5743 CEPH_DEFINE_OID_ONSTACK(oid
);
5748 * When probing a parent image, the image id is already
5749 * known (and the image name likely is not). There's no
5750 * need to fetch the image id again in this case. We
5751 * do still need to set the image format though.
5753 if (rbd_dev
->spec
->image_id
) {
5754 rbd_dev
->image_format
= *rbd_dev
->spec
->image_id
? 2 : 1;
5760 * First, see if the format 2 image id file exists, and if
5761 * so, get the image's persistent id from it.
5763 ret
= ceph_oid_aprintf(&oid
, GFP_KERNEL
, "%s%s", RBD_ID_PREFIX
,
5764 rbd_dev
->spec
->image_name
);
5768 dout("rbd id object name is %s\n", oid
.name
);
5770 /* Response will be an encoded string, which includes a length */
5772 size
= sizeof (__le32
) + RBD_IMAGE_ID_LEN_MAX
;
5773 response
= kzalloc(size
, GFP_NOIO
);
5779 /* If it doesn't exist we'll assume it's a format 1 image */
5781 ret
= rbd_obj_method_sync(rbd_dev
, &oid
, &rbd_dev
->header_oloc
,
5783 response
, RBD_IMAGE_ID_LEN_MAX
);
5784 dout("%s: rbd_obj_method_sync returned %d\n", __func__
, ret
);
5785 if (ret
== -ENOENT
) {
5786 image_id
= kstrdup("", GFP_KERNEL
);
5787 ret
= image_id
? 0 : -ENOMEM
;
5789 rbd_dev
->image_format
= 1;
5790 } else if (ret
>= 0) {
5793 image_id
= ceph_extract_encoded_string(&p
, p
+ ret
,
5795 ret
= PTR_ERR_OR_ZERO(image_id
);
5797 rbd_dev
->image_format
= 2;
5801 rbd_dev
->spec
->image_id
= image_id
;
5802 dout("image_id is %s\n", image_id
);
5806 ceph_oid_destroy(&oid
);
5811 * Undo whatever state changes are made by v1 or v2 header info
5814 static void rbd_dev_unprobe(struct rbd_device
*rbd_dev
)
5816 struct rbd_image_header
*header
;
5818 rbd_dev_parent_put(rbd_dev
);
5820 /* Free dynamic fields from the header, then zero it out */
5822 header
= &rbd_dev
->header
;
5823 ceph_put_snap_context(header
->snapc
);
5824 kfree(header
->snap_sizes
);
5825 kfree(header
->snap_names
);
5826 kfree(header
->object_prefix
);
5827 memset(header
, 0, sizeof (*header
));
5830 static int rbd_dev_v2_header_onetime(struct rbd_device
*rbd_dev
)
5834 ret
= rbd_dev_v2_object_prefix(rbd_dev
);
5839 * Get the and check features for the image. Currently the
5840 * features are assumed to never change.
5842 ret
= rbd_dev_v2_features(rbd_dev
);
5846 /* If the image supports fancy striping, get its parameters */
5848 if (rbd_dev
->header
.features
& RBD_FEATURE_STRIPINGV2
) {
5849 ret
= rbd_dev_v2_striping_info(rbd_dev
);
5854 if (rbd_dev
->header
.features
& RBD_FEATURE_DATA_POOL
) {
5855 ret
= rbd_dev_v2_data_pool(rbd_dev
);
5860 rbd_init_layout(rbd_dev
);
5864 rbd_dev
->header
.features
= 0;
5865 kfree(rbd_dev
->header
.object_prefix
);
5866 rbd_dev
->header
.object_prefix
= NULL
;
5871 * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() ->
5872 * rbd_dev_image_probe() recursion depth, which means it's also the
5873 * length of the already discovered part of the parent chain.
5875 static int rbd_dev_probe_parent(struct rbd_device
*rbd_dev
, int depth
)
5877 struct rbd_device
*parent
= NULL
;
5880 if (!rbd_dev
->parent_spec
)
5883 if (++depth
> RBD_MAX_PARENT_CHAIN_LEN
) {
5884 pr_info("parent chain is too long (%d)\n", depth
);
5889 parent
= __rbd_dev_create(rbd_dev
->rbd_client
, rbd_dev
->parent_spec
);
5896 * Images related by parent/child relationships always share
5897 * rbd_client and spec/parent_spec, so bump their refcounts.
5899 __rbd_get_client(rbd_dev
->rbd_client
);
5900 rbd_spec_get(rbd_dev
->parent_spec
);
5902 ret
= rbd_dev_image_probe(parent
, depth
);
5906 rbd_dev
->parent
= parent
;
5907 atomic_set(&rbd_dev
->parent_ref
, 1);
5911 rbd_dev_unparent(rbd_dev
);
5912 rbd_dev_destroy(parent
);
5916 static void rbd_dev_device_release(struct rbd_device
*rbd_dev
)
5918 clear_bit(RBD_DEV_FLAG_EXISTS
, &rbd_dev
->flags
);
5919 rbd_dev_mapping_clear(rbd_dev
);
5920 rbd_free_disk(rbd_dev
);
5922 unregister_blkdev(rbd_dev
->major
, rbd_dev
->name
);
5926 * rbd_dev->header_rwsem must be locked for write and will be unlocked
5929 static int rbd_dev_device_setup(struct rbd_device
*rbd_dev
)
5933 /* Record our major and minor device numbers. */
5935 if (!single_major
) {
5936 ret
= register_blkdev(0, rbd_dev
->name
);
5938 goto err_out_unlock
;
5940 rbd_dev
->major
= ret
;
5943 rbd_dev
->major
= rbd_major
;
5944 rbd_dev
->minor
= rbd_dev_id_to_minor(rbd_dev
->dev_id
);
5947 /* Set up the blkdev mapping. */
5949 ret
= rbd_init_disk(rbd_dev
);
5951 goto err_out_blkdev
;
5953 ret
= rbd_dev_mapping_set(rbd_dev
);
5957 set_capacity(rbd_dev
->disk
, rbd_dev
->mapping
.size
/ SECTOR_SIZE
);
5958 set_disk_ro(rbd_dev
->disk
, rbd_dev
->opts
->read_only
);
5960 ret
= dev_set_name(&rbd_dev
->dev
, "%d", rbd_dev
->dev_id
);
5962 goto err_out_mapping
;
5964 set_bit(RBD_DEV_FLAG_EXISTS
, &rbd_dev
->flags
);
5965 up_write(&rbd_dev
->header_rwsem
);
5969 rbd_dev_mapping_clear(rbd_dev
);
5971 rbd_free_disk(rbd_dev
);
5974 unregister_blkdev(rbd_dev
->major
, rbd_dev
->name
);
5976 up_write(&rbd_dev
->header_rwsem
);
5980 static int rbd_dev_header_name(struct rbd_device
*rbd_dev
)
5982 struct rbd_spec
*spec
= rbd_dev
->spec
;
5985 /* Record the header object name for this rbd image. */
5987 rbd_assert(rbd_image_format_valid(rbd_dev
->image_format
));
5988 if (rbd_dev
->image_format
== 1)
5989 ret
= ceph_oid_aprintf(&rbd_dev
->header_oid
, GFP_KERNEL
, "%s%s",
5990 spec
->image_name
, RBD_SUFFIX
);
5992 ret
= ceph_oid_aprintf(&rbd_dev
->header_oid
, GFP_KERNEL
, "%s%s",
5993 RBD_HEADER_PREFIX
, spec
->image_id
);
5998 static void rbd_dev_image_release(struct rbd_device
*rbd_dev
)
6000 rbd_dev_unprobe(rbd_dev
);
6002 rbd_unregister_watch(rbd_dev
);
6003 rbd_dev
->image_format
= 0;
6004 kfree(rbd_dev
->spec
->image_id
);
6005 rbd_dev
->spec
->image_id
= NULL
;
6009 * Probe for the existence of the header object for the given rbd
6010 * device. If this image is the one being mapped (i.e., not a
6011 * parent), initiate a watch on its header object before using that
6012 * object to get detailed information about the rbd image.
6014 static int rbd_dev_image_probe(struct rbd_device
*rbd_dev
, int depth
)
6019 * Get the id from the image id object. Unless there's an
6020 * error, rbd_dev->spec->image_id will be filled in with
6021 * a dynamically-allocated string, and rbd_dev->image_format
6022 * will be set to either 1 or 2.
6024 ret
= rbd_dev_image_id(rbd_dev
);
6028 ret
= rbd_dev_header_name(rbd_dev
);
6030 goto err_out_format
;
6033 ret
= rbd_register_watch(rbd_dev
);
6036 pr_info("image %s/%s does not exist\n",
6037 rbd_dev
->spec
->pool_name
,
6038 rbd_dev
->spec
->image_name
);
6039 goto err_out_format
;
6043 ret
= rbd_dev_header_info(rbd_dev
);
6048 * If this image is the one being mapped, we have pool name and
6049 * id, image name and id, and snap name - need to fill snap id.
6050 * Otherwise this is a parent image, identified by pool, image
6051 * and snap ids - need to fill in names for those ids.
6054 ret
= rbd_spec_fill_snap_id(rbd_dev
);
6056 ret
= rbd_spec_fill_names(rbd_dev
);
6059 pr_info("snap %s/%s@%s does not exist\n",
6060 rbd_dev
->spec
->pool_name
,
6061 rbd_dev
->spec
->image_name
,
6062 rbd_dev
->spec
->snap_name
);
6066 if (rbd_dev
->header
.features
& RBD_FEATURE_LAYERING
) {
6067 ret
= rbd_dev_v2_parent_info(rbd_dev
);
6072 * Need to warn users if this image is the one being
6073 * mapped and has a parent.
6075 if (!depth
&& rbd_dev
->parent_spec
)
6077 "WARNING: kernel layering is EXPERIMENTAL!");
6080 ret
= rbd_dev_probe_parent(rbd_dev
, depth
);
6084 dout("discovered format %u image, header name is %s\n",
6085 rbd_dev
->image_format
, rbd_dev
->header_oid
.name
);
6089 rbd_dev_unprobe(rbd_dev
);
6092 rbd_unregister_watch(rbd_dev
);
6094 rbd_dev
->image_format
= 0;
6095 kfree(rbd_dev
->spec
->image_id
);
6096 rbd_dev
->spec
->image_id
= NULL
;
6100 static ssize_t
do_rbd_add(struct bus_type
*bus
,
6104 struct rbd_device
*rbd_dev
= NULL
;
6105 struct ceph_options
*ceph_opts
= NULL
;
6106 struct rbd_options
*rbd_opts
= NULL
;
6107 struct rbd_spec
*spec
= NULL
;
6108 struct rbd_client
*rbdc
;
6111 if (!try_module_get(THIS_MODULE
))
6114 /* parse add command */
6115 rc
= rbd_add_parse_args(buf
, &ceph_opts
, &rbd_opts
, &spec
);
6119 rbdc
= rbd_get_client(ceph_opts
);
6126 rc
= rbd_add_get_pool_id(rbdc
, spec
->pool_name
);
6129 pr_info("pool %s does not exist\n", spec
->pool_name
);
6130 goto err_out_client
;
6132 spec
->pool_id
= (u64
)rc
;
6134 rbd_dev
= rbd_dev_create(rbdc
, spec
, rbd_opts
);
6137 goto err_out_client
;
6139 rbdc
= NULL
; /* rbd_dev now owns this */
6140 spec
= NULL
; /* rbd_dev now owns this */
6141 rbd_opts
= NULL
; /* rbd_dev now owns this */
6143 rbd_dev
->config_info
= kstrdup(buf
, GFP_KERNEL
);
6144 if (!rbd_dev
->config_info
) {
6146 goto err_out_rbd_dev
;
6149 down_write(&rbd_dev
->header_rwsem
);
6150 rc
= rbd_dev_image_probe(rbd_dev
, 0);
6152 up_write(&rbd_dev
->header_rwsem
);
6153 goto err_out_rbd_dev
;
6156 /* If we are mapping a snapshot it must be marked read-only */
6157 if (rbd_dev
->spec
->snap_id
!= CEPH_NOSNAP
)
6158 rbd_dev
->opts
->read_only
= true;
6160 rc
= rbd_dev_device_setup(rbd_dev
);
6162 goto err_out_image_probe
;
6164 if (rbd_dev
->opts
->exclusive
) {
6165 rc
= rbd_add_acquire_lock(rbd_dev
);
6167 goto err_out_device_setup
;
6170 /* Everything's ready. Announce the disk to the world. */
6172 rc
= device_add(&rbd_dev
->dev
);
6174 goto err_out_image_lock
;
6176 add_disk(rbd_dev
->disk
);
6177 /* see rbd_init_disk() */
6178 blk_put_queue(rbd_dev
->disk
->queue
);
6180 spin_lock(&rbd_dev_list_lock
);
6181 list_add_tail(&rbd_dev
->node
, &rbd_dev_list
);
6182 spin_unlock(&rbd_dev_list_lock
);
6184 pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev
->disk
->disk_name
,
6185 (unsigned long long)get_capacity(rbd_dev
->disk
) << SECTOR_SHIFT
,
6186 rbd_dev
->header
.features
);
6189 module_put(THIS_MODULE
);
6193 rbd_dev_image_unlock(rbd_dev
);
6194 err_out_device_setup
:
6195 rbd_dev_device_release(rbd_dev
);
6196 err_out_image_probe
:
6197 rbd_dev_image_release(rbd_dev
);
6199 rbd_dev_destroy(rbd_dev
);
6201 rbd_put_client(rbdc
);
6208 static ssize_t
rbd_add(struct bus_type
*bus
,
6215 return do_rbd_add(bus
, buf
, count
);
6218 static ssize_t
rbd_add_single_major(struct bus_type
*bus
,
6222 return do_rbd_add(bus
, buf
, count
);
6225 static void rbd_dev_remove_parent(struct rbd_device
*rbd_dev
)
6227 while (rbd_dev
->parent
) {
6228 struct rbd_device
*first
= rbd_dev
;
6229 struct rbd_device
*second
= first
->parent
;
6230 struct rbd_device
*third
;
6233 * Follow to the parent with no grandparent and
6236 while (second
&& (third
= second
->parent
)) {
6241 rbd_dev_image_release(second
);
6242 rbd_dev_destroy(second
);
6243 first
->parent
= NULL
;
6244 first
->parent_overlap
= 0;
6246 rbd_assert(first
->parent_spec
);
6247 rbd_spec_put(first
->parent_spec
);
6248 first
->parent_spec
= NULL
;
6252 static ssize_t
do_rbd_remove(struct bus_type
*bus
,
6256 struct rbd_device
*rbd_dev
= NULL
;
6257 struct list_head
*tmp
;
6260 bool already
= false;
6266 sscanf(buf
, "%d %5s", &dev_id
, opt_buf
);
6268 pr_err("dev_id out of range\n");
6271 if (opt_buf
[0] != '\0') {
6272 if (!strcmp(opt_buf
, "force")) {
6275 pr_err("bad remove option at '%s'\n", opt_buf
);
6281 spin_lock(&rbd_dev_list_lock
);
6282 list_for_each(tmp
, &rbd_dev_list
) {
6283 rbd_dev
= list_entry(tmp
, struct rbd_device
, node
);
6284 if (rbd_dev
->dev_id
== dev_id
) {
6290 spin_lock_irq(&rbd_dev
->lock
);
6291 if (rbd_dev
->open_count
&& !force
)
6294 already
= test_and_set_bit(RBD_DEV_FLAG_REMOVING
,
6296 spin_unlock_irq(&rbd_dev
->lock
);
6298 spin_unlock(&rbd_dev_list_lock
);
6299 if (ret
< 0 || already
)
6304 * Prevent new IO from being queued and wait for existing
6305 * IO to complete/fail.
6307 blk_mq_freeze_queue(rbd_dev
->disk
->queue
);
6308 blk_set_queue_dying(rbd_dev
->disk
->queue
);
6311 del_gendisk(rbd_dev
->disk
);
6312 spin_lock(&rbd_dev_list_lock
);
6313 list_del_init(&rbd_dev
->node
);
6314 spin_unlock(&rbd_dev_list_lock
);
6315 device_del(&rbd_dev
->dev
);
6317 rbd_dev_image_unlock(rbd_dev
);
6318 rbd_dev_device_release(rbd_dev
);
6319 rbd_dev_image_release(rbd_dev
);
6320 rbd_dev_destroy(rbd_dev
);
6324 static ssize_t
rbd_remove(struct bus_type
*bus
,
6331 return do_rbd_remove(bus
, buf
, count
);
6334 static ssize_t
rbd_remove_single_major(struct bus_type
*bus
,
6338 return do_rbd_remove(bus
, buf
, count
);
6342 * create control files in sysfs
6345 static int rbd_sysfs_init(void)
6349 ret
= device_register(&rbd_root_dev
);
6353 ret
= bus_register(&rbd_bus_type
);
6355 device_unregister(&rbd_root_dev
);
6360 static void rbd_sysfs_cleanup(void)
6362 bus_unregister(&rbd_bus_type
);
6363 device_unregister(&rbd_root_dev
);
6366 static int rbd_slab_init(void)
6368 rbd_assert(!rbd_img_request_cache
);
6369 rbd_img_request_cache
= KMEM_CACHE(rbd_img_request
, 0);
6370 if (!rbd_img_request_cache
)
6373 rbd_assert(!rbd_obj_request_cache
);
6374 rbd_obj_request_cache
= KMEM_CACHE(rbd_obj_request
, 0);
6375 if (!rbd_obj_request_cache
)
6378 rbd_assert(!rbd_bio_clone
);
6379 rbd_bio_clone
= bioset_create(BIO_POOL_SIZE
, 0, 0);
6386 kmem_cache_destroy(rbd_obj_request_cache
);
6387 rbd_obj_request_cache
= NULL
;
6389 kmem_cache_destroy(rbd_img_request_cache
);
6390 rbd_img_request_cache
= NULL
;
6394 static void rbd_slab_exit(void)
6396 rbd_assert(rbd_obj_request_cache
);
6397 kmem_cache_destroy(rbd_obj_request_cache
);
6398 rbd_obj_request_cache
= NULL
;
6400 rbd_assert(rbd_img_request_cache
);
6401 kmem_cache_destroy(rbd_img_request_cache
);
6402 rbd_img_request_cache
= NULL
;
6404 rbd_assert(rbd_bio_clone
);
6405 bioset_free(rbd_bio_clone
);
6406 rbd_bio_clone
= NULL
;
6409 static int __init
rbd_init(void)
6413 if (!libceph_compatible(NULL
)) {
6414 rbd_warn(NULL
, "libceph incompatibility (quitting)");
6418 rc
= rbd_slab_init();
6423 * The number of active work items is limited by the number of
6424 * rbd devices * queue depth, so leave @max_active at default.
6426 rbd_wq
= alloc_workqueue(RBD_DRV_NAME
, WQ_MEM_RECLAIM
, 0);
6433 rbd_major
= register_blkdev(0, RBD_DRV_NAME
);
6434 if (rbd_major
< 0) {
6440 rc
= rbd_sysfs_init();
6442 goto err_out_blkdev
;
6445 pr_info("loaded (major %d)\n", rbd_major
);
6447 pr_info("loaded\n");
6453 unregister_blkdev(rbd_major
, RBD_DRV_NAME
);
6455 destroy_workqueue(rbd_wq
);
6461 static void __exit
rbd_exit(void)
6463 ida_destroy(&rbd_dev_id_ida
);
6464 rbd_sysfs_cleanup();
6466 unregister_blkdev(rbd_major
, RBD_DRV_NAME
);
6467 destroy_workqueue(rbd_wq
);
6471 module_init(rbd_init
);
6472 module_exit(rbd_exit
);
6474 MODULE_AUTHOR("Alex Elder <elder@inktank.com>");
6475 MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
6476 MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
6477 /* following authorship retained from original osdblk.c */
6478 MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
6480 MODULE_DESCRIPTION("RADOS Block Device (RBD) driver");
6481 MODULE_LICENSE("GPL");