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/console.h>
24 #include <linux/cpu.h>
25 #include <linux/freezer.h>
27 #include <asm/uaccess.h>
31 #define SNAPSHOT_MINOR 231
33 static struct snapshot_data
{
34 struct snapshot_handle handle
;
36 struct bitmap_page
*bitmap
;
42 static atomic_t device_available
= ATOMIC_INIT(1);
44 static int snapshot_open(struct inode
*inode
, struct file
*filp
)
46 struct snapshot_data
*data
;
48 if (!atomic_add_unless(&device_available
, -1, 0))
51 if ((filp
->f_flags
& O_ACCMODE
) == O_RDWR
)
54 nonseekable_open(inode
, filp
);
55 data
= &snapshot_state
;
56 filp
->private_data
= data
;
57 memset(&data
->handle
, 0, sizeof(struct snapshot_handle
));
58 if ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
59 data
->swap
= swsusp_resume_device
?
60 swap_type_of(swsusp_resume_device
, 0, NULL
) : -1;
61 data
->mode
= O_RDONLY
;
64 data
->mode
= O_WRONLY
;
73 static int snapshot_release(struct inode
*inode
, struct file
*filp
)
75 struct snapshot_data
*data
;
78 data
= filp
->private_data
;
79 free_all_swap_pages(data
->swap
, data
->bitmap
);
80 free_bitmap(data
->bitmap
);
82 mutex_lock(&pm_mutex
);
84 enable_nonboot_cpus();
85 mutex_unlock(&pm_mutex
);
87 atomic_inc(&device_available
);
91 static ssize_t
snapshot_read(struct file
*filp
, char __user
*buf
,
92 size_t count
, loff_t
*offp
)
94 struct snapshot_data
*data
;
97 data
= filp
->private_data
;
98 res
= snapshot_read_next(&data
->handle
, count
);
100 if (copy_to_user(buf
, data_of(data
->handle
), res
))
103 *offp
= data
->handle
.offset
;
108 static ssize_t
snapshot_write(struct file
*filp
, const char __user
*buf
,
109 size_t count
, loff_t
*offp
)
111 struct snapshot_data
*data
;
114 data
= filp
->private_data
;
115 res
= snapshot_write_next(&data
->handle
, count
);
117 if (copy_from_user(data_of(data
->handle
), buf
, res
))
120 *offp
= data
->handle
.offset
;
125 static inline int snapshot_suspend(void)
129 mutex_lock(&pm_mutex
);
130 /* Free memory before shutting down devices. */
131 error
= swsusp_shrink_memory();
136 error
= device_suspend(PMSG_FREEZE
);
140 error
= disable_nonboot_cpus();
143 error
= swsusp_suspend();
145 enable_nonboot_cpus();
150 mutex_unlock(&pm_mutex
);
154 static inline int snapshot_restore(void)
158 mutex_lock(&pm_mutex
);
159 pm_prepare_console();
161 error
= device_suspend(PMSG_PRETHAW
);
165 error
= disable_nonboot_cpus();
167 error
= swsusp_resume();
169 enable_nonboot_cpus();
173 pm_restore_console();
174 mutex_unlock(&pm_mutex
);
178 static int snapshot_ioctl(struct inode
*inode
, struct file
*filp
,
179 unsigned int cmd
, unsigned long arg
)
182 struct snapshot_data
*data
;
186 if (_IOC_TYPE(cmd
) != SNAPSHOT_IOC_MAGIC
)
188 if (_IOC_NR(cmd
) > SNAPSHOT_IOC_MAXNR
)
190 if (!capable(CAP_SYS_ADMIN
))
193 data
= filp
->private_data
;
197 case SNAPSHOT_FREEZE
:
200 mutex_lock(&pm_mutex
);
201 if (freeze_processes()) {
205 mutex_unlock(&pm_mutex
);
210 case SNAPSHOT_UNFREEZE
:
213 mutex_lock(&pm_mutex
);
215 mutex_unlock(&pm_mutex
);
219 case SNAPSHOT_ATOMIC_SNAPSHOT
:
220 if (data
->mode
!= O_RDONLY
|| !data
->frozen
|| data
->ready
) {
224 error
= snapshot_suspend();
226 error
= put_user(in_suspend
, (unsigned int __user
*)arg
);
231 case SNAPSHOT_ATOMIC_RESTORE
:
232 snapshot_write_finalize(&data
->handle
);
233 if (data
->mode
!= O_WRONLY
|| !data
->frozen
||
234 !snapshot_image_loaded(&data
->handle
)) {
238 error
= snapshot_restore();
243 memset(&data
->handle
, 0, sizeof(struct snapshot_handle
));
247 case SNAPSHOT_SET_IMAGE_SIZE
:
251 case SNAPSHOT_AVAIL_SWAP
:
252 avail
= count_swap_pages(data
->swap
, 1);
253 avail
<<= PAGE_SHIFT
;
254 error
= put_user(avail
, (loff_t __user
*)arg
);
257 case SNAPSHOT_GET_SWAP_PAGE
:
258 if (data
->swap
< 0 || data
->swap
>= MAX_SWAPFILES
) {
263 data
->bitmap
= alloc_bitmap(count_swap_pages(data
->swap
, 0));
269 offset
= alloc_swapdev_block(data
->swap
, data
->bitmap
);
271 offset
<<= PAGE_SHIFT
;
272 error
= put_user(offset
, (sector_t __user
*)arg
);
278 case SNAPSHOT_FREE_SWAP_PAGES
:
279 if (data
->swap
< 0 || data
->swap
>= MAX_SWAPFILES
) {
283 free_all_swap_pages(data
->swap
, data
->bitmap
);
284 free_bitmap(data
->bitmap
);
288 case SNAPSHOT_SET_SWAP_FILE
:
291 * User space encodes device types as two-byte values,
292 * so we need to recode them
294 if (old_decode_dev(arg
)) {
295 data
->swap
= swap_type_of(old_decode_dev(arg
),
314 if (!mutex_trylock(&pm_mutex
)) {
319 if (pm_ops
->prepare
) {
320 error
= pm_ops
->prepare(PM_SUSPEND_MEM
);
325 /* Put devices to sleep */
327 error
= device_suspend(PMSG_SUSPEND
);
329 printk(KERN_ERR
"Failed to suspend some devices.\n");
331 /* Enter S3, system is already frozen */
332 suspend_enter(PM_SUSPEND_MEM
);
334 /* Wake up devices */
339 pm_ops
->finish(PM_SUSPEND_MEM
);
342 mutex_unlock(&pm_mutex
);
349 if (pm_ops
->prepare
) {
350 error
= pm_ops
->prepare(PM_SUSPEND_DISK
);
355 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK
);
356 error
= pm_ops
->enter(PM_SUSPEND_DISK
);
360 if (pm_ops
&& pm_ops
->finish
) {
361 pm_ops
->finish(PM_SUSPEND_DISK
);
366 printk(KERN_ERR
"SNAPSHOT_PMOPS: invalid argument %ld\n", arg
);
372 case SNAPSHOT_SET_SWAP_AREA
:
376 struct resume_swap_area swap_area
;
379 error
= copy_from_user(&swap_area
, (void __user
*)arg
,
380 sizeof(struct resume_swap_area
));
387 * User space encodes device types as two-byte values,
388 * so we need to recode them
390 swdev
= old_decode_dev(swap_area
.dev
);
392 offset
= swap_area
.offset
;
393 data
->swap
= swap_type_of(swdev
, offset
, NULL
);
411 static const struct file_operations snapshot_fops
= {
412 .open
= snapshot_open
,
413 .release
= snapshot_release
,
414 .read
= snapshot_read
,
415 .write
= snapshot_write
,
417 .ioctl
= snapshot_ioctl
,
420 static struct miscdevice snapshot_device
= {
421 .minor
= SNAPSHOT_MINOR
,
423 .fops
= &snapshot_fops
,
426 static int __init
snapshot_device_init(void)
428 return misc_register(&snapshot_device
);
431 device_initcall(snapshot_device_init
);