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 * NOTE: The following ioctl definitions are wrong and have been replaced with
45 * correct ones. They are only preserved here for compatibility with existing
46 * userland utilities and will be removed in the future.
48 #define SNAPSHOT_ATOMIC_SNAPSHOT _IOW(SNAPSHOT_IOC_MAGIC, 3, void *)
49 #define SNAPSHOT_SET_IMAGE_SIZE _IOW(SNAPSHOT_IOC_MAGIC, 6, unsigned long)
50 #define SNAPSHOT_AVAIL_SWAP _IOR(SNAPSHOT_IOC_MAGIC, 7, void *)
51 #define SNAPSHOT_GET_SWAP_PAGE _IOR(SNAPSHOT_IOC_MAGIC, 8, void *)
54 #define SNAPSHOT_MINOR 231
56 static struct snapshot_data {
57 struct snapshot_handle handle;
62 char platform_support;
65 atomic_t snapshot_device_available = ATOMIC_INIT(1);
67 static int snapshot_open(struct inode *inode, struct file *filp)
69 struct snapshot_data *data;
71 if (!atomic_add_unless(&snapshot_device_available, -1, 0))
74 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
75 atomic_inc(&snapshot_device_available);
78 if(create_basic_memory_bitmaps()) {
79 atomic_inc(&snapshot_device_available);
82 nonseekable_open(inode, filp);
83 data = &snapshot_state;
84 filp->private_data = data;
85 memset(&data->handle, 0, sizeof(struct snapshot_handle));
86 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
87 data->swap = swsusp_resume_device ?
88 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
89 data->mode = O_RDONLY;
92 data->mode = O_WRONLY;
96 data->platform_support = 0;
101 static int snapshot_release(struct inode *inode, struct file *filp)
103 struct snapshot_data *data;
106 free_basic_memory_bitmaps();
107 data = filp->private_data;
108 free_all_swap_pages(data->swap);
110 mutex_lock(&pm_mutex);
112 mutex_unlock(&pm_mutex);
114 atomic_inc(&snapshot_device_available);
118 static ssize_t snapshot_read(struct file *filp, char __user *buf,
119 size_t count, loff_t *offp)
121 struct snapshot_data *data;
124 data = filp->private_data;
127 res = snapshot_read_next(&data->handle, count);
129 if (copy_to_user(buf, data_of(data->handle), res))
132 *offp = data->handle.offset;
137 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
138 size_t count, loff_t *offp)
140 struct snapshot_data *data;
143 data = filp->private_data;
144 res = snapshot_write_next(&data->handle, count);
146 if (copy_from_user(data_of(data->handle), buf, res))
149 *offp = data->handle.offset;
154 static int snapshot_ioctl(struct inode *inode, struct file *filp,
155 unsigned int cmd, unsigned long arg)
158 struct snapshot_data *data;
162 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
164 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
166 if (!capable(CAP_SYS_ADMIN))
169 data = filp->private_data;
173 case SNAPSHOT_FREEZE:
176 mutex_lock(&pm_mutex);
177 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
179 printk("Syncing filesystems ... ");
183 error = freeze_processes();
188 pm_notifier_call_chain(PM_POST_HIBERNATION);
189 mutex_unlock(&pm_mutex);
194 case SNAPSHOT_UNFREEZE:
195 if (!data->frozen || data->ready)
197 mutex_lock(&pm_mutex);
199 pm_notifier_call_chain(PM_POST_HIBERNATION);
200 mutex_unlock(&pm_mutex);
204 case SNAPSHOT_CREATE_IMAGE:
205 case SNAPSHOT_ATOMIC_SNAPSHOT:
206 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
210 error = hibernation_snapshot(data->platform_support);
212 error = put_user(in_suspend, (int __user *)arg);
217 case SNAPSHOT_ATOMIC_RESTORE:
218 snapshot_write_finalize(&data->handle);
219 if (data->mode != O_WRONLY || !data->frozen ||
220 !snapshot_image_loaded(&data->handle)) {
224 error = hibernation_restore(data->platform_support);
229 memset(&data->handle, 0, sizeof(struct snapshot_handle));
233 case SNAPSHOT_PREF_IMAGE_SIZE:
234 case SNAPSHOT_SET_IMAGE_SIZE:
238 case SNAPSHOT_GET_IMAGE_SIZE:
243 size = snapshot_get_image_size();
245 error = put_user(size, (loff_t __user *)arg);
248 case SNAPSHOT_AVAIL_SWAP_SIZE:
249 case SNAPSHOT_AVAIL_SWAP:
250 size = count_swap_pages(data->swap, 1);
252 error = put_user(size, (loff_t __user *)arg);
255 case SNAPSHOT_ALLOC_SWAP_PAGE:
256 case SNAPSHOT_GET_SWAP_PAGE:
257 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
261 offset = alloc_swapdev_block(data->swap);
263 offset <<= PAGE_SHIFT;
264 error = put_user(offset, (loff_t __user *)arg);
270 case SNAPSHOT_FREE_SWAP_PAGES:
271 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
275 free_all_swap_pages(data->swap);
278 case SNAPSHOT_SET_SWAP_FILE: /* This ioctl is deprecated */
279 if (!swsusp_swap_in_use()) {
281 * User space encodes device types as two-byte values,
282 * so we need to recode them
284 if (old_decode_dev(arg)) {
285 data->swap = swap_type_of(old_decode_dev(arg),
303 if (!mutex_trylock(&pm_mutex)) {
308 * Tasks are frozen and the notifiers have been called with
309 * PM_HIBERNATION_PREPARE
311 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
312 mutex_unlock(&pm_mutex);
315 case SNAPSHOT_PLATFORM_SUPPORT:
316 data->platform_support = !!arg;
319 case SNAPSHOT_POWER_OFF:
320 if (data->platform_support)
321 error = hibernation_platform_enter();
324 case SNAPSHOT_PMOPS: /* This ioctl is deprecated */
330 data->platform_support = 1;
335 if (data->platform_support)
336 error = hibernation_platform_enter();
340 if (data->platform_support)
345 printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
350 case SNAPSHOT_SET_SWAP_AREA:
351 if (swsusp_swap_in_use()) {
354 struct resume_swap_area swap_area;
357 error = copy_from_user(&swap_area, (void __user *)arg,
358 sizeof(struct resume_swap_area));
365 * User space encodes device types as two-byte values,
366 * so we need to recode them
368 swdev = old_decode_dev(swap_area.dev);
370 offset = swap_area.offset;
371 data->swap = swap_type_of(swdev, offset, NULL);
389 static const struct file_operations snapshot_fops = {
390 .open = snapshot_open,
391 .release = snapshot_release,
392 .read = snapshot_read,
393 .write = snapshot_write,
395 .ioctl = snapshot_ioctl,
398 static struct miscdevice snapshot_device = {
399 .minor = SNAPSHOT_MINOR,
401 .fops = &snapshot_fops,
404 static int __init snapshot_device_init(void)
406 return misc_register(&snapshot_device);
409 device_initcall(snapshot_device_init);