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 int snapshot_ioctl(struct inode
*inode
, struct file
*filp
,
126 unsigned int cmd
, unsigned long arg
)
129 struct snapshot_data
*data
;
133 if (_IOC_TYPE(cmd
) != SNAPSHOT_IOC_MAGIC
)
135 if (_IOC_NR(cmd
) > SNAPSHOT_IOC_MAXNR
)
137 if (!capable(CAP_SYS_ADMIN
))
140 data
= filp
->private_data
;
144 case SNAPSHOT_FREEZE
:
147 mutex_lock(&pm_mutex
);
148 error
= disable_nonboot_cpus();
150 error
= freeze_processes();
153 enable_nonboot_cpus();
157 mutex_unlock(&pm_mutex
);
162 case SNAPSHOT_UNFREEZE
:
165 mutex_lock(&pm_mutex
);
167 enable_nonboot_cpus();
168 mutex_unlock(&pm_mutex
);
172 case SNAPSHOT_ATOMIC_SNAPSHOT
:
173 if (data
->mode
!= O_RDONLY
|| !data
->frozen
|| data
->ready
) {
177 mutex_lock(&pm_mutex
);
178 /* Free memory before shutting down devices. */
179 error
= swsusp_shrink_memory();
182 error
= device_suspend(PMSG_FREEZE
);
185 error
= swsusp_suspend();
190 mutex_unlock(&pm_mutex
);
192 error
= put_user(in_suspend
, (unsigned int __user
*)arg
);
197 case SNAPSHOT_ATOMIC_RESTORE
:
198 snapshot_write_finalize(&data
->handle
);
199 if (data
->mode
!= O_WRONLY
|| !data
->frozen
||
200 !snapshot_image_loaded(&data
->handle
)) {
204 mutex_lock(&pm_mutex
);
205 pm_prepare_console();
207 error
= device_suspend(PMSG_PRETHAW
);
209 error
= swsusp_resume();
213 pm_restore_console();
214 mutex_unlock(&pm_mutex
);
219 memset(&data
->handle
, 0, sizeof(struct snapshot_handle
));
223 case SNAPSHOT_SET_IMAGE_SIZE
:
227 case SNAPSHOT_AVAIL_SWAP
:
228 avail
= count_swap_pages(data
->swap
, 1);
229 avail
<<= PAGE_SHIFT
;
230 error
= put_user(avail
, (loff_t __user
*)arg
);
233 case SNAPSHOT_GET_SWAP_PAGE
:
234 if (data
->swap
< 0 || data
->swap
>= MAX_SWAPFILES
) {
239 data
->bitmap
= alloc_bitmap(count_swap_pages(data
->swap
, 0));
245 offset
= alloc_swapdev_block(data
->swap
, data
->bitmap
);
247 offset
<<= PAGE_SHIFT
;
248 error
= put_user(offset
, (sector_t __user
*)arg
);
254 case SNAPSHOT_FREE_SWAP_PAGES
:
255 if (data
->swap
< 0 || data
->swap
>= MAX_SWAPFILES
) {
259 free_all_swap_pages(data
->swap
, data
->bitmap
);
260 free_bitmap(data
->bitmap
);
264 case SNAPSHOT_SET_SWAP_FILE
:
267 * User space encodes device types as two-byte values,
268 * so we need to recode them
270 if (old_decode_dev(arg
)) {
271 data
->swap
= swap_type_of(old_decode_dev(arg
),
290 if (!mutex_trylock(&pm_mutex
)) {
295 if (pm_ops
->prepare
) {
296 error
= pm_ops
->prepare(PM_SUSPEND_MEM
);
301 /* Put devices to sleep */
303 error
= device_suspend(PMSG_SUSPEND
);
305 printk(KERN_ERR
"Failed to suspend some devices.\n");
307 /* Enter S3, system is already frozen */
308 suspend_enter(PM_SUSPEND_MEM
);
310 /* Wake up devices */
315 pm_ops
->finish(PM_SUSPEND_MEM
);
318 mutex_unlock(&pm_mutex
);
325 if (pm_ops
->prepare
) {
326 error
= pm_ops
->prepare(PM_SUSPEND_DISK
);
331 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK
);
332 error
= pm_ops
->enter(PM_SUSPEND_DISK
);
336 if (pm_ops
&& pm_ops
->finish
) {
337 pm_ops
->finish(PM_SUSPEND_DISK
);
342 printk(KERN_ERR
"SNAPSHOT_PMOPS: invalid argument %ld\n", arg
);
348 case SNAPSHOT_SET_SWAP_AREA
:
352 struct resume_swap_area swap_area
;
355 error
= copy_from_user(&swap_area
, (void __user
*)arg
,
356 sizeof(struct resume_swap_area
));
363 * User space encodes device types as two-byte values,
364 * so we need to recode them
366 swdev
= old_decode_dev(swap_area
.dev
);
368 offset
= swap_area
.offset
;
369 data
->swap
= swap_type_of(swdev
, offset
, NULL
);
387 static const struct file_operations snapshot_fops
= {
388 .open
= snapshot_open
,
389 .release
= snapshot_release
,
390 .read
= snapshot_read
,
391 .write
= snapshot_write
,
393 .ioctl
= snapshot_ioctl
,
396 static struct miscdevice snapshot_device
= {
397 .minor
= SNAPSHOT_MINOR
,
399 .fops
= &snapshot_fops
,
402 static int __init
snapshot_device_init(void)
404 return misc_register(&snapshot_device
);
407 device_initcall(snapshot_device_init
);