user.c 9.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/compat.h>
24
#include <linux/console.h>
25
#include <linux/cpu.h>
26
#include <linux/freezer.h>
27 28 29 30 31

#include <asm/uaccess.h>

#include "power.h"

32

33 34 35 36 37 38 39 40
#define SNAPSHOT_MINOR	231

static struct snapshot_data {
	struct snapshot_handle handle;
	int swap;
	int mode;
	char frozen;
	char ready;
41
	char platform_support;
42
	bool free_bitmaps;
43 44
} snapshot_state;

45
atomic_t snapshot_device_available = ATOMIC_INIT(1);
46 47 48 49

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

52
	lock_system_sleep();
53 54 55 56 57

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

59
	if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
60
		atomic_inc(&snapshot_device_available);
61 62
		error = -ENOSYS;
		goto Unlock;
63
	}
64 65 66 67 68
	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) {
69
		/* Hibernating.  The image device should be accessible. */
70
		data->swap = swsusp_resume_device ?
71
			swap_type_of(swsusp_resume_device, 0, NULL) : -1;
72
		data->mode = O_RDONLY;
73
		error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
74
		if (error)
75
			pm_notifier_call_chain(PM_POST_HIBERNATION);
76
	} else {
77 78 79 80 81 82
		/*
		 * Resuming.  We may need to wait for the image device to
		 * appear.
		 */
		wait_for_device_probe();

83 84
		data->swap = -1;
		data->mode = O_WRONLY;
85
		error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
86 87 88 89
		if (!error) {
			error = create_basic_memory_bitmaps();
			data->free_bitmaps = !error;
		}
90
		if (error)
91
			pm_notifier_call_chain(PM_POST_RESTORE);
92
	}
93
	if (error)
94
		atomic_inc(&snapshot_device_available);
95

96 97
	data->frozen = 0;
	data->ready = 0;
98
	data->platform_support = 0;
99

100
 Unlock:
101
	unlock_system_sleep();
102 103

	return error;
104 105 106 107 108 109
}

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

110
	lock_system_sleep();
111

112 113
	swsusp_free();
	data = filp->private_data;
114
	free_all_swap_pages(data->swap);
115 116
	if (data->frozen) {
		pm_restore_gfp_mask();
117
		free_basic_memory_bitmaps();
118
		thaw_processes();
119 120
	} else if (data->free_bitmaps) {
		free_basic_memory_bitmaps();
121
	}
122
	pm_notifier_call_chain(data->mode == O_RDONLY ?
123
			PM_POST_HIBERNATION : PM_POST_RESTORE);
124
	atomic_inc(&snapshot_device_available);
125

126
	unlock_system_sleep();
127

128 129 130 131 132 133 134 135
	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 已提交
136
	loff_t pg_offp = *offp & ~PAGE_MASK;
137

138
	lock_system_sleep();
139

140
	data = filp->private_data;
141 142 143 144
	if (!data->ready) {
		res = -ENODATA;
		goto Unlock;
	}
J
Jiri Slaby 已提交
145 146 147 148 149 150
	if (!pg_offp) { /* on page boundary? */
		res = snapshot_read_next(&data->handle);
		if (res <= 0)
			goto Unlock;
	} else {
		res = PAGE_SIZE - pg_offp;
151
	}
152

J
Jiri Slaby 已提交
153 154 155 156 157
	res = simple_read_from_buffer(buf, count, &pg_offp,
			data_of(data->handle), res);
	if (res > 0)
		*offp += res;

158
 Unlock:
159
	unlock_system_sleep();
160

161 162 163 164 165 166 167 168
	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 已提交
169
	loff_t pg_offp = *offp & ~PAGE_MASK;
170

171
	lock_system_sleep();
172

173
	data = filp->private_data;
J
Jiri Slaby 已提交
174 175 176 177 178 179 180

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

