user.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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/kmod.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 <scsi/scsi_scan.h>
  28. #include <asm/uaccess.h>
  29. #include "power.h"
  30. #define SNAPSHOT_MINOR 231
  31. static struct snapshot_data {
  32. struct snapshot_handle handle;
  33. int swap;
  34. int mode;
  35. char frozen;
  36. char ready;
  37. char platform_support;
  38. } snapshot_state;
  39. atomic_t snapshot_device_available = ATOMIC_INIT(1);
  40. static int snapshot_open(struct inode *inode, struct file *filp)
  41. {
  42. struct snapshot_data *data;
  43. int error;
  44. lock_system_sleep();
  45. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  46. error = -EBUSY;
  47. goto Unlock;
  48. }
  49. if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
  50. atomic_inc(&snapshot_device_available);
  51. error = -ENOSYS;
  52. goto Unlock;
  53. }
  54. if(create_basic_memory_bitmaps()) {
  55. atomic_inc(&snapshot_device_available);
  56. error = -ENOMEM;
  57. goto Unlock;
  58. }
  59. nonseekable_open(inode, filp);
  60. data = &snapshot_state;
  61. filp->private_data = data;
  62. memset(&data->handle, 0, sizeof(struct snapshot_handle));
  63. if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
  64. /* Hibernating. The image device should be accessible. */
  65. data->swap = swsusp_resume_device ?
  66. swap_type_of(swsusp_resume_device, 0, NULL) : -1;
  67. data->mode = O_RDONLY;
  68. error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
  69. if (error)
  70. pm_notifier_call_chain(PM_POST_HIBERNATION);
  71. } else {
  72. /*
  73. * Resuming. We may need to wait for the image device to
  74. * appear.
  75. */
  76. wait_for_device_probe();
  77. scsi_complete_async_scans();
  78. data->swap = -1;
  79. data->mode = O_WRONLY;
  80. error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
  81. if (error)
  82. pm_notifier_call_chain(PM_POST_RESTORE);
  83. }
  84. if (error) {
  85. free_basic_memory_bitmaps();
  86. atomic_inc(&snapshot_device_available);
  87. }
  88. data->frozen = 0;
  89. data->ready = 0;
  90. data->platform_support = 0;
  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. free_basic_memory_bitmaps();
  101. data = filp->private_data;
  102. free_all_swap_pages(data->swap);
  103. if (data->frozen) {
  104. pm_restore_gfp_mask();
  105. thaw_processes();
  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. data = filp->private_data;
  179. switch (cmd) {
  180. case SNAPSHOT_FREEZE:
  181. if (data->frozen)
  182. break;
  183. printk("Syncing filesystems ... ");
  184. sys_sync();
  185. printk("done.\n");
  186. error = usermodehelper_disable();
  187. if (error)
  188. break;
  189. error = freeze_processes();
  190. if (error)
  191. usermodehelper_enable();
  192. else
  193. data->frozen = 1;
  194. break;
  195. case SNAPSHOT_UNFREEZE:
  196. if (!data->frozen || data->ready)
  197. break;
  198. pm_restore_gfp_mask();
  199. thaw_processes();
  200. usermodehelper_enable();
  201. data->frozen = 0;
  202. break;
  203. case SNAPSHOT_CREATE_IMAGE:
  204. if (data->mode != O_RDONLY || !data->frozen || data->ready) {
  205. error = -EPERM;
  206. break;
  207. }
  208. pm_restore_gfp_mask();
  209. error = hibernation_snapshot(data->platform_support);
  210. if (!error) {
  211. error = put_user(in_suspend, (int __user *)arg);
  212. if (!error && !freezer_test_done)
  213. data->ready = 1;
  214. if (freezer_test_done) {
  215. freezer_test_done = false;
  216. thaw_processes();
  217. }
  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 = 0;
  233. break;
  234. case SNAPSHOT_PREF_IMAGE_SIZE:
  235. image_size = arg;
  236. break;
  237. case SNAPSHOT_GET_IMAGE_SIZE:
  238. if (!data->ready) {
  239. error = -ENODATA;
  240. break;
  241. }
  242. size = snapshot_get_image_size();
  243. size <<= PAGE_SHIFT;
  244. error = put_user(size, (loff_t __user *)arg);
  245. break;
  246. case SNAPSHOT_AVAIL_SWAP_SIZE:
  247. size = count_swap_pages(data->swap, 1);
  248. size <<= PAGE_SHIFT;
  249. error = put_user(size, (loff_t __user *)arg);
  250. break;
  251. case SNAPSHOT_ALLOC_SWAP_PAGE:
  252. if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
  253. error = -ENODEV;
  254. break;
  255. }
  256. offset = alloc_swapdev_block(data->swap);
  257. if (offset) {
  258. offset <<= PAGE_SHIFT;
  259. error = put_user(offset, (loff_t __user *)arg);
  260. } else {
  261. error = -ENOSPC;
  262. }
  263. break;
  264. case SNAPSHOT_FREE_SWAP_PAGES:
  265. if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
  266. error = -ENODEV;
  267. break;
  268. }
  269. free_all_swap_pages(data->swap);
  270. break;
  271. case SNAPSHOT_S2RAM:
  272. if (!data->frozen) {
  273. error = -EPERM;
  274. break;
  275. }
  276. /*
  277. * Tasks are frozen and the notifiers have been called with
  278. * PM_HIBERNATION_PREPARE
  279. */
  280. error = suspend_devices_and_enter(PM_SUSPEND_MEM);
  281. data->ready = 0;
  282. break;
  283. case SNAPSHOT_PLATFORM_SUPPORT:
  284. data->platform_support = !!arg;
  285. break;
  286. case SNAPSHOT_POWER_OFF:
  287. if (data->platform_support)
  288. error = hibernation_platform_enter();
  289. break;
  290. case SNAPSHOT_SET_SWAP_AREA:
  291. if (swsusp_swap_in_use()) {
  292. error = -EPERM;
  293. } else {
  294. struct resume_swap_area swap_area;
  295. dev_t swdev;
  296. error = copy_from_user(&swap_area, (void __user *)arg,
  297. sizeof(struct resume_swap_area));
  298. if (error) {
  299. error = -EFAULT;
  300. break;
  301. }
  302. /*
  303. * User space encodes device types as two-byte values,
  304. * so we need to recode them
  305. */
  306. swdev = new_decode_dev(swap_area.dev);
  307. if (swdev) {
  308. offset = swap_area.offset;
  309. data->swap = swap_type_of(swdev, offset, NULL);
  310. if (data->swap < 0)
  311. error = -ENODEV;
  312. } else {
  313. data->swap = -1;
  314. error = -EINVAL;
  315. }
  316. }
  317. break;
  318. default:
  319. error = -ENOTTY;
  320. }
  321. mutex_unlock(&pm_mutex);
  322. return error;
  323. }
  324. #ifdef CONFIG_COMPAT
  325. struct compat_resume_swap_area {
  326. compat_loff_t offset;
  327. u32 dev;
  328. } __packed;
  329. static long
  330. snapshot_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  331. {
  332. BUILD_BUG_ON(sizeof(loff_t) != sizeof(compat_loff_t));
  333. switch (cmd) {
  334. case SNAPSHOT_GET_IMAGE_SIZE:
  335. case SNAPSHOT_AVAIL_SWAP_SIZE:
  336. case SNAPSHOT_ALLOC_SWAP_PAGE: {
  337. compat_loff_t __user *uoffset = compat_ptr(arg);
  338. loff_t offset;
  339. mm_segment_t old_fs;
  340. int err;
  341. old_fs = get_fs();
  342. set_fs(KERNEL_DS);
  343. err = snapshot_ioctl(file, cmd, (unsigned long) &offset);
  344. set_fs(old_fs);
  345. if (!err && put_user(offset, uoffset))
  346. err = -EFAULT;
  347. return err;
  348. }
  349. case SNAPSHOT_CREATE_IMAGE:
  350. return snapshot_ioctl(file, cmd,
  351. (unsigned long) compat_ptr(arg));
  352. case SNAPSHOT_SET_SWAP_AREA: {
  353. struct compat_resume_swap_area __user *u_swap_area =
  354. compat_ptr(arg);
  355. struct resume_swap_area swap_area;
  356. mm_segment_t old_fs;
  357. int err;
  358. err = get_user(swap_area.offset, &u_swap_area->offset);
  359. err |= get_user(swap_area.dev, &u_swap_area->dev);
  360. if (err)
  361. return -EFAULT;
  362. old_fs = get_fs();
  363. set_fs(KERNEL_DS);
  364. err = snapshot_ioctl(file, SNAPSHOT_SET_SWAP_AREA,
  365. (unsigned long) &swap_area);
  366. set_fs(old_fs);
  367. return err;
  368. }
  369. default:
  370. return snapshot_ioctl(file, cmd, arg);
  371. }
  372. }
  373. #endif /* CONFIG_COMPAT */
  374. static const struct file_operations snapshot_fops = {
  375. .open = snapshot_open,
  376. .release = snapshot_release,
  377. .read = snapshot_read,
  378. .write = snapshot_write,
  379. .llseek = no_llseek,
  380. .unlocked_ioctl = snapshot_ioctl,
  381. #ifdef CONFIG_COMPAT
  382. .compat_ioctl = snapshot_compat_ioctl,
  383. #endif
  384. };
  385. static struct miscdevice snapshot_device = {
  386. .minor = SNAPSHOT_MINOR,
  387. .name = "snapshot",
  388. .fops = &snapshot_fops,
  389. };
  390. static int __init snapshot_device_init(void)
  391. {
  392. return misc_register(&snapshot_device);
  393. };
  394. device_initcall(snapshot_device_init);