user.c 10.0 KB

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