user.c 10.3 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
#include <scsi/scsi_scan.h>
27 28 29 30 31

#include <asm/uaccess.h>

#include "power.h"

32
/*
33 34 35
 * NOTE: The SNAPSHOT_SET_SWAP_FILE and SNAPSHOT_PMOPS ioctls are obsolete and
 * will be removed in the future.  They are only preserved here for
 * compatibility with existing userland utilities.
36
 */
37
#define SNAPSHOT_SET_SWAP_FILE	_IOW(SNAPSHOT_IOC_MAGIC, 10, unsigned int)
38 39 40 41 42 43
#define SNAPSHOT_PMOPS		_IOW(SNAPSHOT_IOC_MAGIC, 12, unsigned int)

#define PMOPS_PREPARE	1
#define PMOPS_ENTER	2
#define PMOPS_FINISH	3

44 45 46 47 48 49 50 51 52 53
/*
 * NOTE: The following ioctl definitions are wrong and have been replaced with
 * correct ones.  They are only preserved here for compatibility with existing
 * userland utilities and will be removed in the future.
 */
#define SNAPSHOT_ATOMIC_SNAPSHOT	_IOW(SNAPSHOT_IOC_MAGIC, 3, void *)
#define SNAPSHOT_SET_IMAGE_SIZE		_IOW(SNAPSHOT_IOC_MAGIC, 6, unsigned long)
#define SNAPSHOT_AVAIL_SWAP		_IOR(SNAPSHOT_IOC_MAGIC, 7, void *)
#define SNAPSHOT_GET_SWAP_PAGE		_IOR(SNAPSHOT_IOC_MAGIC, 8, void *)

54

55 56 57 58 59 60 61 62
#define SNAPSHOT_MINOR	231

static struct snapshot_data {
	struct snapshot_handle handle;
	int swap;
	int mode;
	char frozen;
	char ready;
63
	char platform_support;
64 65
} snapshot_state;

66
atomic_t snapshot_device_available = ATOMIC_INIT(1);
67 68 69 70

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

73 74 75 76 77 78
	mutex_lock(&pm_mutex);

	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
		error = -EBUSY;
		goto Unlock;
	}
79

80
	if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
81
		atomic_inc(&snapshot_device_available);
82 83
		error = -ENOSYS;
		goto Unlock;
84 85
	}
	if(create_basic_memory_bitmaps()) {
86
		atomic_inc(&snapshot_device_available);
87 88
		error = -ENOMEM;
		goto Unlock;
89
	}
90 91 92 93 94
	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) {
95
		/* Hibernating.  The image device should be accessible. */
96
		data->swap = swsusp_resume_device ?
97
			swap_type_of(swsusp_resume_device, 0, NULL) : -1;
98
		data->mode = O_RDONLY;
99
		error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
100
		if (error)
101
			pm_notifier_call_chain(PM_POST_HIBERNATION);
102
	} else {
103 104 105 106 107 108 109
		/*
		 * Resuming.  We may need to wait for the image device to
		 * appear.
		 */
		wait_for_device_probe();
		scsi_complete_async_scans();

110 111
		data->swap = -1;
		data->mode = O_WRONLY;
112
		error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
113
		if (error)
114
			pm_notifier_call_chain(PM_POST_RESTORE);
115
	}
116
	if (error)
117
		atomic_inc(&snapshot_device_available);
118 119
	data->frozen = 0;
	data->ready = 0;
120
	data->platform_support = 0;
121

122 123 124 125
 Unlock:
	mutex_unlock(&pm_mutex);

	return error;
126 127 128 129 130 131
}

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

132 133
	mutex_lock(&pm_mutex);

134
	swsusp_free();
135
	free_basic_memory_bitmaps();
136
	data = filp->private_data;
137
	free_all_swap_pages(data->swap);
138 139
	if (data->frozen) {
		pm_restore_gfp_mask();
140
		thaw_processes();
141
	}
142
	pm_notifier_call_chain(data->mode == O_RDONLY ?
143
			PM_POST_HIBERNATION : PM_POST_RESTORE);
144
	atomic_inc(&snapshot_device_available);
145 146 147

	mutex_unlock(&pm_mutex);

148 149 150 151 152 153 154 155
	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;
J
Jiri Slaby 已提交
156
	loff_t pg_offp = *offp & ~PAGE_MASK;
157

158 159
	mutex_lock(&pm_mutex);

160
	data = filp->private_data;
