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