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;
72 if (!atomic_add_unless(&snapshot_device_available, -1, 0))
75 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
76 atomic_inc(&snapshot_device_available);
79 if(create_basic_memory_bitmaps()) {
80 atomic_inc(&snapshot_device_available);
83 nonseekable_open(inode, filp);
84 data = &snapshot_state;
85 filp->private_data = data;
86 memset(&data->handle, 0, sizeof(struct snapshot_handle));
87 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
88 data->swap = swsusp_resume_device ?
89 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
90 data->mode = O_RDONLY;
91 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
93 pm_notifier_call_chain(PM_POST_RESTORE);
96 data->mode = O_WRONLY;
97 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
99 pm_notifier_call_chain(PM_POST_HIBERNATION);
102 atomic_inc(&snapshot_device_available);
107 data->platform_support = 0;
112 static int snapshot_release(struct inode *inode, struct file *filp)
114 struct snapshot_data *data;
117 free_basic_memory_bitmaps();
118 data = filp->private_data;
119 free_all_swap_pages(data->swap);
121 mutex_lock(&pm_mutex);
123 mutex_unlock(&pm_mutex);
125 pm_notifier_call_chain(data->mode == O_WRONLY ?
126 PM_POST_HIBERNATION : PM_POST_RESTORE);
127 atomic_inc(&snapshot_device_available);
131 static ssize_t snapshot_read(struct file *filp, char __user *buf,
132 size_t count, loff_t *offp)
134 struct snapshot_data *data;
137 data = filp->private_data;
140 res = snapshot_read_next(&data->handle, count);
142 if (copy_to_user(buf, data_of(data->handle), res))
145 *offp = data->handle.offset;
150 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
151 size_t count, loff_t *offp)
153 struct snapshot_data *data;
156 data = filp->private_data;
157 res = snapshot_write_next(&data->handle, count);
159 if (copy_from_user(data_of(data->handle), buf, res))
162 *offp = data->handle.offset;
167 static int snapshot_ioctl(struct inode *inode, struct file *filp,
168 unsigned int cmd, unsigned long arg)
171 struct snapshot_data *data;
175 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
177 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
179 if (!capable(CAP_SYS_ADMIN))
182 data = filp->private_data;
186 case SNAPSHOT_FREEZE:
189 mutex_lock(&pm_mutex);
190 printk("Syncing filesystems ... ");
194 error = freeze_processes();
197 mutex_unlock(&pm_mutex);
202 case SNAPSHOT_UNFREEZE:
203 if (!data->frozen || data->ready)
205 mutex_lock(&pm_mutex);
207 mutex_unlock(&pm_mutex);
211 case SNAPSHOT_CREATE_IMAGE:
212 case SNAPSHOT_ATOMIC_SNAPSHOT:
213 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
217 error = hibernation_snapshot(data->platform_support);
219 error = put_user(in_suspend, (int __user *)arg);
224 case SNAPSHOT_ATOMIC_RESTORE:
225 snapshot_write_finalize(&data->handle);
226 if (data->mode != O_WRONLY || !data->frozen ||
227 !snapshot_image_loaded(&data->handle)) {
231 error = hibernation_restore(data->platform_support);
236 memset(&data->handle, 0, sizeof(struct snapshot_handle));
240 case SNAPSHOT_PREF_IMAGE_SIZE:
241 case SNAPSHOT_SET_IMAGE_SIZE:
245 case SNAPSHOT_GET_IMAGE_SIZE:
250 size = snapshot_get_image_size();
252 error = put_user(size, (loff_t __user *)arg);
255 case SNAPSHOT_AVAIL_SWAP_SIZE:
256 case SNAPSHOT_AVAIL_SWAP:
257 size = count_swap_pages(data->swap, 1);
259 error = put_user(size, (loff_t __user *)arg);
262 case SNAPSHOT_ALLOC_SWAP_PAGE:
263 case SNAPSHOT_GET_SWAP_PAGE:
264 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
268 offset = alloc_swapdev_block(data->swap);
270 offset <<= PAGE_SHIFT;
271 error = put_user(offset, (loff_t __user *)arg);
277 case SNAPSHOT_FREE_SWAP_PAGES:
278 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
282 free_all_swap_pages(data->swap);
285 case SNAPSHOT_SET_SWAP_FILE: /* This ioctl is deprecated */
286 if (!swsusp_swap_in_use()) {
288 * User space encodes device types as two-byte values,
289 * so we need to recode them
291 if (old_decode_dev(arg)) {
292 data->swap = swap_type_of(old_decode_dev(arg),
310 if (!mutex_trylock(&pm_mutex)) {
315 * Tasks are frozen and the notifiers have been called with
316 * PM_HIBERNATION_PREPARE
318 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
319 mutex_unlock(&pm_mutex);
322 case SNAPSHOT_PLATFORM_SUPPORT:
323 data->platform_support = !!arg;
326 case SNAPSHOT_POWER_OFF:
327 if (data->platform_support)
328 error = hibernation_platform_enter();
331 case SNAPSHOT_PMOPS: /* This ioctl is deprecated */
337 data->platform_support = 1;
342 if (data->platform_support)
343 error = hibernation_platform_enter();
347 if (data->platform_support)
352 printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
357 case SNAPSHOT_SET_SWAP_AREA:
358 if (swsusp_swap_in_use()) {
361 struct resume_swap_area swap_area;
364 error = copy_from_user(&swap_area, (void __user *)arg,
365 sizeof(struct resume_swap_area));
372 * User space encodes device types as two-byte values,
373 * so we need to recode them
375 swdev = old_decode_dev(swap_area.dev);
377 offset = swap_area.offset;
378 data->swap = swap_type_of(swdev, offset, NULL);
396 static const struct file_operations snapshot_fops = {
397 .open = snapshot_open,
398 .release = snapshot_release,
399 .read = snapshot_read,
400 .write = snapshot_write,
402 .ioctl = snapshot_ioctl,
405 static struct miscdevice snapshot_device = {
406 .minor = SNAPSHOT_MINOR,
408 .fops = &snapshot_fops,
411 static int __init snapshot_device_init(void)
413 return misc_register(&snapshot_device);
416 device_initcall(snapshot_device_init);