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 if(create_basic_memory_bitmaps()) {
64 atomic_inc(&snapshot_device_available);
68 nonseekable_open(inode, filp);
69 data = &snapshot_state;
70 filp->private_data = data;
71 memset(&data->handle, 0, sizeof(struct snapshot_handle));
72 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
73 /* Hibernating. The image device should be accessible. */
74 data->swap = swsusp_resume_device ?
75 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
76 data->mode = O_RDONLY;
77 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
79 pm_notifier_call_chain(PM_POST_HIBERNATION);
82 * Resuming. We may need to wait for the image device to
85 wait_for_device_probe();
88 data->mode = O_WRONLY;
89 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
91 pm_notifier_call_chain(PM_POST_RESTORE);
94 free_basic_memory_bitmaps();
95 atomic_inc(&snapshot_device_available);
99 data->platform_support = 0;
102 unlock_system_sleep();
107 static int snapshot_release(struct inode *inode, struct file *filp)
109 struct snapshot_data *data;
114 free_basic_memory_bitmaps();
115 data = filp->private_data;
116 free_all_swap_pages(data->swap);
118 pm_restore_gfp_mask();
121 pm_notifier_call_chain(data->mode == O_RDONLY ?
122 PM_POST_HIBERNATION : PM_POST_RESTORE);
123 atomic_inc(&snapshot_device_available);
125 unlock_system_sleep();
130 static ssize_t snapshot_read(struct file *filp, char __user *buf,
131 size_t count, loff_t *offp)
133 struct snapshot_data *data;
135 loff_t pg_offp = *offp & ~PAGE_MASK;
139 data = filp->private_data;
144 if (!pg_offp) { /* on page boundary? */
145 res = snapshot_read_next(&data->handle);
149 res = PAGE_SIZE - pg_offp;
152 res = simple_read_from_buffer(buf, count, &pg_offp,
153 data_of(data->handle), res);
158 unlock_system_sleep();
163 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
164 size_t count, loff_t *offp)
166 struct snapshot_data *data;
168 loff_t pg_offp = *offp & ~PAGE_MASK;
172 data = filp->private_data;
175 res = snapshot_write_next(&data->handle);
179 res = PAGE_SIZE - pg_offp;
182 res = simple_write_to_buffer(data_of(data->handle), res, &pg_offp,
187 unlock_system_sleep();
192 static long snapshot_ioctl(struct file *filp, unsigned int cmd,
196 struct snapshot_data *data;
200 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
202 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
204 if (!capable(CAP_SYS_ADMIN))
207 if (!mutex_trylock(&pm_mutex))
210 data = filp->private_data;
214 case SNAPSHOT_FREEZE:
218 printk("Syncing filesystems ... ");
222 error = freeze_processes();
227 case SNAPSHOT_UNFREEZE:
228 if (!data->frozen || data->ready)
230 pm_restore_gfp_mask();
235 case SNAPSHOT_CREATE_IMAGE:
236 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
240 pm_restore_gfp_mask();
241 error = hibernation_snapshot(data->platform_support);
243 error = put_user(in_suspend, (int __user *)arg);
244 data->ready = !freezer_test_done && !error;
245 freezer_test_done = false;
249 case SNAPSHOT_ATOMIC_RESTORE:
250 snapshot_write_finalize(&data->handle);
251 if (data->mode != O_WRONLY || !data->frozen ||
252 !snapshot_image_loaded(&data->handle)) {
256 error = hibernation_restore(data->platform_support);
261 memset(&data->handle, 0, sizeof(struct snapshot_handle));
264 * It is necessary to thaw kernel threads here, because
265 * SNAPSHOT_CREATE_IMAGE may be invoked directly after
266 * SNAPSHOT_FREE. In that case, if kernel threads were not
267 * thawed, the preallocation of memory carried out by
268 * hibernation_snapshot() might run into problems (i.e. it
269 * might fail or even deadlock).
271 thaw_kernel_threads();
274 case SNAPSHOT_PREF_IMAGE_SIZE:
278 case SNAPSHOT_GET_IMAGE_SIZE:
283 size = snapshot_get_image_size();
285 error = put_user(size, (loff_t __user *)arg);
288 case SNAPSHOT_AVAIL_SWAP_SIZE:
289 size = count_swap_pages(data->swap, 1);
291 error = put_user(size, (loff_t __user *)arg);
294 case SNAPSHOT_ALLOC_SWAP_PAGE:
295 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
299 offset = alloc_swapdev_block(data->swap);
301 offset <<= PAGE_SHIFT;
302 error = put_user(offset, (loff_t __user *)arg);
308 case SNAPSHOT_FREE_SWAP_PAGES:
309 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
313 free_all_swap_pages(data->swap);
322 * Tasks are frozen and the notifiers have been called with
323 * PM_HIBERNATION_PREPARE
325 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
329 case SNAPSHOT_PLATFORM_SUPPORT:
330 data->platform_support = !!arg;
333 case SNAPSHOT_POWER_OFF:
334 if (data->platform_support)
335 error = hibernation_platform_enter();
338 case SNAPSHOT_SET_SWAP_AREA:
339 if (swsusp_swap_in_use()) {
342 struct resume_swap_area swap_area;
345 error = copy_from_user(&swap_area, (void __user *)arg,
346 sizeof(struct resume_swap_area));
353 * User space encodes device types as two-byte values,
354 * so we need to recode them
356 swdev = new_decode_dev(swap_area.dev);
358 offset = swap_area.offset;
359 data->swap = swap_type_of(swdev, offset, NULL);
374 mutex_unlock(&pm_mutex);
381 struct compat_resume_swap_area {
382 compat_loff_t offset;
387 snapshot_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
389 BUILD_BUG_ON(sizeof(loff_t) != sizeof(compat_loff_t));
392 case SNAPSHOT_GET_IMAGE_SIZE:
393 case SNAPSHOT_AVAIL_SWAP_SIZE:
394 case SNAPSHOT_ALLOC_SWAP_PAGE: {
395 compat_loff_t __user *uoffset = compat_ptr(arg);
402 err = snapshot_ioctl(file, cmd, (unsigned long) &offset);
404 if (!err && put_user(offset, uoffset))
409 case SNAPSHOT_CREATE_IMAGE:
410 return snapshot_ioctl(file, cmd,
411 (unsigned long) compat_ptr(arg));
413 case SNAPSHOT_SET_SWAP_AREA: {
414 struct compat_resume_swap_area __user *u_swap_area =
416 struct resume_swap_area swap_area;
420 err = get_user(swap_area.offset, &u_swap_area->offset);
421 err |= get_user(swap_area.dev, &u_swap_area->dev);
426 err = snapshot_ioctl(file, SNAPSHOT_SET_SWAP_AREA,
427 (unsigned long) &swap_area);
433 return snapshot_ioctl(file, cmd, arg);
437 #endif /* CONFIG_COMPAT */
439 static const struct file_operations snapshot_fops = {
440 .open = snapshot_open,
441 .release = snapshot_release,
442 .read = snapshot_read,
443 .write = snapshot_write,
445 .unlocked_ioctl = snapshot_ioctl,
447 .compat_ioctl = snapshot_compat_ioctl,
451 static struct miscdevice snapshot_device = {
452 .minor = SNAPSHOT_MINOR,
454 .fops = &snapshot_fops,
457 static int __init snapshot_device_init(void)
459 return misc_register(&snapshot_device);
462 device_initcall(snapshot_device_init);