s2disk / resume: Add grace period to allow resume device to come online

Original patch from Dominique Brazziel <dbrazziel@snet.net>
Tue, 12 Jun 2007 00:11:37 -0400 Original mail:

"I have an old system (eMachine 433MH) with a new external USB drive
(which is actually faster than the internal hard drive that came with
the box).  It takes a few seconds for the Linux to bring it online, due
in some part I think to a 5 second delay in kernel driver usb-storage
(specifically usb.c).  This console message gives a clue as to what is
happening:

usb-storage: waiting for device to settle before scanning

Once I installed uswsusp I noticed the boot process stopping because the
'stat' call only happens once (and fails), but almost immediately
afterward the drive would come online.

I worked up a patch that issues the stat call, sleeps for half a second
then checks again.  If it's successful it breaks out of the loop.  I set
the loop counter to 60 for a 30 second timeout.  It takes about 5 to 10
seconds for the drive to come online on my system but again, my system
is slow, mileage may vary."

Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
diff --git a/resume.c b/resume.c
index 2876cb3..a8685f2 100644
--- a/resume.c
+++ b/resume.c
@@ -35,6 +35,9 @@
 #include "splash.h"
 #include "loglevel.h"
 
+#define STAT_TIMEOUT_US 	1000000		// 1 tenth of a second
+#define STAT_LOOP_STEPS		300		// 30 seconds
+
 static char snapshot_dev_name[MAX_STR_LEN] = SNAPSHOT_DEVICE;
 static char resume_dev_name[MAX_STR_LEN] = RESUME_DEVICE;
 static loff_t resume_offset;
@@ -469,6 +472,17 @@
 	orig_loglevel = get_kernel_console_loglevel();
 	set_kernel_console_loglevel(suspend_loglevel);
 
+	/*
+	* 30 seconds grace period to allow resume device
+	* to come online (i.e. external USB drive)
+	*/
+	for (n = 0; n < STAT_LOOP_STEPS; n++) {
+	    if (!stat(resume_dev_name, &stat_buf))
+		break;
+	    usleep(STAT_TIMEOUT_US);
+	}
+
+
 	while (stat(resume_dev_name, &stat_buf)) {
 		fprintf(stderr, 
 			"%s: Could not stat the resume device file '%s'\n"