user.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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/kmod.h>
  15. #include <linux/string.h>
  16. #include <linux/device.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/mm.h>
  19. #include <linux/swap.h>
  20. #include <linux/swapops.h>
  21. #include <linux/pm.h>
  22. #include <linux/fs.h>
  23. #include <linux/console.h>
  24. #include <linux/cpu.h>
  25. #include <linux/freezer.h>
  26. #include <scsi/scsi_scan.h>
  27. #include <asm/uaccess.h>
  28. #include "power.h"
  29. #define SNAPSHOT_MINOR 231
  30. static struct snapshot_data {
  31. struct snapshot_handle handle;
  32. int swap;
  33. int mode;
  34. char frozen;
  35. char ready;
  36. char platform_support;
  37. } snapshot_state;
  38. atomic_t snapshot_device_available = ATOMIC_INIT(1);
  39. static int snapshot_open(struct inode *inode, struct file *filp)
  40. {
  41. struct snapshot_data *data;
  42. int error;
  43. lock_system_sleep();
  44. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  45. error = -EBUSY;
  46. goto Unlock;
  47. }
  48. if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
  49. atomic_inc(&snapshot_device_available);
  50. error = -ENOSYS;
  51. goto Unlock;
  52. }
  53. if(create_basic_memory_bitmaps()) {
  54. atomic_inc(&snapshot_device_available);
  55. error = -ENOMEM;
  56. goto Unlock;
  57. }
  58. nonseekable_open(inode, filp);
  59. data = &snapshot_state;
  60. filp->private_data = data;
  61. memset(&data->handle, 0, sizeof(struct snapshot_handle));
  62. if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
  63. /* Hibernating. The image device should be accessible. */
  64. data->swap = swsusp_resume_device ?
  65. swap_type_of(swsusp_resume_device, 0, NULL) : -1;
  66. data->mode = O_RDONLY;
  67. error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
  68. if (error)
  69. pm_notifier_call_chain(PM_POST_HIBERNATION);
  70. } else {
  71. /*
  72. * Resuming. We may need to wait for the image device to
  73. * appear.
  74. */
  75. wait_for_device_probe();
  76. scsi_complete_async_scans();
  77. data->swap = -1;
  78. data->mode = O_WRONLY;
  79. error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
  80. if (error)
  81. pm_notifier_call_chain(PM_POST_RESTORE);
  82. }
  83. if (error) {
  84. free_basic_memory_bitmaps();
  85. atomic_inc(&snapshot_device_available);
  86. }
  87. data->frozen = 0;
  88. data->ready = 0;
  89. data->platform_support = 0;
  90. Unlock:
  91. unlock_system_sleep();
  92. return error;
  93. }
  94. static int snapshot_release(struct inode *inode, struct file *filp)
  95. {
  96. struct snapshot_data *data;
  97. lock_system_sleep();
  98. swsusp_free();
  99. free_basic_memory_bitmaps();
  100. data = filp->private_data;
  101. free_all_swap_pages(data->swap);
  102. if (data->frozen) {
  103. pm_restore_gfp_mask();
  104. thaw_processes();
  105. }
  106. pm_notifier_call_chain(data->mode == O_RDONLY ?
  107. PM_POST_HIBERNATION : PM_POST_RESTORE);
  108. atomic_inc(&snapshot_device_available);
  109. unlock_system_sleep();
  110. return 0;
  111. }
  112. static ssize_t snapshot_read(struct file *filp, char __user *buf,
  113. size_t count, loff_t *offp)
  114. {
  115. struct snapshot_data *data;
  116. ssize_t res;
  117. loff_t pg_offp = *offp & ~PAGE_MASK;
  118. lock_system_sleep();
  119. data = filp->private_data;
  120. if (!data->ready) {
  121. res = -ENODATA;
  122. goto Unlock;
  123. }
  124. if (!pg_offp) { /* on page boundary? */
  125. res = snapshot_read_next(&data->handle);
  126. if (res <= 0)
  127. goto Unlock;
  128. } else {
  129. res = PAGE_SIZE - pg_offp;
  130. }
  131. res = simple_read_from_buffer(buf, count, &pg_offp,
  132. data_of(data->handle), res);
  133. if (res > 0)
  134. *offp += res;
  135. Unlock:
  136. unlock_system_sleep();
  137. return res;
  138. }
  139. static ssize_t snapshot_write(struct file *filp, const char __user *buf,
  140. size_t count, loff_t *offp)
  141. {
  142. struct snapshot_data *data;
  143. ssize_t res;
  144. loff_t pg_offp = *offp & ~PAGE_MASK;
  145. lock_system_sleep();
  146. data = filp->private_data;
  147. if (!pg_offp) {
  148. res = snapshot_write_next(&data->handle);
  149. if (res <= 0)
  150. goto unlock;
  151. } else {
  152. res = PAGE_SIZE - pg_offp;
  153. }
  154. res = simple_write_to_buffer(data_of(data->handle), res, &pg_offp,
  155. buf, count);
  156. if (res > 0)
  157. *offp += res;
  158. unlock:
  159. unlock_system_sleep();
  160. return res;
  161. }
  162. static long snapshot_ioctl(struct file *filp, unsigned int cmd,
  163. unsigned long arg)
  164. {
  165. int error = 0;
  166. struct snapshot_data *data;
  167. loff_t size;
  168. sector_t offset;
  169. if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
  170. return -ENOTTY;
  171. if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
  172. return -ENOTTY;
  173. if (!capable(CAP_SYS_ADMIN))
  174. return -EPERM;
  175. if (!mutex_trylock(&pm_mutex))
  176. return -EBUSY;
  177. data = filp->private_data;
  178. switch (cmd) {
  179. case SNAPSHOT_FREEZE:
  180. if (data->frozen)
  181. break;
  182. printk("Syncing filesystems ... ");
  183. sys_sync();
  184. printk("done.\n");
  185. error = usermodehelper_disable();
  186. if (error)
  187. break;
  188. error = freeze_processes();
  189. if (error)
  190. usermodehelper_enable();
  191. else
  192. data->frozen = 1;
  193. break;
  194. case SNAPSHOT_UNFREEZE:
  195. if (!data->frozen || data->ready)
  196. break;
  197. pm_restore_gfp_mask();
  198. thaw_processes();
  199. usermodehelper_enable();
  200. data->frozen = 0;
  201. break;
  202. case SNAPSHOT_CREATE_IMAGE:
  203. if (data->mode != O_RDONLY || !data->frozen || data->ready) {
  204. error = -EPERM;
  205. break;
  206. }
  207. pm_restore_gfp_mask();
  208. error = hibernation_snapshot(data->platform_support);
  209. if (!error) {
  210. error = put_user(in_suspend, (int __user *)arg);
  211. if (!error && !freezer_test_done)
  212. data->ready = 1;
  213. if (freezer_test_done) {
  214. freezer_test_done = false;
  215. thaw_processes();
  216. }
  217. }
  218. break;
  219. case SNAPSHOT_ATOMIC_RESTORE:
  220. snapshot_write_finalize(&data->handle);
  221. if (data->mode != O_WRONLY || !data->frozen ||
  222. !snapshot_image_loaded(&data->handle)) {
  223. error = -EPERM;
  224. break;
  225. }
  226. error = hibernation_restore(data->platform_support);
  227. break;
  228. case SNAPSHOT_FREE:
  229. swsusp_free();
  230. memset(&data->handle, 0, sizeof(struct snapshot_handle));
  231. data->ready = 0;
  232. break;
  233. case SNAPSHOT_PREF_IMAGE_SIZE:
  234. image_size = arg;
  235. break;
  236. case SNAPSHOT_GET_IMAGE_SIZE:
  237. if (!data->ready) {
  238. error = -ENODATA;
  239. break;
  240. }
  241. size = snapshot_get_image_size();
  242. size <<= PAGE_SHIFT;
  243. error = put_user(size, (loff_t __user *)arg);
  244. break;
  245. case SNAPSHOT_AVAIL_SWAP_SIZE:
  246. size = count_swap_pages(data->swap, 1);
  247. size <<= PAGE_SHIFT;
  248. error = put_user(size, (loff_t __user *)arg);
  249. break;
  250. case SNAPSHOT_ALLOC_SWAP_PAGE:
  251. if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
  252. error = -ENODEV;
  253. break;
  254. }
  255. offset = alloc_swapdev_block(data->swap);
  256. if (offset) {
  257. offset <<= PAGE_SHIFT;
  258. error = put_user(offset, (loff_t __user *)arg);
  259. } else {
  260. error = -ENOSPC;
  261. }
  262. break;
  263. case SNAPSHOT_FREE_SWAP_PAGES:
  264. if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
  265. error = -ENODEV;
  266. break;
  267. }
  268. free_all_swap_pages(data->swap);
  269. break;
  270. case SNAPSHOT_S2RAM:
  271. if (!data->frozen) {
  272. error = -EPERM;
  273. break;
  274. }
  275. /*
  276. * Tasks are frozen and the notifiers have been called with
  277. * PM_HIBERNATION_PREPARE
  278. */
  279. error = suspend_devices_and_enter(PM_SUSPEND_MEM);
  280. data->ready = 0;
  281. break;
  282. case SNAPSHOT_PLATFORM_SUPPORT:
  283. data->platform_support = !!arg;
  284. break;
  285. case SNAPSHOT_POWER_OFF:
  286. if (data->platform_support)
  287. error = hibernation_platform_enter();
  288. break;
  289. case SNAPSHOT_SET_SWAP_AREA:
  290. if (swsusp_swap_in_use()) {
  291. error = -EPERM;
  292. } else {
  293. struct resume_swap_area swap_area;
  294. dev_t swdev;
  295. error = copy_from_user(&swap_area, (void __user *)arg,
  296. sizeof(struct resume_swap_area));
  297. if (error) {
  298. error = -EFAULT;
  299. break;
  300. }
  301. /*
  302. * User space encodes device types as two-byte values,
  303. * so we need to recode them
  304. */
  305. swdev = new_decode_dev(swap_area.dev);
  306. if (swdev) {
  307. offset = swap_area.offset;
  308. data->swap = swap_type_of(swdev, offset, NULL);
  309. if (data->swap < 0)
  310. error = -ENODEV;
  311. } else {
  312. data->swap = -1;
  313. error = -EINVAL;
  314. }
  315. }
  316. break;
  317. default:
  318. error = -ENOTTY;
  319. }
  320. mutex_unlock(&pm_mutex);
  321. return error;
  322. }
  323. static const struct file_operations snapshot_fops = {
  324. .open = snapshot_open,
  325. .release = snapshot_release,
  326. .read = snapshot_read,
  327. .write = snapshot_write,
  328. .llseek = no_llseek,
  329. .unlocked_ioctl = snapshot_ioctl,
  330. };
  331. static struct miscdevice snapshot_device = {
  332. .minor = SNAPSHOT_MINOR,
  333. .name = "snapshot",
  334. .fops = &snapshot_fops,
  335. };
  336. static int __init snapshot_device_init(void)
  337. {
  338. return misc_register(&snapshot_device);
  339. };
  340. device_initcall(snapshot_device_init);