1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com> 41da177e4SLinus Torvalds * Horst Hummel <Horst.Hummel@de.ibm.com> 51da177e4SLinus Torvalds * Carsten Otte <Cotte@de.ibm.com> 61da177e4SLinus Torvalds * Martin Schwidefsky <schwidefsky@de.ibm.com> 71da177e4SLinus Torvalds * Bugreports.to..: <Linux390@de.ibm.com> 8a53c8fabSHeiko Carstens * Copyright IBM Corp. 1999, 2001 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * gendisk related functions for the dasd driver. 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 14fc19f381SStefan Haberland #define KMSG_COMPONENT "dasd" 15fc19f381SStefan Haberland 161da177e4SLinus Torvalds #include <linux/interrupt.h> 171da177e4SLinus Torvalds #include <linux/fs.h> 181da177e4SLinus Torvalds #include <linux/blkpg.h> 191da177e4SLinus Torvalds 207c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds /* This is ugly... */ 231da177e4SLinus Torvalds #define PRINTK_HEADER "dasd_gendisk:" 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds #include "dasd_int.h" 261da177e4SLinus Torvalds 279c2b9dbaSChristoph Hellwig static struct lock_class_key dasd_bio_compl_lkclass; 289c2b9dbaSChristoph Hellwig 291da177e4SLinus Torvalds /* 301da177e4SLinus Torvalds * Allocate and register gendisk structure for device. 311da177e4SLinus Torvalds */ 328e09f215SStefan Weinhuber int dasd_gendisk_alloc(struct dasd_block *block) 331da177e4SLinus Torvalds { 341da177e4SLinus Torvalds struct gendisk *gdp; 358e09f215SStefan Weinhuber struct dasd_device *base; 36c6eb7b77SHorst Hummel int len; 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds /* Make sure the minor for this device exists. */ 398e09f215SStefan Weinhuber base = block->base; 408e09f215SStefan Weinhuber if (base->devindex >= DASD_PER_MAJOR) 411da177e4SLinus Torvalds return -EBUSY; 421da177e4SLinus Torvalds 43*a58bd768SChristoph Hellwig gdp = __alloc_disk_node(NUMA_NO_NODE, &dasd_bio_compl_lkclass); 441da177e4SLinus Torvalds if (!gdp) 451da177e4SLinus Torvalds return -ENOMEM; 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds /* Initialize gendisk structure. */ 481da177e4SLinus Torvalds gdp->major = DASD_MAJOR; 498e09f215SStefan Weinhuber gdp->first_minor = base->devindex << DASD_PARTN_BITS; 50*a58bd768SChristoph Hellwig gdp->minors = 1 << DASD_PARTN_BITS; 511da177e4SLinus Torvalds gdp->fops = &dasd_device_operations; 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds /* 541da177e4SLinus Torvalds * Set device name. 551da177e4SLinus Torvalds * dasda - dasdz : 26 devices 561da177e4SLinus Torvalds * dasdaa - dasdzz : 676 devices, added up = 702 571da177e4SLinus Torvalds * dasdaaa - dasdzzz : 17576 devices, added up = 18278 581da177e4SLinus Torvalds * dasdaaaa - dasdzzzz : 456976 devices, added up = 475252 591da177e4SLinus Torvalds */ 601da177e4SLinus Torvalds len = sprintf(gdp->disk_name, "dasd"); 618e09f215SStefan Weinhuber if (base->devindex > 25) { 628e09f215SStefan Weinhuber if (base->devindex > 701) { 638e09f215SStefan Weinhuber if (base->devindex > 18277) 641da177e4SLinus Torvalds len += sprintf(gdp->disk_name + len, "%c", 658e09f215SStefan Weinhuber 'a'+(((base->devindex-18278) 661da177e4SLinus Torvalds /17576)%26)); 671da177e4SLinus Torvalds len += sprintf(gdp->disk_name + len, "%c", 688e09f215SStefan Weinhuber 'a'+(((base->devindex-702)/676)%26)); 691da177e4SLinus Torvalds } 701da177e4SLinus Torvalds len += sprintf(gdp->disk_name + len, "%c", 718e09f215SStefan Weinhuber 'a'+(((base->devindex-26)/26)%26)); 721da177e4SLinus Torvalds } 738e09f215SStefan Weinhuber len += sprintf(gdp->disk_name + len, "%c", 'a'+(base->devindex%26)); 741da177e4SLinus Torvalds 7533b62a30SStefan Weinhuber if (base->features & DASD_FEATURE_READONLY || 7633b62a30SStefan Weinhuber test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) 771da177e4SLinus Torvalds set_disk_ro(gdp, 1); 7865f8da47SStefan Weinhuber dasd_add_link_to_gendisk(gdp, base); 798e09f215SStefan Weinhuber gdp->queue = block->request_queue; 808e09f215SStefan Weinhuber block->gdp = gdp; 818e09f215SStefan Weinhuber set_capacity(block->gdp, 0); 82fef912bfSHannes Reinecke device_add_disk(&base->cdev->dev, block->gdp, NULL); 831da177e4SLinus Torvalds return 0; 841da177e4SLinus Torvalds } 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds /* 871da177e4SLinus Torvalds * Unregister and free gendisk structure for device. 881da177e4SLinus Torvalds */ 898e09f215SStefan Weinhuber void dasd_gendisk_free(struct dasd_block *block) 901da177e4SLinus Torvalds { 918e09f215SStefan Weinhuber if (block->gdp) { 928e09f215SStefan Weinhuber del_gendisk(block->gdp); 939eb25122SStefan Haberland block->gdp->private_data = NULL; 948e09f215SStefan Weinhuber put_disk(block->gdp); 958e09f215SStefan Weinhuber block->gdp = NULL; 961da177e4SLinus Torvalds } 978f61701bSHorst Hummel } 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds /* 1001da177e4SLinus Torvalds * Trigger a partition detection. 1011da177e4SLinus Torvalds */ 1028e09f215SStefan Weinhuber int dasd_scan_partitions(struct dasd_block *block) 1031da177e4SLinus Torvalds { 1041da177e4SLinus Torvalds struct block_device *bdev; 105a05e5780SJarod Wilson int rc; 1061da177e4SLinus Torvalds 107841ab72cSChristoph Hellwig bdev = blkdev_get_by_dev(disk_devt(block->gdp), FMODE_READ, NULL); 108841ab72cSChristoph Hellwig if (IS_ERR(bdev)) { 1096ebdf1c7SStefan Haberland DBF_DEV_EVENT(DBF_ERR, block->base, 110841ab72cSChristoph Hellwig "scan partitions error, blkdev_get returned %ld", 111841ab72cSChristoph Hellwig PTR_ERR(bdev)); 1126ebdf1c7SStefan Haberland return -ENODEV; 1136ebdf1c7SStefan Haberland } 1146029a06cSMing Lei 115a8698707SChristoph Hellwig mutex_lock(&block->gdp->open_mutex); 1160384264eSChristoph Hellwig rc = bdev_disk_changed(block->gdp, false); 117a8698707SChristoph Hellwig mutex_unlock(&block->gdp->open_mutex); 118a05e5780SJarod Wilson if (rc) 1196ebdf1c7SStefan Haberland DBF_DEV_EVENT(DBF_ERR, block->base, 120a05e5780SJarod Wilson "scan partitions error, rc %d", rc); 1216ebdf1c7SStefan Haberland 1221da177e4SLinus Torvalds /* 1231da177e4SLinus Torvalds * Since the matching blkdev_put call to the blkdev_get in 1241da177e4SLinus Torvalds * this function is not called before dasd_destroy_partitions 1251da177e4SLinus Torvalds * the offline open_count limit needs to be increased from 1261da177e4SLinus Torvalds * 0 to 1. This is done by setting device->bdev (see 1271da177e4SLinus Torvalds * dasd_generic_set_offline). As long as the partition 1281da177e4SLinus Torvalds * detection is running no offline should be allowed. That 1291da177e4SLinus Torvalds * is why the assignment to device->bdev is done AFTER 1301da177e4SLinus Torvalds * the BLKRRPART ioctl. 1311da177e4SLinus Torvalds */ 1328e09f215SStefan Weinhuber block->bdev = bdev; 1331da177e4SLinus Torvalds return 0; 1341da177e4SLinus Torvalds } 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds /* 1371da177e4SLinus Torvalds * Remove all inodes in the system for a device, delete the 1381da177e4SLinus Torvalds * partitions and make device unusable by setting its size to zero. 1391da177e4SLinus Torvalds */ 1408e09f215SStefan Weinhuber void dasd_destroy_partitions(struct dasd_block *block) 1411da177e4SLinus Torvalds { 1421da177e4SLinus Torvalds struct block_device *bdev; 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds /* 1451da177e4SLinus Torvalds * Get the bdev pointer from the device structure and clear 1461da177e4SLinus Torvalds * device->bdev to lower the offline open_count limit again. 1471da177e4SLinus Torvalds */ 1488e09f215SStefan Weinhuber bdev = block->bdev; 1498e09f215SStefan Weinhuber block->bdev = NULL; 1501da177e4SLinus Torvalds 151a8698707SChristoph Hellwig mutex_lock(&bdev->bd_disk->open_mutex); 1520384264eSChristoph Hellwig bdev_disk_changed(bdev->bd_disk, true); 153a8698707SChristoph Hellwig mutex_unlock(&bdev->bd_disk->open_mutex); 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds /* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */ 1569a1c3542SAl Viro blkdev_put(bdev, FMODE_READ); 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds 1598e09f215SStefan Weinhuber int dasd_gendisk_init(void) 1601da177e4SLinus Torvalds { 1611da177e4SLinus Torvalds int rc; 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds /* Register to static dasd major 94 */ 1641da177e4SLinus Torvalds rc = register_blkdev(DASD_MAJOR, "dasd"); 1651da177e4SLinus Torvalds if (rc != 0) { 166baebc70aSJoe Perches pr_warn("Registering the device driver with major number %d failed\n", 167baebc70aSJoe Perches DASD_MAJOR); 1681da177e4SLinus Torvalds return rc; 1691da177e4SLinus Torvalds } 1701da177e4SLinus Torvalds return 0; 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds 1738e09f215SStefan Weinhuber void dasd_gendisk_exit(void) 1741da177e4SLinus Torvalds { 1751da177e4SLinus Torvalds unregister_blkdev(DASD_MAJOR, "dasd"); 1761da177e4SLinus Torvalds } 177