user.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. #include <linux/suspend.h>
  12. #include <linux/syscalls.h>
  13. #include <linux/reboot.h>
  14. #include <linux/string.h>
  15. #include <linux/device.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/mm.h>
  18. #include <linux/swap.h>
  19. #include <linux/swapops.h>
  20. #include <linux/pm.h>
  21. #include <linux/fs.h>
  22. #include <linux/console.h>
  23. #include <linux/cpu.h>
  24. #include <linux/freezer.h>
  25. #include <asm/uaccess.h>
  26. #include "power.h"
  27. #define SNAPSHOT_MINOR 231
  28. static struct snapshot_data {
  29. struct snapshot_handle handle;
  30. int swap;
  31. struct bitmap_page *bitmap;
  32. int mode;
  33. char frozen;
  34. char ready;
  35. } snapshot_state;
  36. static atomic_t device_available = ATOMIC_INIT(1);
  37. static int snapshot_open(struct inode *inode, struct file *filp)
  38. {
  39. struct snapshot_data *data;
  40. if (!atomic_add_unless(&device_available, -1, 0))
  41. return -EBUSY;
  42. if ((filp->f_flags & O_ACCMODE) == O_RDWR)
  43. return -ENOSYS;
  44. nonseekable_open(inode, filp);
  45. data = &snapshot_state;
  46. filp->private_data = data;
  47. memset(&data->handle, 0, sizeof(struct snapshot_handle));
  48. if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
  49. data->swap = swsusp_resume_device ?
  50. swap_type_of(swsusp_resume_device, 0, NULL) : -1;
  51. data->mode = O_RDONLY;
  52. } else {
  53. data->swap = -1;
  54. data->mode = O_WRONLY;
  55. }
  56. data->bitmap = NULL;
  57. data->frozen = 0;
  58. data->ready = 0;
  59. return 0;
  60. }
  61. static int snapshot_release(struct inode *inode, struct file *filp)
  62. {
  63. struct snapshot_data *data;
  64. swsusp_free();
  65. data = filp->private_data;
  66. free_all_swap_pages(data->swap, data->bitmap);
  67. free_bitmap(data->bitmap);
  68. if (data->frozen) {
  69. mutex_lock(&pm_mutex);
  70. thaw_processes();
  71. enable_nonboot_cpus();
  72. mutex_unlock(&pm_mutex);
  73. }
  74. atomic_inc(&device_available);
  75. return 0;
  76. }
  77. static ssize_t snapshot_read(struct file *filp, char __user *buf,
  78. size_t count, loff_t *offp)
  79. {
  80. struct snapshot_data *data;
  81. ssize_t res;
  82. data = filp->private_data;
  83. res = snapshot_read_next(&data->handle, count);
  84. if (res > 0) {
  85. if (copy_to_user(buf, data_of(data->handle), res))
  86. res = -EFAULT;
  87. else
  88. *offp = data->handle.offset;
  89. }
  90. return res;
  91. }
  92. static ssize_t snapshot_write(struct file *filp, const char __user *buf,
  93. size_t count, loff_t *offp)
  94. {
  95. struct snapshot_data *data;
  96. ssize_t res;
  97. data = filp->private_data;
  98. res = snapshot_write_next(&data->handle, count);
  99. if (res > 0) {
  100. if (copy_from_user(data_of(data->handle), buf, res))
  101. res = -EFAULT;
  102. else
  103. *offp = data->handle.offset;
  104. }
  105. return res;
  106. }
  107. static int snapshot_ioctl(struct inode *inode, struct file *filp,
  108. unsigned int cmd, unsigned long arg)
  109. {
  110. int error = 0;
  111. struct snapshot_data *data;
  112. loff_t avail;
  113. sector_t offset;
  114. if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
  115. return -ENOTTY;
  116. if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
  117. return -ENOTTY;
  118. if (!capable(CAP_SYS_ADMIN))
  119. return -EPERM;
  120. data = filp->private_data;
  121. switch (cmd) {
  122. case SNAPSHOT_FREEZE:
  123. if (data->frozen)
  124. break;
  125. mutex_lock(&pm_mutex);
  126. error = disable_nonboot_cpus();
  127. if (!error) {
  128. error = freeze_processes();
  129. if (error) {
  130. thaw_processes();
  131. enable_nonboot_cpus();
  132. error = -EBUSY;
  133. }
  134. }
  135. mutex_unlock(&pm_mutex);
  136. if (!error)
  137. data->frozen = 1;
  138. break;
  139. case SNAPSHOT_UNFREEZE:
  140. if (!data->frozen)
  141. break;
  142. mutex_lock(&pm_mutex);
  143. thaw_processes();
  144. enable_nonboot_cpus();
  145. mutex_unlock(&pm_mutex);
  146. data->frozen = 0;
  147. break;
  148. case SNAPSHOT_ATOMIC_SNAPSHOT:
  149. if (data->mode != O_RDONLY || !data->frozen || data->ready) {
  150. error = -EPERM;
  151. break;
  152. }
  153. mutex_lock(&pm_mutex);
  154. /* Free memory before shutting down devices. */
  155. error = swsusp_shrink_memory();
  156. if (!error) {
  157. suspend_console();
  158. error = device_suspend(PMSG_FREEZE);
  159. if (!error) {
  160. in_suspend = 1;
  161. error = swsusp_suspend();
  162. device_resume();
  163. }
  164. resume_console();
  165. }
  166. mutex_unlock(&pm_mutex);
  167. if (!error)
  168. error = put_user(in_suspend, (unsigned int __user *)arg);
  169. if (!error)
  170. data->ready = 1;
  171. break;
  172. case SNAPSHOT_ATOMIC_RESTORE:
  173. snapshot_write_finalize(&data->handle);
  174. if (data->mode != O_WRONLY || !data->frozen ||
  175. !snapshot_image_loaded(&data->handle)) {
  176. error = -EPERM;
  177. break;
  178. }
  179. mutex_lock(&pm_mutex);
  180. pm_prepare_console();
  181. suspend_console();
  182. error = device_suspend(PMSG_PRETHAW);
  183. if (!error) {
  184. error = swsusp_resume();
  185. device_resume();
  186. }
  187. resume_console();
  188. pm_restore_console();
  189. mutex_unlock(&pm_mutex);
  190. break;
  191. case SNAPSHOT_FREE:
  192. swsusp_free();
  193. memset(&data->handle, 0, sizeof(struct snapshot_handle));
  194. data->ready = 0;
  195. break;
  196. case SNAPSHOT_SET_IMAGE_SIZE:
  197. image_size = arg;
  198. break;
  199. case SNAPSHOT_AVAIL_SWAP:
  200. avail = count_swap_pages(data->swap, 1);
  201. avail <<= PAGE_SHIFT;
  202. error = put_user(avail, (loff_t __user *)arg);
  203. break;
  204. case SNAPSHOT_GET_SWAP_PAGE:
  205. if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
  206. error = -ENODEV;
  207. break;
  208. }
  209. if (!data->bitmap) {
  210. data->bitmap = alloc_bitmap(count_swap_pages(data->swap, 0));
  211. if (!data->bitmap) {
  212. error = -ENOMEM;
  213. break;
  214. }
  215. }
  216. offset = alloc_swapdev_block(data->swap, data->bitmap);
  217. if (offset) {
  218. offset <<= PAGE_SHIFT;
  219. error = put_user(offset, (sector_t __user *)arg);
  220. } else {
  221. error = -ENOSPC;
  222. }
  223. break;
  224. case SNAPSHOT_FREE_SWAP_PAGES:
  225. if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
  226. error = -ENODEV;
  227. break;
  228. }
  229. free_all_swap_pages(data->swap, data->bitmap);
  230. free_bitmap(data->bitmap);
  231. data->bitmap = NULL;
  232. break;
  233. case SNAPSHOT_SET_SWAP_FILE:
  234. if (!data->bitmap) {
  235. /*
  236. * User space encodes device types as two-byte values,
  237. * so we need to recode them
  238. */
  239. if (old_decode_dev(arg)) {
  240. data->swap = swap_type_of(old_decode_dev(arg),
  241. 0, NULL);
  242. if (data->swap < 0)
  243. error = -ENODEV;
  244. } else {
  245. data->swap = -1;
  246. error = -EINVAL;
  247. }
  248. } else {
  249. error = -EPERM;
  250. }
  251. break;
  252. case SNAPSHOT_S2RAM:
  253. if (!data->frozen) {
  254. error = -EPERM;
  255. break;
  256. }
  257. if (!mutex_trylock(&pm_mutex)) {
  258. error = -EBUSY;
  259. break;
  260. }
  261. if (pm_ops->prepare) {
  262. error = pm_ops->prepare(PM_SUSPEND_MEM);
  263. if (error)
  264. goto OutS3;
  265. }
  266. /* Put devices to sleep */
  267. suspend_console();
  268. error = device_suspend(PMSG_SUSPEND);
  269. if (error) {
  270. printk(KERN_ERR "Failed to suspend some devices.\n");
  271. } else {
  272. /* Enter S3, system is already frozen */
  273. suspend_enter(PM_SUSPEND_MEM);
  274. /* Wake up devices */
  275. device_resume();
  276. }
  277. resume_console();
  278. if (pm_ops->finish)
  279. pm_ops->finish(PM_SUSPEND_MEM);
  280. OutS3:
  281. mutex_unlock(&pm_mutex);
  282. break;
  283. case SNAPSHOT_PMOPS:
  284. switch (arg) {
  285. case PMOPS_PREPARE:
  286. if (pm_ops->prepare) {
  287. error = pm_ops->prepare(PM_SUSPEND_DISK);
  288. }
  289. break;
  290. case PMOPS_ENTER:
  291. kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
  292. error = pm_ops->enter(PM_SUSPEND_DISK);
  293. break;
  294. case PMOPS_FINISH:
  295. if (pm_ops && pm_ops->finish) {
  296. pm_ops->finish(PM_SUSPEND_DISK);
  297. }
  298. break;
  299. default:
  300. printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
  301. error = -EINVAL;
  302. }
  303. break;
  304. case SNAPSHOT_SET_SWAP_AREA:
  305. if (data->bitmap) {
  306. error = -EPERM;
  307. } else {
  308. struct resume_swap_area swap_area;
  309. dev_t swdev;
  310. error = copy_from_user(&swap_area, (void __user *)arg,
  311. sizeof(struct resume_swap_area));
  312. if (error) {
  313. error = -EFAULT;
  314. break;
  315. }
  316. /*
  317. * User space encodes device types as two-byte values,
  318. * so we need to recode them
  319. */
  320. swdev = old_decode_dev(swap_area.dev);
  321. if (swdev) {
  322. offset = swap_area.offset;
  323. data->swap = swap_type_of(swdev, offset, NULL);
  324. if (data->swap < 0)
  325. error = -ENODEV;
  326. } else {
  327. data->swap = -1;
  328. error = -EINVAL;
  329. }
  330. }
  331. break;
  332. default:
  333. error = -ENOTTY;
  334. }
  335. return error;
  336. }
  337. static const struct file_operations snapshot_fops = {
  338. .open = snapshot_open,
  339. .release = snapshot_release,
  340. .read = snapshot_read,
  341. .write = snapshot_write,
  342. .llseek = no_llseek,
  343. .ioctl = snapshot_ioctl,
  344. };
  345. static struct miscdevice snapshot_device = {
  346. .minor = SNAPSHOT_MINOR,
  347. .name = "snapshot",
  348. .fops = &snapshot_fops,
  349. };
  350. static int __init snapshot_device_init(void)
  351. {
  352. return misc_register(&snapshot_device);
  353. };
  354. device_initcall(snapshot_device_init);