swap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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_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. struct bitmap_page *bitmap;
  209. unsigned int k;
  210. };
  211. static void release_swap_writer(struct swap_map_handle *handle)
  212. {
  213. if (handle->cur)
  214. free_page((unsigned long)handle->cur);
  215. handle->cur = NULL;
  216. if (handle->bitmap)
  217. free_bitmap(handle->bitmap);
  218. handle->bitmap = NULL;
  219. }
  220. static int get_swap_writer(struct swap_map_handle *handle)
  221. {
  222. handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
  223. if (!handle->cur)
  224. return -ENOMEM;
  225. handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0));
  226. if (!handle->bitmap) {
  227. release_swap_writer(handle);
  228. return -ENOMEM;
  229. }
  230. handle->cur_swap = alloc_swapdev_block(root_swap, handle->bitmap);
  231. if (!handle->cur_swap) {
  232. release_swap_writer(handle);
  233. return -ENOSPC;
  234. }
  235. handle->k = 0;
  236. return 0;
  237. }
  238. static int swap_write_page(struct swap_map_handle *handle, void *buf,
  239. struct bio **bio_chain)
  240. {
  241. int error = 0;
  242. sector_t offset;
  243. if (!handle->cur)
  244. return -EINVAL;
  245. offset = alloc_swapdev_block(root_swap, handle->bitmap);
  246. error = write_page(buf, offset, bio_chain);
  247. if (error)
  248. return error;
  249. handle->cur->entries[handle->k++] = offset;
  250. if (handle->k >= MAP_PAGE_ENTRIES) {
  251. error = wait_on_bio_chain(bio_chain);
  252. if (error)
  253. goto out;
  254. offset = alloc_swapdev_block(root_swap, handle->bitmap);
  255. if (!offset)
  256. return -ENOSPC;
  257. handle->cur->next_swap = offset;
  258. error = write_page(handle->cur, handle->cur_swap, NULL);
  259. if (error)
  260. goto out;
  261. memset(handle->cur, 0, PAGE_SIZE);
  262. handle->cur_swap = offset;
  263. handle->k = 0;
  264. }
  265. out:
  266. return error;
  267. }
  268. static int flush_swap_writer(struct swap_map_handle *handle)
  269. {
  270. if (handle->cur && handle->cur_swap)
  271. return write_page(handle->cur, handle->cur_swap, NULL);
  272. else
  273. return -EINVAL;
  274. }
  275. /**
  276. * save_image - save the suspend image data
  277. */
  278. static int save_image(struct swap_map_handle *handle,
  279. struct snapshot_handle *snapshot,
  280. unsigned int nr_to_write)
  281. {
  282. unsigned int m;
  283. int ret;
  284. int error = 0;
  285. int nr_pages;
  286. int err2;
  287. struct bio *bio;
  288. struct timeval start;
  289. struct timeval stop;
  290. printk("Saving image data pages (%u pages) ... ", nr_to_write);
  291. m = nr_to_write / 100;
  292. if (!m)
  293. m = 1;
  294. nr_pages = 0;
  295. bio = NULL;
  296. do_gettimeofday(&start);
  297. do {
  298. ret = snapshot_read_next(snapshot, PAGE_SIZE);
  299. if (ret > 0) {
  300. error = swap_write_page(handle, data_of(*snapshot),
  301. &bio);
  302. if (error)
  303. break;
  304. if (!(nr_pages % m))
  305. printk("\b\b\b\b%3d%%", nr_pages / m);
  306. nr_pages++;
  307. }
  308. } while (ret > 0);
  309. err2 = wait_on_bio_chain(&bio);
  310. do_gettimeofday(&stop);
  311. if (!error)
  312. error = err2;
  313. if (!error)
  314. printk("\b\b\b\bdone\n");
  315. swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
  316. return error;
  317. }
  318. /**
  319. * enough_swap - Make sure we have enough swap to save the image.
  320. *
  321. * Returns TRUE or FALSE after checking the total amount of swap
  322. * space avaiable from the resume partition.
  323. */
  324. static int enough_swap(unsigned int nr_pages)
  325. {
  326. unsigned int free_swap = count_swap_pages(root_swap, 1);
  327. pr_debug("swsusp: free swap pages: %u\n", free_swap);
  328. return free_swap > nr_pages + PAGES_FOR_IO;
  329. }
  330. /**
  331. * swsusp_write - Write entire image and metadata.
  332. *
  333. * It is important _NOT_ to umount filesystems at this point. We want
  334. * them synced (in case something goes wrong) but we DO not want to mark
  335. * filesystem clean: it is not. (And it does not matter, if we resume
  336. * correctly, we'll mark system clean, anyway.)
  337. */
  338. int swsusp_write(void)
  339. {
  340. struct swap_map_handle handle;
  341. struct snapshot_handle snapshot;
  342. struct swsusp_info *header;
  343. int error;
  344. error = swsusp_swap_check();
  345. if (error) {
  346. printk(KERN_ERR "swsusp: Cannot find swap device, try "
  347. "swapon -a.\n");
  348. return error;
  349. }
  350. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  351. error = snapshot_read_next(&snapshot, PAGE_SIZE);
  352. if (error < PAGE_SIZE) {
  353. if (error >= 0)
  354. error = -EFAULT;
  355. goto out;
  356. }
  357. header = (struct swsusp_info *)data_of(snapshot);
  358. if (!enough_swap(header->pages)) {
  359. printk(KERN_ERR "swsusp: Not enough free swap\n");
  360. error = -ENOSPC;
  361. goto out;
  362. }
  363. error = get_swap_writer(&handle);
  364. if (!error) {
  365. sector_t start = handle.cur_swap;
  366. error = swap_write_page(&handle, header, NULL);
  367. if (!error)
  368. error = save_image(&handle, &snapshot,
  369. header->pages - 1);
  370. if (!error) {
  371. flush_swap_writer(&handle);
  372. printk("S");
  373. error = mark_swapfiles(start);
  374. printk("|\n");
  375. }
  376. }
  377. if (error)
  378. free_all_swap_pages(root_swap, handle.bitmap);
  379. release_swap_writer(&handle);
  380. out:
  381. swsusp_close();
  382. return error;
  383. }
  384. /**
  385. * The following functions allow us to read data using a swap map
  386. * in a file-alike way
  387. */
  388. static void release_swap_reader(struct swap_map_handle *handle)
  389. {
  390. if (handle->cur)
  391. free_page((unsigned long)handle->cur);
  392. handle->cur = NULL;
  393. }
  394. static int get_swap_reader(struct swap_map_handle *handle, sector_t start)
  395. {
  396. int error;
  397. if (!start)
  398. return -EINVAL;
  399. handle->cur = (struct swap_map_page *)get_zeroed_page(__GFP_WAIT | __GFP_HIGH);
  400. if (!handle->cur)
  401. return -ENOMEM;
  402. error = bio_read_page(start, handle->cur, NULL);
  403. if (error) {
  404. release_swap_reader(handle);
  405. return error;
  406. }
  407. handle->k = 0;
  408. return 0;
  409. }
  410. static int swap_read_page(struct swap_map_handle *handle, void *buf,
  411. struct bio **bio_chain)
  412. {
  413. sector_t offset;
  414. int error;
  415. if (!handle->cur)
  416. return -EINVAL;
  417. offset = handle->cur->entries[handle->k];
  418. if (!offset)
  419. return -EFAULT;
  420. error = bio_read_page(offset, buf, bio_chain);
  421. if (error)
  422. return error;
  423. if (++handle->k >= MAP_PAGE_ENTRIES) {
  424. error = wait_on_bio_chain(bio_chain);
  425. handle->k = 0;
  426. offset = handle->cur->next_swap;
  427. if (!offset)
  428. release_swap_reader(handle);
  429. else if (!error)
  430. error = bio_read_page(offset, handle->cur, NULL);
  431. }
  432. return error;
  433. }
  434. /**
  435. * load_image - load the image using the swap map handle
  436. * @handle and the snapshot handle @snapshot
  437. * (assume there are @nr_pages pages to load)
  438. */
  439. static int load_image(struct swap_map_handle *handle,
  440. struct snapshot_handle *snapshot,
  441. unsigned int nr_to_read)
  442. {
  443. unsigned int m;
  444. int error = 0;
  445. struct timeval start;
  446. struct timeval stop;
  447. struct bio *bio;
  448. int err2;
  449. unsigned nr_pages;
  450. printk("Loading image data pages (%u pages) ... ", nr_to_read);
  451. m = nr_to_read / 100;
  452. if (!m)
  453. m = 1;
  454. nr_pages = 0;
  455. bio = NULL;
  456. do_gettimeofday(&start);
  457. for ( ; ; ) {
  458. error = snapshot_write_next(snapshot, PAGE_SIZE);
  459. if (error <= 0)
  460. break;
  461. error = swap_read_page(handle, data_of(*snapshot), &bio);
  462. if (error)
  463. break;
  464. if (snapshot->sync_read)
  465. error = wait_on_bio_chain(&bio);
  466. if (error)
  467. break;
  468. if (!(nr_pages % m))
  469. printk("\b\b\b\b%3d%%", nr_pages / m);
  470. nr_pages++;
  471. }
  472. err2 = wait_on_bio_chain(&bio);
  473. do_gettimeofday(&stop);
  474. if (!error)
  475. error = err2;
  476. if (!error) {
  477. printk("\b\b\b\bdone\n");
  478. snapshot_write_finalize(snapshot);
  479. if (!snapshot_image_loaded(snapshot))
  480. error = -ENODATA;
  481. }
  482. swsusp_show_speed(&start, &stop, nr_to_read, "Read");
  483. return error;
  484. }
  485. int swsusp_read(void)
  486. {
  487. int error;
  488. struct swap_map_handle handle;
  489. struct snapshot_handle snapshot;
  490. struct swsusp_info *header;
  491. if (IS_ERR(resume_bdev)) {
  492. pr_debug("swsusp: block device not initialised\n");
  493. return PTR_ERR(resume_bdev);
  494. }
  495. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  496. error = snapshot_write_next(&snapshot, PAGE_SIZE);
  497. if (error < PAGE_SIZE)
  498. return error < 0 ? error : -EFAULT;
  499. header = (struct swsusp_info *)data_of(snapshot);
  500. error = get_swap_reader(&handle, swsusp_header.image);
  501. if (!error)
  502. error = swap_read_page(&handle, header, NULL);
  503. if (!error)
  504. error = load_image(&handle, &snapshot, header->pages - 1);
  505. release_swap_reader(&handle);
  506. blkdev_put(resume_bdev);
  507. if (!error)
  508. pr_debug("swsusp: Reading resume file was successful\n");
  509. else
  510. pr_debug("swsusp: Error %d resuming\n", error);
  511. return error;
  512. }
  513. /**
  514. * swsusp_check - Check for swsusp signature in the resume device
  515. */
  516. int swsusp_check(void)
  517. {
  518. int error;
  519. resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
  520. if (!IS_ERR(resume_bdev)) {
  521. set_blocksize(resume_bdev, PAGE_SIZE);
  522. memset(&swsusp_header, 0, sizeof(swsusp_header));
  523. error = bio_read_page(swsusp_resume_block,
  524. &swsusp_header, NULL);
  525. if (error)
  526. return error;
  527. if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
  528. memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
  529. /* Reset swap signature now */
  530. error = bio_write_page(swsusp_resume_block,
  531. &swsusp_header, NULL);
  532. } else {
  533. return -EINVAL;
  534. }
  535. if (error)
  536. blkdev_put(resume_bdev);
  537. else
  538. pr_debug("swsusp: Signature found, resuming\n");
  539. } else {
  540. error = PTR_ERR(resume_bdev);
  541. }
  542. if (error)
  543. pr_debug("swsusp: Error %d check for resume file\n", error);
  544. return error;
  545. }
  546. /**
  547. * swsusp_close - close swap device.
  548. */
  549. void swsusp_close(void)
  550. {
  551. if (IS_ERR(resume_bdev)) {
  552. pr_debug("swsusp: block device not initialised\n");
  553. return;
  554. }
  555. blkdev_put(resume_bdev);
  556. }