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