J
Jiri Slaby 已提交
183 184 185 186 187
	res = simple_write_to_buffer(data_of(data->handle), res, &pg_offp,
			buf, count);
	if (res > 0)
		*offp += res;
unlock:
188
	unlock_system_sleep();
189

190 191 192
	return res;
}

193 194
static long snapshot_ioctl(struct file *filp, unsigned int cmd,
							unsigned long arg)
195 196 197
{
	int error = 0;
	struct snapshot_data *data;
198
	loff_t size;
199
	sector_t offset;
200 201 202 203 204 205 206 207

	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;

208 209
	if (!mutex_trylock(&pm_mutex))
		return -EBUSY;
210

211
	lock_device_hotplug();
212
	data = filp->private_data;
213

214 215 216 217 218
	switch (cmd) {

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

220 221 222 223
		printk("Syncing filesystems ... ");
		sys_sync();
		printk("done.\n");

224
		error = freeze_processes();
225 226 227 228 229 230 231
		if (error)
			break;

		error = create_basic_memory_bitmaps();
		if (error)
			thaw_processes();
		else
232
			data->frozen = 1;
233

234 235 236
		break;

	case SNAPSHOT_UNFREEZE:
237
		if (!data->frozen || data->ready)
238
			break;
239
		pm_restore_gfp_mask();
240
		free_basic_memory_bitmaps();
241
		data->free_bitmaps = false;
242 243 244 245
		thaw_processes();
		data->frozen = 0;
		break;

246
	case SNAPSHOT_CREATE_IMAGE:
247 248 249 250
		if (data->mode != O_RDONLY || !data->frozen  || data->ready) {
			error = -EPERM;
			break;
		}
251
		pm_restore_gfp_mask();
252
		error = hibernation_snapshot(data->platform_support);
253
		if (!error) {
254
			error = put_user(in_suspend, (int __user *)arg);
255 256
			data->ready = !freezer_test_done && !error;
			freezer_test_done = false;
257
		}
258 259 260
		break;

	case SNAPSHOT_ATOMIC_RESTORE:
261
		snapshot_write_finalize(&data->handle);
262 263 264 265 266
		if (data->mode != O_WRONLY || !data->frozen ||
		    !snapshot_image_loaded(&data->handle)) {
			error = -EPERM;
			break;
		}
267
		error = hibernation_restore(data->platform_support);
268 269 270 271 272 273
		break;

	case SNAPSHOT_FREE:
		swsusp_free();
		memset(&data->handle, 0, sizeof(struct snapshot_handle));
		data->ready = 0;
274 275 276 277 278 279 280 281 282
		/*
		 * It is necessary to thaw kernel threads here, because
		 * SNAPSHOT_CREATE_IMAGE may be invoked directly after
		 * SNAPSHOT_FREE.  In that case, if kernel threads were not
		 * thawed, the preallocation of memory carried out by
		 * hibernation_snapshot() might run into problems (i.e. it
		 * might fail or even deadlock).
		 */
		thaw_kernel_threads();
283 284
		break;

285
	case SNAPSHOT_PREF_IMAGE_SIZE:
286 287 288
		image_size = arg;
		break;

289 290 291 292 293 294 295 296 297 298
	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;

299
	case SNAPSHOT_AVAIL_SWAP_SIZE:
300 301 302
		size = count_swap_pages(data->swap, 1);
		size <<= PAGE_SHIFT;
		error = put_user(size, (loff_t __user *)arg);
303 304
		break;

305
	case SNAPSHOT_ALLOC_SWAP_PAGE:
306 307 308 309
		if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
			error = -ENODEV;
			break;
		}
310
		offset = alloc_swapdev_block(data->swap);
311 312
		if (offset) {
			offset <<= PAGE_SHIFT;
313
			error = put_user(offset, (loff_t __user *)arg);
314 315 316 317 318 319 320 321 322 323
		} else {
			error = -ENOSPC;
		}
		break;

	case SNAPSHOT_FREE_SWAP_PAGES:
		if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
			error = -ENODEV;
			break;
		}