161 162 163 164
	if (!data->ready) {
		res = -ENODATA;
		goto Unlock;
	}
J
Jiri Slaby 已提交
165 166 167 168 169 170
	if (!pg_offp) { /* on page boundary? */
		res = snapshot_read_next(&data->handle);
		if (res <= 0)
			goto Unlock;
	} else {
		res = PAGE_SIZE - pg_offp;
171
	}
172

J
Jiri Slaby 已提交
173 174 175 176 177
	res = simple_read_from_buffer(buf, count, &pg_offp,
			data_of(data->handle), res);
	if (res > 0)
		*offp += res;

178 179 180
 Unlock:
	mutex_unlock(&pm_mutex);

181 182 183 184 185 186 187 188
	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;
J
Jiri Slaby 已提交
189
	loff_t pg_offp = *offp & ~PAGE_MASK;
190

191 192
	mutex_lock(&pm_mutex);

193
	data = filp->private_data;
J
Jiri Slaby 已提交
194 195 196 197 198 199 200

	if (!pg_offp) {
		res = snapshot_write_next(&data->handle);
		if (res <= 0)
			goto unlock;
	} else {
		res = PAGE_SIZE - pg_offp;
201
	}
202

J
Jiri Slaby 已提交
203 204 205 206 207
	res = simple_write_to_buffer(data_of(data->handle), res, &pg_offp,
			buf, count);
	if (res > 0)
		*offp += res;
unlock:
208 209
	mutex_unlock(&pm_mutex);

210 211 212
	return res;
}

213 214 215 216 217 218 219 220 221
static void snapshot_deprecated_ioctl(unsigned int cmd)
{
	if (printk_ratelimit())
		printk(KERN_NOTICE "%pf: ioctl '%.8x' is deprecated and will "
				"be removed soon, update your suspend-to-disk "
				"utilities\n",
				__builtin_return_address(0), cmd);
}

222 223
static long snapshot_ioctl(struct file *filp, unsigned int cmd,
							unsigned long arg)
224 225 226
{
	int error = 0;
	struct snapshot_data *data;
227
	loff_t size;
228
	sector_t offset;
229 230 231 232 233 234 235 236

	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;

237 238
	if (!mutex_trylock(&pm_mutex))
		return -EBUSY;
239

240
	data = filp->private_data;
241

242 243 244 245 246
	switch (cmd) {

	case SNAPSHOT_FREEZE:
		if (data->frozen)
			break;
247

248 249 250 251
		printk("Syncing filesystems ... ");
		sys_sync();
		printk("done.\n");

252
		error = usermodehelper_disable();
253
		if (error)
254 255 256 257
			break;

		error = freeze_processes();
		if (error) {
258
			thaw_processes();
259 260
			usermodehelper_enable();
		}
261 262 263 264 265
		if (!error)
			data->frozen = 1;
		break;

	case SNAPSHOT_UNFREEZE:
266
		if (!data->frozen || data->ready)
267
			break;
268
		pm_restore_gfp_mask();
269
		thaw_processes();
270
		usermodehelper_enable();
271 272 273 274
		data->frozen = 0;
		break;

	case SNAPSHOT_ATOMIC_SNAPSHOT:
275 276
		snapshot_deprecated_ioctl(cmd);
	case SNAPSHOT_CREATE_IMAGE:
277 278 279 280
		if (data->mode != O_RDONLY || !data->frozen  || data->ready) {
			error = -EPERM;
			break;
		}
281
		pm_restore_gfp_mask();
282
		error = hibernation_snapshot(data->platform_support);
283
		if (!error)
284
			error = put_user(in_suspend, (int __user *)arg);
285 286 287 288 289
		if (!error)
			data->ready = 1;
		break;

	case SNAPSHOT_ATOMIC_RESTORE:
290
		snapshot_write_finalize(&data->handle);
291 292 293 294 295
		if (data->mode != O_WRONLY || !data->frozen ||
		    !snapshot_image_loaded(&data->handle)) {
			error = -EPERM;
			break;
		}
296
		error = hibernation_restore(data->platform_support);
297 298 299 300 301 302 303 304 305
		break;

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

	case SNAPSHOT_SET_IMAGE_SIZE:
306 307
		snapshot_deprecated_ioctl(cmd);
	case SNAPSHOT_PREF_IMAGE_SIZE:
308 309 310
		image_size = arg;
		break;

311 312 313 314 315 316 317 318 319 320
	case SNAPSHOT_GET_IMAGE_SIZE:
		if (!data->ready) {
			error = -ENODATA;
			break;
		}
		size = snapshot_get_image_size();
		size <<= PAGE_SHIFT;
		error = put_user(size, (loff_t __user *)arg);
		break;

321
	case SNAPSHOT_AVAIL_SWAP:
322 323
		snapshot_deprecated_ioctl(cmd);
	case SNAPSHOT_AVAIL_SWAP_SIZE:
324 325 326
		size = count_swap_pages(data->swap, 1);
		size <<= PAGE_SHIFT;
		error = put_user(size, (loff_t __user *)arg);
327 328 329
		break;

	case SNAPSHOT_GET_SWAP_PAGE:
330 331
		snapshot_deprecated_ioctl(cmd);
	case SNAPSHOT_ALLOC_SWAP_PAGE:
332 333 334 335
		if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
			error = -ENODEV;
			break;
		}
336
		offset = alloc_swapdev_block(data->swap);
337 338
		if (offset) {
			offset <<= PAGE_SHIFT;
339
			error = put_user(offset, (loff_t __user *)arg);
340 341 342 343 344 345 346 347 348 349
		} else {
			error = -ENOSPC;
		}
		break;

	case SNAPSHOT_FREE_SWAP_PAGES:
		if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
			error = -ENODEV;
			break;
		}
