swap.c 15 KB

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