state of floppy write-protect switch ignored after ioctl FDFLUSH

Bruce Perens (
Tue, 26 Sep 95 12:32 PDT

Using kernel 1.2.13, I find that the state of the floppy write-protect
switch is ignored when a disk is changed after ioctl FDFLUSH, causing a
failure to write a non-write-protected floppy disk. To re-create the problem:

Place a write-protected floppy in the drive, and _read_ the
block device using "dd".

Invoke ioctl FDFLUSH. I've appended a program below to do that,
or you can use the "flushb" program, which seems to be
distributed with many Linux systems.

Place a NON-write-protected floppy in the drive, and _write_
the block device using "dd".

"dd" gets a write failure with errno set to EROFS.

Reading the disk, even one byte, causes the state of the switch to be tested
and the write will succeed after that.


Bruce Perens
The Debian Linux Distribution

* Floppy-disk buffer flush program.
* Copyright (C) 1995 Bruce Perens
* This program is free software under the GNU General Public License
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/fd.h>

static const char fd0[] = "/dev/fd0";

main(int argc, char * * argv)
const char * floppy = fd0;
int fd;

if ( argc > 1 )
floppy = argv[1];

fd = open(floppy, O_RDONLY);

if ( fd < 0 ) {
fprintf(stderr, "%s: %s.\n", floppy, strerror(errno));
return -1;

if ( ioctl(fd, FDFLUSH, 0) != 0 ) {
fprintf(stderr, "%s: fdflush: %s.\n", floppy, strerror(errno));
return -1;
return 0;

-- Attention Radio Amateurs: For information on "Linux for Hams",
-- read the WWW page, 
-- or e-mail the word "help" to