Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / kernel / power / disk.c
index 02b6764..81d4d98 100644 (file)
 #include <linux/device.h>
 #include <linux/delay.h>
 #include <linux/fs.h>
-#include "power.h"
-
-
-extern suspend_disk_method_t pm_disk_mode;
-extern struct pm_ops * pm_ops;
+#include <linux/mount.h>
+#include <linux/pm.h>
 
-extern int swsusp_suspend(void);
-extern int swsusp_write(void);
-extern int swsusp_check(void);
-extern int swsusp_read(void);
-extern void swsusp_close(void);
-extern int swsusp_resume(void);
-extern int swsusp_free(void);
+#include "power.h"
 
 
 static int noresume = 0;
@@ -47,64 +38,27 @@ dev_t swsusp_resume_device;
 
 static void power_down(suspend_disk_method_t mode)
 {
-       unsigned long flags;
        int error = 0;
 
-       local_irq_save(flags);
        switch(mode) {
        case PM_DISK_PLATFORM:
-               device_shutdown();
+               kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
                error = pm_ops->enter(PM_SUSPEND_DISK);
                break;
        case PM_DISK_SHUTDOWN:
-               printk("Powering off system\n");
-               device_shutdown();
-               machine_power_off();
+               kernel_power_off();
                break;
        case PM_DISK_REBOOT:
-               device_shutdown();
-               machine_restart(NULL);
+               kernel_restart(NULL);
                break;
        }
-       machine_halt();
+       kernel_halt();
        /* Valid image is on the disk, if we continue we risk serious data corruption
           after resume. */
        printk(KERN_CRIT "Please power me down manually\n");
        while(1);
 }
 
-
-static int in_suspend __nosavedata = 0;
-
-
-/**
- *     free_some_memory -  Try to free as much memory as possible
- *
- *     ... but do not OOM-kill anyone
- *
- *     Notice: all userland should be stopped at this point, or
- *     livelock is possible.
- */
-
-static void free_some_memory(void)
-{
-       unsigned int i = 0;
-       unsigned int tmp;
-       unsigned long pages = 0;
-       char *p = "-\\|/";
-
-       printk("Freeing memory...  ");
-       while ((tmp = shrink_all_memory(10000))) {
-               pages += tmp;
-               printk("\b%c", p[i]);
-               i++;
-               if (i > 3)
-                       i = 0;
-       }
-       printk("\bdone (%li pages freed)\n", pages);
-}
-
-
 static inline void platform_finish(void)
 {
        if (pm_disk_mode == PM_DISK_PLATFORM) {
@@ -113,64 +67,36 @@ static inline void platform_finish(void)
        }
 }
 
-static void finish(void)
-{
-       device_resume();
-       platform_finish();
-       enable_nonboot_cpus();
-       thaw_processes();
-       pm_restore_console();
-}
-
-
 static int prepare_processes(void)
 {
        int error;
 
        pm_prepare_console();
-
-       sys_sync();
+       disable_nonboot_cpus();
 
        if (freeze_processes()) {
                error = -EBUSY;
-               return error;
-       }
-
-       if (pm_disk_mode == PM_DISK_PLATFORM) {
-               if (pm_ops && pm_ops->prepare) {
-                       if ((error = pm_ops->prepare(PM_SUSPEND_DISK)))
-                               return error;
-               }
+               goto thaw;
        }
 
        /* Free memory before shutting down devices. */
-       free_some_memory();
-
-       return 0;
+       if (!(error = swsusp_shrink_memory()))
+               return 0;
+thaw:
+       thaw_processes();
+       enable_nonboot_cpus();
+       pm_restore_console();
+       return error;
 }
 
 static void unprepare_processes(void)
 {
-       enable_nonboot_cpus();
+       platform_finish();
        thaw_processes();
+       enable_nonboot_cpus();
        pm_restore_console();
 }
 
