user.c 7.5 KB

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