1 /* 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> 14 #include <linux/reboot.h> 15 #include <linux/kmod.h> 16 #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> 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 #include <asm/uaccess.h> 31 32 #include "power.h" 33 34 35 #define SNAPSHOT_MINOR 231 36 37 static struct snapshot_data { 38 struct snapshot_handle handle; 39 int swap; 40 int mode; 41 char frozen; 42 char ready; 43 char platform_support; 44 } snapshot_state; 45 46 atomic_t snapshot_device_available = ATOMIC_INIT(1); 47 48 static int snapshot_open(struct inode *inode, struct file *filp) 49 { 50 struct snapshot_data *data; 51 int error; 52 53 lock_system_sleep(); 54 55 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { 56 error = -EBUSY; 57 goto Unlock; 58 } 59 60 if ((filp->f_flags & O_ACCMODE) == O_RDWR) { 61 atomic_inc(&snapshot_device_available); 62 error = -ENOSYS; 63 goto Unlock; 64 } 65 if(create_basic_memory_bitmaps()) { 66 atomic_inc(&snapshot_device_available); 67 error = -ENOMEM; 68 goto Unlock; 69 } 70 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) { 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 * 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 90 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); 95 } 96 if (error) { 97 free_basic_memory_bitmaps(); 98 atomic_inc(&snapshot_device_available); 99 } 100 data->frozen = 0; 101 data->ready = 0; 102 data->platform_support = 0; 103 104 Unlock: 105 unlock_system_sleep(); 106 107 return error; 108 } 109 110 static int snapshot_release(struct inode *inode, struct file *filp) 111 { 112 struct snapshot_data *data; 113 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 if (data->frozen) { 121 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 return 0; 131 } 132 133 static 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; 138 loff_t pg_offp = *offp & ~PAGE_MASK; 139 140 lock_system_sleep(); 141 142 data = filp->private_data; 143 if (!data->ready) { 144 res = -ENODATA; 145 goto Unlock; 146 } 147 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; 153 } 154 155 res = simple_read_from_buffer(buf, count, &pg_offp, 156 data_of(data->handle), res); 157 if (res > 0) 158 *offp += res; 159 160 Unlock: 161 unlock_system_sleep(); 162 163 return res; 164 } 165 166 static 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; 171 loff_t pg_offp = *offp & ~PAGE_MASK; 172 173 lock_system_sleep(); 174 175 data = filp->private_data; 176 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; 183 } 184 185 res = simple_write_to_buffer(data_of(data->handle), res, &pg_offp, 186 buf, count); 187 if (res > 0) 188 *offp += res; 189 unlock: 190 unlock_system_sleep(); 191 192 return res; 193 } 194 195 static long snapshot_ioctl(struct file *filp, unsigned int cmd, 196 unsigned long arg) 197 { 198 int error = 0; 199 struct snapshot_data *data; 200 loff_t size; 201 sector_t offset; 202 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 210 if (!mutex_trylock(&pm_mutex)) 211 return -EBUSY; 212 213 data = filp->private_data; 214 215 switch (cmd) { 216 217 case SNAPSHOT_FREEZE: 218 if (data->frozen) 219 break; 220 221 printk("Syncing filesystems ... "); 222 sys_sync(); 223 printk("done.\n"); 224 225 error = usermodehelper_disable(); 226 if (error) 227 break; 228 229 error = freeze_processes(); 230 if (error) 231 usermodehelper_enable(); 232 else 233 data->frozen = 1; 234 break; 235 236 case SNAPSHOT_UNFREEZE: 237 if (!data->frozen || data->ready) 238 break; 239 pm_restore_gfp_mask(); 240 thaw_processes(); 241 usermodehelper_enable(); 242 data->frozen = 0; 243 break; 244 245 case SNAPSHOT_CREATE_IMAGE: 246 if (data->mode != O_RDONLY || !data->frozen || data->ready) { 247 error = -EPERM; 248 break; 249 } 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 if (!error && !freezer_test_done) 255 data->ready = 1; 256 if (freezer_test_done) { 257 freezer_test_done = false; 258 thaw_processes(); 259 } 260 } 261 break; 262 263 case SNAPSHOT_ATOMIC_RESTORE: 264 snapshot_write_finalize(&data->handle); 265 if (data->mode != O_WRONLY || !data->frozen || 266 !snapshot_image_loaded(&data->handle)) { 267 error = -EPERM; 268 break; 269 } 270 error = hibernation_restore(data->platform_support); 271 break; 272 273 case SNAPSHOT_FREE: 274 swsusp_free(); 275 memset(&data->handle, 0, sizeof(struct snapshot_handle)); 276 data->ready = 0; 277 break; 278 279 case SNAPSHOT_PREF_IMAGE_SIZE: 280 image_size = arg; 281 break; 282 283 case SNAPSHOT_GET_IMAGE_SIZE: 284 if (!data->ready) { 285 error = -ENODATA; 286 break; 287 } 288 size = snapshot_get_image_size(); 289 size <<= PAGE_SHIFT; 290 error = put_user(size, (loff_t __user *)arg); 291 break; 292 293 case SNAPSHOT_AVAIL_SWAP_SIZE: 294 size = count_swap_pages(data->swap, 1); 295 size <<= PAGE_SHIFT; 296 error = put_user(size, (loff_t __user *)arg); 297 break; 298 299 case SNAPSHOT_ALLOC_SWAP_PAGE: 300 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) { 301 error = -ENODEV; 302 break; 303 } 304 offset = alloc_swapdev_block(data->swap); 305 if (offset) { 306 offset <<= PAGE_SHIFT; 307 error = put_user(offset, (loff_t __user *)arg); 308 } else { 309 error = -ENOSPC; 310 } 311 break; 312 313 case SNAPSHOT_FREE_SWAP_PAGES: 314 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) { 315 error = -ENODEV; 316 break; 317 } 318 free_all_swap_pages(data->swap); 319 break; 320 321 case SNAPSHOT_S2RAM: 322 if (!data->frozen) { 323 error = -EPERM; 324 break; 325 } 326 /* 327 * Tasks are frozen and the notifiers have been called with 328 * PM_HIBERNATION_PREPARE 329 */ 330 error = suspend_devices_and_enter(PM_SUSPEND_MEM); 331 data->ready = 0; 332 break; 333 334 case SNAPSHOT_PLATFORM_SUPPORT: 335 data->platform_support = !!arg; 336 break; 337 338 case SNAPSHOT_POWER_OFF: 339 if (data->platform_support) 340 error = hibernation_platform_enter(); 341 break; 342 343 case SNAPSHOT_SET_SWAP_AREA: 344 if (swsusp_swap_in_use()) { 345 error = -EPERM; 346 } else { 347 struct resume_swap_area swap_area; 348 dev_t swdev; 349 350 error = copy_from_user(&swap_area, (void __user *)arg, 351 sizeof(struct resume_swap_area)); 352 if (error) { 353 error = -EFAULT; 354 break; 355 } 356 357 /* 358 * User space encodes device types as two-byte values, 359 * so we need to recode them 360 */ 361 swdev = new_decode_dev(swap_area.dev); 362 if (swdev) { 363 offset = swap_area.offset; 364 data->swap = swap_type_of(swdev, offset, NULL); 365 if (data->swap < 0) 366 error = -ENODEV; 367 } else { 368 data->swap = -1; 369 error = -EINVAL; 370 } 371 } 372 break; 373 374 default: 375 error = -ENOTTY; 376 377 } 378 379 mutex_unlock(&pm_mutex); 380 381 return error; 382 } 383 384 #ifdef CONFIG_COMPAT 385 386 struct compat_resume_swap_area { 387 compat_loff_t offset; 388 u32 dev; 389 } __packed; 390 391 static long 392 snapshot_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 393 { 394 BUILD_BUG_ON(sizeof(loff_t) != sizeof(compat_loff_t)); 395 396 switch (cmd) { 397 case SNAPSHOT_GET_IMAGE_SIZE: 398 case SNAPSHOT_AVAIL_SWAP_SIZE: 399 case SNAPSHOT_ALLOC_SWAP_PAGE: { 400 compat_loff_t __user *uoffset = compat_ptr(arg); 401 loff_t offset; 402 mm_segment_t old_fs; 403 int err; 404 405 old_fs = get_fs(); 406 set_fs(KERNEL_DS); 407 err = snapshot_ioctl(file, cmd, (unsigned long) &offset); 408 set_fs(old_fs); 409 if (!err && put_user(offset, uoffset)) 410 err = -EFAULT; 411 return err; 412 } 413 414 case SNAPSHOT_CREATE_IMAGE: 415 return snapshot_ioctl(file, cmd, 416 (unsigned long) compat_ptr(arg)); 417 418 case SNAPSHOT_SET_SWAP_AREA: { 419 struct compat_resume_swap_area __user *u_swap_area = 420 compat_ptr(arg); 421 struct resume_swap_area swap_area; 422 mm_segment_t old_fs; 423 int err; 424 425 err = get_user(swap_area.offset, &u_swap_area->offset); 426 err |= get_user(swap_area.dev, &u_swap_area->dev); 427 if (err) 428 return -EFAULT; 429 old_fs = get_fs(); 430 set_fs(KERNEL_DS); 431 err = snapshot_ioctl(file, SNAPSHOT_SET_SWAP_AREA, 432 (unsigned long) &swap_area); 433 set_fs(old_fs); 434 return err; 435 } 436 437 default: 438 return snapshot_ioctl(file, cmd, arg); 439 } 440 } 441 442 #endif /* CONFIG_COMPAT */ 443 444 static const struct file_operations snapshot_fops = { 445 .open = snapshot_open, 446 .release = snapshot_release, 447 .read = snapshot_read, 448 .write = snapshot_write, 449 .llseek = no_llseek, 450 .unlocked_ioctl = snapshot_ioctl, 451 #ifdef CONFIG_COMPAT 452 .compat_ioctl = snapshot_compat_ioctl, 453 #endif 454 }; 455 456 static struct miscdevice snapshot_device = { 457 .minor = SNAPSHOT_MINOR, 458 .name = "snapshot", 459 .fops = &snapshot_fops, 460 }; 461 462 static int __init snapshot_device_init(void) 463 { 464 return misc_register(&snapshot_device); 465 }; 466 467 device_initcall(snapshot_device_init); 468