swap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * linux/kernel/power/swap.c
  3. *
  4. * This file provides functions for reading the suspend image from
  5. * and writing it to a swap partition.
  6. *
  7. * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
  8. * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
  9. *
  10. * This file is released under the GPLv2.
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/smp_lock.h>
  15. #include <linux/file.h>
  16. #include <linux/utsname.h>
  17. #include <linux/version.h>
  18. #include <linux/delay.h>
  19. #include <linux/bitops.h>
  20. #include <linux/genhd.h>
  21. #include <linux/device.h>
  22. #include <linux/buffer_head.h>
  23. #include <linux/bio.h>
  24. #include <linux/swap.h>
  25. #include <linux/swapops.h>
  26. #include <linux/pm.h>
  27. #include "power.h"
  28. extern char resume_file[];
  29. #define SWSUSP_SIG "S1SUSPEND"
  30. static struct swsusp_header {
  31. char reserved[PAGE_SIZE - 20 - sizeof(swp_entry_t)];
  32. swp_entry_t image;
  33. char orig_sig[10];
  34. char sig[10];
  35. } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
  36. /*
  37. * Saving part...
  38. */
  39. static unsigned short root_swap = 0xffff;
  40. static int mark_swapfiles(swp_entry_t start)
  41. {
  42. int error;
  43. rw_swap_page_sync(READ,
  44. swp_entry(root_swap, 0),
  45. virt_to_page((unsigned long)&swsusp_header));
  46. if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
  47. !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
  48. memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
  49. memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
  50. swsusp_header.image = start;
  51. error = rw_swap_page_sync(WRITE,
  52. swp_entry(root_swap, 0),
  53. virt_to_page((unsigned long)
  54. &swsusp_header));
  55. } else {
  56. pr_debug("swsusp: Partition is not swap space.\n");
  57. error = -ENODEV;
  58. }
  59. return error;
  60. }
  61. /**
  62. * swsusp_swap_check - check if the resume device is a swap device
  63. * and get its index (if so)
  64. */
  65. static int swsusp_swap_check(void) /* This is called before saving image */
  66. {
  67. int res = swap_type_of(swsusp_resume_device);
  68. if (res >= 0) {
  69. root_swap = res;
  70. return 0;
  71. }
  72. return res;
  73. }
  74. /**
  75. * write_page - Write one page to given swap location.
  76. * @buf: Address we're writing.
  77. * @offset: Offset of the swap page we're writing to.
  78. */
  79. static int write_page(void *buf, unsigned long offset)
  80. {
  81. swp_entry_t entry;
  82. int error = -ENOSPC;
  83. if (offset) {
  84. entry = swp_entry(root_swap, offset);
  85. error = rw_swap_page_sync(WRITE, entry, virt_to_page(buf));
  86. }
  87. return error;
  88. }
  89. /*
  90. * The swap map is a data structure used for keeping track of each page
  91. * written to a swap partition. It consists of many swap_map_page
  92. * structures that contain each an array of MAP_PAGE_SIZE swap entries.
  93. * These structures are stored on the swap and linked together with the
  94. * help of the .next_swap member.
  95. *
  96. * The swap map is created during suspend. The swap map pages are
  97. * allocated and populated one at a time, so we only need one memory
  98. * page to set up the entire structure.
  99. *
  100. * During resume we also only need to use one swap_map_page structure
  101. * at a time.
  102. */
  103. #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(long) - 1)
  104. struct swap_map_page {
  105. unsigned long entries[MAP_PAGE_ENTRIES];
  106. unsigned long next_swap;
  107. };
  108. /**
  109. * The swap_map_handle structure is used for handling swap in
  110. * a file-alike way
  111. */
  112. struct swap_map_handle {
  113. struct swap_map_page *cur;
  114. unsigned long cur_swap;
  115. struct bitmap_page *bitmap;
  116. unsigned int k;
  117. };
  118. static void release_swap_writer(struct swap_map_handle *handle)
  119. {
  120. if (handle->cur)
  121. free_page((unsigned long)handle->cur);
  122. handle->cur = NULL;
  123. if (handle->bitmap)
  124. free_bitmap(handle->bitmap);
  125. handle->bitmap = NULL;
  126. }
  127. static int get_swap_writer(struct swap_map_handle *handle)
  128. {
  129. handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
  130. if (!handle->cur)
  131. return -ENOMEM;
  132. handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0));
  133. if (!handle->bitmap) {
  134. release_swap_writer(handle);
  135. return -ENOMEM;
  136. }
  137. handle->cur_swap = alloc_swap_page(root_swap, handle->bitmap);
  138. if (!handle->cur_swap) {
  139. release_swap_writer(handle);
  140. return -ENOSPC;
  141. }
  142. handle->k = 0;
  143. return 0;
  144. }
  145. static int swap_write_page(struct swap_map_handle *handle, void *buf)
  146. {
  147. int error;
  148. unsigned long offset;
  149. if (!handle->cur)
  150. return -EINVAL;
  151. offset = alloc_swap_page(root_swap, handle->bitmap);
  152. error = write_page(buf, offset);
  153. if (error)
  154. return error;
  155. handle->cur->entries[handle->k++] = offset;
  156. if (handle->k >= MAP_PAGE_ENTRIES) {
  157. offset = alloc_swap_page(root_swap, handle->bitmap);
  158. if (!offset)
  159. return -ENOSPC;
  160. handle->cur->next_swap = offset;
  161. error = write_page(handle->cur, handle->cur_swap);
  162. if (error)
  163. return error;
  164. memset(handle->cur, 0, PAGE_SIZE);
  165. handle->cur_swap = offset;
  166. handle->k = 0;
  167. }
  168. return 0;
  169. }
  170. static int flush_swap_writer(struct swap_map_handle *handle)
  171. {
  172. if (handle->cur && handle->cur_swap)
  173. return write_page(handle->cur, handle->cur_swap);
  174. else
  175. return -EINVAL;
  176. }
  177. /**
  178. * save_image - save the suspend image data
  179. */
  180. static int save_image(struct swap_map_handle *handle,
  181. struct snapshot_handle *snapshot,
  182. unsigned int nr_pages)
  183. {
  184. unsigned int m;
  185. int ret;
  186. int error = 0;
  187. printk("Saving image data pages (%u pages) ... ", nr_pages);
  188. m = nr_pages / 100;
  189. if (!m)
  190. m = 1;
  191. nr_pages = 0;
  192. do {
  193. ret = snapshot_read_next(snapshot, PAGE_SIZE);
  194. if (ret > 0) {
  195. error = swap_write_page(handle, data_of(*snapshot));
  196. if (error)
  197. break;
  198. if (!(nr_pages % m))
  199. printk("\b\b\b\b%3d%%", nr_pages / m);
  200. nr_pages++;
  201. }
  202. } while (ret > 0);
  203. if (!error)
  204. printk("\b\b\b\bdone\n");
  205. return error;
  206. }
  207. /**
  208. * enough_swap - Make sure we have enough swap to save the image.
  209. *
  210. * Returns TRUE or FALSE after checking the total amount of swap
  211. * space avaiable from the resume partition.
  212. */
  213. static int enough_swap(unsigned int nr_pages)
  214. {
  215. unsigned int free_swap = count_swap_pages(root_swap, 1);
  216. pr_debug("swsusp: free swap pages: %u\n", free_swap);
  217. return free_swap > (nr_pages + PAGES_FOR_IO +
  218. (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
  219. }
  220. /**
  221. * swsusp_write - Write entire image and metadata.
  222. *
  223. * It is important _NOT_ to umount filesystems at this point. We want
  224. * them synced (in case something goes wrong) but we DO not want to mark
  225. * filesystem clean: it is not. (And it does not matter, if we resume
  226. * correctly, we'll mark system clean, anyway.)
  227. */
  228. int swsusp_write(void)
  229. {
  230. struct swap_map_handle handle;
  231. struct snapshot_handle snapshot;
  232. struct swsusp_info *header;
  233. int error;
  234. if ((error = swsusp_swap_check())) {
  235. printk(KERN_ERR "swsusp: Cannot find swap device, try swapon -a.\n");
  236. return error;
  237. }
  238. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  239. error = snapshot_read_next(&snapshot, PAGE_SIZE);
  240. if (error < PAGE_SIZE)
  241. return error < 0 ? error : -EFAULT;
  242. header = (struct swsusp_info *)data_of(snapshot);
  243. if (!enough_swap(header->pages)) {
  244. printk(KERN_ERR "swsusp: Not enough free swap\n");
  245. return -ENOSPC;
  246. }
  247. error = get_swap_writer(&handle);
  248. if (!error) {
  249. unsigned long start = handle.cur_swap;
  250. error = swap_write_page(&handle, header);
  251. if (!error)
  252. error = save_image(&handle, &snapshot,
  253. header->pages - 1);
  254. if (!error) {
  255. flush_swap_writer(&handle);
  256. printk("S");
  257. error = mark_swapfiles(swp_entry(root_swap, start));
  258. printk("|\n");
  259. }
  260. }
  261. if (error)
  262. free_all_swap_pages(root_swap, handle.bitmap);
  263. release_swap_writer(&handle);
  264. return error;
  265. }
  266. /*
  267. * Using bio to read from swap.
  268. * This code requires a bit more work than just using buffer heads
  269. * but, it is the recommended way for 2.5/2.6.
  270. * The following are to signal the beginning and end of I/O. Bios
  271. * finish asynchronously, while we want them to happen synchronously.
  272. * A simple atomic_t, and a wait loop take care of this problem.
  273. */
  274. static atomic_t io_done = ATOMIC_INIT(0);
  275. static int end_io(struct bio *bio, unsigned int num, int err)
  276. {
  277. if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
  278. printk(KERN_ERR "I/O error reading swsusp image.\n");
  279. return -EIO;
  280. }
  281. atomic_set(&io_done, 0);
  282. return 0;
  283. }
  284. static struct block_device *resume_bdev;
  285. /**
  286. * submit - submit BIO request.
  287. * @rw: READ or WRITE.
  288. * @off physical offset of page.
  289. * @page: page we're reading or writing.
  290. *
  291. * Straight from the textbook - allocate and initialize the bio.
  292. * If we're writing, make sure the page is marked as dirty.
  293. * Then submit it and wait.
  294. */
  295. static int submit(int rw, pgoff_t page_off, void *page)
  296. {
  297. int error = 0;
  298. struct bio *bio;
  299. bio = bio_alloc(GFP_ATOMIC, 1);
  300. if (!bio)
  301. return -ENOMEM;
  302. bio->bi_sector = page_off * (PAGE_SIZE >> 9);
  303. bio->bi_bdev = resume_bdev;
  304. bio->bi_end_io = end_io;
  305. if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
  306. printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
  307. error = -EFAULT;
  308. goto Done;
  309. }
  310. atomic_set(&io_done, 1);
  311. submit_bio(rw | (1 << BIO_RW_SYNC), bio);
  312. while (atomic_read(&io_done))
  313. yield();
  314. if (rw == READ)
  315. bio_set_pages_dirty(bio);
  316. Done:
  317. bio_put(bio);
  318. return error;
  319. }
  320. static int bio_read_page(pgoff_t page_off, void *page)
  321. {
  322. return submit(READ, page_off, page);
  323. }
  324. static int bio_write_page(pgoff_t page_off, void *page)
  325. {
  326. return submit(WRITE, page_off, page);
  327. }
  328. /**
  329. * The following functions allow us to read data using a swap map
  330. * in a file-alike way
  331. */
  332. static void release_swap_reader(struct swap_map_handle *handle)
  333. {
  334. if (handle->cur)
  335. free_page((unsigned long)handle->cur);
  336. handle->cur = NULL;
  337. }
  338. static int get_swap_reader(struct swap_map_handle *handle,
  339. swp_entry_t start)
  340. {
  341. int error;
  342. if (!swp_offset(start))
  343. return -EINVAL;
  344. handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
  345. if (!handle->cur)
  346. return -ENOMEM;
  347. error = bio_read_page(swp_offset(start), handle->cur);
  348. if (error) {
  349. release_swap_reader(handle);
  350. return error;
  351. }
  352. handle->k = 0;
  353. return 0;
  354. }
  355. static int swap_read_page(struct swap_map_handle *handle, void *buf)
  356. {
  357. unsigned long offset;
  358. int error;
  359. if (!handle->cur)
  360. return -EINVAL;
  361. offset = handle->cur->entries[handle->k];
  362. if (!offset)
  363. return -EFAULT;
  364. error = bio_read_page(offset, buf);
  365. if (error)
  366. return error;
  367. if (++handle->k >= MAP_PAGE_ENTRIES) {
  368. handle->k = 0;
  369. offset = handle->cur->next_swap;
  370. if (!offset)
  371. release_swap_reader(handle);
  372. else
  373. error = bio_read_page(offset, handle->cur);
  374. }
  375. return error;
  376. }
  377. /**
  378. * load_image - load the image using the swap map handle
  379. * @handle and the snapshot handle @snapshot
  380. * (assume there are @nr_pages pages to load)
  381. */
  382. static int load_image(struct swap_map_handle *handle,
  383. struct snapshot_handle *snapshot,
  384. unsigned int nr_pages)
  385. {
  386. unsigned int m;
  387. int ret;
  388. int error = 0;
  389. printk("Loading image data pages (%u pages) ... ", nr_pages);
  390. m = nr_pages / 100;
  391. if (!m)
  392. m = 1;
  393. nr_pages = 0;
  394. do {
  395. ret = snapshot_write_next(snapshot, PAGE_SIZE);
  396. if (ret > 0) {
  397. error = swap_read_page(handle, data_of(*snapshot));
  398. if (error)
  399. break;
  400. if (!(nr_pages % m))
  401. printk("\b\b\b\b%3d%%", nr_pages / m);
  402. nr_pages++;
  403. }
  404. } while (ret > 0);
  405. if (!error) {
  406. printk("\b\b\b\bdone\n");
  407. if (!snapshot_image_loaded(snapshot))
  408. error = -ENODATA;
  409. }
  410. return error;
  411. }
  412. int swsusp_read(void)
  413. {
  414. int error;
  415. struct swap_map_handle handle;
  416. struct snapshot_handle snapshot;
  417. struct swsusp_info *header;
  418. if (IS_ERR(resume_bdev)) {
  419. pr_debug("swsusp: block device not initialised\n");
  420. return PTR_ERR(resume_bdev);
  421. }
  422. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  423. error = snapshot_write_next(&snapshot, PAGE_SIZE);
  424. if (error < PAGE_SIZE)
  425. return error < 0 ? error : -EFAULT;
  426. header = (struct swsusp_info *)data_of(snapshot);
  427. error = get_swap_reader(&handle, swsusp_header.image);
  428. if (!error)
  429. error = swap_read_page(&handle, header);
  430. if (!error)
  431. error = load_image(&handle, &snapshot, header->pages - 1);
  432. release_swap_reader(&handle);
  433. blkdev_put(resume_bdev);
  434. if (!error)
  435. pr_debug("swsusp: Reading resume file was successful\n");
  436. else
  437. pr_debug("swsusp: Error %d resuming\n", error);
  438. return error;
  439. }
  440. /**
  441. * swsusp_check - Check for swsusp signature in the resume device
  442. */
  443. int swsusp_check(void)
  444. {
  445. int error;
  446. resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
  447. if (!IS_ERR(resume_bdev)) {
  448. set_blocksize(resume_bdev, PAGE_SIZE);
  449. memset(&swsusp_header, 0, sizeof(swsusp_header));
  450. if ((error = bio_read_page(0, &swsusp_header)))
  451. return error;
  452. if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
  453. memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
  454. /* Reset swap signature now */
  455. error = bio_write_page(0, &swsusp_header);
  456. } else {
  457. return -EINVAL;
  458. }
  459. if (error)
  460. blkdev_put(resume_bdev);
  461. else
  462. pr_debug("swsusp: Signature found, resuming\n");
  463. } else {
  464. error = PTR_ERR(resume_bdev);
  465. }
  466. if (error)
  467. pr_debug("swsusp: Error %d check for resume file\n", error);
  468. return error;
  469. }
  470. /**
  471. * swsusp_close - close swap device.
  472. */
  473. void swsusp_close(void)
  474. {
  475. if (IS_ERR(resume_bdev)) {
  476. pr_debug("swsusp: block device not initialised\n");
  477. return;
  478. }
  479. blkdev_put(resume_bdev);
  480. }