Add --readonly option

Suggested by Valerie Henson. It can be used as an extra safety guard
against accidentically turning on a write setting. See README.

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
diff --git a/README b/README
index 76aa22d..b7fc96b 100644
--- a/README
+++ b/README
@@ -71,12 +71,19 @@
 	--help		Print this page
 	--cmdhelp=cmd	Print command help, "all" for all of them
 	--showcmd	Turn a job file into command line options
+	--readonly	Turn on safety read-only checks
 
 Any parameters following the options will be assumed to be job files,
 unless they match a job file parameter. You can add as many as you want,
 each job file will be regarded as a separate group and fio will stonewall
 its execution.
 
+The --readonly switch is an extra safety guard to prevent accidentically
+turning on a write setting when that is not desired. Fio will only write
+if rw=write/randwrite/rw/randrw is given, but this extra safety net can
+be used as an extra precaution. It will also enable a write check in the
+io engine core to prevent an accidental write due to a fio bug.
+
 
 Job file
 --------
diff --git a/init.c b/init.c
index f3cb9ad..2a1ee14 100644
--- a/init.c
+++ b/init.c
@@ -24,6 +24,7 @@
 static char **ini_file;
 static int max_jobs = MAX_JOBS;
 static int dump_cmdline;
+static int read_only;
 
 struct thread_data def_thread;
 struct thread_data *threads = NULL;
@@ -89,7 +90,12 @@
 	{
 		.name		= "showcmd",
 		.has_arg	= no_argument,
-		.val		= 's'
+		.val		= 's',
+	},
+	{
+		.name		= "readonly",
+		.has_arg	= no_argument,
+		.val		= 'r',
 	},
 	{
 		.name		= NULL,
@@ -180,6 +186,11 @@
 {
 	struct thread_options *o = &td->o;
 
+	if (read_only && td_write(td)) {
+		log_err("fio: job <%s> has write bit set, but fio is in read-only mode\n", td->o.name);
+		return 1;
+	}
+	
 	if (o->rwmix[DDIR_READ] + o->rwmix[DDIR_WRITE] > 100)
 		o->rwmix[DDIR_WRITE] = 100 - o->rwmix[DDIR_READ];
 
@@ -768,6 +779,9 @@
 		case 's':
 			dump_cmdline = 1;
 			break;
+		case 'r':
+			read_only = 1;
+			break;
 		case 'v':
 			printf("%s\n", fio_version_string);
 			exit(0);
diff --git a/ioengines.c b/ioengines.c
index 8cdabe6..8e6fae2 100644
--- a/ioengines.c
+++ b/ioengines.c
@@ -187,6 +187,8 @@
 
 	assert(io_u->file->flags & FIO_FILE_OPEN);
 
+	assert(!(io_u->ddir == DDIR_WRITE && !td_write(td)));
+
 	io_u->error = 0;
 	io_u->resid = 0;