user.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 int snapshot_ioctl(struct inode *inode, struct file *filp,
  115. unsigned int cmd, unsigned long arg)
  116. {
  117. int error = 0;
  118. struct snapshot_data *data;
  119. loff_t avail;
  120. sector_t offset;
  121. if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
  122. return -ENOTTY;
  123. if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
  124. return -ENOTTY;
  125. if (!capable(CAP_SYS_ADMIN))
  126. return -EPERM;
  127. data = filp->private_data;
  128. switch (cmd) {
  129. case SNAPSHOT_FREEZE:
  130. if (data->frozen)
  131. break;
  132. mutex_lock(&pm_mutex);
  133. error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
  134. if (!error) {
  135. error = freeze_processes();
  136. if (error)
  137. thaw_processes();
  138. }
  139. if (error)
  140. pm_notifier_call_chain(PM_POST_HIBERNATION);
  141. mutex_unlock(&pm_mutex);
  142. if (!error)
  143. data->frozen = 1;
  144. break;
  145. case SNAPSHOT_UNFREEZE:
  146. if (!data->frozen || data->ready)
  147. break;
  148. mutex_lock(&pm_mutex);
  149. thaw_processes();
  150. pm_notifier_call_chain(PM_POST_HIBERNATION);
  151. mutex_unlock(&pm_mutex);
  152. data->frozen = 0;
  153. break;
  154. case SNAPSHOT_ATOMIC_SNAPSHOT:
  155. if (data->mode != O_RDONLY || !data->frozen || data->ready) {
  156. error = -EPERM;
  157. break;
  158. }
  159. error = hibernation_snapshot(data->platform_suspend);
  160. if (!error)
  161. error = put_user(in_suspend, (unsigned int __user *)arg);
  162. if (!error)
  163. data->ready = 1;
  164. break;
  165. case SNAPSHOT_ATOMIC_RESTORE:
  166. snapshot_write_finalize(&data->handle);
  167. if (data->mode != O_WRONLY || !data->frozen ||
  168. !snapshot_image_loaded(&data->handle)) {
  169. error = -EPERM;
  170. break;
  171. }
  172. error = hibernation_restore(data->platform_suspend);
  173. break;
  174. case SNAPSHOT_FREE:
  175. swsusp_free();
  176. memset(&data->handle, 0, sizeof(struct snapshot_handle));
  177. data->ready = 0;
  178. break;
  179. case SNAPSHOT_SET_IMAGE_SIZE:
  180. image_size = arg;
  181. break;
  182. case SNAPSHOT_AVAIL_SWAP:
  183. avail = count_swap_pages(data->swap, 1);
  184. avail <<= PAGE_SHIFT;
  185. error = put_user(avail, (loff_t __user *)arg);
  186. break;
  187. case SNAPSHOT_GET_SWAP_PAGE:
  188. if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
  189. error = -ENODEV;
  190. break;
  191. }
  192. offset = alloc_swapdev_block(data->swap);
  193. if (offset) {
  194. offset <<= PAGE_SHIFT;
  195. error = put_user(offset, (sector_t __user *)arg);
  196. } else {
  197. error = -ENOSPC;
  198. }
  199. break;
  200. case SNAPSHOT_FREE_SWAP_PAGES:
  201. if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
  202. error = -ENODEV;
  203. break;
  204. }
  205. free_all_swap_pages(data->swap);
  206. break;
  207. case SNAPSHOT_SET_SWAP_FILE:
  208. if (!swsusp_swap_in_use()) {
  209. /*
  210. * User space encodes device types as two-byte values,
  211. * so we need to recode them
  212. */
  213. if (old_decode_dev(arg)) {
  214. data->swap = swap_type_of(old_decode_dev(arg),
  215. 0, NULL);
  216. if (data->swap < 0)
  217. error = -ENODEV;
  218. } else {
  219. data->swap = -1;
  220. error = -EINVAL;
  221. }
  222. } else {
  223. error = -EPERM;
  224. }
  225. break;
  226. case SNAPSHOT_S2RAM:
  227. if (!data->frozen) {
  228. error = -EPERM;
  229. break;
  230. }
  231. if (!mutex_trylock(&pm_mutex)) {
  232. error = -EBUSY;
  233. break;
  234. }
  235. /*
  236. * Tasks are frozen and the notifiers have been called with
  237. * PM_HIBERNATION_PREPARE
  238. */
  239. error = suspend_devices_and_enter(PM_SUSPEND_MEM);
  240. mutex_unlock(&pm_mutex);
  241. break;
  242. case SNAPSHOT_PMOPS:
  243. error = -EINVAL;
  244. switch (arg) {
  245. case PMOPS_PREPARE:
  246. data->platform_suspend = 1;
  247. error = 0;
  248. break;
  249. case PMOPS_ENTER:
  250. if (data->platform_suspend)
  251. error = hibernation_platform_enter();
  252. break;
  253. case PMOPS_FINISH:
  254. if (data->platform_suspend)
  255. error = 0;
  256. break;
  257. default:
  258. printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
  259. }
  260. break;
  261. case SNAPSHOT_SET_SWAP_AREA:
  262. if (swsusp_swap_in_use()) {
  263. error = -EPERM;
  264. } else {
  265. struct resume_swap_area swap_area;
  266. dev_t swdev;
  267. error = copy_from_user(&swap_area, (void __user *)arg,
  268. sizeof(struct resume_swap_area));
  269. if (error) {
  270. error = -EFAULT;
  271. break;
  272. }
  273. /*
  274. * User space encodes device types as two-byte values,
  275. * so we need to recode them
  276. */
  277. swdev = old_decode_dev(swap_area.dev);
  278. if (swdev) {
  279. offset = swap_area.offset;
  280. data->swap = swap_type_of(swdev, offset, NULL);
  281. if (data->swap < 0)
  282. error = -ENODEV;
  283. } else {
  284. data->swap = -1;
  285. error = -EINVAL;
  286. }
  287. }
  288. break;
  289. default:
  290. error = -ENOTTY;
  291. }
  292. return error;
  293. }
  294. static const struct file_operations snapshot_fops = {
  295. .open = snapshot_open,
  296. .release = snapshot_release,
  297. .read = snapshot_read,
  298. .write = snapshot_write,
  299. .llseek = no_llseek,
  300. .ioctl = snapshot_ioctl,
  301. };
  302. static struct miscdevice snapshot_device = {
  303. .minor = SNAPSHOT_MINOR,
  304. .name = "snapshot",
  305. .fops = &snapshot_fops,
  306. };
  307. static int __init snapshot_device_init(void)
  308. {
  309. return misc_register(&snapshot_device);
  310. };
  311. device_initcall(snapshot_device_init);