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>
27 #include <scsi/scsi_scan.h>
29 #include <asm/uaccess.h>
34 #define SNAPSHOT_MINOR 231
36 static struct snapshot_data
{
37 struct snapshot_handle handle
;
42 char platform_support
;
45 atomic_t snapshot_device_available
= ATOMIC_INIT(1);
47 static int snapshot_open(struct inode
*inode
, struct file
*filp
)
49 struct snapshot_data
*data
;
54 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
59 if ((filp
->f_flags
& O_ACCMODE
) == O_RDWR
) {
60 atomic_inc(&snapshot_device_available
);
64 if(create_basic_memory_bitmaps()) {
65 atomic_inc(&snapshot_device_available
);
69 nonseekable_open(inode
, filp
);
70 data
= &snapshot_state
;
71 filp
->private_data
= data
;
72 memset(&data
->handle
, 0, sizeof(struct snapshot_handle
));
73 if ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
74 /* Hibernating. The image device should be accessible. */
75 data
->swap
= swsusp_resume_device
?
76 swap_type_of(swsusp_resume_device
, 0, NULL
) : -1;
77 data
->mode
= O_RDONLY
;
78 error
= pm_notifier_call_chain(PM_HIBERNATION_PREPARE
);
80 pm_notifier_call_chain(PM_POST_HIBERNATION
);
83 * Resuming. We may need to wait for the image device to
86 wait_for_device_probe();
87 scsi_complete_async_scans();
90 data
->mode
= O_WRONLY
;
91 error
= pm_notifier_call_chain(PM_RESTORE_PREPARE
);
93 pm_notifier_call_chain(PM_POST_RESTORE
);
96 free_basic_memory_bitmaps();
97 atomic_inc(&snapshot_device_available
);
101 data
->platform_support
= 0;
104 unlock_system_sleep();
109 static int snapshot_release(struct inode
*inode
, struct file
*filp
)
111 struct snapshot_data
*data
;
116 free_basic_memory_bitmaps();
117 data
= filp
->private_data
;
118 free_all_swap_pages(data
->swap
);
120 pm_restore_gfp_mask();
123 pm_notifier_call_chain(data
->mode
== O_RDONLY
?
124 PM_POST_HIBERNATION
: PM_POST_RESTORE
);
125 atomic_inc(&snapshot_device_available
);
127 unlock_system_sleep();
132 static ssize_t
snapshot_read(struct file
*filp
, char __user
*buf
,
133 size_t count
, loff_t
*offp
)
135 struct snapshot_data
*data
;
137 loff_t pg_offp
= *offp
& ~PAGE_MASK
;
141 data
= filp
->private_data
;
146 if (!pg_offp
) { /* on page boundary? */
147 res
= snapshot_read_next(&data
->handle
);
151 res
= PAGE_SIZE
- pg_offp
;
154 res
= simple_read_from_buffer(buf
, count
, &pg_offp
,
155 data_of(data
->handle
), res
);
160 unlock_system_sleep();
165 static ssize_t
snapshot_write(struct file
*filp
, const char __user
*buf
,
166 size_t count
, loff_t
*offp
)
168 struct snapshot_data
*data
;
170 loff_t pg_offp
= *offp
& ~PAGE_MASK
;
174 data
= filp
->private_data
;
177 res
= snapshot_write_next(&data
->handle
);
181 res
= PAGE_SIZE
- pg_offp
;
184 res
= simple_write_to_buffer(data_of(data
->handle
), res
, &pg_offp
,
189 unlock_system_sleep();
194 static long snapshot_ioctl(struct file
*filp
, unsigned int cmd
,
198 struct snapshot_data
*data
;
202 if (_IOC_TYPE(cmd
) != SNAPSHOT_IOC_MAGIC
)
204 if (_IOC_NR(cmd
) > SNAPSHOT_IOC_MAXNR
)
206 if (!capable(CAP_SYS_ADMIN
))
209 if (!mutex_trylock(&pm_mutex
))
212 data
= filp
->private_data
;
216 case SNAPSHOT_FREEZE
:
220 printk("Syncing filesystems ... ");
224 error
= freeze_processes();
229 case SNAPSHOT_UNFREEZE
:
230 if (!data
->frozen
|| data
->ready
)
232 pm_restore_gfp_mask();
237 case SNAPSHOT_CREATE_IMAGE
:
238 if (data
->mode
!= O_RDONLY
|| !data
->frozen
|| data
->ready
) {
242 pm_restore_gfp_mask();
243 error
= hibernation_snapshot(data
->platform_support
);
245 error
= put_user(in_suspend
, (int __user
*)arg
);
246 data
->ready
= !freezer_test_done
&& !error
;
247 freezer_test_done
= false;
251 case SNAPSHOT_ATOMIC_RESTORE
:
252 snapshot_write_finalize(&data
->handle
);
253 if (data
->mode
!= O_WRONLY
|| !data
->frozen
||
254 !snapshot_image_loaded(&data
->handle
)) {
258 error
= hibernation_restore(data
->platform_support
);
263 memset(&data
->handle
, 0, sizeof(struct snapshot_handle
));
266 * It is necessary to thaw kernel threads here, because
267 * SNAPSHOT_CREATE_IMAGE may be invoked directly after
268 * SNAPSHOT_FREE. In that case, if kernel threads were not
269 * thawed, the preallocation of memory carried out by
270 * hibernation_snapshot() might run into problems (i.e. it
271 * might fail or even deadlock).
273 thaw_kernel_threads();
276 case SNAPSHOT_PREF_IMAGE_SIZE
:
280 case SNAPSHOT_GET_IMAGE_SIZE
:
285 size
= snapshot_get_image_size();
287 error
= put_user(size
, (loff_t __user
*)arg
);
290 case SNAPSHOT_AVAIL_SWAP_SIZE
:
291 size
= count_swap_pages(data
->swap
, 1);
293 error
= put_user(size
, (loff_t __user
*)arg
);
296 case SNAPSHOT_ALLOC_SWAP_PAGE
:
297 if (data
->swap
< 0 || data
->swap
>= MAX_SWAPFILES
) {
301 offset
= alloc_swapdev_block(data
->swap
);
303 offset
<<= PAGE_SHIFT
;
304 error
= put_user(offset
, (loff_t __user
*)arg
);
310 case SNAPSHOT_FREE_SWAP_PAGES
:
311 if (data
->swap
< 0 || data
->swap
>= MAX_SWAPFILES
) {
315 free_all_swap_pages(data
->swap
);
324 * Tasks are frozen and the notifiers have been called with
325 * PM_HIBERNATION_PREPARE
327 error
= suspend_devices_and_enter(PM_SUSPEND_MEM
);
331 case SNAPSHOT_PLATFORM_SUPPORT
:
332 data
->platform_support
= !!arg
;
335 case SNAPSHOT_POWER_OFF
:
336 if (data
->platform_support
)
337 error
= hibernation_platform_enter();
340 case SNAPSHOT_SET_SWAP_AREA
:
341 if (swsusp_swap_in_use()) {
344 struct resume_swap_area swap_area
;
347 error
= copy_from_user(&swap_area
, (void __user
*)arg
,
348 sizeof(struct resume_swap_area
));
355 * User space encodes device types as two-byte values,
356 * so we need to recode them
358 swdev
= new_decode_dev(swap_area
.dev
);
360 offset
= swap_area
.offset
;
361 data
->swap
= swap_type_of(swdev
, offset
, NULL
);
376 mutex_unlock(&pm_mutex
);
383 struct compat_resume_swap_area
{
384 compat_loff_t offset
;
389 snapshot_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
391 BUILD_BUG_ON(sizeof(loff_t
) != sizeof(compat_loff_t
));
394 case SNAPSHOT_GET_IMAGE_SIZE
:
395 case SNAPSHOT_AVAIL_SWAP_SIZE
:
396 case SNAPSHOT_ALLOC_SWAP_PAGE
: {
397 compat_loff_t __user
*uoffset
= compat_ptr(arg
);
404 err
= snapshot_ioctl(file
, cmd
, (unsigned long) &offset
);
406 if (!err
&& put_user(offset
, uoffset
))
411 case SNAPSHOT_CREATE_IMAGE
:
412 return snapshot_ioctl(file
, cmd
,
413 (unsigned long) compat_ptr(arg
));
415 case SNAPSHOT_SET_SWAP_AREA
: {
416 struct compat_resume_swap_area __user
*u_swap_area
=
418 struct resume_swap_area swap_area
;
422 err
= get_user(swap_area
.offset
, &u_swap_area
->offset
);
423 err
|= get_user(swap_area
.dev
, &u_swap_area
->dev
);
428 err
= snapshot_ioctl(file
, SNAPSHOT_SET_SWAP_AREA
,
429 (unsigned long) &swap_area
);
435 return snapshot_ioctl(file
, cmd
, arg
);
439 #endif /* CONFIG_COMPAT */
441 static const struct file_operations snapshot_fops
= {
442 .open
= snapshot_open
,
443 .release
= snapshot_release
,
444 .read
= snapshot_read
,
445 .write
= snapshot_write
,
447 .unlocked_ioctl
= snapshot_ioctl
,
449 .compat_ioctl
= snapshot_compat_ioctl
,
453 static struct miscdevice snapshot_device
= {
454 .minor
= SNAPSHOT_MINOR
,
456 .fops
= &snapshot_fops
,
459 static int __init
snapshot_device_init(void)
461 return misc_register(&snapshot_device
);
464 device_initcall(snapshot_device_init
);