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> |
6e1819d6 RW |
26 | |
27 | #include <asm/uaccess.h> | |
28 | ||
29 | #include "power.h" | |
30 | ||
eb57c1cf | 31 | /* |
96f73749 RW |
32 | * NOTE: The SNAPSHOT_SET_SWAP_FILE and SNAPSHOT_PMOPS ioctls are obsolete and |
33 | * will be removed in the future. They are only preserved here for | |
34 | * compatibility with existing userland utilities. | |
eb57c1cf | 35 | */ |
96f73749 | 36 | #define SNAPSHOT_SET_SWAP_FILE _IOW(SNAPSHOT_IOC_MAGIC, 10, unsigned int) |
eb57c1cf RW |
37 | #define SNAPSHOT_PMOPS _IOW(SNAPSHOT_IOC_MAGIC, 12, unsigned int) |
38 | ||
39 | #define PMOPS_PREPARE 1 | |
40 | #define PMOPS_ENTER 2 | |
41 | #define PMOPS_FINISH 3 | |
42 | ||
43 | ||
6e1819d6 RW |
44 | #define SNAPSHOT_MINOR 231 |
45 | ||
46 | static struct snapshot_data { | |
47 | struct snapshot_handle handle; | |
48 | int swap; | |
6e1819d6 RW |
49 | int mode; |
50 | char frozen; | |
51 | char ready; | |
eb57c1cf | 52 | char platform_support; |
6e1819d6 RW |
53 | } snapshot_state; |
54 | ||
0709db60 | 55 | atomic_t snapshot_device_available = ATOMIC_INIT(1); |
6e1819d6 RW |
56 | |
57 | static int snapshot_open(struct inode *inode, struct file *filp) | |
58 | { | |
59 | struct snapshot_data *data; | |
60 | ||
0709db60 | 61 | if (!atomic_add_unless(&snapshot_device_available, -1, 0)) |
6e1819d6 RW |
62 | return -EBUSY; |
63 | ||
1525a2ad | 64 | if ((filp->f_flags & O_ACCMODE) == O_RDWR) { |
0709db60 | 65 | atomic_inc(&snapshot_device_available); |
6e1819d6 | 66 | return -ENOSYS; |
1525a2ad RW |
67 | } |
68 | if(create_basic_memory_bitmaps()) { | |
0709db60 | 69 | atomic_inc(&snapshot_device_available); |
74dfd666 | 70 | return -ENOMEM; |
1525a2ad | 71 | } |
6e1819d6 RW |
72 | nonseekable_open(inode, filp); |
73 | data = &snapshot_state; | |
74 | filp->private_data = data; | |
75 | memset(&data->handle, 0, sizeof(struct snapshot_handle)); | |
76 | if ((filp->f_flags & O_ACCMODE) == O_RDONLY) { | |
915bae9e | 77 | data->swap = swsusp_resume_device ? |
7bf23687 | 78 | swap_type_of(swsusp_resume_device, 0, NULL) : -1; |
6e1819d6 RW |
79 | data->mode = O_RDONLY; |
80 | } else { | |
81 | data->swap = -1; | |
82 | data->mode = O_WRONLY; | |
83 | } | |
6e1819d6 RW |
84 | data->frozen = 0; |
85 | data->ready = 0; | |
eb57c1cf | 86 | data->platform_support = 0; |
6e1819d6 RW |
87 | |
88 | return 0; | |
89 | } | |
90 | ||
91 | static int snapshot_release(struct inode *inode, struct file *filp) | |
92 | { | |
93 | struct snapshot_data *data; | |
94 | ||
95 | swsusp_free(); | |
74dfd666 | 96 | free_basic_memory_bitmaps(); |
6e1819d6 | 97 | data = filp->private_data; |
d1d241cc | 98 | free_all_swap_pages(data->swap); |
6e1819d6 | 99 | if (data->frozen) { |
a6d70980 | 100 | mutex_lock(&pm_mutex); |
6e1819d6 | 101 | thaw_processes(); |
a6d70980 | 102 | mutex_unlock(&pm_mutex); |
6e1819d6 | 103 | } |
0709db60 | 104 | atomic_inc(&snapshot_device_available); |
6e1819d6 RW |
105 | return 0; |
106 | } | |
107 | ||
108 | static ssize_t snapshot_read(struct file *filp, char __user *buf, | |
109 | size_t count, loff_t *offp) | |
110 | { | |
111 | struct snapshot_data *data; | |
112 | ssize_t res; | |
113 | ||
114 | data = filp->private_data; | |
2f41dddb RW |
115 | if (!data->ready) |
116 | return -ENODATA; | |
6e1819d6 RW |
117 | res = snapshot_read_next(&data->handle, count); |
118 | if (res > 0) { | |
119 | if (copy_to_user(buf, data_of(data->handle), res)) | |
120 | res = -EFAULT; | |
121 | else | |
122 | *offp = data->handle.offset; | |
123 | } | |
124 | return res; | |
125 | } | |
126 | ||
127 | static ssize_t snapshot_write(struct file *filp, const char __user *buf, | |
128 | size_t count, loff_t *offp) | |
129 | { | |
130 | struct snapshot_data *data; | |
131 | ssize_t res; | |
132 | ||
133 | data = filp->private_data; | |
134 | res = snapshot_write_next(&data->handle, count); | |
135 | if (res > 0) { | |
136 | if (copy_from_user(data_of(data->handle), buf, res)) | |
137 | res = -EFAULT; | |
138 | else | |
139 | *offp = data->handle.offset; | |
140 | } | |
141 | return res; | |
142 | } | |
143 | ||
144 | static int snapshot_ioctl(struct inode *inode, struct file *filp, | |
145 | unsigned int cmd, unsigned long arg) | |
146 | { | |
147 | int error = 0; | |
148 | struct snapshot_data *data; | |
af508b34 | 149 | loff_t size; |
3aef83e0 | 150 | sector_t offset; |
6e1819d6 RW |
151 | |
152 | if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC) | |
153 | return -ENOTTY; | |
154 | if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR) | |
155 | return -ENOTTY; | |
156 | if (!capable(CAP_SYS_ADMIN)) | |
157 | return -EPERM; | |
158 | ||
159 | data = filp->private_data; | |
160 | ||
161 | switch (cmd) { | |
162 | ||
163 | case SNAPSHOT_FREEZE: | |
164 | if (data->frozen) | |
165 | break; | |
a6d70980 | 166 | mutex_lock(&pm_mutex); |
b10d9117 RW |
167 | error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE); |
168 | if (!error) { | |
232b1432 RW |
169 | printk("Syncing filesystems ... "); |
170 | sys_sync(); | |
171 | printk("done.\n"); | |
172 | ||
b10d9117 RW |
173 | error = freeze_processes(); |
174 | if (error) | |
175 | thaw_processes(); | |
6e1819d6 | 176 | } |
b10d9117 RW |
177 | if (error) |
178 | pm_notifier_call_chain(PM_POST_HIBERNATION); | |
a6d70980 | 179 | mutex_unlock(&pm_mutex); |
6e1819d6 RW |
180 | if (!error) |
181 | data->frozen = 1; | |
182 | break; | |
183 | ||
184 | case SNAPSHOT_UNFREEZE: | |
2f41dddb | 185 | if (!data->frozen || data->ready) |
6e1819d6 | 186 | break; |
a6d70980 | 187 | mutex_lock(&pm_mutex); |
6e1819d6 | 188 | thaw_processes(); |
b10d9117 | 189 | pm_notifier_call_chain(PM_POST_HIBERNATION); |
a6d70980 | 190 | mutex_unlock(&pm_mutex); |
6e1819d6 RW |
191 | data->frozen = 0; |
192 | break; | |
193 | ||
194 | case SNAPSHOT_ATOMIC_SNAPSHOT: | |
195 | if (data->mode != O_RDONLY || !data->frozen || data->ready) { | |
196 | error = -EPERM; | |
197 | break; | |
198 | } | |
eb57c1cf | 199 | error = hibernation_snapshot(data->platform_support); |
6e1819d6 RW |
200 | if (!error) |
201 | error = put_user(in_suspend, (unsigned int __user *)arg); | |
202 | if (!error) | |
203 | data->ready = 1; | |
204 | break; | |
205 | ||
206 | case SNAPSHOT_ATOMIC_RESTORE: | |
8357376d | 207 | snapshot_write_finalize(&data->handle); |
6e1819d6 RW |
208 | if (data->mode != O_WRONLY || !data->frozen || |
209 | !snapshot_image_loaded(&data->handle)) { | |
210 | error = -EPERM; | |
211 | break; | |
212 | } | |
eb57c1cf | 213 | error = hibernation_restore(data->platform_support); |
6e1819d6 RW |
214 | break; |
215 | ||
216 | case SNAPSHOT_FREE: | |
217 | swsusp_free(); | |
218 | memset(&data->handle, 0, sizeof(struct snapshot_handle)); | |
219 | data->ready = 0; | |
220 | break; | |
221 | ||
222 | case SNAPSHOT_SET_IMAGE_SIZE: | |
223 | image_size = arg; | |
224 | break; | |
225 | ||
af508b34 RW |
226 | case SNAPSHOT_GET_IMAGE_SIZE: |
227 | if (!data->ready) { | |
228 | error = -ENODATA; | |
229 | break; | |
230 | } | |
231 | size = snapshot_get_image_size(); | |
232 | size <<= PAGE_SHIFT; | |
233 | error = put_user(size, (loff_t __user *)arg); | |
234 | break; | |
235 | ||
6e1819d6 | 236 | case SNAPSHOT_AVAIL_SWAP: |
af508b34 RW |
237 | size = count_swap_pages(data->swap, 1); |
238 | size <<= PAGE_SHIFT; | |
239 | error = put_user(size, (loff_t __user *)arg); | |
6e1819d6 RW |
240 | break; |
241 | ||
242 | case SNAPSHOT_GET_SWAP_PAGE: | |
243 | if (data->swap < 0 || data->swap >= MAX_SWAPFILES) { | |
244 | error = -ENODEV; | |
245 | break; | |
246 | } | |
d1d241cc | 247 | offset = alloc_swapdev_block(data->swap); |
6e1819d6 RW |
248 | if (offset) { |
249 | offset <<= PAGE_SHIFT; | |
3aef83e0 | 250 | error = put_user(offset, (sector_t __user *)arg); |
6e1819d6 RW |
251 | } else { |
252 | error = -ENOSPC; | |
253 | } | |
254 | break; | |
255 | ||
256 | case SNAPSHOT_FREE_SWAP_PAGES: | |
257 | if (data->swap < 0 || data->swap >= MAX_SWAPFILES) { | |
258 | error = -ENODEV; | |
259 | break; | |
260 | } | |
d1d241cc | 261 | free_all_swap_pages(data->swap); |
6e1819d6 RW |
262 | break; |
263 | ||
96f73749 | 264 | case SNAPSHOT_SET_SWAP_FILE: /* This ioctl is deprecated */ |
d1d241cc | 265 | if (!swsusp_swap_in_use()) { |
6e1819d6 RW |
266 | /* |
267 | * User space encodes device types as two-byte values, | |
268 | * so we need to recode them | |
269 | */ | |
270 | if (old_decode_dev(arg)) { | |
7bf23687 RW |
271 | data->swap = swap_type_of(old_decode_dev(arg), |
272 | 0, NULL); | |
6e1819d6 RW |
273 | if (data->swap < 0) |
274 | error = -ENODEV; | |
275 | } else { | |
276 | data->swap = -1; | |
277 | error = -EINVAL; | |
278 | } | |
279 | } else { | |
280 | error = -EPERM; | |
281 | } | |
282 | break; | |
283 | ||
9b238205 LT |
284 | case SNAPSHOT_S2RAM: |
285 | if (!data->frozen) { | |
286 | error = -EPERM; | |
287 | break; | |
288 | } | |
a6d70980 | 289 | if (!mutex_trylock(&pm_mutex)) { |
9b238205 LT |
290 | error = -EBUSY; |
291 | break; | |
292 | } | |
6c961dfb RW |
293 | /* |
294 | * Tasks are frozen and the notifiers have been called with | |
295 | * PM_HIBERNATION_PREPARE | |
296 | */ | |
297 | error = suspend_devices_and_enter(PM_SUSPEND_MEM); | |
a6d70980 | 298 | mutex_unlock(&pm_mutex); |
9b238205 LT |
299 | break; |
300 | ||
eb57c1cf RW |
301 | case SNAPSHOT_PLATFORM_SUPPORT: |
302 | data->platform_support = !!arg; | |
303 | break; | |
304 | ||
305 | case SNAPSHOT_POWER_OFF: | |
306 | if (data->platform_support) | |
307 | error = hibernation_platform_enter(); | |
308 | break; | |
309 | ||
310 | case SNAPSHOT_PMOPS: /* This ioctl is deprecated */ | |
2b5b09b3 RW |
311 | error = -EINVAL; |
312 | ||
3592695c SS |
313 | switch (arg) { |
314 | ||
315 | case PMOPS_PREPARE: | |
eb57c1cf | 316 | data->platform_support = 1; |
7777fab9 | 317 | error = 0; |
3592695c SS |
318 | break; |
319 | ||
320 | case PMOPS_ENTER: | |
eb57c1cf | 321 | if (data->platform_support) |
7777fab9 | 322 | error = hibernation_platform_enter(); |
3592695c SS |
323 | break; |
324 | ||
325 | case PMOPS_FINISH: | |
eb57c1cf | 326 | if (data->platform_support) |
2b5b09b3 | 327 | error = 0; |
3592695c SS |
328 | break; |
329 | ||
330 | default: | |
331 | printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg); | |
3592695c SS |
332 | |
333 | } | |
334 | break; | |
335 | ||
37b2ba12 | 336 | case SNAPSHOT_SET_SWAP_AREA: |
d1d241cc | 337 | if (swsusp_swap_in_use()) { |
37b2ba12 RW |
338 | error = -EPERM; |
339 | } else { | |
340 | struct resume_swap_area swap_area; | |
341 | dev_t swdev; | |
342 | ||
343 | error = copy_from_user(&swap_area, (void __user *)arg, | |
344 | sizeof(struct resume_swap_area)); | |
345 | if (error) { | |
346 | error = -EFAULT; | |
347 | break; | |
348 | } | |
349 | ||
350 | /* | |
351 | * User space encodes device types as two-byte values, | |
352 | * so we need to recode them | |
353 | */ | |
354 | swdev = old_decode_dev(swap_area.dev); | |
355 | if (swdev) { | |
356 | offset = swap_area.offset; | |
7bf23687 | 357 | data->swap = swap_type_of(swdev, offset, NULL); |
37b2ba12 RW |
358 | if (data->swap < 0) |
359 | error = -ENODEV; | |
360 | } else { | |
361 | data->swap = -1; | |
362 | error = -EINVAL; | |
363 | } | |
364 | } | |
365 | break; | |
366 | ||
6e1819d6 RW |
367 | default: |
368 | error = -ENOTTY; | |
369 | ||
370 | } | |
371 | ||
372 | return error; | |
373 | } | |
374 | ||
15ad7cdc | 375 | static const struct file_operations snapshot_fops = { |
6e1819d6 RW |
376 | .open = snapshot_open, |
377 | .release = snapshot_release, | |
378 | .read = snapshot_read, | |
379 | .write = snapshot_write, | |
380 | .llseek = no_llseek, | |
381 | .ioctl = snapshot_ioctl, | |
382 | }; | |
383 | ||
384 | static struct miscdevice snapshot_device = { | |
385 | .minor = SNAPSHOT_MINOR, | |
386 | .name = "snapshot", | |
387 | .fops = &snapshot_fops, | |
388 | }; | |
389 | ||
390 | static int __init snapshot_device_init(void) | |
391 | { | |
392 | return misc_register(&snapshot_device); | |
393 | }; | |
394 | ||
395 | device_initcall(snapshot_device_init); |