user.c 9.7 KB

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