Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-rc-fixes-2.6
[sfrench/cifs-2.6.git] / kernel / power / user.c
index d65305b515b1ec8c359532b3f29812178fbbb6ad..f5512cb3aa86f61e98caec04c82a01a0dc9f6dcf 100644 (file)
 
 #include "power.h"
 
+/*
+ * NOTE: The SNAPSHOT_SET_SWAP_FILE and SNAPSHOT_PMOPS ioctls are obsolete and
+ * will be removed in the future.  They are only preserved here for
+ * compatibility with existing userland utilities.
+ */
+#define SNAPSHOT_SET_SWAP_FILE _IOW(SNAPSHOT_IOC_MAGIC, 10, unsigned int)
+#define SNAPSHOT_PMOPS         _IOW(SNAPSHOT_IOC_MAGIC, 12, unsigned int)
+
+#define PMOPS_PREPARE  1
+#define PMOPS_ENTER    2
+#define PMOPS_FINISH   3
+
+/*
+ * NOTE: The following ioctl definitions are wrong and have been replaced with
+ * correct ones.  They are only preserved here for compatibility with existing
+ * userland utilities and will be removed in the future.
+ */
+#define SNAPSHOT_ATOMIC_SNAPSHOT       _IOW(SNAPSHOT_IOC_MAGIC, 3, void *)
+#define SNAPSHOT_SET_IMAGE_SIZE                _IOW(SNAPSHOT_IOC_MAGIC, 6, unsigned long)
+#define SNAPSHOT_AVAIL_SWAP            _IOR(SNAPSHOT_IOC_MAGIC, 7, void *)
+#define SNAPSHOT_GET_SWAP_PAGE         _IOR(SNAPSHOT_IOC_MAGIC, 8, void *)
+
+
 #define SNAPSHOT_MINOR 231
 
 static struct snapshot_data {
@@ -36,7 +59,7 @@ static struct snapshot_data {
        int mode;
        char frozen;
        char ready;
-       char platform_suspend;
+       char platform_support;
 } snapshot_state;
 
 atomic_t snapshot_device_available = ATOMIC_INIT(1);
@@ -44,6 +67,7 @@ atomic_t snapshot_device_available = ATOMIC_INIT(1);
 static int snapshot_open(struct inode *inode, struct file *filp)
 {
        struct snapshot_data *data;
+       int error;
 
        if (!atomic_add_unless(&snapshot_device_available, -1, 0))
                return -EBUSY;
@@ -64,13 +88,23 @@ static int snapshot_open(struct inode *inode, struct file *filp)
                data->swap = swsusp_resume_device ?
                        swap_type_of(swsusp_resume_device, 0, NULL) : -1;
                data->mode = O_RDONLY;
+               error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
+               if (error)
+                       pm_notifier_call_chain(PM_POST_RESTORE);
        } else {
                data->swap = -1;
                data->mode = O_WRONLY;
+               error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
+               if (error)
+                       pm_notifier_call_chain(PM_POST_HIBERNATION);
+       }
+       if (error) {
+               atomic_inc(&snapshot_device_available);
+               return error;
        }
        data->frozen = 0;
        data->ready = 0;
-       data->platform_suspend = 0;
+       data->platform_support = 0;
 
        return 0;
 }
@@ -88,6 +122,8 @@ static int snapshot_release(struct inode *inode, struct file *filp)
                thaw_processes();
                mutex_unlock(&pm_mutex);
        }
+       pm_notifier_call_chain(data->mode == O_WRONLY ?
+                       PM_POST_HIBERNATION : PM_POST_RESTORE);
        atomic_inc(&snapshot_device_available);
        return 0;
 }
@@ -128,98 +164,12 @@ static ssize_t snapshot_write(struct file *filp, const char __user *buf,
        return res;
 }
 
