Linux 2.6.34-rc3
[pohmelfs.git] / drivers / block / osdblk.c
blobeb2091aa1c19e2abf7ccfdafce320c44f7d14abb
2 /*
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.
22 Instructions for use
23 --------------------
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,
28 OSD device /dev/osd1.
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:
43 - blkdev unique id
44 - blkdev assigned major
45 - OSD object partition id
46 - OSD object id
47 - OSD device
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
58 of this driver.
62 #include <linux/kernel.h>
63 #include <linux/device.h>
64 #include <linux/module.h>
65 #include <linux/fs.h>
66 #include <scsi/osd_initiator.h>
67 #include <scsi/osd_attributes.h>
68 #include <scsi/osd_sec.h>
69 #include <scsi/scsi_device.h>
71 #define DRV_NAME "osdblk"
72 #define PFX DRV_NAME ": "
74 /* #define _OSDBLK_DEBUG */
75 #ifdef _OSDBLK_DEBUG
76 #define OSDBLK_DEBUG(fmt, a...) \
77 printk(KERN_NOTICE "osdblk @%s:%d: " fmt, __func__, __LINE__, ##a)
78 #else
79 #define OSDBLK_DEBUG(fmt, a...) \
80 do { if (0) printk(fmt, ##a); } while (0)
81 #endif
83 MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
84 MODULE_DESCRIPTION("block device inside an OSD object osdblk.ko");
85 MODULE_LICENSE("GPL");
87 struct osdblk_device;
89 enum {
90 OSDBLK_MINORS_PER_MAJOR = 256, /* max minors per blkdev */
91 OSDBLK_MAX_REQ = 32, /* max parallel requests */
92 OSDBLK_OP_TIMEOUT = 4 * 60, /* sync OSD req timeout */
95 struct osdblk_request {
96 struct request *rq; /* blk layer request */
97 struct bio *bio; /* cloned bio */
98 struct osdblk_device *osdev; /* associated blkdev */
101 struct osdblk_device {
102 int id; /* blkdev unique id */
104 int major; /* blkdev assigned major */
105 struct gendisk *disk; /* blkdev's gendisk and rq */
106 struct request_queue *q;
108 struct osd_dev *osd; /* associated OSD */
110 char name[32]; /* blkdev name, e.g. osdblk34 */
112 spinlock_t lock; /* queue lock */
114 struct osd_obj_id obj; /* OSD partition, obj id */
115 uint8_t obj_cred[OSD_CAP_LEN]; /* OSD cred */
117 struct osdblk_request req[OSDBLK_MAX_REQ]; /* request table */
119 struct list_head node;
121 char osd_path[0]; /* OSD device path */
124 static struct class *class_osdblk; /* /sys/class/osdblk */
125 static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
126 static LIST_HEAD(osdblkdev_list);
128 static const struct block_device_operations osdblk_bd_ops = {
129 .owner = THIS_MODULE,
132 static const struct osd_attr g_attr_logical_length = ATTR_DEF(
133 OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
135 static void osdblk_make_credential(u8 cred_a[OSD_CAP_LEN],
136 const struct osd_obj_id *obj)
138 osd_sec_init_nosec_doall_caps(cred_a, obj, false, true);
141 /* copied from exofs; move to libosd? */
143 * Perform a synchronous OSD operation. copied from exofs; move to libosd?
145 static int osd_sync_op(struct osd_request *or, int timeout, uint8_t *credential)
147 int ret;
149 or->timeout = timeout;
150 ret = osd_finalize_request(or, 0, credential, NULL);
151 if (ret)
152 return ret;
154 ret = osd_execute_request(or);
156 /* osd_req_decode_sense(or, ret); */
157 return ret;
161 * Perform an asynchronous OSD operation. copied from exofs; move to libosd?
163 static int osd_async_op(struct osd_request *or, osd_req_done_fn *async_done,
164 void *caller_context, u8 *cred)
166 int ret;
168 ret = osd_finalize_request(or, 0, cred, NULL);
169 if (ret)
170 return ret;
172 ret = osd_execute_request_async(or, async_done, caller_context);
174 return ret;
177 /* copied from exofs; move to libosd? */
178 static int extract_attr_from_req(struct osd_request *or, struct osd_attr *attr)
180 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
181 void *iter = NULL;
182 int nelem;
184 do {
185 nelem = 1;
186 osd_req_decode_get_attr_list(or, &cur_attr, &nelem, &iter);
187 if ((cur_attr.attr_page == attr->attr_page) &&
188 (cur_attr.attr_id == attr->attr_id)) {
189 attr->len = cur_attr.len;
190 attr->val_ptr = cur_attr.val_ptr;
191 return 0;
193 } while (iter);
195 return -EIO;
198 static int osdblk_get_obj_size(struct osdblk_device *osdev, u64 *size_out)
200 struct osd_request *or;
201 struct osd_attr attr;
202 int ret;
204 /* start request */
205 or = osd_start_request(osdev->osd, GFP_KERNEL);
206 if (!or)
207 return -ENOMEM;
209 /* create a get-attributes(length) request */
210 osd_req_get_attributes(or, &osdev->obj);
212 osd_req_add_get_attr_list(or, &g_attr_logical_length, 1);
214 /* execute op synchronously */
215 ret = osd_sync_op(or, OSDBLK_OP_TIMEOUT, osdev->obj_cred);
216 if (ret)
217 goto out;
219 /* extract length from returned attribute info */
220 attr = g_attr_logical_length;
221 ret = extract_attr_from_req(or, &attr);
222 if (ret)
223 goto out;
225 *size_out = get_unaligned_be64(attr.val_ptr);
227 out:
228 osd_end_request(or);
229 return ret;
233 static void osdblk_osd_complete(struct osd_request *or, void *private)
235 struct osdblk_request *orq = private;
236 struct osd_sense_info osi;
237 int ret = osd_req_decode_sense(or, &osi);
239 if (ret) {
240 ret = -EIO;
241 OSDBLK_DEBUG("osdblk_osd_complete with err=%d\n", ret);
244 /* complete OSD request */
245 osd_end_request(or);
247 /* complete request passed to osdblk by block layer */
248 __blk_end_request_all(orq->rq, ret);
251 static void bio_chain_put(struct bio *chain)
253 struct bio *tmp;
255 while (chain) {
256 tmp = chain;
257 chain = chain->bi_next;
259 bio_put(tmp);
263 static struct bio *bio_chain_clone(struct bio *old_chain, gfp_t gfpmask)
265 struct bio *tmp, *new_chain = NULL, *tail = NULL;
267 while (old_chain) {
268 tmp = bio_kmalloc(gfpmask, old_chain->bi_max_vecs);
269 if (!tmp)
270 goto err_out;
272 __bio_clone(tmp, old_chain);
273 tmp->bi_bdev = NULL;
274 gfpmask &= ~__GFP_WAIT;
275 tmp->bi_next = NULL;
277 if (!new_chain)
278 new_chain = tail = tmp;
279 else {
280 tail->bi_next = tmp;
281 tail = tmp;
284 old_chain = old_chain->bi_next;
287 return new_chain;
289 err_out:
290 OSDBLK_DEBUG("bio_chain_clone with err\n");
291 bio_chain_put(new_chain);
292 return NULL;
295 static void osdblk_rq_fn(struct request_queue *q)
297 struct osdblk_device *osdev = q->queuedata;
299 while (1) {
300 struct request *rq;
301 struct osdblk_request *orq;
302 struct osd_request *or;
303 struct bio *bio;
304 bool do_write, do_flush;
306 /* peek at request from block layer */
307 rq = blk_fetch_request(q);
308 if (!rq)
309 break;
311 /* filter out block requests we don't understand */
312 if (!blk_fs_request(rq) && !blk_barrier_rq(rq)) {
313 blk_end_request_all(rq, 0);
314 continue;
317 /* deduce our operation (read, write, flush) */
318 /* I wish the block layer simplified cmd_type/cmd_flags/cmd[]
319 * into a clearly defined set of RPC commands:
320 * read, write, flush, scsi command, power mgmt req,
321 * driver-specific, etc.
324 do_flush = (rq->special == (void *) 0xdeadbeefUL);
325 do_write = (rq_data_dir(rq) == WRITE);
327 if (!do_flush) { /* osd_flush does not use a bio */
328 /* a bio clone to be passed down to OSD request */
329 bio = bio_chain_clone(rq->bio, GFP_ATOMIC);
330 if (!bio)
331 break;
332 } else
333 bio = NULL;
335 /* alloc internal OSD request, for OSD command execution */
336 or = osd_start_request(osdev->osd, GFP_ATOMIC);
337 if (!or) {
338 bio_chain_put(bio);
339 OSDBLK_DEBUG("osd_start_request with err\n");
340 break;
343 orq = &osdev->req[rq->tag];
344 orq->rq = rq;
345 orq->bio = bio;
346 orq->osdev = osdev;
348 /* init OSD command: flush, write or read */
349 if (do_flush)
350 osd_req_flush_object(or, &osdev->obj,
351 OSD_CDB_FLUSH_ALL, 0, 0);
352 else if (do_write)
353 osd_req_write(or, &osdev->obj, blk_rq_pos(rq) * 512ULL,
354 bio, blk_rq_bytes(rq));
355 else
356 osd_req_read(or, &osdev->obj, blk_rq_pos(rq) * 512ULL,
357 bio, blk_rq_bytes(rq));
359 OSDBLK_DEBUG("%s 0x%x bytes at 0x%llx\n",
360 do_flush ? "flush" : do_write ?
361 "write" : "read", blk_rq_bytes(rq),
362 blk_rq_pos(rq) * 512ULL);
364 /* begin OSD command execution */
365 if (osd_async_op(or, osdblk_osd_complete, orq,
366 osdev->obj_cred)) {
367 osd_end_request(or);
368 blk_requeue_request(q, rq);
369 bio_chain_put(bio);
370 OSDBLK_DEBUG("osd_execute_request_async with err\n");
371 break;
374 /* remove the special 'flush' marker, now that the command
375 * is executing
377 rq->special = NULL;
381 static void osdblk_prepare_flush(struct request_queue *q, struct request *rq)
383 /* add driver-specific marker, to indicate that this request
384 * is a flush command
386 rq->special = (void *) 0xdeadbeefUL;
389 static void osdblk_free_disk(struct osdblk_device *osdev)
391 struct gendisk *disk = osdev->disk;
393 if (!disk)
394 return;
396 if (disk->flags & GENHD_FL_UP)
397 del_gendisk(disk);
398 if (disk->queue)
399 blk_cleanup_queue(disk->queue);
400 put_disk(disk);
403 static int osdblk_init_disk(struct osdblk_device *osdev)
405 struct gendisk *disk;
406 struct request_queue *q;
407 int rc;
408 u64 obj_size = 0;
410 /* contact OSD, request size info about the object being mapped */
411 rc = osdblk_get_obj_size(osdev, &obj_size);
412 if (rc)
413 return rc;
415 /* create gendisk info */
416 disk = alloc_disk(OSDBLK_MINORS_PER_MAJOR);
417 if (!disk)
418 return -ENOMEM;
420 sprintf(disk->disk_name, DRV_NAME "%d", osdev->id);
421 disk->major = osdev->major;
422 disk->first_minor = 0;
423 disk->fops = &osdblk_bd_ops;
424 disk->private_data = osdev;
426 /* init rq */
427 q = blk_init_queue(osdblk_rq_fn, &osdev->lock);
428 if (!q) {
429 put_disk(disk);
430 return -ENOMEM;
433 /* switch queue to TCQ mode; allocate tag map */
434 rc = blk_queue_init_tags(q, OSDBLK_MAX_REQ, NULL);
435 if (rc) {
436 blk_cleanup_queue(q);
437 put_disk(disk);
438 return rc;
441 /* Set our limits to the lower device limits, because osdblk cannot
442 * sleep when allocating a lower-request and therefore cannot be
443 * bouncing.
445 blk_queue_stack_limits(q, osd_request_queue(osdev->osd));
447 blk_queue_prep_rq(q, blk_queue_start_tag);
448 blk_queue_ordered(q, QUEUE_ORDERED_DRAIN_FLUSH, osdblk_prepare_flush);
450 disk->queue = q;
452 q->queuedata = osdev;
454 osdev->disk = disk;
455 osdev->q = q;
457 /* finally, announce the disk to the world */
458 set_capacity(disk, obj_size / 512ULL);
459 add_disk(disk);
461 printk(KERN_INFO "%s: Added of size 0x%llx\n",
462 disk->disk_name, (unsigned long long)obj_size);
464 return 0;
467 /********************************************************************
468 * /sys/class/osdblk/
469 * add map OSD object to blkdev
470 * remove unmap OSD object
471 * list show mappings
472 *******************************************************************/
474 static void class_osdblk_release(struct class *cls)
476 kfree(cls);
479 static ssize_t class_osdblk_list(struct class *c,
480 struct class_attribute *attr,
481 char *data)
483 int n = 0;
484 struct list_head *tmp;
486 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
488 list_for_each(tmp, &osdblkdev_list) {
489 struct osdblk_device *osdev;
491 osdev = list_entry(tmp, struct osdblk_device, node);
493 n += sprintf(data+n, "%d %d %llu %llu %s\n",
494 osdev->id,
495 osdev->major,
496 osdev->obj.partition,
497 osdev->obj.id,
498 osdev->osd_path);
501 mutex_unlock(&ctl_mutex);
502 return n;
505 static ssize_t class_osdblk_add(struct class *c,
506 struct class_attribute *attr,
507 const char *buf, size_t count)
509 struct osdblk_device *osdev;
510 ssize_t rc;
511 int irc, new_id = 0;
512 struct list_head *tmp;
514 if (!try_module_get(THIS_MODULE))
515 return -ENODEV;
517 /* new osdblk_device object */
518 osdev = kzalloc(sizeof(*osdev) + strlen(buf) + 1, GFP_KERNEL);
519 if (!osdev) {
520 rc = -ENOMEM;
521 goto err_out_mod;
524 /* static osdblk_device initialization */
525 spin_lock_init(&osdev->lock);
526 INIT_LIST_HEAD(&osdev->node);
528 /* generate unique id: find highest unique id, add one */
530 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
532 list_for_each(tmp, &osdblkdev_list) {
533 struct osdblk_device *osdev;
535 osdev = list_entry(tmp, struct osdblk_device, node);
536 if (osdev->id > new_id)
537 new_id = osdev->id + 1;
540 osdev->id = new_id;
542 /* add to global list */
543 list_add_tail(&osdev->node, &osdblkdev_list);
545 mutex_unlock(&ctl_mutex);
547 /* parse add command */
548 if (sscanf(buf, "%llu %llu %s", &osdev->obj.partition, &osdev->obj.id,
549 osdev->osd_path) != 3) {
550 rc = -EINVAL;
551 goto err_out_slot;
554 /* initialize rest of new object */
555 sprintf(osdev->name, DRV_NAME "%d", osdev->id);
557 /* contact requested OSD */
558 osdev->osd = osduld_path_lookup(osdev->osd_path);
559 if (IS_ERR(osdev->osd)) {
560 rc = PTR_ERR(osdev->osd);
561 goto err_out_slot;
564 /* build OSD credential */
565 osdblk_make_credential(osdev->obj_cred, &osdev->obj);
567 /* register our block device */
568 irc = register_blkdev(0, osdev->name);
569 if (irc < 0) {
570 rc = irc;
571 goto err_out_osd;
574 osdev->major = irc;
576 /* set up and announce blkdev mapping */
577 rc = osdblk_init_disk(osdev);
578 if (rc)
579 goto err_out_blkdev;
581 return count;
583 err_out_blkdev:
584 unregister_blkdev(osdev->major, osdev->name);
585 err_out_osd:
586 osduld_put_device(osdev->osd);
587 err_out_slot:
588 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
589 list_del_init(&osdev->node);
590 mutex_unlock(&ctl_mutex);
592 kfree(osdev);
593 err_out_mod:
594 OSDBLK_DEBUG("Error adding device %s\n", buf);
595 module_put(THIS_MODULE);
596 return rc;
599 static ssize_t class_osdblk_remove(struct class *c,
600 struct class_attribute *attr,
601 const char *buf,
602 size_t count)
604 struct osdblk_device *osdev = NULL;
605 int target_id, rc;
606 unsigned long ul;
607 struct list_head *tmp;
609 rc = strict_strtoul(buf, 10, &ul);
610 if (rc)
611 return rc;
613 /* convert to int; abort if we lost anything in the conversion */
614 target_id = (int) ul;
615 if (target_id != ul)
616 return -EINVAL;
618 /* remove object from list immediately */
619 mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
621 list_for_each(tmp, &osdblkdev_list) {
622 osdev = list_entry(tmp, struct osdblk_device, node);
623 if (osdev->id == target_id) {
624 list_del_init(&osdev->node);
625 break;
627 osdev = NULL;
630 mutex_unlock(&ctl_mutex);
632 if (!osdev)
633 return -ENOENT;
635 /* clean up and free blkdev and associated OSD connection */
636 osdblk_free_disk(osdev);
637 unregister_blkdev(osdev->major, osdev->name);
638 osduld_put_device(osdev->osd);
639 kfree(osdev);
641 /* release module ref */
642 module_put(THIS_MODULE);
644 return count;
647 static struct class_attribute class_osdblk_attrs[] = {
648 __ATTR(add, 0200, NULL, class_osdblk_add),
649 __ATTR(remove, 0200, NULL, class_osdblk_remove),
650 __ATTR(list, 0444, class_osdblk_list, NULL),
651 __ATTR_NULL
654 static int osdblk_sysfs_init(void)
656 int ret = 0;
659 * create control files in sysfs
660 * /sys/class/osdblk/...
662 class_osdblk = kzalloc(sizeof(*class_osdblk), GFP_KERNEL);
663 if (!class_osdblk)
664 return -ENOMEM;
666 class_osdblk->name = DRV_NAME;
667 class_osdblk->owner = THIS_MODULE;
668 class_osdblk->class_release = class_osdblk_release;
669 class_osdblk->class_attrs = class_osdblk_attrs;
671 ret = class_register(class_osdblk);
672 if (ret) {
673 kfree(class_osdblk);
674 class_osdblk = NULL;
675 printk(PFX "failed to create class osdblk\n");
676 return ret;
679 return 0;
682 static void osdblk_sysfs_cleanup(void)
684 if (class_osdblk)
685 class_destroy(class_osdblk);
686 class_osdblk = NULL;
689 static int __init osdblk_init(void)
691 int rc;
693 rc = osdblk_sysfs_init();
694 if (rc)
695 return rc;
697 return 0;
700 static void __exit osdblk_exit(void)
702 osdblk_sysfs_cleanup();
705 module_init(osdblk_init);
706 module_exit(osdblk_exit);