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