3 osdblk.c -- Export a single SCSI OSD object as a Linux block device
6 Copyright 2009 Red Hat, Inc.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 1) Map a Linux block device to an existing OSD object.
27 In this example, we will use partition id 1234, object id 5678,
30 $ echo "1234 5678 /dev/osd1" > /sys/class/osdblk/add
33 2) List all active blkdev<->object mappings.
35 In this example, we have performed step #1 twice, creating two blkdevs,
36 mapped to two separate OSD objects.
38 $ cat /sys/class/osdblk/list
39 0 174 1234 5678 /dev/osd1
40 1 179 1994 897123 /dev/osd0
42 The columns, in order, are:
44 - blkdev assigned major
45 - OSD object partition id
50 3) Remove an active blkdev<->object mapping.
52 In this example, we remove the mapping with blkdev unique id 1.
54 $ echo 1 > /sys/class/osdblk/remove
57 NOTE: The actual creation and deletion of OSD objects is outside the scope
62 #include <linux/kernel.h>
63 #include <linux/device.h>
64 #include <linux/module.h>
66 #include <linux/slab.h>
67 #include <scsi/osd_initiator.h>
68 #include <scsi/osd_attributes.h>
69 #include <scsi/osd_sec.h>
70 #include <scsi/scsi_device.h>
72 #define DRV_NAME "osdblk"
73 #define PFX DRV_NAME ": "
75 /* #define _OSDBLK_DEBUG */
77 #define OSDBLK_DEBUG(fmt, a...) \
78 printk(KERN_NOTICE "osdblk @%s:%d: " fmt, __func__, __LINE__, ##a)
80 #define OSDBLK_DEBUG(fmt, a...) \
81 do { if (0) printk(fmt, ##a); } while (0)
84 MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
85 MODULE_DESCRIPTION("block device inside an OSD object osdblk.ko");
86 MODULE_LICENSE("GPL");
91 OSDBLK_MINORS_PER_MAJOR
= 256, /* max minors per blkdev */
92 OSDBLK_MAX_REQ
= 32, /* max parallel requests */
93 OSDBLK_OP_TIMEOUT
= 4 * 60, /* sync OSD req timeout */
96 struct osdblk_request
{
97 struct request
*rq
; /* blk layer request */
98 struct bio
*bio
; /* cloned bio */
99 struct osdblk_device
*osdev
; /* associated blkdev */
102 struct osdblk_device
{
103 int id
; /* blkdev unique id */
105 int major
; /* blkdev assigned major */
106 struct gendisk
*disk
; /* blkdev's gendisk and rq */
107 struct request_queue
*q
;
109 struct osd_dev
*osd
; /* associated OSD */
111 char name
[32]; /* blkdev name, e.g. osdblk34 */
113 spinlock_t lock
; /* queue lock */
115 struct osd_obj_id obj
; /* OSD partition, obj id */
116 uint8_t obj_cred
[OSD_CAP_LEN
]; /* OSD cred */
118 struct osdblk_request req
[OSDBLK_MAX_REQ
]; /* request table */
120 struct list_head node
;
122 char osd_path
[0]; /* OSD device path */
125 static struct class *class_osdblk
; /* /sys/class/osdblk */
126 static DEFINE_MUTEX(ctl_mutex
); /* Serialize open/close/setup/teardown */
127 static LIST_HEAD(osdblkdev_list
);
129 static const struct block_device_operations osdblk_bd_ops
= {
130 .owner
= THIS_MODULE
,
133 static const struct osd_attr g_attr_logical_length
= ATTR_DEF(
134 OSD_APAGE_OBJECT_INFORMATION
, OSD_ATTR_OI_LOGICAL_LENGTH
, 8);
136 static void osdblk_make_credential(u8 cred_a
[OSD_CAP_LEN
],
137 const struct osd_obj_id
*obj
)
139 osd_sec_init_nosec_doall_caps(cred_a
, obj
, false, true);
142 /* copied from exofs; move to libosd? */
144 * Perform a synchronous OSD operation. copied from exofs; move to libosd?
146 static int osd_sync_op(struct osd_request
*or, int timeout
, uint8_t *credential
)
150 or->timeout
= timeout
;
151 ret
= osd_finalize_request(or, 0, credential
, NULL
);
155 ret
= osd_execute_request(or);
157 /* osd_req_decode_sense(or, ret); */
162 * Perform an asynchronous OSD operation. copied from exofs; move to libosd?
164 static int osd_async_op(struct osd_request
*or, osd_req_done_fn
*async_done
,
165 void *caller_context
, u8
*cred
)
169 ret
= osd_finalize_request(or, 0, cred
, NULL
);
173 ret
= osd_execute_request_async(or, async_done
, caller_context
);
178 /* copied from exofs; move to libosd? */
179 static int extract_attr_from_req(struct osd_request
*or, struct osd_attr
*attr
)
181 struct osd_attr cur_attr
= {.attr_page
= 0}; /* start with zeros */
187 osd_req_decode_get_attr_list(or, &cur_attr
, &nelem
, &iter
);
188 if ((cur_attr
.attr_page
== attr
->attr_page
) &&
189 (cur_attr
.attr_id
== attr
->attr_id
)) {
190 attr
->len
= cur_attr
.len
;
191 attr
->val_ptr
= cur_attr
.val_ptr
;
199 static int osdblk_get_obj_size(struct osdblk_device
*osdev
, u64
*size_out
)
201 struct osd_request
*or;
202 struct osd_attr attr
;
206 or = osd_start_request(osdev
->osd
, GFP_KERNEL
);
210 /* create a get-attributes(length) request */
211 osd_req_get_attributes(or, &osdev
->obj
);
213 osd_req_add_get_attr_list(or, &g_attr_logical_length
, 1);
215 /* execute op synchronously */
216 ret
= osd_sync_op(or, OSDBLK_OP_TIMEOUT
, osdev
->obj_cred
);
220 /* extract length from returned attribute info */
221 attr
= g_attr_logical_length
;
222 ret
= extract_attr_from_req(or, &attr
);
226 *size_out
= get_unaligned_be64(attr
.val_ptr
);
234 static void osdblk_osd_complete(struct osd_request
*or, void *private)
236 struct osdblk_request
*orq
= private;
237 struct osd_sense_info osi
;
238 int ret
= osd_req_decode_sense(or, &osi
);
242 OSDBLK_DEBUG("osdblk_osd_complete with err=%d\n", ret
);
245 /* complete OSD request */
248 /* complete request passed to osdblk by block layer */
249 __blk_end_request_all(orq
->rq
, ret
);
252 static void bio_chain_put(struct bio
*chain
)
258 chain
= chain
->bi_next
;
264 static struct bio
*bio_chain_clone(struct bio
*old_chain
, gfp_t gfpmask
)
266 struct bio
*tmp
, *new_chain
= NULL
, *tail
= NULL
;
269 tmp
= bio_clone_kmalloc(old_chain
, gfpmask
);
274 gfpmask
&= ~__GFP_DIRECT_RECLAIM
;
278 new_chain
= tail
= tmp
;
284 old_chain
= old_chain
->bi_next
;
290 OSDBLK_DEBUG("bio_chain_clone with err\n");
291 bio_chain_put(new_chain
);
295 static void osdblk_rq_fn(struct request_queue
*q
)
297 struct osdblk_device
*osdev
= q
->queuedata
;
301 struct osdblk_request
*orq
;
302 struct osd_request
*or;
304 bool do_write
, do_flush
;
306 /* peek at request from block layer */
307 rq
= blk_fetch_request(q
);
311 /* deduce our operation (read, write, flush) */
312 /* I wish the block layer simplified cmd_type/cmd_flags/cmd[]
313 * into a clearly defined set of RPC commands:
314 * read, write, flush, scsi command, power mgmt req,
315 * driver-specific, etc.
318 do_flush
= (req_op(rq
) == REQ_OP_FLUSH
);
319 do_write
= (rq_data_dir(rq
) == WRITE
);
321 if (!do_flush
) { /* osd_flush does not use a bio */
322 /* a bio clone to be passed down to OSD request */
323 bio
= bio_chain_clone(rq
->bio
, GFP_ATOMIC
);
329 /* alloc internal OSD request, for OSD command execution */
330 or = osd_start_request(osdev
->osd
, GFP_ATOMIC
);
333 OSDBLK_DEBUG("osd_start_request with err\n");
337 orq
= &osdev
->req
[rq
->tag
];
342 /* init OSD command: flush, write or read */
344 osd_req_flush_object(or, &osdev
->obj
,
345 OSD_CDB_FLUSH_ALL
, 0, 0);
347 osd_req_write(or, &osdev
->obj
, blk_rq_pos(rq
) * 512ULL,
348 bio
, blk_rq_bytes(rq
));
350 osd_req_read(or, &osdev
->obj
, blk_rq_pos(rq
) * 512ULL,
351 bio
, blk_rq_bytes(rq
));
353 OSDBLK_DEBUG("%s 0x%x bytes at 0x%llx\n",
354 do_flush
? "flush" : do_write
?
355 "write" : "read", blk_rq_bytes(rq
),
356 blk_rq_pos(rq
) * 512ULL);
358 /* begin OSD command execution */
359 if (osd_async_op(or, osdblk_osd_complete
, orq
,
362 blk_requeue_request(q
, rq
);
364 OSDBLK_DEBUG("osd_execute_request_async with err\n");
368 /* remove the special 'flush' marker, now that the command
375 static void osdblk_free_disk(struct osdblk_device
*osdev
)
377 struct gendisk
*disk
= osdev
->disk
;
382 if (disk
->flags
& GENHD_FL_UP
)
385 blk_cleanup_queue(disk
->queue
);
389 static int osdblk_init_disk(struct osdblk_device
*osdev
)
391 struct gendisk
*disk
;
392 struct request_queue
*q
;
396 /* contact OSD, request size info about the object being mapped */
397 rc
= osdblk_get_obj_size(osdev
, &obj_size
);
401 /* create gendisk info */
402 disk
= alloc_disk(OSDBLK_MINORS_PER_MAJOR
);
406 sprintf(disk
->disk_name
, DRV_NAME
"%d", osdev
->id
);
407 disk
->major
= osdev
->major
;
408 disk
->first_minor
= 0;
409 disk
->fops
= &osdblk_bd_ops
;
410 disk
->private_data
= osdev
;
413 q
= blk_init_queue(osdblk_rq_fn
, &osdev
->lock
);
419 /* switch queue to TCQ mode; allocate tag map */
420 rc
= blk_queue_init_tags(q
, OSDBLK_MAX_REQ
, NULL
, BLK_TAG_ALLOC_FIFO
);
422 blk_cleanup_queue(q
);
427 /* Set our limits to the lower device limits, because osdblk cannot
428 * sleep when allocating a lower-request and therefore cannot be
431 blk_queue_stack_limits(q
, osd_request_queue(osdev
->osd
));
433 blk_queue_prep_rq(q
, blk_queue_start_tag
);
434 blk_queue_write_cache(q
, true, false);
438 q
->queuedata
= osdev
;
443 /* finally, announce the disk to the world */
444 set_capacity(disk
, obj_size
/ 512ULL);
447 printk(KERN_INFO
"%s: Added of size 0x%llx\n",
448 disk
->disk_name
, (unsigned long long)obj_size
);
453 /********************************************************************
455 * add map OSD object to blkdev
456 * remove unmap OSD object
458 *******************************************************************/
460 static void class_osdblk_release(struct class *cls
)
465 static ssize_t
class_osdblk_list(struct class *c
,
466 struct class_attribute
*attr
,
470 struct list_head
*tmp
;
472 mutex_lock_nested(&ctl_mutex
, SINGLE_DEPTH_NESTING
);
474 list_for_each(tmp
, &osdblkdev_list
) {
475 struct osdblk_device
*osdev
;
477 osdev
= list_entry(tmp
, struct osdblk_device
, node
);
479 n
+= sprintf(data
+n
, "%d %d %llu %llu %s\n",
482 osdev
->obj
.partition
,
487 mutex_unlock(&ctl_mutex
);
491 static ssize_t
class_osdblk_add(struct class *c
,
492 struct class_attribute
*attr
,
493 const char *buf
, size_t count
)
495 struct osdblk_device
*osdev
;
498 struct list_head
*tmp
;
500 if (!try_module_get(THIS_MODULE
))
503 /* new osdblk_device object */
504 osdev
= kzalloc(sizeof(*osdev
) + strlen(buf
) + 1, GFP_KERNEL
);
510 /* static osdblk_device initialization */
511 spin_lock_init(&osdev
->lock
);
512 INIT_LIST_HEAD(&osdev
->node
);
514 /* generate unique id: find highest unique id, add one */
516 mutex_lock_nested(&ctl_mutex
, SINGLE_DEPTH_NESTING
);
518 list_for_each(tmp
, &osdblkdev_list
) {
519 struct osdblk_device
*osdev
;
521 osdev
= list_entry(tmp
, struct osdblk_device
, node
);
522 if (osdev
->id
> new_id
)
523 new_id
= osdev
->id
+ 1;
528 /* add to global list */
529 list_add_tail(&osdev
->node
, &osdblkdev_list
);
531 mutex_unlock(&ctl_mutex
);
533 /* parse add command */
534 if (sscanf(buf
, "%llu %llu %s", &osdev
->obj
.partition
, &osdev
->obj
.id
,
535 osdev
->osd_path
) != 3) {
540 /* initialize rest of new object */
541 sprintf(osdev
->name
, DRV_NAME
"%d", osdev
->id
);
543 /* contact requested OSD */
544 osdev
->osd
= osduld_path_lookup(osdev
->osd_path
);
545 if (IS_ERR(osdev
->osd
)) {
546 rc
= PTR_ERR(osdev
->osd
);
550 /* build OSD credential */
551 osdblk_make_credential(osdev
->obj_cred
, &osdev
->obj
);
553 /* register our block device */
554 irc
= register_blkdev(0, osdev
->name
);
562 /* set up and announce blkdev mapping */
563 rc
= osdblk_init_disk(osdev
);
570 unregister_blkdev(osdev
->major
, osdev
->name
);
572 osduld_put_device(osdev
->osd
);
574 mutex_lock_nested(&ctl_mutex
, SINGLE_DEPTH_NESTING
);
575 list_del_init(&osdev
->node
);
576 mutex_unlock(&ctl_mutex
);
580 OSDBLK_DEBUG("Error adding device %s\n", buf
);
581 module_put(THIS_MODULE
);
585 static ssize_t
class_osdblk_remove(struct class *c
,
586 struct class_attribute
*attr
,
590 struct osdblk_device
*osdev
= NULL
;
593 struct list_head
*tmp
;
595 rc
= kstrtoul(buf
, 10, &ul
);
599 /* convert to int; abort if we lost anything in the conversion */
600 target_id
= (int) ul
;
604 /* remove object from list immediately */
605 mutex_lock_nested(&ctl_mutex
, SINGLE_DEPTH_NESTING
);
607 list_for_each(tmp
, &osdblkdev_list
) {
608 osdev
= list_entry(tmp
, struct osdblk_device
, node
);
609 if (osdev
->id
== target_id
) {
610 list_del_init(&osdev
->node
);
616 mutex_unlock(&ctl_mutex
);
621 /* clean up and free blkdev and associated OSD connection */
622 osdblk_free_disk(osdev
);
623 unregister_blkdev(osdev
->major
, osdev
->name
);
624 osduld_put_device(osdev
->osd
);
627 /* release module ref */
628 module_put(THIS_MODULE
);
633 static struct class_attribute class_osdblk_attrs
[] = {
634 __ATTR(add
, 0200, NULL
, class_osdblk_add
),
635 __ATTR(remove
, 0200, NULL
, class_osdblk_remove
),
636 __ATTR(list
, 0444, class_osdblk_list
, NULL
),
640 static int osdblk_sysfs_init(void)
645 * create control files in sysfs
646 * /sys/class/osdblk/...
648 class_osdblk
= kzalloc(sizeof(*class_osdblk
), GFP_KERNEL
);
652 class_osdblk
->name
= DRV_NAME
;
653 class_osdblk
->owner
= THIS_MODULE
;
654 class_osdblk
->class_release
= class_osdblk_release
;
655 class_osdblk
->class_attrs
= class_osdblk_attrs
;
657 ret
= class_register(class_osdblk
);
661 printk(PFX
"failed to create class osdblk\n");
668 static void osdblk_sysfs_cleanup(void)
671 class_destroy(class_osdblk
);
675 static int __init
osdblk_init(void)
679 rc
= osdblk_sysfs_init();
686 static void __exit
osdblk_exit(void)
688 osdblk_sysfs_cleanup();
691 module_init(osdblk_init
);
692 module_exit(osdblk_exit
);