]>
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> |
6e1819d6 RW |
15 | #include <linux/string.h> |
16 | #include <linux/device.h> | |
17 | #include <linux/miscdevice.h> | |
18 | #include <linux/mm.h> | |
19 | #include <linux/swap.h> | |
20 | #include <linux/swapops.h> | |
21 | #include <linux/pm.h> | |
22 | #include <linux/fs.h> | |
97c7801c | 23 | #include <linux/console.h> |
e3920fb4 | 24 | #include <linux/cpu.h> |
7dfb7103 | 25 | #include <linux/freezer.h> |
52d11025 | 26 | #include <linux/smp_lock.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 | } |
25f2f3da | 117 | if (error) |
c3e94d89 | 118 | atomic_inc(&snapshot_device_available); |
6e1819d6 RW |
119 | data->frozen = 0; |
120 | data->ready = 0; | |
eb57c1cf | 121 | data->platform_support = 0; |
6e1819d6 | 122 | |
25f2f3da RW |
123 | Unlock: |
124 | mutex_unlock(&pm_mutex); | |
125 | ||
126 | return error; | |
6e1819d6 RW |
127 | } |
128 | ||
129 | static int snapshot_release(struct inode *inode, struct file *filp) | |
130 | { | |
131 | struct snapshot_data *data; | |
132 | ||
25f2f3da RW |
133 | mutex_lock(&pm_mutex); |
134 | ||
6e1819d6 | 135 | swsusp_free(); |
74dfd666 | 136 | free_basic_memory_bitmaps(); |
6e1819d6 | 137 | data = filp->private_data; |
d1d241cc | 138 | free_all_swap_pages(data->swap); |
25f2f3da | 139 | if (data->frozen) |
6e1819d6 | 140 | thaw_processes(); |
c3e94d89 AS |
141 | pm_notifier_call_chain(data->mode == O_WRONLY ? |
142 | PM_POST_HIBERNATION : PM_POST_RESTORE); | |
0709db60 | 143 | atomic_inc(&snapshot_device_available); |
25f2f3da RW |
144 | |
145 | mutex_unlock(&pm_mutex); | |
146 | ||
6e1819d6 RW |
147 | return 0; |
148 | } | |
149 | ||
150 | static ssize_t snapshot_read(struct file *filp, char __user *buf, | |
151 | size_t count, loff_t *offp) | |
152 | { | |
153 | struct snapshot_data *data; | |
154 | ssize_t res; | |
155 | ||
25f2f3da RW |
156 | mutex_lock(&pm_mutex); |
157 | ||
6e1819d6 | 158 | data = filp->private_data; |
25f2f3da RW |
159 | if (!data->ready) { |
160 | res = -ENODATA; | |
161 | goto Unlock; | |
162 | } | |
6e1819d6 RW |
163 | res = snapshot_read_next(&data->handle, count); |
164 | if (res > 0) { | |
165 | if (copy_to_user(buf, data_of(data->handle), res)) | |
166 | res = -EFAULT; | |
167 | else | |
168 | *offp = data->handle.offset; | |
169 | } | |
25f2f3da RW |
170 | |
171 | Unlock: | |
172 | mutex_unlock(&pm_mutex); | |
173 | ||
6e1819d6 RW |
174 | return res; |
175 | } | |
176 | ||
177 | static ssize_t snapshot_write(struct file *filp, const char __user *buf, | |
178 | size_t count, loff_t *offp) | |
179 | { | |
180 | struct snapshot_data *data; | |
181 | ssize_t res; | |
182 | ||
25f2f3da RW |
183 | mutex_lock(&pm_mutex); |
184 | ||
6e1819d6 RW |
185 | data = filp->private_data; |
186 | res = snapshot_write_next(&data->handle, count); | |
187 | if (res > 0) { | |
188 | if (copy_from_user(data_of(data->handle), buf, res)) | |
189 | res = -EFAULT; | |
190 | else | |
191 | *offp = data->handle.offset; | |
192 | } | |
25f2f3da RW |
193 | |
194 | mutex_unlock(&pm_mutex); | |
195 | ||
6e1819d6 RW |
196 | return res; |
197 | } | |
198 | ||
52d11025 AC |
199 | static long snapshot_ioctl(struct file *filp, unsigned int cmd, |
200 | unsigned long arg) | |
6e1819d6 RW |
201 | { |
202 | int error = 0; | |
203 | struct snapshot_data *data; | |
af508b34 | 204 | loff_t size; |
3aef83e0 | 205 | sector_t offset; |
6e1819d6 RW |
206 | |
207 | if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC) | |
208 | return -ENOTTY; | |
209 | if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR) | |
210 | return -ENOTTY; | |
211 | if (!capable(CAP_SYS_ADMIN)) | |
212 | return -EPERM; | |
213 | ||
25f2f3da RW |
214 | if (!mutex_trylock(&pm_mutex)) |
215 | return -EBUSY; | |
6e1819d6 | 216 | |
25f2f3da | 217 | data = filp->private_data; |
52d11025 | 218 | |
6e1819d6 RW |
219 | switch (cmd) { |
220 | ||
221 | case SNAPSHOT_FREEZE: | |
222 | if (data->frozen) | |
223 | break; | |
1bfcf130 | 224 | |
c3e94d89 AS |
225 | printk("Syncing filesystems ... "); |
226 | sys_sync(); | |
227 | printk("done.\n"); | |
228 | ||
1bfcf130 | 229 | error = usermodehelper_disable(); |
b10d9117 | 230 | if (error) |
1bfcf130 RW |
231 | break; |
232 | ||
233 | error = freeze_processes(); | |
234 | if (error) { | |
c3e94d89 | 235 | thaw_processes(); |
1bfcf130 RW |
236 | usermodehelper_enable(); |
237 | } | |
6e1819d6 RW |
238 | if (!error) |
239 | data->frozen = 1; | |
240 | break; | |
241 | ||
242 | case SNAPSHOT_UNFREEZE: | |
2f41dddb | 243 | if (!data->frozen || data->ready) |
6e1819d6 | 244 | break; |
6e1819d6 | 245 | thaw_processes(); |
1bfcf130 | 246 | usermodehelper_enable(); |
6e1819d6 RW |
247 | data->frozen = 0; |
248 | break; | |
249 | ||
cc5d207c | 250 | case SNAPSHOT_CREATE_IMAGE: |
6e1819d6 RW |
251 | case SNAPSHOT_ATOMIC_SNAPSHOT: |
252 | if (data->mode != O_RDONLY || !data->frozen || data->ready) { | |
253 | error = -EPERM; | |
254 | break; | |
255 | } | |
eb57c1cf | 256 | error = hibernation_snapshot(data->platform_support); |
6e1819d6 | 257 | if (!error) |
cc5d207c | 258 | error = put_user(in_suspend, (int __user *)arg); |
6e1819d6 RW |
259 | if (!error) |
260 | data->ready = 1; | |
261 | break; | |
262 | ||
263 | case SNAPSHOT_ATOMIC_RESTORE: | |
8357376d | 264 | snapshot_write_finalize(&data->handle); |
6e1819d6 RW |
265 | if (data->mode != O_WRONLY || !data->frozen || |
266 | !snapshot_image_loaded(&data->handle)) { | |
267 | error = -EPERM; | |
268 | break; | |
269 | } | |
eb57c1cf | 270 | error = hibernation_restore(data->platform_support); |
6e1819d6 RW |
271 | break; |
272 | ||
273 | case SNAPSHOT_FREE: | |
274 | swsusp_free(); | |
275 | memset(&data->handle, 0, sizeof(struct snapshot_handle)); | |
276 | data->ready = 0; | |
277 | break; | |
278 | ||
cc5d207c | 279 | case SNAPSHOT_PREF_IMAGE_SIZE: |
6e1819d6 RW |
280 | case SNAPSHOT_SET_IMAGE_SIZE: |
281 | image_size = arg; | |
282 | break; | |
283 | ||
af508b34 RW |
284 | case SNAPSHOT_GET_IMAGE_SIZE: |
285 | if (!data->ready) { | |
286 | error = -ENODATA; | |
287 | break; | |
288 | } | |
289 | size = snapshot_get_image_size(); | |
290 | size <<= PAGE_SHIFT; | |
291 | error = put_user(size, (loff_t __user *)arg); | |
292 | break; | |
293 | ||
cc5d207c | 294 | case SNAPSHOT_AVAIL_SWAP_SIZE: |
6e1819d6 | 295 | case SNAPSHOT_AVAIL_SWAP: |
af508b34 RW |
296 | size = count_swap_pages(data->swap, 1); |
297 | size <<= PAGE_SHIFT; | |
298 | error = put_user(size, (loff_t __user *)arg); | |
6e1819d6 RW |
299 | break; |
300 | ||
cc5d207c | 301 | case SNAPSHOT_ALLOC_SWAP_PAGE: |
6e1819d6 RW |
302 | case SNAPSHOT_GET_SWAP_PAGE: |
303 | if (data->swap < 0 || data->swap >= MAX_SWAPFILES) { | |
304 | error = -ENODEV; | |
305 | break; | |
306 | } | |
d1d241cc | 307 | offset = alloc_swapdev_block(data->swap); |
6e1819d6 RW |
308 | if (offset) { |
309 | offset <<= PAGE_SHIFT; | |
cc5d207c | 310 | error = put_user(offset, (loff_t __user *)arg); |
6e1819d6 RW |
311 | } else { |
312 | error = -ENOSPC; | |
313 | } | |
314 | break; | |
315 | ||
316 | case SNAPSHOT_FREE_SWAP_PAGES: | |
317 | if (data->swap < 0 || data->swap >= MAX_SWAPFILES) { | |
318 | error = -ENODEV; | |
319 | break; | |
320 | } | |
d1d241cc | 321 | free_all_swap_pages(data->swap); |
6e1819d6 RW |
322 | break; |
323 | ||
96f73749 | 324 | case SNAPSHOT_SET_SWAP_FILE: /* This ioctl is deprecated */ |
d1d241cc | 325 | if (!swsusp_swap_in_use()) { |
6e1819d6 RW |
326 | /* |
327 | * User space encodes device types as two-byte values, | |
328 | * so we need to recode them | |
329 | */ | |
330 | if (old_decode_dev(arg)) { | |
7bf23687 RW |
331 | data->swap = swap_type_of(old_decode_dev(arg), |
332 | 0, NULL); | |
6e1819d6 RW |
333 | if (data->swap < 0) |
334 | error = -ENODEV; | |
335 | } else { | |
336 | data->swap = -1; | |
337 | error = -EINVAL; | |
338 | } | |
339 | } else { | |
340 | error = -EPERM; | |
341 | } | |
342 | break; | |
343 | ||
9b238205 LT |
344 | case SNAPSHOT_S2RAM: |
345 | if (!data->frozen) { | |
346 | error = -EPERM; | |
347 | break; | |
348 | } | |
6c961dfb RW |
349 | /* |
350 | * Tasks are frozen and the notifiers have been called with | |
351 | * PM_HIBERNATION_PREPARE | |
352 | */ | |
353 | error = suspend_devices_and_enter(PM_SUSPEND_MEM); | |
9b238205 LT |
354 | break; |
355 | ||
eb57c1cf RW |
356 | case SNAPSHOT_PLATFORM_SUPPORT: |
357 | data->platform_support = !!arg; | |
358 | break; | |
359 | ||
360 | case SNAPSHOT_POWER_OFF: | |
361 | if (data->platform_support) | |
362 | error = hibernation_platform_enter(); | |
363 | break; | |
364 | ||
365 | case SNAPSHOT_PMOPS: /* This ioctl is deprecated */ | |
2b5b09b3 RW |
366 | error = -EINVAL; |
367 | ||
3592695c SS |
368 | switch (arg) { |
369 | ||
370 | case PMOPS_PREPARE: | |
eb57c1cf | 371 | data->platform_support = 1; |
7777fab9 | 372 | error = 0; |
3592695c SS |
373 | break; |
374 | ||
375 | case PMOPS_ENTER: | |
eb57c1cf | 376 | if (data->platform_support) |
7777fab9 | 377 | error = hibernation_platform_enter(); |
3592695c SS |
378 | break; |
379 | ||
380 | case PMOPS_FINISH: | |
eb57c1cf | 381 | if (data->platform_support) |
2b5b09b3 | 382 | error = 0; |
3592695c SS |
383 | break; |
384 | ||
385 | default: | |
386 | printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg); | |
3592695c SS |
387 | |
388 | } | |
389 | break; | |
390 | ||
37b2ba12 | 391 | case SNAPSHOT_SET_SWAP_AREA: |
d1d241cc | 392 | if (swsusp_swap_in_use()) { |
37b2ba12 RW |
393 | error = -EPERM; |
394 | } else { | |
395 | struct resume_swap_area swap_area; | |
396 | dev_t swdev; | |
397 | ||
398 | error = copy_from_user(&swap_area, (void __user *)arg, | |
399 | sizeof(struct resume_swap_area)); | |
400 | if (error) { | |
401 | error = -EFAULT; | |
402 | break; | |
403 | } | |
404 | ||
405 | /* | |
406 | * User space encodes device types as two-byte values, | |
407 | * so we need to recode them | |
408 | */ | |
409 | swdev = old_decode_dev(swap_area.dev); | |
410 | if (swdev) { | |
411 | offset = swap_area.offset; | |
7bf23687 | 412 | data->swap = swap_type_of(swdev, offset, NULL); |
37b2ba12 RW |
413 | if (data->swap < 0) |
414 | error = -ENODEV; | |
415 | } else { | |
416 | data->swap = -1; | |
417 | error = -EINVAL; | |
418 | } | |
419 | } | |
420 | break; | |
421 | ||
6e1819d6 RW |
422 | default: |
423 | error = -ENOTTY; | |
424 | ||
425 | } | |
25f2f3da RW |
426 | |
427 | mutex_unlock(&pm_mutex); | |
428 | ||
6e1819d6 RW |
429 | return error; |
430 | } | |
431 | ||
15ad7cdc | 432 | static const struct file_operations snapshot_fops = { |
6e1819d6 RW |
433 | .open = snapshot_open, |
434 | .release = snapshot_release, | |
435 | .read = snapshot_read, | |
436 | .write = snapshot_write, | |
437 | .llseek = no_llseek, | |
52d11025 | 438 | .unlocked_ioctl = snapshot_ioctl, |
6e1819d6 RW |
439 | }; |
440 | ||
441 | static struct miscdevice snapshot_device = { | |
442 | .minor = SNAPSHOT_MINOR, | |
443 | .name = "snapshot", | |
444 | .fops = &snapshot_fops, | |
445 | }; | |
446 | ||
447 | static int __init snapshot_device_init(void) | |
448 | { | |
449 | return misc_register(&snapshot_device); | |
450 | }; | |
451 | ||
452 | device_initcall(snapshot_device_init); |