unix_io: check for read-only devices when opening R/W
When we open a device on linux, test whether it is writable
right away, rather than trying to proceed and clean up when
writes start failing.
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
diff --git a/lib/ext2fs/unix_io.c b/lib/ext2fs/unix_io.c
index d77e59d..797fce8 100644
--- a/lib/ext2fs/unix_io.c
+++ b/lib/ext2fs/unix_io.c
@@ -31,6 +31,12 @@
#ifdef __linux__
#include <sys/utsname.h>
#endif
+#ifdef HAVE_SYS_IOCTL_H
+#include <sys/ioctl.h>
+#endif
+#ifdef HAVE_SYS_MOUNT_H
+#include <sys/mount.h>
+#endif
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
@@ -41,6 +47,10 @@
#include <sys/resource.h>
#endif
+#if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
+#define BLKROGET _IO(0x12, 94) /* Get read-only status (0 = read_write). */
+#endif
+
#include "ext2_fs.h"
#include "ext2fs.h"
@@ -453,6 +463,21 @@
goto cleanup;
}
+#ifdef BLKROGET
+ if (flags & IO_FLAG_RW) {
+ int error;
+ int readonly = 0;
+
+ /* Is the block device actually writable? */
+ error = ioctl(data->dev, BLKROGET, &readonly);
+ if (!error && readonly) {
+ close(data->dev);
+ retval = EPERM;
+ goto cleanup;
+ }
+ }
+#endif
+
#ifdef __linux__
#undef RLIM_INFINITY
#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))