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/compat.h>
25 #include <linux/console.h>
26 #include <linux/cpu.h>
27 #include <linux/freezer.h>
28 #include <scsi/scsi_scan.h>
30 #include <asm/uaccess.h>
35 #define SNAPSHOT_MINOR 231
37 static struct snapshot_data
{
38 struct snapshot_handle handle
;
43 char platform_support
;
46 atomic_t snapshot_device_available
= ATOMIC_INIT(1);
48 static int snapshot_open(struct inode
*inode
, struct file
*filp
)
50 struct snapshot_data
*data
;
55 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
60 if ((filp
->f_flags
& O_ACCMODE
) == O_RDWR
) {
61 atomic_inc(&snapshot_device_available
);
65 if(create_basic_memory_bitmaps()) {
66 atomic_inc(&snapshot_device_available
);
70 nonseekable_open(inode
, filp
);
71 data
= &snapshot_state
;
72 filp
->private_data
= data
;
73 memset(&data
->handle
, 0, sizeof(struct snapshot_handle
));
74 if ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
75 /* Hibernating. The image device should be accessible. */
76 data
->swap
= swsusp_resume_device
?
77 swap_type_of(swsusp_resume_device
, 0, NULL
) : -1;
78 data
->mode
= O_RDONLY
;
79 error
= pm_notifier_call_chain(PM_HIBERNATION_PREPARE
);
81 pm_notifier_call_chain(PM_POST_HIBERNATION
);
84 * Resuming. We may need to wait for the image device to
87 wait_for_device_probe();
88 scsi_complete_async_scans();
91 data
->mode
= O_WRONLY
;
92 error
= pm_notifier_call_chain(PM_RESTORE_PREPARE
);
94 pm_notifier_call_chain(PM_POST_RESTORE
);
97 free_basic_memory_bitmaps();
98 atomic_inc(&snapshot_device_available
);
102 data
->platform_support
= 0;
105 unlock_system_sleep();
110 static int snapshot_release(struct inode
*inode
, struct file
*filp
)
112 struct snapshot_data
*data
;
117 free_basic_memory_bitmaps();
118 data
= filp
->private_data
;
119 free_all_swap_pages(data
->swap
);
121 pm_restore_gfp_mask();
124 pm_notifier_call_chain(data
->mode
== O_RDONLY
?
125 PM_POST_HIBERNATION
: PM_POST_RESTORE
);
126 atomic_inc(&snapshot_device_available
);
128 unlock_system_sleep();
133 static ssize_t
snapshot_read(struct file
*filp
, char __user
*buf
,
134 size_t count
, loff_t
*offp
)
136 struct snapshot_data
*data
;
138 loff_t pg_offp
= *offp
& ~PAGE_MASK
;
142 data
= filp
->private_data
;
147 if (!pg_offp
) { /* on page boundary? */
148 res
= snapshot_read_next(&data
->handle
);
152 res
= PAGE_SIZE
- pg_offp
;
155 res
= simple_read_from_buffer(buf
, count
, &pg_offp
,
156 data_of(data
->handle
), res
);
161 unlock_system_sleep();
166 static ssize_t
snapshot_write(struct file
*filp
, const char __user
*buf
,
167 size_t count
, loff_t
*offp
)
169 struct snapshot_data
*data
;
171 loff_t pg_offp
= *offp
& ~PAGE_MASK
;
175 data
= filp
->private_data
;
178 res
= snapshot_write_next(&data
->handle
);
182 res
= PAGE_SIZE
- pg_offp
;
185 res
= simple_write_to_buffer(data_of(data
->handle
), res
, &pg_offp
,
190 unlock_system_sleep();
195 static long snapshot_ioctl(struct file
*filp
, unsigned int cmd
,
199 struct snapshot_data
*data
;
203 if (_IOC_TYPE(cmd
) != SNAPSHOT_IOC_MAGIC
)
205 if (_IOC_NR(cmd
) > SNAPSHOT_IOC_MAXNR
)
207 if (!capable(CAP_SYS_ADMIN
))
210 if (!mutex_trylock(&pm_mutex
))
213 data
= filp
->private_data
;
217 case SNAPSHOT_FREEZE
:
221 printk("Syncing filesystems ... ");
225 error
= usermodehelper_disable();
229 error
= freeze_processes();
231 usermodehelper_enable();
236 case SNAPSHOT_UNFREEZE
:
237 if (!data
->frozen
|| data
->ready
)
239 pm_restore_gfp_mask();
241 usermodehelper_enable();
245 case SNAPSHOT_CREATE_IMAGE
:
246 if (data
->mode
!= O_RDONLY
|| !data
->frozen
|| data
->ready
) {
250 pm_restore_gfp_mask();
251 error
= hibernation_snapshot(data
->platform_support
);
253 thaw_kernel_threads();
255 error
= put_user(in_suspend
, (int __user
*)arg
);
256 if (!error
&& !freezer_test_done
)
258 if (freezer_test_done
) {
259 freezer_test_done
= false;
260 thaw_kernel_threads();
265 case SNAPSHOT_ATOMIC_RESTORE
:
266 snapshot_write_finalize(&data
->handle
);
267 if (data
->mode
!= O_WRONLY
|| !data
->frozen
||
268 !snapshot_image_loaded(&data
->handle
)) {
272 error
= hibernation_restore(data
->platform_support
);
277 memset(&data
->handle
, 0, sizeof(struct snapshot_handle
));
280 * It is necessary to thaw kernel threads here, because
281 * SNAPSHOT_CREATE_IMAGE may be invoked directly after
282 * SNAPSHOT_FREE. In that case, if kernel threads were not
283 * thawed, the preallocation of memory carried out by
284 * hibernation_snapshot() might run into problems (i.e. it
285 * might fail or even deadlock).
287 thaw_kernel_threads();
290 case SNAPSHOT_PREF_IMAGE_SIZE
:
294 case SNAPSHOT_GET_IMAGE_SIZE
:
299 size
= snapshot_get_image_size();
301 error
= put_user(size
, (loff_t __user
*)arg
);
304 case SNAPSHOT_AVAIL_SWAP_SIZE
:
305 size
= count_swap_pages(data
->swap
, 1);
307 error
= put_user(size
, (loff_t __user
*)arg
);
310 case SNAPSHOT_ALLOC_SWAP_PAGE
:
311 if (data
->swap
< 0 || data
->swap
>= MAX_SWAPFILES
) {
315 offset
= alloc_swapdev_block(data
->swap
);
317 offset
<<= PAGE_SHIFT
;
318 error
= put_user(offset
, (loff_t __user
*)arg
);
324 case SNAPSHOT_FREE_SWAP_PAGES
:
325 if (data
->swap
< 0 || data
->swap
>= MAX_SWAPFILES
) {
329 free_all_swap_pages(data
->swap
);
338 * Tasks are frozen and the notifiers have been called with
339 * PM_HIBERNATION_PREPARE
341 error
= suspend_devices_and_enter(PM_SUSPEND_MEM
);
345 case SNAPSHOT_PLATFORM_SUPPORT
:
346 data
->platform_support
= !!arg
;
349 case SNAPSHOT_POWER_OFF
:
350 if (data
->platform_support
)
351 error
= hibernation_platform_enter();
354 case SNAPSHOT_SET_SWAP_AREA
:
355 if (swsusp_swap_in_use()) {
358 struct resume_swap_area swap_area
;
361 error
= copy_from_user(&swap_area
, (void __user
*)arg
,
362 sizeof(struct resume_swap_area
));
369 * User space encodes device types as two-byte values,
370 * so we need to recode them
372 swdev
= new_decode_dev(swap_area
.dev
);
374 offset
= swap_area
.offset
;
375 data
->swap
= swap_type_of(swdev
, offset
, NULL
);
390 mutex_unlock(&pm_mutex
);
397 struct compat_resume_swap_area
{
398 compat_loff_t offset
;
403 snapshot_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
405 BUILD_BUG_ON(sizeof(loff_t
) != sizeof(compat_loff_t
));
408 case SNAPSHOT_GET_IMAGE_SIZE
:
409 case SNAPSHOT_AVAIL_SWAP_SIZE
:
410 case SNAPSHOT_ALLOC_SWAP_PAGE
: {
411 compat_loff_t __user
*uoffset
= compat_ptr(arg
);
418 err
= snapshot_ioctl(file
, cmd
, (unsigned long) &offset
);
420 if (!err
&& put_user(offset
, uoffset
))
425 case SNAPSHOT_CREATE_IMAGE
:
426 return snapshot_ioctl(file
, cmd
,
427 (unsigned long) compat_ptr(arg
));
429 case SNAPSHOT_SET_SWAP_AREA
: {
430 struct compat_resume_swap_area __user
*u_swap_area
=
432 struct resume_swap_area swap_area
;
436 err
= get_user(swap_area
.offset
, &u_swap_area
->offset
);
437 err
|= get_user(swap_area
.dev
, &u_swap_area
->dev
);
442 err
= snapshot_ioctl(file
, SNAPSHOT_SET_SWAP_AREA
,
443 (unsigned long) &swap_area
);
449 return snapshot_ioctl(file
, cmd
, arg
);
453 #endif /* CONFIG_COMPAT */
455 static const struct file_operations snapshot_fops
= {
456 .open
= snapshot_open
,
457 .release
= snapshot_release
,
458 .read
= snapshot_read
,
459 .write
= snapshot_write
,
461 .unlocked_ioctl
= snapshot_ioctl
,
463 .compat_ioctl
= snapshot_compat_ioctl
,
467 static struct miscdevice snapshot_device
= {
468 .minor
= SNAPSHOT_MINOR
,
470 .fops
= &snapshot_fops
,
473 static int __init
snapshot_device_init(void)
475 return misc_register(&snapshot_device
);
478 device_initcall(snapshot_device_init
);