swsusp: fix hibernation code ordering
[deliverable/linux.git] / kernel / power / user.c
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>
14 #include <linux/reboot.h>
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>
23 #include <linux/console.h>
24 #include <linux/cpu.h>
25 #include <linux/freezer.h>
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;
36 int mode;
37 char frozen;
38 char ready;
39 char platform_suspend;
40 } snapshot_state;
41
42 atomic_t snapshot_device_available = ATOMIC_INIT(1);
43
44 static int snapshot_open(struct inode *inode, struct file *filp)
45 {
46 struct snapshot_data *data;
47
48 if (!atomic_add_unless(&snapshot_device_available, -1, 0))
49 return -EBUSY;
50
51 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
52 atomic_inc(&snapshot_device_available);
53 return -ENOSYS;
54 }
55 if(create_basic_memory_bitmaps()) {
56 atomic_inc(&snapshot_device_available);
57 return -ENOMEM;
58 }
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) {
64 data->swap = swsusp_resume_device ?
65 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
66 data->mode = O_RDONLY;
67 } else {
68 data->swap = -1;
69 data->mode = O_WRONLY;
70 }
71 data->frozen = 0;
72 data->ready = 0;
73 data->platform_suspend = 0;
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();
83 free_basic_memory_bitmaps();
84 data = filp->private_data;
85 free_all_swap_pages(data->swap);
86 if (data->frozen) {
87 mutex_lock(&pm_mutex);
88 thaw_processes();
89 mutex_unlock(&pm_mutex);
90 }
91 atomic_inc(&snapshot_device_available);
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;
102 if (!data->ready)
103 return -ENODATA;
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;
136 loff_t avail;
137 sector_t offset;
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;
153 mutex_lock(&pm_mutex);
154 if (freeze_processes()) {
155 thaw_processes();
156 error = -EBUSY;
157 }
158 mutex_unlock(&pm_mutex);
159 if (!error)
160 data->frozen = 1;
161 break;
162
163 case SNAPSHOT_UNFREEZE:
164 if (!data->frozen || data->ready)
165 break;
166 mutex_lock(&pm_mutex);
167 thaw_processes();
168 mutex_unlock(&pm_mutex);
169 data->frozen = 0;
170 break;
171
172 case SNAPSHOT_ATOMIC_SNAPSHOT:
173 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
174 error = -EPERM;
175 break;
176 }
177 error = hibernation_snapshot(data->platform_suspend);
178 if (!error)
179 error = put_user(in_suspend, (unsigned int __user *)arg);
180 if (!error)
181 data->ready = 1;
182 break;
183
184 case SNAPSHOT_ATOMIC_RESTORE:
185 snapshot_write_finalize(&data->handle);
186 if (data->mode != O_WRONLY || !data->frozen ||
187 !snapshot_image_loaded(&data->handle)) {
188 error = -EPERM;
189 break;
190 }
191 error = hibernation_restore(data->platform_suspend);
192 break;
193
194 case SNAPSHOT_FREE:
195 swsusp_free();
196 memset(&data->handle, 0, sizeof(struct snapshot_handle));
197 data->ready = 0;
198 break;
199
200 case SNAPSHOT_SET_IMAGE_SIZE:
201 image_size = arg;
202 break;
203
204 case SNAPSHOT_AVAIL_SWAP:
205 avail = count_swap_pages(data->swap, 1);
206 avail <<= PAGE_SHIFT;
207 error = put_user(avail, (loff_t __user *)arg);
208 break;
209
210 case SNAPSHOT_GET_SWAP_PAGE:
211 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
212 error = -ENODEV;
213 break;
214 }
215 offset = alloc_swapdev_block(data->swap);
216 if (offset) {
217 offset <<= PAGE_SHIFT;
218 error = put_user(offset, (sector_t __user *)arg);
219 } else {
220 error = -ENOSPC;
221 }
222 break;
223
224 case SNAPSHOT_FREE_SWAP_PAGES:
225 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
226 error = -ENODEV;
227 break;
228 }
229 free_all_swap_pages(data->swap);
230 break;
231
232 case SNAPSHOT_SET_SWAP_FILE:
233 if (!swsusp_swap_in_use()) {
234 /*
235 * User space encodes device types as two-byte values,
236 * so we need to recode them
237 */
238 if (old_decode_dev(arg)) {
239 data->swap = swap_type_of(old_decode_dev(arg),
240 0, NULL);
241 if (data->swap < 0)
242 error = -ENODEV;
243 } else {
244 data->swap = -1;
245 error = -EINVAL;
246 }
247 } else {
248 error = -EPERM;
249 }
250 break;
251
252 case SNAPSHOT_S2RAM:
253 if (!pm_ops) {
254 error = -ENOSYS;
255 break;
256 }
257
258 if (!data->frozen) {
259 error = -EPERM;
260 break;
261 }
262
263 if (!mutex_trylock(&pm_mutex)) {
264 error = -EBUSY;
265 break;
266 }
267
268 if (pm_ops->prepare) {
269 error = pm_ops->prepare(PM_SUSPEND_MEM);
270 if (error)
271 goto OutS3;
272 }
273
274 /* Put devices to sleep */
275 suspend_console();
276 error = device_suspend(PMSG_SUSPEND);
277 if (error) {
278 printk(KERN_ERR "Failed to suspend some devices.\n");
279 } else {
280 error = disable_nonboot_cpus();
281 if (!error) {
282 /* Enter S3, system is already frozen */
283 suspend_enter(PM_SUSPEND_MEM);
284 enable_nonboot_cpus();
285 }
286 /* Wake up devices */
287 device_resume();
288 }
289 resume_console();
290 if (pm_ops->finish)
291 pm_ops->finish(PM_SUSPEND_MEM);
292
293 OutS3:
294 mutex_unlock(&pm_mutex);
295 break;
296
297 case SNAPSHOT_PMOPS:
298 error = -EINVAL;
299
300 switch (arg) {
301
302 case PMOPS_PREPARE:
303 data->platform_suspend = 1;
304 error = 0;
305 break;
306
307 case PMOPS_ENTER:
308 if (data->platform_suspend)
309 error = hibernation_platform_enter();
310
311 break;
312
313 case PMOPS_FINISH:
314 if (data->platform_suspend)
315 error = 0;
316
317 break;
318
319 default:
320 printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
321
322 }
323 break;
324
325 case SNAPSHOT_SET_SWAP_AREA:
326 if (swsusp_swap_in_use()) {
327 error = -EPERM;
328 } else {
329 struct resume_swap_area swap_area;
330 dev_t swdev;
331
332 error = copy_from_user(&swap_area, (void __user *)arg,
333 sizeof(struct resume_swap_area));
334 if (error) {
335 error = -EFAULT;
336 break;
337 }
338
339 /*
340 * User space encodes device types as two-byte values,
341 * so we need to recode them
342 */
343 swdev = old_decode_dev(swap_area.dev);
344 if (swdev) {
345 offset = swap_area.offset;
346 data->swap = swap_type_of(swdev, offset, NULL);
347 if (data->swap < 0)
348 error = -ENODEV;
349 } else {
350 data->swap = -1;
351 error = -EINVAL;
352 }
353 }
354 break;
355
356 default:
357 error = -ENOTTY;
358
359 }
360
361 return error;
362 }
363
364 static const struct file_operations snapshot_fops = {
365 .open = snapshot_open,
366 .release = snapshot_release,
367 .read = snapshot_read,
368 .write = snapshot_write,
369 .llseek = no_llseek,
370 .ioctl = snapshot_ioctl,
371 };
372
373 static struct miscdevice snapshot_device = {
374 .minor = SNAPSHOT_MINOR,
375 .name = "snapshot",
376 .fops = &snapshot_fops,
377 };
378
379 static int __init snapshot_device_init(void)
380 {
381 return misc_register(&snapshot_device);
382 };
383
384 device_initcall(snapshot_device_init);
This page took 0.038539 seconds and 5 git commands to generate.