324
		free_all_swap_pages(data->swap);
325 326
		break;

327 328 329 330 331
	case SNAPSHOT_S2RAM:
		if (!data->frozen) {
			error = -EPERM;
			break;
		}
332 333 334 335 336
		/*
		 * Tasks are frozen and the notifiers have been called with
		 * PM_HIBERNATION_PREPARE
		 */
		error = suspend_devices_and_enter(PM_SUSPEND_MEM);
337
		data->ready = 0;
338 339
		break;

340 341 342 343 344 345 346 347 348
	case SNAPSHOT_PLATFORM_SUPPORT:
		data->platform_support = !!arg;
		break;

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

349
	case SNAPSHOT_SET_SWAP_AREA:
350
		if (swsusp_swap_in_use()) {
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
			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
			 */
367
			swdev = new_decode_dev(swap_area.dev);
368 369
			if (swdev) {
				offset = swap_area.offset;
370
				data->swap = swap_type_of(swdev, offset, NULL);
371 372 373 374 375 376 377 378 379
				if (data->swap < 0)
					error = -ENODEV;
			} else {
				data->swap = -1;
				error = -EINVAL;
			}
		}
		break;

380 381 382 383
	default:
		error = -ENOTTY;

	}
384

385
	unlock_device_hotplug();
386 387
	mutex_unlock(&pm_mutex);

388 389 390
	return error;
}

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
#ifdef CONFIG_COMPAT

struct compat_resume_swap_area {
	compat_loff_t offset;
	u32 dev;
} __packed;

static long
snapshot_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	BUILD_BUG_ON(sizeof(loff_t) != sizeof(compat_loff_t));

	switch (cmd) {
	case SNAPSHOT_GET_IMAGE_SIZE:
	case SNAPSHOT_AVAIL_SWAP_SIZE:
	case SNAPSHOT_ALLOC_SWAP_PAGE: {
		compat_loff_t __user *uoffset = compat_ptr(arg);
		loff_t offset;
		mm_segment_t old_fs;
		int err;

		old_fs = get_fs();
		set_fs(KERNEL_DS);
		err = snapshot_ioctl(file, cmd, (unsigned long) &offset);
		set_fs(old_fs);
		if (!err && put_user(offset, uoffset))
			err = -EFAULT;
		return err;
	}

	case SNAPSHOT_CREATE_IMAGE:
		return snapshot_ioctl(file, cmd,
				      (unsigned long) compat_ptr(arg));

	case SNAPSHOT_SET_SWAP_AREA: {
		struct compat_resume_swap_area __user *u_swap_area =
			compat_ptr(arg);
		struct resume_swap_area swap_area;
		mm_segment_t old_fs;
		int err;

		err = get_user(swap_area.offset, &u_swap_area->offset);
		err |= get_user(swap_area.dev, &u_swap_area->dev);
		if (err)
			return -EFAULT;
		old_fs = get_fs();
		set_fs(KERNEL_DS);
		err = snapshot_ioctl(file, SNAPSHOT_SET_SWAP_AREA,
				     (unsigned long) &swap_area);
		set_fs(old_fs);
		return err;
	}

	default:
		return snapshot_ioctl(file, cmd, arg);
	}
}

#endif /* CONFIG_COMPAT */

451
static const struct file_operations snapshot_fops = {
452 453 454 455 456
	.open = snapshot_open,
	.release = snapshot_release,
	.read = snapshot_read,
	.write = snapshot_write,
	.llseek = no_llseek,
457
	.unlocked_ioctl = snapshot_ioctl,
458 459 460
#ifdef CONFIG_COMPAT
	.compat_ioctl = snapshot_compat_ioctl,
#endif
461 462 463 464 465 466 467 468 469 470 471 472 473 474
};

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