11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com> 31da177e4SLinus Torvalds * Horst Hummel <Horst.Hummel@de.ibm.com> 41da177e4SLinus Torvalds * Carsten Otte <Cotte@de.ibm.com> 51da177e4SLinus Torvalds * Martin Schwidefsky <schwidefsky@de.ibm.com> 61da177e4SLinus Torvalds * Bugreports.to..: <Linux390@de.ibm.com> 7*a53c8fabSHeiko Carstens * Copyright IBM Corp. 1999, 2001 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * gendisk related functions for the dasd driver. 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 13fc19f381SStefan Haberland #define KMSG_COMPONENT "dasd" 14fc19f381SStefan Haberland 151da177e4SLinus Torvalds #include <linux/interrupt.h> 161da177e4SLinus Torvalds #include <linux/fs.h> 171da177e4SLinus Torvalds #include <linux/blkpg.h> 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds #include <asm/uaccess.h> 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds /* This is ugly... */ 221da177e4SLinus Torvalds #define PRINTK_HEADER "dasd_gendisk:" 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds #include "dasd_int.h" 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds /* 271da177e4SLinus Torvalds * Allocate and register gendisk structure for device. 281da177e4SLinus Torvalds */ 298e09f215SStefan Weinhuber int dasd_gendisk_alloc(struct dasd_block *block) 301da177e4SLinus Torvalds { 311da177e4SLinus Torvalds struct gendisk *gdp; 328e09f215SStefan Weinhuber struct dasd_device *base; 33c6eb7b77SHorst Hummel int len; 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds /* Make sure the minor for this device exists. */ 368e09f215SStefan Weinhuber base = block->base; 378e09f215SStefan Weinhuber if (base->devindex >= DASD_PER_MAJOR) 381da177e4SLinus Torvalds return -EBUSY; 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds gdp = alloc_disk(1 << DASD_PARTN_BITS); 411da177e4SLinus Torvalds if (!gdp) 421da177e4SLinus Torvalds return -ENOMEM; 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds /* Initialize gendisk structure. */ 451da177e4SLinus Torvalds gdp->major = DASD_MAJOR; 468e09f215SStefan Weinhuber gdp->first_minor = base->devindex << DASD_PARTN_BITS; 471da177e4SLinus Torvalds gdp->fops = &dasd_device_operations; 488e09f215SStefan Weinhuber gdp->driverfs_dev = &base->cdev->dev; 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds /* 511da177e4SLinus Torvalds * Set device name. 521da177e4SLinus Torvalds * dasda - dasdz : 26 devices 531da177e4SLinus Torvalds * dasdaa - dasdzz : 676 devices, added up = 702 541da177e4SLinus Torvalds * dasdaaa - dasdzzz : 17576 devices, added up = 18278 551da177e4SLinus Torvalds * dasdaaaa - dasdzzzz : 456976 devices, added up = 475252 561da177e4SLinus Torvalds */ 571da177e4SLinus Torvalds len = sprintf(gdp->disk_name, "dasd"); 588e09f215SStefan Weinhuber if (base->devindex > 25) { 598e09f215SStefan Weinhuber if (base->devindex > 701) { 608e09f215SStefan Weinhuber if (base->devindex > 18277) 611da177e4SLinus Torvalds len += sprintf(gdp->disk_name + len, "%c", 628e09f215SStefan Weinhuber 'a'+(((base->devindex-18278) 631da177e4SLinus Torvalds /17576)%26)); 641da177e4SLinus Torvalds len += sprintf(gdp->disk_name + len, "%c", 658e09f215SStefan Weinhuber 'a'+(((base->devindex-702)/676)%26)); 661da177e4SLinus Torvalds } 671da177e4SLinus Torvalds len += sprintf(gdp->disk_name + len, "%c", 688e09f215SStefan Weinhuber 'a'+(((base->devindex-26)/26)%26)); 691da177e4SLinus Torvalds } 708e09f215SStefan Weinhuber len += sprintf(gdp->disk_name + len, "%c", 'a'+(base->devindex%26)); 711da177e4SLinus Torvalds 7233b62a30SStefan Weinhuber if (base->features & DASD_FEATURE_READONLY || 7333b62a30SStefan Weinhuber test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) 741da177e4SLinus Torvalds set_disk_ro(gdp, 1); 7565f8da47SStefan Weinhuber dasd_add_link_to_gendisk(gdp, base); 768e09f215SStefan Weinhuber gdp->queue = block->request_queue; 778e09f215SStefan Weinhuber block->gdp = gdp; 788e09f215SStefan Weinhuber set_capacity(block->gdp, 0); 798e09f215SStefan Weinhuber add_disk(block->gdp); 801da177e4SLinus Torvalds return 0; 811da177e4SLinus Torvalds } 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds /* 841da177e4SLinus Torvalds * Unregister and free gendisk structure for device. 851da177e4SLinus Torvalds */ 868e09f215SStefan Weinhuber void dasd_gendisk_free(struct dasd_block *block) 871da177e4SLinus Torvalds { 888e09f215SStefan Weinhuber if (block->gdp) { 898e09f215SStefan Weinhuber del_gendisk(block->gdp); 908e09f215SStefan Weinhuber block->gdp->queue = NULL; 919eb25122SStefan Haberland block->gdp->private_data = NULL; 928e09f215SStefan Weinhuber put_disk(block->gdp); 938e09f215SStefan Weinhuber block->gdp = NULL; 941da177e4SLinus Torvalds } 958f61701bSHorst Hummel } 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /* 981da177e4SLinus Torvalds * Trigger a partition detection. 991da177e4SLinus Torvalds */ 1008e09f215SStefan Weinhuber int dasd_scan_partitions(struct dasd_block *block) 1011da177e4SLinus Torvalds { 1021da177e4SLinus Torvalds struct block_device *bdev; 1031da177e4SLinus Torvalds 1048e09f215SStefan Weinhuber bdev = bdget_disk(block->gdp, 0); 105e525fd89STejun Heo if (!bdev || blkdev_get(bdev, FMODE_READ, NULL) < 0) 1061da177e4SLinus Torvalds return -ENODEV; 1071da177e4SLinus Torvalds /* 1081da177e4SLinus Torvalds * See fs/partition/check.c:register_disk,rescan_partitions 1091da177e4SLinus Torvalds * Can't call rescan_partitions directly. Use ioctl. 1101da177e4SLinus Torvalds */ 1111da177e4SLinus Torvalds ioctl_by_bdev(bdev, BLKRRPART, 0); 1121da177e4SLinus Torvalds /* 1131da177e4SLinus Torvalds * Since the matching blkdev_put call to the blkdev_get in 1141da177e4SLinus Torvalds * this function is not called before dasd_destroy_partitions 1151da177e4SLinus Torvalds * the offline open_count limit needs to be increased from 1161da177e4SLinus Torvalds * 0 to 1. This is done by setting device->bdev (see 1171da177e4SLinus Torvalds * dasd_generic_set_offline). As long as the partition 1181da177e4SLinus Torvalds * detection is running no offline should be allowed. That 1191da177e4SLinus Torvalds * is why the assignment to device->bdev is done AFTER 1201da177e4SLinus Torvalds * the BLKRRPART ioctl. 1211da177e4SLinus Torvalds */ 1228e09f215SStefan Weinhuber block->bdev = bdev; 1231da177e4SLinus Torvalds return 0; 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds /* 1271da177e4SLinus Torvalds * Remove all inodes in the system for a device, delete the 1281da177e4SLinus Torvalds * partitions and make device unusable by setting its size to zero. 1291da177e4SLinus Torvalds */ 1308e09f215SStefan Weinhuber void dasd_destroy_partitions(struct dasd_block *block) 1311da177e4SLinus Torvalds { 1321da177e4SLinus Torvalds /* The two structs have 168/176 byte on 31/64 bit. */ 1331da177e4SLinus Torvalds struct blkpg_partition bpart; 1341da177e4SLinus Torvalds struct blkpg_ioctl_arg barg; 1351da177e4SLinus Torvalds struct block_device *bdev; 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds /* 1381da177e4SLinus Torvalds * Get the bdev pointer from the device structure and clear 1391da177e4SLinus Torvalds * device->bdev to lower the offline open_count limit again. 1401da177e4SLinus Torvalds */ 1418e09f215SStefan Weinhuber bdev = block->bdev; 1428e09f215SStefan Weinhuber block->bdev = NULL; 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds /* 1451da177e4SLinus Torvalds * See fs/partition/check.c:delete_partition 1461da177e4SLinus Torvalds * Can't call delete_partitions directly. Use ioctl. 1471da177e4SLinus Torvalds * The ioctl also does locking and invalidation. 1481da177e4SLinus Torvalds */ 1491da177e4SLinus Torvalds memset(&bpart, 0, sizeof(struct blkpg_partition)); 1501da177e4SLinus Torvalds memset(&barg, 0, sizeof(struct blkpg_ioctl_arg)); 1512b67fc46SHeiko Carstens barg.data = (void __force __user *) &bpart; 1521da177e4SLinus Torvalds barg.op = BLKPG_DEL_PARTITION; 1538e09f215SStefan Weinhuber for (bpart.pno = block->gdp->minors - 1; bpart.pno > 0; bpart.pno--) 1541da177e4SLinus Torvalds ioctl_by_bdev(bdev, BLKPG, (unsigned long) &barg); 1551da177e4SLinus Torvalds 1568e09f215SStefan Weinhuber invalidate_partition(block->gdp, 0); 1571da177e4SLinus Torvalds /* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */ 1589a1c3542SAl Viro blkdev_put(bdev, FMODE_READ); 1598e09f215SStefan Weinhuber set_capacity(block->gdp, 0); 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 1628e09f215SStefan Weinhuber int dasd_gendisk_init(void) 1631da177e4SLinus Torvalds { 1641da177e4SLinus Torvalds int rc; 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds /* Register to static dasd major 94 */ 1671da177e4SLinus Torvalds rc = register_blkdev(DASD_MAJOR, "dasd"); 1681da177e4SLinus Torvalds if (rc != 0) { 169fc19f381SStefan Haberland pr_warning("Registering the device driver with major number " 170fc19f381SStefan Haberland "%d failed\n", DASD_MAJOR); 1711da177e4SLinus Torvalds return rc; 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds return 0; 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds 1768e09f215SStefan Weinhuber void dasd_gendisk_exit(void) 1771da177e4SLinus Torvalds { 1781da177e4SLinus Torvalds unregister_blkdev(DASD_MAJOR, "dasd"); 1791da177e4SLinus Torvalds } 180