user.c 9.9 KB

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