user.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. /*
  28. * NOTE: The SNAPSHOT_SET_SWAP_FILE and SNAPSHOT_PMOPS ioctls are obsolete and
  29. * will be removed in the future. They are only preserved here for
  30. * compatibility with existing userland utilities.
  31. */
  32. #define SNAPSHOT_SET_SWAP_FILE _IOW(SNAPSHOT_IOC_MAGIC, 10, unsigned int)
  33. #define SNAPSHOT_PMOPS _IOW(SNAPSHOT_IOC_MAGIC, 12, unsigned int)
  34. #define PMOPS_PREPARE 1
  35. #define PMOPS_ENTER 2
  36. #define PMOPS_FINISH 3
  37. /*
  38. * NOTE: The following ioctl definitions are wrong and have been replaced with
  39. * correct ones. They are only preserved here for compatibility with existing
  40. * userland utilities and will be removed in the future.
  41. */
  42. #define SNAPSHOT_ATOMIC_SNAPSHOT _IOW(SNAPSHOT_IOC_MAGIC, 3, void *)
  43. #define SNAPSHOT_SET_IMAGE_SIZE _IOW(SNAPSHOT_IOC_MAGIC, 6, unsigned long)
  44. #define SNAPSHOT_AVAIL_SWAP _IOR(SNAPSHOT_IOC_MAGIC, 7, void *)
  45. #define SNAPSHOT_GET_SWAP_PAGE _IOR(SNAPSHOT_IOC_MAGIC, 8, void *)
  46. #define SNAPSHOT_MINOR 231
  47. static struct snapshot_data {
  48. struct snapshot_handle handle;
  49. int swap;
  50. int mode;
  51. char frozen;
  52. char ready;
  53. char platform_support;
  54. } snapshot_state;
  55. atomic_t snapshot_device_available = ATOMIC_INIT(1);
  56. static int snapshot_open(struct inode *inode, struct file *filp)
  57. {
  58. struct snapshot_data *data;
  59. int error;
  60. if (!atomic_add_unless(&snapshot_device_available, -1, 0))
  61. return -EBUSY;
  62. if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
  63. atomic_inc(&snapshot_device_available);
  64. return -ENOSYS;
  65. }
  66. if(create_basic_memory_bitmaps()) {
  67. atomic_inc(&snapshot_device_available);
  68. return -ENOMEM;
  69. }
  70. nonseekable_open(inode, filp);
  71. data = &snapshot_state;
  72. filp->private_data = data;
  73. memset(&data->handle, 0, sizeof(struct snapshot_handle));
  74. if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
  75. data->swap = swsusp_resume_device ?
  76. swap_type_of(swsusp_resume_device, 0, NULL) : -1;
  77. data->mode = O_RDONLY;
  78. error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
  79. if (error)
  80. pm_notifier_call_chain(PM_POST_RESTORE);
  81. } else {
  82. data->swap = -1;
  83. data->mode = O_WRONLY;
  84. error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
  85. if (error)
  86. pm_notifier_call_chain(PM_POST_HIBERNATION);
  87. }
  88. if (error) {
  89. atomic_inc(&snapshot_device_available);
  90. return error;
  91. }
  92. data->frozen = 0;
  93. data->ready = 0;
  94. data->platform_support = 0;
  95. return 0;
  96. }
  97. static int snapshot_release(struct inode *inode, struct file *filp)
  98. {
  99. struct snapshot_data *data;
  100. swsusp_free();
  101. free_basic_memory_bitmaps();
  102. data = filp->private_data;
  103. free_all_swap_pages(data->swap);
  104. if (data->frozen) {
  105. mutex_lock(&pm_mutex);
  106. thaw_processes();
  107. mutex_unlock(&pm_mutex);
  108. }
  109. pm_notifier_call_chain(data->mode == O_WRONLY ?
  110. PM_POST_HIBERNATION : PM_POST_RESTORE);
  111. atomic_inc(&snapshot_device_available);
  112. return 0;
  113. }
  114. static ssize_t snapshot_read(struct file *filp, char __user *buf,
  115. size_t count, loff_t *offp)
  116. {
  117. struct snapshot_data *data;
  118. ssize_t res;
  119. data = filp->private_data;
  120. if (!data->ready)
  121. return -ENODATA;
  122. res = snapshot_read_next(&data->handle, count);
  123. if (res > 0) {
  124. if (copy_to_user(buf, data_of(data->handle), res))
  125. res = -EFAULT;
  126. else
  127. *offp = data->handle.offset;
  128. }
  129. return res;
  130. }
  131. static ssize_t snapshot_write(struct file *filp, const char __user *buf,
  132. size_t count, loff_t *offp)
  133. {
  134. struct snapshot_data *data;
  135. ssize_t res;
  136. data = filp->private_data;
  137. res = snapshot_write_next(&data->handle, count);
  138. if (res > 0) {
  139. if (copy_from_user(data_of(data->handle), buf, res))
  140. res = -EFAULT;
  141. else
  142. *offp = data->handle.offset;
  143. }
  144. return res;
  145. }
  146. static int snapshot_ioctl(struct inode *inode, struct file *filp,
  147. unsigned int cmd, unsigned long arg)
  148. {
  149. int error = 0;
  150. struct snapshot_data *data;
  151. loff_t size;
  152. sector_t offset;
  153. if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
  154. return -ENOTTY;
  155. if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
  156. return -ENOTTY;
  157. if (!capable(CAP_SYS_ADMIN))
  158. return -EPERM;
  159. data = filp->private_data;
  160. switch (cmd) {
  161. case SNAPSHOT_FREEZE:
  162. if (data->frozen)
  163. break;
  164. mutex_lock(&pm_mutex);
  165. printk("Syncing filesystems ... ");
  166. sys_sync();
  167. printk("done.\n");
  168. error = freeze_processes();
  169. if (error)
  170. thaw_processes();
  171. mutex_unlock(&pm_mutex);
  172. if (!error)
  173. data->frozen = 1;
  174. break;
  175. case SNAPSHOT_UNFREEZE:
  176. if (!data->frozen || data->ready)
  177. break;
  178. mutex_lock(&pm_mutex);
  179. thaw_processes();
  180. mutex_unlock(&pm_mutex);
  181. data->frozen = 0;
  182. break;
  183. case SNAPSHOT_CREATE_IMAGE:
  184. case SNAPSHOT_ATOMIC_SNAPSHOT:
  185. if (data->mode != O_RDONLY || !data->frozen || data->ready) {
  186. error = -EPERM;
  187. break;
  188. }
  189. error = hibernation_snapshot(data->platform_support);
  190. if (!error)
  191. error = put_user(in_suspend, (int __user *)arg);
  192. if (!error)
  193. data->ready = 1;
  194. break;
  195. case SNAPSHOT_ATOMIC_RESTORE:
  196. snapshot_write_finalize(&data->handle);
  197. if (data->mode != O_WRONLY || !data->frozen ||
  198. !snapshot_image_loaded(&data->handle)) {
  199. error = -EPERM;
  200. break;
  201. }
  202. error = hibernation_restore(data->platform_support);
  203. break;
  204. case SNAPSHOT_FREE:
  205. swsusp_free();
  206. memset(&data->handle, 0, sizeof(struct snapshot_handle));
  207. data->ready = 0;
  208. break;
  209. case SNAPSHOT_PREF_IMAGE_SIZE:
  210. case SNAPSHOT_SET_IMAGE_SIZE:
  211. image_size = arg;
  212. break;
  213. case SNAPSHOT_GET_IMAGE_SIZE:
  214. if (!data->ready) {
  215. error = -ENODATA;
  216. break;
  217. }
  218. size = snapshot_get_image_size();
  219. size <<= PAGE_SHIFT;
  220. error = put_user(size, (loff_t __user *)arg);
  221. break;
  222. case SNAPSHOT_AVAIL_SWAP_SIZE:
  223. case SNAPSHOT_AVAIL_SWAP:
  224. size = count_swap_pages(data->swap, 1);
  225. size <<= PAGE_SHIFT;
  226. error = put_user(size, (loff_t __user *)arg);
  227. break;
  228. case SNAPSHOT_ALLOC_SWAP_PAGE:
  229. case SNAPSHOT_GET_SWAP_PAGE:
  230. if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
  231. error = -ENODEV;
  232. break;
  233. }
  234. offset = alloc_swapdev_block(data->swap);
  235. if (offset) {
  236. offset <<= PAGE_SHIFT;
  237. error = put_user(offset, (loff_t __user *)arg);
  238. } else {
  239. error = -ENOSPC;
  240. }
  241. break;
  242. case SNAPSHOT_FREE_SWAP_PAGES:
  243. if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
  244. error = -ENODEV;
  245. break;
  246. }
  247. free_all_swap_pages(data->swap);
  248. break;
  249. case SNAPSHOT_SET_SWAP_FILE: /* This ioctl is deprecated */
  250. if (!swsusp_swap_in_use()) {
  251. /*
  252. * User space encodes device types as two-byte values,
  253. * so we need to recode them
  254. */
  255. if (old_decode_dev(arg)) {
  256. data->swap = swap_type_of(old_decode_dev(arg),
  257. 0, NULL);
  258. if (data->swap < 0)
  259. error = -ENODEV;
  260. } else {
  261. data->swap = -1;
  262. error = -EINVAL;
  263. }
  264. } else {
  265. error = -EPERM;
  266. }
  267. break;
  268. case SNAPSHOT_S2RAM:
  269. if (!data->frozen) {
  270. error = -EPERM;
  271. break;
  272. }
  273. if (!mutex_trylock(&pm_mutex)) {
  274. error = -EBUSY;
  275. break;
  276. }
  277. /*
  278. * Tasks are frozen and the notifiers have been called with
  279. * PM_HIBERNATION_PREPARE
  280. */
  281. error = suspend_devices_and_enter(PM_SUSPEND_MEM);
  282. mutex_unlock(&pm_mutex);
  283. break;
  284. case SNAPSHOT_PLATFORM_SUPPORT:
  285. data->platform_support = !!arg;
  286. break;
  287. case SNAPSHOT_POWER_OFF:
  288. if (data->platform_support)
  289. error = hibernation_platform_enter();
  290. break;
  291. case SNAPSHOT_PMOPS: /* This ioctl is deprecated */
  292. error = -EINVAL;
  293. switch (arg) {
  294. case PMOPS_PREPARE:
  295. data->platform_support = 1;
  296. error = 0;
  297. break;
  298. case PMOPS_ENTER:
  299. if (data->platform_support)
  300. error = hibernation_platform_enter();
  301. break;
  302. case PMOPS_FINISH:
  303. if (data->platform_support)
  304. error = 0;
  305. break;
  306. default:
  307. printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
  308. }
  309. break;
  310. case SNAPSHOT_SET_SWAP_AREA:
  311. if (swsusp_swap_in_use()) {
  312. error = -EPERM;
  313. } else {
  314. struct resume_swap_area swap_area;
  315. dev_t swdev;
  316. error = copy_from_user(&swap_area, (void __user *)arg,
  317. sizeof(struct resume_swap_area));
  318. if (error) {
  319. error = -EFAULT;
  320. break;
  321. }
  322. /*
  323. * User space encodes device types as two-byte values,
  324. * so we need to recode them
  325. */
  326. swdev = old_decode_dev(swap_area.dev);
  327. if (swdev) {
  328. offset = swap_area.offset;
  329. data->swap = swap_type_of(swdev, offset, NULL);
  330. if (data->swap < 0)
  331. error = -ENODEV;
  332. } else {
  333. data->swap = -1;
  334. error = -EINVAL;
  335. }
  336. }
  337. break;
  338. default:
  339. error = -ENOTTY;
  340. }
  341. return error;
  342. }
  343. static const struct file_operations snapshot_fops = {
  344. .open = snapshot_open,
  345. .release = snapshot_release,
  346. .read = snapshot_read,
  347. .write = snapshot_write,
  348. .llseek = no_llseek,
  349. .ioctl = snapshot_ioctl,
  350. };
  351. static struct miscdevice snapshot_device = {
  352. .minor = SNAPSHOT_MINOR,
  353. .name = "snapshot",
  354. .fops = &snapshot_fops,
  355. };
  356. static int __init snapshot_device_init(void)
  357. {
  358. return misc_register(&snapshot_device);
  359. };
  360. device_initcall(snapshot_device_init);