Whamcloud - gitweb
unix_io: check for read-only devices when opening R/W
authorEric Sandeen <sandeen@redhat.com>
Fri, 10 Oct 2008 22:17:43 +0000 (17:17 -0500)
committerTheodore Ts'o <tytso@mit.edu>
Mon, 13 Oct 2008 03:12:22 +0000 (23:12 -0400)
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>
lib/ext2fs/unix_io.c

index d77e59d..797fce8 100644 (file)
 #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
 #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 @@ static errcode_t unix_open(const char *name, int flags, io_channel *channel)
                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)))