user.c 10 KB

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