user.c 9.6 KB

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