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/string.h>
15 #include <linux/device.h>
16 #include <linux/miscdevice.h>
18 #include <linux/swap.h>
19 #include <linux/swapops.h>
23 #include <asm/uaccess.h>
27 #define SNAPSHOT_MINOR 231
29 static struct snapshot_data {
30 struct snapshot_handle handle;
32 struct bitmap_page *bitmap;
38 static atomic_t device_available = ATOMIC_INIT(1);
40 static int snapshot_open(struct inode *inode, struct file *filp)
42 struct snapshot_data *data;
44 if (!atomic_add_unless(&device_available, -1, 0))
47 if ((filp->f_flags & O_ACCMODE) == O_RDWR)
50 nonseekable_open(inode, filp);
51 data = &snapshot_state;
52 filp->private_data = data;
53 memset(&data->handle, 0, sizeof(struct snapshot_handle));
54 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
55 data->swap = swsusp_resume_device ? swap_type_of(swsusp_resume_device) : -1;
56 data->mode = O_RDONLY;
59 data->mode = O_WRONLY;
68 static int snapshot_release(struct inode *inode, struct file *filp)
70 struct snapshot_data *data;
73 data = filp->private_data;
74 free_all_swap_pages(data->swap, data->bitmap);
75 free_bitmap(data->bitmap);
79 enable_nonboot_cpus();
82 atomic_inc(&device_available);
86 static ssize_t snapshot_read(struct file *filp, char __user *buf,
87 size_t count, loff_t *offp)
89 struct snapshot_data *data;
92 data = filp->private_data;
93 res = snapshot_read_next(&data->handle, count);
95 if (copy_to_user(buf, data_of(data->handle), res))
98 *offp = data->handle.offset;
103 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
104 size_t count, loff_t *offp)
106 struct snapshot_data *data;
109 data = filp->private_data;
110 res = snapshot_write_next(&data->handle, count);
112 if (copy_from_user(data_of(data->handle), buf, res))
115 *offp = data->handle.offset;
120 static int snapshot_ioctl(struct inode *inode, struct file *filp,
121 unsigned int cmd, unsigned long arg)
124 struct snapshot_data *data;
125 loff_t offset, avail;
127 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
129 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
131 if (!capable(CAP_SYS_ADMIN))
134 data = filp->private_data;
138 case SNAPSHOT_FREEZE:
142 pm_prepare_console();
143 disable_nonboot_cpus();
144 if (freeze_processes()) {
146 enable_nonboot_cpus();
147 pm_restore_console();
155 case SNAPSHOT_UNFREEZE:
160 enable_nonboot_cpus();
161 pm_restore_console();
166 case SNAPSHOT_ATOMIC_SNAPSHOT:
167 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
172 /* Free memory before shutting down devices. */
173 error = swsusp_shrink_memory();
175 error = device_suspend(PMSG_FREEZE);
178 error = swsusp_suspend();
184 error = put_user(in_suspend, (unsigned int __user *)arg);
189 case SNAPSHOT_ATOMIC_RESTORE:
190 if (data->mode != O_WRONLY || !data->frozen ||
191 !snapshot_image_loaded(&data->handle)) {
196 pm_prepare_console();
197 error = device_suspend(PMSG_FREEZE);
199 error = swsusp_resume();
202 pm_restore_console();
208 memset(&data->handle, 0, sizeof(struct snapshot_handle));
212 case SNAPSHOT_SET_IMAGE_SIZE:
216 case SNAPSHOT_AVAIL_SWAP:
217 avail = count_swap_pages(data->swap, 1);
218 avail <<= PAGE_SHIFT;
219 error = put_user(avail, (loff_t __user *)arg);
222 case SNAPSHOT_GET_SWAP_PAGE:
223 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
228 data->bitmap = alloc_bitmap(count_swap_pages(data->swap, 0));
234 offset = alloc_swap_page(data->swap, data->bitmap);
236 offset <<= PAGE_SHIFT;
237 error = put_user(offset, (loff_t __user *)arg);
243 case SNAPSHOT_FREE_SWAP_PAGES:
244 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
248 free_all_swap_pages(data->swap, data->bitmap);
249 free_bitmap(data->bitmap);
253 case SNAPSHOT_SET_SWAP_FILE:
256 * User space encodes device types as two-byte values,
257 * so we need to recode them
259 if (old_decode_dev(arg)) {
260 data->swap = swap_type_of(old_decode_dev(arg));
280 static struct file_operations snapshot_fops = {
281 .open = snapshot_open,
282 .release = snapshot_release,
283 .read = snapshot_read,
284 .write = snapshot_write,
286 .ioctl = snapshot_ioctl,
289 static struct miscdevice snapshot_device = {
290 .minor = SNAPSHOT_MINOR,
292 .fops = &snapshot_fops,
295 static int __init snapshot_device_init(void)
297 return misc_register(&snapshot_device);
300 device_initcall(snapshot_device_init);