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
#include <linux/kmod.h>
16 17 18 19 20 21 22 23
#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>
24
#include <linux/compat.h>
25
#include <linux/console.h>
26
#include <linux/cpu.h>
27
#include <linux/freezer.h>
28
#include <scsi/scsi_scan.h>
29 30 31 32 33

#include <asm/uaccess.h>

#include "power.h"

34

35 36 37 38 39 40 41 42
#define SNAPSHOT_MINOR	231

static struct snapshot_data {
	struct snapshot_handle handle;
	int swap;
	int mode;
	char frozen;
	char ready;
43
	char platform_support;
44 45
} snapshot_state;

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

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

53
	lock_system_sleep();
54 55 56 57 58

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

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

90 91
		data->swap = -1;
		data->mode = O_WRONLY;
92
		error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
93
		if (error)
94
			pm_notifier_call_chain(PM_POST_RESTORE);
95
	}
96 97
	if (error) {
		free_basic_memory_bitmaps();
98
		atomic_inc(&snapshot_device_available);
99
	}
100 101
	data->frozen = 0;
	data->ready = 0;
102
	data->platform_support = 0;
103

104
 Unlock:
105
	unlock_system_sleep();
106 107

	return error;
108 109 110 111 112 113
}

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

114
	lock_system_sleep();
115

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

128
	unlock_system_sleep();
129

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

140
	lock_system_sleep();
141

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

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

160
 Unlock:
161
	unlock_system_sleep();
162

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

173
	lock_system_sleep();
174

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

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

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

192 193 194
	return res;
}

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

	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;

210 211
	if (!mutex_trylock(&pm_mutex))
		return -EBUSY;
212

213
	data = filp->private_data;
214

215 216 217 218 219
	switch (cmd) {

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

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

225
		error = usermodehelper_disable();
226
		if (error)
227 228 229
			break;

		error = freeze_processes();
230
		if (error)
231
			usermodehelper_enable();
232
		else
233 234 235 236
			data->frozen = 1;
		break;

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

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

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

	case SNAPSHOT_FREE:
		swsusp_free();
		memset(&data->handle, 0, sizeof(struct snapshot_handle));
		data->ready = 0;
277 278 279 280 281 282 283 284 285
		/*
		 * 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();
286 287
		break;

288
	case SNAPSHOT_PREF_IMAGE_SIZE:
289 290 291
		image_size = arg;
		break;

292 293 294 295 296 297 298 299 300 301
	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;

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

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

	case SNAPSHOT_FREE_SWAP_PAGES:
		if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
			error = -ENODEV;
			break;
		}
327
		free_all_swap_pages(data->swap);
328 329
		break;

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

343 344 345 346 347 348 349 350 351
	case SNAPSHOT_PLATFORM_SUPPORT:
		data->platform_support = !!arg;
		break;

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

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

383 384 385 386
	default:
		error = -ENOTTY;

	}
387 388 389

	mutex_unlock(&pm_mutex);

390 391 392
	return error;
}

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 451 452
#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 */

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

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