2 * linux/kernel/power/user.c
4 * This file provides the user space interface for software suspend/resume.
6 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
8 * This file is released under the GPLv2.
12 #include <linux/suspend.h>
13 #include <linux/syscalls.h>
14 #include <linux/reboot.h>
15 #include <linux/string.h>
16 #include <linux/device.h>
17 #include <linux/miscdevice.h>
19 #include <linux/swap.h>
20 #include <linux/swapops.h>
23 #include <linux/console.h>
24 #include <linux/cpu.h>
25 #include <linux/freezer.h>
27 #include <asm/uaccess.h>
32 * NOTE: The SNAPSHOT_SET_SWAP_FILE and SNAPSHOT_PMOPS ioctls are obsolete and
33 * will be removed in the future. They are only preserved here for
34 * compatibility with existing userland utilities.
36 #define SNAPSHOT_SET_SWAP_FILE _IOW(SNAPSHOT_IOC_MAGIC, 10, unsigned int)
37 #define SNAPSHOT_PMOPS _IOW(SNAPSHOT_IOC_MAGIC, 12, unsigned int)
39 #define PMOPS_PREPARE 1
41 #define PMOPS_FINISH 3
44 #define SNAPSHOT_MINOR 231
46 static struct snapshot_data {
47 struct snapshot_handle handle;
52 char platform_support;
55 atomic_t snapshot_device_available = ATOMIC_INIT(1);
57 static int snapshot_open(struct inode *inode, struct file *filp)
59 struct snapshot_data *data;
61 if (!atomic_add_unless(&snapshot_device_available, -1, 0))
64 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
65 atomic_inc(&snapshot_device_available);
68 if(create_basic_memory_bitmaps()) {
69 atomic_inc(&snapshot_device_available);
72 nonseekable_open(inode, filp);
73 data = &snapshot_state;
74 filp->private_data = data;
75 memset(&data->handle, 0, sizeof(struct snapshot_handle));
76 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
77 data->swap = swsusp_resume_device ?
78 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
79 data->mode = O_RDONLY;
82 data->mode = O_WRONLY;
86 data->platform_support = 0;
91 static int snapshot_release(struct inode *inode, struct file *filp)
93 struct snapshot_data *data;
96 free_basic_memory_bitmaps();
97 data = filp->private_data;
98 free_all_swap_pages(data->swap);
100 mutex_lock(&pm_mutex);
102 mutex_unlock(&pm_mutex);
104 atomic_inc(&snapshot_device_available);
108 static ssize_t snapshot_read(struct file *filp, char __user *buf,
109 size_t count, loff_t *offp)
111 struct snapshot_data *data;
114 data = filp->private_data;
117 res = snapshot_read_next(&data->handle, count);
119 if (copy_to_user(buf, data_of(data->handle), res))
122 *offp = data->handle.offset;
127 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
128 size_t count, loff_t *offp)
130 struct snapshot_data *data;
133 data = filp->private_data;
134 res = snapshot_write_next(&data->handle, count);
136 if (copy_from_user(data_of(data->handle), buf, res))
139 *offp = data->handle.offset;
144 static int snapshot_ioctl(struct inode *inode, struct file *filp,
145 unsigned int cmd, unsigned long arg)
148 struct snapshot_data *data;
152 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
154 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
156 if (!capable(CAP_SYS_ADMIN))
159 data = filp->private_data;
163 case SNAPSHOT_FREEZE:
166 mutex_lock(&pm_mutex);
167 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
169 printk("Syncing filesystems ... ");
173 error = freeze_processes();
178 pm_notifier_call_chain(PM_POST_HIBERNATION);
179 mutex_unlock(&pm_mutex);
184 case SNAPSHOT_UNFREEZE:
185 if (!data->frozen || data->ready)
187 mutex_lock(&pm_mutex);
189 pm_notifier_call_chain(PM_POST_HIBERNATION);
190 mutex_unlock(&pm_mutex);
194 case SNAPSHOT_ATOMIC_SNAPSHOT:
195 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
199 error = hibernation_snapshot(data->platform_support);
201 error = put_user(in_suspend, (unsigned int __user *)arg);
206 case SNAPSHOT_ATOMIC_RESTORE:
207 snapshot_write_finalize(&data->handle);
208 if (data->mode != O_WRONLY || !data->frozen ||
209 !snapshot_image_loaded(&data->handle)) {
213 error = hibernation_restore(data->platform_support);
218 memset(&data->handle, 0, sizeof(struct snapshot_handle));
222 case SNAPSHOT_SET_IMAGE_SIZE:
226 case SNAPSHOT_GET_IMAGE_SIZE:
231 size = snapshot_get_image_size();
233 error = put_user(size, (loff_t __user *)arg);
236 case SNAPSHOT_AVAIL_SWAP:
237 size = count_swap_pages(data->swap, 1);
239 error = put_user(size, (loff_t __user *)arg);
242 case SNAPSHOT_GET_SWAP_PAGE:
243 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
247 offset = alloc_swapdev_block(data->swap);
249 offset <<= PAGE_SHIFT;
250 error = put_user(offset, (sector_t __user *)arg);
256 case SNAPSHOT_FREE_SWAP_PAGES:
257 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
261 free_all_swap_pages(data->swap);
264 case SNAPSHOT_SET_SWAP_FILE: /* This ioctl is deprecated */
265 if (!swsusp_swap_in_use()) {
267 * User space encodes device types as two-byte values,
268 * so we need to recode them
270 if (old_decode_dev(arg)) {
271 data->swap = swap_type_of(old_decode_dev(arg),
289 if (!mutex_trylock(&pm_mutex)) {
294 * Tasks are frozen and the notifiers have been called with
295 * PM_HIBERNATION_PREPARE
297 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
298 mutex_unlock(&pm_mutex);
301 case SNAPSHOT_PLATFORM_SUPPORT:
302 data->platform_support = !!arg;
305 case SNAPSHOT_POWER_OFF:
306 if (data->platform_support)
307 error = hibernation_platform_enter();
310 case SNAPSHOT_PMOPS: /* This ioctl is deprecated */
316 data->platform_support = 1;
321 if (data->platform_support)
322 error = hibernation_platform_enter();
326 if (data->platform_support)
331 printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
336 case SNAPSHOT_SET_SWAP_AREA:
337 if (swsusp_swap_in_use()) {
340 struct resume_swap_area swap_area;
343 error = copy_from_user(&swap_area, (void __user *)arg,
344 sizeof(struct resume_swap_area));
351 * User space encodes device types as two-byte values,
352 * so we need to recode them
354 swdev = old_decode_dev(swap_area.dev);
356 offset = swap_area.offset;
357 data->swap = swap_type_of(swdev, offset, NULL);
375 static const struct file_operations snapshot_fops = {
376 .open = snapshot_open,
377 .release = snapshot_release,
378 .read = snapshot_read,
379 .write = snapshot_write,
381 .ioctl = snapshot_ioctl,
384 static struct miscdevice snapshot_device = {
385 .minor = SNAPSHOT_MINOR,
387 .fops = &snapshot_fops,
390 static int __init snapshot_device_init(void)
392 return misc_register(&snapshot_device);
395 device_initcall(snapshot_device_init);