user.c 9.2 KB

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