user.c 9.7 KB

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