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