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:
143 pm_prepare_console();
144 disable_nonboot_cpus();
145 if (freeze_processes()) {
147 enable_nonboot_cpus();
148 pm_restore_console();
156 case SNAPSHOT_UNFREEZE:
161 enable_nonboot_cpus();
162 pm_restore_console();
167 case SNAPSHOT_ATOMIC_SNAPSHOT:
168 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
173 /* Free memory before shutting down devices. */
174 error = swsusp_shrink_memory();
176 error = device_suspend(PMSG_FREEZE);
179 error = swsusp_suspend();
185 error = put_user(in_suspend, (unsigned int __user *)arg);
190 case SNAPSHOT_ATOMIC_RESTORE:
191 if (data->mode != O_WRONLY || !data->frozen ||
192 !snapshot_image_loaded(&data->handle)) {
197 pm_prepare_console();
198 error = device_suspend(PMSG_FREEZE);
200 error = swsusp_resume();
203 pm_restore_console();
209 memset(&data->handle, 0, sizeof(struct snapshot_handle));
213 case SNAPSHOT_SET_IMAGE_SIZE:
217 case SNAPSHOT_AVAIL_SWAP:
218 avail = count_swap_pages(data->swap, 1);
219 avail <<= PAGE_SHIFT;
220 error = put_user(avail, (loff_t __user *)arg);
223 case SNAPSHOT_GET_SWAP_PAGE:
224 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
229 data->bitmap = alloc_bitmap(count_swap_pages(data->swap, 0));
235 offset = alloc_swap_page(data->swap, data->bitmap);
237 offset <<= PAGE_SHIFT;
238 error = put_user(offset, (loff_t __user *)arg);
244 case SNAPSHOT_FREE_SWAP_PAGES:
245 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
249 free_all_swap_pages(data->swap, data->bitmap);
250 free_bitmap(data->bitmap);
254 case SNAPSHOT_SET_SWAP_FILE:
257 * User space encodes device types as two-byte values,
258 * so we need to recode them
260 if (old_decode_dev(arg)) {
261 data->swap = swap_type_of(old_decode_dev(arg));
281 static struct file_operations snapshot_fops = {
282 .open = snapshot_open,
283 .release = snapshot_release,
284 .read = snapshot_read,
285 .write = snapshot_write,
287 .ioctl = snapshot_ioctl,
290 static struct miscdevice snapshot_device = {
291 .minor = SNAPSHOT_MINOR,
293 .fops = &snapshot_fops,
296 static int __init snapshot_device_init(void)
298 return misc_register(&snapshot_device);
301 device_initcall(snapshot_device_init);