user.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 <scsi/scsi_scan.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. /* Hibernating. The image device should be accessible. */
  82. data->swap = swsusp_resume_device ?
  83. swap_type_of(swsusp_resume_device, 0, NULL) : -1;
  84. data->mode = O_RDONLY;
  85. error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
  86. if (error)
  87. pm_notifier_call_chain(PM_POST_HIBERNATION);
  88. } else {
  89. /*
  90. * Resuming. We may need to wait for the image device to
  91. * appear.
  92. */
  93. wait_for_device_probe();
  94. scsi_complete_async_scans();
  95. data->swap = -1;
  96. data->mode = O_WRONLY;
  97. error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
  98. if (error)
  99. pm_notifier_call_chain(PM_POST_RESTORE);
  100. }
  101. if (error)
  102. atomic_inc(&snapshot_device_available);
  103. data->frozen = 0;
  104. data->ready = 0;
  105. data->platform_support = 0;
  106. Unlock:
  107. mutex_unlock(&pm_mutex);
  108. return error;
  109. }
  110. static int snapshot_release(struct inode *inode, struct file *filp)
  111. {
  112. struct snapshot_data *data;
  113. mutex_lock(&pm_mutex);
  114. swsusp_free();
  115. free_basic_memory_bitmaps();
  116. data = filp->private_data;
  117. free_all_swap_pages(data->swap);
  118. if (data->frozen)
  119. thaw_processes();
  120. pm_notifier_call_chain(data->mode == O_WRONLY ?
  121. PM_POST_HIBERNATION : PM_POST_RESTORE);
  122. atomic_inc(&snapshot_device_available);
  123. mutex_unlock(&pm_mutex);
  124. return 0;
  125. }
  126. static ssize_t snapshot_read(struct file *filp, char __user *buf,
  127. size_t count, loff_t *offp)
  128. {
  129. struct snapshot_data *data;
  130. ssize_t res;
  131. mutex_lock(&pm_mutex);
  132. data = filp->private_data;
  133. if (!data->ready) {
  134. res = -ENODATA;
  135. goto Unlock;
  136. }
  137. res = snapshot_read_next(&data->handle, count);
  138. if (res > 0) {
  139. if (copy_to_user(buf, data_of(data->handle), res))
  140. res = -EFAULT;
  141. else
  142. *offp = data->handle.offset;
  143. }
  144. Unlock:
  145. mutex_unlock(&pm_mutex);
  146. return res;
  147. }
  148. static ssize_t snapshot_write(struct file *filp, const char __user *buf,
  149. size_t count, loff_t *offp)
  150. {
  151. struct snapshot_data *data;
  152. ssize_t res;
  153. mutex_lock(&pm_mutex);
  154. data = filp->private_data;
  155. res = snapshot_write_next(&data->handle, count);
  156. if (res > 0) {
  157. if (copy_from_user(data_of(data->handle), buf, res))
  158. res = -EFAULT;
  159. else
  160. *offp = data->handle.offset;
  161. }
  162. mutex_unlock(&pm_mutex);
  163. return res;
  164. }
  165. static void snapshot_deprecated_ioctl(unsigned int cmd)
  166. {
  167. if (printk_ratelimit())
  168. printk(KERN_NOTICE "%pf: ioctl '%.8x' is deprecated and will "
  169. "be removed soon, update your suspend-to-disk "
  170. "utilities\n",
  171. __builtin_return_address(0), cmd);
  172. }
  173. static long snapshot_ioctl(struct file *filp, unsigned int cmd,
  174. unsigned long arg)
  175. {
  176. int error = 0;
  177. struct snapshot_data *data;
  178. loff_t size;
  179. sector_t offset;
  180. if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
  181. return -ENOTTY;
  182. if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
  183. return -ENOTTY;
  184. if (!capable(CAP_SYS_ADMIN))
  185. return -EPERM;
  186. if (!mutex_trylock(&pm_mutex))
  187. return -EBUSY;
  188. data = filp->private_data;
  189. switch (cmd) {
  190. case SNAPSHOT_FREEZE:
  191. if (data->frozen)
  192. break;
  193. printk("Syncing filesystems ... ");
  194. sys_sync();
  195. printk("done.\n");
  196. error = usermodehelper_disable();
  197. if (error)
  198. break;
  199. error = freeze_processes();
  200. if (error) {
  201. thaw_processes();
  202. usermodehelper_enable();
  203. }
  204. if (!error)
  205. data->frozen = 1;
  206. break;
  207. case SNAPSHOT_UNFREEZE:
  208. if (!data->frozen || data->ready)
  209. break;
  210. thaw_processes();
  211. usermodehelper_enable();
  212. data->frozen = 0;
  213. break;
  214. case SNAPSHOT_ATOMIC_SNAPSHOT:
  215. snapshot_deprecated_ioctl(cmd);
  216. case SNAPSHOT_CREATE_IMAGE:
  217. if (data->mode != O_RDONLY || !data->frozen || data->ready) {
  218. error = -EPERM;
  219. break;
  220. }
  221. error = hibernation_snapshot(data->platform_support);
  222. if (!error)
  223. error = put_user(in_suspend, (int __user *)arg);
  224. if (!error)
  225. data->ready = 1;
  226. break;
  227. case SNAPSHOT_ATOMIC_RESTORE:
  228. snapshot_write_finalize(&data->handle);
  229. if (data->mode != O_WRONLY || !data->frozen ||
  230. !snapshot_image_loaded(&data->handle)) {
  231. error = -EPERM;
  232. break;
  233. }
  234. error = hibernation_restore(data->platform_support);
  235. break;
  236. case SNAPSHOT_FREE:
  237. swsusp_free();
  238. memset(&data->handle, 0, sizeof(struct snapshot_handle));
  239. data->ready = 0;
  240. break;
  241. case SNAPSHOT_SET_IMAGE_SIZE:
  242. snapshot_deprecated_ioctl(cmd);
  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:
  256. snapshot_deprecated_ioctl(cmd);
  257. case SNAPSHOT_AVAIL_SWAP_SIZE:
  258. size = count_swap_pages(data->swap, 1);
  259. size <<= PAGE_SHIFT;
  260. error = put_user(size, (loff_t __user *)arg);
  261. break;
  262. case SNAPSHOT_GET_SWAP_PAGE:
  263. snapshot_deprecated_ioctl(cmd);
  264. case SNAPSHOT_ALLOC_SWAP_PAGE:
  265. if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
  266. error = -ENODEV;
  267. break;
  268. }
  269. offset = alloc_swapdev_block(data->swap);
  270. if (offset) {
  271. offset <<= PAGE_SHIFT;
  272. error = put_user(offset, (loff_t __user *)arg);
  273. } else {
  274. error = -ENOSPC;
  275. }
  276. break;
  277. case SNAPSHOT_FREE_SWAP_PAGES:
  278. if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
  279. error = -ENODEV;
  280. break;
  281. }
  282. free_all_swap_pages(data->swap);
  283. break;
  284. case SNAPSHOT_SET_SWAP_FILE: /* This ioctl is deprecated */
  285. snapshot_deprecated_ioctl(cmd);
  286. if (!swsusp_swap_in_use()) {
  287. /*
  288. * User space encodes device types as two-byte values,
  289. * so we need to recode them
  290. */
  291. if (old_decode_dev(arg)) {
  292. data->swap = swap_type_of(old_decode_dev(arg),
  293. 0, NULL);
  294. if (data->swap < 0)
  295. error = -ENODEV;
  296. } else {
  297. data->swap = -1;
  298. error = -EINVAL;
  299. }
  300. } else {
  301. error = -EPERM;
  302. }
  303. break;
  304. case SNAPSHOT_S2RAM:
  305. if (!data->frozen) {
  306. error = -EPERM;
  307. break;
  308. }
  309. /*
  310. * Tasks are frozen and the notifiers have been called with
  311. * PM_HIBERNATION_PREPARE
  312. */
  313. error = suspend_devices_and_enter(PM_SUSPEND_MEM);
  314. break;
  315. case SNAPSHOT_PLATFORM_SUPPORT:
  316. data->platform_support = !!arg;
  317. break;
  318. case SNAPSHOT_POWER_OFF:
  319. if (data->platform_support)
  320. error = hibernation_platform_enter();
  321. break;
  322. case SNAPSHOT_PMOPS: /* This ioctl is deprecated */
  323. snapshot_deprecated_ioctl(cmd);
  324. error = -EINVAL;
  325. switch (arg) {
  326. case PMOPS_PREPARE:
  327. data->platform_support = 1;
  328. error = 0;
  329. break;
  330. case PMOPS_ENTER:
  331. if (data->platform_support)
  332. error = hibernation_platform_enter();
  333. break;
  334. case PMOPS_FINISH:
  335. if (data->platform_support)
  336. error = 0;
  337. break;
  338. default:
  339. printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
  340. }
  341. break;
  342. case SNAPSHOT_SET_SWAP_AREA:
  343. if (swsusp_swap_in_use()) {
  344. error = -EPERM;
  345. } else {
  346. struct resume_swap_area swap_area;
  347. dev_t swdev;
  348. error = copy_from_user(&swap_area, (void __user *)arg,
  349. sizeof(struct resume_swap_area));
  350. if (error) {
  351. error = -EFAULT;
  352. break;
  353. }
  354. /*
  355. * User space encodes device types as two-byte values,
  356. * so we need to recode them
  357. */
  358. swdev = new_decode_dev(swap_area.dev);
  359. if (swdev) {
  360. offset = swap_area.offset;
  361. data->swap = swap_type_of(swdev, offset, NULL);
  362. if (data->swap < 0)
  363. error = -ENODEV;
  364. } else {
  365. data->swap = -1;
  366. error = -EINVAL;
  367. }
  368. }
  369. break;
  370. default:
  371. error = -ENOTTY;
  372. }
  373. mutex_unlock(&pm_mutex);
  374. return error;
  375. }
  376. static const struct file_operations snapshot_fops = {
  377. .open = snapshot_open,
  378. .release = snapshot_release,
  379. .read = snapshot_read,
  380. .write = snapshot_write,
  381. .llseek = no_llseek,
  382. .unlocked_ioctl = snapshot_ioctl,
  383. };
  384. static struct miscdevice snapshot_device = {
  385. .minor = SNAPSHOT_MINOR,
  386. .name = "snapshot",
  387. .fops = &snapshot_fops,
  388. };
  389. static int __init snapshot_device_init(void)
  390. {
  391. return misc_register(&snapshot_device);
  392. };
  393. device_initcall(snapshot_device_init);