-static int prepare_devices(void)
-{
-       int error;
-
-       disable_nonboot_cpus();
-       if ((error = device_suspend(PMSG_FREEZE))) {
-               printk("Some devices failed to suspend\n");
-               platform_finish();
-               enable_nonboot_cpus();
-               return error;
-       }
-
-       return 0;
-}
-
 /**
  *     pm_suspend_disk - The granpappy of power management.
  *
@@ -185,34 +111,39 @@ int pm_suspend_disk(void)
        int error;
 
        error = prepare_processes();
-       if (!error) {
-               error = prepare_devices();
-       }
+       if (error)
+               return error;
 
+       error = device_suspend(PMSG_FREEZE);
        if (error) {
+               printk("Some devices failed to suspend\n");
                unprepare_processes();
                return error;
        }
 
-       pr_debug("PM: Attempting to suspend to disk.\n");
-       if (pm_disk_mode == PM_DISK_FIRMWARE)
-               return pm_ops->enter(PM_SUSPEND_DISK);
-
        pr_debug("PM: snapshotting memory.\n");
        in_suspend = 1;
        if ((error = swsusp_suspend()))
                goto Done;
 
        if (in_suspend) {
+               device_resume();
                pr_debug("PM: writing image.\n");
                error = swsusp_write();
                if (!error)
                        power_down(pm_disk_mode);
+               else {
+                       swsusp_free();
+                       unprepare_processes();
+                       return error;
+               }
        } else
                pr_debug("PM: Image restored successfully.\n");
+
        swsusp_free();
  Done:
-       finish();
+       device_resume();
+       unprepare_processes();
        return error;
 }
 
@@ -233,11 +164,25 @@ static int software_resume(void)
 {
        int error;
 
+       down(&pm_sem);
+       if (!swsusp_resume_device) {
+               if (!strlen(resume_file)) {
+                       up(&pm_sem);
+                       return -ENOENT;
+               }
+               swsusp_resume_device = name_to_dev_t(resume_file);
+               pr_debug("swsusp: Resume From Partition %s\n", resume_file);
+       } else {
+               pr_debug("swsusp: Resume From Partition %d:%d\n",
+                        MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
+       }
+
        if (noresume) {
                /**
                 * FIXME: If noresume is specified, we need to find the partition
                 * and reset it back to normal swap space.
                 */
+               up(&pm_sem);
                return 0;
        }
 
@@ -250,30 +195,35 @@ static int software_resume(void)
 
        if ((error = prepare_processes())) {
                swsusp_close();
-               goto Cleanup;
+               goto Done;
        }
 
        pr_debug("PM: Reading swsusp image.\n");
 
-       if ((error = swsusp_read()))
-               goto Cleanup;
+       if ((error = swsusp_read())) {
+               swsusp_free();
+               goto Thaw;
+       }
 
        pr_debug("PM: Preparing devices for restore.\n");
 
-       if ((error = prepare_devices()))
-               goto Free;
+       if ((error = device_suspend(PMSG_FREEZE))) {
+               printk("Some devices failed to suspend\n");
+               swsusp_free();
+               goto Thaw;
+       }
 
        mb();
 
        pr_debug("PM: Restoring saved image.\n");
        swsusp_resume();
        pr_debug("PM: Restore failed, recovering.n");
-       finish();
- Free:
-       swsusp_free();
- Cleanup:
+       device_resume();
+ Thaw:
        unprepare_processes();
  Done:
+       /* For success case, the suspend path will release the lock */
+       up(&pm_sem);
        pr_debug("PM: Resume from disk failed.\n");
        return 0;
 }
@@ -366,35 +316,55 @@ static ssize_t resume_show(struct subsystem * subsys, char *buf)
                       MINOR(swsusp_resume_device));
 }
 
-static ssize_t resume_store(struct subsystem * subsys, const char * buf, size_t n)
+static ssize_t resume_store(struct subsystem *subsys, const char *buf, size_t n)
 {
-       int len;
-       char *p;
        unsigned int maj, min;
-       int error = -EINVAL;
        dev_t res;
+       int ret = -EINVAL;
 
-       p = memchr(buf, '\n', n);
-       len = p ? p - buf : n;
+       if (sscanf(buf, "%u:%u", &maj, &min) != 2)
+               goto out;
 
-       if (sscanf(buf, "%u:%u", &maj, &min) == 2) {
-               res = MKDEV(maj,min);
-               if (maj == MAJOR(res) && min == MINOR(res)) {
-                       swsusp_resume_device = res;
-                       printk("Attempting manual resume\n");
-                       noresume = 0;
-                       software_resume();
-               }
-       }
+       res = MKDEV(maj,min);
+       if (maj != MAJOR(res) || min != MINOR(res))
+               goto out;
 
-       return error >= 0 ? n : error;
+       down(&pm_sem);
+       swsusp_resume_device = res;
+       up(&pm_sem);
+       printk("Attempting manual resume\n");
+       noresume = 0;
+       software_resume();
+       ret = n;
+out:
+       return ret;
 }
 
 power_attr(resume);
 
+static ssize_t image_size_show(struct subsystem * subsys, char *buf)
+{
+       return sprintf(buf, "%lu\n", image_size);
+}
+
+static ssize_t image_size_store(struct subsystem * subsys, const char * buf, size_t n)
+{
+       unsigned long size;
+
+       if (sscanf(buf, "%lu", &size) == 1) {
+               image_size = size;
+               return n;
+       }
+
+       return -EINVAL;
+}
+
+power_attr(image_size);
+
 static struct attribute * g[] = {
        &disk_attr.attr,
        &resume_attr.attr,
+       &image_size_attr.attr,
        NULL,
 };