swap.c 14 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/file.h>
  15. #include <linux/utsname.h>
  16. #include <linux/version.h>
  17. #include <linux/delay.h>
  18. #include <linux/bitops.h>
  19. #include <linux/genhd.h>
  20. #include <linux/device.h>
  21. #include <linux/buffer_head.h>
  22. #include <linux/bio.h>
  23. #include <linux/blkdev.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. struct swsusp_header {
  31. char reserved[PAGE_SIZE - 20 - sizeof(sector_t)];
  32. sector_t image;
  33. char orig_sig[10];
  34. char sig[10];
  35. } __attribute__((packed));
  36. static struct swsusp_header *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_WAIT | __GFP_HIGH, 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. /*
  118. * Saving part
  119. */
  120. static int mark_swapfiles(sector_t start)
  121. {
  122. int error;
  123. bio_read_page(swsusp_resume_block, swsusp_header, NULL);
  124. if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) ||
  125. !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) {
  126. memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10);
  127. memcpy(swsusp_header->sig,SWSUSP_SIG, 10);
  128. swsusp_header->image = start;
  129. error = bio_write_page(swsusp_resume_block,
  130. swsusp_header, NULL);
  131. } else {
  132. printk(KERN_ERR "swsusp: Swap header not found!\n");
  133. error = -ENODEV;
  134. }
  135. return error;
  136. }
  137. /**
  138. * swsusp_swap_check - check if the resume device is a swap device
  139. * and get its index (if so)
  140. */
  141. static int swsusp_swap_check(void) /* This is called before saving image */
  142. {
  143. int res;
  144. res = swap_type_of(swsusp_resume_device, swsusp_resume_block,
  145. &resume_bdev);
  146. if (res < 0)
  147. return res;
  148. root_swap = res;
  149. res = blkdev_get(resume_bdev, FMODE_WRITE, O_RDWR);
  150. if (res)
  151. return res;
  152. res = set_blocksize(resume_bdev, PAGE_SIZE);
  153. if (res < 0)
  154. blkdev_put(resume_bdev);
  155. return res;
  156. }
  157. /**
  158. * write_page - Write one page to given swap location.
  159. * @buf: Address we're writing.
  160. * @offset: Offset of the swap page we're writing to.
  161. * @bio_chain: Link the next write BIO here
  162. */
  163. static int write_page(void *buf, sector_t offset, struct bio **bio_chain)
  164. {
  165. void *src;
  166. if (!offset)
  167. return -ENOSPC;
  168. if (bio_chain) {
  169. src = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
  170. if (src) {
  171. memcpy(src, buf, PAGE_SIZE);
  172. } else {
  173. WARN_ON_ONCE(1);
  174. bio_chain = NULL; /* Go synchronous */
  175. src = buf;
  176. }
  177. } else {
  178. src = buf;
  179. }
  180. return bio_write_page(offset, src, bio_chain);
  181. }
  182. /*
  183. * The swap map is a data structure used for keeping track of each page
  184. * written to a swap partition. It consists of many swap_map_page
  185. * structures that contain each an array of MAP_PAGE_SIZE swap entries.
  186. * These structures are stored on the swap and linked together with the
  187. * help of the .next_swap member.
  188. *
  189. * The swap map is created during suspend. The swap map pages are
  190. * allocated and populated one at a time, so we only need one memory
  191. * page to set up the entire structure.
  192. *
  193. * During resume we also only need to use one swap_map_page structure
  194. * at a time.
  195. */
  196. #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
  197. struct swap_map_page {
  198. sector_t entries[MAP_PAGE_ENTRIES];
  199. sector_t next_swap;
  200. };
  201. /**
  202. * The swap_map_handle structure is used for handling swap in
  203. * a file-alike way
  204. */
  205. struct swap_map_handle {
  206. struct swap_map_page *cur;
  207. sector_t cur_swap;
  208. unsigned int k;
  209. };
  210. static void release_swap_writer(struct swap_map_handle *handle)
  211. {
  212. if (handle->cur)
  213. free_page((unsigned long)handle->cur);
  214. handle->cur = NULL;
  215. }
  216. static int get_swap_writer(struct swap_map_handle *handle)
  217. {
  218. handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
  219. if (!handle->cur)
  220. return -ENOMEM;
  221. handle->cur_swap = alloc_swapdev_block(root_swap);
  222. if (!handle->cur_swap) {
  223. release_swap_writer(handle);
  224. return -ENOSPC;
  225. }
  226. handle->k = 0;
  227. return 0;
  228. }
  229. static int swap_write_page(struct swap_map_handle *handle, void *buf,
  230. struct bio **bio_chain)
  231. {
  232. int error = 0;
  233. sector_t offset;
  234. if (!handle->cur)
  235. return -EINVAL;
  236. offset = alloc_swapdev_block(root_swap);
  237. error = write_page(buf, offset, bio_chain);
  238. if (error)
  239. return error;
  240. handle->cur->entries[handle->k++] = offset;
  241. if (handle->k >= MAP_PAGE_ENTRIES) {
  242. error = wait_on_bio_chain(bio_chain);
  243. if (error)
  244. goto out;
  245. offset = alloc_swapdev_block(root_swap);
  246. if (!offset)
  247. return -ENOSPC;
  248. handle->cur->next_swap = offset;
  249. error = write_page(handle->cur, handle->cur_swap, NULL);
  250. if (error)
  251. goto out;
  252. memset(handle->cur, 0, PAGE_SIZE);
  253. handle->cur_swap = offset;
  254. handle->k = 0;
  255. }
  256. out:
  257. return error;
  258. }
  259. static int flush_swap_writer(struct swap_map_handle *handle)
  260. {
  261. if (handle->cur && handle->cur_swap)
  262. return write_page(handle->cur, handle->cur_swap, NULL);
  263. else
  264. return -EINVAL;
  265. }
  266. /**
  267. * save_image - save the suspend image data
  268. */
  269. static int save_image(struct swap_map_handle *handle,
  270. struct snapshot_handle *snapshot,
  271. unsigned int nr_to_write)
  272. {
  273. unsigned int m;
  274. int ret;
  275. int error = 0;
  276. int nr_pages;
  277. int err2;
  278. struct bio *bio;
  279. struct timeval start;
  280. struct timeval stop;
  281. printk("Saving image data pages (%u pages) ... ", nr_to_write);
  282. m = nr_to_write / 100;
  283. if (!m)
  284. m = 1;
  285. nr_pages = 0;
  286. bio = NULL;
  287. do_gettimeofday(&start);
  288. do {
  289. ret = snapshot_read_next(snapshot, PAGE_SIZE);
  290. if (ret > 0) {
  291. error = swap_write_page(handle, data_of(*snapshot),
  292. &bio);
  293. if (error)
  294. break;
  295. if (!(nr_pages % m))
  296. printk("\b\b\b\b%3d%%", nr_pages / m);
  297. nr_pages++;
  298. }
  299. } while (ret > 0);
  300. err2 = wait_on_bio_chain(&bio);
  301. do_gettimeofday(&stop);
  302. if (!error)
  303. error = err2;
  304. if (!error)
  305. printk("\b\b\b\bdone\n");
  306. swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
  307. return error;
  308. }
  309. /**
  310. * enough_swap - Make sure we have enough swap to save the image.
  311. *
  312. * Returns TRUE or FALSE after checking the total amount of swap
  313. * space avaiable from the resume partition.
  314. */
  315. static int enough_swap(unsigned int nr_pages)
  316. {
  317. unsigned int free_swap = count_swap_pages(root_swap, 1);
  318. pr_debug("swsusp: free swap pages: %u\n", free_swap);
  319. return free_swap > nr_pages + PAGES_FOR_IO;
  320. }
  321. /**
  322. * swsusp_write - Write entire image and metadata.
  323. *
  324. * It is important _NOT_ to umount filesystems at this point. We want
  325. * them synced (in case something goes wrong) but we DO not want to mark
  326. * filesystem clean: it is not. (And it does not matter, if we resume
  327. * correctly, we'll mark system clean, anyway.)
  328. */
  329. int swsusp_write(void)
  330. {
  331. struct swap_map_handle handle;
  332. struct snapshot_handle snapshot;
  333. struct swsusp_info *header;
  334. int error;
  335. error = swsusp_swap_check();
  336. if (error) {
  337. printk(KERN_ERR "swsusp: Cannot find swap device, try "
  338. "swapon -a.\n");
  339. return error;
  340. }
  341. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  342. error = snapshot_read_next(&snapshot, PAGE_SIZE);
  343. if (error < PAGE_SIZE) {
  344. if (error >= 0)
  345. error = -EFAULT;
  346. goto out;
  347. }
  348. header = (struct swsusp_info *)data_of(snapshot);
  349. if (!enough_swap(header->pages)) {
  350. printk(KERN_ERR "swsusp: Not enough free swap\n");
  351. error = -ENOSPC;
  352. goto out;
  353. }
  354. error = get_swap_writer(&handle);
  355. if (!error) {
  356. sector_t start = handle.cur_swap;
  357. error = swap_write_page(&handle, header, NULL);
  358. if (!error)
  359. error = save_image(&handle, &snapshot,
  360. header->pages - 1);
  361. if (!error) {
  362. flush_swap_writer(&handle);
  363. printk("S");
  364. error = mark_swapfiles(start);
  365. printk("|\n");
  366. }
  367. }
  368. if (error)
  369. free_all_swap_pages(root_swap);
  370. release_swap_writer(&handle);
  371. out:
  372. swsusp_close();
  373. return error;
  374. }
  375. /**
  376. * The following functions allow us to read data using a swap map
  377. * in a file-alike way
  378. */
  379. static void release_swap_reader(struct swap_map_handle *handle)
  380. {
  381. if (handle->cur)
  382. free_page((unsigned long)handle->cur);
  383. handle->cur = NULL;
  384. }
  385. static int get_swap_reader(struct swap_map_handle *handle, sector_t start)
  386. {
  387. int error;
  388. if (!start)
  389. return -EINVAL;
  390. handle->cur = (struct swap_map_page *)get_zeroed_page(__GFP_WAIT | __GFP_HIGH);
  391. if (!handle->cur)
  392. return -ENOMEM;
  393. error = bio_read_page(start, handle->cur, NULL);
  394. if (error) {
  395. release_swap_reader(handle);
  396. return error;
  397. }
  398. handle->k = 0;
  399. return 0;
  400. }
  401. static int swap_read_page(struct swap_map_handle *handle, void *buf,
  402. struct bio **bio_chain)
  403. {
  404. sector_t offset;
  405. int error;
  406. if (!handle->cur)
  407. return -EINVAL;
  408. offset = handle->cur->entries[handle->k];
  409. if (!offset)
  410. return -EFAULT;
  411. error = bio_read_page(offset, buf, bio_chain);
  412. if (error)
  413. return error;
  414. if (++handle->k >= MAP_PAGE_ENTRIES) {
  415. error = wait_on_bio_chain(bio_chain);
  416. handle->k = 0;
  417. offset = handle->cur->next_swap;
  418. if (!offset)
  419. release_swap_reader(handle);
  420. else if (!error)
  421. error = bio_read_page(offset, handle->cur, NULL);
  422. }
  423. return error;
  424. }
  425. /**
  426. * load_image - load the image using the swap map handle
  427. * @handle and the snapshot handle @snapshot
  428. * (assume there are @nr_pages pages to load)
  429. */
  430. static int load_image(struct swap_map_handle *handle,
  431. struct snapshot_handle *snapshot,
  432. unsigned int nr_to_read)
  433. {
  434. unsigned int m;
  435. int error = 0;
  436. struct timeval start;
  437. struct timeval stop;
  438. struct bio *bio;
  439. int err2;
  440. unsigned nr_pages;
  441. printk("Loading image data pages (%u pages) ... ", nr_to_read);
  442. m = nr_to_read / 100;
  443. if (!m)
  444. m = 1;
  445. nr_pages = 0;
  446. bio = NULL;
  447. do_gettimeofday(&start);
  448. for ( ; ; ) {
  449. error = snapshot_write_next(snapshot, PAGE_SIZE);
  450. if (error <= 0)
  451. break;
  452. error = swap_read_page(handle, data_of(*snapshot), &bio);
  453. if (error)
  454. break;
  455. if (snapshot->sync_read)
  456. error = wait_on_bio_chain(&bio);
  457. if (error)
  458. break;
  459. if (!(nr_pages % m))
  460. printk("\b\b\b\b%3d%%", nr_pages / m);
  461. nr_pages++;
  462. }
  463. err2 = wait_on_bio_chain(&bio);
  464. do_gettimeofday(&stop);
  465. if (!error)
  466. error = err2;
  467. if (!error) {
  468. printk("\b\b\b\bdone\n");
  469. snapshot_write_finalize(snapshot);
  470. if (!snapshot_image_loaded(snapshot))
  471. error = -ENODATA;
  472. }
  473. swsusp_show_speed(&start, &stop, nr_to_read, "Read");
  474. return error;
  475. }
  476. int swsusp_read(void)
  477. {
  478. int error;
  479. struct swap_map_handle handle;
  480. struct snapshot_handle snapshot;
  481. struct swsusp_info *header;
  482. if (IS_ERR(resume_bdev)) {
  483. pr_debug("swsusp: block device not initialised\n");
  484. return PTR_ERR(resume_bdev);
  485. }
  486. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  487. error = snapshot_write_next(&snapshot, PAGE_SIZE);
  488. if (error < PAGE_SIZE)
  489. return error < 0 ? error : -EFAULT;
  490. header = (struct swsusp_info *)data_of(snapshot);
  491. error = get_swap_reader(&handle, swsusp_header->image);
  492. if (!error)
  493. error = swap_read_page(&handle, header, NULL);
  494. if (!error)
  495. error = load_image(&handle, &snapshot, header->pages - 1);
  496. release_swap_reader(&handle);
  497. blkdev_put(resume_bdev);
  498. if (!error)
  499. pr_debug("swsusp: Reading resume file was successful\n");
  500. else
  501. pr_debug("swsusp: Error %d resuming\n", error);
  502. return error;
  503. }
  504. /**
  505. * swsusp_check - Check for swsusp signature in the resume device
  506. */
  507. int swsusp_check(void)
  508. {
  509. int error;
  510. resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
  511. if (!IS_ERR(resume_bdev)) {
  512. set_blocksize(resume_bdev, PAGE_SIZE);
  513. memset(swsusp_header, 0, PAGE_SIZE);
  514. error = bio_read_page(swsusp_resume_block,
  515. swsusp_header, NULL);
  516. if (error)
  517. return error;
  518. if (!memcmp(SWSUSP_SIG, swsusp_header->sig, 10)) {
  519. memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
  520. /* Reset swap signature now */
  521. error = bio_write_page(swsusp_resume_block,
  522. swsusp_header, NULL);
  523. } else {
  524. return -EINVAL;
  525. }
  526. if (error)
  527. blkdev_put(resume_bdev);
  528. else
  529. pr_debug("swsusp: Signature found, resuming\n");
  530. } else {
  531. error = PTR_ERR(resume_bdev);
  532. }
  533. if (error)
  534. pr_debug("swsusp: Error %d check for resume file\n", error);
  535. return error;
  536. }
  537. /**
  538. * swsusp_close - close swap device.
  539. */
  540. void swsusp_close(void)
  541. {
  542. if (IS_ERR(resume_bdev)) {
  543. pr_debug("swsusp: block device not initialised\n");
  544. return;
  545. }
  546. blkdev_put(resume_bdev);
  547. }
  548. static int swsusp_header_init(void)
  549. {
  550. swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
  551. if (!swsusp_header)
  552. panic("Could not allocate memory for swsusp_header\n");
  553. return 0;
  554. }
  555. core_initcall(swsusp_header_init);