user.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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/console.h>
  23. #include <linux/cpu.h>
  24. #include <linux/freezer.h>
  25. #include <linux/smp_lock.h>
  26. #include <asm/uaccess.h>
  27. #include "power.h"
  28. /*
  29. * NOTE: The SNAPSHOT_SET_SWAP_FILE and SNAPSHOT_PMOPS ioctls are obsolete and
  30. * will be removed in the future. They are only preserved here for
  31. * compatibility with existing userland utilities.
  32. */
  33. #define SNAPSHOT_SET_SWAP_FILE _IOW(SNAPSHOT_IOC_MAGIC, 10, unsigned int)
  34. #define SNAPSHOT_PMOPS _IOW(SNAPSHOT_IOC_MAGIC, 12, unsigned int)
  35. #define PMOPS_PREPARE 1
  36. #define PMOPS_ENTER 2
  37. #define PMOPS_FINISH 3
  38. /*
  39. * NOTE: The following ioctl definitions are wrong and have been replaced with
  40. * correct ones. They are only preserved here for compatibility with existing
  41. * userland utilities and will be removed in the future.
  42. */
  43. #define SNAPSHOT_ATOMIC_SNAPSHOT _IOW(SNAPSHOT_IOC_MAGIC, 3, void *)
  44. #define SNAPSHOT_SET_IMAGE_SIZE _IOW(SNAPSHOT_IOC_MAGIC, 6, unsigned long)
  45. #define SNAPSHOT_AVAIL_SWAP _IOR(SNAPSHOT_IOC_MAGIC, 7, void *)
  46. #define SNAPSHOT_GET_SWAP_PAGE _IOR(SNAPSHOT_IOC_MAGIC, 8, void *)
  47. #define SNAPSHOT_MINOR 231
  48. static struct snapshot_data {
  49. struct snapshot_handle handle;
  50. int swap;
  51. int mode;
  52. char frozen;
  53. char ready;
  54. char platform_support;
  55. } snapshot_state;
  56. atomic_t snapshot_device_available = ATOMIC_INIT(1);
  57. static int snapshot_open(struct inode *inode, struct file *filp)
  58. {
  59. struct snapshot_data *data;
  60. int error;
  61. mutex_lock(&pm_mutex);
  62. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  63. error = -EBUSY;
  64. goto Unlock;
  65. }
  66. if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
  67. atomic_inc(&snapshot_device_available);
  68. error = -ENOSYS;
  69. goto Unlock;
  70. }
  71. if(create_basic_memory_bitmaps()) {
  72. atomic_inc(&snapshot_device_available);
  73. error = -ENOMEM;
  74. goto Unlock;
  75. }
  76. nonseekable_open(inode, filp);
  77. data = &snapshot_state;
  78. filp->private_data = data;
  79. memset(&data->handle, 0, sizeof(struct snapshot_handle));
  80. if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
  81. data->swap = swsusp_resume_device ?
  82. swap_type_of(swsusp_resume_device, 0, NULL) : -1;
  83. data->mode = O_RDONLY;
  84. error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
  85. if (error)
  86. pm_notifier_call_chain(PM_POST_RESTORE);
  87. } else {
  88. data->swap = -1;
  89. data->mode = O_WRONLY;
  90. error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
  91. if (error)
  92. pm_notifier_call_chain(PM_POST_HIBERNATION);
  93. }
  94. if (error)
  95. atomic_inc(&snapshot_device_available);
  96. data->frozen = 0;
  97. data->ready = 0;
  98. data->platform_support = 0;
  99. Unlock:
  100. mutex_unlock(&pm_mutex);
  101. return error;
  102. }
  103. static int snapshot_release(struct inode *inode, struct file *filp)
  104. {
  105. struct snapshot_data *data;
  106. mutex_lock(&pm_mutex);
  107. swsusp_free();
  108. free_basic_memory_bitmaps();
  109. data = filp->private_data;
  110. free_all_swap_pages(data->swap);
  111. if (data->frozen)
  112. thaw_processes();
  113. pm_notifier_call_chain(data->mode == O_WRONLY ?
  114. PM_POST_HIBERNATION : PM_POST_RESTORE);
  115. atomic_inc(&snapshot_device_available);
  116. mutex_unlock(&pm_mutex);
  117. return 0;
  118. }
  119. static ssize_t snapshot_read(struct file *filp, char __user *buf,
  120. size_t count, loff_t *offp)
  121. {
  122. struct snapshot_data *data;
  123. ssize_t res;
  124. mutex_lock(&pm_mutex);
  125. data = filp->private_data;
  126. if (!data->ready) {
  127. res = -ENODATA;
  128. goto Unlock;
  129. }
  130. res = snapshot_read_next(&data->handle, count);
  131. if (res > 0) {
  132. if (copy_to_user(buf, data_of(data->handle), res))
  133. res = -EFAULT;
  134. else
  135. *offp = data->handle.offset;
  136. }
  137. Unlock:
  138. mutex_unlock(&pm_mutex);
  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. mutex_lock(&pm_mutex);
  147. data = filp->private_data;
  148. res = snapshot_write_next(&data->handle, count);
  149. if (res > 0) {
  150. if (copy_from_user(data_of(data->handle), buf, res))
  151. res = -EFAULT;
  152. else
  153. *offp = data->handle.offset;
  154. }
  155. mutex_unlock(&pm_mutex);
  156. return res;
  157. }
  158. static long snapshot_ioctl(struct file *filp, unsigned int cmd,
  159. unsigned long arg)
  160. {
  161. int error = 0;
  162. struct snapshot_data *data;
  163. loff_t size;
  164. sector_t offset;
  165. if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
  166. return -ENOTTY;
  167. if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
  168. return -ENOTTY;
  169. if (!capable(CAP_SYS_ADMIN))
  170. return -EPERM;
  171. if (!mutex_trylock(&pm_mutex))
  172. return -EBUSY;
  173. data = filp->private_data;
  174. switch (cmd) {
  175. case SNAPSHOT_FREEZE:
  176. if (data->frozen)
  177. break;
  178. printk("Syncing filesystems ... ");
  179. sys_sync();
  180. printk("done.\n");
  181. error = usermodehelper_disable();
  182. if (error)
  183. break;
  184. error = freeze_processes();
  185. if (error) {
  186. thaw_processes();
  187. usermodehelper_enable();
  188. }
  189. if (!error)
  190. data->frozen = 1;
  191. break;
  192. case SNAPSHOT_UNFREEZE:
  193. if (!data->frozen || data->ready)
  194. break;
  195. thaw_processes();
  196. usermodehelper_enable();
  197. data->frozen = 0;
  198. break;
  199. case SNAPSHOT_CREATE_IMAGE:
  200. case SNAPSHOT_ATOMIC_SNAPSHOT:
  201. if (data->mode != O_RDONLY || !data->frozen || data->ready) {
  202. error = -EPERM;
  203. break;
  204. }
  205. error = hibernation_snapshot(data->platform_support);
  206. if (!error)
  207. error = put_user(in_suspend, (int __user *)arg);
  208. if (!error)
  209. data->ready = 1;
  210. break;
  211. case SNAPSHOT_ATOMIC_RESTORE:
  212. snapshot_write_finalize(&data->handle);
  213. if (data->mode != O_WRONLY || !data->frozen ||
  214. !snapshot_image_loaded(&data->handle)) {
  215. error = -EPERM;
  216. break;
  217. }
  218. error = hibernation_restore(data->platform_support);
  219. break;
  220. case SNAPSHOT_FREE:
  221. swsusp_free();
  222. memset(&data->handle, 0, sizeof(struct snapshot_handle));
  223. data->ready = 0;
  224. break;
  225. case SNAPSHOT_PREF_IMAGE_SIZE:
  226. case SNAPSHOT_SET_IMAGE_SIZE:
  227. image_size = arg;
  228. break;
  229. case SNAPSHOT_GET_IMAGE_SIZE:
  230. if (!data->ready) {
  231. error = -ENODATA;
  232. break;
  233. }
  234. size = snapshot_get_image_size();
  235. size <<= PAGE_SHIFT;
  236. error = put_user(size, (loff_t __user *)arg);
  237. break;
  238. case SNAPSHOT_AVAIL_SWAP_SIZE:
  239. case SNAPSHOT_AVAIL_SWAP:
  240. size = count_swap_pages(data->swap, 1);
  241. size <<= PAGE_SHIFT;
  242. error = put_user(size, (loff_t __user *)arg);
  243. break;
  244. case SNAPSHOT_ALLOC_SWAP_PAGE:
  245. case SNAPSHOT_GET_SWAP_PAGE:
  246. if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
  247. error = -ENODEV;
  248. break;
  249. }
  250. offset = alloc_swapdev_block(data->swap);
  251. if (offset) {
  252. offset <<= PAGE_SHIFT;
  253. error = put_user(offset, (loff_t __user *)arg);
  254. } else {
  255. error = -ENOSPC;
  256. }
  257. break;
  258. case SNAPSHOT_FREE_SWAP_PAGES:
  259. if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
  260. error = -ENODEV;
  261. break;
  262. }
  263. free_all_swap_pages(data->swap);
  264. break;
  265. case SNAPSHOT_SET_SWAP_FILE: /* This ioctl is deprecated */
  266. if (!swsusp_swap_in_use()) {
  267. /*
  268. * User space encodes device types as two-byte values,
  269. * so we need to recode them
  270. */
  271. if (old_decode_dev(arg)) {
  272. data->swap = swap_type_of(old_decode_dev(arg),
  273. 0, NULL);
  274. if (data->swap < 0)
  275. error = -ENODEV;
  276. } else {
  277. data->swap = -1;
  278. error = -EINVAL;
  279. }
  280. } else {
  281. error = -EPERM;
  282. }
  283. break;
  284. case SNAPSHOT_S2RAM:
  285. if (!data->frozen) {
  286. error = -EPERM;
  287. break;
  288. }
  289. /*
  290. * Tasks are frozen and the notifiers have been called with
  291. * PM_HIBERNATION_PREPARE
  292. */
  293. error = suspend_devices_and_enter(PM_SUSPEND_MEM);
  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_PMOPS: /* This ioctl is deprecated */
  303. error = -EINVAL;
  304. switch (arg) {
  305. case PMOPS_PREPARE:
  306. data->platform_support = 1;
  307. error = 0;
  308. break;
  309. case PMOPS_ENTER:
  310. if (data->platform_support)
  311. error = hibernation_platform_enter();
  312. break;
  313. case PMOPS_FINISH:
  314. if (data->platform_support)
  315. error = 0;
  316. break;
  317. default:
  318. printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
  319. }
  320. break;
  321. case SNAPSHOT_SET_SWAP_AREA:
  322. if (swsusp_swap_in_use()) {
  323. error = -EPERM;
  324. } else {
  325. struct resume_swap_area swap_area;
  326. dev_t swdev;
  327. error = copy_from_user(&swap_area, (void __user *)arg,
  328. sizeof(struct resume_swap_area));
  329. if (error) {
  330. error = -EFAULT;
  331. break;
  332. }
  333. /*
  334. * User space encodes device types as two-byte values,
  335. * so we need to recode them
  336. */
  337. swdev = old_decode_dev(swap_area.dev);
  338. if (swdev) {
  339. offset = swap_area.offset;
  340. data->swap = swap_type_of(swdev, offset, NULL);
  341. if (data->swap < 0)
  342. error = -ENODEV;
  343. } else {
  344. data->swap = -1;
  345. error = -EINVAL;
  346. }
  347. }
  348. break;
  349. default:
  350. error = -ENOTTY;
  351. }
  352. mutex_unlock(&pm_mutex);
  353. return error;
  354. }
  355. static const struct file_operations snapshot_fops = {
  356. .open = snapshot_open,
  357. .release = snapshot_release,
  358. .read = snapshot_read,
  359. .write = snapshot_write,
  360. .llseek = no_llseek,
  361. .unlocked_ioctl = snapshot_ioctl,
  362. };
  363. static struct miscdevice snapshot_device = {
  364. .minor = SNAPSHOT_MINOR,
  365. .name = "snapshot",
  366. .fops = &snapshot_fops,
  367. };
  368. static int __init snapshot_device_init(void)
  369. {
  370. return misc_register(&snapshot_device);
  371. };
  372. device_initcall(snapshot_device_init);