Commit | Line | Data |
---|---|---|
6e1819d6 RW |
1 | /* |
2 | * linux/kernel/power/user.c | |
3 | * | |
4 | * This file provides the user space interface for software suspend/resume. | |
5 | * | |
6 | * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl> | |
7 | * | |
8 | * This file is released under the GPLv2. | |
9 | * | |
10 | */ | |
11 | ||
12 | #include <linux/suspend.h> | |
13 | #include <linux/syscalls.h> | |
3592695c | 14 | #include <linux/reboot.h> |
74da1ff7 | 15 | #include <linux/kmod.h> |
6e1819d6 RW |
16 | #include <linux/string.h> |
17 | #include <linux/device.h> | |
18 | #include <linux/miscdevice.h> | |
19 | #include <linux/mm.h> | |
20 | #include <linux/swap.h> | |
21 | #include <linux/swapops.h> | |
22 | #include <linux/pm.h> | |
23 | #include <linux/fs.h> | |
c336078b | 24 | #include <linux/compat.h> |
97c7801c | 25 | #include <linux/console.h> |
e3920fb4 | 26 | #include <linux/cpu.h> |
7dfb7103 | 27 | #include <linux/freezer.h> |
c7510859 | 28 | #include <scsi/scsi_scan.h> |
6e1819d6 RW |
29 | |
30 | #include <asm/uaccess.h> | |
31 | ||
32 | #include "power.h" | |
33 | ||
eb57c1cf | 34 | |
6e1819d6 RW |
35 | #define SNAPSHOT_MINOR 231 |
36 | ||
37 | static struct snapshot_data { | |
38 | struct snapshot_handle handle; | |
39 | int swap; | |
6e1819d6 RW |
40 | int mode; |
41 | char frozen; | |
42 | char ready; | |
eb57c1cf | 43 | char platform_support; |
6e1819d6 RW |
44 | } snapshot_state; |
45 | ||
0709db60 | 46 | atomic_t snapshot_device_available = ATOMIC_INIT(1); |
6e1819d6 RW |
47 | |
48 | static int snapshot_open(struct inode *inode, struct file *filp) | |
49 | { | |
50 | struct snapshot_data *data; | |
c3e94d89 | 51 | int error; |
6e1819d6 | 52 | |
bcda53fa | 53 | lock_system_sleep(); |
25f2f3da RW |
54 | |
55 | if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { | |
56 | error = -EBUSY; | |
57 | goto Unlock; | |
58 | } | |
6e1819d6 | 59 | |
1525a2ad | 60 | if ((filp->f_flags & O_ACCMODE) == O_RDWR) { |
0709db60 | 61 | atomic_inc(&snapshot_device_available); |
25f2f3da RW |
62 | error = -ENOSYS; |
63 | goto Unlock; | |
1525a2ad RW |
64 | } |
65 | if(create_basic_memory_bitmaps()) { | |
0709db60 | 66 | atomic_inc(&snapshot_device_available); |
25f2f3da RW |
67 | error = -ENOMEM; |
68 | goto Unlock; | |
1525a2ad | 69 | } |
6e1819d6 RW |
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) { | |
c7510859 | 75 | /* Hibernating. The image device should be accessible. */ |
915bae9e | 76 | data->swap = swsusp_resume_device ? |
7bf23687 | 77 | swap_type_of(swsusp_resume_device, 0, NULL) : -1; |
6e1819d6 | 78 | data->mode = O_RDONLY; |
ebae2604 | 79 | error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE); |
c3e94d89 | 80 | if (error) |
ebae2604 | 81 | pm_notifier_call_chain(PM_POST_HIBERNATION); |
6e1819d6 | 82 | } else { |
c7510859 RW |
83 | /* |
84 | * Resuming. We may need to wait for the image device to | |
85 | * appear. | |
86 | */ | |
87 | wait_for_device_probe(); | |
88 | scsi_complete_async_scans(); | |
89 | ||
6e1819d6 RW |
90 | data->swap = -1; |
91 | data->mode = O_WRONLY; | |
ebae2604 | 92 | error = pm_notifier_call_chain(PM_RESTORE_PREPARE); |
c3e94d89 | 93 | if (error) |
ebae2604 | 94 | pm_notifier_call_chain(PM_POST_RESTORE); |
c3e94d89 | 95 | } |
8440f4b1 MK |
96 | if (error) { |
97 | free_basic_memory_bitmaps(); | |
c3e94d89 | 98 | atomic_inc(&snapshot_device_available); |
8440f4b1 | 99 | } |
6e1819d6 RW |
100 | data->frozen = 0; |
101 | data->ready = 0; | |
eb57c1cf | 102 | data->platform_support = 0; |
6e1819d6 | 103 | |
25f2f3da | 104 | Unlock: |
bcda53fa | 105 | unlock_system_sleep(); |
25f2f3da RW |
106 | |
107 | return error; | |
6e1819d6 RW |
108 | } |
109 | ||
110 | static int snapshot_release(struct inode *inode, struct file *filp) | |
111 | { | |
112 | struct snapshot_data *data; | |
113 | ||
bcda53fa | 114 | lock_system_sleep(); |
25f2f3da | 115 | |
6e1819d6 | 116 | swsusp_free(); |
74dfd666 | 117 | free_basic_memory_bitmaps(); |
6e1819d6 | 118 | data = filp->private_data; |
d1d241cc | 119 | free_all_swap_pages(data->swap); |
9744997a RW |
120 | if (data->frozen) { |
121 | pm_restore_gfp_mask(); | |
6e1819d6 | 122 | thaw_processes(); |
9744997a | 123 | } |
1497dd1d | 124 | pm_notifier_call_chain(data->mode == O_RDONLY ? |
c3e94d89 | 125 | PM_POST_HIBERNATION : PM_POST_RESTORE); |
0709db60 | 126 | atomic_inc(&snapshot_device_available); |
25f2f3da | 127 | |
bcda53fa | 128 | unlock_system_sleep(); |
25f2f3da | 129 | |
6e1819d6 RW |
130 | return 0; |
131 | } | |
132 | ||
133 | static ssize_t snapshot_read(struct file *filp, char __user *buf, | |
134 | size_t count, loff_t *offp) | |
135 | { | |
136 | struct snapshot_data *data; | |
137 | ssize_t res; | |
d3c1b24c | 138 | loff_t pg_offp = *offp & ~PAGE_MASK; |
6e1819d6 | 139 | |
bcda53fa | 140 | lock_system_sleep(); |
25f2f3da | 141 | |
6e1819d6 | 142 | data = filp->private_data; |
25f2f3da RW |
143 | if (!data->ready) { |
144 | res = -ENODATA; | |
145 | goto Unlock; | |
146 | } | |
d3c1b24c JS |
147 | if (!pg_offp) { /* on page boundary? */ |
148 | res = snapshot_read_next(&data->handle); | |
149 | if (res <= 0) | |
150 | goto Unlock; | |
151 | } else { | |
152 | res = PAGE_SIZE - pg_offp; | |
6e1819d6 | 153 | } |
25f2f3da | 154 | |
d3c1b24c JS |
155 | res = simple_read_from_buffer(buf, count, &pg_offp, |
156 | data_of(data->handle), res); | |
157 | if (res > 0) | |
158 | *offp += res; | |
159 | ||
25f2f3da | 160 | Unlock: |
bcda53fa | 161 | unlock_system_sleep(); |
25f2f3da | 162 | |
6e1819d6 RW |
163 | return res; |
164 | } | |
165 | ||
166 | static ssize_t snapshot_write(struct file *filp, const char __user *buf, | |
167 | size_t count, loff_t *offp) | |
168 | { | |
169 | struct snapshot_data *data; | |
170 | ssize_t res; | |
d3c1b24c | 171 | loff_t pg_offp = *offp & ~PAGE_MASK; |
6e1819d6 | 172 | |
bcda53fa | 173 | lock_system_sleep(); |
25f2f3da | 174 | |
6e1819d6 | 175 | data = filp->private_data; |
d3c1b24c JS |
176 | |
177 | if (!pg_offp) { | |
178 | res = snapshot_write_next(&data->handle); | |
179 | if (res <= 0) | |
180 | goto unlock; | |
181 | } else { | |
182 | res = PAGE_SIZE - pg_offp; | |
6e1819d6 | 183 | } |
25f2f3da | 184 | |
d3c1b24c JS |
185 | res = simple_write_to_buffer(data_of(data->handle), res, &pg_offp, |
186 | buf, count); | |
187 | if (res > 0) | |
188 | *offp += res; | |
189 | unlock: | |
bcda53fa | 190 | unlock_system_sleep(); |
25f2f3da | 191 | |
6e1819d6 RW |
192 | return res; |
193 | } | |
194 | ||
52d11025 AC |
195 | static long snapshot_ioctl(struct file *filp, unsigned int cmd, |
196 | unsigned long arg) | |
6e1819d6 RW |
197 | { |
198 | int error = 0; | |
199 | struct snapshot_data *data; | |
af508b34 | 200 | loff_t size; |
3aef83e0 | 201 | sector_t offset; |
6e1819d6 RW |
202 | |
203 | if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC) | |
204 | return -ENOTTY; | |
205 | if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR) | |
206 | return -ENOTTY; | |
207 | if (!capable(CAP_SYS_ADMIN)) | |
208 | return -EPERM; | |
209 | ||
25f2f3da RW |
210 | if (!mutex_trylock(&pm_mutex)) |
211 | return -EBUSY; | |
6e1819d6 | 212 | |
25f2f3da | 213 | data = filp->private_data; |
52d11025 | 214 | |
6e1819d6 RW |
215 | switch (cmd) { |
216 | ||
217 | case SNAPSHOT_FREEZE: | |
218 | if (data->frozen) | |
219 | break; | |
1bfcf130 | 220 | |
c3e94d89 AS |
221 | printk("Syncing filesystems ... "); |
222 | sys_sync(); | |
223 | printk("done.\n"); | |
224 | ||
1bfcf130 | 225 | error = usermodehelper_disable(); |
b10d9117 | 226 | if (error) |
1bfcf130 RW |
227 | break; |
228 | ||
229 | error = freeze_processes(); | |
03afed8b | 230 | if (error) |
1bfcf130 | 231 | usermodehelper_enable(); |
e5b16746 | 232 | else |
6e1819d6 RW |
233 | data->frozen = 1; |
234 | break; | |
235 | ||
236 | case SNAPSHOT_UNFREEZE: | |
2f41dddb | 237 | if (!data->frozen || data->ready) |
6e1819d6 | 238 | break; |
c9e664f1 | 239 | pm_restore_gfp_mask(); |
6e1819d6 | 240 | thaw_processes(); |
1bfcf130 | 241 | usermodehelper_enable(); |
6e1819d6 RW |
242 | data->frozen = 0; |
243 | break; | |
244 | ||
b694e52e | 245 | case SNAPSHOT_CREATE_IMAGE: |
6e1819d6 RW |
246 | if (data->mode != O_RDONLY || !data->frozen || data->ready) { |
247 | error = -EPERM; | |
248 | break; | |
249 | } | |
c9e664f1 | 250 | pm_restore_gfp_mask(); |
eb57c1cf | 251 | error = hibernation_snapshot(data->platform_support); |
51d6ff7a | 252 | if (!error) { |
cc5d207c | 253 | error = put_user(in_suspend, (int __user *)arg); |
a556d5b5 SB |
254 | data->ready = !freezer_test_done && !error; |
255 | freezer_test_done = false; | |
97819a26 | 256 | } |
6e1819d6 RW |
257 | break; |
258 | ||
259 | case SNAPSHOT_ATOMIC_RESTORE: | |
8357376d | 260 | snapshot_write_finalize(&data->handle); |
6e1819d6 RW |
261 | if (data->mode != O_WRONLY || !data->frozen || |
262 | !snapshot_image_loaded(&data->handle)) { | |
263 | error = -EPERM; | |
264 | break; | |
265 | } | |
eb57c1cf | 266 | error = hibernation_restore(data->platform_support); |
6e1819d6 RW |
267 | break; |
268 | ||
269 | case SNAPSHOT_FREE: | |
270 | swsusp_free(); | |
271 | memset(&data->handle, 0, sizeof(struct snapshot_handle)); | |
272 | data->ready = 0; | |
181e9bde RW |
273 | /* |
274 | * It is necessary to thaw kernel threads here, because | |
275 | * SNAPSHOT_CREATE_IMAGE may be invoked directly after | |
276 | * SNAPSHOT_FREE. In that case, if kernel threads were not | |
277 | * thawed, the preallocation of memory carried out by | |
278 | * hibernation_snapshot() might run into problems (i.e. it | |
279 | * might fail or even deadlock). | |
280 | */ | |
281 | thaw_kernel_threads(); | |
6e1819d6 RW |
282 | break; |
283 | ||
b694e52e | 284 | case SNAPSHOT_PREF_IMAGE_SIZE: |
6e1819d6 RW |
285 | image_size = arg; |
286 | break; | |
287 | ||
af508b34 RW |
288 | case SNAPSHOT_GET_IMAGE_SIZE: |
289 | if (!data->ready) { | |
290 | error = -ENODATA; | |
291 | break; | |
292 | } | |
293 | size = snapshot_get_image_size(); | |
294 | size <<= PAGE_SHIFT; | |
295 | error = put_user(size, (loff_t __user *)arg); | |
296 | break; | |
297 | ||
b694e52e | 298 | case SNAPSHOT_AVAIL_SWAP_SIZE: |
af508b34 RW |
299 | size = count_swap_pages(data->swap, 1); |
300 | size <<= PAGE_SHIFT; | |
301 | error = put_user(size, (loff_t __user *)arg); | |
6e1819d6 RW |
302 | break; |
303 | ||
b694e52e | 304 | case SNAPSHOT_ALLOC_SWAP_PAGE: |
6e1819d6 RW |
305 | if (data->swap < 0 || data->swap >= MAX_SWAPFILES) { |
306 | error = -ENODEV; | |
307 | break; | |
308 | } | |
d1d241cc | 309 | offset = alloc_swapdev_block(data->swap); |
6e1819d6 RW |
310 | if (offset) { |
311 | offset <<= PAGE_SHIFT; | |
cc5d207c | 312 | error = put_user(offset, (loff_t __user *)arg); |
6e1819d6 RW |
313 | } else { |
314 | error = -ENOSPC; | |
315 | } | |
316 | break; | |
317 | ||
318 | case SNAPSHOT_FREE_SWAP_PAGES: | |
319 | if (data->swap < 0 || data->swap >= MAX_SWAPFILES) { | |
320 | error = -ENODEV; | |
321 | break; | |
322 | } | |
d1d241cc | 323 | free_all_swap_pages(data->swap); |
6e1819d6 RW |
324 | break; |
325 | ||
9b238205 LT |
326 | case SNAPSHOT_S2RAM: |
327 | if (!data->frozen) { | |
328 | error = -EPERM; | |
329 | break; | |
330 | } | |
6c961dfb RW |
331 | /* |
332 | * Tasks are frozen and the notifiers have been called with | |
333 | * PM_HIBERNATION_PREPARE | |
334 | */ | |
335 | error = suspend_devices_and_enter(PM_SUSPEND_MEM); | |
36cb7035 | 336 | data->ready = 0; |
9b238205 LT |
337 | break; |
338 | ||
eb57c1cf RW |
339 | case SNAPSHOT_PLATFORM_SUPPORT: |
340 | data->platform_support = !!arg; | |
341 | break; | |
342 | ||
343 | case SNAPSHOT_POWER_OFF: | |
344 | if (data->platform_support) | |
345 | error = hibernation_platform_enter(); | |
346 | break; | |
347 | ||
37b2ba12 | 348 | case SNAPSHOT_SET_SWAP_AREA: |
d1d241cc | 349 | if (swsusp_swap_in_use()) { |
37b2ba12 RW |
350 | error = -EPERM; |
351 | } else { | |
352 | struct resume_swap_area swap_area; | |
353 | dev_t swdev; | |
354 | ||
355 | error = copy_from_user(&swap_area, (void __user *)arg, | |
356 | sizeof(struct resume_swap_area)); | |
357 | if (error) { | |
358 | error = -EFAULT; | |
359 | break; | |
360 | } | |
361 | ||
362 | /* | |
363 | * User space encodes device types as two-byte values, | |
364 | * so we need to recode them | |
365 | */ | |
d88d4050 | 366 | swdev = new_decode_dev(swap_area.dev); |
37b2ba12 RW |
367 | if (swdev) { |
368 | offset = swap_area.offset; | |
7bf23687 | 369 | data->swap = swap_type_of(swdev, offset, NULL); |
37b2ba12 RW |
370 | if (data->swap < 0) |
371 | error = -ENODEV; | |
372 | } else { | |
373 | data->swap = -1; | |
374 | error = -EINVAL; | |
375 | } | |
376 | } | |
377 | break; | |
378 | ||
6e1819d6 RW |
379 | default: |
380 | error = -ENOTTY; | |
381 | ||
382 | } | |
25f2f3da RW |
383 | |
384 | mutex_unlock(&pm_mutex); | |
385 | ||
6e1819d6 RW |
386 | return error; |
387 | } | |
388 | ||
c336078b BH |
389 | #ifdef CONFIG_COMPAT |
390 | ||
391 | struct compat_resume_swap_area { | |
392 | compat_loff_t offset; | |
393 | u32 dev; | |
394 | } __packed; | |
395 | ||
396 | static long | |
397 | snapshot_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
398 | { | |
399 | BUILD_BUG_ON(sizeof(loff_t) != sizeof(compat_loff_t)); | |
400 | ||
401 | switch (cmd) { | |
402 | case SNAPSHOT_GET_IMAGE_SIZE: | |
403 | case SNAPSHOT_AVAIL_SWAP_SIZE: | |
404 | case SNAPSHOT_ALLOC_SWAP_PAGE: { | |
405 | compat_loff_t __user *uoffset = compat_ptr(arg); | |
406 | loff_t offset; | |
407 | mm_segment_t old_fs; | |
408 | int err; | |
409 | ||
410 | old_fs = get_fs(); | |
411 | set_fs(KERNEL_DS); | |
412 | err = snapshot_ioctl(file, cmd, (unsigned long) &offset); | |
413 | set_fs(old_fs); | |
414 | if (!err && put_user(offset, uoffset)) | |
415 | err = -EFAULT; | |
416 | return err; | |
417 | } | |
418 | ||
419 | case SNAPSHOT_CREATE_IMAGE: | |
420 | return snapshot_ioctl(file, cmd, | |
421 | (unsigned long) compat_ptr(arg)); | |
422 | ||
423 | case SNAPSHOT_SET_SWAP_AREA: { | |
424 | struct compat_resume_swap_area __user *u_swap_area = | |
425 | compat_ptr(arg); | |
426 | struct resume_swap_area swap_area; | |
427 | mm_segment_t old_fs; | |
428 | int err; | |
429 | ||
430 | err = get_user(swap_area.offset, &u_swap_area->offset); | |
431 | err |= get_user(swap_area.dev, &u_swap_area->dev); | |
432 | if (err) | |
433 | return -EFAULT; | |
434 | old_fs = get_fs(); | |
435 | set_fs(KERNEL_DS); | |
436 | err = snapshot_ioctl(file, SNAPSHOT_SET_SWAP_AREA, | |
437 | (unsigned long) &swap_area); | |
438 | set_fs(old_fs); | |
439 | return err; | |
440 | } | |
441 | ||
442 | default: | |
443 | return snapshot_ioctl(file, cmd, arg); | |
444 | } | |
445 | } | |
446 | ||
447 | #endif /* CONFIG_COMPAT */ | |
448 | ||
15ad7cdc | 449 | static const struct file_operations snapshot_fops = { |
6e1819d6 RW |
450 | .open = snapshot_open, |
451 | .release = snapshot_release, | |
452 | .read = snapshot_read, | |
453 | .write = snapshot_write, | |
454 | .llseek = no_llseek, | |
52d11025 | 455 | .unlocked_ioctl = snapshot_ioctl, |
c336078b BH |
456 | #ifdef CONFIG_COMPAT |
457 | .compat_ioctl = snapshot_compat_ioctl, | |
458 | #endif | |
6e1819d6 RW |
459 | }; |
460 | ||
461 | static struct miscdevice snapshot_device = { | |
462 | .minor = SNAPSHOT_MINOR, | |
463 | .name = "snapshot", | |
464 | .fops = &snapshot_fops, | |
465 | }; | |
466 | ||
467 | static int __init snapshot_device_init(void) | |
468 | { | |
469 | return misc_register(&snapshot_device); | |
470 | }; | |
471 | ||
472 | device_initcall(snapshot_device_init); |