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 */
dasd_gendisk_alloc(struct dasd_block * block)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.flags = BLK_MQ_F_SHOULD_MERGE;
60 block->tag_set.numa_node = NUMA_NO_NODE;
61 rc = blk_mq_alloc_tag_set(&block->tag_set);
62 if (rc)
63 return rc;
64
65 gdp = blk_mq_alloc_disk(&block->tag_set, &lim, block);
66 if (IS_ERR(gdp)) {
67 blk_mq_free_tag_set(&block->tag_set);
68 return PTR_ERR(gdp);
69 }
70
71 /* Initialize gendisk structure. */
72 gdp->major = DASD_MAJOR;
73 gdp->first_minor = base->devindex << DASD_PARTN_BITS;
74 gdp->minors = 1 << DASD_PARTN_BITS;
75 gdp->fops = &dasd_device_operations;
76
77 /*
78 * Set device name.
79 * dasda - dasdz : 26 devices
80 * dasdaa - dasdzz : 676 devices, added up = 702
81 * dasdaaa - dasdzzz : 17576 devices, added up = 18278
82 * dasdaaaa - dasdzzzz : 456976 devices, added up = 475252
83 */
84 len = sprintf(gdp->disk_name, "dasd");
85 if (base->devindex > 25) {
86 if (base->devindex > 701) {
87 if (base->devindex > 18277)
88 len += sprintf(gdp->disk_name + len, "%c",
89 'a'+(((base->devindex-18278)
90 /17576)%26));
91 len += sprintf(gdp->disk_name + len, "%c",
92 'a'+(((base->devindex-702)/676)%26));
93 }
94 len += sprintf(gdp->disk_name + len, "%c",
95 'a'+(((base->devindex-26)/26)%26));
96 }
97 len += sprintf(gdp->disk_name + len, "%c", 'a'+(base->devindex%26));
98
99 if (base->features & DASD_FEATURE_READONLY ||
100 test_bit(DASD_FLAG_DEVICE_RO, &base->flags))
101 set_disk_ro(gdp, 1);
102 dasd_add_link_to_gendisk(gdp, base);
103 block->gdp = gdp;
104 set_capacity(block->gdp, 0);
105
106 rc = device_add_disk(&base->cdev->dev, block->gdp, NULL);
107 if (rc) {
108 dasd_gendisk_free(block);
109 return rc;
110 }
111
112 return 0;
113 }
114
115 /*
116 * Unregister and free gendisk structure for device.
117 */
dasd_gendisk_free(struct dasd_block * block)118 void dasd_gendisk_free(struct dasd_block *block)
119 {
120 if (block->gdp) {
121 del_gendisk(block->gdp);
122 block->gdp->private_data = NULL;
123 put_disk(block->gdp);
124 block->gdp = NULL;
125 blk_mq_free_tag_set(&block->tag_set);
126 }
127 }
128
129 /*
130 * Trigger a partition detection.
131 */
dasd_scan_partitions(struct dasd_block * block)132 int dasd_scan_partitions(struct dasd_block *block)
133 {
134 struct file *bdev_file;
135 int rc;
136
137 bdev_file = bdev_file_open_by_dev(disk_devt(block->gdp), BLK_OPEN_READ,
138 NULL, NULL);
139 if (IS_ERR(bdev_file)) {
140 DBF_DEV_EVENT(DBF_ERR, block->base,
141 "scan partitions error, blkdev_get returned %ld",
142 PTR_ERR(bdev_file));
143 return -ENODEV;
144 }
145
146 mutex_lock(&block->gdp->open_mutex);
147 rc = bdev_disk_changed(block->gdp, false);
148 mutex_unlock(&block->gdp->open_mutex);
149 if (rc)
150 DBF_DEV_EVENT(DBF_ERR, block->base,
151 "scan partitions error, rc %d", rc);
152
153 /*
154 * Since the matching fput() call to the
155 * bdev_file_open_by_path() in this function is not called before
156 * dasd_destroy_partitions the offline open_count limit needs to be
157 * increased from 0 to 1. This is done by setting device->bdev_file
158 * (see dasd_generic_set_offline). As long as the partition detection
159 * is running no offline should be allowed. That is why the assignment
160 * to block->bdev_file is done AFTER the BLKRRPART ioctl.
161 */
162 block->bdev_file = bdev_file;
163 return 0;
164 }
165
166 /*
167 * Remove all inodes in the system for a device, delete the
168 * partitions and make device unusable by setting its size to zero.
169 */
dasd_destroy_partitions(struct dasd_block * block)170 void dasd_destroy_partitions(struct dasd_block *block)
171 {
172 struct file *bdev_file;
173
174 /*
175 * Get the bdev_file pointer from the device structure and clear
176 * device->bdev_file to lower the offline open_count limit again.
177 */
178 bdev_file = block->bdev_file;
179 block->bdev_file = NULL;
180
181 mutex_lock(&file_bdev(bdev_file)->bd_disk->open_mutex);
182 bdev_disk_changed(file_bdev(bdev_file)->bd_disk, true);
183 mutex_unlock(&file_bdev(bdev_file)->bd_disk->open_mutex);
184
185 /* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */
186 fput(bdev_file);
187 }
188
dasd_gendisk_init(void)189 int dasd_gendisk_init(void)
190 {
191 int rc;
192
193 /* Register to static dasd major 94 */
194 rc = register_blkdev(DASD_MAJOR, "dasd");
195 if (rc != 0) {
196 pr_warn("Registering the device driver with major number %d failed\n",
197 DASD_MAJOR);
198 return rc;
199 }
200 return 0;
201 }
202
dasd_gendisk_exit(void)203 void dasd_gendisk_exit(void)
204 {
205 unregister_blkdev(DASD_MAJOR, "dasd");
206 }
207