]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - kernel/power/user.c
[PATCH] s390: fix cmm kernel thread handling
[linux-2.6.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/string.h>
15 #include <linux/device.h>
16 #include <linux/miscdevice.h>
17 #include <linux/mm.h>
18 #include <linux/swap.h>
19 #include <linux/swapops.h>
20 #include <linux/pm.h>
21 #include <linux/fs.h>
22 #include <linux/cpu.h>
23
24 #include <asm/uaccess.h>
25
26 #include "power.h"
27
28 #define SNAPSHOT_MINOR  231
29
30 static struct snapshot_data {
31         struct snapshot_handle handle;
32         int swap;
33         struct bitmap_page *bitmap;
34         int mode;
35         char frozen;
36         char ready;
37 } snapshot_state;
38
39 static atomic_t device_available = ATOMIC_INIT(1);
40
41 static int snapshot_open(struct inode *inode, struct file *filp)
42 {
43         struct snapshot_data *data;
44
45         if (!atomic_add_unless(&device_available, -1, 0))
46                 return -EBUSY;
47
48         if ((filp->f_flags & O_ACCMODE) == O_RDWR)
49                 return -ENOSYS;
50
51         nonseekable_open(inode, filp);
52         data = &snapshot_state;
53         filp->private_data = data;
54         memset(&data->handle, 0, sizeof(struct snapshot_handle));
55         if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
56                 data->swap = swsusp_resume_device ? swap_type_of(swsusp_resume_device) : -1;
57                 data->mode = O_RDONLY;
58         } else {
59                 data->swap = -1;
60                 data->mode = O_WRONLY;
61         }
62         data->bitmap = NULL;
63         data->frozen = 0;
64         data->ready = 0;
65
66         return 0;
67 }
68
69 static int snapshot_release(struct inode *inode, struct file *filp)
70 {
71         struct snapshot_data *data;
72
73         swsusp_free();
74         data = filp->private_data;
75         free_all_swap_pages(data->swap, data->bitmap);
76         free_bitmap(data->bitmap);
77         if (data->frozen) {
78                 down(&pm_sem);
79                 thaw_processes();
80                 enable_nonboot_cpus();
81                 up(&pm_sem);
82         }
83         atomic_inc(&device_available);
84         return 0;
85 }
86
87 static ssize_t snapshot_read(struct file *filp, char __user *buf,
88                              size_t count, loff_t *offp)
89 {
90         struct snapshot_data *data;
91         ssize_t res;
92
93         data = filp->private_data;
94         res = snapshot_read_next(&data->handle, count);
95         if (res > 0) {
96                 if (copy_to_user(buf, data_of(data->handle), res))
97                         res = -EFAULT;
98                 else
99                         *offp = data->handle.offset;
100         }
101         return res;
102 }
103
104 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
105                               size_t count, loff_t *offp)
106 {
107         struct snapshot_data *data;
108         ssize_t res;
109
110         data = filp->private_data;
111         res = snapshot_write_next(&data->handle, count);
112         if (res > 0) {
113                 if (copy_from_user(data_of(data->handle), buf, res))
114                         res = -EFAULT;
115                 else
116                         *offp = data->handle.offset;
117         }
118         return res;
119 }
120
121 static int snapshot_ioctl(struct inode *inode, struct file *filp,
122                           unsigned int cmd, unsigned long arg)
123 {
124         int error = 0;
125         struct snapshot_data *data;
126         loff_t offset, avail;
127
128         if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
129                 return -ENOTTY;
130         if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
131                 return -ENOTTY;
132         if (!capable(CAP_SYS_ADMIN))
133                 return -EPERM;
134
135         data = filp->private_data;
136
137         switch (cmd) {
138
139         case SNAPSHOT_FREEZE:
140                 if (data->frozen)
141                         break;
142                 down(&pm_sem);
143                 error = disable_nonboot_cpus();
144                 if (!error) {
145                         error = freeze_processes();
146                         if (error) {
147                                 thaw_processes();
148                                 error = -EBUSY;
149                         }
150                 }
151                 enable_nonboot_cpus();
152                 up(&pm_sem);
153                 if (!error)
154                         data->frozen = 1;
155                 break;
156
157         case SNAPSHOT_UNFREEZE:
158                 if (!data->frozen)
159                         break;
160                 down(&pm_sem);
161                 thaw_processes();
162                 enable_nonboot_cpus();
163                 up(&pm_sem);
164                 data->frozen = 0;
165                 break;
166
167         case SNAPSHOT_ATOMIC_SNAPSHOT:
168                 if (data->mode != O_RDONLY || !data->frozen  || data->ready) {
169                         error = -EPERM;
170                         break;
171                 }
172                 down(&pm_sem);
173                 /* Free memory before shutting down devices. */
174                 error = swsusp_shrink_memory();
175                 if (!error) {
176                         error = device_suspend(PMSG_FREEZE);
177                         if (!error) {
178                                 in_suspend = 1;
179                                 error = swsusp_suspend();
180                                 device_resume();
181                         }
182                 }
183                 up(&pm_sem);
184                 if (!error)
185                         error = put_user(in_suspend, (unsigned int __user *)arg);
186                 if (!error)
187                         data->ready = 1;
188                 break;
189
190         case SNAPSHOT_ATOMIC_RESTORE:
191                 if (data->mode != O_WRONLY || !data->frozen ||
192                     !snapshot_image_loaded(&data->handle)) {
193                         error = -EPERM;
194                         break;
195                 }
196                 snapshot_free_unused_memory(&data->handle);
197                 down(&pm_sem);
198                 pm_prepare_console();
199                 error = device_suspend(PMSG_FREEZE);
200                 if (!error) {
201                         error = swsusp_resume();
202                         device_resume();
203                 }
204                 pm_restore_console();
205                 up(&pm_sem);
206                 break;
207
208         case SNAPSHOT_FREE:
209                 swsusp_free();
210                 memset(&data->handle, 0, sizeof(struct snapshot_handle));
211                 data->ready = 0;
212                 break;
213
214         case SNAPSHOT_SET_IMAGE_SIZE:
215                 image_size = arg;
216                 break;
217
218         case SNAPSHOT_AVAIL_SWAP:
219                 avail = count_swap_pages(data->swap, 1);
220                 avail <<= PAGE_SHIFT;
221                 error = put_user(avail, (loff_t __user *)arg);
222                 break;
223
224         case SNAPSHOT_GET_SWAP_PAGE:
225                 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
226                         error = -ENODEV;
227                         break;
228                 }
229                 if (!data->bitmap) {
230                         data->bitmap = alloc_bitmap(count_swap_pages(data->swap, 0));
231                         if (!data->bitmap) {
232                                 error = -ENOMEM;
233                                 break;
234                         }
235                 }
236                 offset = alloc_swap_page(data->swap, data->bitmap);
237                 if (offset) {
238                         offset <<= PAGE_SHIFT;
239                         error = put_user(offset, (loff_t __user *)arg);
240                 } else {
241                         error = -ENOSPC;
242                 }
243                 break;
244
245         case SNAPSHOT_FREE_SWAP_PAGES:
246                 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
247                         error = -ENODEV;
248                         break;
249                 }
250                 free_all_swap_pages(data->swap, data->bitmap);
251                 free_bitmap(data->bitmap);
252                 data->bitmap = NULL;
253                 break;
254
255         case SNAPSHOT_SET_SWAP_FILE:
256                 if (!data->bitmap) {
257                         /*
258                          * User space encodes device types as two-byte values,
259                          * so we need to recode them
260                          */
261                         if (old_decode_dev(arg)) {
262                                 data->swap = swap_type_of(old_decode_dev(arg));
263                                 if (data->swap < 0)
264                                         error = -ENODEV;
265                         } else {
266                                 data->swap = -1;
267                                 error = -EINVAL;
268                         }
269                 } else {
270                         error = -EPERM;
271                 }
272                 break;
273
274         case SNAPSHOT_S2RAM:
275                 if (!data->frozen) {
276                         error = -EPERM;
277                         break;
278                 }
279
280                 if (down_trylock(&pm_sem)) {
281                         error = -EBUSY;
282                         break;
283                 }
284
285                 if (pm_ops->prepare) {
286                         error = pm_ops->prepare(PM_SUSPEND_MEM);
287                         if (error)
288                                 goto OutS3;
289                 }
290
291                 /* Put devices to sleep */
292                 error = device_suspend(PMSG_SUSPEND);
293                 if (error) {
294                         printk(KERN_ERR "Failed to suspend some devices.\n");
295                 } else {
296                         /* Enter S3, system is already frozen */
297                         suspend_enter(PM_SUSPEND_MEM);
298
299                         /* Wake up devices */
300                         device_resume();
301                 }
302
303                 if (pm_ops->finish)
304                         pm_ops->finish(PM_SUSPEND_MEM);
305
306 OutS3:
307                 up(&pm_sem);
308                 break;
309
310         default:
311                 error = -ENOTTY;
312
313         }
314
315         return error;
316 }
317
318 static struct file_operations snapshot_fops = {
319         .open = snapshot_open,
320         .release = snapshot_release,
321         .read = snapshot_read,
322         .write = snapshot_write,
323         .llseek = no_llseek,
324         .ioctl = snapshot_ioctl,
325 };
326
327 static struct miscdevice snapshot_device = {
328         .minor = SNAPSHOT_MINOR,
329         .name = "snapshot",
330         .fops = &snapshot_fops,
331 };
332
333 static int __init snapshot_device_init(void)
334 {
335         return misc_register(&snapshot_device);
336 };
337
338 device_initcall(snapshot_device_init);