blob: f5512cb3aa86f61e98caec04c82a01a0dc9f6dcf [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
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +020031/*
Rafael J. Wysocki96f73742007-10-26 01:02:15 +020032 * 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.
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +020035 */
Rafael J. Wysocki96f73742007-10-26 01:02:15 +020036#define SNAPSHOT_SET_SWAP_FILE _IOW(SNAPSHOT_IOC_MAGIC, 10, unsigned int)
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +020037#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
Rafael J. Wysockicc5d2072007-10-26 01:03:33 +020043/*
44 * NOTE: The following ioctl definitions are wrong and have been replaced with
45 * correct ones. They are only preserved here for compatibility with existing
46 * userland utilities and will be removed in the future.
47 */
48#define SNAPSHOT_ATOMIC_SNAPSHOT _IOW(SNAPSHOT_IOC_MAGIC, 3, void *)
49#define SNAPSHOT_SET_IMAGE_SIZE _IOW(SNAPSHOT_IOC_MAGIC, 6, unsigned long)
50#define SNAPSHOT_AVAIL_SWAP _IOR(SNAPSHOT_IOC_MAGIC, 7, void *)
51#define SNAPSHOT_GET_SWAP_PAGE _IOR(SNAPSHOT_IOC_MAGIC, 8, void *)
52
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +020053
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080054#define SNAPSHOT_MINOR 231
55
56static struct snapshot_data {
57 struct snapshot_handle handle;
58 int swap;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080059 int mode;
60 char frozen;
61 char ready;
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +020062 char platform_support;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080063} snapshot_state;
64
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070065atomic_t snapshot_device_available = ATOMIC_INIT(1);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080066
67static int snapshot_open(struct inode *inode, struct file *filp)
68{
69 struct snapshot_data *data;
Alan Sternc3e94d82007-11-19 23:38:25 +010070 int error;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080071
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070072 if (!atomic_add_unless(&snapshot_device_available, -1, 0))
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080073 return -EBUSY;
74
Rafael J. Wysocki1525a2a2007-05-06 14:50:44 -070075 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070076 atomic_inc(&snapshot_device_available);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080077 return -ENOSYS;
Rafael J. Wysocki1525a2a2007-05-06 14:50:44 -070078 }
79 if(create_basic_memory_bitmaps()) {
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070080 atomic_inc(&snapshot_device_available);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -070081 return -ENOMEM;
Rafael J. Wysocki1525a2a2007-05-06 14:50:44 -070082 }
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080083 nonseekable_open(inode, filp);
84 data = &snapshot_state;
85 filp->private_data = data;
86 memset(&data->handle, 0, sizeof(struct snapshot_handle));
87 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
Rafael J. Wysocki915bae92006-12-06 20:34:07 -080088 data->swap = swsusp_resume_device ?
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -080089 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080090 data->mode = O_RDONLY;
Alan Sternc3e94d82007-11-19 23:38:25 +010091 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
92 if (error)
93 pm_notifier_call_chain(PM_POST_RESTORE);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080094 } else {
95 data->swap = -1;
96 data->mode = O_WRONLY;
Alan Sternc3e94d82007-11-19 23:38:25 +010097 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
98 if (error)
99 pm_notifier_call_chain(PM_POST_HIBERNATION);
100 }
101 if (error) {
102 atomic_inc(&snapshot_device_available);
103 return error;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800104 }
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800105 data->frozen = 0;
106 data->ready = 0;
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200107 data->platform_support = 0;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800108
109 return 0;
110}
111
112static int snapshot_release(struct inode *inode, struct file *filp)
113{
114 struct snapshot_data *data;
115
116 swsusp_free();
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700117 free_basic_memory_bitmaps();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800118 data = filp->private_data;
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700119 free_all_swap_pages(data->swap);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800120 if (data->frozen) {
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800121 mutex_lock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800122 thaw_processes();
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800123 mutex_unlock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800124 }
Alan Sternc3e94d82007-11-19 23:38:25 +0100125 pm_notifier_call_chain(data->mode == O_WRONLY ?
126 PM_POST_HIBERNATION : PM_POST_RESTORE);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700127 atomic_inc(&snapshot_device_available);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800128 return 0;
129}
130
131static ssize_t snapshot_read(struct file *filp, char __user *buf,
132 size_t count, loff_t *offp)
133{
134 struct snapshot_data *data;
135 ssize_t res;
136
137 data = filp->private_data;
Rafael J. Wysocki2f41ddd2007-06-16 10:16:03 -0700138 if (!data->ready)
139 return -ENODATA;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800140 res = snapshot_read_next(&data->handle, count);
141 if (res > 0) {
142 if (copy_to_user(buf, data_of(data->handle), res))
143 res = -EFAULT;
144 else
145 *offp = data->handle.offset;
146 }
147 return res;
148}
149
150static ssize_t snapshot_write(struct file *filp, const char __user *buf,
151 size_t count, loff_t *offp)
152{
153 struct snapshot_data *data;
154 ssize_t res;
155
156 data = filp->private_data;
157 res = snapshot_write_next(&data->handle, count);
158 if (res > 0) {
159 if (copy_from_user(data_of(data->handle), buf, res))
160 res = -EFAULT;
161 else
162 *offp = data->handle.offset;
163 }
164 return res;
165}
166
167static int snapshot_ioctl(struct inode *inode, struct file *filp,
168 unsigned int cmd, unsigned long arg)
169{
170 int error = 0;
171 struct snapshot_data *data;
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +0200172 loff_t size;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800173 sector_t offset;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800174
175 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
176 return -ENOTTY;
177 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
178 return -ENOTTY;
179 if (!capable(CAP_SYS_ADMIN))
180 return -EPERM;
181
182 data = filp->private_data;
183
184 switch (cmd) {
185
186 case SNAPSHOT_FREEZE:
187 if (data->frozen)
188 break;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800189 mutex_lock(&pm_mutex);
Alan Sternc3e94d82007-11-19 23:38:25 +0100190 printk("Syncing filesystems ... ");
191 sys_sync();
192 printk("done.\n");
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700193
Alan Sternc3e94d82007-11-19 23:38:25 +0100194 error = freeze_processes();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700195 if (error)
Alan Sternc3e94d82007-11-19 23:38:25 +0100196 thaw_processes();
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800197 mutex_unlock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800198 if (!error)
199 data->frozen = 1;
200 break;
201
202 case SNAPSHOT_UNFREEZE:
Rafael J. Wysocki2f41ddd2007-06-16 10:16:03 -0700203 if (!data->frozen || data->ready)
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800204 break;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800205 mutex_lock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800206 thaw_processes();
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800207 mutex_unlock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800208 data->frozen = 0;
209 break;
210
Rafael J. Wysockicc5d2072007-10-26 01:03:33 +0200211 case SNAPSHOT_CREATE_IMAGE:
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800212 case SNAPSHOT_ATOMIC_SNAPSHOT:
213 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
214 error = -EPERM;
215 break;
216 }
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200217 error = hibernation_snapshot(data->platform_support);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800218 if (!error)
Rafael J. Wysockicc5d2072007-10-26 01:03:33 +0200219 error = put_user(in_suspend, (int __user *)arg);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800220 if (!error)
221 data->ready = 1;
222 break;
223
224 case SNAPSHOT_ATOMIC_RESTORE:
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800225 snapshot_write_finalize(&data->handle);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800226 if (data->mode != O_WRONLY || !data->frozen ||
227 !snapshot_image_loaded(&data->handle)) {
228 error = -EPERM;
229 break;
230 }
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200231 error = hibernation_restore(data->platform_support);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800232 break;
233
234 case SNAPSHOT_FREE:
235 swsusp_free();
236 memset(&data->handle, 0, sizeof(struct snapshot_handle));
237 data->ready = 0;
238 break;
239
Rafael J. Wysockicc5d2072007-10-26 01:03:33 +0200240 case SNAPSHOT_PREF_IMAGE_SIZE:
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800241 case SNAPSHOT_SET_IMAGE_SIZE:
242 image_size = arg;
243 break;
244
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +0200245 case SNAPSHOT_GET_IMAGE_SIZE:
246 if (!data->ready) {
247 error = -ENODATA;
248 break;
249 }
250 size = snapshot_get_image_size();
251 size <<= PAGE_SHIFT;
252 error = put_user(size, (loff_t __user *)arg);
253 break;
254
Rafael J. Wysockicc5d2072007-10-26 01:03:33 +0200255 case SNAPSHOT_AVAIL_SWAP_SIZE:
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800256 case SNAPSHOT_AVAIL_SWAP:
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +0200257 size = count_swap_pages(data->swap, 1);
258 size <<= PAGE_SHIFT;
259 error = put_user(size, (loff_t __user *)arg);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800260 break;
261
Rafael J. Wysockicc5d2072007-10-26 01:03:33 +0200262 case SNAPSHOT_ALLOC_SWAP_PAGE:
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800263 case SNAPSHOT_GET_SWAP_PAGE:
264 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
265 error = -ENODEV;
266 break;
267 }
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700268 offset = alloc_swapdev_block(data->swap);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800269 if (offset) {
270 offset <<= PAGE_SHIFT;
Rafael J. Wysockicc5d2072007-10-26 01:03:33 +0200271 error = put_user(offset, (loff_t __user *)arg);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800272 } else {
273 error = -ENOSPC;
274 }
275 break;
276
277 case SNAPSHOT_FREE_SWAP_PAGES:
278 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
279 error = -ENODEV;
280 break;
281 }
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700282 free_all_swap_pages(data->swap);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800283 break;
284
Rafael J. Wysocki96f73742007-10-26 01:02:15 +0200285 case SNAPSHOT_SET_SWAP_FILE: /* This ioctl is deprecated */
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700286 if (!swsusp_swap_in_use()) {
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800287 /*
288 * User space encodes device types as two-byte values,
289 * so we need to recode them
290 */
291 if (old_decode_dev(arg)) {
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800292 data->swap = swap_type_of(old_decode_dev(arg),
293 0, NULL);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800294 if (data->swap < 0)
295 error = -ENODEV;
296 } else {
297 data->swap = -1;
298 error = -EINVAL;
299 }
300 } else {
301 error = -EPERM;
302 }
303 break;
304
Luca Tettamanti9b238202006-03-23 03:00:09 -0800305 case SNAPSHOT_S2RAM:
306 if (!data->frozen) {
307 error = -EPERM;
308 break;
309 }
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800310 if (!mutex_trylock(&pm_mutex)) {
Luca Tettamanti9b238202006-03-23 03:00:09 -0800311 error = -EBUSY;
312 break;
313 }
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700314 /*
315 * Tasks are frozen and the notifiers have been called with
316 * PM_HIBERNATION_PREPARE
317 */
318 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800319 mutex_unlock(&pm_mutex);
Luca Tettamanti9b238202006-03-23 03:00:09 -0800320 break;
321
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200322 case SNAPSHOT_PLATFORM_SUPPORT:
323 data->platform_support = !!arg;
324 break;
325
326 case SNAPSHOT_POWER_OFF:
327 if (data->platform_support)
328 error = hibernation_platform_enter();
329 break;
330
331 case SNAPSHOT_PMOPS: /* This ioctl is deprecated */
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800332 error = -EINVAL;
333
Stefan Seyfried35926952006-12-06 20:34:06 -0800334 switch (arg) {
335
336 case PMOPS_PREPARE:
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200337 data->platform_support = 1;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700338 error = 0;
Stefan Seyfried35926952006-12-06 20:34:06 -0800339 break;
340
341 case PMOPS_ENTER:
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200342 if (data->platform_support)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700343 error = hibernation_platform_enter();
Stefan Seyfried35926952006-12-06 20:34:06 -0800344 break;
345
346 case PMOPS_FINISH:
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200347 if (data->platform_support)
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800348 error = 0;
Stefan Seyfried35926952006-12-06 20:34:06 -0800349 break;
350
351 default:
352 printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
Stefan Seyfried35926952006-12-06 20:34:06 -0800353
354 }
355 break;
356
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800357 case SNAPSHOT_SET_SWAP_AREA:
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700358 if (swsusp_swap_in_use()) {
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800359 error = -EPERM;
360 } else {
361 struct resume_swap_area swap_area;
362 dev_t swdev;
363
364 error = copy_from_user(&swap_area, (void __user *)arg,
365 sizeof(struct resume_swap_area));
366 if (error) {
367 error = -EFAULT;
368 break;
369 }
370
371 /*
372 * User space encodes device types as two-byte values,
373 * so we need to recode them
374 */
375 swdev = old_decode_dev(swap_area.dev);
376 if (swdev) {
377 offset = swap_area.offset;
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800378 data->swap = swap_type_of(swdev, offset, NULL);
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800379 if (data->swap < 0)
380 error = -ENODEV;
381 } else {
382 data->swap = -1;
383 error = -EINVAL;
384 }
385 }
386 break;
387
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800388 default:
389 error = -ENOTTY;
390
391 }
392
393 return error;
394}
395
Helge Deller15ad7cd2006-12-06 20:40:36 -0800396static const struct file_operations snapshot_fops = {
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800397 .open = snapshot_open,
398 .release = snapshot_release,
399 .read = snapshot_read,
400 .write = snapshot_write,
401 .llseek = no_llseek,
402 .ioctl = snapshot_ioctl,
403};
404
405static struct miscdevice snapshot_device = {
406 .minor = SNAPSHOT_MINOR,
407 .name = "snapshot",
408 .fops = &snapshot_fops,
409};
410
411static int __init snapshot_device_init(void)
412{
413 return misc_register(&snapshot_device);
414};
415
416device_initcall(snapshot_device_init);