swap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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/blkdev.h>
  25. #include <linux/swap.h>
  26. #include <linux/swapops.h>
  27. #include <linux/pm.h>
  28. #include "power.h"
  29. extern char resume_file[];
  30. #define SWSUSP_SIG "S1SUSPEND"
  31. static struct swsusp_header {
  32. char reserved[PAGE_SIZE - 20 - sizeof(swp_entry_t)];
  33. swp_entry_t image;
  34. char orig_sig[10];
  35. char sig[10];
  36. } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
  37. /*
  38. * Saving part...
  39. */
  40. static unsigned short root_swap = 0xffff;
  41. static int mark_swapfiles(swp_entry_t start)
  42. {
  43. int error;
  44. rw_swap_page_sync(READ, swp_entry(root_swap, 0),
  45. virt_to_page((unsigned long)&swsusp_header), NULL);
  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, swp_entry(root_swap, 0),
  52. virt_to_page((unsigned long)&swsusp_header),
  53. NULL);
  54. } else {
  55. pr_debug("swsusp: Partition is not swap space.\n");
  56. error = -ENODEV;
  57. }
  58. return error;
  59. }
  60. /**
  61. * swsusp_swap_check - check if the resume device is a swap device
  62. * and get its index (if so)
  63. */
  64. static int swsusp_swap_check(void) /* This is called before saving image */
  65. {
  66. int res = swap_type_of(swsusp_resume_device);
  67. if (res >= 0) {
  68. root_swap = res;
  69. return 0;
  70. }
  71. return res;
  72. }
  73. /**
  74. * write_page - Write one page to given swap location.
  75. * @buf: Address we're writing.
  76. * @offset: Offset of the swap page we're writing to.
  77. * @bio_chain: Link the next write BIO here
  78. */
  79. static int write_page(void *buf, unsigned long offset, struct bio **bio_chain)
  80. {
  81. swp_entry_t entry;
  82. int error = -ENOSPC;
  83. if (offset) {
  84. struct page *page = virt_to_page(buf);
  85. if (bio_chain) {
  86. /*
  87. * Whether or not we successfully allocated a copy page,
  88. * we take a ref on the page here. It gets undone in
  89. * wait_on_bio_chain().
  90. */
  91. struct page *page_copy;
  92. page_copy = alloc_page(GFP_ATOMIC);
  93. if (page_copy == NULL) {
  94. WARN_ON_ONCE(1);
  95. bio_chain = NULL; /* Go synchronous */
  96. get_page(page);
  97. } else {
  98. memcpy(page_address(page_copy),
  99. page_address(page), PAGE_SIZE);
  100. page = page_copy;
  101. }
  102. }
  103. entry = swp_entry(root_swap, offset);
  104. error = rw_swap_page_sync(WRITE, entry, page, bio_chain);
  105. }
  106. return error;
  107. }
  108. /*
  109. * The swap map is a data structure used for keeping track of each page
  110. * written to a swap partition. It consists of many swap_map_page
  111. * structures that contain each an array of MAP_PAGE_SIZE swap entries.
  112. * These structures are stored on the swap and linked together with the
  113. * help of the .next_swap member.
  114. *
  115. * The swap map is created during suspend. The swap map pages are
  116. * allocated and populated one at a time, so we only need one memory
  117. * page to set up the entire structure.
  118. *
  119. * During resume we also only need to use one swap_map_page structure
  120. * at a time.
  121. */
  122. #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(long) - 1)
  123. struct swap_map_page {
  124. unsigned long entries[MAP_PAGE_ENTRIES];
  125. unsigned long next_swap;
  126. };
  127. /**
  128. * The swap_map_handle structure is used for handling swap in
  129. * a file-alike way
  130. */
  131. struct swap_map_handle {
  132. struct swap_map_page *cur;
  133. unsigned long cur_swap;
  134. struct bitmap_page *bitmap;
  135. unsigned int k;
  136. };
  137. static void release_swap_writer(struct swap_map_handle *handle)
  138. {
  139. if (handle->cur)
  140. free_page((unsigned long)handle->cur);
  141. handle->cur = NULL;
  142. if (handle->bitmap)
  143. free_bitmap(handle->bitmap);
  144. handle->bitmap = NULL;
  145. }
  146. static void show_speed(struct timeval *start, struct timeval *stop,
  147. unsigned nr_pages, char *msg)
  148. {
  149. s64 elapsed_centisecs64;
  150. int centisecs;
  151. int k;
  152. int kps;
  153. elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
  154. do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
  155. centisecs = elapsed_centisecs64;
  156. if (centisecs == 0)
  157. centisecs = 1; /* avoid div-by-zero */
  158. k = nr_pages * (PAGE_SIZE / 1024);
  159. kps = (k * 100) / centisecs;
  160. printk("%s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n", msg, k,
  161. centisecs / 100, centisecs % 100,
  162. kps / 1000, (kps % 1000) / 10);
  163. }
  164. static int get_swap_writer(struct swap_map_handle *handle)
  165. {
  166. handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
  167. if (!handle->cur)
  168. return -ENOMEM;
  169. handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0));
  170. if (!handle->bitmap) {
  171. release_swap_writer(handle);
  172. return -ENOMEM;
  173. }
  174. handle->cur_swap = alloc_swap_page(root_swap, handle->bitmap);
  175. if (!handle->cur_swap) {
  176. release_swap_writer(handle);
  177. return -ENOSPC;
  178. }
  179. handle->k = 0;
  180. return 0;
  181. }
  182. static int wait_on_bio_chain(struct bio **bio_chain)
  183. {
  184. struct bio *bio;
  185. struct bio *next_bio;
  186. int ret = 0;
  187. if (bio_chain == NULL)
  188. return 0;
  189. bio = *bio_chain;
  190. if (bio == NULL)
  191. return 0;
  192. while (bio) {
  193. struct page *page;
  194. next_bio = bio->bi_private;
  195. page = bio->bi_io_vec[0].bv_page;
  196. wait_on_page_locked(page);
  197. if (!PageUptodate(page) || PageError(page))
  198. ret = -EIO;
  199. put_page(page);
  200. bio_put(bio);
  201. bio = next_bio;
  202. }
  203. *bio_chain = NULL;
  204. return ret;
  205. }
  206. static int swap_write_page(struct swap_map_handle *handle, void *buf,
  207. struct bio **bio_chain)
  208. {
  209. int error = 0;
  210. unsigned long offset;
  211. if (!handle->cur)
  212. return -EINVAL;
  213. offset = alloc_swap_page(root_swap, handle->bitmap);
  214. error = write_page(buf, offset, bio_chain);
  215. if (error)
  216. return error;
  217. handle->cur->entries[handle->k++] = offset;
  218. if (handle->k >= MAP_PAGE_ENTRIES) {
  219. error = wait_on_bio_chain(bio_chain);
  220. if (error)
  221. goto out;
  222. offset = alloc_swap_page(root_swap, handle->bitmap);
  223. if (!offset)
  224. return -ENOSPC;
  225. handle->cur->next_swap = offset;
  226. error = write_page(handle->cur, handle->cur_swap, NULL);
  227. if (error)
  228. goto out;
  229. memset(handle->cur, 0, PAGE_SIZE);
  230. handle->cur_swap = offset;
  231. handle->k = 0;
  232. }
  233. out:
  234. return error;
  235. }
  236. static int flush_swap_writer(struct swap_map_handle *handle)
  237. {
  238. if (handle->cur && handle->cur_swap)
  239. return write_page(handle->cur, handle->cur_swap, NULL);
  240. else
  241. return -EINVAL;
  242. }
  243. /**
  244. * save_image - save the suspend image data
  245. */
  246. static int save_image(struct swap_map_handle *handle,
  247. struct snapshot_handle *snapshot,
  248. unsigned int nr_to_write)
  249. {
  250. unsigned int m;
  251. int ret;
  252. int error = 0;
  253. int nr_pages;
  254. int err2;
  255. struct bio *bio;
  256. struct timeval start;
  257. struct timeval stop;
  258. printk("Saving image data pages (%u pages) ... ", nr_to_write);
  259. m = nr_to_write / 100;
  260. if (!m)
  261. m = 1;
  262. nr_pages = 0;
  263. bio = NULL;
  264. do_gettimeofday(&start);
  265. do {
  266. ret = snapshot_read_next(snapshot, PAGE_SIZE);
  267. if (ret > 0) {
  268. error = swap_write_page(handle, data_of(*snapshot),
  269. &bio);
  270. if (error)
  271. break;
  272. if (!(nr_pages % m))
  273. printk("\b\b\b\b%3d%%", nr_pages / m);
  274. nr_pages++;
  275. }
  276. } while (ret > 0);
  277. err2 = wait_on_bio_chain(&bio);
  278. do_gettimeofday(&stop);
  279. if (!error)
  280. error = err2;
  281. if (!error)
  282. printk("\b\b\b\bdone\n");
  283. show_speed(&start, &stop, nr_to_write, "Wrote");
  284. return error;
  285. }
  286. /**
  287. * enough_swap - Make sure we have enough swap to save the image.
  288. *
  289. * Returns TRUE or FALSE after checking the total amount of swap
  290. * space avaiable from the resume partition.
  291. */
  292. static int enough_swap(unsigned int nr_pages)
  293. {
  294. unsigned int free_swap = count_swap_pages(root_swap, 1);
  295. pr_debug("swsusp: free swap pages: %u\n", free_swap);
  296. return free_swap > nr_pages + PAGES_FOR_IO;
  297. }
  298. /**
  299. * swsusp_write - Write entire image and metadata.
  300. *
  301. * It is important _NOT_ to umount filesystems at this point. We want
  302. * them synced (in case something goes wrong) but we DO not want to mark
  303. * filesystem clean: it is not. (And it does not matter, if we resume
  304. * correctly, we'll mark system clean, anyway.)
  305. */
  306. int swsusp_write(void)
  307. {
  308. struct swap_map_handle handle;
  309. struct snapshot_handle snapshot;
  310. struct swsusp_info *header;
  311. int error;
  312. if ((error = swsusp_swap_check())) {
  313. printk(KERN_ERR "swsusp: Cannot find swap device, try "
  314. "swapon -a.\n");
  315. return error;
  316. }
  317. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  318. error = snapshot_read_next(&snapshot, PAGE_SIZE);
  319. if (error < PAGE_SIZE)
  320. return error < 0 ? error : -EFAULT;
  321. header = (struct swsusp_info *)data_of(snapshot);
  322. if (!enough_swap(header->pages)) {
  323. printk(KERN_ERR "swsusp: Not enough free swap\n");
  324. return -ENOSPC;
  325. }
  326. error = get_swap_writer(&handle);
  327. if (!error) {
  328. unsigned long start = handle.cur_swap;
  329. error = swap_write_page(&handle, header, NULL);
  330. if (!error)
  331. error = save_image(&handle, &snapshot,
  332. header->pages - 1);
  333. if (!error) {
  334. flush_swap_writer(&handle);
  335. printk("S");
  336. error = mark_swapfiles(swp_entry(root_swap, start));
  337. printk("|\n");
  338. }
  339. }
  340. if (error)
  341. free_all_swap_pages(root_swap, handle.bitmap);
  342. release_swap_writer(&handle);
  343. return error;
  344. }
  345. static struct block_device *resume_bdev;
  346. /**
  347. * submit - submit BIO request.
  348. * @rw: READ or WRITE.
  349. * @off physical offset of page.
  350. * @page: page we're reading or writing.
  351. * @bio_chain: list of pending biod (for async reading)
  352. *
  353. * Straight from the textbook - allocate and initialize the bio.
  354. * If we're reading, make sure the page is marked as dirty.
  355. * Then submit it and, if @bio_chain == NULL, wait.
  356. */
  357. static int submit(int rw, pgoff_t page_off, struct page *page,
  358. struct bio **bio_chain)
  359. {
  360. struct bio *bio;
  361. bio = bio_alloc(GFP_ATOMIC, 1);
  362. if (!bio)
  363. return -ENOMEM;
  364. bio->bi_sector = page_off * (PAGE_SIZE >> 9);
  365. bio->bi_bdev = resume_bdev;
  366. bio->bi_end_io = end_swap_bio_read;
  367. if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
  368. printk("swsusp: ERROR: adding page to bio at %ld\n", page_off);
  369. bio_put(bio);
  370. return -EFAULT;
  371. }
  372. lock_page(page);
  373. bio_get(bio);
  374. if (bio_chain == NULL) {
  375. submit_bio(rw | (1 << BIO_RW_SYNC), bio);
  376. wait_on_page_locked(page);
  377. if (rw == READ)
  378. bio_set_pages_dirty(bio);
  379. bio_put(bio);
  380. } else {
  381. get_page(page);
  382. bio->bi_private = *bio_chain;
  383. *bio_chain = bio;
  384. submit_bio(rw | (1 << BIO_RW_SYNC), bio);
  385. }
  386. return 0;
  387. }
  388. static int bio_read_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
  389. {
  390. return submit(READ, page_off, virt_to_page(addr), bio_chain);
  391. }
  392. static int bio_write_page(pgoff_t page_off, void *addr)
  393. {
  394. return submit(WRITE, page_off, virt_to_page(addr), NULL);
  395. }
  396. /**
  397. * The following functions allow us to read data using a swap map
  398. * in a file-alike way
  399. */
  400. static void release_swap_reader(struct swap_map_handle *handle)
  401. {
  402. if (handle->cur)
  403. free_page((unsigned long)handle->cur);
  404. handle->cur = NULL;
  405. }
  406. static int get_swap_reader(struct swap_map_handle *handle,
  407. swp_entry_t start)
  408. {
  409. int error;
  410. if (!swp_offset(start))
  411. return -EINVAL;
  412. handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
  413. if (!handle->cur)
  414. return -ENOMEM;
  415. error = bio_read_page(swp_offset(start), handle->cur, NULL);
  416. if (error) {
  417. release_swap_reader(handle);
  418. return error;
  419. }
  420. handle->k = 0;
  421. return 0;
  422. }
  423. static int swap_read_page(struct swap_map_handle *handle, void *buf,
  424. struct bio **bio_chain)
  425. {
  426. unsigned long offset;
  427. int error;
  428. if (!handle->cur)
  429. return -EINVAL;
  430. offset = handle->cur->entries[handle->k];
  431. if (!offset)
  432. return -EFAULT;
  433. error = bio_read_page(offset, buf, bio_chain);
  434. if (error)
  435. return error;
  436. if (++handle->k >= MAP_PAGE_ENTRIES) {
  437. error = wait_on_bio_chain(bio_chain);
  438. handle->k = 0;
  439. offset = handle->cur->next_swap;
  440. if (!offset)
  441. release_swap_reader(handle);
  442. else if (!error)
  443. error = bio_read_page(offset, handle->cur, NULL);
  444. }
  445. return error;
  446. }
  447. /**
  448. * load_image - load the image using the swap map handle
  449. * @handle and the snapshot handle @snapshot
  450. * (assume there are @nr_pages pages to load)
  451. */
  452. static int load_image(struct swap_map_handle *handle,
  453. struct snapshot_handle *snapshot,
  454. unsigned int nr_to_read)
  455. {
  456. unsigned int m;
  457. int error = 0;
  458. struct timeval start;
  459. struct timeval stop;
  460. struct bio *bio;
  461. int err2;
  462. unsigned nr_pages;
  463. printk("Loading image data pages (%u pages) ... ", nr_to_read);
  464. m = nr_to_read / 100;
  465. if (!m)
  466. m = 1;
  467. nr_pages = 0;
  468. bio = NULL;
  469. do_gettimeofday(&start);
  470. for ( ; ; ) {
  471. error = snapshot_write_next(snapshot, PAGE_SIZE);
  472. if (error <= 0)
  473. break;
  474. error = swap_read_page(handle, data_of(*snapshot), &bio);
  475. if (error)
  476. break;
  477. if (snapshot->sync_read)
  478. error = wait_on_bio_chain(&bio);
  479. if (error)
  480. break;
  481. if (!(nr_pages % m))
  482. printk("\b\b\b\b%3d%%", nr_pages / m);
  483. nr_pages++;
  484. }
  485. err2 = wait_on_bio_chain(&bio);
  486. do_gettimeofday(&stop);
  487. if (!error)
  488. error = err2;
  489. if (!error) {
  490. printk("\b\b\b\bdone\n");
  491. snapshot_free_unused_memory(snapshot);
  492. if (!snapshot_image_loaded(snapshot))
  493. error = -ENODATA;
  494. }
  495. show_speed(&start, &stop, nr_to_read, "Read");
  496. return error;
  497. }
  498. int swsusp_read(void)
  499. {
  500. int error;
  501. struct swap_map_handle handle;
  502. struct snapshot_handle snapshot;
  503. struct swsusp_info *header;
  504. if (IS_ERR(resume_bdev)) {
  505. pr_debug("swsusp: block device not initialised\n");
  506. return PTR_ERR(resume_bdev);
  507. }
  508. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  509. error = snapshot_write_next(&snapshot, PAGE_SIZE);
  510. if (error < PAGE_SIZE)
  511. return error < 0 ? error : -EFAULT;
  512. header = (struct swsusp_info *)data_of(snapshot);
  513. error = get_swap_reader(&handle, swsusp_header.image);
  514. if (!error)
  515. error = swap_read_page(&handle, header, NULL);
  516. if (!error)
  517. error = load_image(&handle, &snapshot, header->pages - 1);
  518. release_swap_reader(&handle);
  519. blkdev_put(resume_bdev);
  520. if (!error)
  521. pr_debug("swsusp: Reading resume file was successful\n");
  522. else
  523. pr_debug("swsusp: Error %d resuming\n", error);
  524. return error;
  525. }
  526. /**
  527. * swsusp_check - Check for swsusp signature in the resume device
  528. */
  529. int swsusp_check(void)
  530. {
  531. int error;
  532. resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
  533. if (!IS_ERR(resume_bdev)) {
  534. set_blocksize(resume_bdev, PAGE_SIZE);
  535. memset(&swsusp_header, 0, sizeof(swsusp_header));
  536. if ((error = bio_read_page(0, &swsusp_header, NULL)))
  537. return error;
  538. if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
  539. memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
  540. /* Reset swap signature now */
  541. error = bio_write_page(0, &swsusp_header);
  542. } else {
  543. return -EINVAL;
  544. }
  545. if (error)
  546. blkdev_put(resume_bdev);
  547. else
  548. pr_debug("swsusp: Signature found, resuming\n");
  549. } else {
  550. error = PTR_ERR(resume_bdev);
  551. }
  552. if (error)
  553. pr_debug("swsusp: Error %d check for resume file\n", error);
  554. return error;
  555. }
  556. /**
  557. * swsusp_close - close swap device.
  558. */
  559. void swsusp_close(void)
  560. {
  561. if (IS_ERR(resume_bdev)) {
  562. pr_debug("swsusp: block device not initialised\n");
  563. return;
  564. }
  565. blkdev_put(resume_bdev);
  566. }