blob: 3e100075b13cb6ab449034af458266e20dcaa369 [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>
Paul Gortmaker74da1ff2011-05-26 12:48:41 -040015#include <linux/kmod.h>
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080016#include <linux/string.h>
17#include <linux/device.h>
18#include <linux/miscdevice.h>
19#include <linux/mm.h>
20#include <linux/swap.h>
21#include <linux/swapops.h>
22#include <linux/pm.h>
23#include <linux/fs.h>
Ben Hutchingsc3360782011-12-27 22:54:52 +010024#include <linux/compat.h>
Rafael J. Wysocki97c78012006-10-11 01:20:45 -070025#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070026#include <linux/cpu.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080027#include <linux/freezer.h>
Rafael J. Wysockic7510852009-04-12 20:06:56 +020028#include <scsi/scsi_scan.h>
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080029
30#include <asm/uaccess.h>
31
32#include "power.h"
33
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +020034
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080035#define SNAPSHOT_MINOR 231
36
37static struct snapshot_data {
38 struct snapshot_handle handle;
39 int swap;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080040 int mode;
41 char frozen;
42 char ready;
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +020043 char platform_support;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080044} snapshot_state;
45
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070046atomic_t snapshot_device_available = ATOMIC_INIT(1);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080047
48static int snapshot_open(struct inode *inode, struct file *filp)
49{
50 struct snapshot_data *data;
Alan Sternc3e94d82007-11-19 23:38:25 +010051 int error;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080052
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010053 lock_system_sleep();
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +020054
55 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
56 error = -EBUSY;
57 goto Unlock;
58 }
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080059
Rafael J. Wysocki1525a2a2007-05-06 14:50:44 -070060 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070061 atomic_inc(&snapshot_device_available);
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +020062 error = -ENOSYS;
63 goto Unlock;
Rafael J. Wysocki1525a2a2007-05-06 14:50:44 -070064 }
65 if(create_basic_memory_bitmaps()) {
Rafael J. Wysocki0709db62007-05-06 14:50:45 -070066 atomic_inc(&snapshot_device_available);
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +020067 error = -ENOMEM;
68 goto Unlock;
Rafael J. Wysocki1525a2a2007-05-06 14:50:44 -070069 }
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080070 nonseekable_open(inode, filp);
71 data = &snapshot_state;
72 filp->private_data = data;
73 memset(&data->handle, 0, sizeof(struct snapshot_handle));
74 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
Rafael J. Wysockic7510852009-04-12 20:06:56 +020075 /* Hibernating. The image device should be accessible. */
Rafael J. Wysocki915bae92006-12-06 20:34:07 -080076 data->swap = swsusp_resume_device ?
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -080077 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080078 data->mode = O_RDONLY;
Alan Sternc3e94d82007-11-19 23:38:25 +010079 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
80 if (error)
81 pm_notifier_call_chain(PM_POST_HIBERNATION);
Andrey Borzenkovebae2602009-02-14 02:05:14 +010082 } else {
Rafael J. Wysockic7510852009-04-12 20:06:56 +020083 /*
84 * Resuming. We may need to wait for the image device to
85 * appear.
86 */
87 wait_for_device_probe();
88 scsi_complete_async_scans();
89
Andrey Borzenkovebae2602009-02-14 02:05:14 +010090 data->swap = -1;
91 data->mode = O_WRONLY;
92 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
93 if (error)
94 pm_notifier_call_chain(PM_POST_RESTORE);
Alan Sternc3e94d82007-11-19 23:38:25 +010095 }
Michal Kubecek8440f4b2011-06-18 20:34:01 +020096 if (error) {
97 free_basic_memory_bitmaps();
Alan Sternc3e94d82007-11-19 23:38:25 +010098 atomic_inc(&snapshot_device_available);
Michal Kubecek8440f4b2011-06-18 20:34:01 +020099 }
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800100 data->frozen = 0;
101 data->ready = 0;
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200102 data->platform_support = 0;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800103
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200104 Unlock:
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100105 unlock_system_sleep();
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200106
107 return error;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800108}
109
110static int snapshot_release(struct inode *inode, struct file *filp)
111{
112 struct snapshot_data *data;
113
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100114 lock_system_sleep();
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200115
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800116 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. Wysocki97449972011-05-10 21:10:01 +0200120 if (data->frozen) {
121 pm_restore_gfp_mask();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800122 thaw_processes();
Rafael J. Wysocki97449972011-05-10 21:10:01 +0200123 }
Takashi Iwai1497dd12010-12-10 00:16:39 +0100124 pm_notifier_call_chain(data->mode == O_RDONLY ?
Alan Sternc3e94d82007-11-19 23:38:25 +0100125 PM_POST_HIBERNATION : PM_POST_RESTORE);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700126 atomic_inc(&snapshot_device_available);
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200127
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100128 unlock_system_sleep();
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200129
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800130 return 0;
131}
132
133static ssize_t snapshot_read(struct file *filp, char __user *buf,
134 size_t count, loff_t *offp)
135{
136 struct snapshot_data *data;
137 ssize_t res;
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200138 loff_t pg_offp = *offp & ~PAGE_MASK;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800139
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100140 lock_system_sleep();
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200141
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800142 data = filp->private_data;
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200143 if (!data->ready) {
144 res = -ENODATA;
145 goto Unlock;
146 }
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200147 if (!pg_offp) { /* on page boundary? */
148 res = snapshot_read_next(&data->handle);
149 if (res <= 0)
150 goto Unlock;
151 } else {
152 res = PAGE_SIZE - pg_offp;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800153 }
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200154
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200155 res = simple_read_from_buffer(buf, count, &pg_offp,
156 data_of(data->handle), res);
157 if (res > 0)
158 *offp += res;
159
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200160 Unlock:
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100161 unlock_system_sleep();
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200162
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800163 return res;
164}
165
166static ssize_t snapshot_write(struct file *filp, const char __user *buf,
167 size_t count, loff_t *offp)
168{
169 struct snapshot_data *data;
170 ssize_t res;
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200171 loff_t pg_offp = *offp & ~PAGE_MASK;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800172
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100173 lock_system_sleep();
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200174
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800175 data = filp->private_data;
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200176
177 if (!pg_offp) {
178 res = snapshot_write_next(&data->handle);
179 if (res <= 0)
180 goto unlock;
181 } else {
182 res = PAGE_SIZE - pg_offp;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800183 }
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200184
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200185 res = simple_write_to_buffer(data_of(data->handle), res, &pg_offp,
186 buf, count);
187 if (res > 0)
188 *offp += res;
189unlock:
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100190 unlock_system_sleep();
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200191
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800192 return res;
193}
194
Alan Cox52d11022008-06-11 22:07:52 +0200195static long snapshot_ioctl(struct file *filp, unsigned int cmd,
196 unsigned long arg)
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800197{
198 int error = 0;
199 struct snapshot_data *data;
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +0200200 loff_t size;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800201 sector_t offset;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800202
203 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
204 return -ENOTTY;
205 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
206 return -ENOTTY;
207 if (!capable(CAP_SYS_ADMIN))
208 return -EPERM;
209
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200210 if (!mutex_trylock(&pm_mutex))
211 return -EBUSY;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800212
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200213 data = filp->private_data;
Alan Cox52d11022008-06-11 22:07:52 +0200214
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800215 switch (cmd) {
216
217 case SNAPSHOT_FREEZE:
218 if (data->frozen)
219 break;
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700220
Alan Sternc3e94d82007-11-19 23:38:25 +0100221 printk("Syncing filesystems ... ");
222 sys_sync();
223 printk("done.\n");
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700224
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700225 error = usermodehelper_disable();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700226 if (error)
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700227 break;
228
229 error = freeze_processes();
Tejun Heo03afed82011-11-21 12:32:24 -0800230 if (error)
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700231 usermodehelper_enable();
Srivatsa S. Bhate5b16742011-12-03 00:20:30 +0100232 else
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800233 data->frozen = 1;
234 break;
235
236 case SNAPSHOT_UNFREEZE:
Rafael J. Wysocki2f41ddd2007-06-16 10:16:03 -0700237 if (!data->frozen || data->ready)
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800238 break;
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100239 pm_restore_gfp_mask();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800240 thaw_processes();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700241 usermodehelper_enable();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800242 data->frozen = 0;
243 break;
244
Jiri Slabyb694e522010-01-27 23:47:50 +0100245 case SNAPSHOT_CREATE_IMAGE:
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800246 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
247 error = -EPERM;
248 break;
249 }
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100250 pm_restore_gfp_mask();
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200251 error = hibernation_snapshot(data->platform_support);
Srivatsa S. Bhatfe9161d2012-02-01 22:16:36 +0100252 if (error) {
253 thaw_kernel_threads();
254 } else {
Rafael J. Wysockicc5d2072007-10-26 01:03:33 +0200255 error = put_user(in_suspend, (int __user *)arg);
Srivatsa S. Bhat97819a22011-12-01 22:33:10 +0100256 if (!error && !freezer_test_done)
257 data->ready = 1;
258 if (freezer_test_done) {
259 freezer_test_done = false;
Srivatsa S. Bhatfe9161d2012-02-01 22:16:36 +0100260 thaw_kernel_threads();
Srivatsa S. Bhat97819a22011-12-01 22:33:10 +0100261 }
262 }
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800263 break;
264
265 case SNAPSHOT_ATOMIC_RESTORE:
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800266 snapshot_write_finalize(&data->handle);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800267 if (data->mode != O_WRONLY || !data->frozen ||
268 !snapshot_image_loaded(&data->handle)) {
269 error = -EPERM;
270 break;
271 }
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200272 error = hibernation_restore(data->platform_support);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800273 break;
274
275 case SNAPSHOT_FREE:
276 swsusp_free();
277 memset(&data->handle, 0, sizeof(struct snapshot_handle));
278 data->ready = 0;
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100279 /*
280 * It is necessary to thaw kernel threads here, because
281 * SNAPSHOT_CREATE_IMAGE may be invoked directly after
282 * SNAPSHOT_FREE. In that case, if kernel threads were not
283 * thawed, the preallocation of memory carried out by
284 * hibernation_snapshot() might run into problems (i.e. it
285 * might fail or even deadlock).
286 */
287 thaw_kernel_threads();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800288 break;
289
Jiri Slabyb694e522010-01-27 23:47:50 +0100290 case SNAPSHOT_PREF_IMAGE_SIZE:
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800291 image_size = arg;
292 break;
293
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +0200294 case SNAPSHOT_GET_IMAGE_SIZE:
295 if (!data->ready) {
296 error = -ENODATA;
297 break;
298 }
299 size = snapshot_get_image_size();
300 size <<= PAGE_SHIFT;
301 error = put_user(size, (loff_t __user *)arg);
302 break;
303
Jiri Slabyb694e522010-01-27 23:47:50 +0100304 case SNAPSHOT_AVAIL_SWAP_SIZE:
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +0200305 size = count_swap_pages(data->swap, 1);
306 size <<= PAGE_SHIFT;
307 error = put_user(size, (loff_t __user *)arg);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800308 break;
309
Jiri Slabyb694e522010-01-27 23:47:50 +0100310 case SNAPSHOT_ALLOC_SWAP_PAGE:
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800311 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
312 error = -ENODEV;
313 break;
314 }
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700315 offset = alloc_swapdev_block(data->swap);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800316 if (offset) {
317 offset <<= PAGE_SHIFT;
Rafael J. Wysockicc5d2072007-10-26 01:03:33 +0200318 error = put_user(offset, (loff_t __user *)arg);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800319 } else {
320 error = -ENOSPC;
321 }
322 break;
323
324 case SNAPSHOT_FREE_SWAP_PAGES:
325 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
326 error = -ENODEV;
327 break;
328 }
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700329 free_all_swap_pages(data->swap);
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800330 break;
331
Luca Tettamanti9b238202006-03-23 03:00:09 -0800332 case SNAPSHOT_S2RAM:
333 if (!data->frozen) {
334 error = -EPERM;
335 break;
336 }
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700337 /*
338 * Tasks are frozen and the notifiers have been called with
339 * PM_HIBERNATION_PREPARE
340 */
341 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
Rafael J. Wysocki36cb7032011-05-10 21:10:13 +0200342 data->ready = 0;
Luca Tettamanti9b238202006-03-23 03:00:09 -0800343 break;
344
Rafael J. Wysockieb57c1c2007-10-26 01:01:10 +0200345 case SNAPSHOT_PLATFORM_SUPPORT:
346 data->platform_support = !!arg;
347 break;
348
349 case SNAPSHOT_POWER_OFF:
350 if (data->platform_support)
351 error = hibernation_platform_enter();
352 break;
353
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800354 case SNAPSHOT_SET_SWAP_AREA:
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700355 if (swsusp_swap_in_use()) {
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800356 error = -EPERM;
357 } else {
358 struct resume_swap_area swap_area;
359 dev_t swdev;
360
361 error = copy_from_user(&swap_area, (void __user *)arg,
362 sizeof(struct resume_swap_area));
363 if (error) {
364 error = -EFAULT;
365 break;
366 }
367
368 /*
369 * User space encodes device types as two-byte values,
370 * so we need to recode them
371 */
Jiri Slabyd88d4052010-04-10 22:28:56 +0200372 swdev = new_decode_dev(swap_area.dev);
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800373 if (swdev) {
374 offset = swap_area.offset;
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800375 data->swap = swap_type_of(swdev, offset, NULL);
Rafael J. Wysocki37b2ba12006-12-06 20:34:15 -0800376 if (data->swap < 0)
377 error = -ENODEV;
378 } else {
379 data->swap = -1;
380 error = -EINVAL;
381 }
382 }
383 break;
384
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800385 default:
386 error = -ENOTTY;
387
388 }
Rafael J. Wysocki25f2f3d2008-06-11 22:09:45 +0200389
390 mutex_unlock(&pm_mutex);
391
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800392 return error;
393}
394
Ben Hutchingsc3360782011-12-27 22:54:52 +0100395#ifdef CONFIG_COMPAT
396
397struct compat_resume_swap_area {
398 compat_loff_t offset;
399 u32 dev;
400} __packed;
401
402static long
403snapshot_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
404{
405 BUILD_BUG_ON(sizeof(loff_t) != sizeof(compat_loff_t));
406
407 switch (cmd) {
408 case SNAPSHOT_GET_IMAGE_SIZE:
409 case SNAPSHOT_AVAIL_SWAP_SIZE:
410 case SNAPSHOT_ALLOC_SWAP_PAGE: {
411 compat_loff_t __user *uoffset = compat_ptr(arg);
412 loff_t offset;
413 mm_segment_t old_fs;
414 int err;
415
416 old_fs = get_fs();
417 set_fs(KERNEL_DS);
418 err = snapshot_ioctl(file, cmd, (unsigned long) &offset);
419 set_fs(old_fs);
420 if (!err && put_user(offset, uoffset))
421 err = -EFAULT;
422 return err;
423 }
424
425 case SNAPSHOT_CREATE_IMAGE:
426 return snapshot_ioctl(file, cmd,
427 (unsigned long) compat_ptr(arg));
428
429 case SNAPSHOT_SET_SWAP_AREA: {
430 struct compat_resume_swap_area __user *u_swap_area =
431 compat_ptr(arg);
432 struct resume_swap_area swap_area;
433 mm_segment_t old_fs;
434 int err;
435
436 err = get_user(swap_area.offset, &u_swap_area->offset);
437 err |= get_user(swap_area.dev, &u_swap_area->dev);
438 if (err)
439 return -EFAULT;
440 old_fs = get_fs();
441 set_fs(KERNEL_DS);
442 err = snapshot_ioctl(file, SNAPSHOT_SET_SWAP_AREA,
443 (unsigned long) &swap_area);
444 set_fs(old_fs);
445 return err;
446 }
447
448 default:
449 return snapshot_ioctl(file, cmd, arg);
450 }
451}
452
453#endif /* CONFIG_COMPAT */
454
Helge Deller15ad7cd2006-12-06 20:40:36 -0800455static const struct file_operations snapshot_fops = {
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800456 .open = snapshot_open,
457 .release = snapshot_release,
458 .read = snapshot_read,
459 .write = snapshot_write,
460 .llseek = no_llseek,
Alan Cox52d11022008-06-11 22:07:52 +0200461 .unlocked_ioctl = snapshot_ioctl,
Ben Hutchingsc3360782011-12-27 22:54:52 +0100462#ifdef CONFIG_COMPAT
463 .compat_ioctl = snapshot_compat_ioctl,
464#endif
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800465};
466
467static struct miscdevice snapshot_device = {
468 .minor = SNAPSHOT_MINOR,
469 .name = "snapshot",
470 .fops = &snapshot_fops,
471};
472
473static int __init snapshot_device_init(void)
474{
475 return misc_register(&snapshot_device);
476};
477
478device_initcall(snapshot_device_init);