[PATCH] swsusp: Change code ordering in user.c
[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 struct bitmap_page *bitmap;
37 int mode;
38 char frozen;
39 char ready;
40 } snapshot_state;
41
42 static atomic_t 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(&device_available, -1, 0))
49 return -EBUSY;
50
51 if ((filp->f_flags & O_ACCMODE) == O_RDWR)
52 return -ENOSYS;
53
54 nonseekable_open(inode, filp);
55 data = &snapshot_state;
56 filp->private_data = data;
57 memset(&data->handle, 0, sizeof(struct snapshot_handle));
58 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
59 data->swap = swsusp_resume_device ?
60 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
61 data->mode = O_RDONLY;
62 } else {
63 data->swap = -1;
64 data->mode = O_WRONLY;
65 }
66 data->bitmap = NULL;
67 data->frozen = 0;
68 data->ready = 0;
69
70 return 0;
71 }
72
73 static int snapshot_release(struct inode *inode, struct file *filp)
74 {
75 struct snapshot_data *data;
76
77 swsusp_free();
78 data = filp->private_data;
79 free_all_swap_pages(data->swap, data->bitmap);
80 free_bitmap(data->bitmap);
81 if (data->frozen) {
82 mutex_lock(&pm_mutex);
83 thaw_processes();
84 enable_nonboot_cpus();
85 mutex_unlock(&pm_mutex);
86 }
87 atomic_inc(&device_available);
88 return 0;
89 }
90
91 static ssize_t snapshot_read(struct file *filp, char __user *buf,
92 size_t count, loff_t *offp)
93 {
94 struct snapshot_data *data;
95 ssize_t res;
96
97 data = filp->private_data;
98 res = snapshot_read_next(&data->handle, count);
99 if (res > 0) {
100 if (copy_to_user(buf, data_of(data->handle), res))
101 res = -EFAULT;
102 else
103 *offp = data->handle.offset;
104 }
105 return res;
106 }
107
108 static ssize_t snapshot_write(struct file *filp, const 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;
115 res = snapshot_write_next(&data->handle, count);
116 if (res > 0) {
117 if (copy_from_user(data_of(data->handle), buf, res))
118 res = -EFAULT;
119 else
120 *offp = data->handle.offset;
121 }
122 return res;
123 }
124
125 static inline int snapshot_suspend(void)
126 {
127 int error;
128
129 mutex_lock(&pm_mutex);
130 /* Free memory before shutting down devices. */
131 error = swsusp_shrink_memory();
132 if (error)
133 goto Finish;
134
135 suspend_console();
136 error = device_suspend(PMSG_FREEZE);
137 if (error)
138 goto Resume_devices;
139
140 error = disable_nonboot_cpus();
141 if (!error) {
142 in_suspend = 1;
143 error = swsusp_suspend();
144 }
145 enable_nonboot_cpus();
146 Resume_devices:
147 device_resume();
148 resume_console();
149 Finish:
150 mutex_unlock(&pm_mutex);
151 return error;
152 }
153
154 static inline int snapshot_restore(void)
155 {
156 int error;
157
158 mutex_lock(&pm_mutex);
159 pm_prepare_console();
160 suspend_console();
161 error = device_suspend(PMSG_PRETHAW);
162 if (error)
163 goto Resume_devices;
164
165 error = disable_nonboot_cpus();
166 if (!error)
167 error = swsusp_resume();
168
169 enable_nonboot_cpus();
170 Resume_devices:
171 device_resume();
172 resume_console();
173 pm_restore_console();
174 mutex_unlock(&pm_mutex);
175 return error;
176 }
177
178 static int snapshot_ioctl(struct inode *inode, struct file *filp,
179 unsigned int cmd, unsigned long arg)
180 {
181 int error = 0;
182 struct snapshot_data *data;
183 loff_t avail;
184 sector_t offset;
185
186 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
187 return -ENOTTY;
188 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
189 return -ENOTTY;
190 if (!capable(CAP_SYS_ADMIN))
191 return -EPERM;
192
193 data = filp->private_data;
194
195 switch (cmd) {
196
197 case SNAPSHOT_FREEZE:
198 if (data->frozen)
199 break;
200 mutex_lock(&pm_mutex);
201 if (freeze_processes()) {
202 thaw_processes();
203 error = -EBUSY;
204 }
205 mutex_unlock(&pm_mutex);
206 if (!error)
207 data->frozen = 1;
208 break;
209
210 case SNAPSHOT_UNFREEZE:
211 if (!data->frozen)
212 break;
213 mutex_lock(&pm_mutex);
214 thaw_processes();
215 mutex_unlock(&pm_mutex);
216 data->frozen = 0;
217 break;
218
219 case SNAPSHOT_ATOMIC_SNAPSHOT:
220 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
221 error = -EPERM;
222 break;
223 }
224 error = snapshot_suspend();
225 if (!error)
226 error = put_user(in_suspend, (unsigned int __user *)arg);
227 if (!error)
228 data->ready = 1;
229 break;
230
231 case SNAPSHOT_ATOMIC_RESTORE:
232 snapshot_write_finalize(&data->handle);
233 if (data->mode != O_WRONLY || !data->frozen ||
234 !snapshot_image_loaded(&data->handle)) {
235 error = -EPERM;
236 break;
237 }
238 error = snapshot_restore();
239 break;
240
241 case SNAPSHOT_FREE:
242 swsusp_free();
243 memset(&data->handle, 0, sizeof(struct snapshot_handle));
244 data->ready = 0;
245 break;
246
247 case SNAPSHOT_SET_IMAGE_SIZE:
248 image_size = arg;
249 break;
250
251 case SNAPSHOT_AVAIL_SWAP:
252 avail = count_swap_pages(data->swap, 1);
253 avail <<= PAGE_SHIFT;
254 error = put_user(avail, (loff_t __user *)arg);
255 break;
256
257 case SNAPSHOT_GET_SWAP_PAGE:
258 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
259 error = -ENODEV;
260 break;
261 }
262 if (!data->bitmap) {
263 data->bitmap = alloc_bitmap(count_swap_pages(data->swap, 0));
264 if (!data->bitmap) {
265 error = -ENOMEM;
266 break;
267 }
268 }
269 offset = alloc_swapdev_block(data->swap, data->bitmap);
270 if (offset) {
271 offset <<= PAGE_SHIFT;
272 error = put_user(offset, (sector_t __user *)arg);
273 } else {
274 error = -ENOSPC;
275 }
276 break;
277
278 case SNAPSHOT_FREE_SWAP_PAGES:
279 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
280 error = -ENODEV;
281 break;
282 }
283 free_all_swap_pages(data->swap, data->bitmap);
284 free_bitmap(data->bitmap);
285 data->bitmap = NULL;
286 break;
287
288 case SNAPSHOT_SET_SWAP_FILE:
289 if (!data->bitmap) {
290 /*
291 * User space encodes device types as two-byte values,
292 * so we need to recode them
293 */
294 if (old_decode_dev(arg)) {
295 data->swap = swap_type_of(old_decode_dev(arg),
296 0, NULL);
297 if (data->swap < 0)
298 error = -ENODEV;
299 } else {
300 data->swap = -1;
301 error = -EINVAL;
302 }
303 } else {
304 error = -EPERM;
305 }
306 break;
307
308 case SNAPSHOT_S2RAM:
309 if (!data->frozen) {
310 error = -EPERM;
311 break;
312 }
313
314 if (!mutex_trylock(&pm_mutex)) {
315 error = -EBUSY;
316 break;
317 }
318
319 if (pm_ops->prepare) {
320 error = pm_ops->prepare(PM_SUSPEND_MEM);
321 if (error)
322 goto OutS3;
323 }
324
325 /* Put devices to sleep */
326 suspend_console();
327 error = device_suspend(PMSG_SUSPEND);
328 if (error) {
329 printk(KERN_ERR "Failed to suspend some devices.\n");
330 } else {
331 /* Enter S3, system is already frozen */
332 suspend_enter(PM_SUSPEND_MEM);
333
334 /* Wake up devices */
335 device_resume();
336 }
337 resume_console();
338 if (pm_ops->finish)
339 pm_ops->finish(PM_SUSPEND_MEM);
340
341 OutS3:
342 mutex_unlock(&pm_mutex);
343 break;
344
345 case SNAPSHOT_PMOPS:
346 switch (arg) {
347
348 case PMOPS_PREPARE:
349 if (pm_ops->prepare) {
350 error = pm_ops->prepare(PM_SUSPEND_DISK);
351 }
352 break;
353
354 case PMOPS_ENTER:
355 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
356 error = pm_ops->enter(PM_SUSPEND_DISK);
357 break;
358
359 case PMOPS_FINISH:
360 if (pm_ops && pm_ops->finish) {
361 pm_ops->finish(PM_SUSPEND_DISK);
362 }
363 break;
364
365 default:
366 printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
367 error = -EINVAL;
368
369 }
370 break;
371
372 case SNAPSHOT_SET_SWAP_AREA:
373 if (data->bitmap) {
374 error = -EPERM;
375 } else {
376 struct resume_swap_area swap_area;
377 dev_t swdev;
378
379 error = copy_from_user(&swap_area, (void __user *)arg,
380 sizeof(struct resume_swap_area));
381 if (error) {
382 error = -EFAULT;
383 break;
384 }
385
386 /*
387 * User space encodes device types as two-byte values,
388 * so we need to recode them
389 */
390 swdev = old_decode_dev(swap_area.dev);
391 if (swdev) {
392 offset = swap_area.offset;
393 data->swap = swap_type_of(swdev, offset, NULL);
394 if (data->swap < 0)
395 error = -ENODEV;
396 } else {
397 data->swap = -1;
398 error = -EINVAL;
399 }
400 }
401 break;
402
403 default:
404 error = -ENOTTY;
405
406 }
407
408 return error;
409 }
410
411 static const struct file_operations snapshot_fops = {
412 .open = snapshot_open,
413 .release = snapshot_release,
414 .read = snapshot_read,
415 .write = snapshot_write,
416 .llseek = no_llseek,
417 .ioctl = snapshot_ioctl,
418 };
419
420 static struct miscdevice snapshot_device = {
421 .minor = SNAPSHOT_MINOR,
422 .name = "snapshot",
423 .fops = &snapshot_fops,
424 };
425
426 static int __init snapshot_device_init(void)
427 {
428 return misc_register(&snapshot_device);
429 };
430
431 device_initcall(snapshot_device_init);
This page took 0.040618 seconds and 5 git commands to generate.