user.c 9.3 KB

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