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/compat.h>
24 #include <linux/console.h>
25 #include <linux/cpu.h>
26 #include <linux/freezer.h>
28 #include <asm/uaccess.h>
33 #define SNAPSHOT_MINOR 231
35 static struct snapshot_data {
36 struct snapshot_handle handle;
41 char platform_support;
44 atomic_t snapshot_device_available = ATOMIC_INIT(1);
46 static int snapshot_open(struct inode *inode, struct file *filp)
48 struct snapshot_data *data;
53 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
58 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
59 atomic_inc(&snapshot_device_available);
63 nonseekable_open(inode, filp);
64 data = &snapshot_state;
65 filp->private_data = data;
66 memset(&data->handle, 0, sizeof(struct snapshot_handle));
67 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
68 /* Hibernating. The image device should be accessible. */
69 data->swap = swsusp_resume_device ?
70 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
71 data->mode = O_RDONLY;
72 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
74 pm_notifier_call_chain(PM_POST_HIBERNATION);
77 * Resuming. We may need to wait for the image device to
80 wait_for_device_probe();
83 data->mode = O_WRONLY;
84 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
86 pm_notifier_call_chain(PM_POST_RESTORE);
89 atomic_inc(&snapshot_device_available);
93 data->platform_support = 0;
96 unlock_system_sleep();
101 static int snapshot_release(struct inode *inode, struct file *filp)
103 struct snapshot_data *data;
108 data = filp->private_data;
109 free_all_swap_pages(data->swap);
111 pm_restore_gfp_mask();
112 free_basic_memory_bitmaps();
115 pm_notifier_call_chain(data->mode == O_RDONLY ?
116 PM_POST_HIBERNATION : PM_POST_RESTORE);
117 atomic_inc(&snapshot_device_available);
119 unlock_system_sleep();
124 static ssize_t snapshot_read(struct file *filp, char __user *buf,
125 size_t count, loff_t *offp)
127 struct snapshot_data *data;
129 loff_t pg_offp = *offp & ~PAGE_MASK;
133 data = filp->private_data;
138 if (!pg_offp) { /* on page boundary? */
139 res = snapshot_read_next(&data->handle);
143 res = PAGE_SIZE - pg_offp;
146 res = simple_read_from_buffer(buf, count, &pg_offp,
147 data_of(data->handle), res);
152 unlock_system_sleep();
157 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
158 size_t count, loff_t *offp)
160 struct snapshot_data *data;
162 loff_t pg_offp = *offp & ~PAGE_MASK;
166 data = filp->private_data;
169 res = snapshot_write_next(&data->handle);
173 res = PAGE_SIZE - pg_offp;
176 res = simple_write_to_buffer(data_of(data->handle), res, &pg_offp,
181 unlock_system_sleep();
186 static long snapshot_ioctl(struct file *filp, unsigned int cmd,
190 struct snapshot_data *data;
194 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
196 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
198 if (!capable(CAP_SYS_ADMIN))
201 if (!mutex_trylock(&pm_mutex))
204 lock_device_hotplug();
205 data = filp->private_data;
209 case SNAPSHOT_FREEZE:
213 printk("Syncing filesystems ... ");
217 error = freeze_processes();
221 error = create_basic_memory_bitmaps();
229 case SNAPSHOT_UNFREEZE:
230 if (!data->frozen || data->ready)
232 pm_restore_gfp_mask();
233 free_basic_memory_bitmaps();
238 case SNAPSHOT_CREATE_IMAGE:
239 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
243 pm_restore_gfp_mask();
244 error = hibernation_snapshot(data->platform_support);
246 error = put_user(in_suspend, (int __user *)arg);
247 data->ready = !freezer_test_done && !error;
248 freezer_test_done = false;
252 case SNAPSHOT_ATOMIC_RESTORE:
253 snapshot_write_finalize(&data->handle);
254 if (data->mode != O_WRONLY || !data->frozen ||
255 !snapshot_image_loaded(&data->handle)) {
259 error = hibernation_restore(data->platform_support);
264 memset(&data->handle, 0, sizeof(struct snapshot_handle));
267 * It is necessary to thaw kernel threads here, because
268 * SNAPSHOT_CREATE_IMAGE may be invoked directly after
269 * SNAPSHOT_FREE. In that case, if kernel threads were not
270 * thawed, the preallocation of memory carried out by
271 * hibernation_snapshot() might run into problems (i.e. it
272 * might fail or even deadlock).
274 thaw_kernel_threads();
277 case SNAPSHOT_PREF_IMAGE_SIZE:
281 case SNAPSHOT_GET_IMAGE_SIZE:
286 size = snapshot_get_image_size();
288 error = put_user(size, (loff_t __user *)arg);
291 case SNAPSHOT_AVAIL_SWAP_SIZE:
292 size = count_swap_pages(data->swap, 1);
294 error = put_user(size, (loff_t __user *)arg);
297 case SNAPSHOT_ALLOC_SWAP_PAGE:
298 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
302 offset = alloc_swapdev_block(data->swap);
304 offset <<= PAGE_SHIFT;
305 error = put_user(offset, (loff_t __user *)arg);
311 case SNAPSHOT_FREE_SWAP_PAGES:
312 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
316 free_all_swap_pages(data->swap);
325 * Tasks are frozen and the notifiers have been called with
326 * PM_HIBERNATION_PREPARE
328 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
332 case SNAPSHOT_PLATFORM_SUPPORT:
333 data->platform_support = !!arg;
336 case SNAPSHOT_POWER_OFF:
337 if (data->platform_support)
338 error = hibernation_platform_enter();
341 case SNAPSHOT_SET_SWAP_AREA:
342 if (swsusp_swap_in_use()) {
345 struct resume_swap_area swap_area;
348 error = copy_from_user(&swap_area, (void __user *)arg,
349 sizeof(struct resume_swap_area));
356 * User space encodes device types as two-byte values,
357 * so we need to recode them
359 swdev = new_decode_dev(swap_area.dev);
361 offset = swap_area.offset;
362 data->swap = swap_type_of(swdev, offset, NULL);
377 unlock_device_hotplug();
378 mutex_unlock(&pm_mutex);
385 struct compat_resume_swap_area {
386 compat_loff_t offset;
391 snapshot_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
393 BUILD_BUG_ON(sizeof(loff_t) != sizeof(compat_loff_t));
396 case SNAPSHOT_GET_IMAGE_SIZE:
397 case SNAPSHOT_AVAIL_SWAP_SIZE:
398 case SNAPSHOT_ALLOC_SWAP_PAGE: {
399 compat_loff_t __user *uoffset = compat_ptr(arg);
406 err = snapshot_ioctl(file, cmd, (unsigned long) &offset);
408 if (!err && put_user(offset, uoffset))
413 case SNAPSHOT_CREATE_IMAGE:
414 return snapshot_ioctl(file, cmd,
415 (unsigned long) compat_ptr(arg));
417 case SNAPSHOT_SET_SWAP_AREA: {
418 struct compat_resume_swap_area __user *u_swap_area =
420 struct resume_swap_area swap_area;
424 err = get_user(swap_area.offset, &u_swap_area->offset);
425 err |= get_user(swap_area.dev, &u_swap_area->dev);
430 err = snapshot_ioctl(file, SNAPSHOT_SET_SWAP_AREA,
431 (unsigned long) &swap_area);
437 return snapshot_ioctl(file, cmd, arg);
441 #endif /* CONFIG_COMPAT */
443 static const struct file_operations snapshot_fops = {
444 .open = snapshot_open,
445 .release = snapshot_release,
446 .read = snapshot_read,
447 .write = snapshot_write,
449 .unlocked_ioctl = snapshot_ioctl,
451 .compat_ioctl = snapshot_compat_ioctl,
455 static struct miscdevice snapshot_device = {
456 .minor = SNAPSHOT_MINOR,
458 .fops = &snapshot_fops,
461 static int __init snapshot_device_init(void)
463 return misc_register(&snapshot_device);
466 device_initcall(snapshot_device_init);