350
		free_all_swap_pages(data->swap);
351 352
		break;

353
	case SNAPSHOT_SET_SWAP_FILE: /* This ioctl is deprecated */
354
		snapshot_deprecated_ioctl(cmd);
355
		if (!swsusp_swap_in_use()) {
356 357 358 359 360
			/*
			 * User space encodes device types as two-byte values,
			 * so we need to recode them
			 */
			if (old_decode_dev(arg)) {
361 362
				data->swap = swap_type_of(old_decode_dev(arg),
							0, NULL);
363 364 365 366 367 368 369 370 371 372 373
				if (data->swap < 0)
					error = -ENODEV;
			} else {
				data->swap = -1;
				error = -EINVAL;
			}
		} else {
			error = -EPERM;
		}
		break;

374 375 376 377 378
	case SNAPSHOT_S2RAM:
		if (!data->frozen) {
			error = -EPERM;
			break;
		}
379 380 381 382 383
		/*
		 * Tasks are frozen and the notifiers have been called with
		 * PM_HIBERNATION_PREPARE
		 */
		error = suspend_devices_and_enter(PM_SUSPEND_MEM);
384 385
		break;

386 387 388 389 390 391 392 393 394 395
	case SNAPSHOT_PLATFORM_SUPPORT:
		data->platform_support = !!arg;
		break;

	case SNAPSHOT_POWER_OFF:
		if (data->platform_support)
			error = hibernation_platform_enter();
		break;

	case SNAPSHOT_PMOPS: /* This ioctl is deprecated */
396
		snapshot_deprecated_ioctl(cmd);
397 398
		error = -EINVAL;

399 400 401
		switch (arg) {

		case PMOPS_PREPARE:
402
			data->platform_support = 1;
403
			error = 0;
404 405 406
			break;

		case PMOPS_ENTER:
407
			if (data->platform_support)
408
				error = hibernation_platform_enter();
409 410 411
			break;

		case PMOPS_FINISH:
412
			if (data->platform_support)
413
				error = 0;
414 415 416 417 418 419 420 421
			break;

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

		}
		break;

422
	case SNAPSHOT_SET_SWAP_AREA:
423
		if (swsusp_swap_in_use()) {
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
			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
			 */
440
			swdev = new_decode_dev(swap_area.dev);
441 442
			if (swdev) {
				offset = swap_area.offset;
443
				data->swap = swap_type_of(swdev, offset, NULL);
444 445 446 447 448 449 450 451 452
				if (data->swap < 0)
					error = -ENODEV;
			} else {
				data->swap = -1;
				error = -EINVAL;
			}
		}
		break;

453 454 455 456
	default:
		error = -ENOTTY;

	}
457 458 459

	mutex_unlock(&pm_mutex);

460 461 462
	return error;
}

463
static const struct file_operations snapshot_fops = {
464 465 466 467 468
	.open = snapshot_open,
	.release = snapshot_release,
	.read = snapshot_read,
	.write = snapshot_write,
	.llseek = no_llseek,
469
	.unlocked_ioctl = snapshot_ioctl,
470 471 472 473 474 475 476 477 478 479 480 481 482 483
};

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);