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 thaw_kernel_threads();
247 error
= put_user(in_suspend
, (int __user
*)arg
);
248 if (!error
&& !freezer_test_done
)
250 if (freezer_test_done
) {
251 freezer_test_done
= false;
252 thaw_kernel_threads();
257 case SNAPSHOT_ATOMIC_RESTORE
:
258 snapshot_write_finalize(&data
->handle
);
259 if (data
->mode
!= O_WRONLY
|| !data
->frozen
||
260 !snapshot_image_loaded(&data
->handle
)) {
264 error
= hibernation_restore(data
->platform_support
);
269 memset(&data
->handle
, 0, sizeof(struct snapshot_handle
));
272 * It is necessary to thaw kernel threads here, because
273 * SNAPSHOT_CREATE_IMAGE may be invoked directly after
274 * SNAPSHOT_FREE. In that case, if kernel threads were not
275 * thawed, the preallocation of memory carried out by
276 * hibernation_snapshot() might run into problems (i.e. it
277 * might fail or even deadlock).
279 thaw_kernel_threads();
282 case SNAPSHOT_PREF_IMAGE_SIZE
:
286 case SNAPSHOT_GET_IMAGE_SIZE
:
291 size
= snapshot_get_image_size();
293 error
= put_user(size
, (loff_t __user
*)arg
);
296 case SNAPSHOT_AVAIL_SWAP_SIZE
:
297 size
= count_swap_pages(data
->swap
, 1);
299 error
= put_user(size
, (loff_t __user
*)arg
);
302 case SNAPSHOT_ALLOC_SWAP_PAGE
:
303 if (data
->swap
< 0 || data
->swap
>= MAX_SWAPFILES
) {
307 offset
= alloc_swapdev_block(data
->swap
);
309 offset
<<= PAGE_SHIFT
;
310 error
= put_user(offset
, (loff_t __user
*)arg
);
316 case SNAPSHOT_FREE_SWAP_PAGES
:
317 if (data
->swap
< 0 || data
->swap
>= MAX_SWAPFILES
) {
321 free_all_swap_pages(data
->swap
);
330 * Tasks are frozen and the notifiers have been called with
331 * PM_HIBERNATION_PREPARE
333 error
= suspend_devices_and_enter(PM_SUSPEND_MEM
);
337 case SNAPSHOT_PLATFORM_SUPPORT
:
338 data
->platform_support
= !!arg
;
341 case SNAPSHOT_POWER_OFF
:
342 if (data
->platform_support
)
343 error
= hibernation_platform_enter();
346 case SNAPSHOT_SET_SWAP_AREA
:
347 if (swsusp_swap_in_use()) {
350 struct resume_swap_area swap_area
;
353 error
= copy_from_user(&swap_area
, (void __user
*)arg
,
354 sizeof(struct resume_swap_area
));
361 * User space encodes device types as two-byte values,
362 * so we need to recode them
364 swdev
= new_decode_dev(swap_area
.dev
);
366 offset
= swap_area
.offset
;
367 data
->swap
= swap_type_of(swdev
, offset
, NULL
);
382 mutex_unlock(&pm_mutex
);
389 struct compat_resume_swap_area
{
390 compat_loff_t offset
;
395 snapshot_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
397 BUILD_BUG_ON(sizeof(loff_t
) != sizeof(compat_loff_t
));
400 case SNAPSHOT_GET_IMAGE_SIZE
:
401 case SNAPSHOT_AVAIL_SWAP_SIZE
:
402 case SNAPSHOT_ALLOC_SWAP_PAGE
: {
403 compat_loff_t __user
*uoffset
= compat_ptr(arg
);
410 err
= snapshot_ioctl(file
, cmd
, (unsigned long) &offset
);
412 if (!err
&& put_user(offset
, uoffset
))
417 case SNAPSHOT_CREATE_IMAGE
:
418 return snapshot_ioctl(file
, cmd
,
419 (unsigned long) compat_ptr(arg
));
421 case SNAPSHOT_SET_SWAP_AREA
: {
422 struct compat_resume_swap_area __user
*u_swap_area
=
424 struct resume_swap_area swap_area
;
428 err
= get_user(swap_area
.offset
, &u_swap_area
->offset
);
429 err
|= get_user(swap_area
.dev
, &u_swap_area
->dev
);
434 err
= snapshot_ioctl(file
, SNAPSHOT_SET_SWAP_AREA
,
435 (unsigned long) &swap_area
);
441 return snapshot_ioctl(file
, cmd
, arg
);
445 #endif /* CONFIG_COMPAT */
447 static const struct file_operations snapshot_fops
= {
448 .open
= snapshot_open
,
449 .release
= snapshot_release
,
450 .read
= snapshot_read
,
451 .write
= snapshot_write
,
453 .unlocked_ioctl
= snapshot_ioctl
,
455 .compat_ioctl
= snapshot_compat_ioctl
,
459 static struct miscdevice snapshot_device
= {
460 .minor
= SNAPSHOT_MINOR
,
462 .fops
= &snapshot_fops
,
465 static int __init
snapshot_device_init(void)
467 return misc_register(&snapshot_device
);
470 device_initcall(snapshot_device_init
);