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/reboot.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>
22 #include <linux/compat.h>
23 #include <linux/console.h>
24 #include <linux/cpu.h>
25 #include <linux/freezer.h>
27 #include <linux/uaccess.h>
32 #define SNAPSHOT_MINOR 231
34 static struct snapshot_data {
35 struct snapshot_handle handle;
40 bool 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;
49 int error, nr_calls = 0;
51 if (!hibernation_available())
56 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
61 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
62 atomic_inc(&snapshot_device_available);
66 nonseekable_open(inode, filp);
67 data = &snapshot_state;
68 filp->private_data = data;
69 memset(&data->handle, 0, sizeof(struct snapshot_handle));
70 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
71 /* Hibernating. The image device should be accessible. */
72 data->swap = swsusp_resume_device ?
73 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
74 data->mode = O_RDONLY;
75 data->free_bitmaps = false;
76 error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls);
78 __pm_notifier_call_chain(PM_POST_HIBERNATION, --nr_calls, NULL);
81 * Resuming. We may need to wait for the image device to
84 wait_for_device_probe();
87 data->mode = O_WRONLY;
88 error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls);
90 error = create_basic_memory_bitmaps();
91 data->free_bitmaps = !error;
96 __pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL);
99 atomic_inc(&snapshot_device_available);
101 data->frozen = false;
103 data->platform_support = false;
106 unlock_system_sleep();
111 static int snapshot_release(struct inode *inode, struct file *filp)
113 struct snapshot_data *data;
118 data = filp->private_data;
119 free_all_swap_pages(data->swap);
121 pm_restore_gfp_mask();
122 free_basic_memory_bitmaps();
124 } else if (data->free_bitmaps) {
125 free_basic_memory_bitmaps();
127 pm_notifier_call_chain(data->mode == O_RDONLY ?
128 PM_POST_HIBERNATION : PM_POST_RESTORE);
129 atomic_inc(&snapshot_device_available);
131 unlock_system_sleep();
136 static ssize_t snapshot_read(struct file *filp, char __user *buf,
137 size_t count, loff_t *offp)
139 struct snapshot_data *data;
141 loff_t pg_offp = *offp & ~PAGE_MASK;
145 data = filp->private_data;
150 if (!pg_offp) { /* on page boundary? */
151 res = snapshot_read_next(&data->handle);
155 res = PAGE_SIZE - pg_offp;
158 res = simple_read_from_buffer(buf, count, &pg_offp,
159 data_of(data->handle), res);
164 unlock_system_sleep();
169 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
170 size_t count, loff_t *offp)
172 struct snapshot_data *data;
174 loff_t pg_offp = *offp & ~PAGE_MASK;
178 data = filp->private_data;
181 res = snapshot_write_next(&data->handle);
185 res = PAGE_SIZE - pg_offp;
188 if (!data_of(data->handle)) {
193 res = simple_write_to_buffer(data_of(data->handle), res, &pg_offp,
198 unlock_system_sleep();
203 static long snapshot_ioctl(struct file *filp, unsigned int cmd,
207 struct snapshot_data *data;
211 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
213 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
215 if (!capable(CAP_SYS_ADMIN))
218 if (!mutex_trylock(&system_transition_mutex))
221 lock_device_hotplug();
222 data = filp->private_data;
226 case SNAPSHOT_FREEZE:
232 error = freeze_processes();
236 error = create_basic_memory_bitmaps();
244 case SNAPSHOT_UNFREEZE:
245 if (!data->frozen || data->ready)
247 pm_restore_gfp_mask();
248 free_basic_memory_bitmaps();
249 data->free_bitmaps = false;
251 data->frozen = false;
254 case SNAPSHOT_CREATE_IMAGE:
255 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
259 pm_restore_gfp_mask();
260 error = hibernation_snapshot(data->platform_support);
262 error = put_user(in_suspend, (int __user *)arg);
263 data->ready = !freezer_test_done && !error;
264 freezer_test_done = false;
268 case SNAPSHOT_ATOMIC_RESTORE:
269 snapshot_write_finalize(&data->handle);
270 if (data->mode != O_WRONLY || !data->frozen ||
271 !snapshot_image_loaded(&data->handle)) {
275 error = hibernation_restore(data->platform_support);
280 memset(&data->handle, 0, sizeof(struct snapshot_handle));
283 * It is necessary to thaw kernel threads here, because
284 * SNAPSHOT_CREATE_IMAGE may be invoked directly after
285 * SNAPSHOT_FREE. In that case, if kernel threads were not
286 * thawed, the preallocation of memory carried out by
287 * hibernation_snapshot() might run into problems (i.e. it
288 * might fail or even deadlock).
290 thaw_kernel_threads();
293 case SNAPSHOT_PREF_IMAGE_SIZE:
297 case SNAPSHOT_GET_IMAGE_SIZE:
302 size = snapshot_get_image_size();
304 error = put_user(size, (loff_t __user *)arg);
307 case SNAPSHOT_AVAIL_SWAP_SIZE:
308 size = count_swap_pages(data->swap, 1);
310 error = put_user(size, (loff_t __user *)arg);
313 case SNAPSHOT_ALLOC_SWAP_PAGE:
314 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
318 offset = alloc_swapdev_block(data->swap);
320 offset <<= PAGE_SHIFT;
321 error = put_user(offset, (loff_t __user *)arg);
327 case SNAPSHOT_FREE_SWAP_PAGES:
328 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
332 free_all_swap_pages(data->swap);
341 * Tasks are frozen and the notifiers have been called with
342 * PM_HIBERNATION_PREPARE
344 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
348 case SNAPSHOT_PLATFORM_SUPPORT:
349 data->platform_support = !!arg;
352 case SNAPSHOT_POWER_OFF:
353 if (data->platform_support)
354 error = hibernation_platform_enter();
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 = new_decode_dev(swap_area.dev);
377 offset = swap_area.offset;
378 data->swap = swap_type_of(swdev, offset, NULL);
393 unlock_device_hotplug();
394 mutex_unlock(&system_transition_mutex);
401 struct compat_resume_swap_area {
402 compat_loff_t offset;
407 snapshot_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
409 BUILD_BUG_ON(sizeof(loff_t) != sizeof(compat_loff_t));
412 case SNAPSHOT_GET_IMAGE_SIZE:
413 case SNAPSHOT_AVAIL_SWAP_SIZE:
414 case SNAPSHOT_ALLOC_SWAP_PAGE: {
415 compat_loff_t __user *uoffset = compat_ptr(arg);
422 err = snapshot_ioctl(file, cmd, (unsigned long) &offset);
424 if (!err && put_user(offset, uoffset))
429 case SNAPSHOT_CREATE_IMAGE:
430 return snapshot_ioctl(file, cmd,
431 (unsigned long) compat_ptr(arg));
433 case SNAPSHOT_SET_SWAP_AREA: {
434 struct compat_resume_swap_area __user *u_swap_area =
436 struct resume_swap_area swap_area;
440 err = get_user(swap_area.offset, &u_swap_area->offset);
441 err |= get_user(swap_area.dev, &u_swap_area->dev);
446 err = snapshot_ioctl(file, SNAPSHOT_SET_SWAP_AREA,
447 (unsigned long) &swap_area);
453 return snapshot_ioctl(file, cmd, arg);
457 #endif /* CONFIG_COMPAT */
459 static const struct file_operations snapshot_fops = {
460 .open = snapshot_open,
461 .release = snapshot_release,
462 .read = snapshot_read,
463 .write = snapshot_write,
465 .unlocked_ioctl = snapshot_ioctl,
467 .compat_ioctl = snapshot_compat_ioctl,
471 static struct miscdevice snapshot_device = {
472 .minor = SNAPSHOT_MINOR,
474 .fops = &snapshot_fops,
477 static int __init snapshot_device_init(void)
479 return misc_register(&snapshot_device);
482 device_initcall(snapshot_device_init);