blob: 4f217683455ff6a81b42d89d779085a4d30e13dc [file] [log] [blame]
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -08001/*
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>
Stefan Seyfried35926952006-12-06 20:34:06 -080014#include <linux/reboot.h>
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080015#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>
Rafael J. Wysocki97c78012006-10-11 01:20:45 -070023#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070024#include <linux/cpu.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080025#include <linux/freezer.h>
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080026
27#include <asm/uaccess.h>
28
29#include "power.h"
30
31#define SNAPSHOT_MINOR 231
32
33static 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
42static atomic_t device_available = ATOMIC_INIT(1);
43
44static 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) {
Rafael J. Wysocki915bae92006-12-06 20:34:07 -080059 data->swap = swsusp_resume_device ?
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -080060 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080061 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
73static 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) {
Stephen Hemmingera6d70982006-12-06 20:34:35 -080082 mutex_lock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080083 thaw_processes();
84 enable_nonboot_cpus();
Stephen Hemmingera6d70982006-12-06 20:34:35 -080085 mutex_unlock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080086 }
87 atomic_inc(&device_available);
88 return 0;
89}
90
91static 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
108static 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
Rafael J. Wysocki25913052007-02-10 01:43:33 -0800125static 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
154static 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
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800178static 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;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800183 loff_t avail;
184 sector_t offset;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800185
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;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800200 mutex_lock(&pm_mutex);
Rafael J. Wysocki25913052007-02-10 01:43:33 -0800201 if (freeze_processes()) {
202 thaw_processes();
203 error = -EBUSY;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800204 }
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800205 mutex_unlock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800206 if (!error)
207 data->frozen = 1;
208 break;
209
210 case SNAPSHOT_UNFREEZE:
211 if (!data->frozen)
212 break;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800213 mutex_lock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800214 thaw_processes();
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800215 mutex_unlock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800216 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 }
Rafael J. Wysocki25913052007-02-10 01:43:33 -0800224 error = snapshot_suspend();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800225 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:
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800232 snapshot_write_finalize(&data->handle);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800233 if (data->mode != O_WRONLY || !data->frozen ||
234 !snapshot_image_loaded(&data->handle)) {
235 error = -EPERM;
236 break;
237 }
Rafael J. Wysocki25913052007-02-10 01:43:33 -0800238 error = snapshot_restore();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800239 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 }
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800269 offset = alloc_swapdev_block(data->swap, data->bitmap);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800270 if (offset) {
271 offset <<= PAGE_SHIFT;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800272 error = put_user(offset, (sector_t __user *)arg);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800273 } 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)) {
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800295 data->swap = swap_type_of(old_decode_dev(arg),
296 0, NULL);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800297 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
Luca Tettamanti9b238202006-03-23 03:00:09 -0800308 case SNAPSHOT_S2RAM:
309 if (!data->frozen) {
310 error = -EPERM;
311 break;
312 }
313
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800314 if (!mutex_trylock(&pm_mutex)) {
Luca Tettamanti9b238202006-03-23 03:00:09 -0800315 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 */
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700326 suspend_console();
Luca Tettamanti9b238202006-03-23 03:00:09 -0800327 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 }
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700337 resume_console();
Luca Tettamanti9b238202006-03-23 03:00:09 -0800338 if (pm_ops->finish)
339 pm_ops->finish(PM_SUSPEND_MEM);
340
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800341 OutS3:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800342 mutex_unlock(&pm_mutex);
Luca Tettamanti9b238202006-03-23 03:00:09 -0800343 break;
344
Stefan Seyfried35926952006-12-06 20:34:06 -0800345 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
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800372 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;
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800393 data->swap = swap_type_of(swdev, offset, NULL);
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800394 if (data->swap < 0)
395 error = -ENODEV;
396 } else {
397 data->swap = -1;
398 error = -EINVAL;
399 }
400 }
401 break;
402
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800403 default:
404 error = -ENOTTY;
405
406 }
407
408 return error;
409}
410
Helge Deller15ad7cd2006-12-06 20:40:36 -0800411static const struct file_operations snapshot_fops = {
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800412 .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
420static struct miscdevice snapshot_device = {
421 .minor = SNAPSHOT_MINOR,
422 .name = "snapshot",
423 .fops = &snapshot_fops,
424};
425
426static int __init snapshot_device_init(void)
427{
428 return misc_register(&snapshot_device);
429};
430
431device_initcall(snapshot_device_init);