Commit bf9c0538 authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Jens Axboe

ataflop: use a separate gendisk for each media format

The Atari floppy driver usually autodetects the media when used with the
ormal /dev/fd? devices, which also are the only nodes created by udev.
But it also supports various aliases that force a given media format.
That is currently supported using the blk_register_region framework
which finds the floppy gendisk even for a 'mismatched' dev_t.  The
problem with this (besides the code complexity) is that it creates
multiple struct block_device instances for the whole device of a
single gendisk, which can lead to interesting issues in code not
aware of that fact.

To fix this just create a separate gendisk for each of the aliases
if they are accessed.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 0033a9b4
...@@ -297,7 +297,7 @@ static struct atari_floppy_struct { ...@@ -297,7 +297,7 @@ static struct atari_floppy_struct {
unsigned int wpstat; /* current state of WP signal (for unsigned int wpstat; /* current state of WP signal (for
disk change detection) */ disk change detection) */
int flags; /* flags */ int flags; /* flags */
struct gendisk *disk; struct gendisk *disk[NUM_DISK_MINORS];
int ref; int ref;
int type; int type;
struct blk_mq_tag_set tag_set; struct blk_mq_tag_set tag_set;
...@@ -723,12 +723,16 @@ static void fd_error( void ) ...@@ -723,12 +723,16 @@ static void fd_error( void )
static int do_format(int drive, int type, struct atari_format_descr *desc) static int do_format(int drive, int type, struct atari_format_descr *desc)
{ {
struct request_queue *q = unit[drive].disk->queue; struct request_queue *q;
unsigned char *p; unsigned char *p;
int sect, nsect; int sect, nsect;
unsigned long flags; unsigned long flags;
int ret; int ret;
if (type)
type--;
q = unit[drive].disk[type]->queue;
blk_mq_freeze_queue(q); blk_mq_freeze_queue(q);
blk_mq_quiesce_queue(q); blk_mq_quiesce_queue(q);
...@@ -738,7 +742,7 @@ static int do_format(int drive, int type, struct atari_format_descr *desc) ...@@ -738,7 +742,7 @@ static int do_format(int drive, int type, struct atari_format_descr *desc)
local_irq_restore(flags); local_irq_restore(flags);
if (type) { if (type) {
if (--type >= NUM_DISK_MINORS || if (type >= NUM_DISK_MINORS ||
minor2disktype[type].drive_types > DriveType) { minor2disktype[type].drive_types > DriveType) {
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
...@@ -1154,7 +1158,7 @@ static void fd_rwsec_done1(int status) ...@@ -1154,7 +1158,7 @@ static void fd_rwsec_done1(int status)
if (SUDT[-1].blocks > ReqBlock) { if (SUDT[-1].blocks > ReqBlock) {
/* try another disk type */ /* try another disk type */
SUDT--; SUDT--;
set_capacity(unit[SelectedDrive].disk, set_capacity(unit[SelectedDrive].disk[0],
SUDT->blocks); SUDT->blocks);
} else } else
Probing = 0; Probing = 0;
...@@ -1169,7 +1173,7 @@ static void fd_rwsec_done1(int status) ...@@ -1169,7 +1173,7 @@ static void fd_rwsec_done1(int status)
/* record not found, but not probing. Maybe stretch wrong ? Restart probing */ /* record not found, but not probing. Maybe stretch wrong ? Restart probing */
if (SUD.autoprobe) { if (SUD.autoprobe) {
SUDT = atari_disk_type + StartDiskType[DriveType]; SUDT = atari_disk_type + StartDiskType[DriveType];
set_capacity(unit[SelectedDrive].disk, set_capacity(unit[SelectedDrive].disk[0],
SUDT->blocks); SUDT->blocks);
Probing = 1; Probing = 1;
} }
...@@ -1515,7 +1519,7 @@ static blk_status_t ataflop_queue_rq(struct blk_mq_hw_ctx *hctx, ...@@ -1515,7 +1519,7 @@ static blk_status_t ataflop_queue_rq(struct blk_mq_hw_ctx *hctx,
if (!UDT) { if (!UDT) {
Probing = 1; Probing = 1;
UDT = atari_disk_type + StartDiskType[DriveType]; UDT = atari_disk_type + StartDiskType[DriveType];
set_capacity(floppy->disk, UDT->blocks); set_capacity(bd->rq->rq_disk, UDT->blocks);
UD.autoprobe = 1; UD.autoprobe = 1;
} }
} }
...@@ -1533,7 +1537,7 @@ static blk_status_t ataflop_queue_rq(struct blk_mq_hw_ctx *hctx, ...@@ -1533,7 +1537,7 @@ static blk_status_t ataflop_queue_rq(struct blk_mq_hw_ctx *hctx,
} }
type = minor2disktype[type].index; type = minor2disktype[type].index;
UDT = &atari_disk_type[type]; UDT = &atari_disk_type[type];
set_capacity(floppy->disk, UDT->blocks); set_capacity(bd->rq->rq_disk, UDT->blocks);
UD.autoprobe = 0; UD.autoprobe = 0;
} }
...@@ -1658,7 +1662,7 @@ static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, ...@@ -1658,7 +1662,7 @@ static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode,
printk (KERN_INFO "floppy%d: setting %s %p!\n", printk (KERN_INFO "floppy%d: setting %s %p!\n",
drive, dtp->name, dtp); drive, dtp->name, dtp);
UDT = dtp; UDT = dtp;
set_capacity(floppy->disk, UDT->blocks); set_capacity(disk, UDT->blocks);
if (cmd == FDDEFPRM) { if (cmd == FDDEFPRM) {
/* save settings as permanent default type */ /* save settings as permanent default type */
...@@ -1702,7 +1706,7 @@ static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, ...@@ -1702,7 +1706,7 @@ static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode,
return -EINVAL; return -EINVAL;
UDT = dtp; UDT = dtp;
set_capacity(floppy->disk, UDT->blocks); set_capacity(disk, UDT->blocks);
return 0; return 0;
case FDMSGON: case FDMSGON:
...@@ -1725,7 +1729,7 @@ static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, ...@@ -1725,7 +1729,7 @@ static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode,
UDT = NULL; UDT = NULL;
/* MSch: invalidate default_params */ /* MSch: invalidate default_params */
default_params[drive].blocks = 0; default_params[drive].blocks = 0;
set_capacity(floppy->disk, MAX_DISK_SIZE * 2); set_capacity(disk, MAX_DISK_SIZE * 2);
fallthrough; fallthrough;
case FDFMTEND: case FDFMTEND:
case FDFLUSH: case FDFLUSH:
...@@ -1962,14 +1966,50 @@ static const struct blk_mq_ops ataflop_mq_ops = { ...@@ -1962,14 +1966,50 @@ static const struct blk_mq_ops ataflop_mq_ops = {
.commit_rqs = ataflop_commit_rqs, .commit_rqs = ataflop_commit_rqs,
}; };
static struct kobject *floppy_find(dev_t dev, int *part, void *data) static int ataflop_alloc_disk(unsigned int drive, unsigned int type)
{ {
int drive = *part & 3; struct gendisk *disk;
int type = *part >> 2; int ret;
disk = alloc_disk(1);
if (!disk)
return -ENOMEM;
disk->queue = blk_mq_init_queue(&unit[drive].tag_set);
if (IS_ERR(disk->queue)) {
ret = PTR_ERR(disk->queue);
disk->queue = NULL;
put_disk(disk);
return ret;
}
disk->major = FLOPPY_MAJOR;
disk->first_minor = drive + (type << 2);
sprintf(disk->disk_name, "fd%d", drive);
disk->fops = &floppy_fops;
disk->events = DISK_EVENT_MEDIA_CHANGE;
disk->private_data = &unit[drive];
set_capacity(disk, MAX_DISK_SIZE * 2);
unit[drive].disk[type] = disk;
return 0;
}
static DEFINE_MUTEX(ataflop_probe_lock);
static void ataflop_probe(dev_t dev)
{
int drive = MINOR(dev) & 3;
int type = MINOR(dev) >> 2;
if (drive >= FD_MAX_UNITS || type > NUM_DISK_MINORS) if (drive >= FD_MAX_UNITS || type > NUM_DISK_MINORS)
return NULL; return;
*part = 0; mutex_lock(&ataflop_probe_lock);
return get_disk_and_module(unit[drive].disk); if (!unit[drive].disk[type]) {
if (ataflop_alloc_disk(drive, type) == 0)
add_disk(unit[drive].disk[type]);
}
mutex_unlock(&ataflop_probe_lock);
} }
static int __init atari_floppy_init (void) static int __init atari_floppy_init (void)
...@@ -1981,23 +2021,26 @@ static int __init atari_floppy_init (void) ...@@ -1981,23 +2021,26 @@ static int __init atari_floppy_init (void)
/* Amiga, Mac, ... don't have Atari-compatible floppy :-) */ /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
return -ENODEV; return -ENODEV;
if (register_blkdev(FLOPPY_MAJOR,"fd")) mutex_lock(&ataflop_probe_lock);
return -EBUSY; ret = __register_blkdev(FLOPPY_MAJOR, "fd", ataflop_probe);
if (ret)
goto out_unlock;
for (i = 0; i < FD_MAX_UNITS; i++) { for (i = 0; i < FD_MAX_UNITS; i++) {
unit[i].disk = alloc_disk(1); memset(&unit[i].tag_set, 0, sizeof(unit[i].tag_set));
if (!unit[i].disk) { unit[i].tag_set.ops = &ataflop_mq_ops;
ret = -ENOMEM; unit[i].tag_set.nr_hw_queues = 1;
unit[i].tag_set.nr_maps = 1;
unit[i].tag_set.queue_depth = 2;
unit[i].tag_set.numa_node = NUMA_NO_NODE;
unit[i].tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
ret = blk_mq_alloc_tag_set(&unit[i].tag_set);
if (ret)
goto err; goto err;
}
unit[i].disk->queue = blk_mq_init_sq_queue(&unit[i].tag_set, ret = ataflop_alloc_disk(i, 0);
&ataflop_mq_ops, 2, if (ret) {
BLK_MQ_F_SHOULD_MERGE); blk_mq_free_tag_set(&unit[i].tag_set);
if (IS_ERR(unit[i].disk->queue)) {
put_disk(unit[i].disk);
ret = PTR_ERR(unit[i].disk->queue);
unit[i].disk->queue = NULL;
goto err; goto err;
} }
} }
...@@ -2027,19 +2070,9 @@ static int __init atari_floppy_init (void) ...@@ -2027,19 +2070,9 @@ static int __init atari_floppy_init (void)
for (i = 0; i < FD_MAX_UNITS; i++) { for (i = 0; i < FD_MAX_UNITS; i++) {
unit[i].track = -1; unit[i].track = -1;
unit[i].flags = 0; unit[i].flags = 0;
unit[i].disk->major = FLOPPY_MAJOR; add_disk(unit[i].disk[0]);
unit[i].disk->first_minor = i;
sprintf(unit[i].disk->disk_name, "fd%d", i);
unit[i].disk->fops = &floppy_fops;
unit[i].disk->events = DISK_EVENT_MEDIA_CHANGE;
unit[i].disk->private_data = &unit[i];
set_capacity(unit[i].disk, MAX_DISK_SIZE * 2);
add_disk(unit[i].disk);
} }
blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
floppy_find, NULL, NULL);
printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n", printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n",
DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E', DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E',
UseTrackbuffer ? "" : "no "); UseTrackbuffer ? "" : "no ");
...@@ -2049,14 +2082,14 @@ static int __init atari_floppy_init (void) ...@@ -2049,14 +2082,14 @@ static int __init atari_floppy_init (void)
err: err:
while (--i >= 0) { while (--i >= 0) {
struct gendisk *disk = unit[i].disk; blk_cleanup_queue(unit[i].disk[0]->queue);
put_disk(unit[i].disk[0]);
blk_cleanup_queue(disk->queue);
blk_mq_free_tag_set(&unit[i].tag_set); blk_mq_free_tag_set(&unit[i].tag_set);
put_disk(unit[i].disk);
} }
unregister_blkdev(FLOPPY_MAJOR, "fd"); unregister_blkdev(FLOPPY_MAJOR, "fd");
out_unlock:
mutex_unlock(&ataflop_probe_lock);
return ret; return ret;
} }
...@@ -2101,13 +2134,17 @@ __setup("floppy=", atari_floppy_setup); ...@@ -2101,13 +2134,17 @@ __setup("floppy=", atari_floppy_setup);
static void __exit atari_floppy_exit(void) static void __exit atari_floppy_exit(void)
{ {
int i; int i, type;
blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
for (i = 0; i < FD_MAX_UNITS; i++) { for (i = 0; i < FD_MAX_UNITS; i++) {
del_gendisk(unit[i].disk); for (type = 0; type < NUM_DISK_MINORS; type++) {
blk_cleanup_queue(unit[i].disk->queue); if (!unit[i].disk[type])
continue;
del_gendisk(unit[i].disk[type]);
blk_cleanup_queue(unit[i].disk[type]->queue);
put_disk(unit[i].disk[type]);
}
blk_mq_free_tag_set(&unit[i].tag_set); blk_mq_free_tag_set(&unit[i].tag_set);
put_disk(unit[i].disk);
} }
unregister_blkdev(FLOPPY_MAJOR, "fd"); unregister_blkdev(FLOPPY_MAJOR, "fd");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment