• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4  *		    Horst Hummel <Horst.Hummel@de.ibm.com>
5  *		    Carsten Otte <Cotte@de.ibm.com>
6  *		    Martin Schwidefsky <schwidefsky@de.ibm.com>
7  * Bugreports.to..: <Linux390@de.ibm.com>
8  * Copyright IBM Corp. 1999, 2001
9  *
10  * gendisk related functions for the dasd driver.
11  *
12  */
13 
14 #define KMSG_COMPONENT "dasd"
15 
16 #include <linux/interrupt.h>
17 #include <linux/major.h>
18 #include <linux/fs.h>
19 #include <linux/blkpg.h>
20 
21 #include <linux/uaccess.h>
22 
23 /* This is ugly... */
24 #define PRINTK_HEADER "dasd_gendisk:"
25 
26 #include "dasd_int.h"
27 
28 static struct lock_class_key dasd_bio_compl_lkclass;
29 
30 /*
31  * Allocate and register gendisk structure for device.
32  */
dasd_gendisk_alloc(struct dasd_block * block)33 int dasd_gendisk_alloc(struct dasd_block *block)
34 {
35 	struct gendisk *gdp;
36 	struct dasd_device *base;
37 	int len;
38 
39 	/* Make sure the minor for this device exists. */
40 	base = block->base;
41 	if (base->devindex >= DASD_PER_MAJOR)
42 		return -EBUSY;
43 
44 	gdp = __alloc_disk_node(block->request_queue, NUMA_NO_NODE,
45 				&dasd_bio_compl_lkclass);
46 	if (!gdp)
47 		return -ENOMEM;
48 
49 	/* Initialize gendisk structure. */
50 	gdp->major = DASD_MAJOR;
51 	gdp->first_minor = base->devindex << DASD_PARTN_BITS;
52 	gdp->minors = 1 << DASD_PARTN_BITS;
53 	gdp->fops = &dasd_device_operations;
54 
55 	/*
56 	 * Set device name.
57 	 *   dasda - dasdz : 26 devices
58 	 *   dasdaa - dasdzz : 676 devices, added up = 702
59 	 *   dasdaaa - dasdzzz : 17576 devices, added up = 18278
60 	 *   dasdaaaa - dasdzzzz : 456976 devices, added up = 475252
61 	 */
62 	len = sprintf(gdp->disk_name, "dasd");
63 	if (base->devindex > 25) {
64 		if (base->devindex > 701) {
65 			if (base->devindex > 18277)
66 			        len += sprintf(gdp->disk_name + len, "%c",
67 					       'a'+(((base->devindex-18278)
68 						     /17576)%26));
69 			len += sprintf(gdp->disk_name + len, "%c",
70 				       'a'+(((base->devindex-702)/676)%26));
71 		}
72 		len += sprintf(gdp->disk_name + len, "%c",
73 			       'a'+(((base->devindex-26)/26)%26));
74 	}
75 	len += sprintf(gdp->disk_name + len, "%c", 'a'+(base->devindex%26));
76 
77 	if (base->features & DASD_FEATURE_READONLY ||
78 	    test_bit(DASD_FLAG_DEVICE_RO, &base->flags))
79 		set_disk_ro(gdp, 1);
80 	dasd_add_link_to_gendisk(gdp, base);
81 	block->gdp = gdp;
82 	set_capacity(block->gdp, 0);
83 	device_add_disk(&base->cdev->dev, block->gdp, NULL);
84 	return 0;
85 }
86 
87 /*
88  * Unregister and free gendisk structure for device.
89  */
dasd_gendisk_free(struct dasd_block * block)90 void dasd_gendisk_free(struct dasd_block *block)
91 {
92 	if (block->gdp) {
93 		del_gendisk(block->gdp);
94 		block->gdp->private_data = NULL;
95 		put_disk(block->gdp);
96 		block->gdp = NULL;
97 	}
98 }
99 
100 /*
101  * Trigger a partition detection.
102  */
dasd_scan_partitions(struct dasd_block * block)103 int dasd_scan_partitions(struct dasd_block *block)
104 {
105 	struct block_device *bdev;
106 	int rc;
107 
108 	bdev = blkdev_get_by_dev(disk_devt(block->gdp), FMODE_READ, NULL);
109 	if (IS_ERR(bdev)) {
110 		DBF_DEV_EVENT(DBF_ERR, block->base,
111 			      "scan partitions error, blkdev_get returned %ld",
112 			      PTR_ERR(bdev));
113 		return -ENODEV;
114 	}
115 
116 	mutex_lock(&block->gdp->open_mutex);
117 	rc = bdev_disk_changed(block->gdp, false);
118 	mutex_unlock(&block->gdp->open_mutex);
119 	if (rc)
120 		DBF_DEV_EVENT(DBF_ERR, block->base,
121 				"scan partitions error, rc %d", rc);
122 
123 	/*
124 	 * Since the matching blkdev_put call to the blkdev_get in
125 	 * this function is not called before dasd_destroy_partitions
126 	 * the offline open_count limit needs to be increased from
127 	 * 0 to 1. This is done by setting device->bdev (see
128 	 * dasd_generic_set_offline). As long as the partition
129 	 * detection is running no offline should be allowed. That
130 	 * is why the assignment to device->bdev is done AFTER
131 	 * the BLKRRPART ioctl.
132 	 */
133 	block->bdev = bdev;
134 	return 0;
135 }
136 
137 /*
138  * Remove all inodes in the system for a device, delete the
139  * partitions and make device unusable by setting its size to zero.
140  */
dasd_destroy_partitions(struct dasd_block * block)141 void dasd_destroy_partitions(struct dasd_block *block)
142 {
143 	struct block_device *bdev;
144 
145 	/*
146 	 * Get the bdev pointer from the device structure and clear
147 	 * device->bdev to lower the offline open_count limit again.
148 	 */
149 	bdev = block->bdev;
150 	block->bdev = NULL;
151 
152 	mutex_lock(&bdev->bd_disk->open_mutex);
153 	bdev_disk_changed(bdev->bd_disk, true);
154 	mutex_unlock(&bdev->bd_disk->open_mutex);
155 
156 	/* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */
157 	blkdev_put(bdev, FMODE_READ);
158 }
159 
dasd_gendisk_init(void)160 int dasd_gendisk_init(void)
161 {
162 	int rc;
163 
164 	/* Register to static dasd major 94 */
165 	rc = register_blkdev(DASD_MAJOR, "dasd");
166 	if (rc != 0) {
167 		pr_warn("Registering the device driver with major number %d failed\n",
168 			DASD_MAJOR);
169 		return rc;
170 	}
171 	return 0;
172 }
173 
dasd_gendisk_exit(void)174 void dasd_gendisk_exit(void)
175 {
176 	unregister_blkdev(DASD_MAJOR, "dasd");
177 }
178