xref: /openbmc/linux/drivers/s390/block/dasd_genhd.c (revision 11dfe199eb31079a6f2517a59c380ad55f156696)
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;
36*11dfe199SLuis Chamberlain 	int len, rc;
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 
434a1fa41dSChristoph Hellwig 	gdp = __alloc_disk_node(block->request_queue, NUMA_NO_NODE,
444a1fa41dSChristoph Hellwig 				&dasd_bio_compl_lkclass);
451da177e4SLinus Torvalds 	if (!gdp)
461da177e4SLinus Torvalds 		return -ENOMEM;
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 	/* Initialize gendisk structure. */
491da177e4SLinus Torvalds 	gdp->major = DASD_MAJOR;
508e09f215SStefan Weinhuber 	gdp->first_minor = base->devindex << DASD_PARTN_BITS;
51a58bd768SChristoph Hellwig 	gdp->minors = 1 << DASD_PARTN_BITS;
521da177e4SLinus Torvalds 	gdp->fops = &dasd_device_operations;
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds 	/*
551da177e4SLinus Torvalds 	 * Set device name.
561da177e4SLinus Torvalds 	 *   dasda - dasdz : 26 devices
571da177e4SLinus Torvalds 	 *   dasdaa - dasdzz : 676 devices, added up = 702
581da177e4SLinus Torvalds 	 *   dasdaaa - dasdzzz : 17576 devices, added up = 18278
591da177e4SLinus Torvalds 	 *   dasdaaaa - dasdzzzz : 456976 devices, added up = 475252
601da177e4SLinus Torvalds 	 */
611da177e4SLinus Torvalds 	len = sprintf(gdp->disk_name, "dasd");
628e09f215SStefan Weinhuber 	if (base->devindex > 25) {
638e09f215SStefan Weinhuber 		if (base->devindex > 701) {
648e09f215SStefan Weinhuber 			if (base->devindex > 18277)
651da177e4SLinus Torvalds 			        len += sprintf(gdp->disk_name + len, "%c",
668e09f215SStefan Weinhuber 					       'a'+(((base->devindex-18278)
671da177e4SLinus Torvalds 						     /17576)%26));
681da177e4SLinus Torvalds 			len += sprintf(gdp->disk_name + len, "%c",
698e09f215SStefan Weinhuber 				       'a'+(((base->devindex-702)/676)%26));
701da177e4SLinus Torvalds 		}
711da177e4SLinus Torvalds 		len += sprintf(gdp->disk_name + len, "%c",
728e09f215SStefan Weinhuber 			       'a'+(((base->devindex-26)/26)%26));
731da177e4SLinus Torvalds 	}
748e09f215SStefan Weinhuber 	len += sprintf(gdp->disk_name + len, "%c", 'a'+(base->devindex%26));
751da177e4SLinus Torvalds 
7633b62a30SStefan Weinhuber 	if (base->features & DASD_FEATURE_READONLY ||
7733b62a30SStefan Weinhuber 	    test_bit(DASD_FLAG_DEVICE_RO, &base->flags))
781da177e4SLinus Torvalds 		set_disk_ro(gdp, 1);
7965f8da47SStefan Weinhuber 	dasd_add_link_to_gendisk(gdp, base);
808e09f215SStefan Weinhuber 	block->gdp = gdp;
818e09f215SStefan Weinhuber 	set_capacity(block->gdp, 0);
82*11dfe199SLuis Chamberlain 
83*11dfe199SLuis Chamberlain 	rc = device_add_disk(&base->cdev->dev, block->gdp, NULL);
84*11dfe199SLuis Chamberlain 	if (rc) {
85*11dfe199SLuis Chamberlain 		dasd_gendisk_free(block);
86*11dfe199SLuis Chamberlain 		return rc;
87*11dfe199SLuis Chamberlain 	}
88*11dfe199SLuis Chamberlain 
891da177e4SLinus Torvalds 	return 0;
901da177e4SLinus Torvalds }
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds /*
931da177e4SLinus Torvalds  * Unregister and free gendisk structure for device.
941da177e4SLinus Torvalds  */
958e09f215SStefan Weinhuber void dasd_gendisk_free(struct dasd_block *block)
961da177e4SLinus Torvalds {
978e09f215SStefan Weinhuber 	if (block->gdp) {
988e09f215SStefan Weinhuber 		del_gendisk(block->gdp);
999eb25122SStefan Haberland 		block->gdp->private_data = NULL;
1008e09f215SStefan Weinhuber 		put_disk(block->gdp);
1018e09f215SStefan Weinhuber 		block->gdp = NULL;
1021da177e4SLinus Torvalds 	}
1038f61701bSHorst Hummel }
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds /*
1061da177e4SLinus Torvalds  * Trigger a partition detection.
1071da177e4SLinus Torvalds  */
1088e09f215SStefan Weinhuber int dasd_scan_partitions(struct dasd_block *block)
1091da177e4SLinus Torvalds {
1101da177e4SLinus Torvalds 	struct block_device *bdev;
111a05e5780SJarod Wilson 	int rc;
1121da177e4SLinus Torvalds 
113841ab72cSChristoph Hellwig 	bdev = blkdev_get_by_dev(disk_devt(block->gdp), FMODE_READ, NULL);
114841ab72cSChristoph Hellwig 	if (IS_ERR(bdev)) {
1156ebdf1c7SStefan Haberland 		DBF_DEV_EVENT(DBF_ERR, block->base,
116841ab72cSChristoph Hellwig 			      "scan partitions error, blkdev_get returned %ld",
117841ab72cSChristoph Hellwig 			      PTR_ERR(bdev));
1186ebdf1c7SStefan Haberland 		return -ENODEV;
1196ebdf1c7SStefan Haberland 	}
1206029a06cSMing Lei 
121a8698707SChristoph Hellwig 	mutex_lock(&block->gdp->open_mutex);
1220384264eSChristoph Hellwig 	rc = bdev_disk_changed(block->gdp, false);
123a8698707SChristoph Hellwig 	mutex_unlock(&block->gdp->open_mutex);
124a05e5780SJarod Wilson 	if (rc)
1256ebdf1c7SStefan Haberland 		DBF_DEV_EVENT(DBF_ERR, block->base,
126a05e5780SJarod Wilson 				"scan partitions error, rc %d", rc);
1276ebdf1c7SStefan Haberland 
1281da177e4SLinus Torvalds 	/*
1291da177e4SLinus Torvalds 	 * Since the matching blkdev_put call to the blkdev_get in
1301da177e4SLinus Torvalds 	 * this function is not called before dasd_destroy_partitions
1311da177e4SLinus Torvalds 	 * the offline open_count limit needs to be increased from
1321da177e4SLinus Torvalds 	 * 0 to 1. This is done by setting device->bdev (see
1331da177e4SLinus Torvalds 	 * dasd_generic_set_offline). As long as the partition
1341da177e4SLinus Torvalds 	 * detection is running no offline should be allowed. That
1351da177e4SLinus Torvalds 	 * is why the assignment to device->bdev is done AFTER
1361da177e4SLinus Torvalds 	 * the BLKRRPART ioctl.
1371da177e4SLinus Torvalds 	 */
1388e09f215SStefan Weinhuber 	block->bdev = bdev;
1391da177e4SLinus Torvalds 	return 0;
1401da177e4SLinus Torvalds }
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds /*
1431da177e4SLinus Torvalds  * Remove all inodes in the system for a device, delete the
1441da177e4SLinus Torvalds  * partitions and make device unusable by setting its size to zero.
1451da177e4SLinus Torvalds  */
1468e09f215SStefan Weinhuber void dasd_destroy_partitions(struct dasd_block *block)
1471da177e4SLinus Torvalds {
1481da177e4SLinus Torvalds 	struct block_device *bdev;
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	/*
1511da177e4SLinus Torvalds 	 * Get the bdev pointer from the device structure and clear
1521da177e4SLinus Torvalds 	 * device->bdev to lower the offline open_count limit again.
1531da177e4SLinus Torvalds 	 */
1548e09f215SStefan Weinhuber 	bdev = block->bdev;
1558e09f215SStefan Weinhuber 	block->bdev = NULL;
1561da177e4SLinus Torvalds 
157a8698707SChristoph Hellwig 	mutex_lock(&bdev->bd_disk->open_mutex);
1580384264eSChristoph Hellwig 	bdev_disk_changed(bdev->bd_disk, true);
159a8698707SChristoph Hellwig 	mutex_unlock(&bdev->bd_disk->open_mutex);
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	/* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */
1629a1c3542SAl Viro 	blkdev_put(bdev, FMODE_READ);
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds 
1658e09f215SStefan Weinhuber int dasd_gendisk_init(void)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds 	int rc;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	/* Register to static dasd major 94 */
1701da177e4SLinus Torvalds 	rc = register_blkdev(DASD_MAJOR, "dasd");
1711da177e4SLinus Torvalds 	if (rc != 0) {
172baebc70aSJoe Perches 		pr_warn("Registering the device driver with major number %d failed\n",
173baebc70aSJoe Perches 			DASD_MAJOR);
1741da177e4SLinus Torvalds 		return rc;
1751da177e4SLinus Torvalds 	}
1761da177e4SLinus Torvalds 	return 0;
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds 
1798e09f215SStefan Weinhuber void dasd_gendisk_exit(void)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	unregister_blkdev(DASD_MAJOR, "dasd");
1821da177e4SLinus Torvalds }
183