blktrace: load improvements

- Set the state explicitly to SETTING_UP when loading the
  traces. This could take a while, so tell the status thread
  that we are setting up so it knows how to display us
  correctly.

- Fix a bug with multiple devices where we didn't cache the
  major/minor properly. This made blktrace spend tons of CPU
  cycles looking up /sys.

- Handle multiple devices correctly when adding files.

Signed-off-by: Jens Axboe <axboe@fb.com>
diff --git a/blktrace.c b/blktrace.c
index 9e4e599..107a65b 100644
--- a/blktrace.c
+++ b/blktrace.c
@@ -217,6 +217,8 @@
 
 		dprint(FD_BLKTRACE, "add devices %s\n", dev);
 		fileno = add_file_exclusive(td, dev);
+		td->files[fileno]->major = maj;
+		td->files[fileno]->minor = min;
 		trace_add_open_close_event(td, fileno, FIO_LOG_OPEN_FILE);
 		last_fileno = fileno;
 	}
@@ -369,7 +371,7 @@
 	unsigned int cpu;
 	unsigned int rw_bs[2];
 	struct fifo *fifo;
-	int fd, i;
+	int fd, i, old_state;
 	struct fio_file *f;
 
 	fd = open(filename, O_RDONLY);
@@ -380,6 +382,9 @@
 
 	fifo = fifo_alloc(TRACE_FIFO_SIZE);
 
+	old_state = td->runstate;
+	td_set_runstate(td, TD_SETTING_UP);
+
 	td->o.size = 0;
 
 	cpu = 0;
@@ -458,6 +463,8 @@
 	fifo_free(fifo);
 	close(fd);
 
+	td_set_runstate(td, old_state);
+
 	if (!td->files_index) {
 		log_err("fio: did not find replay device(s)\n");
 		return 1;
diff --git a/engines/net.c b/engines/net.c
index dd06861..1df8d06 100644
--- a/engines/net.c
+++ b/engines/net.c
@@ -1194,7 +1194,7 @@
 	struct netio_data *nd;
 
 	if (!td->files_index) {
-		add_file(td, td->o.filename ?: "net", 0);
+		add_file(td, td->o.filename ?: "net", 0, 0);
 		td->o.nr_files = td->o.nr_files ?: 1;
 	}
 
diff --git a/engines/rbd.c b/engines/rbd.c
index 39fa0ce..5a4a3e2 100644
--- a/engines/rbd.c
+++ b/engines/rbd.c
@@ -377,7 +377,7 @@
 	 * The size of the RBD is set instead of a artificial file.
 	 */
 	if (!td->files_index) {
-		add_file(td, td->o.filename ? : "rbd", 0);
+		add_file(td, td->o.filename ? : "rbd", 0, 0);
 		td->o.nr_files = td->o.nr_files ? : 1;
 	}
 	f = td->files[0];
diff --git a/file.h b/file.h
index d065a25..c929d1d 100644
--- a/file.h
+++ b/file.h
@@ -167,7 +167,7 @@
 extern int __must_check generic_get_file_size(struct thread_data *, struct fio_file *);
 extern int __must_check file_lookup_open(struct fio_file *f, int flags);
 extern int __must_check pre_read_files(struct thread_data *);
-extern int add_file(struct thread_data *, const char *, int);
+extern int add_file(struct thread_data *, const char *, int, int);
 extern int add_file_exclusive(struct thread_data *, const char *);
 extern void get_file(struct fio_file *);
 extern int __must_check put_file(struct thread_data *, struct fio_file *);
diff --git a/filesetup.c b/filesetup.c
index 7669d70..1acf6ad 100644
--- a/filesetup.c
+++ b/filesetup.c
@@ -1145,7 +1145,7 @@
 	}
 }
 
-int add_file(struct thread_data *td, const char *fname, int numjob)
+int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
 {
 	int cur_files = td->files_index;
 	char file_name[PATH_MAX];
@@ -1237,6 +1237,9 @@
 	if (!td->o.open_files)
 		td->o.open_files = 1;
 
+	if (inc)
+		td->o.nr_files++;
+
 	dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
 							cur_files);
 
@@ -1253,7 +1256,7 @@
 			return i;
 	}
 
-	return add_file(td, fname, 0);
+	return add_file(td, fname, 0, 1);
 }
 
 void get_file(struct fio_file *f)
@@ -1362,8 +1365,7 @@
 		}
 
 		if (S_ISREG(sb.st_mode)) {
-			add_file(td, full_path, 0);
-			td->o.nr_files++;
+			add_file(td, full_path, 0, 1);
 			continue;
 		}
 		if (!S_ISDIR(sb.st_mode))
diff --git a/init.c b/init.c
index 6a65e55..73ec9eb 100644
--- a/init.c
+++ b/init.c
@@ -1026,10 +1026,10 @@
 		file_alloced = 1;
 
 		if (o->nr_files == 1 && exists_and_not_file(jobname))
-			add_file(td, jobname, job_add_num);
+			add_file(td, jobname, job_add_num, 0);
 		else {
 			for (i = 0; i < o->nr_files; i++)
-				add_file(td, make_filename(fname, o, jobname, job_add_num, i), job_add_num);
+				add_file(td, make_filename(fname, o, jobname, job_add_num, i), job_add_num, 0);
 		}
 	}
 
diff --git a/iolog.c b/iolog.c
index eeaca29..b8ee067 100644
--- a/iolog.c
+++ b/iolog.c
@@ -323,8 +323,7 @@
 		} else if (r == 2) {
 			rw = DDIR_INVAL;
 			if (!strcmp(act, "add")) {
-				td->o.nr_files++;
-				fileno = add_file(td, fname, 0);
+				fileno = add_file(td, fname, 0, 1);
 				file_action = FIO_LOG_ADD_FILE;
 				continue;
 			} else if (!strcmp(act, "open")) {
diff --git a/options.c b/options.c
index 625d3a2..6d3956e 100644
--- a/options.c
+++ b/options.c
@@ -827,8 +827,7 @@
 	while ((fname = get_next_name(&str)) != NULL) {
 		if (!strlen(fname))
 			break;
-		add_file(td, fname, 0);
-		td->o.nr_files++;
+		add_file(td, fname, 0, 1);
 	}
 
 	free(p);