-static inline int platform_prepare(void)
-{
-       int error = 0;
-
-       if (hibernation_ops)
-               error = hibernation_ops->prepare();
-
-       return error;
-}
-
-static inline void platform_finish(void)
-{
-       if (hibernation_ops)
-               hibernation_ops->finish();
-}
-
-static inline int snapshot_suspend(int platform_suspend)
-{
-       int error;
-
-       mutex_lock(&pm_mutex);
-       /* Free memory before shutting down devices. */
-       error = swsusp_shrink_memory();
-       if (error)
-               goto Finish;
-
-       if (platform_suspend) {
-               error = platform_prepare();
-               if (error)
-                       goto Finish;
-       }
-       suspend_console();
-       error = device_suspend(PMSG_FREEZE);
-       if (error)
-               goto Resume_devices;
-
-       error = disable_nonboot_cpus();
-       if (!error) {
-               in_suspend = 1;
-               error = swsusp_suspend();
-       }
-       enable_nonboot_cpus();
- Resume_devices:
-       if (platform_suspend)
-               platform_finish();
-
-       device_resume();
-       resume_console();
- Finish:
-       mutex_unlock(&pm_mutex);
-       return error;
-}
-
-static inline int snapshot_restore(int platform_suspend)
-{
-       int error;
-
-       mutex_lock(&pm_mutex);
-       pm_prepare_console();
-       if (platform_suspend) {
-               error = platform_prepare();
-               if (error)
-                       goto Finish;
-       }
-       suspend_console();
-       error = device_suspend(PMSG_PRETHAW);
-       if (error)
-               goto Resume_devices;
-
-       error = disable_nonboot_cpus();
-       if (!error)
-               error = swsusp_resume();
-
-       enable_nonboot_cpus();
- Resume_devices:
-       if (platform_suspend)
-               platform_finish();
-
-       device_resume();
-       resume_console();
- Finish:
-       pm_restore_console();
-       mutex_unlock(&pm_mutex);
-       return error;
-}
-
 static int snapshot_ioctl(struct inode *inode, struct file *filp,
                           unsigned int cmd, unsigned long arg)
 {
        int error = 0;
        struct snapshot_data *data;
-       loff_t avail;
+       loff_t size;
        sector_t offset;
 
        if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
@@ -237,10 +187,13 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp,
                if (data->frozen)
                        break;
                mutex_lock(&pm_mutex);
-               if (freeze_processes()) {
+               printk("Syncing filesystems ... ");
+               sys_sync();
+               printk("done.\n");
+
+               error = freeze_processes();
+               if (error)
                        thaw_processes();
-                       error = -EBUSY;
-               }
                mutex_unlock(&pm_mutex);
                if (!error)
                        data->frozen = 1;
@@ -255,14 +208,15 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp,
                data->frozen = 0;
                break;
 
+       case SNAPSHOT_CREATE_IMAGE:
        case SNAPSHOT_ATOMIC_SNAPSHOT:
                if (data->mode != O_RDONLY || !data->frozen  || data->ready) {
                        error = -EPERM;
                        break;
                }
-               error = snapshot_suspend(data->platform_suspend);
+               error = hibernation_snapshot(data->platform_support);
                if (!error)
-                       error = put_user(in_suspend, (unsigned int __user *)arg);
+                       error = put_user(in_suspend, (int __user *)arg);
                if (!error)
                        data->ready = 1;
                break;
@@ -274,7 +228,7 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp,
                        error = -EPERM;
                        break;
                }
-               error = snapshot_restore(data->platform_suspend);
+               error = hibernation_restore(data->platform_support);
                break;
 
        case SNAPSHOT_FREE:
@@ -283,16 +237,29 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp,
                data->ready = 0;
                break;
 
+       case SNAPSHOT_PREF_IMAGE_SIZE:
        case SNAPSHOT_SET_IMAGE_SIZE:
                image_size = arg;
                break;
 
+       case SNAPSHOT_GET_IMAGE_SIZE:
+               if (!data->ready) {
+                       error = -ENODATA;
+                       break;
+               }
+               size = snapshot_get_image_size();
+               size <<= PAGE_SHIFT;
+               error = put_user(size, (loff_t __user *)arg);
+               break;
+
+       case SNAPSHOT_AVAIL_SWAP_SIZE:
        case SNAPSHOT_AVAIL_SWAP:
