user.c 10 KB

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