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