blob: b902a7e3bd12084e8f3a479826d22ce0bc00b411 [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;
70
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070071 if (!atomic_add_unless(&snapshot_device_available, -1, 0))
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080072 return -EBUSY;
73
Rafael J. Wysocki1525a2a2007-05-06 14:50:44 -070074 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070075 atomic_inc(&snapshot_device_available);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080076 return -ENOSYS;
Rafael J. Wysocki1525a2a2007-05-06 14:50:44 -070077 }
78 if(create_basic_memory_bitmaps()) {
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070079 atomic_inc(&snapshot_device_available);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -070080 return -ENOMEM;
Rafael J. Wysocki1525a2a2007-05-06 14:50:44 -070081 }
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080082 nonseekable_open(inode, filp);
83 data = &snapshot_state;
84 filp->private_data = data;
85 memset(&data->handle, 0, sizeof(struct snapshot_handle));
86 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
Rafael J. Wysocki915bae92006-12-06 20:34:07 -080087 data->swap = swsusp_resume_device ?
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -080088 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080089 data->mode = O_RDONLY;
90 } else {
91 data->swap = -1;
92 data->mode = O_WRONLY;
93 }
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080094 data->frozen = 0;
95 data->ready = 0;
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +020096 data->platform_support = 0;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080097
98 return 0;
99}
100
101static int snapshot_release(struct inode *inode, struct file *filp)
102{
103 struct snapshot_data *data;
104
105 swsusp_free();
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700106 free_basic_memory_bitmaps();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800107 data = filp->private_data;
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700108 free_all_swap_pages(data->swap);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800109 if (data->frozen) {
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800110 mutex_lock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800111 thaw_processes();
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800112 mutex_unlock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800113 }
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700114 atomic_inc(&snapshot_device_available);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800115 return 0;
116}
117
118static ssize_t snapshot_read(struct file *filp, char __user *buf,
119 size_t count, loff_t *offp)
120{
121 struct snapshot_data *data;
122 ssize_t res;
123
124 data = filp->private_data;
Rafael J. Wysocki2f41ddd2007-06-16 10:16:03 -0700125 if (!data->ready)
126 return -ENODATA;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800127 res = snapshot_read_next(&data->handle, count);
128 if (res > 0) {
129 if (copy_to_user(buf, data_of(data->handle), res))
130 res = -EFAULT;
131 else
132 *offp = data->handle.offset;
133 }
134 return res;
135}
136
137static ssize_t snapshot_write(struct file *filp, const char __user *buf,
138 size_t count, loff_t *offp)
139{
140 struct snapshot_data *data;
141 ssize_t res;
142
143 data = filp->private_data;
144 res = snapshot_write_next(&data->handle, count);
145 if (res > 0) {
146 if (copy_from_user(data_of(data->handle), buf, res))
147 res = -EFAULT;
148 else
149 *offp = data->handle.offset;
150 }
151 return res;
152}
153
154static int snapshot_ioctl(struct inode *inode, struct file *filp,
155 unsigned int cmd, unsigned long arg)
156{
157 int error = 0;
158 struct snapshot_data *data;
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +0200159 loff_t size;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800160 sector_t offset;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800161
162 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
163 return -ENOTTY;
164 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
165 return -ENOTTY;
166 if (!capable(CAP_SYS_ADMIN))
167 return -EPERM;
168
169 data = filp->private_data;
170
171 switch (cmd) {
172
173 case SNAPSHOT_FREEZE:
174 if (data->frozen)
175 break;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800176 mutex_lock(&pm_mutex);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700177 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
178 if (!error) {
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700179 printk("Syncing filesystems ... ");
180 sys_sync();
181 printk("done.\n");
182
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700183 error = freeze_processes();
184 if (error)
185 thaw_processes();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800186 }
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700187 if (error)
188 pm_notifier_call_chain(PM_POST_HIBERNATION);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800189 mutex_unlock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800190 if (!error)
191 data->frozen = 1;
192 break;
193
194 case SNAPSHOT_UNFREEZE:
Rafael J. Wysocki2f41ddd2007-06-16 10:16:03 -0700195 if (!data->frozen || data->ready)
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800196 break;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800197 mutex_lock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800198 thaw_processes();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700199 pm_notifier_call_chain(PM_POST_HIBERNATION);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800200 mutex_unlock(&pm_mutex);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800201 data->frozen = 0;
202 break;
203
Rafael J. Wysockicc5d2072007-10-26 01:03:33 +0200204 case SNAPSHOT_CREATE_IMAGE:
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800205 case SNAPSHOT_ATOMIC_SNAPSHOT:
206 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
207 error = -EPERM;
208 break;
209 }
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200210 error = hibernation_snapshot(data->platform_support);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800211 if (!error)
Rafael J. Wysockicc5d2072007-10-26 01:03:33 +0200212 error = put_user(in_suspend, (int __user *)arg);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800213 if (!error)
214 data->ready = 1;
215 break;
216
217 case SNAPSHOT_ATOMIC_RESTORE:
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800218 snapshot_write_finalize(&data->handle);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800219 if (data->mode != O_WRONLY || !data->frozen ||
220 !snapshot_image_loaded(&data->handle)) {
221 error = -EPERM;
222 break;
223 }
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200224 error = hibernation_restore(data->platform_support);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800225 break;
226
227 case SNAPSHOT_FREE:
228 swsusp_free();
229 memset(&data->handle, 0, sizeof(struct snapshot_handle));
230 data->ready = 0;
231 break;
232
Rafael J. Wysockicc5d2072007-10-26 01:03:33 +0200233 case SNAPSHOT_PREF_IMAGE_SIZE:
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800234 case SNAPSHOT_SET_IMAGE_SIZE:
235 image_size = arg;
236 break;
237
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +0200238 case SNAPSHOT_GET_IMAGE_SIZE:
239 if (!data->ready) {
240 error = -ENODATA;
241 break;
242 }
243 size = snapshot_get_image_size();
244 size <<= PAGE_SHIFT;
245 error = put_user(size, (loff_t __user *)arg);
246 break;
247
Rafael J. Wysockicc5d2072007-10-26 01:03:33 +0200248 case SNAPSHOT_AVAIL_SWAP_SIZE:
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800249 case SNAPSHOT_AVAIL_SWAP:
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +0200250 size = count_swap_pages(data->swap, 1);
251 size <<= PAGE_SHIFT;
252 error = put_user(size, (loff_t __user *)arg);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800253 break;
254
Rafael J. Wysockicc5d2072007-10-26 01:03:33 +0200255 case SNAPSHOT_ALLOC_SWAP_PAGE:
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800256 case SNAPSHOT_GET_SWAP_PAGE:
257 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
258 error = -ENODEV;
259 break;
260 }
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700261 offset = alloc_swapdev_block(data->swap);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800262 if (offset) {
263 offset <<= PAGE_SHIFT;
Rafael J. Wysockicc5d2072007-10-26 01:03:33 +0200264 error = put_user(offset, (loff_t __user *)arg);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800265 } else {
266 error = -ENOSPC;
267 }
268 break;
269
270 case SNAPSHOT_FREE_SWAP_PAGES:
271 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
272 error = -ENODEV;
273 break;
274 }
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700275 free_all_swap_pages(data->swap);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800276 break;
277
Rafael J. Wysocki96f73742007-10-26 01:02:15 +0200278 case SNAPSHOT_SET_SWAP_FILE: /* This ioctl is deprecated */
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700279 if (!swsusp_swap_in_use()) {
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800280 /*
281 * User space encodes device types as two-byte values,
282 * so we need to recode them
283 */
284 if (old_decode_dev(arg)) {
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800285 data->swap = swap_type_of(old_decode_dev(arg),
286 0, NULL);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800287 if (data->swap < 0)
288 error = -ENODEV;
289 } else {
290 data->swap = -1;
291 error = -EINVAL;
292 }
293 } else {
294 error = -EPERM;
295 }
296 break;
297
Luca Tettamanti9b238202006-03-23 03:00:09 -0800298 case SNAPSHOT_S2RAM:
299 if (!data->frozen) {
300 error = -EPERM;
301 break;
302 }
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800303 if (!mutex_trylock(&pm_mutex)) {
Luca Tettamanti9b238202006-03-23 03:00:09 -0800304 error = -EBUSY;
305 break;
306 }
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700307 /*
308 * Tasks are frozen and the notifiers have been called with
309 * PM_HIBERNATION_PREPARE
310 */
311 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800312 mutex_unlock(&pm_mutex);
Luca Tettamanti9b238202006-03-23 03:00:09 -0800313 break;
314
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200315 case SNAPSHOT_PLATFORM_SUPPORT:
316 data->platform_support = !!arg;
317 break;
318
319 case SNAPSHOT_POWER_OFF:
320 if (data->platform_support)
321 error = hibernation_platform_enter();
322 break;
323
324 case SNAPSHOT_PMOPS: /* This ioctl is deprecated */
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800325 error = -EINVAL;
326
Stefan Seyfried35926952006-12-06 20:34:06 -0800327 switch (arg) {
328
329 case PMOPS_PREPARE:
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200330 data->platform_support = 1;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700331 error = 0;
Stefan Seyfried35926952006-12-06 20:34:06 -0800332 break;
333
334 case PMOPS_ENTER:
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200335 if (data->platform_support)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700336 error = hibernation_platform_enter();
Stefan Seyfried35926952006-12-06 20:34:06 -0800337 break;
338
339 case PMOPS_FINISH:
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200340 if (data->platform_support)
Rafael J. Wysocki2b5b09b2007-02-10 01:43:35 -0800341 error = 0;
Stefan Seyfried35926952006-12-06 20:34:06 -0800342 break;
343
344 default:
345 printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
Stefan Seyfried35926952006-12-06 20:34:06 -0800346
347 }
348 break;
349
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800350 case SNAPSHOT_SET_SWAP_AREA:
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700351 if (swsusp_swap_in_use()) {
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800352 error = -EPERM;
353 } else {
354 struct resume_swap_area swap_area;
355 dev_t swdev;
356
357 error = copy_from_user(&swap_area, (void __user *)arg,
358 sizeof(struct resume_swap_area));
359 if (error) {
360 error = -EFAULT;
361 break;
362 }
363
364 /*
365 * User space encodes device types as two-byte values,
366 * so we need to recode them
367 */
368 swdev = old_decode_dev(swap_area.dev);
369 if (swdev) {
370 offset = swap_area.offset;
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800371 data->swap = swap_type_of(swdev, offset, NULL);
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800372 if (data->swap < 0)
373 error = -ENODEV;
374 } else {
375 data->swap = -1;
376 error = -EINVAL;
377 }
378 }
379 break;
380
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800381 default:
382 error = -ENOTTY;
383
384 }
385
386 return error;
387}
388
Helge Deller15ad7cd2006-12-06 20:40:36 -0800389static const struct file_operations snapshot_fops = {
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800390 .open = snapshot_open,
391 .release = snapshot_release,
392 .read = snapshot_read,
393 .write = snapshot_write,
394 .llseek = no_llseek,
395 .ioctl = snapshot_ioctl,
396};
397
398static struct miscdevice snapshot_device = {
399 .minor = SNAPSHOT_MINOR,
400 .name = "snapshot",
401 .fops = &snapshot_fops,
402};
403
404static int __init snapshot_device_init(void)
405{
406 return misc_register(&snapshot_device);
407};
408
409device_initcall(snapshot_device_init);