Linux Audio

Check our new training course

Linux kernel drivers training

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