user.c 10 KB

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