-               avail = count_swap_pages(data->swap, 1);
-               avail <<= PAGE_SHIFT;
-               error = put_user(avail, (loff_t __user *)arg);
+               size = count_swap_pages(data->swap, 1);
+               size <<= PAGE_SHIFT;
+               error = put_user(size, (loff_t __user *)arg);
                break;
 
+       case SNAPSHOT_ALLOC_SWAP_PAGE:
        case SNAPSHOT_GET_SWAP_PAGE:
                if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
                        error = -ENODEV;
@@ -301,7 +268,7 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp,
                offset = alloc_swapdev_block(data->swap);
                if (offset) {
                        offset <<= PAGE_SHIFT;
-                       error = put_user(offset, (sector_t __user *)arg);
+                       error = put_user(offset, (loff_t __user *)arg);
                } else {
                        error = -ENOSPC;
                }
@@ -315,7 +282,7 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp,
                free_all_swap_pages(data->swap);
                break;
 
-       case SNAPSHOT_SET_SWAP_FILE:
+       case SNAPSHOT_SET_SWAP_FILE: /* This ioctl is deprecated */
                if (!swsusp_swap_in_use()) {
                        /*
                         * User space encodes device types as two-byte values,
@@ -336,75 +303,49 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp,
                break;
 
        case SNAPSHOT_S2RAM:
-               if (!pm_ops) {
-                       error = -ENOSYS;
-                       break;
-               }
-
                if (!data->frozen) {
                        error = -EPERM;
                        break;
                }
-
                if (!mutex_trylock(&pm_mutex)) {
                        error = -EBUSY;
                        break;
                }
+               /*
+                * Tasks are frozen and the notifiers have been called with
+                * PM_HIBERNATION_PREPARE
+                */
+               error = suspend_devices_and_enter(PM_SUSPEND_MEM);
+               mutex_unlock(&pm_mutex);
+               break;
 
-               if (pm_ops->prepare) {
-                       error = pm_ops->prepare(PM_SUSPEND_MEM);
-                       if (error)
-                               goto OutS3;
-               }
-
-               /* Put devices to sleep */
-               suspend_console();
-               error = device_suspend(PMSG_SUSPEND);
-               if (error) {
-                       printk(KERN_ERR "Failed to suspend some devices.\n");
-               } else {
-                       error = disable_nonboot_cpus();
-                       if (!error) {
-                               /* Enter S3, system is already frozen */
-                               suspend_enter(PM_SUSPEND_MEM);
-                               enable_nonboot_cpus();
-                       }
-                       /* Wake up devices */
-                       device_resume();
-               }
-               resume_console();
-               if (pm_ops->finish)
-                       pm_ops->finish(PM_SUSPEND_MEM);
+       case SNAPSHOT_PLATFORM_SUPPORT:
+               data->platform_support = !!arg;
+               break;
 
- OutS3:
-               mutex_unlock(&pm_mutex);
+       case SNAPSHOT_POWER_OFF:
+               if (data->platform_support)
+                       error = hibernation_platform_enter();
                break;
 
-       case SNAPSHOT_PMOPS:
+       case SNAPSHOT_PMOPS: /* This ioctl is deprecated */
                error = -EINVAL;
 
                switch (arg) {
 
                case PMOPS_PREPARE:
-                       if (hibernation_ops) {
-                               data->platform_suspend = 1;
-                               error = 0;
-                       } else {
-                               error = -ENOSYS;
-                       }
+                       data->platform_support = 1;
+                       error = 0;
                        break;
 
                case PMOPS_ENTER:
-                       if (data->platform_suspend) {
-                               kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
-                               error = hibernation_ops->enter();
-                       }
+                       if (data->platform_support)
+                               error = hibernation_platform_enter();
                        break;
 
                case PMOPS_FINISH:
-                       if (data->platform_suspend)
+                       if (data->platform_support)
                                error = 0;
-
                        break;
 
                default: