user.c 7.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * linux/kernel/power/user.c
 *
 * This file provides the user space interface for software suspend/resume.
 *
 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
 *
 * This file is released under the GPLv2.
 *
 */

#include <linux/suspend.h>
#include <linux/syscalls.h>
14
#include <linux/reboot.h>
15 16 17 18 19 20 21 22
#include <linux/string.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/mm.h>
#include <linux/swap.h>
#include <linux/swapops.h>
#include <linux/pm.h>
#include <linux/fs.h>
23
#include <linux/console.h>
24
#include <linux/cpu.h>
25
#include <linux/freezer.h>
26 27 28 29 30 31 32 33 34 35 36 37 38

#include <asm/uaccess.h>

#include "power.h"

#define SNAPSHOT_MINOR	231

static struct snapshot_data {
	struct snapshot_handle handle;
	int swap;
	int mode;
	char frozen;
	char ready;
39
	char platform_suspend;
40 41
} snapshot_state;

42
atomic_t snapshot_device_available = ATOMIC_INIT(1);
43 44 45 46 47

static int snapshot_open(struct inode *inode, struct file *filp)
{
	struct snapshot_data *data;

48
	if (!atomic_add_unless(&snapshot_device_available, -1, 0))
49 50
		return -EBUSY;

51
	if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
52
		atomic_inc(&snapshot_device_available);
53
		return -ENOSYS;
54 55
	}
	if(create_basic_memory_bitmaps()) {
56
		atomic_inc(&snapshot_device_available);
57
		return -ENOMEM;
58
	}
59 60 61 62 63
	nonseekable_open(inode, filp);
	data = &snapshot_state;
	filp->private_data = data;
	memset(&data->handle, 0, sizeof(struct snapshot_handle));
	if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
64
		data->swap = swsusp_resume_device ?
65
			swap_type_of(swsusp_resume_device, 0, NULL) : -1;
66 67 68 69 70 71 72
		data->mode = O_RDONLY;
	} else {
		data->swap = -1;
		data->mode = O_WRONLY;
	}
	data->frozen = 0;
	data->ready = 0;
73
	data->platform_suspend = 0;
74 75 76 77 78 79 80 81 82

	return 0;
}

static int snapshot_release(struct inode *inode, struct file *filp)
{
	struct snapshot_data *data;

	swsusp_free();
83
	free_basic_memory_bitmaps();
84
	data = filp->private_data;
85
	free_all_swap_pages(data->swap);
86
	if (data->frozen) {
87
		mutex_lock(&pm_mutex);
88
		thaw_processes();
89
		mutex_unlock(&pm_mutex);
90
	}
91
	atomic_inc(&snapshot_device_available);
92 93 94 95 96 97 98 99 100 101
	return 0;
}

static ssize_t snapshot_read(struct file *filp, char __user *buf,
                             size_t count, loff_t *offp)
{
	struct snapshot_data *data;
	ssize_t res;

	data = filp->private_data;
102 103
	if (!data->ready)
		return -ENODATA;
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	res = snapshot_read_next(&data->handle, count);
	if (res > 0) {
		if (copy_to_user(buf, data_of(data->handle), res))
			res = -EFAULT;
		else
			*offp = data->handle.offset;
	}
	return res;
}

static ssize_t snapshot_write(struct file *filp, const char __user *buf,
                              size_t count, loff_t *offp)
{
	struct snapshot_data *data;
	ssize_t res;

	data = filp->private_data;
	res = snapshot_write_next(&data->handle, count);
	if (res > 0) {
		if (copy_from_user(data_of(data->handle), buf, res))
			res = -EFAULT;
		else
			*offp = data->handle.offset;
	}
	return res;
}

static int snapshot_ioctl(struct inode *inode, struct file *filp,
                          unsigned int cmd, unsigned long arg)
{
	int error = 0;
	struct snapshot_data *data;
136 137
	loff_t avail;
	sector_t offset;
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

	if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
		return -ENOTTY;
	if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
		return -ENOTTY;
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

	data = filp->private_data;

	switch (cmd) {

	case SNAPSHOT_FREEZE:
		if (data->frozen)
			break;
153
		mutex_lock(&pm_mutex);
154 155 156 157 158
		error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
		if (!error) {
			error = freeze_processes();
			if (error)
				thaw_processes();
159
		}
160 161
		if (error)
			pm_notifier_call_chain(PM_POST_HIBERNATION);
162
		mutex_unlock(&pm_mutex);
163 164 165 166 167
		if (!error)
			data->frozen = 1;
		break;

	case SNAPSHOT_UNFREEZE:
168
		if (!data->frozen || data->ready)
169
			break;
170
		mutex_lock(&pm_mutex);
171
		thaw_processes();
172
		pm_notifier_call_chain(PM_POST_HIBERNATION);
173
		mutex_unlock(&pm_mutex);
174 175 176 177 178 179 180 181
		data->frozen = 0;
		break;

	case SNAPSHOT_ATOMIC_SNAPSHOT:
		if (data->mode != O_RDONLY || !data->frozen  || data->ready) {
			error = -EPERM;
			break;
		}
182
		error = hibernation_snapshot(data->platform_suspend);
183 184 185 186 187 188 189
		if (!error)
			error = put_user(in_suspend, (unsigned int __user *)arg);
		if (!error)
			data->ready = 1;
		break;

	case SNAPSHOT_ATOMIC_RESTORE:
190
		snapshot_write_finalize(&data->handle);
191 192 193 194 195
		if (data->mode != O_WRONLY || !data->frozen ||
		    !snapshot_image_loaded(&data->handle)) {
			error = -EPERM;
			break;
		}
196
		error = hibernation_restore(data->platform_suspend);
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
		break;

	case SNAPSHOT_FREE:
		swsusp_free();
		memset(&data->handle, 0, sizeof(struct snapshot_handle));
		data->ready = 0;
		break;

	case SNAPSHOT_SET_IMAGE_SIZE:
		image_size = arg;
		break;

	case SNAPSHOT_AVAIL_SWAP:
		avail = count_swap_pages(data->swap, 1);
		avail <<= PAGE_SHIFT;
		error = put_user(avail, (loff_t __user *)arg);
		break;

	case SNAPSHOT_GET_SWAP_PAGE:
		if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
			error = -ENODEV;
			break;
		}
220
		offset = alloc_swapdev_block(data->swap);
221 222
		if (offset) {
			offset <<= PAGE_SHIFT;
223
			error = put_user(offset, (sector_t __user *)arg);
224 225 226 227 228 229 230 231 232 233
		} else {
			error = -ENOSPC;
		}
		break;

	case SNAPSHOT_FREE_SWAP_PAGES:
		if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
			error = -ENODEV;
			break;
		}
234
		free_all_swap_pages(data->swap);
235 236 237
		break;

	case SNAPSHOT_SET_SWAP_FILE:
238
		if (!swsusp_swap_in_use()) {
239 240 241 242 243
			/*
			 * User space encodes device types as two-byte values,
			 * so we need to recode them
			 */
			if (old_decode_dev(arg)) {
244 245
				data->swap = swap_type_of(old_decode_dev(arg),
							0, NULL);
246 247 248 249 250 251 252 253 254 255 256
				if (data->swap < 0)
					error = -ENODEV;
			} else {
				data->swap = -1;
				error = -EINVAL;
			}
		} else {
			error = -EPERM;
		}
		break;

257
	case SNAPSHOT_S2RAM:
258 259 260 261 262
		if (!pm_ops) {
			error = -ENOSYS;
			break;
		}

263 264 265 266 267
		if (!data->frozen) {
			error = -EPERM;
			break;
		}

268
		if (!mutex_trylock(&pm_mutex)) {
269 270 271 272 273 274 275 276 277 278 279
			error = -EBUSY;
			break;
		}

		if (pm_ops->prepare) {
			error = pm_ops->prepare(PM_SUSPEND_MEM);
			if (error)
				goto OutS3;
		}

		/* Put devices to sleep */
280
		suspend_console();
281 282 283 284
		error = device_suspend(PMSG_SUSPEND);
		if (error) {
			printk(KERN_ERR "Failed to suspend some devices.\n");
		} else {
285 286 287 288 289 290
			error = disable_nonboot_cpus();
			if (!error) {
				/* Enter S3, system is already frozen */
				suspend_enter(PM_SUSPEND_MEM);
				enable_nonboot_cpus();
			}
291 292 293
			/* Wake up devices */
			device_resume();
		}
294
		resume_console();
295 296 297
		if (pm_ops->finish)
			pm_ops->finish(PM_SUSPEND_MEM);

R
Rafael J. Wysocki 已提交
298
 OutS3:
299
		mutex_unlock(&pm_mutex);
300 301
		break;

302
	case SNAPSHOT_PMOPS:
303 304
		error = -EINVAL;

305 306 307
		switch (arg) {

		case PMOPS_PREPARE:
308 309
			data->platform_suspend = 1;
			error = 0;
310 311 312
			break;

		case PMOPS_ENTER:
313 314 315
			if (data->platform_suspend)
				error = hibernation_platform_enter();

316 317 318
			break;

		case PMOPS_FINISH:
319 320 321
			if (data->platform_suspend)
				error = 0;

322 323 324 325 326 327 328 329
			break;

		default:
			printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);

		}
		break;

330
	case SNAPSHOT_SET_SWAP_AREA:
331
		if (swsusp_swap_in_use()) {
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
			error = -EPERM;
		} else {
			struct resume_swap_area swap_area;
			dev_t swdev;

			error = copy_from_user(&swap_area, (void __user *)arg,
					sizeof(struct resume_swap_area));
			if (error) {
				error = -EFAULT;
				break;
			}

			/*
			 * User space encodes device types as two-byte values,
			 * so we need to recode them
			 */
			swdev = old_decode_dev(swap_area.dev);
			if (swdev) {
				offset = swap_area.offset;
351
				data->swap = swap_type_of(swdev, offset, NULL);
352 353 354 355 356 357 358 359 360
				if (data->swap < 0)
					error = -ENODEV;
			} else {
				data->swap = -1;
				error = -EINVAL;
			}
		}
		break;

361 362 363 364 365 366 367 368
	default:
		error = -ENOTTY;

	}

	return error;
}

369
static const struct file_operations snapshot_fops = {
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
	.open = snapshot_open,
	.release = snapshot_release,
	.read = snapshot_read,
	.write = snapshot_write,
	.llseek = no_llseek,
	.ioctl = snapshot_ioctl,
};

static struct miscdevice snapshot_device = {
	.minor = SNAPSHOT_MINOR,
	.name = "snapshot",
	.fops = &snapshot_fops,
};

static int __init snapshot_device_init(void)
{
	return misc_register(&snapshot_device);
};

device_initcall(snapshot_device_init);