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/kmod.h>
16 #include <linux/string.h>
17 #include <linux/device.h>
18 #include <linux/miscdevice.h>
20 #include <linux/swap.h>
21 #include <linux/swapops.h>
24 #include <linux/console.h>
25 #include <linux/cpu.h>
26 #include <linux/freezer.h>
27 #include <scsi/scsi_scan.h>
29 #include <asm/uaccess.h>
34 * NOTE: The SNAPSHOT_SET_SWAP_FILE and SNAPSHOT_PMOPS ioctls are obsolete and
35 * will be removed in the future. They are only preserved here for
36 * compatibility with existing userland utilities.
38 #define SNAPSHOT_SET_SWAP_FILE _IOW(SNAPSHOT_IOC_MAGIC, 10, unsigned int)
39 #define SNAPSHOT_PMOPS _IOW(SNAPSHOT_IOC_MAGIC, 12, unsigned int)
41 #define PMOPS_PREPARE 1
43 #define PMOPS_FINISH 3
46 * NOTE: The following ioctl definitions are wrong and have been replaced with
47 * correct ones. They are only preserved here for compatibility with existing
48 * userland utilities and will be removed in the future.
50 #define SNAPSHOT_ATOMIC_SNAPSHOT _IOW(SNAPSHOT_IOC_MAGIC, 3, void *)
51 #define SNAPSHOT_SET_IMAGE_SIZE _IOW(SNAPSHOT_IOC_MAGIC, 6, unsigned long)
52 #define SNAPSHOT_AVAIL_SWAP _IOR(SNAPSHOT_IOC_MAGIC, 7, void *)
53 #define SNAPSHOT_GET_SWAP_PAGE _IOR(SNAPSHOT_IOC_MAGIC, 8, void *)
56 #define SNAPSHOT_MINOR 231
58 static struct snapshot_data {
59 struct snapshot_handle handle;
64 char platform_support;
67 atomic_t snapshot_device_available = ATOMIC_INIT(1);
69 static int snapshot_open(struct inode *inode, struct file *filp)
71 struct snapshot_data *data;
74 mutex_lock(&pm_mutex);
76 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
81 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
82 atomic_inc(&snapshot_device_available);
86 if(create_basic_memory_bitmaps()) {
87 atomic_inc(&snapshot_device_available);
91 nonseekable_open(inode, filp);
92 data = &snapshot_state;
93 filp->private_data = data;
94 memset(&data->handle, 0, sizeof(struct snapshot_handle));
95 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
96 /* Hibernating. The image device should be accessible. */
97 data->swap = swsusp_resume_device ?
98 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
99 data->mode = O_RDONLY;
100 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
102 pm_notifier_call_chain(PM_POST_HIBERNATION);
105 * Resuming. We may need to wait for the image device to
108 wait_for_device_probe();
109 scsi_complete_async_scans();
112 data->mode = O_WRONLY;
113 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
115 pm_notifier_call_chain(PM_POST_RESTORE);
118 free_basic_memory_bitmaps();
119 atomic_inc(&snapshot_device_available);
123 data->platform_support = 0;
126 mutex_unlock(&pm_mutex);
131 static int snapshot_release(struct inode *inode, struct file *filp)
133 struct snapshot_data *data;
135 mutex_lock(&pm_mutex);
138 free_basic_memory_bitmaps();
139 data = filp->private_data;
140 free_all_swap_pages(data->swap);
142 pm_restore_gfp_mask();
145 pm_notifier_call_chain(data->mode == O_RDONLY ?
146 PM_POST_HIBERNATION : PM_POST_RESTORE);
147 atomic_inc(&snapshot_device_available);
149 mutex_unlock(&pm_mutex);
154 static ssize_t snapshot_read(struct file *filp, char __user *buf,
155 size_t count, loff_t *offp)
157 struct snapshot_data *data;
159 loff_t pg_offp = *offp & ~PAGE_MASK;
161 mutex_lock(&pm_mutex);
163 data = filp->private_data;
168 if (!pg_offp) { /* on page boundary? */
169 res = snapshot_read_next(&data->handle);
173 res = PAGE_SIZE - pg_offp;
176 res = simple_read_from_buffer(buf, count, &pg_offp,
177 data_of(data->handle), res);
182 mutex_unlock(&pm_mutex);
187 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
188 size_t count, loff_t *offp)
190 struct snapshot_data *data;
192 loff_t pg_offp = *offp & ~PAGE_MASK;
194 mutex_lock(&pm_mutex);
196 data = filp->private_data;
199 res = snapshot_write_next(&data->handle);
203 res = PAGE_SIZE - pg_offp;
206 res = simple_write_to_buffer(data_of(data->handle), res, &pg_offp,
211 mutex_unlock(&pm_mutex);
216 static void snapshot_deprecated_ioctl(unsigned int cmd)
218 if (printk_ratelimit())
219 printk(KERN_NOTICE "%pf: ioctl '%.8x' is deprecated and will "
220 "be removed soon, update your suspend-to-disk "
222 __builtin_return_address(0), cmd);
225 static long snapshot_ioctl(struct file *filp, unsigned int cmd,
229 struct snapshot_data *data;
233 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
235 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
237 if (!capable(CAP_SYS_ADMIN))
240 if (!mutex_trylock(&pm_mutex))
243 data = filp->private_data;
247 case SNAPSHOT_FREEZE:
251 printk("Syncing filesystems ... ");
255 error = usermodehelper_disable();
259 error = freeze_processes();
261 usermodehelper_enable();
266 case SNAPSHOT_UNFREEZE:
267 if (!data->frozen || data->ready)
269 pm_restore_gfp_mask();
271 usermodehelper_enable();
275 case SNAPSHOT_ATOMIC_SNAPSHOT:
276 snapshot_deprecated_ioctl(cmd);
277 case SNAPSHOT_CREATE_IMAGE:
278 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
282 pm_restore_gfp_mask();
283 error = hibernation_snapshot(data->platform_support);
285 error = put_user(in_suspend, (int __user *)arg);
286 if (!error && !freezer_test_done)
288 if (freezer_test_done) {
289 freezer_test_done = false;
295 case SNAPSHOT_ATOMIC_RESTORE:
296 snapshot_write_finalize(&data->handle);
297 if (data->mode != O_WRONLY || !data->frozen ||
298 !snapshot_image_loaded(&data->handle)) {
302 error = hibernation_restore(data->platform_support);
307 memset(&data->handle, 0, sizeof(struct snapshot_handle));
311 case SNAPSHOT_SET_IMAGE_SIZE:
312 snapshot_deprecated_ioctl(cmd);
313 case SNAPSHOT_PREF_IMAGE_SIZE:
317 case SNAPSHOT_GET_IMAGE_SIZE:
322 size = snapshot_get_image_size();
324 error = put_user(size, (loff_t __user *)arg);
327 case SNAPSHOT_AVAIL_SWAP:
328 snapshot_deprecated_ioctl(cmd);
329 case SNAPSHOT_AVAIL_SWAP_SIZE:
330 size = count_swap_pages(data->swap, 1);
332 error = put_user(size, (loff_t __user *)arg);
335 case SNAPSHOT_GET_SWAP_PAGE:
336 snapshot_deprecated_ioctl(cmd);
337 case SNAPSHOT_ALLOC_SWAP_PAGE:
338 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
342 offset = alloc_swapdev_block(data->swap);
344 offset <<= PAGE_SHIFT;
345 error = put_user(offset, (loff_t __user *)arg);
351 case SNAPSHOT_FREE_SWAP_PAGES:
352 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
356 free_all_swap_pages(data->swap);
359 case SNAPSHOT_SET_SWAP_FILE: /* This ioctl is deprecated */
360 snapshot_deprecated_ioctl(cmd);
361 if (!swsusp_swap_in_use()) {
363 * User space encodes device types as two-byte values,
364 * so we need to recode them
366 if (old_decode_dev(arg)) {
367 data->swap = swap_type_of(old_decode_dev(arg),
386 * Tasks are frozen and the notifiers have been called with
387 * PM_HIBERNATION_PREPARE
389 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
393 case SNAPSHOT_PLATFORM_SUPPORT:
394 data->platform_support = !!arg;
397 case SNAPSHOT_POWER_OFF:
398 if (data->platform_support)
399 error = hibernation_platform_enter();
402 case SNAPSHOT_PMOPS: /* This ioctl is deprecated */
403 snapshot_deprecated_ioctl(cmd);
409 data->platform_support = 1;
414 if (data->platform_support)
415 error = hibernation_platform_enter();
419 if (data->platform_support)
424 printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
429 case SNAPSHOT_SET_SWAP_AREA:
430 if (swsusp_swap_in_use()) {
433 struct resume_swap_area swap_area;
436 error = copy_from_user(&swap_area, (void __user *)arg,
437 sizeof(struct resume_swap_area));
444 * User space encodes device types as two-byte values,
445 * so we need to recode them
447 swdev = new_decode_dev(swap_area.dev);
449 offset = swap_area.offset;
450 data->swap = swap_type_of(swdev, offset, NULL);
465 mutex_unlock(&pm_mutex);
470 static const struct file_operations snapshot_fops = {
471 .open = snapshot_open,
472 .release = snapshot_release,
473 .read = snapshot_read,
474 .write = snapshot_write,
476 .unlocked_ioctl = snapshot_ioctl,
479 static struct miscdevice snapshot_device = {
480 .minor = SNAPSHOT_MINOR,
482 .fops = &snapshot_fops,
485 static int __init snapshot_device_init(void)
487 return misc_register(&snapshot_device);
490 device_initcall(snapshot_device_init);