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)
128 #define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \
129 RBD_FEATURE_STRIPINGV2 | \
130 RBD_FEATURE_EXCLUSIVE_LOCK | \
131 RBD_FEATURE_DATA_POOL)
133 /* Features supported by this (client software) implementation. */
135 #define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL)
138 * An RBD device name will be "rbd#", where the "rbd" comes from
139 * RBD_DRV_NAME above, and # is a unique integer identifier.
141 #define DEV_NAME_LEN 32
144 * block device image metadata (in-memory version)
146 struct rbd_image_header
{
147 /* These six fields never change for a given rbd image */
153 u64 features
; /* Might be changeable someday? */
155 /* The remaining fields need to be updated occasionally */
157 struct ceph_snap_context
*snapc
;
158 char *snap_names
; /* format 1 only */
159 u64
*snap_sizes
; /* format 1 only */
163 * An rbd image specification.
165 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
166 * identify an image. Each rbd_dev structure includes a pointer to
167 * an rbd_spec structure that encapsulates this identity.
169 * Each of the id's in an rbd_spec has an associated name. For a
170 * user-mapped image, the names are supplied and the id's associated
171 * with them are looked up. For a layered image, a parent image is
172 * defined by the tuple, and the names are looked up.
174 * An rbd_dev structure contains a parent_spec pointer which is
175 * non-null if the image it represents is a child in a layered
176 * image. This pointer will refer to the rbd_spec structure used
177 * by the parent rbd_dev for its own identity (i.e., the structure
178 * is shared between the parent and child).
180 * Since these structures are populated once, during the discovery
181 * phase of image construction, they are effectively immutable so
182 * we make no effort to synchronize access to them.
184 * Note that code herein does not assume the image name is known (it
185 * could be a null pointer).
189 const char *pool_name
;
191 const char *image_id
;
192 const char *image_name
;
195 const char *snap_name
;
201 * an instance of the client. multiple devices may share an rbd client.
204 struct ceph_client
*client
;
206 struct list_head node
;
209 struct rbd_img_request
;
210 typedef void (*rbd_img_callback_t
)(struct rbd_img_request
*);
212 #define BAD_WHICH U32_MAX /* Good which or bad which, which? */
214 struct rbd_obj_request
;
215 typedef void (*rbd_obj_callback_t
)(struct rbd_obj_request
*);
217 enum obj_request_type
{
218 OBJ_REQUEST_NODATA
, OBJ_REQUEST_BIO
, OBJ_REQUEST_PAGES
221 enum obj_operation_type
{
228 OBJ_REQ_DONE
, /* completion flag: not done = 0, done = 1 */
229 OBJ_REQ_IMG_DATA
, /* object usage: standalone = 0, image = 1 */
230 OBJ_REQ_KNOWN
, /* EXISTS flag valid: no = 0, yes = 1 */
231 OBJ_REQ_EXISTS
, /* target exists: no = 0, yes = 1 */
234 struct rbd_obj_request
{
236 u64 offset
; /* object start byte */
237 u64 length
; /* bytes from offset */
241 * An object request associated with an image will have its
242 * img_data flag set; a standalone object request will not.
244 * A standalone object request will have which == BAD_WHICH
245 * and a null obj_request pointer.
247 * An object request initiated in support of a layered image
248 * object (to check for its existence before a write) will
249 * have which == BAD_WHICH and a non-null obj_request pointer.
251 * Finally, an object request for rbd image data will have
252 * which != BAD_WHICH, and will have a non-null img_request
253 * pointer. The value of which will be in the range
254 * 0..(img_request->obj_request_count-1).
257 struct rbd_obj_request
*obj_request
; /* STAT op */
259 struct rbd_img_request
*img_request
;
261 /* links for img_request->obj_requests list */
262 struct list_head links
;
265 u32 which
; /* posn image request list */
267 enum obj_request_type type
;
269 struct bio
*bio_list
;
275 struct page
**copyup_pages
;
276 u32 copyup_page_count
;
278 struct ceph_osd_request
*osd_req
;
280 u64 xferred
; /* bytes transferred */
283 rbd_obj_callback_t callback
;
284 struct completion completion
;
290 IMG_REQ_WRITE
, /* I/O direction: read = 0, write = 1 */
291 IMG_REQ_CHILD
, /* initiator: block = 0, child image = 1 */
292 IMG_REQ_LAYERED
, /* ENOENT handling: normal = 0, layered = 1 */
293 IMG_REQ_DISCARD
, /* discard: normal = 0, discard request = 1 */
296 struct rbd_img_request
{
297 struct rbd_device
*rbd_dev
;
298 u64 offset
; /* starting image byte offset */
299 u64 length
; /* byte count from offset */
302 u64 snap_id
; /* for reads */
303 struct ceph_snap_context
*snapc
; /* for writes */
306 struct request
*rq
; /* block request */
307 struct rbd_obj_request
*obj_request
; /* obj req initiator */
309 struct page
**copyup_pages
;
310 u32 copyup_page_count
;
311 spinlock_t completion_lock
;/* protects next_completion */
313 rbd_img_callback_t callback
;
314 u64 xferred
;/* aggregate bytes transferred */
315 int result
; /* first nonzero obj_request result */
317 u32 obj_request_count
;
318 struct list_head obj_requests
; /* rbd_obj_request structs */
323 #define for_each_obj_request(ireq, oreq) \
324 list_for_each_entry(oreq, &(ireq)->obj_requests, links)
325 #define for_each_obj_request_from(ireq, oreq) \
326 list_for_each_entry_from(oreq, &(ireq)->obj_requests, links)
327 #define for_each_obj_request_safe(ireq, oreq, n) \
328 list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links)
330 enum rbd_watch_state
{
331 RBD_WATCH_STATE_UNREGISTERED
,
332 RBD_WATCH_STATE_REGISTERED
,
333 RBD_WATCH_STATE_ERROR
,
336 enum rbd_lock_state
{
337 RBD_LOCK_STATE_UNLOCKED
,
338 RBD_LOCK_STATE_LOCKED
,
339 RBD_LOCK_STATE_RELEASING
,
342 /* WatchNotify::ClientId */
343 struct rbd_client_id
{
357 int dev_id
; /* blkdev unique id */
359 int major
; /* blkdev assigned major */
361 struct gendisk
*disk
; /* blkdev's gendisk and rq */
363 u32 image_format
; /* Either 1 or 2 */
364 struct rbd_client
*rbd_client
;
366 char name
[DEV_NAME_LEN
]; /* blkdev name, e.g. rbd3 */
368 spinlock_t lock
; /* queue, flags, open_count */
370 struct rbd_image_header header
;
371 unsigned long flags
; /* possibly lock protected */
372 struct rbd_spec
*spec
;
373 struct rbd_options
*opts
;
374 char *config_info
; /* add{,_single_major} string */
376 struct ceph_object_id header_oid
;
377 struct ceph_object_locator header_oloc
;
379 struct ceph_file_layout layout
; /* used for all rbd requests */
381 struct mutex watch_mutex
;
382 enum rbd_watch_state watch_state
;
383 struct ceph_osd_linger_request
*watch_handle
;
385 struct delayed_work watch_dwork
;
387 struct rw_semaphore lock_rwsem
;
388 enum rbd_lock_state lock_state
;
389 char lock_cookie
[32];
390 struct rbd_client_id owner_cid
;
391 struct work_struct acquired_lock_work
;
392 struct work_struct released_lock_work
;
393 struct delayed_work lock_dwork
;
394 struct work_struct unlock_work
;
395 wait_queue_head_t lock_waitq
;
397 struct workqueue_struct
*task_wq
;
399 struct rbd_spec
*parent_spec
;
402 struct rbd_device
*parent
;
404 /* Block layer tags. */
405 struct blk_mq_tag_set tag_set
;
407 /* protects updating the header */
408 struct rw_semaphore header_rwsem
;
410 struct rbd_mapping mapping
;
412 struct list_head node
;
416 unsigned long open_count
; /* protected by lock */
420 * Flag bits for rbd_dev->flags:
421 * - REMOVING (which is coupled with rbd_dev->open_count) is protected
423 * - BLACKLISTED is protected by rbd_dev->lock_rwsem
426 RBD_DEV_FLAG_EXISTS
, /* mapped snapshot has not been deleted */
427 RBD_DEV_FLAG_REMOVING
, /* this mapping is being removed */
428 RBD_DEV_FLAG_BLACKLISTED
, /* our ceph_client is blacklisted */
431 static DEFINE_MUTEX(client_mutex
); /* Serialize client creation */
433 static LIST_HEAD(rbd_dev_list
); /* devices */
434 static DEFINE_SPINLOCK(rbd_dev_list_lock
);
436 static LIST_HEAD(rbd_client_list
); /* clients */
437 static DEFINE_SPINLOCK(rbd_client_list_lock
);
439 /* Slab caches for frequently-allocated structures */
441 static struct kmem_cache
*rbd_img_request_cache
;
442 static struct kmem_cache
*rbd_obj_request_cache
;
444 static struct bio_set
*rbd_bio_clone
;
446 static int rbd_major
;
447 static DEFINE_IDA(rbd_dev_id_ida
);
449 static struct workqueue_struct
*rbd_wq
;
452 * single-major requires >= 0.75 version of userspace rbd utility.
454 static bool single_major
= true;
455 module_param(single_major
, bool, S_IRUGO
);
456 MODULE_PARM_DESC(single_major
, "Use a single major number for all rbd devices (default: true)");
458 static int rbd_img_request_submit(struct rbd_img_request
*img_request
);
460 static ssize_t
rbd_add(struct bus_type
*bus
, const char *buf
,
462 static ssize_t
rbd_remove(struct bus_type
*bus
, const char *buf
,
464 static ssize_t
rbd_add_single_major(struct bus_type
*bus
, const char *buf
,
466 static ssize_t
rbd_remove_single_major(struct bus_type
*bus
, const char *buf
,
468 static int rbd_dev_image_probe(struct rbd_device
*rbd_dev
, int depth
);
469 static void rbd_spec_put(struct rbd_spec
*spec
);
471 static int rbd_dev_id_to_minor(int dev_id
)
473 return dev_id
<< RBD_SINGLE_MAJOR_PART_SHIFT
;
476 static int minor_to_rbd_dev_id(int minor
)
478 return minor
>> RBD_SINGLE_MAJOR_PART_SHIFT
;
481 static bool __rbd_is_lock_owner(struct rbd_device
*rbd_dev
)
483 return rbd_dev
->lock_state
== RBD_LOCK_STATE_LOCKED
||
484 rbd_dev
->lock_state
== RBD_LOCK_STATE_RELEASING
;
487 static bool rbd_is_lock_owner(struct rbd_device
*rbd_dev
)
491 down_read(&rbd_dev
->lock_rwsem
);
492 is_lock_owner
= __rbd_is_lock_owner(rbd_dev
);
493 up_read(&rbd_dev
->lock_rwsem
);
494 return is_lock_owner
;
497 static ssize_t
rbd_supported_features_show(struct bus_type
*bus
, char *buf
)
499 return sprintf(buf
, "0x%llx\n", RBD_FEATURES_SUPPORTED
);
502 static BUS_ATTR(add
, S_IWUSR
, NULL
, rbd_add
);
503 static BUS_ATTR(remove
, S_IWUSR
, NULL
, rbd_remove
);
504 static BUS_ATTR(add_single_major
, S_IWUSR
, NULL
, rbd_add_single_major
);
505 static BUS_ATTR(remove_single_major
, S_IWUSR
, NULL
, rbd_remove_single_major
);
506 static BUS_ATTR(supported_features
, S_IRUGO
, rbd_supported_features_show
, NULL
);
508 static struct attribute
*rbd_bus_attrs
[] = {
510 &bus_attr_remove
.attr
,
511 &bus_attr_add_single_major
.attr
,
512 &bus_attr_remove_single_major
.attr
,
513 &bus_attr_supported_features
.attr
,
517 static umode_t
rbd_bus_is_visible(struct kobject
*kobj
,
518 struct attribute
*attr
, int index
)
521 (attr
== &bus_attr_add_single_major
.attr
||
522 attr
== &bus_attr_remove_single_major
.attr
))
528 static const struct attribute_group rbd_bus_group
= {
529 .attrs
= rbd_bus_attrs
,
530 .is_visible
= rbd_bus_is_visible
,
532 __ATTRIBUTE_GROUPS(rbd_bus
);
534 static struct bus_type rbd_bus_type
= {
536 .bus_groups
= rbd_bus_groups
,
539 static void rbd_root_dev_release(struct device
*dev
)
543 static struct device rbd_root_dev
= {
545 .release
= rbd_root_dev_release
,
548 static __printf(2, 3)
549 void rbd_warn(struct rbd_device
*rbd_dev
, const char *fmt
, ...)
551 struct va_format vaf
;
559 printk(KERN_WARNING
"%s: %pV\n", RBD_DRV_NAME
, &vaf
);
560 else if (rbd_dev
->disk
)
561 printk(KERN_WARNING
"%s: %s: %pV\n",
562 RBD_DRV_NAME
, rbd_dev
->disk
->disk_name
, &vaf
);
563 else if (rbd_dev
->spec
&& rbd_dev
->spec
->image_name
)
564 printk(KERN_WARNING
"%s: image %s: %pV\n",
565 RBD_DRV_NAME
, rbd_dev
->spec
->image_name
, &vaf
);
566 else if (rbd_dev
->spec
&& rbd_dev
->spec
->image_id
)
567 printk(KERN_WARNING
"%s: id %s: %pV\n",
568 RBD_DRV_NAME
, rbd_dev
->spec
->image_id
, &vaf
);
570 printk(KERN_WARNING
"%s: rbd_dev %p: %pV\n",
571 RBD_DRV_NAME
, rbd_dev
, &vaf
);
576 #define rbd_assert(expr) \
577 if (unlikely(!(expr))) { \
578 printk(KERN_ERR "\nAssertion failure in %s() " \
580 "\trbd_assert(%s);\n\n", \
581 __func__, __LINE__, #expr); \
584 #else /* !RBD_DEBUG */
585 # define rbd_assert(expr) ((void) 0)
586 #endif /* !RBD_DEBUG */
588 static void rbd_osd_copyup_callback(struct rbd_obj_request
*obj_request
);
589 static int rbd_img_obj_request_submit(struct rbd_obj_request
*obj_request
);
590 static void rbd_img_parent_read(struct rbd_obj_request
*obj_request
);
591 static void rbd_dev_remove_parent(struct rbd_device
*rbd_dev
);
593 static int rbd_dev_refresh(struct rbd_device
*rbd_dev
);
594 static int rbd_dev_v2_header_onetime(struct rbd_device
*rbd_dev
);
595 static int rbd_dev_header_info(struct rbd_device
*rbd_dev
);
596 static int rbd_dev_v2_parent_info(struct rbd_device
*rbd_dev
);
597 static const char *rbd_dev_v2_snap_name(struct rbd_device
*rbd_dev
,
599 static int _rbd_dev_v2_snap_size(struct rbd_device
*rbd_dev
, u64 snap_id
,
600 u8
*order
, u64
*snap_size
);
601 static int _rbd_dev_v2_snap_features(struct rbd_device
*rbd_dev
, u64 snap_id
,
604 static int rbd_open(struct block_device
*bdev
, fmode_t mode
)
606 struct rbd_device
*rbd_dev
= bdev
->bd_disk
->private_data
;
607 bool removing
= false;
609 spin_lock_irq(&rbd_dev
->lock
);
610 if (test_bit(RBD_DEV_FLAG_REMOVING
, &rbd_dev
->flags
))
613 rbd_dev
->open_count
++;
614 spin_unlock_irq(&rbd_dev
->lock
);
618 (void) get_device(&rbd_dev
->dev
);
623 static void rbd_release(struct gendisk
*disk
, fmode_t mode
)
625 struct rbd_device
*rbd_dev
= disk
->private_data
;
626 unsigned long open_count_before
;
628 spin_lock_irq(&rbd_dev
->lock
);
629 open_count_before
= rbd_dev
->open_count
--;
630 spin_unlock_irq(&rbd_dev
->lock
);
631 rbd_assert(open_count_before
> 0);
633 put_device(&rbd_dev
->dev
);
636 static int rbd_ioctl_set_ro(struct rbd_device
*rbd_dev
, unsigned long arg
)
640 if (get_user(ro
, (int __user
*)arg
))
643 /* Snapshots can't be marked read-write */
644 if (rbd_dev
->spec
->snap_id
!= CEPH_NOSNAP
&& !ro
)
647 /* Let blkdev_roset() handle it */
651 static int rbd_ioctl(struct block_device
*bdev
, fmode_t mode
,
652 unsigned int cmd
, unsigned long arg
)
654 struct rbd_device
*rbd_dev
= bdev
->bd_disk
->private_data
;
659 ret
= rbd_ioctl_set_ro(rbd_dev
, arg
);
669 static int rbd_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
670 unsigned int cmd
, unsigned long arg
)
672 return rbd_ioctl(bdev
, mode
, cmd
, arg
);
674 #endif /* CONFIG_COMPAT */
676 static const struct block_device_operations rbd_bd_ops
= {
677 .owner
= THIS_MODULE
,
679 .release
= rbd_release
,
682 .compat_ioctl
= rbd_compat_ioctl
,
687 * Initialize an rbd client instance. Success or not, this function
688 * consumes ceph_opts. Caller holds client_mutex.
690 static struct rbd_client
*rbd_client_create(struct ceph_options
*ceph_opts
)
692 struct rbd_client
*rbdc
;
695 dout("%s:\n", __func__
);
696 rbdc
= kmalloc(sizeof(struct rbd_client
), GFP_KERNEL
);
700 kref_init(&rbdc
->kref
);
701 INIT_LIST_HEAD(&rbdc
->node
);
703 rbdc
->client
= ceph_create_client(ceph_opts
, rbdc
);
704 if (IS_ERR(rbdc
->client
))
706 ceph_opts
= NULL
; /* Now rbdc->client is responsible for ceph_opts */
708 ret
= ceph_open_session(rbdc
->client
);
712 spin_lock(&rbd_client_list_lock
);
713 list_add_tail(&rbdc
->node
, &rbd_client_list
);
714 spin_unlock(&rbd_client_list_lock
);
716 dout("%s: rbdc %p\n", __func__
, rbdc
);
720 ceph_destroy_client(rbdc
->client
);
725 ceph_destroy_options(ceph_opts
);
726 dout("%s: error %d\n", __func__
, ret
);
731 static struct rbd_client
*__rbd_get_client(struct rbd_client
*rbdc
)
733 kref_get(&rbdc
->kref
);
739 * Find a ceph client with specific addr and configuration. If
740 * found, bump its reference count.
742 static struct rbd_client
*rbd_client_find(struct ceph_options
*ceph_opts
)
744 struct rbd_client
*client_node
;
747 if (ceph_opts
->flags
& CEPH_OPT_NOSHARE
)
750 spin_lock(&rbd_client_list_lock
);
751 list_for_each_entry(client_node
, &rbd_client_list
, node
) {
752 if (!ceph_compare_options(ceph_opts
, client_node
->client
)) {
753 __rbd_get_client(client_node
);
759 spin_unlock(&rbd_client_list_lock
);
761 return found
? client_node
: NULL
;
765 * (Per device) rbd map options
772 /* string args above */
780 static match_table_t rbd_opts_tokens
= {
781 {Opt_queue_depth
, "queue_depth=%d"},
783 /* string args above */
784 {Opt_read_only
, "read_only"},
785 {Opt_read_only
, "ro"}, /* Alternate spelling */
786 {Opt_read_write
, "read_write"},
787 {Opt_read_write
, "rw"}, /* Alternate spelling */
788 {Opt_lock_on_read
, "lock_on_read"},
789 {Opt_exclusive
, "exclusive"},
800 #define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ
801 #define RBD_READ_ONLY_DEFAULT false
802 #define RBD_LOCK_ON_READ_DEFAULT false
803 #define RBD_EXCLUSIVE_DEFAULT false
805 static int parse_rbd_opts_token(char *c
, void *private)
807 struct rbd_options
*rbd_opts
= private;
808 substring_t argstr
[MAX_OPT_ARGS
];
809 int token
, intval
, ret
;
811 token
= match_token(c
, rbd_opts_tokens
, argstr
);
812 if (token
< Opt_last_int
) {
813 ret
= match_int(&argstr
[0], &intval
);
815 pr_err("bad mount option arg (not int) at '%s'\n", c
);
818 dout("got int token %d val %d\n", token
, intval
);
819 } else if (token
> Opt_last_int
&& token
< Opt_last_string
) {
820 dout("got string token %d val %s\n", token
, argstr
[0].from
);
822 dout("got token %d\n", token
);
826 case Opt_queue_depth
:
828 pr_err("queue_depth out of range\n");
831 rbd_opts
->queue_depth
= intval
;
834 rbd_opts
->read_only
= true;
837 rbd_opts
->read_only
= false;
839 case Opt_lock_on_read
:
840 rbd_opts
->lock_on_read
= true;
843 rbd_opts
->exclusive
= true;
846 /* libceph prints "bad option" msg */
853 static char* obj_op_name(enum obj_operation_type op_type
)
868 * Get a ceph client with specific addr and configuration, if one does
869 * not exist create it. Either way, ceph_opts is consumed by this
872 static struct rbd_client
*rbd_get_client(struct ceph_options
*ceph_opts
)
874 struct rbd_client
*rbdc
;
876 mutex_lock_nested(&client_mutex
, SINGLE_DEPTH_NESTING
);
877 rbdc
= rbd_client_find(ceph_opts
);
878 if (rbdc
) /* using an existing client */
879 ceph_destroy_options(ceph_opts
);
881 rbdc
= rbd_client_create(ceph_opts
);
882 mutex_unlock(&client_mutex
);
888 * Destroy ceph client
890 * Caller must hold rbd_client_list_lock.
892 static void rbd_client_release(struct kref
*kref
)
894 struct rbd_client
*rbdc
= container_of(kref
, struct rbd_client
, kref
);
896 dout("%s: rbdc %p\n", __func__
, rbdc
);
897 spin_lock(&rbd_client_list_lock
);
898 list_del(&rbdc
->node
);
899 spin_unlock(&rbd_client_list_lock
);
901 ceph_destroy_client(rbdc
->client
);
906 * Drop reference to ceph client node. If it's not referenced anymore, release
909 static void rbd_put_client(struct rbd_client
*rbdc
)
912 kref_put(&rbdc
->kref
, rbd_client_release
);
915 static bool rbd_image_format_valid(u32 image_format
)
917 return image_format
== 1 || image_format
== 2;
920 static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk
*ondisk
)
925 /* The header has to start with the magic rbd header text */
926 if (memcmp(&ondisk
->text
, RBD_HEADER_TEXT
, sizeof (RBD_HEADER_TEXT
)))
929 /* The bio layer requires at least sector-sized I/O */
931 if (ondisk
->options
.order
< SECTOR_SHIFT
)
934 /* If we use u64 in a few spots we may be able to loosen this */
936 if (ondisk
->options
.order
> 8 * sizeof (int) - 1)
940 * The size of a snapshot header has to fit in a size_t, and
941 * that limits the number of snapshots.
943 snap_count
= le32_to_cpu(ondisk
->snap_count
);
944 size
= SIZE_MAX
- sizeof (struct ceph_snap_context
);
945 if (snap_count
> size
/ sizeof (__le64
))
949 * Not only that, but the size of the entire the snapshot
950 * header must also be representable in a size_t.
952 size
-= snap_count
* sizeof (__le64
);
953 if ((u64
) size
< le64_to_cpu(ondisk
->snap_names_len
))
960 * returns the size of an object in the image
962 static u32
rbd_obj_bytes(struct rbd_image_header
*header
)
964 return 1U << header
->obj_order
;
967 static void rbd_init_layout(struct rbd_device
*rbd_dev
)
969 if (rbd_dev
->header
.stripe_unit
== 0 ||
970 rbd_dev
->header
.stripe_count
== 0) {
971 rbd_dev
->header
.stripe_unit
= rbd_obj_bytes(&rbd_dev
->header
);
972 rbd_dev
->header
.stripe_count
= 1;
975 rbd_dev
->layout
.stripe_unit
= rbd_dev
->header
.stripe_unit
;
976 rbd_dev
->layout
.stripe_count
= rbd_dev
->header
.stripe_count
;
977 rbd_dev
->layout
.object_size
= rbd_obj_bytes(&rbd_dev
->header
);
978 rbd_dev
->layout
.pool_id
= rbd_dev
->header
.data_pool_id
== CEPH_NOPOOL
?
979 rbd_dev
->spec
->pool_id
: rbd_dev
->header
.data_pool_id
;
980 RCU_INIT_POINTER(rbd_dev
->layout
.pool_ns
, NULL
);
984 * Fill an rbd image header with information from the given format 1
987 static int rbd_header_from_disk(struct rbd_device
*rbd_dev
,
988 struct rbd_image_header_ondisk
*ondisk
)
990 struct rbd_image_header
*header
= &rbd_dev
->header
;
991 bool first_time
= header
->object_prefix
== NULL
;
992 struct ceph_snap_context
*snapc
;
993 char *object_prefix
= NULL
;
994 char *snap_names
= NULL
;
995 u64
*snap_sizes
= NULL
;
1000 /* Allocate this now to avoid having to handle failure below */
1003 object_prefix
= kstrndup(ondisk
->object_prefix
,
1004 sizeof(ondisk
->object_prefix
),
1010 /* Allocate the snapshot context and fill it in */
1012 snap_count
= le32_to_cpu(ondisk
->snap_count
);
1013 snapc
= ceph_create_snap_context(snap_count
, GFP_KERNEL
);
1016 snapc
->seq
= le64_to_cpu(ondisk
->snap_seq
);
1018 struct rbd_image_snap_ondisk
*snaps
;
1019 u64 snap_names_len
= le64_to_cpu(ondisk
->snap_names_len
);
1021 /* We'll keep a copy of the snapshot names... */
1023 if (snap_names_len
> (u64
)SIZE_MAX
)
1025 snap_names
= kmalloc(snap_names_len
, GFP_KERNEL
);
1029 /* ...as well as the array of their sizes. */
1030 snap_sizes
= kmalloc_array(snap_count
,
1031 sizeof(*header
->snap_sizes
),
1037 * Copy the names, and fill in each snapshot's id
1040 * Note that rbd_dev_v1_header_info() guarantees the
1041 * ondisk buffer we're working with has
1042 * snap_names_len bytes beyond the end of the
1043 * snapshot id array, this memcpy() is safe.
1045 memcpy(snap_names
, &ondisk
->snaps
[snap_count
], snap_names_len
);
1046 snaps
= ondisk
->snaps
;
1047 for (i
= 0; i
< snap_count
; i
++) {
1048 snapc
->snaps
[i
] = le64_to_cpu(snaps
[i
].id
);
1049 snap_sizes
[i
] = le64_to_cpu(snaps
[i
].image_size
);
1053 /* We won't fail any more, fill in the header */
1056 header
->object_prefix
= object_prefix
;
1057 header
->obj_order
= ondisk
->options
.order
;
1058 rbd_init_layout(rbd_dev
);
1060 ceph_put_snap_context(header
->snapc
);
1061 kfree(header
->snap_names
);
1062 kfree(header
->snap_sizes
);
1065 /* The remaining fields always get updated (when we refresh) */
1067 header
->image_size
= le64_to_cpu(ondisk
->image_size
);
1068 header
->snapc
= snapc
;
1069 header
->snap_names
= snap_names
;
1070 header
->snap_sizes
= snap_sizes
;
1078 ceph_put_snap_context(snapc
);
1079 kfree(object_prefix
);
1084 static const char *_rbd_dev_v1_snap_name(struct rbd_device
*rbd_dev
, u32 which
)
1086 const char *snap_name
;
1088 rbd_assert(which
< rbd_dev
->header
.snapc
->num_snaps
);
1090 /* Skip over names until we find the one we are looking for */
1092 snap_name
= rbd_dev
->header
.snap_names
;
1094 snap_name
+= strlen(snap_name
) + 1;
1096 return kstrdup(snap_name
, GFP_KERNEL
);
1100 * Snapshot id comparison function for use with qsort()/bsearch().
1101 * Note that result is for snapshots in *descending* order.
1103 static int snapid_compare_reverse(const void *s1
, const void *s2
)
1105 u64 snap_id1
= *(u64
*)s1
;
1106 u64 snap_id2
= *(u64
*)s2
;
1108 if (snap_id1
< snap_id2
)
1110 return snap_id1
== snap_id2
? 0 : -1;
1114 * Search a snapshot context to see if the given snapshot id is
1117 * Returns the position of the snapshot id in the array if it's found,
1118 * or BAD_SNAP_INDEX otherwise.
1120 * Note: The snapshot array is in kept sorted (by the osd) in
1121 * reverse order, highest snapshot id first.
1123 static u32
rbd_dev_snap_index(struct rbd_device
*rbd_dev
, u64 snap_id
)
1125 struct ceph_snap_context
*snapc
= rbd_dev
->header
.snapc
;
1128 found
= bsearch(&snap_id
, &snapc
->snaps
, snapc
->num_snaps
,
1129 sizeof (snap_id
), snapid_compare_reverse
);
1131 return found
? (u32
)(found
- &snapc
->snaps
[0]) : BAD_SNAP_INDEX
;
1134 static const char *rbd_dev_v1_snap_name(struct rbd_device
*rbd_dev
,
1138 const char *snap_name
;
1140 which
= rbd_dev_snap_index(rbd_dev
, snap_id
);
1141 if (which
== BAD_SNAP_INDEX
)
1142 return ERR_PTR(-ENOENT
);
1144 snap_name
= _rbd_dev_v1_snap_name(rbd_dev
, which
);
1145 return snap_name
? snap_name
: ERR_PTR(-ENOMEM
);
1148 static const char *rbd_snap_name(struct rbd_device
*rbd_dev
, u64 snap_id
)
1150 if (snap_id
== CEPH_NOSNAP
)
1151 return RBD_SNAP_HEAD_NAME
;
1153 rbd_assert(rbd_image_format_valid(rbd_dev
->image_format
));
1154 if (rbd_dev
->image_format
== 1)
1155 return rbd_dev_v1_snap_name(rbd_dev
, snap_id
);
1157 return rbd_dev_v2_snap_name(rbd_dev
, snap_id
);
1160 static int rbd_snap_size(struct rbd_device
*rbd_dev
, u64 snap_id
,
1163 rbd_assert(rbd_image_format_valid(rbd_dev
->image_format
));
1164 if (snap_id
== CEPH_NOSNAP
) {
1165 *snap_size
= rbd_dev
->header
.image_size
;
1166 } else if (rbd_dev
->image_format
== 1) {
1169 which
= rbd_dev_snap_index(rbd_dev
, snap_id
);
1170 if (which
== BAD_SNAP_INDEX
)
1173 *snap_size
= rbd_dev
->header
.snap_sizes
[which
];
1178 ret
= _rbd_dev_v2_snap_size(rbd_dev
, snap_id
, NULL
, &size
);
1187 static int rbd_snap_features(struct rbd_device
*rbd_dev
, u64 snap_id
,
1190 rbd_assert(rbd_image_format_valid(rbd_dev
->image_format
));
1191 if (snap_id
== CEPH_NOSNAP
) {
1192 *snap_features
= rbd_dev
->header
.features
;
1193 } else if (rbd_dev
->image_format
== 1) {
1194 *snap_features
= 0; /* No features for format 1 */
1199 ret
= _rbd_dev_v2_snap_features(rbd_dev
, snap_id
, &features
);
1203 *snap_features
= features
;
1208 static int rbd_dev_mapping_set(struct rbd_device
*rbd_dev
)
1210 u64 snap_id
= rbd_dev
->spec
->snap_id
;
1215 ret
= rbd_snap_size(rbd_dev
, snap_id
, &size
);
1218 ret
= rbd_snap_features(rbd_dev
, snap_id
, &features
);
1222 rbd_dev
->mapping
.size
= size
;
1223 rbd_dev
->mapping
.features
= features
;
1228 static void rbd_dev_mapping_clear(struct rbd_device
*rbd_dev
)
1230 rbd_dev
->mapping
.size
= 0;
1231 rbd_dev
->mapping
.features
= 0;
1234 static u64
rbd_segment_offset(struct rbd_device
*rbd_dev
, u64 offset
)
1236 u64 segment_size
= rbd_obj_bytes(&rbd_dev
->header
);
1238 return offset
& (segment_size
- 1);
1241 static u64
rbd_segment_length(struct rbd_device
*rbd_dev
,
1242 u64 offset
, u64 length
)
1244 u64 segment_size
= rbd_obj_bytes(&rbd_dev
->header
);
1246 offset
&= segment_size
- 1;
1248 rbd_assert(length
<= U64_MAX
- offset
);
1249 if (offset
+ length
> segment_size
)
1250 length
= segment_size
- offset
;
1259 static void bio_chain_put(struct bio
*chain
)
1265 chain
= chain
->bi_next
;
1271 * zeros a bio chain, starting at specific offset
1273 static void zero_bio_chain(struct bio
*chain
, int start_ofs
)
1276 struct bvec_iter iter
;
1277 unsigned long flags
;
1282 bio_for_each_segment(bv
, chain
, iter
) {
1283 if (pos
+ bv
.bv_len
> start_ofs
) {
1284 int remainder
= max(start_ofs
- pos
, 0);
1285 buf
= bvec_kmap_irq(&bv
, &flags
);
1286 memset(buf
+ remainder
, 0,
1287 bv
.bv_len
- remainder
);
1288 flush_dcache_page(bv
.bv_page
);
1289 bvec_kunmap_irq(buf
, &flags
);
1294 chain
= chain
->bi_next
;
1299 * similar to zero_bio_chain(), zeros data defined by a page array,
1300 * starting at the given byte offset from the start of the array and
1301 * continuing up to the given end offset. The pages array is
1302 * assumed to be big enough to hold all bytes up to the end.
1304 static void zero_pages(struct page
**pages
, u64 offset
, u64 end
)
1306 struct page
**page
= &pages
[offset
>> PAGE_SHIFT
];
1308 rbd_assert(end
> offset
);
1309 rbd_assert(end
- offset
<= (u64
)SIZE_MAX
);
1310 while (offset
< end
) {
1313 unsigned long flags
;
1316 page_offset
= offset
& ~PAGE_MASK
;
1317 length
= min_t(size_t, PAGE_SIZE
- page_offset
, end
- offset
);
1318 local_irq_save(flags
);
1319 kaddr
= kmap_atomic(*page
);
1320 memset(kaddr
+ page_offset
, 0, length
);
1321 flush_dcache_page(*page
);
1322 kunmap_atomic(kaddr
);
1323 local_irq_restore(flags
);
1331 * Clone a portion of a bio, starting at the given byte offset
1332 * and continuing for the number of bytes indicated.
1334 static struct bio
*bio_clone_range(struct bio
*bio_src
,
1335 unsigned int offset
,
1341 bio
= bio_clone_fast(bio_src
, gfpmask
, rbd_bio_clone
);
1343 return NULL
; /* ENOMEM */
1345 bio_advance(bio
, offset
);
1346 bio
->bi_iter
.bi_size
= len
;
1352 * Clone a portion of a bio chain, starting at the given byte offset
1353 * into the first bio in the source chain and continuing for the
1354 * number of bytes indicated. The result is another bio chain of
1355 * exactly the given length, or a null pointer on error.
1357 * The bio_src and offset parameters are both in-out. On entry they
1358 * refer to the first source bio and the offset into that bio where
1359 * the start of data to be cloned is located.
1361 * On return, bio_src is updated to refer to the bio in the source
1362 * chain that contains first un-cloned byte, and *offset will
1363 * contain the offset of that byte within that bio.
1365 static struct bio
*bio_chain_clone_range(struct bio
**bio_src
,
1366 unsigned int *offset
,
1370 struct bio
*bi
= *bio_src
;
1371 unsigned int off
= *offset
;
1372 struct bio
*chain
= NULL
;
1375 /* Build up a chain of clone bios up to the limit */
1377 if (!bi
|| off
>= bi
->bi_iter
.bi_size
|| !len
)
1378 return NULL
; /* Nothing to clone */
1382 unsigned int bi_size
;
1386 rbd_warn(NULL
, "bio_chain exhausted with %u left", len
);
1387 goto out_err
; /* EINVAL; ran out of bio's */
1389 bi_size
= min_t(unsigned int, bi
->bi_iter
.bi_size
- off
, len
);
1390 bio
= bio_clone_range(bi
, off
, bi_size
, gfpmask
);
1392 goto out_err
; /* ENOMEM */
1395 end
= &bio
->bi_next
;
1398 if (off
== bi
->bi_iter
.bi_size
) {
1409 bio_chain_put(chain
);
1415 * The default/initial value for all object request flags is 0. For
1416 * each flag, once its value is set to 1 it is never reset to 0
1419 static void obj_request_img_data_set(struct rbd_obj_request
*obj_request
)
1421 if (test_and_set_bit(OBJ_REQ_IMG_DATA
, &obj_request
->flags
)) {
1422 struct rbd_device
*rbd_dev
;
1424 rbd_dev
= obj_request
->img_request
->rbd_dev
;
1425 rbd_warn(rbd_dev
, "obj_request %p already marked img_data",
1430 static bool obj_request_img_data_test(struct rbd_obj_request
*obj_request
)
1433 return test_bit(OBJ_REQ_IMG_DATA
, &obj_request
->flags
) != 0;
1436 static void obj_request_done_set(struct rbd_obj_request
*obj_request
)
1438 if (test_and_set_bit(OBJ_REQ_DONE
, &obj_request
->flags
)) {
1439 struct rbd_device
*rbd_dev
= NULL
;
1441 if (obj_request_img_data_test(obj_request
))
1442 rbd_dev
= obj_request
->img_request
->rbd_dev
;
1443 rbd_warn(rbd_dev
, "obj_request %p already marked done",
1448 static bool obj_request_done_test(struct rbd_obj_request
*obj_request
)
1451 return test_bit(OBJ_REQ_DONE
, &obj_request
->flags
) != 0;
1455 * This sets the KNOWN flag after (possibly) setting the EXISTS
1456 * flag. The latter is set based on the "exists" value provided.
1458 * Note that for our purposes once an object exists it never goes
1459 * away again. It's possible that the response from two existence
1460 * checks are separated by the creation of the target object, and
1461 * the first ("doesn't exist") response arrives *after* the second
1462 * ("does exist"). In that case we ignore the second one.
1464 static void obj_request_existence_set(struct rbd_obj_request
*obj_request
,
1468 set_bit(OBJ_REQ_EXISTS
, &obj_request
->flags
);
1469 set_bit(OBJ_REQ_KNOWN
, &obj_request
->flags
);
1473 static bool obj_request_known_test(struct rbd_obj_request
*obj_request
)
1476 return test_bit(OBJ_REQ_KNOWN
, &obj_request
->flags
) != 0;
1479 static bool obj_request_exists_test(struct rbd_obj_request
*obj_request
)
1482 return test_bit(OBJ_REQ_EXISTS
, &obj_request
->flags
) != 0;
1485 static bool obj_request_overlaps_parent(struct rbd_obj_request
*obj_request
)
1487 struct rbd_device
*rbd_dev
= obj_request
->img_request
->rbd_dev
;
1489 return obj_request
->img_offset
<
1490 round_up(rbd_dev
->parent_overlap
, rbd_obj_bytes(&rbd_dev
->header
));
1493 static void rbd_obj_request_get(struct rbd_obj_request
*obj_request
)
1495 dout("%s: obj %p (was %d)\n", __func__
, obj_request
,
1496 kref_read(&obj_request
->kref
));
1497 kref_get(&obj_request
->kref
);
1500 static void rbd_obj_request_destroy(struct kref
*kref
);
1501 static void rbd_obj_request_put(struct rbd_obj_request
*obj_request
)
1503 rbd_assert(obj_request
!= NULL
);
1504 dout("%s: obj %p (was %d)\n", __func__
, obj_request
,
1505 kref_read(&obj_request
->kref
));
1506 kref_put(&obj_request
->kref
, rbd_obj_request_destroy
);
1509 static void rbd_img_request_get(struct rbd_img_request
*img_request
)
1511 dout("%s: img %p (was %d)\n", __func__
, img_request
,
1512 kref_read(&img_request
->kref
));
1513 kref_get(&img_request
->kref
);
1516 static bool img_request_child_test(struct rbd_img_request
*img_request
);
1517 static void rbd_parent_request_destroy(struct kref
*kref
);
1518 static void rbd_img_request_destroy(struct kref
*kref
);
1519 static void rbd_img_request_put(struct rbd_img_request
*img_request
)
1521 rbd_assert(img_request
!= NULL
);
1522 dout("%s: img %p (was %d)\n", __func__
, img_request
,
1523 kref_read(&img_request
->kref
));
1524 if (img_request_child_test(img_request
))
1525 kref_put(&img_request
->kref
, rbd_parent_request_destroy
);
1527 kref_put(&img_request
->kref
, rbd_img_request_destroy
);
1530 static inline void rbd_img_obj_request_add(struct rbd_img_request
*img_request
,
1531 struct rbd_obj_request
*obj_request
)
1533 rbd_assert(obj_request
->img_request
== NULL
);
1535 /* Image request now owns object's original reference */
1536 obj_request
->img_request
= img_request
;
1537 obj_request
->which
= img_request
->obj_request_count
;
1538 rbd_assert(!obj_request_img_data_test(obj_request
));
1539 obj_request_img_data_set(obj_request
);
1540 rbd_assert(obj_request
->which
!= BAD_WHICH
);
1541 img_request
->obj_request_count
++;
1542 list_add_tail(&obj_request
->links
, &img_request
->obj_requests
);
1543 dout("%s: img %p obj %p w=%u\n", __func__
, img_request
, obj_request
,
1544 obj_request
->which
);
1547 static inline void rbd_img_obj_request_del(struct rbd_img_request
*img_request
,
1548 struct rbd_obj_request
*obj_request
)
1550 rbd_assert(obj_request
->which
!= BAD_WHICH
);
1552 dout("%s: img %p obj %p w=%u\n", __func__
, img_request
, obj_request
,
1553 obj_request
->which
);
1554 list_del(&obj_request
->links
);
1555 rbd_assert(img_request
->obj_request_count
> 0);
1556 img_request
->obj_request_count
--;
1557 rbd_assert(obj_request
->which
== img_request
->obj_request_count
);
1558 obj_request
->which
= BAD_WHICH
;
1559 rbd_assert(obj_request_img_data_test(obj_request
));
1560 rbd_assert(obj_request
->img_request
== img_request
);
1561 obj_request
->img_request
= NULL
;
1562 obj_request
->callback
= NULL
;
1563 rbd_obj_request_put(obj_request
);
1566 static bool obj_request_type_valid(enum obj_request_type type
)
1569 case OBJ_REQUEST_NODATA
:
1570 case OBJ_REQUEST_BIO
:
1571 case OBJ_REQUEST_PAGES
:
1578 static void rbd_img_obj_callback(struct rbd_obj_request
*obj_request
);
1580 static void rbd_obj_request_submit(struct rbd_obj_request
*obj_request
)
1582 struct ceph_osd_request
*osd_req
= obj_request
->osd_req
;
1584 dout("%s %p object_no %016llx %llu~%llu osd_req %p\n", __func__
,
1585 obj_request
, obj_request
->object_no
, obj_request
->offset
,
1586 obj_request
->length
, osd_req
);
1587 if (obj_request_img_data_test(obj_request
)) {
1588 WARN_ON(obj_request
->callback
!= rbd_img_obj_callback
);
1589 rbd_img_request_get(obj_request
->img_request
);
1591 ceph_osdc_start_request(osd_req
->r_osdc
, osd_req
, false);
1594 static void rbd_img_request_complete(struct rbd_img_request
*img_request
)
1597 dout("%s: img %p\n", __func__
, img_request
);
1600 * If no error occurred, compute the aggregate transfer
1601 * count for the image request. We could instead use
1602 * atomic64_cmpxchg() to update it as each object request
1603 * completes; not clear which way is better off hand.
1605 if (!img_request
->result
) {
1606 struct rbd_obj_request
*obj_request
;
1609 for_each_obj_request(img_request
, obj_request
)
1610 xferred
+= obj_request
->xferred
;
1611 img_request
->xferred
= xferred
;
1614 if (img_request
->callback
)
1615 img_request
->callback(img_request
);
1617 rbd_img_request_put(img_request
);
1621 * The default/initial value for all image request flags is 0. Each
1622 * is conditionally set to 1 at image request initialization time
1623 * and currently never change thereafter.
1625 static void img_request_write_set(struct rbd_img_request
*img_request
)
1627 set_bit(IMG_REQ_WRITE
, &img_request
->flags
);
1631 static bool img_request_write_test(struct rbd_img_request
*img_request
)
1634 return test_bit(IMG_REQ_WRITE
, &img_request
->flags
) != 0;
1638 * Set the discard flag when the img_request is an discard request
1640 static void img_request_discard_set(struct rbd_img_request
*img_request
)
1642 set_bit(IMG_REQ_DISCARD
, &img_request
->flags
);
1646 static bool img_request_discard_test(struct rbd_img_request
*img_request
)
1649 return test_bit(IMG_REQ_DISCARD
, &img_request
->flags
) != 0;
1652 static void img_request_child_set(struct rbd_img_request
*img_request
)
1654 set_bit(IMG_REQ_CHILD
, &img_request
->flags
);
1658 static void img_request_child_clear(struct rbd_img_request
*img_request
)
1660 clear_bit(IMG_REQ_CHILD
, &img_request
->flags
);
1664 static bool img_request_child_test(struct rbd_img_request
*img_request
)
1667 return test_bit(IMG_REQ_CHILD
, &img_request
->flags
) != 0;
1670 static void img_request_layered_set(struct rbd_img_request
*img_request
)
1672 set_bit(IMG_REQ_LAYERED
, &img_request
->flags
);
1676 static void img_request_layered_clear(struct rbd_img_request
*img_request
)
1678 clear_bit(IMG_REQ_LAYERED
, &img_request
->flags
);
1682 static bool img_request_layered_test(struct rbd_img_request
*img_request
)
1685 return test_bit(IMG_REQ_LAYERED
, &img_request
->flags
) != 0;
1688 static enum obj_operation_type
1689 rbd_img_request_op_type(struct rbd_img_request
*img_request
)
1691 if (img_request_write_test(img_request
))
1692 return OBJ_OP_WRITE
;
1693 else if (img_request_discard_test(img_request
))
1694 return OBJ_OP_DISCARD
;
1700 rbd_img_obj_request_read_callback(struct rbd_obj_request
*obj_request
)
1702 u64 xferred
= obj_request
->xferred
;
1703 u64 length
= obj_request
->length
;
1705 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__
,
1706 obj_request
, obj_request
->img_request
, obj_request
->result
,
1709 * ENOENT means a hole in the image. We zero-fill the entire
1710 * length of the request. A short read also implies zero-fill
1711 * to the end of the request. An error requires the whole
1712 * length of the request to be reported finished with an error
1713 * to the block layer. In each case we update the xferred
1714 * count to indicate the whole request was satisfied.
1716 rbd_assert(obj_request
->type
!= OBJ_REQUEST_NODATA
);
1717 if (obj_request
->result
== -ENOENT
) {
1718 if (obj_request
->type
== OBJ_REQUEST_BIO
)
1719 zero_bio_chain(obj_request
->bio_list
, 0);
1721 zero_pages(obj_request
->pages
, 0, length
);
1722 obj_request
->result
= 0;
1723 } else if (xferred
< length
&& !obj_request
->result
) {
1724 if (obj_request
->type
== OBJ_REQUEST_BIO
)
1725 zero_bio_chain(obj_request
->bio_list
, xferred
);
1727 zero_pages(obj_request
->pages
, xferred
, length
);
1729 obj_request
->xferred
= length
;
1730 obj_request_done_set(obj_request
);
1733 static void rbd_obj_request_complete(struct rbd_obj_request
*obj_request
)
1735 dout("%s: obj %p cb %p\n", __func__
, obj_request
,
1736 obj_request
->callback
);
1737 if (obj_request
->callback
)
1738 obj_request
->callback(obj_request
);
1740 complete_all(&obj_request
->completion
);
1743 static void rbd_obj_request_error(struct rbd_obj_request
*obj_request
, int err
)
1745 obj_request
->result
= err
;
1746 obj_request
->xferred
= 0;
1748 * kludge - mirror rbd_obj_request_submit() to match a put in
1749 * rbd_img_obj_callback()
1751 if (obj_request_img_data_test(obj_request
)) {
1752 WARN_ON(obj_request
->callback
!= rbd_img_obj_callback
);
1753 rbd_img_request_get(obj_request
->img_request
);
1755 obj_request_done_set(obj_request
);
1756 rbd_obj_request_complete(obj_request
);
1759 static void rbd_osd_read_callback(struct rbd_obj_request
*obj_request
)
1761 struct rbd_img_request
*img_request
= NULL
;
1762 struct rbd_device
*rbd_dev
= NULL
;
1763 bool layered
= false;
1765 if (obj_request_img_data_test(obj_request
)) {
1766 img_request
= obj_request
->img_request
;
1767 layered
= img_request
&& img_request_layered_test(img_request
);
1768 rbd_dev
= img_request
->rbd_dev
;
1771 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__
,
1772 obj_request
, img_request
, obj_request
->result
,
1773 obj_request
->xferred
, obj_request
->length
);
1774 if (layered
&& obj_request
->result
== -ENOENT
&&
1775 obj_request
->img_offset
< rbd_dev
->parent_overlap
)
1776 rbd_img_parent_read(obj_request
);
1777 else if (img_request
)
1778 rbd_img_obj_request_read_callback(obj_request
);
1780 obj_request_done_set(obj_request
);
1783 static void rbd_osd_write_callback(struct rbd_obj_request
*obj_request
)
1785 dout("%s: obj %p result %d %llu\n", __func__
, obj_request
,
1786 obj_request
->result
, obj_request
->length
);
1788 * There is no such thing as a successful short write. Set
1789 * it to our originally-requested length.
1791 obj_request
->xferred
= obj_request
->length
;
1792 obj_request_done_set(obj_request
);
1795 static void rbd_osd_discard_callback(struct rbd_obj_request
*obj_request
)
1797 dout("%s: obj %p result %d %llu\n", __func__
, obj_request
,
1798 obj_request
->result
, obj_request
->length
);
1800 * There is no such thing as a successful short discard. Set
1801 * it to our originally-requested length.
1803 obj_request
->xferred
= obj_request
->length
;
1804 /* discarding a non-existent object is not a problem */
1805 if (obj_request
->result
== -ENOENT
)
1806 obj_request
->result
= 0;
1807 obj_request_done_set(obj_request
);
1811 * For a simple stat call there's nothing to do. We'll do more if
1812 * this is part of a write sequence for a layered image.
1814 static void rbd_osd_stat_callback(struct rbd_obj_request
*obj_request
)
1816 dout("%s: obj %p\n", __func__
, obj_request
);
1817 obj_request_done_set(obj_request
);
1820 static void rbd_osd_call_callback(struct rbd_obj_request
*obj_request
)
1822 dout("%s: obj %p\n", __func__
, obj_request
);
1824 if (obj_request_img_data_test(obj_request
))
1825 rbd_osd_copyup_callback(obj_request
);
1827 obj_request_done_set(obj_request
);
1830 static void rbd_osd_req_callback(struct ceph_osd_request
*osd_req
)
1832 struct rbd_obj_request
*obj_request
= osd_req
->r_priv
;
1835 dout("%s: osd_req %p\n", __func__
, osd_req
);
1836 rbd_assert(osd_req
== obj_request
->osd_req
);
1837 if (obj_request_img_data_test(obj_request
)) {
1838 rbd_assert(obj_request
->img_request
);
1839 rbd_assert(obj_request
->which
!= BAD_WHICH
);
1841 rbd_assert(obj_request
->which
== BAD_WHICH
);
1844 if (osd_req
->r_result
< 0)
1845 obj_request
->result
= osd_req
->r_result
;
1848 * We support a 64-bit length, but ultimately it has to be
1849 * passed to the block layer, which just supports a 32-bit
1852 obj_request
->xferred
= osd_req
->r_ops
[0].outdata_len
;
1853 rbd_assert(obj_request
->xferred
< (u64
)UINT_MAX
);
1855 opcode
= osd_req
->r_ops
[0].op
;
1857 case CEPH_OSD_OP_READ
:
1858 rbd_osd_read_callback(obj_request
);
1860 case CEPH_OSD_OP_SETALLOCHINT
:
1861 rbd_assert(osd_req
->r_ops
[1].op
== CEPH_OSD_OP_WRITE
||
1862 osd_req
->r_ops
[1].op
== CEPH_OSD_OP_WRITEFULL
);
1864 case CEPH_OSD_OP_WRITE
:
1865 case CEPH_OSD_OP_WRITEFULL
:
1866 rbd_osd_write_callback(obj_request
);
1868 case CEPH_OSD_OP_STAT
:
1869 rbd_osd_stat_callback(obj_request
);
1871 case CEPH_OSD_OP_DELETE
:
1872 case CEPH_OSD_OP_TRUNCATE
:
1873 case CEPH_OSD_OP_ZERO
:
1874 rbd_osd_discard_callback(obj_request
);
1876 case CEPH_OSD_OP_CALL
:
1877 rbd_osd_call_callback(obj_request
);
1880 rbd_warn(NULL
, "unexpected OSD op: object_no %016llx opcode %d",
1881 obj_request
->object_no
, opcode
);
1885 if (obj_request_done_test(obj_request
))
1886 rbd_obj_request_complete(obj_request
);
1889 static void rbd_osd_req_format_read(struct rbd_obj_request
*obj_request
)
1891 struct ceph_osd_request
*osd_req
= obj_request
->osd_req
;
1893 rbd_assert(obj_request_img_data_test(obj_request
));
1894 osd_req
->r_snapid
= obj_request
->img_request
->snap_id
;
1897 static void rbd_osd_req_format_write(struct rbd_obj_request
*obj_request
)
1899 struct ceph_osd_request
*osd_req
= obj_request
->osd_req
;
1901 ktime_get_real_ts(&osd_req
->r_mtime
);
1902 osd_req
->r_data_offset
= obj_request
->offset
;
1905 static struct ceph_osd_request
*
1906 __rbd_osd_req_create(struct rbd_device
*rbd_dev
,
1907 struct ceph_snap_context
*snapc
,
1908 int num_ops
, unsigned int flags
,
1909 struct rbd_obj_request
*obj_request
)
1911 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
1912 struct ceph_osd_request
*req
;
1913 const char *name_format
= rbd_dev
->image_format
== 1 ?
1914 RBD_V1_DATA_FORMAT
: RBD_V2_DATA_FORMAT
;
1916 req
= ceph_osdc_alloc_request(osdc
, snapc
, num_ops
, false, GFP_NOIO
);
1920 req
->r_flags
= flags
;
1921 req
->r_callback
= rbd_osd_req_callback
;
1922 req
->r_priv
= obj_request
;
1924 req
->r_base_oloc
.pool
= rbd_dev
->layout
.pool_id
;
1925 if (ceph_oid_aprintf(&req
->r_base_oid
, GFP_NOIO
, name_format
,
1926 rbd_dev
->header
.object_prefix
, obj_request
->object_no
))
1929 if (ceph_osdc_alloc_messages(req
, GFP_NOIO
))
1935 ceph_osdc_put_request(req
);
1940 * Create an osd request. A read request has one osd op (read).
1941 * A write request has either one (watch) or two (hint+write) osd ops.
1942 * (All rbd data writes are prefixed with an allocation hint op, but
1943 * technically osd watch is a write request, hence this distinction.)
1945 static struct ceph_osd_request
*rbd_osd_req_create(
1946 struct rbd_device
*rbd_dev
,
1947 enum obj_operation_type op_type
,
1948 unsigned int num_ops
,
1949 struct rbd_obj_request
*obj_request
)
1951 struct ceph_snap_context
*snapc
= NULL
;
1953 if (obj_request_img_data_test(obj_request
) &&
1954 (op_type
== OBJ_OP_DISCARD
|| op_type
== OBJ_OP_WRITE
)) {
1955 struct rbd_img_request
*img_request
= obj_request
->img_request
;
1956 if (op_type
== OBJ_OP_WRITE
) {
1957 rbd_assert(img_request_write_test(img_request
));
1959 rbd_assert(img_request_discard_test(img_request
));
1961 snapc
= img_request
->snapc
;
1964 rbd_assert(num_ops
== 1 || ((op_type
== OBJ_OP_WRITE
) && num_ops
== 2));
1966 return __rbd_osd_req_create(rbd_dev
, snapc
, num_ops
,
1967 (op_type
== OBJ_OP_WRITE
|| op_type
== OBJ_OP_DISCARD
) ?
1968 CEPH_OSD_FLAG_WRITE
: CEPH_OSD_FLAG_READ
, obj_request
);
1972 * Create a copyup osd request based on the information in the object
1973 * request supplied. A copyup request has two or three osd ops, a
1974 * copyup method call, potentially a hint op, and a write or truncate
1977 static struct ceph_osd_request
*
1978 rbd_osd_req_create_copyup(struct rbd_obj_request
*obj_request
)
1980 struct rbd_img_request
*img_request
;
1981 int num_osd_ops
= 3;
1983 rbd_assert(obj_request_img_data_test(obj_request
));
1984 img_request
= obj_request
->img_request
;
1985 rbd_assert(img_request
);
1986 rbd_assert(img_request_write_test(img_request
) ||
1987 img_request_discard_test(img_request
));
1989 if (img_request_discard_test(img_request
))
1992 return __rbd_osd_req_create(img_request
->rbd_dev
,
1993 img_request
->snapc
, num_osd_ops
,
1994 CEPH_OSD_FLAG_WRITE
, obj_request
);
1997 static void rbd_osd_req_destroy(struct ceph_osd_request
*osd_req
)
1999 ceph_osdc_put_request(osd_req
);
2002 static struct rbd_obj_request
*
2003 rbd_obj_request_create(enum obj_request_type type
)
2005 struct rbd_obj_request
*obj_request
;
2007 rbd_assert(obj_request_type_valid(type
));
2009 obj_request
= kmem_cache_zalloc(rbd_obj_request_cache
, GFP_NOIO
);
2013 obj_request
->which
= BAD_WHICH
;
2014 obj_request
->type
= type
;
2015 INIT_LIST_HEAD(&obj_request
->links
);
2016 init_completion(&obj_request
->completion
);
2017 kref_init(&obj_request
->kref
);
2019 dout("%s %p\n", __func__
, obj_request
);
2023 static void rbd_obj_request_destroy(struct kref
*kref
)
2025 struct rbd_obj_request
*obj_request
;
2027 obj_request
= container_of(kref
, struct rbd_obj_request
, kref
);
2029 dout("%s: obj %p\n", __func__
, obj_request
);
2031 rbd_assert(obj_request
->img_request
== NULL
);
2032 rbd_assert(obj_request
->which
== BAD_WHICH
);
2034 if (obj_request
->osd_req
)
2035 rbd_osd_req_destroy(obj_request
->osd_req
);
2037 rbd_assert(obj_request_type_valid(obj_request
->type
));
2038 switch (obj_request
->type
) {
2039 case OBJ_REQUEST_NODATA
:
2040 break; /* Nothing to do */
2041 case OBJ_REQUEST_BIO
:
2042 if (obj_request
->bio_list
)
2043 bio_chain_put(obj_request
->bio_list
);
2045 case OBJ_REQUEST_PAGES
:
2046 /* img_data requests don't own their page array */
2047 if (obj_request
->pages
&&
2048 !obj_request_img_data_test(obj_request
))
2049 ceph_release_page_vector(obj_request
->pages
,
2050 obj_request
->page_count
);
2054 kmem_cache_free(rbd_obj_request_cache
, obj_request
);
2057 /* It's OK to call this for a device with no parent */
2059 static void rbd_spec_put(struct rbd_spec
*spec
);
2060 static void rbd_dev_unparent(struct rbd_device
*rbd_dev
)
2062 rbd_dev_remove_parent(rbd_dev
);
2063 rbd_spec_put(rbd_dev
->parent_spec
);
2064 rbd_dev
->parent_spec
= NULL
;
2065 rbd_dev
->parent_overlap
= 0;
2069 * Parent image reference counting is used to determine when an
2070 * image's parent fields can be safely torn down--after there are no
2071 * more in-flight requests to the parent image. When the last
2072 * reference is dropped, cleaning them up is safe.
2074 static void rbd_dev_parent_put(struct rbd_device
*rbd_dev
)
2078 if (!rbd_dev
->parent_spec
)
2081 counter
= atomic_dec_return_safe(&rbd_dev
->parent_ref
);
2085 /* Last reference; clean up parent data structures */
2088 rbd_dev_unparent(rbd_dev
);
2090 rbd_warn(rbd_dev
, "parent reference underflow");
2094 * If an image has a non-zero parent overlap, get a reference to its
2097 * Returns true if the rbd device has a parent with a non-zero
2098 * overlap and a reference for it was successfully taken, or
2101 static bool rbd_dev_parent_get(struct rbd_device
*rbd_dev
)
2105 if (!rbd_dev
->parent_spec
)
2108 down_read(&rbd_dev
->header_rwsem
);
2109 if (rbd_dev
->parent_overlap
)
2110 counter
= atomic_inc_return_safe(&rbd_dev
->parent_ref
);
2111 up_read(&rbd_dev
->header_rwsem
);
2114 rbd_warn(rbd_dev
, "parent reference overflow");
2120 * Caller is responsible for filling in the list of object requests
2121 * that comprises the image request, and the Linux request pointer
2122 * (if there is one).
2124 static struct rbd_img_request
*rbd_img_request_create(
2125 struct rbd_device
*rbd_dev
,
2126 u64 offset
, u64 length
,
2127 enum obj_operation_type op_type
,
2128 struct ceph_snap_context
*snapc
)
2130 struct rbd_img_request
*img_request
;
2132 img_request
= kmem_cache_alloc(rbd_img_request_cache
, GFP_NOIO
);
2136 img_request
->rq
= NULL
;
2137 img_request
->rbd_dev
= rbd_dev
;
2138 img_request
->offset
= offset
;
2139 img_request
->length
= length
;
2140 img_request
->flags
= 0;
2141 if (op_type
== OBJ_OP_DISCARD
) {
2142 img_request_discard_set(img_request
);
2143 img_request
->snapc
= snapc
;
2144 } else if (op_type
== OBJ_OP_WRITE
) {
2145 img_request_write_set(img_request
);
2146 img_request
->snapc
= snapc
;
2148 img_request
->snap_id
= rbd_dev
->spec
->snap_id
;
2150 if (rbd_dev_parent_get(rbd_dev
))
2151 img_request_layered_set(img_request
);
2152 spin_lock_init(&img_request
->completion_lock
);
2153 img_request
->next_completion
= 0;
2154 img_request
->callback
= NULL
;
2155 img_request
->result
= 0;
2156 img_request
->obj_request_count
= 0;
2157 INIT_LIST_HEAD(&img_request
->obj_requests
);
2158 kref_init(&img_request
->kref
);
2160 dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__
, rbd_dev
,
2161 obj_op_name(op_type
), offset
, length
, img_request
);
2166 static void rbd_img_request_destroy(struct kref
*kref
)
2168 struct rbd_img_request
*img_request
;
2169 struct rbd_obj_request
*obj_request
;
2170 struct rbd_obj_request
*next_obj_request
;
2172 img_request
= container_of(kref
, struct rbd_img_request
, kref
);
2174 dout("%s: img %p\n", __func__
, img_request
);
2176 for_each_obj_request_safe(img_request
, obj_request
, next_obj_request
)
2177 rbd_img_obj_request_del(img_request
, obj_request
);
2178 rbd_assert(img_request
->obj_request_count
== 0);
2180 if (img_request_layered_test(img_request
)) {
2181 img_request_layered_clear(img_request
);
2182 rbd_dev_parent_put(img_request
->rbd_dev
);
2185 if (img_request_write_test(img_request
) ||
2186 img_request_discard_test(img_request
))
2187 ceph_put_snap_context(img_request
->snapc
);
2189 kmem_cache_free(rbd_img_request_cache
, img_request
);
2192 static struct rbd_img_request
*rbd_parent_request_create(
2193 struct rbd_obj_request
*obj_request
,
2194 u64 img_offset
, u64 length
)
2196 struct rbd_img_request
*parent_request
;
2197 struct rbd_device
*rbd_dev
;
2199 rbd_assert(obj_request
->img_request
);
2200 rbd_dev
= obj_request
->img_request
->rbd_dev
;
2202 parent_request
= rbd_img_request_create(rbd_dev
->parent
, img_offset
,
2203 length
, OBJ_OP_READ
, NULL
);
2204 if (!parent_request
)
2207 img_request_child_set(parent_request
);
2208 rbd_obj_request_get(obj_request
);
2209 parent_request
->obj_request
= obj_request
;
2211 return parent_request
;
2214 static void rbd_parent_request_destroy(struct kref
*kref
)
2216 struct rbd_img_request
*parent_request
;
2217 struct rbd_obj_request
*orig_request
;
2219 parent_request
= container_of(kref
, struct rbd_img_request
, kref
);
2220 orig_request
= parent_request
->obj_request
;
2222 parent_request
->obj_request
= NULL
;
2223 rbd_obj_request_put(orig_request
);
2224 img_request_child_clear(parent_request
);
2226 rbd_img_request_destroy(kref
);
2229 static bool rbd_img_obj_end_request(struct rbd_obj_request
*obj_request
)
2231 struct rbd_img_request
*img_request
;
2232 unsigned int xferred
;
2236 rbd_assert(obj_request_img_data_test(obj_request
));
2237 img_request
= obj_request
->img_request
;
2239 rbd_assert(obj_request
->xferred
<= (u64
)UINT_MAX
);
2240 xferred
= (unsigned int)obj_request
->xferred
;
2241 result
= obj_request
->result
;
2243 struct rbd_device
*rbd_dev
= img_request
->rbd_dev
;
2244 enum obj_operation_type op_type
;
2246 if (img_request_discard_test(img_request
))
2247 op_type
= OBJ_OP_DISCARD
;
2248 else if (img_request_write_test(img_request
))
2249 op_type
= OBJ_OP_WRITE
;
2251 op_type
= OBJ_OP_READ
;
2253 rbd_warn(rbd_dev
, "%s %llx at %llx (%llx)",
2254 obj_op_name(op_type
), obj_request
->length
,
2255 obj_request
->img_offset
, obj_request
->offset
);
2256 rbd_warn(rbd_dev
, " result %d xferred %x",
2258 if (!img_request
->result
)
2259 img_request
->result
= result
;
2261 * Need to end I/O on the entire obj_request worth of
2262 * bytes in case of error.
2264 xferred
= obj_request
->length
;
2267 if (img_request_child_test(img_request
)) {
2268 rbd_assert(img_request
->obj_request
!= NULL
);
2269 more
= obj_request
->which
< img_request
->obj_request_count
- 1;
2271 blk_status_t status
= errno_to_blk_status(result
);
2273 rbd_assert(img_request
->rq
!= NULL
);
2275 more
= blk_update_request(img_request
->rq
, status
, xferred
);
2277 __blk_mq_end_request(img_request
->rq
, status
);
2283 static void rbd_img_obj_callback(struct rbd_obj_request
*obj_request
)
2285 struct rbd_img_request
*img_request
;
2286 u32 which
= obj_request
->which
;
2289 rbd_assert(obj_request_img_data_test(obj_request
));
2290 img_request
= obj_request
->img_request
;
2292 dout("%s: img %p obj %p\n", __func__
, img_request
, obj_request
);
2293 rbd_assert(img_request
!= NULL
);
2294 rbd_assert(img_request
->obj_request_count
> 0);
2295 rbd_assert(which
!= BAD_WHICH
);
2296 rbd_assert(which
< img_request
->obj_request_count
);
2298 spin_lock_irq(&img_request
->completion_lock
);
2299 if (which
!= img_request
->next_completion
)
2302 for_each_obj_request_from(img_request
, obj_request
) {
2304 rbd_assert(which
< img_request
->obj_request_count
);
2306 if (!obj_request_done_test(obj_request
))
2308 more
= rbd_img_obj_end_request(obj_request
);
2312 rbd_assert(more
^ (which
== img_request
->obj_request_count
));
2313 img_request
->next_completion
= which
;
2315 spin_unlock_irq(&img_request
->completion_lock
);
2316 rbd_img_request_put(img_request
);
2319 rbd_img_request_complete(img_request
);
2323 * Add individual osd ops to the given ceph_osd_request and prepare
2324 * them for submission. num_ops is the current number of
2325 * osd operations already to the object request.
2327 static void rbd_img_obj_request_fill(struct rbd_obj_request
*obj_request
,
2328 struct ceph_osd_request
*osd_request
,
2329 enum obj_operation_type op_type
,
2330 unsigned int num_ops
)
2332 struct rbd_img_request
*img_request
= obj_request
->img_request
;
2333 struct rbd_device
*rbd_dev
= img_request
->rbd_dev
;
2334 u64 object_size
= rbd_obj_bytes(&rbd_dev
->header
);
2335 u64 offset
= obj_request
->offset
;
2336 u64 length
= obj_request
->length
;
2340 if (op_type
== OBJ_OP_DISCARD
) {
2341 if (!offset
&& length
== object_size
&&
2342 (!img_request_layered_test(img_request
) ||
2343 !obj_request_overlaps_parent(obj_request
))) {
2344 opcode
= CEPH_OSD_OP_DELETE
;
2345 } else if ((offset
+ length
== object_size
)) {
2346 opcode
= CEPH_OSD_OP_TRUNCATE
;
2348 down_read(&rbd_dev
->header_rwsem
);
2349 img_end
= rbd_dev
->header
.image_size
;
2350 up_read(&rbd_dev
->header_rwsem
);
2352 if (obj_request
->img_offset
+ length
== img_end
)
2353 opcode
= CEPH_OSD_OP_TRUNCATE
;
2355 opcode
= CEPH_OSD_OP_ZERO
;
2357 } else if (op_type
== OBJ_OP_WRITE
) {
2358 if (!offset
&& length
== object_size
)
2359 opcode
= CEPH_OSD_OP_WRITEFULL
;
2361 opcode
= CEPH_OSD_OP_WRITE
;
2362 osd_req_op_alloc_hint_init(osd_request
, num_ops
,
2363 object_size
, object_size
);
2366 opcode
= CEPH_OSD_OP_READ
;
2369 if (opcode
== CEPH_OSD_OP_DELETE
)
2370 osd_req_op_init(osd_request
, num_ops
, opcode
, 0);
2372 osd_req_op_extent_init(osd_request
, num_ops
, opcode
,
2373 offset
, length
, 0, 0);
2375 if (obj_request
->type
== OBJ_REQUEST_BIO
)
2376 osd_req_op_extent_osd_data_bio(osd_request
, num_ops
,
2377 obj_request
->bio_list
, length
);
2378 else if (obj_request
->type
== OBJ_REQUEST_PAGES
)
2379 osd_req_op_extent_osd_data_pages(osd_request
, num_ops
,
2380 obj_request
->pages
, length
,
2381 offset
& ~PAGE_MASK
, false, false);
2383 /* Discards are also writes */
2384 if (op_type
== OBJ_OP_WRITE
|| op_type
== OBJ_OP_DISCARD
)
2385 rbd_osd_req_format_write(obj_request
);
2387 rbd_osd_req_format_read(obj_request
);
2391 * Split up an image request into one or more object requests, each
2392 * to a different object. The "type" parameter indicates whether
2393 * "data_desc" is the pointer to the head of a list of bio
2394 * structures, or the base of a page array. In either case this
2395 * function assumes data_desc describes memory sufficient to hold
2396 * all data described by the image request.
2398 static int rbd_img_request_fill(struct rbd_img_request
*img_request
,
2399 enum obj_request_type type
,
2402 struct rbd_device
*rbd_dev
= img_request
->rbd_dev
;
2403 struct rbd_obj_request
*obj_request
= NULL
;
2404 struct rbd_obj_request
*next_obj_request
;
2405 struct bio
*bio_list
= NULL
;
2406 unsigned int bio_offset
= 0;
2407 struct page
**pages
= NULL
;
2408 enum obj_operation_type op_type
;
2412 dout("%s: img %p type %d data_desc %p\n", __func__
, img_request
,
2413 (int)type
, data_desc
);
2415 img_offset
= img_request
->offset
;
2416 resid
= img_request
->length
;
2417 rbd_assert(resid
> 0);
2418 op_type
= rbd_img_request_op_type(img_request
);
2420 if (type
== OBJ_REQUEST_BIO
) {
2421 bio_list
= data_desc
;
2422 rbd_assert(img_offset
==
2423 bio_list
->bi_iter
.bi_sector
<< SECTOR_SHIFT
);
2424 } else if (type
== OBJ_REQUEST_PAGES
) {
2429 struct ceph_osd_request
*osd_req
;
2430 u64 object_no
= img_offset
>> rbd_dev
->header
.obj_order
;
2431 u64 offset
= rbd_segment_offset(rbd_dev
, img_offset
);
2432 u64 length
= rbd_segment_length(rbd_dev
, img_offset
, resid
);
2434 obj_request
= rbd_obj_request_create(type
);
2438 obj_request
->object_no
= object_no
;
2439 obj_request
->offset
= offset
;
2440 obj_request
->length
= length
;
2443 * set obj_request->img_request before creating the
2444 * osd_request so that it gets the right snapc
2446 rbd_img_obj_request_add(img_request
, obj_request
);
2448 if (type
== OBJ_REQUEST_BIO
) {
2449 unsigned int clone_size
;
2451 rbd_assert(length
<= (u64
)UINT_MAX
);
2452 clone_size
= (unsigned int)length
;
2453 obj_request
->bio_list
=
2454 bio_chain_clone_range(&bio_list
,
2458 if (!obj_request
->bio_list
)
2460 } else if (type
== OBJ_REQUEST_PAGES
) {
2461 unsigned int page_count
;
2463 obj_request
->pages
= pages
;
2464 page_count
= (u32
)calc_pages_for(offset
, length
);
2465 obj_request
->page_count
= page_count
;
2466 if ((offset
+ length
) & ~PAGE_MASK
)
2467 page_count
--; /* more on last page */
2468 pages
+= page_count
;
2471 osd_req
= rbd_osd_req_create(rbd_dev
, op_type
,
2472 (op_type
== OBJ_OP_WRITE
) ? 2 : 1,
2477 obj_request
->osd_req
= osd_req
;
2478 obj_request
->callback
= rbd_img_obj_callback
;
2479 obj_request
->img_offset
= img_offset
;
2481 rbd_img_obj_request_fill(obj_request
, osd_req
, op_type
, 0);
2483 img_offset
+= length
;
2490 for_each_obj_request_safe(img_request
, obj_request
, next_obj_request
)
2491 rbd_img_obj_request_del(img_request
, obj_request
);
2497 rbd_osd_copyup_callback(struct rbd_obj_request
*obj_request
)
2499 struct rbd_img_request
*img_request
;
2500 struct rbd_device
*rbd_dev
;
2501 struct page
**pages
;
2504 dout("%s: obj %p\n", __func__
, obj_request
);
2506 rbd_assert(obj_request
->type
== OBJ_REQUEST_BIO
||
2507 obj_request
->type
== OBJ_REQUEST_NODATA
);
2508 rbd_assert(obj_request_img_data_test(obj_request
));
2509 img_request
= obj_request
->img_request
;
2510 rbd_assert(img_request
);
2512 rbd_dev
= img_request
->rbd_dev
;
2513 rbd_assert(rbd_dev
);
2515 pages
= obj_request
->copyup_pages
;
2516 rbd_assert(pages
!= NULL
);
2517 obj_request
->copyup_pages
= NULL
;
2518 page_count
= obj_request
->copyup_page_count
;
2519 rbd_assert(page_count
);
2520 obj_request
->copyup_page_count
= 0;
2521 ceph_release_page_vector(pages
, page_count
);
2524 * We want the transfer count to reflect the size of the
2525 * original write request. There is no such thing as a
2526 * successful short write, so if the request was successful
2527 * we can just set it to the originally-requested length.
2529 if (!obj_request
->result
)
2530 obj_request
->xferred
= obj_request
->length
;
2532 obj_request_done_set(obj_request
);
2536 rbd_img_obj_parent_read_full_callback(struct rbd_img_request
*img_request
)
2538 struct rbd_obj_request
*orig_request
;
2539 struct ceph_osd_request
*osd_req
;
2540 struct rbd_device
*rbd_dev
;
2541 struct page
**pages
;
2542 enum obj_operation_type op_type
;
2547 rbd_assert(img_request_child_test(img_request
));
2549 /* First get what we need from the image request */
2551 pages
= img_request
->copyup_pages
;
2552 rbd_assert(pages
!= NULL
);
2553 img_request
->copyup_pages
= NULL
;
2554 page_count
= img_request
->copyup_page_count
;
2555 rbd_assert(page_count
);
2556 img_request
->copyup_page_count
= 0;
2558 orig_request
= img_request
->obj_request
;
2559 rbd_assert(orig_request
!= NULL
);
2560 rbd_assert(obj_request_type_valid(orig_request
->type
));
2561 img_result
= img_request
->result
;
2562 parent_length
= img_request
->length
;
2563 rbd_assert(img_result
|| parent_length
== img_request
->xferred
);
2564 rbd_img_request_put(img_request
);
2566 rbd_assert(orig_request
->img_request
);
2567 rbd_dev
= orig_request
->img_request
->rbd_dev
;
2568 rbd_assert(rbd_dev
);
2571 * If the overlap has become 0 (most likely because the
2572 * image has been flattened) we need to free the pages
2573 * and re-submit the original write request.
2575 if (!rbd_dev
->parent_overlap
) {
2576 ceph_release_page_vector(pages
, page_count
);
2577 rbd_obj_request_submit(orig_request
);
2585 * The original osd request is of no use to use any more.
2586 * We need a new one that can hold the three ops in a copyup
2587 * request. Allocate the new copyup osd request for the
2588 * original request, and release the old one.
2590 img_result
= -ENOMEM
;
2591 osd_req
= rbd_osd_req_create_copyup(orig_request
);
2594 rbd_osd_req_destroy(orig_request
->osd_req
);
2595 orig_request
->osd_req
= osd_req
;
2596 orig_request
->copyup_pages
= pages
;
2597 orig_request
->copyup_page_count
= page_count
;
2599 /* Initialize the copyup op */
2601 osd_req_op_cls_init(osd_req
, 0, CEPH_OSD_OP_CALL
, "rbd", "copyup");
2602 osd_req_op_cls_request_data_pages(osd_req
, 0, pages
, parent_length
, 0,
2605 /* Add the other op(s) */
2607 op_type
= rbd_img_request_op_type(orig_request
->img_request
);
2608 rbd_img_obj_request_fill(orig_request
, osd_req
, op_type
, 1);
2610 /* All set, send it off. */
2612 rbd_obj_request_submit(orig_request
);
2616 ceph_release_page_vector(pages
, page_count
);
2617 rbd_obj_request_error(orig_request
, img_result
);
2621 * Read from the parent image the range of data that covers the
2622 * entire target of the given object request. This is used for
2623 * satisfying a layered image write request when the target of an
2624 * object request from the image request does not exist.
2626 * A page array big enough to hold the returned data is allocated
2627 * and supplied to rbd_img_request_fill() as the "data descriptor."
2628 * When the read completes, this page array will be transferred to
2629 * the original object request for the copyup operation.
2631 * If an error occurs, it is recorded as the result of the original
2632 * object request in rbd_img_obj_exists_callback().
2634 static int rbd_img_obj_parent_read_full(struct rbd_obj_request
*obj_request
)
2636 struct rbd_device
*rbd_dev
= obj_request
->img_request
->rbd_dev
;
2637 struct rbd_img_request
*parent_request
= NULL
;
2640 struct page
**pages
= NULL
;
2644 rbd_assert(rbd_dev
->parent
!= NULL
);
2647 * Determine the byte range covered by the object in the
2648 * child image to which the original request was to be sent.
2650 img_offset
= obj_request
->img_offset
- obj_request
->offset
;
2651 length
= rbd_obj_bytes(&rbd_dev
->header
);
2654 * There is no defined parent data beyond the parent
2655 * overlap, so limit what we read at that boundary if
2658 if (img_offset
+ length
> rbd_dev
->parent_overlap
) {
2659 rbd_assert(img_offset
< rbd_dev
->parent_overlap
);
2660 length
= rbd_dev
->parent_overlap
- img_offset
;
2664 * Allocate a page array big enough to receive the data read
2667 page_count
= (u32
)calc_pages_for(0, length
);
2668 pages
= ceph_alloc_page_vector(page_count
, GFP_NOIO
);
2669 if (IS_ERR(pages
)) {
2670 result
= PTR_ERR(pages
);
2676 parent_request
= rbd_parent_request_create(obj_request
,
2677 img_offset
, length
);
2678 if (!parent_request
)
2681 result
= rbd_img_request_fill(parent_request
, OBJ_REQUEST_PAGES
, pages
);
2685 parent_request
->copyup_pages
= pages
;
2686 parent_request
->copyup_page_count
= page_count
;
2687 parent_request
->callback
= rbd_img_obj_parent_read_full_callback
;
2689 result
= rbd_img_request_submit(parent_request
);
2693 parent_request
->copyup_pages
= NULL
;
2694 parent_request
->copyup_page_count
= 0;
2695 parent_request
->obj_request
= NULL
;
2696 rbd_obj_request_put(obj_request
);
2699 ceph_release_page_vector(pages
, page_count
);
2701 rbd_img_request_put(parent_request
);
2705 static void rbd_img_obj_exists_callback(struct rbd_obj_request
*obj_request
)
2707 struct rbd_obj_request
*orig_request
;
2708 struct rbd_device
*rbd_dev
;
2711 rbd_assert(!obj_request_img_data_test(obj_request
));
2714 * All we need from the object request is the original
2715 * request and the result of the STAT op. Grab those, then
2716 * we're done with the request.
2718 orig_request
= obj_request
->obj_request
;
2719 obj_request
->obj_request
= NULL
;
2720 rbd_obj_request_put(orig_request
);
2721 rbd_assert(orig_request
);
2722 rbd_assert(orig_request
->img_request
);
2724 result
= obj_request
->result
;
2725 obj_request
->result
= 0;
2727 dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__
,
2728 obj_request
, orig_request
, result
,
2729 obj_request
->xferred
, obj_request
->length
);
2730 rbd_obj_request_put(obj_request
);
2733 * If the overlap has become 0 (most likely because the
2734 * image has been flattened) we need to re-submit the
2737 rbd_dev
= orig_request
->img_request
->rbd_dev
;
2738 if (!rbd_dev
->parent_overlap
) {
2739 rbd_obj_request_submit(orig_request
);
2744 * Our only purpose here is to determine whether the object
2745 * exists, and we don't want to treat the non-existence as
2746 * an error. If something else comes back, transfer the
2747 * error to the original request and complete it now.
2750 obj_request_existence_set(orig_request
, true);
2751 } else if (result
== -ENOENT
) {
2752 obj_request_existence_set(orig_request
, false);
2754 goto fail_orig_request
;
2758 * Resubmit the original request now that we have recorded
2759 * whether the target object exists.
2761 result
= rbd_img_obj_request_submit(orig_request
);
2763 goto fail_orig_request
;
2768 rbd_obj_request_error(orig_request
, result
);
2771 static int rbd_img_obj_exists_submit(struct rbd_obj_request
*obj_request
)
2773 struct rbd_device
*rbd_dev
= obj_request
->img_request
->rbd_dev
;
2774 struct rbd_obj_request
*stat_request
;
2775 struct page
**pages
;
2780 stat_request
= rbd_obj_request_create(OBJ_REQUEST_PAGES
);
2784 stat_request
->object_no
= obj_request
->object_no
;
2786 stat_request
->osd_req
= rbd_osd_req_create(rbd_dev
, OBJ_OP_READ
, 1,
2788 if (!stat_request
->osd_req
) {
2790 goto fail_stat_request
;
2794 * The response data for a STAT call consists of:
2801 size
= sizeof (__le64
) + sizeof (__le32
) + sizeof (__le32
);
2802 page_count
= (u32
)calc_pages_for(0, size
);
2803 pages
= ceph_alloc_page_vector(page_count
, GFP_NOIO
);
2804 if (IS_ERR(pages
)) {
2805 ret
= PTR_ERR(pages
);
2806 goto fail_stat_request
;
2809 osd_req_op_init(stat_request
->osd_req
, 0, CEPH_OSD_OP_STAT
, 0);
2810 osd_req_op_raw_data_in_pages(stat_request
->osd_req
, 0, pages
, size
, 0,
2813 rbd_obj_request_get(obj_request
);
2814 stat_request
->obj_request
= obj_request
;
2815 stat_request
->pages
= pages
;
2816 stat_request
->page_count
= page_count
;
2817 stat_request
->callback
= rbd_img_obj_exists_callback
;
2819 rbd_obj_request_submit(stat_request
);
2823 rbd_obj_request_put(stat_request
);
2827 static bool img_obj_request_simple(struct rbd_obj_request
*obj_request
)
2829 struct rbd_img_request
*img_request
= obj_request
->img_request
;
2830 struct rbd_device
*rbd_dev
= img_request
->rbd_dev
;
2833 if (!img_request_write_test(img_request
) &&
2834 !img_request_discard_test(img_request
))
2837 /* Non-layered writes */
2838 if (!img_request_layered_test(img_request
))
2842 * Layered writes outside of the parent overlap range don't
2843 * share any data with the parent.
2845 if (!obj_request_overlaps_parent(obj_request
))
2849 * Entire-object layered writes - we will overwrite whatever
2850 * parent data there is anyway.
2852 if (!obj_request
->offset
&&
2853 obj_request
->length
== rbd_obj_bytes(&rbd_dev
->header
))
2857 * If the object is known to already exist, its parent data has
2858 * already been copied.
2860 if (obj_request_known_test(obj_request
) &&
2861 obj_request_exists_test(obj_request
))
2867 static int rbd_img_obj_request_submit(struct rbd_obj_request
*obj_request
)
2869 rbd_assert(obj_request_img_data_test(obj_request
));
2870 rbd_assert(obj_request_type_valid(obj_request
->type
));
2871 rbd_assert(obj_request
->img_request
);
2873 if (img_obj_request_simple(obj_request
)) {
2874 rbd_obj_request_submit(obj_request
);
2879 * It's a layered write. The target object might exist but
2880 * we may not know that yet. If we know it doesn't exist,
2881 * start by reading the data for the full target object from
2882 * the parent so we can use it for a copyup to the target.
2884 if (obj_request_known_test(obj_request
))
2885 return rbd_img_obj_parent_read_full(obj_request
);
2887 /* We don't know whether the target exists. Go find out. */
2889 return rbd_img_obj_exists_submit(obj_request
);
2892 static int rbd_img_request_submit(struct rbd_img_request
*img_request
)
2894 struct rbd_obj_request
*obj_request
;
2895 struct rbd_obj_request
*next_obj_request
;
2898 dout("%s: img %p\n", __func__
, img_request
);
2900 rbd_img_request_get(img_request
);
2901 for_each_obj_request_safe(img_request
, obj_request
, next_obj_request
) {
2902 ret
= rbd_img_obj_request_submit(obj_request
);
2908 rbd_img_request_put(img_request
);
2912 static void rbd_img_parent_read_callback(struct rbd_img_request
*img_request
)
2914 struct rbd_obj_request
*obj_request
;
2915 struct rbd_device
*rbd_dev
;
2920 rbd_assert(img_request_child_test(img_request
));
2922 /* First get what we need from the image request and release it */
2924 obj_request
= img_request
->obj_request
;
2925 img_xferred
= img_request
->xferred
;
2926 img_result
= img_request
->result
;
2927 rbd_img_request_put(img_request
);
2930 * If the overlap has become 0 (most likely because the
2931 * image has been flattened) we need to re-submit the
2934 rbd_assert(obj_request
);
2935 rbd_assert(obj_request
->img_request
);
2936 rbd_dev
= obj_request
->img_request
->rbd_dev
;
2937 if (!rbd_dev
->parent_overlap
) {
2938 rbd_obj_request_submit(obj_request
);
2942 obj_request
->result
= img_result
;
2943 if (obj_request
->result
)
2947 * We need to zero anything beyond the parent overlap
2948 * boundary. Since rbd_img_obj_request_read_callback()
2949 * will zero anything beyond the end of a short read, an
2950 * easy way to do this is to pretend the data from the
2951 * parent came up short--ending at the overlap boundary.
2953 rbd_assert(obj_request
->img_offset
< U64_MAX
- obj_request
->length
);
2954 obj_end
= obj_request
->img_offset
+ obj_request
->length
;
2955 if (obj_end
> rbd_dev
->parent_overlap
) {
2958 if (obj_request
->img_offset
< rbd_dev
->parent_overlap
)
2959 xferred
= rbd_dev
->parent_overlap
-
2960 obj_request
->img_offset
;
2962 obj_request
->xferred
= min(img_xferred
, xferred
);
2964 obj_request
->xferred
= img_xferred
;
2967 rbd_img_obj_request_read_callback(obj_request
);
2968 rbd_obj_request_complete(obj_request
);
2971 static void rbd_img_parent_read(struct rbd_obj_request
*obj_request
)
2973 struct rbd_img_request
*img_request
;
2976 rbd_assert(obj_request_img_data_test(obj_request
));
2977 rbd_assert(obj_request
->img_request
!= NULL
);
2978 rbd_assert(obj_request
->result
== (s32
) -ENOENT
);
2979 rbd_assert(obj_request_type_valid(obj_request
->type
));
2981 /* rbd_read_finish(obj_request, obj_request->length); */
2982 img_request
= rbd_parent_request_create(obj_request
,
2983 obj_request
->img_offset
,
2984 obj_request
->length
);
2989 if (obj_request
->type
== OBJ_REQUEST_BIO
)
2990 result
= rbd_img_request_fill(img_request
, OBJ_REQUEST_BIO
,
2991 obj_request
->bio_list
);
2993 result
= rbd_img_request_fill(img_request
, OBJ_REQUEST_PAGES
,
2994 obj_request
->pages
);
2998 img_request
->callback
= rbd_img_parent_read_callback
;
2999 result
= rbd_img_request_submit(img_request
);
3006 rbd_img_request_put(img_request
);
3007 obj_request
->result
= result
;
3008 obj_request
->xferred
= 0;
3009 obj_request_done_set(obj_request
);
3012 static const struct rbd_client_id rbd_empty_cid
;
3014 static bool rbd_cid_equal(const struct rbd_client_id
*lhs
,
3015 const struct rbd_client_id
*rhs
)
3017 return lhs
->gid
== rhs
->gid
&& lhs
->handle
== rhs
->handle
;
3020 static struct rbd_client_id
rbd_get_cid(struct rbd_device
*rbd_dev
)
3022 struct rbd_client_id cid
;
3024 mutex_lock(&rbd_dev
->watch_mutex
);
3025 cid
.gid
= ceph_client_gid(rbd_dev
->rbd_client
->client
);
3026 cid
.handle
= rbd_dev
->watch_cookie
;
3027 mutex_unlock(&rbd_dev
->watch_mutex
);
3032 * lock_rwsem must be held for write
3034 static void rbd_set_owner_cid(struct rbd_device
*rbd_dev
,
3035 const struct rbd_client_id
*cid
)
3037 dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__
, rbd_dev
,
3038 rbd_dev
->owner_cid
.gid
, rbd_dev
->owner_cid
.handle
,
3039 cid
->gid
, cid
->handle
);
3040 rbd_dev
->owner_cid
= *cid
; /* struct */
3043 static void format_lock_cookie(struct rbd_device
*rbd_dev
, char *buf
)
3045 mutex_lock(&rbd_dev
->watch_mutex
);
3046 sprintf(buf
, "%s %llu", RBD_LOCK_COOKIE_PREFIX
, rbd_dev
->watch_cookie
);
3047 mutex_unlock(&rbd_dev
->watch_mutex
);
3050 static void __rbd_lock(struct rbd_device
*rbd_dev
, const char *cookie
)
3052 struct rbd_client_id cid
= rbd_get_cid(rbd_dev
);
3054 strcpy(rbd_dev
->lock_cookie
, cookie
);
3055 rbd_set_owner_cid(rbd_dev
, &cid
);
3056 queue_work(rbd_dev
->task_wq
, &rbd_dev
->acquired_lock_work
);
3060 * lock_rwsem must be held for write
3062 static int rbd_lock(struct rbd_device
*rbd_dev
)
3064 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
3068 WARN_ON(__rbd_is_lock_owner(rbd_dev
) ||
3069 rbd_dev
->lock_cookie
[0] != '\0');
3071 format_lock_cookie(rbd_dev
, cookie
);
3072 ret
= ceph_cls_lock(osdc
, &rbd_dev
->header_oid
, &rbd_dev
->header_oloc
,
3073 RBD_LOCK_NAME
, CEPH_CLS_LOCK_EXCLUSIVE
, cookie
,
3074 RBD_LOCK_TAG
, "", 0);
3078 rbd_dev
->lock_state
= RBD_LOCK_STATE_LOCKED
;
3079 __rbd_lock(rbd_dev
, cookie
);
3084 * lock_rwsem must be held for write
3086 static void rbd_unlock(struct rbd_device
*rbd_dev
)
3088 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
3091 WARN_ON(!__rbd_is_lock_owner(rbd_dev
) ||
3092 rbd_dev
->lock_cookie
[0] == '\0');
3094 ret
= ceph_cls_unlock(osdc
, &rbd_dev
->header_oid
, &rbd_dev
->header_oloc
,
3095 RBD_LOCK_NAME
, rbd_dev
->lock_cookie
);
3096 if (ret
&& ret
!= -ENOENT
)
3097 rbd_warn(rbd_dev
, "failed to unlock: %d", ret
);
3099 /* treat errors as the image is unlocked */
3100 rbd_dev
->lock_state
= RBD_LOCK_STATE_UNLOCKED
;
3101 rbd_dev
->lock_cookie
[0] = '\0';
3102 rbd_set_owner_cid(rbd_dev
, &rbd_empty_cid
);
3103 queue_work(rbd_dev
->task_wq
, &rbd_dev
->released_lock_work
);
3106 static int __rbd_notify_op_lock(struct rbd_device
*rbd_dev
,
3107 enum rbd_notify_op notify_op
,
3108 struct page
***preply_pages
,
3111 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
3112 struct rbd_client_id cid
= rbd_get_cid(rbd_dev
);
3113 int buf_size
= 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN
;
3117 dout("%s rbd_dev %p notify_op %d\n", __func__
, rbd_dev
, notify_op
);
3119 /* encode *LockPayload NotifyMessage (op + ClientId) */
3120 ceph_start_encoding(&p
, 2, 1, buf_size
- CEPH_ENCODING_START_BLK_LEN
);
3121 ceph_encode_32(&p
, notify_op
);
3122 ceph_encode_64(&p
, cid
.gid
);
3123 ceph_encode_64(&p
, cid
.handle
);
3125 return ceph_osdc_notify(osdc
, &rbd_dev
->header_oid
,
3126 &rbd_dev
->header_oloc
, buf
, buf_size
,
3127 RBD_NOTIFY_TIMEOUT
, preply_pages
, preply_len
);
3130 static void rbd_notify_op_lock(struct rbd_device
*rbd_dev
,
3131 enum rbd_notify_op notify_op
)
3133 struct page
**reply_pages
;
3136 __rbd_notify_op_lock(rbd_dev
, notify_op
, &reply_pages
, &reply_len
);
3137 ceph_release_page_vector(reply_pages
, calc_pages_for(0, reply_len
));
3140 static void rbd_notify_acquired_lock(struct work_struct
*work
)
3142 struct rbd_device
*rbd_dev
= container_of(work
, struct rbd_device
,
3143 acquired_lock_work
);
3145 rbd_notify_op_lock(rbd_dev
, RBD_NOTIFY_OP_ACQUIRED_LOCK
);
3148 static void rbd_notify_released_lock(struct work_struct
*work
)
3150 struct rbd_device
*rbd_dev
= container_of(work
, struct rbd_device
,
3151 released_lock_work
);
3153 rbd_notify_op_lock(rbd_dev
, RBD_NOTIFY_OP_RELEASED_LOCK
);
3156 static int rbd_request_lock(struct rbd_device
*rbd_dev
)
3158 struct page
**reply_pages
;
3160 bool lock_owner_responded
= false;
3163 dout("%s rbd_dev %p\n", __func__
, rbd_dev
);
3165 ret
= __rbd_notify_op_lock(rbd_dev
, RBD_NOTIFY_OP_REQUEST_LOCK
,
3166 &reply_pages
, &reply_len
);
3167 if (ret
&& ret
!= -ETIMEDOUT
) {
3168 rbd_warn(rbd_dev
, "failed to request lock: %d", ret
);
3172 if (reply_len
> 0 && reply_len
<= PAGE_SIZE
) {
3173 void *p
= page_address(reply_pages
[0]);
3174 void *const end
= p
+ reply_len
;
3177 ceph_decode_32_safe(&p
, end
, n
, e_inval
); /* num_acks */
3182 ceph_decode_need(&p
, end
, 8 + 8, e_inval
);
3183 p
+= 8 + 8; /* skip gid and cookie */
3185 ceph_decode_32_safe(&p
, end
, len
, e_inval
);
3189 if (lock_owner_responded
) {
3191 "duplicate lock owners detected");
3196 lock_owner_responded
= true;
3197 ret
= ceph_start_decoding(&p
, end
, 1, "ResponseMessage",
3201 "failed to decode ResponseMessage: %d",
3206 ret
= ceph_decode_32(&p
);
3210 if (!lock_owner_responded
) {
3211 rbd_warn(rbd_dev
, "no lock owners detected");
3216 ceph_release_page_vector(reply_pages
, calc_pages_for(0, reply_len
));
3224 static void wake_requests(struct rbd_device
*rbd_dev
, bool wake_all
)
3226 dout("%s rbd_dev %p wake_all %d\n", __func__
, rbd_dev
, wake_all
);
3228 cancel_delayed_work(&rbd_dev
->lock_dwork
);
3230 wake_up_all(&rbd_dev
->lock_waitq
);
3232 wake_up(&rbd_dev
->lock_waitq
);
3235 static int get_lock_owner_info(struct rbd_device
*rbd_dev
,
3236 struct ceph_locker
**lockers
, u32
*num_lockers
)
3238 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
3243 dout("%s rbd_dev %p\n", __func__
, rbd_dev
);
3245 ret
= ceph_cls_lock_info(osdc
, &rbd_dev
->header_oid
,
3246 &rbd_dev
->header_oloc
, RBD_LOCK_NAME
,
3247 &lock_type
, &lock_tag
, lockers
, num_lockers
);
3251 if (*num_lockers
== 0) {
3252 dout("%s rbd_dev %p no lockers detected\n", __func__
, rbd_dev
);
3256 if (strcmp(lock_tag
, RBD_LOCK_TAG
)) {
3257 rbd_warn(rbd_dev
, "locked by external mechanism, tag %s",
3263 if (lock_type
== CEPH_CLS_LOCK_SHARED
) {
3264 rbd_warn(rbd_dev
, "shared lock type detected");
3269 if (strncmp((*lockers
)[0].id
.cookie
, RBD_LOCK_COOKIE_PREFIX
,
3270 strlen(RBD_LOCK_COOKIE_PREFIX
))) {
3271 rbd_warn(rbd_dev
, "locked by external mechanism, cookie %s",
3272 (*lockers
)[0].id
.cookie
);
3282 static int find_watcher(struct rbd_device
*rbd_dev
,
3283 const struct ceph_locker
*locker
)
3285 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
3286 struct ceph_watch_item
*watchers
;
3292 ret
= ceph_osdc_list_watchers(osdc
, &rbd_dev
->header_oid
,
3293 &rbd_dev
->header_oloc
, &watchers
,
3298 sscanf(locker
->id
.cookie
, RBD_LOCK_COOKIE_PREFIX
" %llu", &cookie
);
3299 for (i
= 0; i
< num_watchers
; i
++) {
3300 if (!memcmp(&watchers
[i
].addr
, &locker
->info
.addr
,
3301 sizeof(locker
->info
.addr
)) &&
3302 watchers
[i
].cookie
== cookie
) {
3303 struct rbd_client_id cid
= {
3304 .gid
= le64_to_cpu(watchers
[i
].name
.num
),
3308 dout("%s rbd_dev %p found cid %llu-%llu\n", __func__
,
3309 rbd_dev
, cid
.gid
, cid
.handle
);
3310 rbd_set_owner_cid(rbd_dev
, &cid
);
3316 dout("%s rbd_dev %p no watchers\n", __func__
, rbd_dev
);
3324 * lock_rwsem must be held for write
3326 static int rbd_try_lock(struct rbd_device
*rbd_dev
)
3328 struct ceph_client
*client
= rbd_dev
->rbd_client
->client
;
3329 struct ceph_locker
*lockers
;
3334 ret
= rbd_lock(rbd_dev
);
3338 /* determine if the current lock holder is still alive */
3339 ret
= get_lock_owner_info(rbd_dev
, &lockers
, &num_lockers
);
3343 if (num_lockers
== 0)
3346 ret
= find_watcher(rbd_dev
, lockers
);
3349 ret
= 0; /* have to request lock */
3353 rbd_warn(rbd_dev
, "%s%llu seems dead, breaking lock",
3354 ENTITY_NAME(lockers
[0].id
.name
));
3356 ret
= ceph_monc_blacklist_add(&client
->monc
,
3357 &lockers
[0].info
.addr
);
3359 rbd_warn(rbd_dev
, "blacklist of %s%llu failed: %d",
3360 ENTITY_NAME(lockers
[0].id
.name
), ret
);
3364 ret
= ceph_cls_break_lock(&client
->osdc
, &rbd_dev
->header_oid
,
3365 &rbd_dev
->header_oloc
, RBD_LOCK_NAME
,
3366 lockers
[0].id
.cookie
,
3367 &lockers
[0].id
.name
);
3368 if (ret
&& ret
!= -ENOENT
)
3372 ceph_free_lockers(lockers
, num_lockers
);
3376 ceph_free_lockers(lockers
, num_lockers
);
3381 * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED
3383 static enum rbd_lock_state
rbd_try_acquire_lock(struct rbd_device
*rbd_dev
,
3386 enum rbd_lock_state lock_state
;
3388 down_read(&rbd_dev
->lock_rwsem
);
3389 dout("%s rbd_dev %p read lock_state %d\n", __func__
, rbd_dev
,
3390 rbd_dev
->lock_state
);
3391 if (__rbd_is_lock_owner(rbd_dev
)) {
3392 lock_state
= rbd_dev
->lock_state
;
3393 up_read(&rbd_dev
->lock_rwsem
);
3397 up_read(&rbd_dev
->lock_rwsem
);
3398 down_write(&rbd_dev
->lock_rwsem
);
3399 dout("%s rbd_dev %p write lock_state %d\n", __func__
, rbd_dev
,
3400 rbd_dev
->lock_state
);
3401 if (!__rbd_is_lock_owner(rbd_dev
)) {
3402 *pret
= rbd_try_lock(rbd_dev
);
3404 rbd_warn(rbd_dev
, "failed to acquire lock: %d", *pret
);
3407 lock_state
= rbd_dev
->lock_state
;
3408 up_write(&rbd_dev
->lock_rwsem
);
3412 static void rbd_acquire_lock(struct work_struct
*work
)
3414 struct rbd_device
*rbd_dev
= container_of(to_delayed_work(work
),
3415 struct rbd_device
, lock_dwork
);
3416 enum rbd_lock_state lock_state
;
3419 dout("%s rbd_dev %p\n", __func__
, rbd_dev
);
3421 lock_state
= rbd_try_acquire_lock(rbd_dev
, &ret
);
3422 if (lock_state
!= RBD_LOCK_STATE_UNLOCKED
|| ret
== -EBLACKLISTED
) {
3423 if (lock_state
== RBD_LOCK_STATE_LOCKED
)
3424 wake_requests(rbd_dev
, true);
3425 dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__
,
3426 rbd_dev
, lock_state
, ret
);
3430 ret
= rbd_request_lock(rbd_dev
);
3431 if (ret
== -ETIMEDOUT
) {
3432 goto again
; /* treat this as a dead client */
3433 } else if (ret
== -EROFS
) {
3434 rbd_warn(rbd_dev
, "peer will not release lock");
3436 * If this is rbd_add_acquire_lock(), we want to fail
3437 * immediately -- reuse BLACKLISTED flag. Otherwise we
3440 if (!(rbd_dev
->disk
->flags
& GENHD_FL_UP
)) {
3441 set_bit(RBD_DEV_FLAG_BLACKLISTED
, &rbd_dev
->flags
);
3442 /* wake "rbd map --exclusive" process */
3443 wake_requests(rbd_dev
, false);
3445 } else if (ret
< 0) {
3446 rbd_warn(rbd_dev
, "error requesting lock: %d", ret
);
3447 mod_delayed_work(rbd_dev
->task_wq
, &rbd_dev
->lock_dwork
,
3451 * lock owner acked, but resend if we don't see them
3454 dout("%s rbd_dev %p requeueing lock_dwork\n", __func__
,
3456 mod_delayed_work(rbd_dev
->task_wq
, &rbd_dev
->lock_dwork
,
3457 msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT
* MSEC_PER_SEC
));
3462 * lock_rwsem must be held for write
3464 static bool rbd_release_lock(struct rbd_device
*rbd_dev
)
3466 dout("%s rbd_dev %p read lock_state %d\n", __func__
, rbd_dev
,
3467 rbd_dev
->lock_state
);
3468 if (rbd_dev
->lock_state
!= RBD_LOCK_STATE_LOCKED
)
3471 rbd_dev
->lock_state
= RBD_LOCK_STATE_RELEASING
;
3472 downgrade_write(&rbd_dev
->lock_rwsem
);
3474 * Ensure that all in-flight IO is flushed.
3476 * FIXME: ceph_osdc_sync() flushes the entire OSD client, which
3477 * may be shared with other devices.
3479 ceph_osdc_sync(&rbd_dev
->rbd_client
->client
->osdc
);
3480 up_read(&rbd_dev
->lock_rwsem
);
3482 down_write(&rbd_dev
->lock_rwsem
);
3483 dout("%s rbd_dev %p write lock_state %d\n", __func__
, rbd_dev
,
3484 rbd_dev
->lock_state
);
3485 if (rbd_dev
->lock_state
!= RBD_LOCK_STATE_RELEASING
)
3488 rbd_unlock(rbd_dev
);
3490 * Give others a chance to grab the lock - we would re-acquire
3491 * almost immediately if we got new IO during ceph_osdc_sync()
3492 * otherwise. We need to ack our own notifications, so this
3493 * lock_dwork will be requeued from rbd_wait_state_locked()
3494 * after wake_requests() in rbd_handle_released_lock().
3496 cancel_delayed_work(&rbd_dev
->lock_dwork
);
3500 static void rbd_release_lock_work(struct work_struct
*work
)
3502 struct rbd_device
*rbd_dev
= container_of(work
, struct rbd_device
,
3505 down_write(&rbd_dev
->lock_rwsem
);
3506 rbd_release_lock(rbd_dev
);
3507 up_write(&rbd_dev
->lock_rwsem
);
3510 static void rbd_handle_acquired_lock(struct rbd_device
*rbd_dev
, u8 struct_v
,
3513 struct rbd_client_id cid
= { 0 };
3515 if (struct_v
>= 2) {
3516 cid
.gid
= ceph_decode_64(p
);
3517 cid
.handle
= ceph_decode_64(p
);
3520 dout("%s rbd_dev %p cid %llu-%llu\n", __func__
, rbd_dev
, cid
.gid
,
3522 if (!rbd_cid_equal(&cid
, &rbd_empty_cid
)) {
3523 down_write(&rbd_dev
->lock_rwsem
);
3524 if (rbd_cid_equal(&cid
, &rbd_dev
->owner_cid
)) {
3526 * we already know that the remote client is
3529 up_write(&rbd_dev
->lock_rwsem
);
3533 rbd_set_owner_cid(rbd_dev
, &cid
);
3534 downgrade_write(&rbd_dev
->lock_rwsem
);
3536 down_read(&rbd_dev
->lock_rwsem
);
3539 if (!__rbd_is_lock_owner(rbd_dev
))
3540 wake_requests(rbd_dev
, false);
3541 up_read(&rbd_dev
->lock_rwsem
);
3544 static void rbd_handle_released_lock(struct rbd_device
*rbd_dev
, u8 struct_v
,
3547 struct rbd_client_id cid
= { 0 };
3549 if (struct_v
>= 2) {
3550 cid
.gid
= ceph_decode_64(p
);
3551 cid
.handle
= ceph_decode_64(p
);
3554 dout("%s rbd_dev %p cid %llu-%llu\n", __func__
, rbd_dev
, cid
.gid
,
3556 if (!rbd_cid_equal(&cid
, &rbd_empty_cid
)) {
3557 down_write(&rbd_dev
->lock_rwsem
);
3558 if (!rbd_cid_equal(&cid
, &rbd_dev
->owner_cid
)) {
3559 dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n",
3560 __func__
, rbd_dev
, cid
.gid
, cid
.handle
,
3561 rbd_dev
->owner_cid
.gid
, rbd_dev
->owner_cid
.handle
);
3562 up_write(&rbd_dev
->lock_rwsem
);
3566 rbd_set_owner_cid(rbd_dev
, &rbd_empty_cid
);
3567 downgrade_write(&rbd_dev
->lock_rwsem
);
3569 down_read(&rbd_dev
->lock_rwsem
);
3572 if (!__rbd_is_lock_owner(rbd_dev
))
3573 wake_requests(rbd_dev
, false);
3574 up_read(&rbd_dev
->lock_rwsem
);
3578 * Returns result for ResponseMessage to be encoded (<= 0), or 1 if no
3579 * ResponseMessage is needed.
3581 static int rbd_handle_request_lock(struct rbd_device
*rbd_dev
, u8 struct_v
,
3584 struct rbd_client_id my_cid
= rbd_get_cid(rbd_dev
);
3585 struct rbd_client_id cid
= { 0 };
3588 if (struct_v
>= 2) {
3589 cid
.gid
= ceph_decode_64(p
);
3590 cid
.handle
= ceph_decode_64(p
);
3593 dout("%s rbd_dev %p cid %llu-%llu\n", __func__
, rbd_dev
, cid
.gid
,
3595 if (rbd_cid_equal(&cid
, &my_cid
))
3598 down_read(&rbd_dev
->lock_rwsem
);
3599 if (__rbd_is_lock_owner(rbd_dev
)) {
3600 if (rbd_dev
->lock_state
== RBD_LOCK_STATE_LOCKED
&&
3601 rbd_cid_equal(&rbd_dev
->owner_cid
, &rbd_empty_cid
))
3605 * encode ResponseMessage(0) so the peer can detect
3610 if (rbd_dev
->lock_state
== RBD_LOCK_STATE_LOCKED
) {
3611 if (!rbd_dev
->opts
->exclusive
) {
3612 dout("%s rbd_dev %p queueing unlock_work\n",
3614 queue_work(rbd_dev
->task_wq
,
3615 &rbd_dev
->unlock_work
);
3617 /* refuse to release the lock */
3624 up_read(&rbd_dev
->lock_rwsem
);
3628 static void __rbd_acknowledge_notify(struct rbd_device
*rbd_dev
,
3629 u64 notify_id
, u64 cookie
, s32
*result
)
3631 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
3632 int buf_size
= 4 + CEPH_ENCODING_START_BLK_LEN
;
3639 /* encode ResponseMessage */
3640 ceph_start_encoding(&p
, 1, 1,
3641 buf_size
- CEPH_ENCODING_START_BLK_LEN
);
3642 ceph_encode_32(&p
, *result
);
3647 ret
= ceph_osdc_notify_ack(osdc
, &rbd_dev
->header_oid
,
3648 &rbd_dev
->header_oloc
, notify_id
, cookie
,
3651 rbd_warn(rbd_dev
, "acknowledge_notify failed: %d", ret
);
3654 static void rbd_acknowledge_notify(struct rbd_device
*rbd_dev
, u64 notify_id
,
3657 dout("%s rbd_dev %p\n", __func__
, rbd_dev
);
3658 __rbd_acknowledge_notify(rbd_dev
, notify_id
, cookie
, NULL
);
3661 static void rbd_acknowledge_notify_result(struct rbd_device
*rbd_dev
,
3662 u64 notify_id
, u64 cookie
, s32 result
)
3664 dout("%s rbd_dev %p result %d\n", __func__
, rbd_dev
, result
);
3665 __rbd_acknowledge_notify(rbd_dev
, notify_id
, cookie
, &result
);
3668 static void rbd_watch_cb(void *arg
, u64 notify_id
, u64 cookie
,
3669 u64 notifier_id
, void *data
, size_t data_len
)
3671 struct rbd_device
*rbd_dev
= arg
;
3673 void *const end
= p
+ data_len
;
3679 dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n",
3680 __func__
, rbd_dev
, cookie
, notify_id
, data_len
);
3682 ret
= ceph_start_decoding(&p
, end
, 1, "NotifyMessage",
3685 rbd_warn(rbd_dev
, "failed to decode NotifyMessage: %d",
3690 notify_op
= ceph_decode_32(&p
);
3692 /* legacy notification for header updates */
3693 notify_op
= RBD_NOTIFY_OP_HEADER_UPDATE
;
3697 dout("%s rbd_dev %p notify_op %u\n", __func__
, rbd_dev
, notify_op
);
3698 switch (notify_op
) {
3699 case RBD_NOTIFY_OP_ACQUIRED_LOCK
:
3700 rbd_handle_acquired_lock(rbd_dev
, struct_v
, &p
);
3701 rbd_acknowledge_notify(rbd_dev
, notify_id
, cookie
);
3703 case RBD_NOTIFY_OP_RELEASED_LOCK
:
3704 rbd_handle_released_lock(rbd_dev
, struct_v
, &p
);
3705 rbd_acknowledge_notify(rbd_dev
, notify_id
, cookie
);
3707 case RBD_NOTIFY_OP_REQUEST_LOCK
:
3708 ret
= rbd_handle_request_lock(rbd_dev
, struct_v
, &p
);
3710 rbd_acknowledge_notify_result(rbd_dev
, notify_id
,
3713 rbd_acknowledge_notify(rbd_dev
, notify_id
, cookie
);
3715 case RBD_NOTIFY_OP_HEADER_UPDATE
:
3716 ret
= rbd_dev_refresh(rbd_dev
);
3718 rbd_warn(rbd_dev
, "refresh failed: %d", ret
);
3720 rbd_acknowledge_notify(rbd_dev
, notify_id
, cookie
);
3723 if (rbd_is_lock_owner(rbd_dev
))
3724 rbd_acknowledge_notify_result(rbd_dev
, notify_id
,
3725 cookie
, -EOPNOTSUPP
);
3727 rbd_acknowledge_notify(rbd_dev
, notify_id
, cookie
);
3732 static void __rbd_unregister_watch(struct rbd_device
*rbd_dev
);
3734 static void rbd_watch_errcb(void *arg
, u64 cookie
, int err
)
3736 struct rbd_device
*rbd_dev
= arg
;
3738 rbd_warn(rbd_dev
, "encountered watch error: %d", err
);
3740 down_write(&rbd_dev
->lock_rwsem
);
3741 rbd_set_owner_cid(rbd_dev
, &rbd_empty_cid
);
3742 up_write(&rbd_dev
->lock_rwsem
);
3744 mutex_lock(&rbd_dev
->watch_mutex
);
3745 if (rbd_dev
->watch_state
== RBD_WATCH_STATE_REGISTERED
) {
3746 __rbd_unregister_watch(rbd_dev
);
3747 rbd_dev
->watch_state
= RBD_WATCH_STATE_ERROR
;
3749 queue_delayed_work(rbd_dev
->task_wq
, &rbd_dev
->watch_dwork
, 0);
3751 mutex_unlock(&rbd_dev
->watch_mutex
);
3755 * watch_mutex must be locked
3757 static int __rbd_register_watch(struct rbd_device
*rbd_dev
)
3759 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
3760 struct ceph_osd_linger_request
*handle
;
3762 rbd_assert(!rbd_dev
->watch_handle
);
3763 dout("%s rbd_dev %p\n", __func__
, rbd_dev
);
3765 handle
= ceph_osdc_watch(osdc
, &rbd_dev
->header_oid
,
3766 &rbd_dev
->header_oloc
, rbd_watch_cb
,
3767 rbd_watch_errcb
, rbd_dev
);
3769 return PTR_ERR(handle
);
3771 rbd_dev
->watch_handle
= handle
;
3776 * watch_mutex must be locked
3778 static void __rbd_unregister_watch(struct rbd_device
*rbd_dev
)
3780 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
3783 rbd_assert(rbd_dev
->watch_handle
);
3784 dout("%s rbd_dev %p\n", __func__
, rbd_dev
);
3786 ret
= ceph_osdc_unwatch(osdc
, rbd_dev
->watch_handle
);
3788 rbd_warn(rbd_dev
, "failed to unwatch: %d", ret
);
3790 rbd_dev
->watch_handle
= NULL
;
3793 static int rbd_register_watch(struct rbd_device
*rbd_dev
)
3797 mutex_lock(&rbd_dev
->watch_mutex
);
3798 rbd_assert(rbd_dev
->watch_state
== RBD_WATCH_STATE_UNREGISTERED
);
3799 ret
= __rbd_register_watch(rbd_dev
);
3803 rbd_dev
->watch_state
= RBD_WATCH_STATE_REGISTERED
;
3804 rbd_dev
->watch_cookie
= rbd_dev
->watch_handle
->linger_id
;
3807 mutex_unlock(&rbd_dev
->watch_mutex
);
3811 static void cancel_tasks_sync(struct rbd_device
*rbd_dev
)
3813 dout("%s rbd_dev %p\n", __func__
, rbd_dev
);
3815 cancel_delayed_work_sync(&rbd_dev
->watch_dwork
);
3816 cancel_work_sync(&rbd_dev
->acquired_lock_work
);
3817 cancel_work_sync(&rbd_dev
->released_lock_work
);
3818 cancel_delayed_work_sync(&rbd_dev
->lock_dwork
);
3819 cancel_work_sync(&rbd_dev
->unlock_work
);
3822 static void rbd_unregister_watch(struct rbd_device
*rbd_dev
)
3824 WARN_ON(waitqueue_active(&rbd_dev
->lock_waitq
));
3825 cancel_tasks_sync(rbd_dev
);
3827 mutex_lock(&rbd_dev
->watch_mutex
);
3828 if (rbd_dev
->watch_state
== RBD_WATCH_STATE_REGISTERED
)
3829 __rbd_unregister_watch(rbd_dev
);
3830 rbd_dev
->watch_state
= RBD_WATCH_STATE_UNREGISTERED
;
3831 mutex_unlock(&rbd_dev
->watch_mutex
);
3833 ceph_osdc_flush_notifies(&rbd_dev
->rbd_client
->client
->osdc
);
3837 * lock_rwsem must be held for write
3839 static void rbd_reacquire_lock(struct rbd_device
*rbd_dev
)
3841 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
3845 WARN_ON(rbd_dev
->lock_state
!= RBD_LOCK_STATE_LOCKED
);
3847 format_lock_cookie(rbd_dev
, cookie
);
3848 ret
= ceph_cls_set_cookie(osdc
, &rbd_dev
->header_oid
,
3849 &rbd_dev
->header_oloc
, RBD_LOCK_NAME
,
3850 CEPH_CLS_LOCK_EXCLUSIVE
, rbd_dev
->lock_cookie
,
3851 RBD_LOCK_TAG
, cookie
);
3853 if (ret
!= -EOPNOTSUPP
)
3854 rbd_warn(rbd_dev
, "failed to update lock cookie: %d",
3858 * Lock cookie cannot be updated on older OSDs, so do
3859 * a manual release and queue an acquire.
3861 if (rbd_release_lock(rbd_dev
))
3862 queue_delayed_work(rbd_dev
->task_wq
,
3863 &rbd_dev
->lock_dwork
, 0);
3865 __rbd_lock(rbd_dev
, cookie
);
3869 static void rbd_reregister_watch(struct work_struct
*work
)
3871 struct rbd_device
*rbd_dev
= container_of(to_delayed_work(work
),
3872 struct rbd_device
, watch_dwork
);
3875 dout("%s rbd_dev %p\n", __func__
, rbd_dev
);
3877 mutex_lock(&rbd_dev
->watch_mutex
);
3878 if (rbd_dev
->watch_state
!= RBD_WATCH_STATE_ERROR
) {
3879 mutex_unlock(&rbd_dev
->watch_mutex
);
3883 ret
= __rbd_register_watch(rbd_dev
);
3885 rbd_warn(rbd_dev
, "failed to reregister watch: %d", ret
);
3886 if (ret
== -EBLACKLISTED
|| ret
== -ENOENT
) {
3887 set_bit(RBD_DEV_FLAG_BLACKLISTED
, &rbd_dev
->flags
);
3888 wake_requests(rbd_dev
, true);
3890 queue_delayed_work(rbd_dev
->task_wq
,
3891 &rbd_dev
->watch_dwork
,
3894 mutex_unlock(&rbd_dev
->watch_mutex
);
3898 rbd_dev
->watch_state
= RBD_WATCH_STATE_REGISTERED
;
3899 rbd_dev
->watch_cookie
= rbd_dev
->watch_handle
->linger_id
;
3900 mutex_unlock(&rbd_dev
->watch_mutex
);
3902 down_write(&rbd_dev
->lock_rwsem
);
3903 if (rbd_dev
->lock_state
== RBD_LOCK_STATE_LOCKED
)
3904 rbd_reacquire_lock(rbd_dev
);
3905 up_write(&rbd_dev
->lock_rwsem
);
3907 ret
= rbd_dev_refresh(rbd_dev
);
3909 rbd_warn(rbd_dev
, "reregisteration refresh failed: %d", ret
);
3913 * Synchronous osd object method call. Returns the number of bytes
3914 * returned in the outbound buffer, or a negative error code.
3916 static int rbd_obj_method_sync(struct rbd_device
*rbd_dev
,
3917 struct ceph_object_id
*oid
,
3918 struct ceph_object_locator
*oloc
,
3919 const char *method_name
,
3920 const void *outbound
,
3921 size_t outbound_size
,
3923 size_t inbound_size
)
3925 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
3926 struct page
*req_page
= NULL
;
3927 struct page
*reply_page
;
3931 * Method calls are ultimately read operations. The result
3932 * should placed into the inbound buffer provided. They
3933 * also supply outbound data--parameters for the object
3934 * method. Currently if this is present it will be a
3938 if (outbound_size
> PAGE_SIZE
)
3941 req_page
= alloc_page(GFP_KERNEL
);
3945 memcpy(page_address(req_page
), outbound
, outbound_size
);
3948 reply_page
= alloc_page(GFP_KERNEL
);
3951 __free_page(req_page
);
3955 ret
= ceph_osdc_call(osdc
, oid
, oloc
, RBD_DRV_NAME
, method_name
,
3956 CEPH_OSD_FLAG_READ
, req_page
, outbound_size
,
3957 reply_page
, &inbound_size
);
3959 memcpy(inbound
, page_address(reply_page
), inbound_size
);
3964 __free_page(req_page
);
3965 __free_page(reply_page
);
3970 * lock_rwsem must be held for read
3972 static void rbd_wait_state_locked(struct rbd_device
*rbd_dev
)
3978 * Note the use of mod_delayed_work() in rbd_acquire_lock()
3979 * and cancel_delayed_work() in wake_requests().
3981 dout("%s rbd_dev %p queueing lock_dwork\n", __func__
, rbd_dev
);
3982 queue_delayed_work(rbd_dev
->task_wq
, &rbd_dev
->lock_dwork
, 0);
3983 prepare_to_wait_exclusive(&rbd_dev
->lock_waitq
, &wait
,
3984 TASK_UNINTERRUPTIBLE
);
3985 up_read(&rbd_dev
->lock_rwsem
);
3987 down_read(&rbd_dev
->lock_rwsem
);
3988 } while (rbd_dev
->lock_state
!= RBD_LOCK_STATE_LOCKED
&&
3989 !test_bit(RBD_DEV_FLAG_BLACKLISTED
, &rbd_dev
->flags
));
3991 finish_wait(&rbd_dev
->lock_waitq
, &wait
);
3994 static void rbd_queue_workfn(struct work_struct
*work
)
3996 struct request
*rq
= blk_mq_rq_from_pdu(work
);
3997 struct rbd_device
*rbd_dev
= rq
->q
->queuedata
;
3998 struct rbd_img_request
*img_request
;
3999 struct ceph_snap_context
*snapc
= NULL
;
4000 u64 offset
= (u64
)blk_rq_pos(rq
) << SECTOR_SHIFT
;
4001 u64 length
= blk_rq_bytes(rq
);
4002 enum obj_operation_type op_type
;
4004 bool must_be_locked
;
4007 switch (req_op(rq
)) {
4008 case REQ_OP_DISCARD
:
4009 case REQ_OP_WRITE_ZEROES
:
4010 op_type
= OBJ_OP_DISCARD
;
4013 op_type
= OBJ_OP_WRITE
;
4016 op_type
= OBJ_OP_READ
;
4019 dout("%s: non-fs request type %d\n", __func__
, req_op(rq
));
4024 /* Ignore/skip any zero-length requests */
4027 dout("%s: zero-length request\n", __func__
);
4032 rbd_assert(op_type
== OBJ_OP_READ
||
4033 rbd_dev
->spec
->snap_id
== CEPH_NOSNAP
);
4036 * Quit early if the mapped snapshot no longer exists. It's
4037 * still possible the snapshot will have disappeared by the
4038 * time our request arrives at the osd, but there's no sense in
4039 * sending it if we already know.
4041 if (!test_bit(RBD_DEV_FLAG_EXISTS
, &rbd_dev
->flags
)) {
4042 dout("request for non-existent snapshot");
4043 rbd_assert(rbd_dev
->spec
->snap_id
!= CEPH_NOSNAP
);
4048 if (offset
&& length
> U64_MAX
- offset
+ 1) {
4049 rbd_warn(rbd_dev
, "bad request range (%llu~%llu)", offset
,
4052 goto err_rq
; /* Shouldn't happen */
4055 blk_mq_start_request(rq
);
4057 down_read(&rbd_dev
->header_rwsem
);
4058 mapping_size
= rbd_dev
->mapping
.size
;
4059 if (op_type
!= OBJ_OP_READ
) {
4060 snapc
= rbd_dev
->header
.snapc
;
4061 ceph_get_snap_context(snapc
);
4063 up_read(&rbd_dev
->header_rwsem
);
4065 if (offset
+ length
> mapping_size
) {
4066 rbd_warn(rbd_dev
, "beyond EOD (%llu~%llu > %llu)", offset
,
4067 length
, mapping_size
);
4073 (rbd_dev
->header
.features
& RBD_FEATURE_EXCLUSIVE_LOCK
) &&
4074 (op_type
!= OBJ_OP_READ
|| rbd_dev
->opts
->lock_on_read
);
4075 if (must_be_locked
) {
4076 down_read(&rbd_dev
->lock_rwsem
);
4077 if (rbd_dev
->lock_state
!= RBD_LOCK_STATE_LOCKED
&&
4078 !test_bit(RBD_DEV_FLAG_BLACKLISTED
, &rbd_dev
->flags
)) {
4079 if (rbd_dev
->opts
->exclusive
) {
4080 rbd_warn(rbd_dev
, "exclusive lock required");
4084 rbd_wait_state_locked(rbd_dev
);
4086 if (test_bit(RBD_DEV_FLAG_BLACKLISTED
, &rbd_dev
->flags
)) {
4087 result
= -EBLACKLISTED
;
4092 img_request
= rbd_img_request_create(rbd_dev
, offset
, length
, op_type
,
4098 img_request
->rq
= rq
;
4099 snapc
= NULL
; /* img_request consumes a ref */
4101 if (op_type
== OBJ_OP_DISCARD
)
4102 result
= rbd_img_request_fill(img_request
, OBJ_REQUEST_NODATA
,
4105 result
= rbd_img_request_fill(img_request
, OBJ_REQUEST_BIO
,
4108 goto err_img_request
;
4110 result
= rbd_img_request_submit(img_request
);
4112 goto err_img_request
;
4115 up_read(&rbd_dev
->lock_rwsem
);
4119 rbd_img_request_put(img_request
);
4122 up_read(&rbd_dev
->lock_rwsem
);
4125 rbd_warn(rbd_dev
, "%s %llx at %llx result %d",
4126 obj_op_name(op_type
), length
, offset
, result
);
4127 ceph_put_snap_context(snapc
);
4129 blk_mq_end_request(rq
, errno_to_blk_status(result
));
4132 static blk_status_t
rbd_queue_rq(struct blk_mq_hw_ctx
*hctx
,
4133 const struct blk_mq_queue_data
*bd
)
4135 struct request
*rq
= bd
->rq
;
4136 struct work_struct
*work
= blk_mq_rq_to_pdu(rq
);
4138 queue_work(rbd_wq
, work
);
4142 static void rbd_free_disk(struct rbd_device
*rbd_dev
)
4144 blk_cleanup_queue(rbd_dev
->disk
->queue
);
4145 blk_mq_free_tag_set(&rbd_dev
->tag_set
);
4146 put_disk(rbd_dev
->disk
);
4147 rbd_dev
->disk
= NULL
;
4150 static int rbd_obj_read_sync(struct rbd_device
*rbd_dev
,
4151 struct ceph_object_id
*oid
,
4152 struct ceph_object_locator
*oloc
,
4153 void *buf
, int buf_len
)
4156 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
4157 struct ceph_osd_request
*req
;
4158 struct page
**pages
;
4159 int num_pages
= calc_pages_for(0, buf_len
);
4162 req
= ceph_osdc_alloc_request(osdc
, NULL
, 1, false, GFP_KERNEL
);
4166 ceph_oid_copy(&req
->r_base_oid
, oid
);
4167 ceph_oloc_copy(&req
->r_base_oloc
, oloc
);
4168 req
->r_flags
= CEPH_OSD_FLAG_READ
;
4170 ret
= ceph_osdc_alloc_messages(req
, GFP_KERNEL
);
4174 pages
= ceph_alloc_page_vector(num_pages
, GFP_KERNEL
);
4175 if (IS_ERR(pages
)) {
4176 ret
= PTR_ERR(pages
);
4180 osd_req_op_extent_init(req
, 0, CEPH_OSD_OP_READ
, 0, buf_len
, 0, 0);
4181 osd_req_op_extent_osd_data_pages(req
, 0, pages
, buf_len
, 0, false,
4184 ceph_osdc_start_request(osdc
, req
, false);
4185 ret
= ceph_osdc_wait_request(osdc
, req
);
4187 ceph_copy_from_page_vector(pages
, buf
, 0, ret
);
4190 ceph_osdc_put_request(req
);
4195 * Read the complete header for the given rbd device. On successful
4196 * return, the rbd_dev->header field will contain up-to-date
4197 * information about the image.
4199 static int rbd_dev_v1_header_info(struct rbd_device
*rbd_dev
)
4201 struct rbd_image_header_ondisk
*ondisk
= NULL
;
4208 * The complete header will include an array of its 64-bit
4209 * snapshot ids, followed by the names of those snapshots as
4210 * a contiguous block of NUL-terminated strings. Note that
4211 * the number of snapshots could change by the time we read
4212 * it in, in which case we re-read it.
4219 size
= sizeof (*ondisk
);
4220 size
+= snap_count
* sizeof (struct rbd_image_snap_ondisk
);
4222 ondisk
= kmalloc(size
, GFP_KERNEL
);
4226 ret
= rbd_obj_read_sync(rbd_dev
, &rbd_dev
->header_oid
,
4227 &rbd_dev
->header_oloc
, ondisk
, size
);
4230 if ((size_t)ret
< size
) {
4232 rbd_warn(rbd_dev
, "short header read (want %zd got %d)",
4236 if (!rbd_dev_ondisk_valid(ondisk
)) {
4238 rbd_warn(rbd_dev
, "invalid header");
4242 names_size
= le64_to_cpu(ondisk
->snap_names_len
);
4243 want_count
= snap_count
;
4244 snap_count
= le32_to_cpu(ondisk
->snap_count
);
4245 } while (snap_count
!= want_count
);
4247 ret
= rbd_header_from_disk(rbd_dev
, ondisk
);
4255 * Clear the rbd device's EXISTS flag if the snapshot it's mapped to
4256 * has disappeared from the (just updated) snapshot context.
4258 static void rbd_exists_validate(struct rbd_device
*rbd_dev
)
4262 if (!test_bit(RBD_DEV_FLAG_EXISTS
, &rbd_dev
->flags
))
4265 snap_id
= rbd_dev
->spec
->snap_id
;
4266 if (snap_id
== CEPH_NOSNAP
)
4269 if (rbd_dev_snap_index(rbd_dev
, snap_id
) == BAD_SNAP_INDEX
)
4270 clear_bit(RBD_DEV_FLAG_EXISTS
, &rbd_dev
->flags
);
4273 static void rbd_dev_update_size(struct rbd_device
*rbd_dev
)
4278 * If EXISTS is not set, rbd_dev->disk may be NULL, so don't
4279 * try to update its size. If REMOVING is set, updating size
4280 * is just useless work since the device can't be opened.
4282 if (test_bit(RBD_DEV_FLAG_EXISTS
, &rbd_dev
->flags
) &&
4283 !test_bit(RBD_DEV_FLAG_REMOVING
, &rbd_dev
->flags
)) {
4284 size
= (sector_t
)rbd_dev
->mapping
.size
/ SECTOR_SIZE
;
4285 dout("setting size to %llu sectors", (unsigned long long)size
);
4286 set_capacity(rbd_dev
->disk
, size
);
4287 revalidate_disk(rbd_dev
->disk
);
4291 static int rbd_dev_refresh(struct rbd_device
*rbd_dev
)
4296 down_write(&rbd_dev
->header_rwsem
);
4297 mapping_size
= rbd_dev
->mapping
.size
;
4299 ret
= rbd_dev_header_info(rbd_dev
);
4304 * If there is a parent, see if it has disappeared due to the
4305 * mapped image getting flattened.
4307 if (rbd_dev
->parent
) {
4308 ret
= rbd_dev_v2_parent_info(rbd_dev
);
4313 if (rbd_dev
->spec
->snap_id
== CEPH_NOSNAP
) {
4314 rbd_dev
->mapping
.size
= rbd_dev
->header
.image_size
;
4316 /* validate mapped snapshot's EXISTS flag */
4317 rbd_exists_validate(rbd_dev
);
4321 up_write(&rbd_dev
->header_rwsem
);
4322 if (!ret
&& mapping_size
!= rbd_dev
->mapping
.size
)
4323 rbd_dev_update_size(rbd_dev
);
4328 static int rbd_init_request(struct blk_mq_tag_set
*set
, struct request
*rq
,
4329 unsigned int hctx_idx
, unsigned int numa_node
)
4331 struct work_struct
*work
= blk_mq_rq_to_pdu(rq
);
4333 INIT_WORK(work
, rbd_queue_workfn
);
4337 static const struct blk_mq_ops rbd_mq_ops
= {
4338 .queue_rq
= rbd_queue_rq
,
4339 .init_request
= rbd_init_request
,
4342 static int rbd_init_disk(struct rbd_device
*rbd_dev
)
4344 struct gendisk
*disk
;
4345 struct request_queue
*q
;
4349 /* create gendisk info */
4350 disk
= alloc_disk(single_major
?
4351 (1 << RBD_SINGLE_MAJOR_PART_SHIFT
) :
4352 RBD_MINORS_PER_MAJOR
);
4356 snprintf(disk
->disk_name
, sizeof(disk
->disk_name
), RBD_DRV_NAME
"%d",
4358 disk
->major
= rbd_dev
->major
;
4359 disk
->first_minor
= rbd_dev
->minor
;
4361 disk
->flags
|= GENHD_FL_EXT_DEVT
;
4362 disk
->fops
= &rbd_bd_ops
;
4363 disk
->private_data
= rbd_dev
;
4365 memset(&rbd_dev
->tag_set
, 0, sizeof(rbd_dev
->tag_set
));
4366 rbd_dev
->tag_set
.ops
= &rbd_mq_ops
;
4367 rbd_dev
->tag_set
.queue_depth
= rbd_dev
->opts
->queue_depth
;
4368 rbd_dev
->tag_set
.numa_node
= NUMA_NO_NODE
;
4369 rbd_dev
->tag_set
.flags
= BLK_MQ_F_SHOULD_MERGE
| BLK_MQ_F_SG_MERGE
;
4370 rbd_dev
->tag_set
.nr_hw_queues
= 1;
4371 rbd_dev
->tag_set
.cmd_size
= sizeof(struct work_struct
);
4373 err
= blk_mq_alloc_tag_set(&rbd_dev
->tag_set
);
4377 q
= blk_mq_init_queue(&rbd_dev
->tag_set
);
4383 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, q
);
4384 /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */
4386 /* set io sizes to object size */
4387 segment_size
= rbd_obj_bytes(&rbd_dev
->header
);
4388 blk_queue_max_hw_sectors(q
, segment_size
/ SECTOR_SIZE
);
4389 q
->limits
.max_sectors
= queue_max_hw_sectors(q
);
4390 blk_queue_max_segments(q
, USHRT_MAX
);
4391 blk_queue_max_segment_size(q
, segment_size
);
4392 blk_queue_io_min(q
, segment_size
);
4393 blk_queue_io_opt(q
, segment_size
);
4395 /* enable the discard support */
4396 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, q
);
4397 q
->limits
.discard_granularity
= segment_size
;
4398 blk_queue_max_discard_sectors(q
, segment_size
/ SECTOR_SIZE
);
4399 blk_queue_max_write_zeroes_sectors(q
, segment_size
/ SECTOR_SIZE
);
4401 if (!ceph_test_opt(rbd_dev
->rbd_client
->client
, NOCRC
))
4402 q
->backing_dev_info
->capabilities
|= BDI_CAP_STABLE_WRITES
;
4405 * disk_release() expects a queue ref from add_disk() and will
4406 * put it. Hold an extra ref until add_disk() is called.
4408 WARN_ON(!blk_get_queue(q
));
4410 q
->queuedata
= rbd_dev
;
4412 rbd_dev
->disk
= disk
;
4416 blk_mq_free_tag_set(&rbd_dev
->tag_set
);
4426 static struct rbd_device
*dev_to_rbd_dev(struct device
*dev
)
4428 return container_of(dev
, struct rbd_device
, dev
);
4431 static ssize_t
rbd_size_show(struct device
*dev
,
4432 struct device_attribute
*attr
, char *buf
)
4434 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4436 return sprintf(buf
, "%llu\n",
4437 (unsigned long long)rbd_dev
->mapping
.size
);
4441 * Note this shows the features for whatever's mapped, which is not
4442 * necessarily the base image.
4444 static ssize_t
rbd_features_show(struct device
*dev
,
4445 struct device_attribute
*attr
, char *buf
)
4447 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4449 return sprintf(buf
, "0x%016llx\n",
4450 (unsigned long long)rbd_dev
->mapping
.features
);
4453 static ssize_t
rbd_major_show(struct device
*dev
,
4454 struct device_attribute
*attr
, char *buf
)
4456 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4459 return sprintf(buf
, "%d\n", rbd_dev
->major
);
4461 return sprintf(buf
, "(none)\n");
4464 static ssize_t
rbd_minor_show(struct device
*dev
,
4465 struct device_attribute
*attr
, char *buf
)
4467 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4469 return sprintf(buf
, "%d\n", rbd_dev
->minor
);
4472 static ssize_t
rbd_client_addr_show(struct device
*dev
,
4473 struct device_attribute
*attr
, char *buf
)
4475 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4476 struct ceph_entity_addr
*client_addr
=
4477 ceph_client_addr(rbd_dev
->rbd_client
->client
);
4479 return sprintf(buf
, "%pISpc/%u\n", &client_addr
->in_addr
,
4480 le32_to_cpu(client_addr
->nonce
));
4483 static ssize_t
rbd_client_id_show(struct device
*dev
,
4484 struct device_attribute
*attr
, char *buf
)
4486 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4488 return sprintf(buf
, "client%lld\n",
4489 ceph_client_gid(rbd_dev
->rbd_client
->client
));
4492 static ssize_t
rbd_cluster_fsid_show(struct device
*dev
,
4493 struct device_attribute
*attr
, char *buf
)
4495 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4497 return sprintf(buf
, "%pU\n", &rbd_dev
->rbd_client
->client
->fsid
);
4500 static ssize_t
rbd_config_info_show(struct device
*dev
,
4501 struct device_attribute
*attr
, char *buf
)
4503 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4505 return sprintf(buf
, "%s\n", rbd_dev
->config_info
);
4508 static ssize_t
rbd_pool_show(struct device
*dev
,
4509 struct device_attribute
*attr
, char *buf
)
4511 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4513 return sprintf(buf
, "%s\n", rbd_dev
->spec
->pool_name
);
4516 static ssize_t
rbd_pool_id_show(struct device
*dev
,
4517 struct device_attribute
*attr
, char *buf
)
4519 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4521 return sprintf(buf
, "%llu\n",
4522 (unsigned long long) rbd_dev
->spec
->pool_id
);
4525 static ssize_t
rbd_name_show(struct device
*dev
,
4526 struct device_attribute
*attr
, char *buf
)
4528 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4530 if (rbd_dev
->spec
->image_name
)
4531 return sprintf(buf
, "%s\n", rbd_dev
->spec
->image_name
);
4533 return sprintf(buf
, "(unknown)\n");
4536 static ssize_t
rbd_image_id_show(struct device
*dev
,
4537 struct device_attribute
*attr
, char *buf
)
4539 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4541 return sprintf(buf
, "%s\n", rbd_dev
->spec
->image_id
);
4545 * Shows the name of the currently-mapped snapshot (or
4546 * RBD_SNAP_HEAD_NAME for the base image).
4548 static ssize_t
rbd_snap_show(struct device
*dev
,
4549 struct device_attribute
*attr
,
4552 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4554 return sprintf(buf
, "%s\n", rbd_dev
->spec
->snap_name
);
4557 static ssize_t
rbd_snap_id_show(struct device
*dev
,
4558 struct device_attribute
*attr
, char *buf
)
4560 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4562 return sprintf(buf
, "%llu\n", rbd_dev
->spec
->snap_id
);
4566 * For a v2 image, shows the chain of parent images, separated by empty
4567 * lines. For v1 images or if there is no parent, shows "(no parent
4570 static ssize_t
rbd_parent_show(struct device
*dev
,
4571 struct device_attribute
*attr
,
4574 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4577 if (!rbd_dev
->parent
)
4578 return sprintf(buf
, "(no parent image)\n");
4580 for ( ; rbd_dev
->parent
; rbd_dev
= rbd_dev
->parent
) {
4581 struct rbd_spec
*spec
= rbd_dev
->parent_spec
;
4583 count
+= sprintf(&buf
[count
], "%s"
4584 "pool_id %llu\npool_name %s\n"
4585 "image_id %s\nimage_name %s\n"
4586 "snap_id %llu\nsnap_name %s\n"
4588 !count
? "" : "\n", /* first? */
4589 spec
->pool_id
, spec
->pool_name
,
4590 spec
->image_id
, spec
->image_name
?: "(unknown)",
4591 spec
->snap_id
, spec
->snap_name
,
4592 rbd_dev
->parent_overlap
);
4598 static ssize_t
rbd_image_refresh(struct device
*dev
,
4599 struct device_attribute
*attr
,
4603 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4606 ret
= rbd_dev_refresh(rbd_dev
);
4613 static DEVICE_ATTR(size
, S_IRUGO
, rbd_size_show
, NULL
);
4614 static DEVICE_ATTR(features
, S_IRUGO
, rbd_features_show
, NULL
);
4615 static DEVICE_ATTR(major
, S_IRUGO
, rbd_major_show
, NULL
);
4616 static DEVICE_ATTR(minor
, S_IRUGO
, rbd_minor_show
, NULL
);
4617 static DEVICE_ATTR(client_addr
, S_IRUGO
, rbd_client_addr_show
, NULL
);
4618 static DEVICE_ATTR(client_id
, S_IRUGO
, rbd_client_id_show
, NULL
);
4619 static DEVICE_ATTR(cluster_fsid
, S_IRUGO
, rbd_cluster_fsid_show
, NULL
);
4620 static DEVICE_ATTR(config_info
, S_IRUSR
, rbd_config_info_show
, NULL
);
4621 static DEVICE_ATTR(pool
, S_IRUGO
, rbd_pool_show
, NULL
);
4622 static DEVICE_ATTR(pool_id
, S_IRUGO
, rbd_pool_id_show
, NULL
);
4623 static DEVICE_ATTR(name
, S_IRUGO
, rbd_name_show
, NULL
);
4624 static DEVICE_ATTR(image_id
, S_IRUGO
, rbd_image_id_show
, NULL
);
4625 static DEVICE_ATTR(refresh
, S_IWUSR
, NULL
, rbd_image_refresh
);
4626 static DEVICE_ATTR(current_snap
, S_IRUGO
, rbd_snap_show
, NULL
);
4627 static DEVICE_ATTR(snap_id
, S_IRUGO
, rbd_snap_id_show
, NULL
);
4628 static DEVICE_ATTR(parent
, S_IRUGO
, rbd_parent_show
, NULL
);
4630 static struct attribute
*rbd_attrs
[] = {
4631 &dev_attr_size
.attr
,
4632 &dev_attr_features
.attr
,
4633 &dev_attr_major
.attr
,
4634 &dev_attr_minor
.attr
,
4635 &dev_attr_client_addr
.attr
,
4636 &dev_attr_client_id
.attr
,
4637 &dev_attr_cluster_fsid
.attr
,
4638 &dev_attr_config_info
.attr
,
4639 &dev_attr_pool
.attr
,
4640 &dev_attr_pool_id
.attr
,
4641 &dev_attr_name
.attr
,
4642 &dev_attr_image_id
.attr
,
4643 &dev_attr_current_snap
.attr
,
4644 &dev_attr_snap_id
.attr
,
4645 &dev_attr_parent
.attr
,
4646 &dev_attr_refresh
.attr
,
4650 static struct attribute_group rbd_attr_group
= {
4654 static const struct attribute_group
*rbd_attr_groups
[] = {
4659 static void rbd_dev_release(struct device
*dev
);
4661 static const struct device_type rbd_device_type
= {
4663 .groups
= rbd_attr_groups
,
4664 .release
= rbd_dev_release
,
4667 static struct rbd_spec
*rbd_spec_get(struct rbd_spec
*spec
)
4669 kref_get(&spec
->kref
);
4674 static void rbd_spec_free(struct kref
*kref
);
4675 static void rbd_spec_put(struct rbd_spec
*spec
)
4678 kref_put(&spec
->kref
, rbd_spec_free
);
4681 static struct rbd_spec
*rbd_spec_alloc(void)
4683 struct rbd_spec
*spec
;
4685 spec
= kzalloc(sizeof (*spec
), GFP_KERNEL
);
4689 spec
->pool_id
= CEPH_NOPOOL
;
4690 spec
->snap_id
= CEPH_NOSNAP
;
4691 kref_init(&spec
->kref
);
4696 static void rbd_spec_free(struct kref
*kref
)
4698 struct rbd_spec
*spec
= container_of(kref
, struct rbd_spec
, kref
);
4700 kfree(spec
->pool_name
);
4701 kfree(spec
->image_id
);
4702 kfree(spec
->image_name
);
4703 kfree(spec
->snap_name
);
4707 static void rbd_dev_free(struct rbd_device
*rbd_dev
)
4709 WARN_ON(rbd_dev
->watch_state
!= RBD_WATCH_STATE_UNREGISTERED
);
4710 WARN_ON(rbd_dev
->lock_state
!= RBD_LOCK_STATE_UNLOCKED
);
4712 ceph_oid_destroy(&rbd_dev
->header_oid
);
4713 ceph_oloc_destroy(&rbd_dev
->header_oloc
);
4714 kfree(rbd_dev
->config_info
);
4716 rbd_put_client(rbd_dev
->rbd_client
);
4717 rbd_spec_put(rbd_dev
->spec
);
4718 kfree(rbd_dev
->opts
);
4722 static void rbd_dev_release(struct device
*dev
)
4724 struct rbd_device
*rbd_dev
= dev_to_rbd_dev(dev
);
4725 bool need_put
= !!rbd_dev
->opts
;
4728 destroy_workqueue(rbd_dev
->task_wq
);
4729 ida_simple_remove(&rbd_dev_id_ida
, rbd_dev
->dev_id
);
4732 rbd_dev_free(rbd_dev
);
4735 * This is racy, but way better than putting module outside of
4736 * the release callback. The race window is pretty small, so
4737 * doing something similar to dm (dm-builtin.c) is overkill.
4740 module_put(THIS_MODULE
);
4743 static struct rbd_device
*__rbd_dev_create(struct rbd_client
*rbdc
,
4744 struct rbd_spec
*spec
)
4746 struct rbd_device
*rbd_dev
;
4748 rbd_dev
= kzalloc(sizeof(*rbd_dev
), GFP_KERNEL
);
4752 spin_lock_init(&rbd_dev
->lock
);
4753 INIT_LIST_HEAD(&rbd_dev
->node
);
4754 init_rwsem(&rbd_dev
->header_rwsem
);
4756 rbd_dev
->header
.data_pool_id
= CEPH_NOPOOL
;
4757 ceph_oid_init(&rbd_dev
->header_oid
);
4758 rbd_dev
->header_oloc
.pool
= spec
->pool_id
;
4760 mutex_init(&rbd_dev
->watch_mutex
);
4761 rbd_dev
->watch_state
= RBD_WATCH_STATE_UNREGISTERED
;
4762 INIT_DELAYED_WORK(&rbd_dev
->watch_dwork
, rbd_reregister_watch
);
4764 init_rwsem(&rbd_dev
->lock_rwsem
);
4765 rbd_dev
->lock_state
= RBD_LOCK_STATE_UNLOCKED
;
4766 INIT_WORK(&rbd_dev
->acquired_lock_work
, rbd_notify_acquired_lock
);
4767 INIT_WORK(&rbd_dev
->released_lock_work
, rbd_notify_released_lock
);
4768 INIT_DELAYED_WORK(&rbd_dev
->lock_dwork
, rbd_acquire_lock
);
4769 INIT_WORK(&rbd_dev
->unlock_work
, rbd_release_lock_work
);
4770 init_waitqueue_head(&rbd_dev
->lock_waitq
);
4772 rbd_dev
->dev
.bus
= &rbd_bus_type
;
4773 rbd_dev
->dev
.type
= &rbd_device_type
;
4774 rbd_dev
->dev
.parent
= &rbd_root_dev
;
4775 device_initialize(&rbd_dev
->dev
);
4777 rbd_dev
->rbd_client
= rbdc
;
4778 rbd_dev
->spec
= spec
;
4784 * Create a mapping rbd_dev.
4786 static struct rbd_device
*rbd_dev_create(struct rbd_client
*rbdc
,
4787 struct rbd_spec
*spec
,
4788 struct rbd_options
*opts
)
4790 struct rbd_device
*rbd_dev
;
4792 rbd_dev
= __rbd_dev_create(rbdc
, spec
);
4796 rbd_dev
->opts
= opts
;
4798 /* get an id and fill in device name */
4799 rbd_dev
->dev_id
= ida_simple_get(&rbd_dev_id_ida
, 0,
4800 minor_to_rbd_dev_id(1 << MINORBITS
),
4802 if (rbd_dev
->dev_id
< 0)
4805 sprintf(rbd_dev
->name
, RBD_DRV_NAME
"%d", rbd_dev
->dev_id
);
4806 rbd_dev
->task_wq
= alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM
,
4808 if (!rbd_dev
->task_wq
)
4811 /* we have a ref from do_rbd_add() */
4812 __module_get(THIS_MODULE
);
4814 dout("%s rbd_dev %p dev_id %d\n", __func__
, rbd_dev
, rbd_dev
->dev_id
);
4818 ida_simple_remove(&rbd_dev_id_ida
, rbd_dev
->dev_id
);
4820 rbd_dev_free(rbd_dev
);
4824 static void rbd_dev_destroy(struct rbd_device
*rbd_dev
)
4827 put_device(&rbd_dev
->dev
);
4831 * Get the size and object order for an image snapshot, or if
4832 * snap_id is CEPH_NOSNAP, gets this information for the base
4835 static int _rbd_dev_v2_snap_size(struct rbd_device
*rbd_dev
, u64 snap_id
,
4836 u8
*order
, u64
*snap_size
)
4838 __le64 snapid
= cpu_to_le64(snap_id
);
4843 } __attribute__ ((packed
)) size_buf
= { 0 };
4845 ret
= rbd_obj_method_sync(rbd_dev
, &rbd_dev
->header_oid
,
4846 &rbd_dev
->header_oloc
, "get_size",
4847 &snapid
, sizeof(snapid
),
4848 &size_buf
, sizeof(size_buf
));
4849 dout("%s: rbd_obj_method_sync returned %d\n", __func__
, ret
);
4852 if (ret
< sizeof (size_buf
))
4856 *order
= size_buf
.order
;
4857 dout(" order %u", (unsigned int)*order
);
4859 *snap_size
= le64_to_cpu(size_buf
.size
);
4861 dout(" snap_id 0x%016llx snap_size = %llu\n",
4862 (unsigned long long)snap_id
,
4863 (unsigned long long)*snap_size
);
4868 static int rbd_dev_v2_image_size(struct rbd_device
*rbd_dev
)
4870 return _rbd_dev_v2_snap_size(rbd_dev
, CEPH_NOSNAP
,
4871 &rbd_dev
->header
.obj_order
,
4872 &rbd_dev
->header
.image_size
);
4875 static int rbd_dev_v2_object_prefix(struct rbd_device
*rbd_dev
)
4881 reply_buf
= kzalloc(RBD_OBJ_PREFIX_LEN_MAX
, GFP_KERNEL
);
4885 ret
= rbd_obj_method_sync(rbd_dev
, &rbd_dev
->header_oid
,
4886 &rbd_dev
->header_oloc
, "get_object_prefix",
4887 NULL
, 0, reply_buf
, RBD_OBJ_PREFIX_LEN_MAX
);
4888 dout("%s: rbd_obj_method_sync returned %d\n", __func__
, ret
);
4893 rbd_dev
->header
.object_prefix
= ceph_extract_encoded_string(&p
,
4894 p
+ ret
, NULL
, GFP_NOIO
);
4897 if (IS_ERR(rbd_dev
->header
.object_prefix
)) {
4898 ret
= PTR_ERR(rbd_dev
->header
.object_prefix
);
4899 rbd_dev
->header
.object_prefix
= NULL
;
4901 dout(" object_prefix = %s\n", rbd_dev
->header
.object_prefix
);
4909 static int _rbd_dev_v2_snap_features(struct rbd_device
*rbd_dev
, u64 snap_id
,
4912 __le64 snapid
= cpu_to_le64(snap_id
);
4916 } __attribute__ ((packed
)) features_buf
= { 0 };
4920 ret
= rbd_obj_method_sync(rbd_dev
, &rbd_dev
->header_oid
,
4921 &rbd_dev
->header_oloc
, "get_features",
4922 &snapid
, sizeof(snapid
),
4923 &features_buf
, sizeof(features_buf
));
4924 dout("%s: rbd_obj_method_sync returned %d\n", __func__
, ret
);
4927 if (ret
< sizeof (features_buf
))
4930 unsup
= le64_to_cpu(features_buf
.incompat
) & ~RBD_FEATURES_SUPPORTED
;
4932 rbd_warn(rbd_dev
, "image uses unsupported features: 0x%llx",
4937 *snap_features
= le64_to_cpu(features_buf
.features
);
4939 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
4940 (unsigned long long)snap_id
,
4941 (unsigned long long)*snap_features
,
4942 (unsigned long long)le64_to_cpu(features_buf
.incompat
));
4947 static int rbd_dev_v2_features(struct rbd_device
*rbd_dev
)
4949 return _rbd_dev_v2_snap_features(rbd_dev
, CEPH_NOSNAP
,
4950 &rbd_dev
->header
.features
);
4953 static int rbd_dev_v2_parent_info(struct rbd_device
*rbd_dev
)
4955 struct rbd_spec
*parent_spec
;
4957 void *reply_buf
= NULL
;
4967 parent_spec
= rbd_spec_alloc();
4971 size
= sizeof (__le64
) + /* pool_id */
4972 sizeof (__le32
) + RBD_IMAGE_ID_LEN_MAX
+ /* image_id */
4973 sizeof (__le64
) + /* snap_id */
4974 sizeof (__le64
); /* overlap */
4975 reply_buf
= kmalloc(size
, GFP_KERNEL
);
4981 snapid
= cpu_to_le64(rbd_dev
->spec
->snap_id
);
4982 ret
= rbd_obj_method_sync(rbd_dev
, &rbd_dev
->header_oid
,
4983 &rbd_dev
->header_oloc
, "get_parent",
4984 &snapid
, sizeof(snapid
), reply_buf
, size
);
4985 dout("%s: rbd_obj_method_sync returned %d\n", __func__
, ret
);
4990 end
= reply_buf
+ ret
;
4992 ceph_decode_64_safe(&p
, end
, pool_id
, out_err
);
4993 if (pool_id
== CEPH_NOPOOL
) {
4995 * Either the parent never existed, or we have
4996 * record of it but the image got flattened so it no
4997 * longer has a parent. When the parent of a
4998 * layered image disappears we immediately set the
4999 * overlap to 0. The effect of this is that all new
5000 * requests will be treated as if the image had no
5003 if (rbd_dev
->parent_overlap
) {
5004 rbd_dev
->parent_overlap
= 0;
5005 rbd_dev_parent_put(rbd_dev
);
5006 pr_info("%s: clone image has been flattened\n",
5007 rbd_dev
->disk
->disk_name
);
5010 goto out
; /* No parent? No problem. */
5013 /* The ceph file layout needs to fit pool id in 32 bits */
5016 if (pool_id
> (u64
)U32_MAX
) {
5017 rbd_warn(NULL
, "parent pool id too large (%llu > %u)",
5018 (unsigned long long)pool_id
, U32_MAX
);
5022 image_id
= ceph_extract_encoded_string(&p
, end
, NULL
, GFP_KERNEL
);
5023 if (IS_ERR(image_id
)) {
5024 ret
= PTR_ERR(image_id
);
5027 ceph_decode_64_safe(&p
, end
, snap_id
, out_err
);
5028 ceph_decode_64_safe(&p
, end
, overlap
, out_err
);
5031 * The parent won't change (except when the clone is
5032 * flattened, already handled that). So we only need to
5033 * record the parent spec we have not already done so.
5035 if (!rbd_dev
->parent_spec
) {
5036 parent_spec
->pool_id
= pool_id
;
5037 parent_spec
->image_id
= image_id
;
5038 parent_spec
->snap_id
= snap_id
;
5039 rbd_dev
->parent_spec
= parent_spec
;
5040 parent_spec
= NULL
; /* rbd_dev now owns this */
5046 * We always update the parent overlap. If it's zero we issue
5047 * a warning, as we will proceed as if there was no parent.
5051 /* refresh, careful to warn just once */
5052 if (rbd_dev
->parent_overlap
)
5054 "clone now standalone (overlap became 0)");
5057 rbd_warn(rbd_dev
, "clone is standalone (overlap 0)");
5060 rbd_dev
->parent_overlap
= overlap
;
5066 rbd_spec_put(parent_spec
);
5071 static int rbd_dev_v2_striping_info(struct rbd_device
*rbd_dev
)
5075 __le64 stripe_count
;
5076 } __attribute__ ((packed
)) striping_info_buf
= { 0 };
5077 size_t size
= sizeof (striping_info_buf
);
5084 ret
= rbd_obj_method_sync(rbd_dev
, &rbd_dev
->header_oid
,
5085 &rbd_dev
->header_oloc
, "get_stripe_unit_count",
5086 NULL
, 0, &striping_info_buf
, size
);
5087 dout("%s: rbd_obj_method_sync returned %d\n", __func__
, ret
);
5094 * We don't actually support the "fancy striping" feature
5095 * (STRIPINGV2) yet, but if the striping sizes are the
5096 * defaults the behavior is the same as before. So find
5097 * out, and only fail if the image has non-default values.
5100 obj_size
= rbd_obj_bytes(&rbd_dev
->header
);
5101 p
= &striping_info_buf
;
5102 stripe_unit
= ceph_decode_64(&p
);
5103 if (stripe_unit
!= obj_size
) {
5104 rbd_warn(rbd_dev
, "unsupported stripe unit "
5105 "(got %llu want %llu)",
5106 stripe_unit
, obj_size
);
5109 stripe_count
= ceph_decode_64(&p
);
5110 if (stripe_count
!= 1) {
5111 rbd_warn(rbd_dev
, "unsupported stripe count "
5112 "(got %llu want 1)", stripe_count
);
5115 rbd_dev
->header
.stripe_unit
= stripe_unit
;
5116 rbd_dev
->header
.stripe_count
= stripe_count
;
5121 static int rbd_dev_v2_data_pool(struct rbd_device
*rbd_dev
)
5123 __le64 data_pool_id
;
5126 ret
= rbd_obj_method_sync(rbd_dev
, &rbd_dev
->header_oid
,
5127 &rbd_dev
->header_oloc
, "get_data_pool",
5128 NULL
, 0, &data_pool_id
, sizeof(data_pool_id
));
5131 if (ret
< sizeof(data_pool_id
))
5134 rbd_dev
->header
.data_pool_id
= le64_to_cpu(data_pool_id
);
5135 WARN_ON(rbd_dev
->header
.data_pool_id
== CEPH_NOPOOL
);
5139 static char *rbd_dev_image_name(struct rbd_device
*rbd_dev
)
5141 CEPH_DEFINE_OID_ONSTACK(oid
);
5142 size_t image_id_size
;
5147 void *reply_buf
= NULL
;
5149 char *image_name
= NULL
;
5152 rbd_assert(!rbd_dev
->spec
->image_name
);
5154 len
= strlen(rbd_dev
->spec
->image_id
);
5155 image_id_size
= sizeof (__le32
) + len
;
5156 image_id
= kmalloc(image_id_size
, GFP_KERNEL
);
5161 end
= image_id
+ image_id_size
;
5162 ceph_encode_string(&p
, end
, rbd_dev
->spec
->image_id
, (u32
)len
);
5164 size
= sizeof (__le32
) + RBD_IMAGE_NAME_LEN_MAX
;
5165 reply_buf
= kmalloc(size
, GFP_KERNEL
);
5169 ceph_oid_printf(&oid
, "%s", RBD_DIRECTORY
);
5170 ret
= rbd_obj_method_sync(rbd_dev
, &oid
, &rbd_dev
->header_oloc
,
5171 "dir_get_name", image_id
, image_id_size
,
5176 end
= reply_buf
+ ret
;
5178 image_name
= ceph_extract_encoded_string(&p
, end
, &len
, GFP_KERNEL
);
5179 if (IS_ERR(image_name
))
5182 dout("%s: name is %s len is %zd\n", __func__
, image_name
, len
);
5190 static u64
rbd_v1_snap_id_by_name(struct rbd_device
*rbd_dev
, const char *name
)
5192 struct ceph_snap_context
*snapc
= rbd_dev
->header
.snapc
;
5193 const char *snap_name
;
5196 /* Skip over names until we find the one we are looking for */
5198 snap_name
= rbd_dev
->header
.snap_names
;
5199 while (which
< snapc
->num_snaps
) {
5200 if (!strcmp(name
, snap_name
))
5201 return snapc
->snaps
[which
];
5202 snap_name
+= strlen(snap_name
) + 1;
5208 static u64
rbd_v2_snap_id_by_name(struct rbd_device
*rbd_dev
, const char *name
)
5210 struct ceph_snap_context
*snapc
= rbd_dev
->header
.snapc
;
5215 for (which
= 0; !found
&& which
< snapc
->num_snaps
; which
++) {
5216 const char *snap_name
;
5218 snap_id
= snapc
->snaps
[which
];
5219 snap_name
= rbd_dev_v2_snap_name(rbd_dev
, snap_id
);
5220 if (IS_ERR(snap_name
)) {
5221 /* ignore no-longer existing snapshots */
5222 if (PTR_ERR(snap_name
) == -ENOENT
)
5227 found
= !strcmp(name
, snap_name
);
5230 return found
? snap_id
: CEPH_NOSNAP
;
5234 * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
5235 * no snapshot by that name is found, or if an error occurs.
5237 static u64
rbd_snap_id_by_name(struct rbd_device
*rbd_dev
, const char *name
)
5239 if (rbd_dev
->image_format
== 1)
5240 return rbd_v1_snap_id_by_name(rbd_dev
, name
);
5242 return rbd_v2_snap_id_by_name(rbd_dev
, name
);
5246 * An image being mapped will have everything but the snap id.
5248 static int rbd_spec_fill_snap_id(struct rbd_device
*rbd_dev
)
5250 struct rbd_spec
*spec
= rbd_dev
->spec
;
5252 rbd_assert(spec
->pool_id
!= CEPH_NOPOOL
&& spec
->pool_name
);
5253 rbd_assert(spec
->image_id
&& spec
->image_name
);
5254 rbd_assert(spec
->snap_name
);
5256 if (strcmp(spec
->snap_name
, RBD_SNAP_HEAD_NAME
)) {
5259 snap_id
= rbd_snap_id_by_name(rbd_dev
, spec
->snap_name
);
5260 if (snap_id
== CEPH_NOSNAP
)
5263 spec
->snap_id
= snap_id
;
5265 spec
->snap_id
= CEPH_NOSNAP
;
5272 * A parent image will have all ids but none of the names.
5274 * All names in an rbd spec are dynamically allocated. It's OK if we
5275 * can't figure out the name for an image id.
5277 static int rbd_spec_fill_names(struct rbd_device
*rbd_dev
)
5279 struct ceph_osd_client
*osdc
= &rbd_dev
->rbd_client
->client
->osdc
;
5280 struct rbd_spec
*spec
= rbd_dev
->spec
;
5281 const char *pool_name
;
5282 const char *image_name
;
5283 const char *snap_name
;
5286 rbd_assert(spec
->pool_id
!= CEPH_NOPOOL
);
5287 rbd_assert(spec
->image_id
);
5288 rbd_assert(spec
->snap_id
!= CEPH_NOSNAP
);
5290 /* Get the pool name; we have to make our own copy of this */
5292 pool_name
= ceph_pg_pool_name_by_id(osdc
->osdmap
, spec
->pool_id
);
5294 rbd_warn(rbd_dev
, "no pool with id %llu", spec
->pool_id
);
5297 pool_name
= kstrdup(pool_name
, GFP_KERNEL
);
5301 /* Fetch the image name; tolerate failure here */
5303 image_name
= rbd_dev_image_name(rbd_dev
);
5305 rbd_warn(rbd_dev
, "unable to get image name");
5307 /* Fetch the snapshot name */
5309 snap_name
= rbd_snap_name(rbd_dev
, spec
->snap_id
);
5310 if (IS_ERR(snap_name
)) {
5311 ret
= PTR_ERR(snap_name
);
5315 spec
->pool_name
= pool_name
;
5316 spec
->image_name
= image_name
;
5317 spec
->snap_name
= snap_name
;
5327 static int rbd_dev_v2_snap_context(struct rbd_device
*rbd_dev
)
5336 struct ceph_snap_context
*snapc
;
5340 * We'll need room for the seq value (maximum snapshot id),
5341 * snapshot count, and array of that many snapshot ids.
5342 * For now we have a fixed upper limit on the number we're
5343 * prepared to receive.
5345 size
= sizeof (__le64
) + sizeof (__le32
) +
5346 RBD_MAX_SNAP_COUNT
* sizeof (__le64
);
5347 reply_buf
= kzalloc(size
, GFP_KERNEL
);
5351 ret
= rbd_obj_method_sync(rbd_dev
, &rbd_dev
->header_oid
,
5352 &rbd_dev
->header_oloc
, "get_snapcontext",
5353 NULL
, 0, reply_buf
, size
);
5354 dout("%s: rbd_obj_method_sync returned %d\n", __func__
, ret
);
5359 end
= reply_buf
+ ret
;
5361 ceph_decode_64_safe(&p
, end
, seq
, out
);
5362 ceph_decode_32_safe(&p
, end
, snap_count
, out
);
5365 * Make sure the reported number of snapshot ids wouldn't go
5366 * beyond the end of our buffer. But before checking that,
5367 * make sure the computed size of the snapshot context we
5368 * allocate is representable in a size_t.
5370 if (snap_count
> (SIZE_MAX
- sizeof (struct ceph_snap_context
))
5375 if (!ceph_has_room(&p
, end
, snap_count
* sizeof (__le64
)))
5379 snapc
= ceph_create_snap_context(snap_count
, GFP_KERNEL
);
5385 for (i
= 0; i
< snap_count
; i
++)
5386 snapc
->snaps
[i
] = ceph_decode_64(&p
);
5388 ceph_put_snap_context(rbd_dev
->header
.snapc
);
5389 rbd_dev
->header
.snapc
= snapc
;
5391 dout(" snap context seq = %llu, snap_count = %u\n",
5392 (unsigned long long)seq
, (unsigned int)snap_count
);
5399 static const char *rbd_dev_v2_snap_name(struct rbd_device
*rbd_dev
,
5410 size
= sizeof (__le32
) + RBD_MAX_SNAP_NAME_LEN
;
5411 reply_buf
= kmalloc(size
, GFP_KERNEL
);
5413 return ERR_PTR(-ENOMEM
);
5415 snapid
= cpu_to_le64(snap_id
);
5416 ret
= rbd_obj_method_sync(rbd_dev
, &rbd_dev
->header_oid
,
5417 &rbd_dev
->header_oloc
, "get_snapshot_name",
5418 &snapid
, sizeof(snapid
), reply_buf
, size
);
5419 dout("%s: rbd_obj_method_sync returned %d\n", __func__
, ret
);
5421 snap_name
= ERR_PTR(ret
);
5426 end
= reply_buf
+ ret
;
5427 snap_name
= ceph_extract_encoded_string(&p
, end
, NULL
, GFP_KERNEL
);
5428 if (IS_ERR(snap_name
))
5431 dout(" snap_id 0x%016llx snap_name = %s\n",
5432 (unsigned long long)snap_id
, snap_name
);
5439 static int rbd_dev_v2_header_info(struct rbd_device
*rbd_dev
)
5441 bool first_time
= rbd_dev
->header
.object_prefix
== NULL
;
5444 ret
= rbd_dev_v2_image_size(rbd_dev
);
5449 ret
= rbd_dev_v2_header_onetime(rbd_dev
);
5454 ret
= rbd_dev_v2_snap_context(rbd_dev
);
5455 if (ret
&& first_time
) {
5456 kfree(rbd_dev
->header
.object_prefix
);
5457 rbd_dev
->header
.object_prefix
= NULL
;
5463 static int rbd_dev_header_info(struct rbd_device
*rbd_dev
)
5465 rbd_assert(rbd_image_format_valid(rbd_dev
->image_format
));
5467 if (rbd_dev
->image_format
== 1)
5468 return rbd_dev_v1_header_info(rbd_dev
);
5470 return rbd_dev_v2_header_info(rbd_dev
);
5474 * Skips over white space at *buf, and updates *buf to point to the
5475 * first found non-space character (if any). Returns the length of
5476 * the token (string of non-white space characters) found. Note
5477 * that *buf must be terminated with '\0'.
5479 static inline size_t next_token(const char **buf
)
5482 * These are the characters that produce nonzero for
5483 * isspace() in the "C" and "POSIX" locales.
5485 const char *spaces
= " \f\n\r\t\v";
5487 *buf
+= strspn(*buf
, spaces
); /* Find start of token */
5489 return strcspn(*buf
, spaces
); /* Return token length */
5493 * Finds the next token in *buf, dynamically allocates a buffer big
5494 * enough to hold a copy of it, and copies the token into the new
5495 * buffer. The copy is guaranteed to be terminated with '\0'. Note
5496 * that a duplicate buffer is created even for a zero-length token.
5498 * Returns a pointer to the newly-allocated duplicate, or a null
5499 * pointer if memory for the duplicate was not available. If
5500 * the lenp argument is a non-null pointer, the length of the token
5501 * (not including the '\0') is returned in *lenp.
5503 * If successful, the *buf pointer will be updated to point beyond
5504 * the end of the found token.
5506 * Note: uses GFP_KERNEL for allocation.
5508 static inline char *dup_token(const char **buf
, size_t *lenp
)
5513 len
= next_token(buf
);
5514 dup
= kmemdup(*buf
, len
+ 1, GFP_KERNEL
);
5517 *(dup
+ len
) = '\0';
5527 * Parse the options provided for an "rbd add" (i.e., rbd image
5528 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
5529 * and the data written is passed here via a NUL-terminated buffer.
5530 * Returns 0 if successful or an error code otherwise.
5532 * The information extracted from these options is recorded in
5533 * the other parameters which return dynamically-allocated
5536 * The address of a pointer that will refer to a ceph options
5537 * structure. Caller must release the returned pointer using
5538 * ceph_destroy_options() when it is no longer needed.
5540 * Address of an rbd options pointer. Fully initialized by
5541 * this function; caller must release with kfree().
5543 * Address of an rbd image specification pointer. Fully
5544 * initialized by this function based on parsed options.
5545 * Caller must release with rbd_spec_put().
5547 * The options passed take this form:
5548 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
5551 * A comma-separated list of one or more monitor addresses.
5552 * A monitor address is an ip address, optionally followed
5553 * by a port number (separated by a colon).
5554 * I.e.: ip1[:port1][,ip2[:port2]...]
5556 * A comma-separated list of ceph and/or rbd options.
5558 * The name of the rados pool containing the rbd image.
5560 * The name of the image in that pool to map.
5562 * An optional snapshot id. If provided, the mapping will
5563 * present data from the image at the time that snapshot was
5564 * created. The image head is used if no snapshot id is
5565 * provided. Snapshot mappings are always read-only.
5567 static int rbd_add_parse_args(const char *buf
,
5568 struct ceph_options
**ceph_opts
,
5569 struct rbd_options
**opts
,
5570 struct rbd_spec
**rbd_spec
)
5574 const char *mon_addrs
;
5576 size_t mon_addrs_size
;
5577 struct rbd_spec
*spec
= NULL
;
5578 struct rbd_options
*rbd_opts
= NULL
;
5579 struct ceph_options
*copts
;
5582 /* The first four tokens are required */
5584 len
= next_token(&buf
);
5586 rbd_warn(NULL
, "no monitor address(es) provided");
5590 mon_addrs_size
= len
+ 1;
5594 options
= dup_token(&buf
, NULL
);
5598 rbd_warn(NULL
, "no options provided");
5602 spec
= rbd_spec_alloc();
5606 spec
->pool_name
= dup_token(&buf
, NULL
);
5607 if (!spec
->pool_name
)
5609 if (!*spec
->pool_name
) {
5610 rbd_warn(NULL
, "no pool name provided");
5614 spec
->image_name
= dup_token(&buf
, NULL
);
5615 if (!spec
->image_name
)
5617 if (!*spec
->image_name
) {
5618 rbd_warn(NULL
, "no image name provided");
5623 * Snapshot name is optional; default is to use "-"
5624 * (indicating the head/no snapshot).
5626 len
= next_token(&buf
);
5628 buf
= RBD_SNAP_HEAD_NAME
; /* No snapshot supplied */
5629 len
= sizeof (RBD_SNAP_HEAD_NAME
) - 1;
5630 } else if (len
> RBD_MAX_SNAP_NAME_LEN
) {
5631 ret
= -ENAMETOOLONG
;
5634 snap_name
= kmemdup(buf
, len
+ 1, GFP_KERNEL
);
5637 *(snap_name
+ len
) = '\0';
5638 spec
->snap_name
= snap_name
;
5640 /* Initialize all rbd options to the defaults */
5642 rbd_opts
= kzalloc(sizeof (*rbd_opts
), GFP_KERNEL
);
5646 rbd_opts
->read_only
= RBD_READ_ONLY_DEFAULT
;
5647 rbd_opts
->queue_depth
= RBD_QUEUE_DEPTH_DEFAULT
;
5648 rbd_opts
->lock_on_read
= RBD_LOCK_ON_READ_DEFAULT
;
5649 rbd_opts
->exclusive
= RBD_EXCLUSIVE_DEFAULT
;
5651 copts
= ceph_parse_options(options
, mon_addrs
,
5652 mon_addrs
+ mon_addrs_size
- 1,
5653 parse_rbd_opts_token
, rbd_opts
);
5654 if (IS_ERR(copts
)) {
5655 ret
= PTR_ERR(copts
);
5676 * Return pool id (>= 0) or a negative error code.
5678 static int rbd_add_get_pool_id(struct rbd_client
*rbdc
, const char *pool_name
)
5680 struct ceph_options
*opts
= rbdc
->client
->options
;
5686 ret
= ceph_pg_poolid_by_name(rbdc
->client
->osdc
.osdmap
, pool_name
);
5687 if (ret
== -ENOENT
&& tries
++ < 1) {
5688 ret
= ceph_monc_get_version(&rbdc
->client
->monc
, "osdmap",
5693 if (rbdc
->client
->osdc
.osdmap
->epoch
< newest_epoch
) {
5694 ceph_osdc_maybe_request_map(&rbdc
->client
->osdc
);
5695 (void) ceph_monc_wait_osdmap(&rbdc
->client
->monc
,
5697 opts
->mount_timeout
);
5700 /* the osdmap we have is new enough */
5708 static void rbd_dev_image_unlock(struct rbd_device
*rbd_dev
)
5710 down_write(&rbd_dev
->lock_rwsem
);
5711 if (__rbd_is_lock_owner(rbd_dev
))
5712 rbd_unlock(rbd_dev
);
5713 up_write(&rbd_dev
->lock_rwsem
);
5716 static int rbd_add_acquire_lock(struct rbd_device
*rbd_dev
)
5718 if (!(rbd_dev
->header
.features
& RBD_FEATURE_EXCLUSIVE_LOCK
)) {
5719 rbd_warn(rbd_dev
, "exclusive-lock feature is not enabled");
5723 /* FIXME: "rbd map --exclusive" should be in interruptible */
5724 down_read(&rbd_dev
->lock_rwsem
);
5725 rbd_wait_state_locked(rbd_dev
);
5726 up_read(&rbd_dev
->lock_rwsem
);
5727 if (test_bit(RBD_DEV_FLAG_BLACKLISTED
, &rbd_dev
->flags
)) {
5728 rbd_warn(rbd_dev
, "failed to acquire exclusive lock");
5736 * An rbd format 2 image has a unique identifier, distinct from the
5737 * name given to it by the user. Internally, that identifier is
5738 * what's used to specify the names of objects related to the image.
5740 * A special "rbd id" object is used to map an rbd image name to its
5741 * id. If that object doesn't exist, then there is no v2 rbd image
5742 * with the supplied name.
5744 * This function will record the given rbd_dev's image_id field if
5745 * it can be determined, and in that case will return 0. If any
5746 * errors occur a negative errno will be returned and the rbd_dev's
5747 * image_id field will be unchanged (and should be NULL).
5749 static int rbd_dev_image_id(struct rbd_device
*rbd_dev
)
5753 CEPH_DEFINE_OID_ONSTACK(oid
);
5758 * When probing a parent image, the image id is already
5759 * known (and the image name likely is not). There's no
5760 * need to fetch the image id again in this case. We
5761 * do still need to set the image format though.
5763 if (rbd_dev
->spec
->image_id
) {
5764 rbd_dev
->image_format
= *rbd_dev
->spec
->image_id
? 2 : 1;
5770 * First, see if the format 2 image id file exists, and if
5771 * so, get the image's persistent id from it.
5773 ret
= ceph_oid_aprintf(&oid
, GFP_KERNEL
, "%s%s", RBD_ID_PREFIX
,
5774 rbd_dev
->spec
->image_name
);
5778 dout("rbd id object name is %s\n", oid
.name
);
5780 /* Response will be an encoded string, which includes a length */
5782 size
= sizeof (__le32
) + RBD_IMAGE_ID_LEN_MAX
;
5783 response
= kzalloc(size
, GFP_NOIO
);
5789 /* If it doesn't exist we'll assume it's a format 1 image */
5791 ret
= rbd_obj_method_sync(rbd_dev
, &oid
, &rbd_dev
->header_oloc
,
5793 response
, RBD_IMAGE_ID_LEN_MAX
);
5794 dout("%s: rbd_obj_method_sync returned %d\n", __func__
, ret
);
5795 if (ret
== -ENOENT
) {
5796 image_id
= kstrdup("", GFP_KERNEL
);
5797 ret
= image_id
? 0 : -ENOMEM
;
5799 rbd_dev
->image_format
= 1;
5800 } else if (ret
>= 0) {
5803 image_id
= ceph_extract_encoded_string(&p
, p
+ ret
,
5805 ret
= PTR_ERR_OR_ZERO(image_id
);
5807 rbd_dev
->image_format
= 2;
5811 rbd_dev
->spec
->image_id
= image_id
;
5812 dout("image_id is %s\n", image_id
);
5816 ceph_oid_destroy(&oid
);
5821 * Undo whatever state changes are made by v1 or v2 header info
5824 static void rbd_dev_unprobe(struct rbd_device
*rbd_dev
)
5826 struct rbd_image_header
*header
;
5828 rbd_dev_parent_put(rbd_dev
);
5830 /* Free dynamic fields from the header, then zero it out */
5832 header
= &rbd_dev
->header
;
5833 ceph_put_snap_context(header
->snapc
);
5834 kfree(header
->snap_sizes
);
5835 kfree(header
->snap_names
);
5836 kfree(header
->object_prefix
);
5837 memset(header
, 0, sizeof (*header
));
5840 static int rbd_dev_v2_header_onetime(struct rbd_device
*rbd_dev
)
5844 ret
= rbd_dev_v2_object_prefix(rbd_dev
);
5849 * Get the and check features for the image. Currently the
5850 * features are assumed to never change.
5852 ret
= rbd_dev_v2_features(rbd_dev
);
5856 /* If the image supports fancy striping, get its parameters */
5858 if (rbd_dev
->header
.features
& RBD_FEATURE_STRIPINGV2
) {
5859 ret
= rbd_dev_v2_striping_info(rbd_dev
);
5864 if (rbd_dev
->header
.features
& RBD_FEATURE_DATA_POOL
) {
5865 ret
= rbd_dev_v2_data_pool(rbd_dev
);
5870 rbd_init_layout(rbd_dev
);
5874 rbd_dev
->header
.features
= 0;
5875 kfree(rbd_dev
->header
.object_prefix
);
5876 rbd_dev
->header
.object_prefix
= NULL
;
5881 * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() ->
5882 * rbd_dev_image_probe() recursion depth, which means it's also the
5883 * length of the already discovered part of the parent chain.
5885 static int rbd_dev_probe_parent(struct rbd_device
*rbd_dev
, int depth
)
5887 struct rbd_device
*parent
= NULL
;
5890 if (!rbd_dev
->parent_spec
)
5893 if (++depth
> RBD_MAX_PARENT_CHAIN_LEN
) {
5894 pr_info("parent chain is too long (%d)\n", depth
);
5899 parent
= __rbd_dev_create(rbd_dev
->rbd_client
, rbd_dev
->parent_spec
);
5906 * Images related by parent/child relationships always share
5907 * rbd_client and spec/parent_spec, so bump their refcounts.
5909 __rbd_get_client(rbd_dev
->rbd_client
);
5910 rbd_spec_get(rbd_dev
->parent_spec
);
5912 ret
= rbd_dev_image_probe(parent
, depth
);
5916 rbd_dev
->parent
= parent
;
5917 atomic_set(&rbd_dev
->parent_ref
, 1);
5921 rbd_dev_unparent(rbd_dev
);
5922 rbd_dev_destroy(parent
);
5926 static void rbd_dev_device_release(struct rbd_device
*rbd_dev
)
5928 clear_bit(RBD_DEV_FLAG_EXISTS
, &rbd_dev
->flags
);
5929 rbd_dev_mapping_clear(rbd_dev
);
5930 rbd_free_disk(rbd_dev
);
5932 unregister_blkdev(rbd_dev
->major
, rbd_dev
->name
);
5936 * rbd_dev->header_rwsem must be locked for write and will be unlocked
5939 static int rbd_dev_device_setup(struct rbd_device
*rbd_dev
)
5943 /* Record our major and minor device numbers. */
5945 if (!single_major
) {
5946 ret
= register_blkdev(0, rbd_dev
->name
);
5948 goto err_out_unlock
;
5950 rbd_dev
->major
= ret
;
5953 rbd_dev
->major
= rbd_major
;
5954 rbd_dev
->minor
= rbd_dev_id_to_minor(rbd_dev
->dev_id
);
5957 /* Set up the blkdev mapping. */
5959 ret
= rbd_init_disk(rbd_dev
);
5961 goto err_out_blkdev
;
5963 ret
= rbd_dev_mapping_set(rbd_dev
);
5967 set_capacity(rbd_dev
->disk
, rbd_dev
->mapping
.size
/ SECTOR_SIZE
);
5968 set_disk_ro(rbd_dev
->disk
, rbd_dev
->opts
->read_only
);
5970 ret
= dev_set_name(&rbd_dev
->dev
, "%d", rbd_dev
->dev_id
);
5972 goto err_out_mapping
;
5974 set_bit(RBD_DEV_FLAG_EXISTS
, &rbd_dev
->flags
);
5975 up_write(&rbd_dev
->header_rwsem
);
5979 rbd_dev_mapping_clear(rbd_dev
);
5981 rbd_free_disk(rbd_dev
);
5984 unregister_blkdev(rbd_dev
->major
, rbd_dev
->name
);
5986 up_write(&rbd_dev
->header_rwsem
);
5990 static int rbd_dev_header_name(struct rbd_device
*rbd_dev
)
5992 struct rbd_spec
*spec
= rbd_dev
->spec
;
5995 /* Record the header object name for this rbd image. */
5997 rbd_assert(rbd_image_format_valid(rbd_dev
->image_format
));
5998 if (rbd_dev
->image_format
== 1)
5999 ret
= ceph_oid_aprintf(&rbd_dev
->header_oid
, GFP_KERNEL
, "%s%s",
6000 spec
->image_name
, RBD_SUFFIX
);
6002 ret
= ceph_oid_aprintf(&rbd_dev
->header_oid
, GFP_KERNEL
, "%s%s",
6003 RBD_HEADER_PREFIX
, spec
->image_id
);
6008 static void rbd_dev_image_release(struct rbd_device
*rbd_dev
)
6010 rbd_dev_unprobe(rbd_dev
);
6012 rbd_unregister_watch(rbd_dev
);
6013 rbd_dev
->image_format
= 0;
6014 kfree(rbd_dev
->spec
->image_id
);
6015 rbd_dev
->spec
->image_id
= NULL
;
6019 * Probe for the existence of the header object for the given rbd
6020 * device. If this image is the one being mapped (i.e., not a
6021 * parent), initiate a watch on its header object before using that
6022 * object to get detailed information about the rbd image.
6024 static int rbd_dev_image_probe(struct rbd_device
*rbd_dev
, int depth
)
6029 * Get the id from the image id object. Unless there's an
6030 * error, rbd_dev->spec->image_id will be filled in with
6031 * a dynamically-allocated string, and rbd_dev->image_format
6032 * will be set to either 1 or 2.
6034 ret
= rbd_dev_image_id(rbd_dev
);
6038 ret
= rbd_dev_header_name(rbd_dev
);
6040 goto err_out_format
;
6043 ret
= rbd_register_watch(rbd_dev
);
6046 pr_info("image %s/%s does not exist\n",
6047 rbd_dev
->spec
->pool_name
,
6048 rbd_dev
->spec
->image_name
);
6049 goto err_out_format
;
6053 ret
= rbd_dev_header_info(rbd_dev
);
6058 * If this image is the one being mapped, we have pool name and
6059 * id, image name and id, and snap name - need to fill snap id.
6060 * Otherwise this is a parent image, identified by pool, image
6061 * and snap ids - need to fill in names for those ids.
6064 ret
= rbd_spec_fill_snap_id(rbd_dev
);
6066 ret
= rbd_spec_fill_names(rbd_dev
);
6069 pr_info("snap %s/%s@%s does not exist\n",
6070 rbd_dev
->spec
->pool_name
,
6071 rbd_dev
->spec
->image_name
,
6072 rbd_dev
->spec
->snap_name
);
6076 if (rbd_dev
->header
.features
& RBD_FEATURE_LAYERING
) {
6077 ret
= rbd_dev_v2_parent_info(rbd_dev
);
6082 * Need to warn users if this image is the one being
6083 * mapped and has a parent.
6085 if (!depth
&& rbd_dev
->parent_spec
)
6087 "WARNING: kernel layering is EXPERIMENTAL!");
6090 ret
= rbd_dev_probe_parent(rbd_dev
, depth
);
6094 dout("discovered format %u image, header name is %s\n",
6095 rbd_dev
->image_format
, rbd_dev
->header_oid
.name
);
6099 rbd_dev_unprobe(rbd_dev
);
6102 rbd_unregister_watch(rbd_dev
);
6104 rbd_dev
->image_format
= 0;
6105 kfree(rbd_dev
->spec
->image_id
);
6106 rbd_dev
->spec
->image_id
= NULL
;
6110 static ssize_t
do_rbd_add(struct bus_type
*bus
,
6114 struct rbd_device
*rbd_dev
= NULL
;
6115 struct ceph_options
*ceph_opts
= NULL
;
6116 struct rbd_options
*rbd_opts
= NULL
;
6117 struct rbd_spec
*spec
= NULL
;
6118 struct rbd_client
*rbdc
;
6121 if (!try_module_get(THIS_MODULE
))
6124 /* parse add command */
6125 rc
= rbd_add_parse_args(buf
, &ceph_opts
, &rbd_opts
, &spec
);
6129 rbdc
= rbd_get_client(ceph_opts
);
6136 rc
= rbd_add_get_pool_id(rbdc
, spec
->pool_name
);
6139 pr_info("pool %s does not exist\n", spec
->pool_name
);
6140 goto err_out_client
;
6142 spec
->pool_id
= (u64
)rc
;
6144 rbd_dev
= rbd_dev_create(rbdc
, spec
, rbd_opts
);
6147 goto err_out_client
;
6149 rbdc
= NULL
; /* rbd_dev now owns this */
6150 spec
= NULL
; /* rbd_dev now owns this */
6151 rbd_opts
= NULL
; /* rbd_dev now owns this */
6153 rbd_dev
->config_info
= kstrdup(buf
, GFP_KERNEL
);
6154 if (!rbd_dev
->config_info
) {
6156 goto err_out_rbd_dev
;
6159 down_write(&rbd_dev
->header_rwsem
);
6160 rc
= rbd_dev_image_probe(rbd_dev
, 0);
6162 up_write(&rbd_dev
->header_rwsem
);
6163 goto err_out_rbd_dev
;
6166 /* If we are mapping a snapshot it must be marked read-only */
6167 if (rbd_dev
->spec
->snap_id
!= CEPH_NOSNAP
)
6168 rbd_dev
->opts
->read_only
= true;
6170 rc
= rbd_dev_device_setup(rbd_dev
);
6172 goto err_out_image_probe
;
6174 if (rbd_dev
->opts
->exclusive
) {
6175 rc
= rbd_add_acquire_lock(rbd_dev
);
6177 goto err_out_device_setup
;
6180 /* Everything's ready. Announce the disk to the world. */
6182 rc
= device_add(&rbd_dev
->dev
);
6184 goto err_out_image_lock
;
6186 add_disk(rbd_dev
->disk
);
6187 /* see rbd_init_disk() */
6188 blk_put_queue(rbd_dev
->disk
->queue
);
6190 spin_lock(&rbd_dev_list_lock
);
6191 list_add_tail(&rbd_dev
->node
, &rbd_dev_list
);
6192 spin_unlock(&rbd_dev_list_lock
);
6194 pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev
->disk
->disk_name
,
6195 (unsigned long long)get_capacity(rbd_dev
->disk
) << SECTOR_SHIFT
,
6196 rbd_dev
->header
.features
);
6199 module_put(THIS_MODULE
);
6203 rbd_dev_image_unlock(rbd_dev
);
6204 err_out_device_setup
:
6205 rbd_dev_device_release(rbd_dev
);
6206 err_out_image_probe
:
6207 rbd_dev_image_release(rbd_dev
);
6209 rbd_dev_destroy(rbd_dev
);
6211 rbd_put_client(rbdc
);
6218 static ssize_t
rbd_add(struct bus_type
*bus
,
6225 return do_rbd_add(bus
, buf
, count
);
6228 static ssize_t
rbd_add_single_major(struct bus_type
*bus
,
6232 return do_rbd_add(bus
, buf
, count
);
6235 static void rbd_dev_remove_parent(struct rbd_device
*rbd_dev
)
6237 while (rbd_dev
->parent
) {
6238 struct rbd_device
*first
= rbd_dev
;
6239 struct rbd_device
*second
= first
->parent
;
6240 struct rbd_device
*third
;
6243 * Follow to the parent with no grandparent and
6246 while (second
&& (third
= second
->parent
)) {
6251 rbd_dev_image_release(second
);
6252 rbd_dev_destroy(second
);
6253 first
->parent
= NULL
;
6254 first
->parent_overlap
= 0;
6256 rbd_assert(first
->parent_spec
);
6257 rbd_spec_put(first
->parent_spec
);
6258 first
->parent_spec
= NULL
;
6262 static ssize_t
do_rbd_remove(struct bus_type
*bus
,
6266 struct rbd_device
*rbd_dev
= NULL
;
6267 struct list_head
*tmp
;
6270 bool already
= false;
6276 sscanf(buf
, "%d %5s", &dev_id
, opt_buf
);
6278 pr_err("dev_id out of range\n");
6281 if (opt_buf
[0] != '\0') {
6282 if (!strcmp(opt_buf
, "force")) {
6285 pr_err("bad remove option at '%s'\n", opt_buf
);
6291 spin_lock(&rbd_dev_list_lock
);
6292 list_for_each(tmp
, &rbd_dev_list
) {
6293 rbd_dev
= list_entry(tmp
, struct rbd_device
, node
);
6294 if (rbd_dev
->dev_id
== dev_id
) {
6300 spin_lock_irq(&rbd_dev
->lock
);
6301 if (rbd_dev
->open_count
&& !force
)
6304 already
= test_and_set_bit(RBD_DEV_FLAG_REMOVING
,
6306 spin_unlock_irq(&rbd_dev
->lock
);
6308 spin_unlock(&rbd_dev_list_lock
);
6309 if (ret
< 0 || already
)
6314 * Prevent new IO from being queued and wait for existing
6315 * IO to complete/fail.
6317 blk_mq_freeze_queue(rbd_dev
->disk
->queue
);
6318 blk_set_queue_dying(rbd_dev
->disk
->queue
);
6321 del_gendisk(rbd_dev
->disk
);
6322 spin_lock(&rbd_dev_list_lock
);
6323 list_del_init(&rbd_dev
->node
);
6324 spin_unlock(&rbd_dev_list_lock
);
6325 device_del(&rbd_dev
->dev
);
6327 rbd_dev_image_unlock(rbd_dev
);
6328 rbd_dev_device_release(rbd_dev
);
6329 rbd_dev_image_release(rbd_dev
);
6330 rbd_dev_destroy(rbd_dev
);
6334 static ssize_t
rbd_remove(struct bus_type
*bus
,
6341 return do_rbd_remove(bus
, buf
, count
);
6344 static ssize_t
rbd_remove_single_major(struct bus_type
*bus
,
6348 return do_rbd_remove(bus
, buf
, count
);
6352 * create control files in sysfs
6355 static int rbd_sysfs_init(void)
6359 ret
= device_register(&rbd_root_dev
);
6363 ret
= bus_register(&rbd_bus_type
);
6365 device_unregister(&rbd_root_dev
);
6370 static void rbd_sysfs_cleanup(void)
6372 bus_unregister(&rbd_bus_type
);
6373 device_unregister(&rbd_root_dev
);
6376 static int rbd_slab_init(void)
6378 rbd_assert(!rbd_img_request_cache
);
6379 rbd_img_request_cache
= KMEM_CACHE(rbd_img_request
, 0);
6380 if (!rbd_img_request_cache
)
6383 rbd_assert(!rbd_obj_request_cache
);
6384 rbd_obj_request_cache
= KMEM_CACHE(rbd_obj_request
, 0);
6385 if (!rbd_obj_request_cache
)
6388 rbd_assert(!rbd_bio_clone
);
6389 rbd_bio_clone
= bioset_create(BIO_POOL_SIZE
, 0, 0);
6396 kmem_cache_destroy(rbd_obj_request_cache
);
6397 rbd_obj_request_cache
= NULL
;
6399 kmem_cache_destroy(rbd_img_request_cache
);
6400 rbd_img_request_cache
= NULL
;
6404 static void rbd_slab_exit(void)
6406 rbd_assert(rbd_obj_request_cache
);
6407 kmem_cache_destroy(rbd_obj_request_cache
);
6408 rbd_obj_request_cache
= NULL
;
6410 rbd_assert(rbd_img_request_cache
);
6411 kmem_cache_destroy(rbd_img_request_cache
);
6412 rbd_img_request_cache
= NULL
;
6414 rbd_assert(rbd_bio_clone
);
6415 bioset_free(rbd_bio_clone
);
6416 rbd_bio_clone
= NULL
;
6419 static int __init
rbd_init(void)
6423 if (!libceph_compatible(NULL
)) {
6424 rbd_warn(NULL
, "libceph incompatibility (quitting)");
6428 rc
= rbd_slab_init();
6433 * The number of active work items is limited by the number of
6434 * rbd devices * queue depth, so leave @max_active at default.
6436 rbd_wq
= alloc_workqueue(RBD_DRV_NAME
, WQ_MEM_RECLAIM
, 0);
6443 rbd_major
= register_blkdev(0, RBD_DRV_NAME
);
6444 if (rbd_major
< 0) {
6450 rc
= rbd_sysfs_init();
6452 goto err_out_blkdev
;
6455 pr_info("loaded (major %d)\n", rbd_major
);
6457 pr_info("loaded\n");
6463 unregister_blkdev(rbd_major
, RBD_DRV_NAME
);
6465 destroy_workqueue(rbd_wq
);
6471 static void __exit
rbd_exit(void)
6473 ida_destroy(&rbd_dev_id_ida
);
6474 rbd_sysfs_cleanup();
6476 unregister_blkdev(rbd_major
, RBD_DRV_NAME
);
6477 destroy_workqueue(rbd_wq
);
6481 module_init(rbd_init
);
6482 module_exit(rbd_exit
);
6484 MODULE_AUTHOR("Alex Elder <elder@inktank.com>");
6485 MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
6486 MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
6487 /* following authorship retained from original osdblk.c */
6488 MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
6490 MODULE_DESCRIPTION("RADOS Block Device (RBD) driver");
6491 MODULE_LICENSE("GPL");