user.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 && !freezer_test_done)
  243. data->ready = 1;
  244. if (freezer_test_done) {
  245. freezer_test_done = false;
  246. thaw_processes();
  247. }
  248. }
  249. break;
  250. case SNAPSHOT_ATOMIC_RESTORE:
  251. snapshot_write_finalize(&data->handle);
  252. if (data->mode != O_WRONLY || !data->frozen ||
  253. !snapshot_image_loaded(&data->handle)) {
  254. error = -EPERM;
  255. break;
  256. }
  257. error = hibernation_restore(data->platform_support);
  258. break;
  259. case SNAPSHOT_FREE:
  260. swsusp_free();
  261. memset(&data->handle, 0, sizeof(struct snapshot_handle));
  262. data->ready = 0;
  263. break;
  264. case SNAPSHOT_SET_IMAGE_SIZE:
  265. snapshot_deprecated_ioctl(cmd);
  266. case SNAPSHOT_PREF_IMAGE_SIZE:
  267. image_size = arg;
  268. break;
  269. case SNAPSHOT_GET_IMAGE_SIZE:
  270. if (!data->ready) {
  271. error = -ENODATA;
  272. break;
  273. }
  274. size = snapshot_get_image_size();
  275. size <<= PAGE_SHIFT;
  276. error = put_user(size, (loff_t __user *)arg);
  277. break;
  278. case SNAPSHOT_AVAIL_SWAP:
  279. snapshot_deprecated_ioctl(cmd);
  280. case SNAPSHOT_AVAIL_SWAP_SIZE:
  281. size = count_swap_pages(data->swap, 1);
  282. size <<= PAGE_SHIFT;
  283. error = put_user(size, (loff_t __user *)arg);
  284. break;
  285. case SNAPSHOT_GET_SWAP_PAGE:
  286. snapshot_deprecated_ioctl(cmd);
  287. case SNAPSHOT_ALLOC_SWAP_PAGE:
  288. if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
  289. error = -ENODEV;
  290. break;
  291. }
  292. offset = alloc_swapdev_block(data->swap);
  293. if (offset) {
  294. offset <<= PAGE_SHIFT;
  295. error = put_user(offset, (loff_t __user *)arg);
  296. } else {
  297. error = -ENOSPC;
  298. }
  299. break;
  300. case SNAPSHOT_FREE_SWAP_PAGES:
  301. if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
  302. error = -ENODEV;
  303. break;
  304. }
  305. free_all_swap_pages(data->swap);
  306. break;
  307. case SNAPSHOT_SET_SWAP_FILE: /* This ioctl is deprecated */
  308. snapshot_deprecated_ioctl(cmd);
  309. if (!swsusp_swap_in_use()) {
  310. /*
  311. * User space encodes device types as two-byte values,
  312. * so we need to recode them
  313. */
  314. if (old_decode_dev(arg)) {
  315. data->swap = swap_type_of(old_decode_dev(arg),
  316. 0, NULL);
  317. if (data->swap < 0)
  318. error = -ENODEV;
  319. } else {
  320. data->swap = -1;
  321. error = -EINVAL;
  322. }
  323. } else {
  324. error = -EPERM;
  325. }
  326. break;
  327. case SNAPSHOT_S2RAM:
  328. if (!data->frozen) {
  329. error = -EPERM;
  330. break;
  331. }
  332. /*
  333. * Tasks are frozen and the notifiers have been called with
  334. * PM_HIBERNATION_PREPARE
  335. */
  336. error = suspend_devices_and_enter(PM_SUSPEND_MEM);
  337. data->ready = 0;
  338. break;
  339. case SNAPSHOT_PLATFORM_SUPPORT:
  340. data->platform_support = !!arg;
  341. break;
  342. case SNAPSHOT_POWER_OFF:
  343. if (data->platform_support)
  344. error = hibernation_platform_enter();
  345. break;
  346. case SNAPSHOT_PMOPS: /* This ioctl is deprecated */
  347. snapshot_deprecated_ioctl(cmd);
  348. error = -EINVAL;
  349. switch (arg) {
  350. case PMOPS_PREPARE:
  351. data->platform_support = 1;
  352. error = 0;
  353. break;
  354. case PMOPS_ENTER:
  355. if (data->platform_support)
  356. error = hibernation_platform_enter();
  357. break;
  358. case PMOPS_FINISH:
  359. if (data->platform_support)
  360. error = 0;
  361. break;
  362. default:
  363. printk(KERN_ERR "SNAPSHOT_PMOPS: invalid argument %ld\n", arg);
  364. }
  365. break;
  366. case SNAPSHOT_SET_SWAP_AREA:
  367. if (swsusp_swap_in_use()) {
  368. error = -EPERM;
  369. } else {
  370. struct resume_swap_area swap_area;
  371. dev_t swdev;
  372. error = copy_from_user(&swap_area, (void __user *)arg,
  373. sizeof(struct resume_swap_area));
  374. if (error) {
  375. error = -EFAULT;
  376. break;
  377. }
  378. /*
  379. * User space encodes device types as two-byte values,
  380. * so we need to recode them
  381. */
  382. swdev = new_decode_dev(swap_area.dev);
  383. if (swdev) {
  384. offset = swap_area.offset;
  385. data->swap = swap_type_of(swdev, offset, NULL);
  386. if (data->swap < 0)
  387. error = -ENODEV;
  388. } else {
  389. data->swap = -1;
  390. error = -EINVAL;
  391. }
  392. }
  393. break;
  394. default:
  395. error = -ENOTTY;
  396. }
  397. mutex_unlock(&pm_mutex);
  398. return error;
  399. }
  400. static const struct file_operations snapshot_fops = {
  401. .open = snapshot_open,
  402. .release = snapshot_release,
  403. .read = snapshot_read,
  404. .write = snapshot_write,
  405. .llseek = no_llseek,
  406. .unlocked_ioctl = snapshot_ioctl,
  407. };
  408. static struct miscdevice snapshot_device = {
  409. .minor = SNAPSHOT_MINOR,
  410. .name = "snapshot",
  411. .fops = &snapshot_fops,
  412. };
  413. static int __init snapshot_device_init(void)
  414. {
  415. return misc_register(&snapshot_device);
  416. };
  417. device_initcall(snapshot_device_init);