swap.c 15 KB

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