user.c 10 KB

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