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 271da177e4SLinus Torvalds /* 281da177e4SLinus Torvalds * Allocate and register gendisk structure for device. 291da177e4SLinus Torvalds */ 308e09f215SStefan Weinhuber int dasd_gendisk_alloc(struct dasd_block *block) 311da177e4SLinus Torvalds { 321da177e4SLinus Torvalds struct gendisk *gdp; 338e09f215SStefan Weinhuber struct dasd_device *base; 34c6eb7b77SHorst Hummel int len; 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds /* Make sure the minor for this device exists. */ 378e09f215SStefan Weinhuber base = block->base; 388e09f215SStefan Weinhuber if (base->devindex >= DASD_PER_MAJOR) 391da177e4SLinus Torvalds return -EBUSY; 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds gdp = alloc_disk(1 << DASD_PARTN_BITS); 421da177e4SLinus Torvalds if (!gdp) 431da177e4SLinus Torvalds return -ENOMEM; 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds /* Initialize gendisk structure. */ 461da177e4SLinus Torvalds gdp->major = DASD_MAJOR; 478e09f215SStefan Weinhuber gdp->first_minor = base->devindex << DASD_PARTN_BITS; 481da177e4SLinus Torvalds gdp->fops = &dasd_device_operations; 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); 79fef912bfSHannes Reinecke device_add_disk(&base->cdev->dev, block->gdp, NULL); 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); 909eb25122SStefan Haberland block->gdp->private_data = NULL; 918e09f215SStefan Weinhuber put_disk(block->gdp); 928e09f215SStefan Weinhuber block->gdp = NULL; 931da177e4SLinus Torvalds } 948f61701bSHorst Hummel } 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds /* 971da177e4SLinus Torvalds * Trigger a partition detection. 981da177e4SLinus Torvalds */ 998e09f215SStefan Weinhuber int dasd_scan_partitions(struct dasd_block *block) 1001da177e4SLinus Torvalds { 1011da177e4SLinus Torvalds struct block_device *bdev; 102a05e5780SJarod Wilson int rc; 1031da177e4SLinus Torvalds 104841ab72cSChristoph Hellwig bdev = blkdev_get_by_dev(disk_devt(block->gdp), FMODE_READ, NULL); 105841ab72cSChristoph Hellwig if (IS_ERR(bdev)) { 1066ebdf1c7SStefan Haberland DBF_DEV_EVENT(DBF_ERR, block->base, 107841ab72cSChristoph Hellwig "scan partitions error, blkdev_get returned %ld", 108841ab72cSChristoph Hellwig PTR_ERR(bdev)); 1096ebdf1c7SStefan Haberland return -ENODEV; 1106ebdf1c7SStefan Haberland } 1116029a06cSMing Lei 112a8698707SChristoph Hellwig mutex_lock(&block->gdp->open_mutex); 113*0384264eSChristoph Hellwig rc = bdev_disk_changed(block->gdp, false); 114a8698707SChristoph Hellwig mutex_unlock(&block->gdp->open_mutex); 115a05e5780SJarod Wilson if (rc) 1166ebdf1c7SStefan Haberland DBF_DEV_EVENT(DBF_ERR, block->base, 117a05e5780SJarod Wilson "scan partitions error, rc %d", rc); 1186ebdf1c7SStefan Haberland 1191da177e4SLinus Torvalds /* 1201da177e4SLinus Torvalds * Since the matching blkdev_put call to the blkdev_get in 1211da177e4SLinus Torvalds * this function is not called before dasd_destroy_partitions 1221da177e4SLinus Torvalds * the offline open_count limit needs to be increased from 1231da177e4SLinus Torvalds * 0 to 1. This is done by setting device->bdev (see 1241da177e4SLinus Torvalds * dasd_generic_set_offline). As long as the partition 1251da177e4SLinus Torvalds * detection is running no offline should be allowed. That 1261da177e4SLinus Torvalds * is why the assignment to device->bdev is done AFTER 1271da177e4SLinus Torvalds * the BLKRRPART ioctl. 1281da177e4SLinus Torvalds */ 1298e09f215SStefan Weinhuber block->bdev = bdev; 1301da177e4SLinus Torvalds return 0; 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds /* 1341da177e4SLinus Torvalds * Remove all inodes in the system for a device, delete the 1351da177e4SLinus Torvalds * partitions and make device unusable by setting its size to zero. 1361da177e4SLinus Torvalds */ 1378e09f215SStefan Weinhuber void dasd_destroy_partitions(struct dasd_block *block) 1381da177e4SLinus Torvalds { 1391da177e4SLinus Torvalds struct block_device *bdev; 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds /* 1421da177e4SLinus Torvalds * Get the bdev pointer from the device structure and clear 1431da177e4SLinus Torvalds * device->bdev to lower the offline open_count limit again. 1441da177e4SLinus Torvalds */ 1458e09f215SStefan Weinhuber bdev = block->bdev; 1468e09f215SStefan Weinhuber block->bdev = NULL; 1471da177e4SLinus Torvalds 148a8698707SChristoph Hellwig mutex_lock(&bdev->bd_disk->open_mutex); 149*0384264eSChristoph Hellwig bdev_disk_changed(bdev->bd_disk, true); 150a8698707SChristoph Hellwig mutex_unlock(&bdev->bd_disk->open_mutex); 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds /* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */ 1539a1c3542SAl Viro blkdev_put(bdev, FMODE_READ); 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds 1568e09f215SStefan Weinhuber int dasd_gendisk_init(void) 1571da177e4SLinus Torvalds { 1581da177e4SLinus Torvalds int rc; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds /* Register to static dasd major 94 */ 1611da177e4SLinus Torvalds rc = register_blkdev(DASD_MAJOR, "dasd"); 1621da177e4SLinus Torvalds if (rc != 0) { 163baebc70aSJoe Perches pr_warn("Registering the device driver with major number %d failed\n", 164baebc70aSJoe Perches DASD_MAJOR); 1651da177e4SLinus Torvalds return rc; 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds return 0; 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 1708e09f215SStefan Weinhuber void dasd_gendisk_exit(void) 1711da177e4SLinus Torvalds { 1721da177e4SLinus Torvalds unregister_blkdev(DASD_MAJOR, "dasd"); 1731da177e4SLinus Torvalds } 174