hrtimer: raise softirq unlocked to avoid circular lock dependency
[deliverable/linux.git] / kernel / power / user.c
CommitLineData
6e1819d6
RW
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>
3592695c 14#include <linux/reboot.h>
6e1819d6
RW
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>
97c7801c 23#include <linux/console.h>
e3920fb4 24#include <linux/cpu.h>
7dfb7103 25#include <linux/freezer.h>
6e1819d6
RW
26
27#include <asm/uaccess.h>
28
29#include "power.h"
30
eb57c1cf 31/*
96f73749
RW
32 * 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.
eb57c1cf 35 */
96f73749 36#define SNAPSHOT_SET_SWAP_FILE _IOW(SNAPSHOT_IOC_MAGIC, 10, unsigned int)
eb57c1cf
RW
37#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
cc5d207c
RW
43/*
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
eb57c1cf 53
6e1819d6
RW
54#define SNAPSHOT_MINOR 231
55
56static struct snapshot_data {
57 struct snapshot_handle handle;
58 int swap;
6e1819d6
RW
59 int mode;
60 char frozen;
61 char ready;
eb57c1cf 62 char platform_support;
6e1819d6
RW
63} snapshot_state;
64
0709db60 65atomic_t snapshot_device_available = ATOMIC_INIT(1);
6e1819d6
RW
66
67static int snapshot_open(struct inode *inode, struct file *filp)
68{
69 struct snapshot_data *data;
c3e94d89 70 int error;
6e1819d6 71
0709db60 72 if (!atomic_add_unless(&snapshot_device_available, -1, 0))
6e1819d6
RW
73 return -EBUSY;
74
1525a2ad 75 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
0709db60 76 atomic_inc(&snapshot_device_available);
6e1819d6 77 return -ENOSYS;
1525a2ad
RW
78 }
79 if(create_basic_memory_bitmaps()) {
0709db60 80 atomic_inc(&snapshot_device_available);
74dfd666 81 return -ENOMEM;
1525a2ad 82 }
6e1819d6
RW
83 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) {
915bae9e 88 data->swap = swsusp_resume_device ?
7bf23687 89 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
6e1819d6 90 data->mode = O_RDONLY;
c3e94d89
AS
91 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
92 if (error)
93 pm_notifier_call_chain(PM_POST_RESTORE);
6e1819d6
RW
94 } else {
95 data->swap = -1;
96 data->mode = O_WRONLY;
c3e94d89
AS
97 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;
6e1819d6 104 }
6e1819d6
RW
105 data->frozen = 0;
106 data->ready = 0;
eb57c1cf 107 data->platform_support = 0;
6e1819d6
RW
108
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();
74dfd666 117 free_basic_memory_bitmaps();
6e1819d6 118 data = filp->private_data;
d1d241cc 119 free_all_swap_pages(data->swap);
6e1819d6 120 if (data->frozen) {
a6d70980 121 mutex_lock(&pm_mutex);
6e1819d6 122 thaw_processes();
a6d70980 123 mutex_unlock(&pm_mutex);
6e1819d6 124 }
c3e94d89
AS
125 pm_notifier_call_chain(data->mode == O_WRONLY ?
126 PM_POST_HIBERNATION : PM_POST_RESTORE);
0709db60 127 atomic_inc(&snapshot_device_available);
6e1819d6
RW
128 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;
2f41dddb
RW
138 if (!data->ready)
139 return -ENODATA;
6e1819d6
RW
140 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;
af508b34 172 loff_t size;
3aef83e0 173 sector_t offset;
6e1819d6
RW
174
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;
a6d70980 189 mutex_lock(&pm_mutex);
c3e94d89
AS
190 printk("Syncing filesystems ... ");
191 sys_sync();
192 printk("done.\n");
193
194 error = freeze_processes();
b10d9117 195 if (error)
c3e94d89 196 thaw_processes();
a6d70980 197 mutex_unlock(&pm_mutex);
6e1819d6
RW
198 if (!error)
199 data->frozen = 1;
200 break;
201
202 case SNAPSHOT_UNFREEZE:
2f41dddb 203 if (!data->frozen || data->ready)
6e1819d6 204 break;
a6d70980 205 mutex_lock(&pm_mutex);
6e1819d6 206 thaw_processes();
a6d70980 207 mutex_unlock(&pm_mutex);
6e1819d6
RW
208 data->frozen = 0;
209 break;
210
cc5d207c 211 case SNAPSHOT_CREATE_IMAGE:
6e1819d6
RW
212 case SNAPSHOT_ATOMIC_SNAPSHOT:
213 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
214 error = -EPERM;
215 break;
216 }
eb57c1cf 217 error = hibernation_snapshot(data->platform_support);
6e1819d6 218 if (!error)
cc5d207c 219 error = put_user(in_suspend, (int __user *)arg);
6e1819d6
RW
220 if (!error)
221 data->ready = 1;
222 break;
223
224 case SNAPSHOT_ATOMIC_RESTORE:
8357376d 225 snapshot_write_finalize(&data->handle);
6e1819d6
RW
226 if (data->mode != O_WRONLY || !data->frozen ||
227 !snapshot_image_loaded(&data->handle)) {
228 error = -EPERM;
229 break;
230 }
eb57c1cf 231 error = hibernation_restore(data->platform_support);
6e1819d6
RW
232 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
cc5d207c 240 case SNAPSHOT_PREF_IMAGE_SIZE:
6e1819d6
RW
241 case SNAPSHOT_SET_IMAGE_SIZE:
242 image_size = arg;
243 break;
244
af508b34
RW
245 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
cc5d207c 255 case SNAPSHOT_AVAIL_SWAP_SIZE:
6e1819d6 256 case SNAPSHOT_AVAIL_SWAP:
af508b34
RW
257 size = count_swap_pages(data->swap, 1);
258 size <<= PAGE_SHIFT;
259 error = put_user(size, (loff_t __user *)arg);
6e1819d6
RW
260 break;
261
cc5d207c 262 case SNAPSHOT_ALLOC_SWAP_PAGE:
6e1819d6
RW
263 case SNAPSHOT_GET_SWAP_PAGE:
264 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
265 error = -ENODEV;
266 break;
267 }
d1d241cc 268 offset = alloc_swapdev_block(data->swap);
6e1819d6
RW
269 if (offset) {
270 offset <<= PAGE_SHIFT;
cc5d207c 271 error = put_user(offset, (loff_t __user *)arg);
6e1819d6
RW
272 } 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 }
d1d241cc 282 free_all_swap_pages(data->swap);
6e1819d6
RW
283 break;
284
96f73749 285 case SNAPSHOT_SET_SWAP_FILE: /* This ioctl is deprecated */
d1d241cc 286 if (!swsusp_swap_in_use()) {
6e1819d6
RW
287 /*
288 * User space encodes device types as two-byte values,
289 * so we need to recode them
290 */
291 if (old_decode_dev(arg)) {
7bf23687
RW
292 data->swap = swap_type_of(old_decode_dev(arg),
293 0, NULL);
6e1819d6
RW
294 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
9b238205
LT
305 case SNAPSHOT_S2RAM:
306 if (!data->frozen) {
307 error = -EPERM;
308 break;
309 }
a6d70980 310 if (!mutex_trylock(&pm_mutex)) {
9b238205
LT
311 error = -EBUSY;
312 break;
313 }
6c961dfb
RW
314 /*
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);
a6d70980 319 mutex_unlock(&pm_mutex);
9b238205
LT
320 break;
321
eb57c1cf
RW
322 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 */
2b5b09b3
RW
332 error = -EINVAL;
333
3592695c
SS
334 switch (arg) {
335
336 case PMOPS_PREPARE:
eb57c1cf 337 data->platform_support = 1;
7777fab9 338 error = 0;
3592695c
SS
339 break;
340
341 case PMOPS_ENTER:
eb57c1cf 342 if (data->platform_support)
7777fab9 343 error = hibernation_platform_enter();
3592695c
SS
344 break;
345
346 case PMOPS_FINISH:
eb57c1cf 347 if (data->platform_support)
2b5b09b3 348 error = 0;
3592695c
SS
349 break;
350
351 default:
352 printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
3592695c
SS
353
354 }
355 break;
356
37b2ba12 357 case SNAPSHOT_SET_SWAP_AREA:
d1d241cc 358 if (swsusp_swap_in_use()) {
37b2ba12
RW
359 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;
7bf23687 378 data->swap = swap_type_of(swdev, offset, NULL);
37b2ba12
RW
379 if (data->swap < 0)
380 error = -ENODEV;
381 } else {
382 data->swap = -1;
383 error = -EINVAL;
384 }
385 }
386 break;
387
6e1819d6
RW
388 default:
389 error = -ENOTTY;
390
391 }
392
393 return error;
394}
395
15ad7cdc 396static const struct file_operations snapshot_fops = {
6e1819d6
RW
397 .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);
This page took 0.301319 seconds and 5 git commands to generate.