xref: /linux/drivers/s390/block/dasd_genhd.c (revision e814f3fd16acfb7f9966773953de8f740a1e3202)
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 #include <linux/interrupt.h>
15 #include <linux/major.h>
16 #include <linux/fs.h>
17 #include <linux/blkpg.h>
18 
19 #include <linux/uaccess.h>
20 
21 #include "dasd_int.h"
22 
23 static unsigned int queue_depth = 32;
24 static unsigned int nr_hw_queues = 4;
25 
26 module_param(queue_depth, uint, 0444);
27 MODULE_PARM_DESC(queue_depth, "Default queue depth for new DASD devices");
28 
29 module_param(nr_hw_queues, uint, 0444);
30 MODULE_PARM_DESC(nr_hw_queues, "Default number of hardware queues for new DASD devices");
31 
32 /*
33  * Allocate and register gendisk structure for device.
34  */
35 int dasd_gendisk_alloc(struct dasd_block *block)
36 {
37 	struct queue_limits lim = {
38 		/*
39 		 * With page sized segments, each segment can be translated into
40 		 * one idaw/tidaw.
41 		 */
42 		.max_segment_size = PAGE_SIZE,
43 		.seg_boundary_mask = PAGE_SIZE - 1,
44 		.max_segments = USHRT_MAX,
45 	};
46 	struct gendisk *gdp;
47 	struct dasd_device *base;
48 	int len, rc;
49 
50 	/* Make sure the minor for this device exists. */
51 	base = block->base;
52 	if (base->devindex >= DASD_PER_MAJOR)
53 		return -EBUSY;
54 
55 	block->tag_set.ops = &dasd_mq_ops;
56 	block->tag_set.cmd_size = sizeof(struct dasd_ccw_req);
57 	block->tag_set.nr_hw_queues = nr_hw_queues;
58 	block->tag_set.queue_depth = queue_depth;
59 	block->tag_set.numa_node = NUMA_NO_NODE;
60 	rc = blk_mq_alloc_tag_set(&block->tag_set);
61 	if (rc)
62 		return rc;
63 
64 	gdp = blk_mq_alloc_disk(&block->tag_set, &lim, block);
65 	if (IS_ERR(gdp)) {
66 		blk_mq_free_tag_set(&block->tag_set);
67 		return PTR_ERR(gdp);
68 	}
69 
70 	/* Initialize gendisk structure. */
71 	gdp->major = DASD_MAJOR;
72 	gdp->first_minor = base->devindex << DASD_PARTN_BITS;
73 	gdp->minors = 1 << DASD_PARTN_BITS;
74 	gdp->fops = &dasd_device_operations;
75 
76 	/*
77 	 * Set device name.
78 	 *   dasda - dasdz : 26 devices
79 	 *   dasdaa - dasdzz : 676 devices, added up = 702
80 	 *   dasdaaa - dasdzzz : 17576 devices, added up = 18278
81 	 *   dasdaaaa - dasdzzzz : 456976 devices, added up = 475252
82 	 */
83 	len = sprintf(gdp->disk_name, "dasd");
84 	if (base->devindex > 25) {
85 		if (base->devindex > 701) {
86 			if (base->devindex > 18277)
87 			        len += sprintf(gdp->disk_name + len, "%c",
88 					       'a'+(((base->devindex-18278)
89 						     /17576)%26));
90 			len += sprintf(gdp->disk_name + len, "%c",
91 				       'a'+(((base->devindex-702)/676)%26));
92 		}
93 		len += sprintf(gdp->disk_name + len, "%c",
94 			       'a'+(((base->devindex-26)/26)%26));
95 	}
96 	len += sprintf(gdp->disk_name + len, "%c", 'a'+(base->devindex%26));
97 
98 	if (base->features & DASD_FEATURE_READONLY ||
99 	    test_bit(DASD_FLAG_DEVICE_RO, &base->flags))
100 		set_disk_ro(gdp, 1);
101 	dasd_add_link_to_gendisk(gdp, base);
102 	block->gdp = gdp;
103 	set_capacity(block->gdp, 0);
104 
105 	rc = device_add_disk(&base->cdev->dev, block->gdp, NULL);
106 	if (rc) {
107 		dasd_gendisk_free(block);
108 		return rc;
109 	}
110 
111 	return 0;
112 }
113 
114 /*
115  * Unregister and free gendisk structure for device.
116  */
117 void dasd_gendisk_free(struct dasd_block *block)
118 {
119 	if (block->gdp) {
120 		del_gendisk(block->gdp);
121 		block->gdp->private_data = NULL;
122 		put_disk(block->gdp);
123 		block->gdp = NULL;
124 		blk_mq_free_tag_set(&block->tag_set);
125 	}
126 }
127 
128 /*
129  * Trigger a partition detection.
130  */
131 int dasd_scan_partitions(struct dasd_block *block)
132 {
133 	struct file *bdev_file;
134 	int rc;
135 
136 	bdev_file = bdev_file_open_by_dev(disk_devt(block->gdp), BLK_OPEN_READ,
137 				       NULL, NULL);
138 	if (IS_ERR(bdev_file)) {
139 		DBF_DEV_EVENT(DBF_ERR, block->base,
140 			      "scan partitions error, blkdev_get returned %ld",
141 			      PTR_ERR(bdev_file));
142 		return -ENODEV;
143 	}
144 
145 	mutex_lock(&block->gdp->open_mutex);
146 	rc = bdev_disk_changed(block->gdp, false);
147 	mutex_unlock(&block->gdp->open_mutex);
148 	if (rc)
149 		DBF_DEV_EVENT(DBF_ERR, block->base,
150 				"scan partitions error, rc %d", rc);
151 
152 	/*
153 	 * Since the matching fput() call to the
154 	 * bdev_file_open_by_path() in this function is not called before
155 	 * dasd_destroy_partitions the offline open_count limit needs to be
156 	 * increased from 0 to 1. This is done by setting device->bdev_file
157 	 * (see dasd_generic_set_offline). As long as the partition detection
158 	 * is running no offline should be allowed. That is why the assignment
159 	 * to block->bdev_file is done AFTER the BLKRRPART ioctl.
160 	 */
161 	block->bdev_file = bdev_file;
162 	return 0;
163 }
164 
165 /*
166  * Remove all inodes in the system for a device, delete the
167  * partitions and make device unusable by setting its size to zero.
168  */
169 void dasd_destroy_partitions(struct dasd_block *block)
170 {
171 	struct file *bdev_file;
172 
173 	/*
174 	 * Get the bdev_file pointer from the device structure and clear
175 	 * device->bdev_file to lower the offline open_count limit again.
176 	 */
177 	bdev_file = block->bdev_file;
178 	block->bdev_file = NULL;
179 
180 	mutex_lock(&file_bdev(bdev_file)->bd_disk->open_mutex);
181 	bdev_disk_changed(file_bdev(bdev_file)->bd_disk, true);
182 	mutex_unlock(&file_bdev(bdev_file)->bd_disk->open_mutex);
183 
184 	/* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */
185 	fput(bdev_file);
186 }
187 
188 int dasd_gendisk_init(void)
189 {
190 	int rc;
191 
192 	/* Register to static dasd major 94 */
193 	rc = register_blkdev(DASD_MAJOR, "dasd");
194 	if (rc != 0) {
195 		pr_warn("Registering the device driver with major number %d failed\n",
196 			DASD_MAJOR);
197 		return rc;
198 	}
199 	return 0;
200 }
201 
202 void dasd_gendisk_exit(void)
203 {
204 	unregister_blkdev(DASD_MAJOR, "dasd");
205 }
206