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/utsname.h>
  16. #include <linux/delay.h>
  17. #include <linux/bitops.h>
  18. #include <linux/genhd.h>
  19. #include <linux/device.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/bio.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/swap.h>
  24. #include <linux/swapops.h>
  25. #include <linux/pm.h>
  26. #include "power.h"
  27. #define SWSUSP_SIG "S1SUSPEND"
  28. struct swsusp_header {
  29. char reserved[PAGE_SIZE - 20 - sizeof(sector_t) - sizeof(int)];
  30. sector_t image;
  31. unsigned int flags; /* Flags to pass to the "boot" kernel */
  32. char orig_sig[10];
  33. char sig[10];
  34. } __attribute__((packed));
  35. static struct swsusp_header *swsusp_header;
  36. /*
  37. * General things
  38. */
  39. static unsigned short root_swap = 0xffff;
  40. static struct block_device *resume_bdev;
  41. /**
  42. * submit - submit BIO request.
  43. * @rw: READ or WRITE.
  44. * @off physical offset of page.
  45. * @page: page we're reading or writing.
  46. * @bio_chain: list of pending biod (for async reading)
  47. *
  48. * Straight from the textbook - allocate and initialize the bio.
  49. * If we're reading, make sure the page is marked as dirty.
  50. * Then submit it and, if @bio_chain == NULL, wait.
  51. */
  52. static int submit(int rw, pgoff_t page_off, struct page *page,
  53. struct bio **bio_chain)
  54. {
  55. const int bio_rw = rw | (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
  56. struct bio *bio;
  57. bio = bio_alloc(__GFP_WAIT | __GFP_HIGH, 1);
  58. bio->bi_sector = page_off * (PAGE_SIZE >> 9);
  59. bio->bi_bdev = resume_bdev;
  60. bio->bi_end_io = end_swap_bio_read;
  61. if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
  62. printk(KERN_ERR "PM: Adding page to bio failed at %ld\n",
  63. page_off);
  64. bio_put(bio);
  65. return -EFAULT;
  66. }
  67. lock_page(page);
  68. bio_get(bio);
  69. if (bio_chain == NULL) {
  70. submit_bio(bio_rw, bio);
  71. wait_on_page_locked(page);
  72. if (rw == READ)
  73. bio_set_pages_dirty(bio);
  74. bio_put(bio);
  75. } else {
  76. if (rw == READ)
  77. get_page(page); /* These pages are freed later */
  78. bio->bi_private = *bio_chain;
  79. *bio_chain = bio;
  80. submit_bio(bio_rw, bio);
  81. }
  82. return 0;
  83. }
  84. static int bio_read_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
  85. {
  86. return submit(READ, page_off, virt_to_page(addr), bio_chain);
  87. }
  88. static int bio_write_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
  89. {
  90. return submit(WRITE, page_off, virt_to_page(addr), bio_chain);
  91. }
  92. static int wait_on_bio_chain(struct bio **bio_chain)
  93. {
  94. struct bio *bio;
  95. struct bio *next_bio;
  96. int ret = 0;
  97. if (bio_chain == NULL)
  98. return 0;
  99. bio = *bio_chain;
  100. if (bio == NULL)
  101. return 0;
  102. while (bio) {
  103. struct page *page;
  104. next_bio = bio->bi_private;
  105. page = bio->bi_io_vec[0].bv_page;
  106. wait_on_page_locked(page);
  107. if (!PageUptodate(page) || PageError(page))
  108. ret = -EIO;
  109. put_page(page);
  110. bio_put(bio);
  111. bio = next_bio;
  112. }
  113. *bio_chain = NULL;
  114. return ret;
  115. }
  116. /*
  117. * Saving part
  118. */
  119. static int mark_swapfiles(sector_t start, unsigned int flags)
  120. {
  121. int error;
  122. bio_read_page(swsusp_resume_block, swsusp_header, NULL);
  123. if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) ||
  124. !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) {
  125. memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10);
  126. memcpy(swsusp_header->sig,SWSUSP_SIG, 10);
  127. swsusp_header->image = start;
  128. swsusp_header->flags = flags;
  129. error = bio_write_page(swsusp_resume_block,
  130. swsusp_header, NULL);
  131. } else {
  132. printk(KERN_ERR "PM: 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);
  150. if (res)
  151. return res;
  152. res = set_blocksize(resume_bdev, PAGE_SIZE);
  153. if (res < 0)
  154. blkdev_put(resume_bdev, FMODE_WRITE);
  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(KERN_INFO "PM: Saving image data pages (%u pages) ... ",
  282. nr_to_write);
  283. m = nr_to_write / 100;
  284. if (!m)
  285. m = 1;
  286. nr_pages = 0;
  287. bio = NULL;
  288. do_gettimeofday(&start);
  289. do {
  290. ret = snapshot_read_next(snapshot, PAGE_SIZE);
  291. if (ret > 0) {
  292. error = swap_write_page(handle, data_of(*snapshot),
  293. &bio);
  294. if (error)
  295. break;
  296. if (!(nr_pages % m))
  297. printk("\b\b\b\b%3d%%", nr_pages / m);
  298. nr_pages++;
  299. }
  300. } while (ret > 0);
  301. err2 = wait_on_bio_chain(&bio);
  302. do_gettimeofday(&stop);
  303. if (!error)
  304. error = err2;
  305. if (!error)
  306. printk("\b\b\b\bdone\n");
  307. swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
  308. return error;
  309. }
  310. /**
  311. * enough_swap - Make sure we have enough swap to save the image.
  312. *
  313. * Returns TRUE or FALSE after checking the total amount of swap
  314. * space avaiable from the resume partition.
  315. */
  316. static int enough_swap(unsigned int nr_pages)
  317. {
  318. unsigned int free_swap = count_swap_pages(root_swap, 1);
  319. pr_debug("PM: Free swap pages: %u\n", free_swap);
  320. return free_swap > nr_pages + PAGES_FOR_IO;
  321. }
  322. /**
  323. * swsusp_write - Write entire image and metadata.
  324. * @flags: flags to pass to the "boot" kernel in the image header
  325. *
  326. * It is important _NOT_ to umount filesystems at this point. We want
  327. * them synced (in case something goes wrong) but we DO not want to mark
  328. * filesystem clean: it is not. (And it does not matter, if we resume
  329. * correctly, we'll mark system clean, anyway.)
  330. */
  331. int swsusp_write(unsigned int flags)
  332. {
  333. struct swap_map_handle handle;
  334. struct snapshot_handle snapshot;
  335. struct swsusp_info *header;
  336. int error;
  337. error = swsusp_swap_check();
  338. if (error) {
  339. printk(KERN_ERR "PM: Cannot find swap device, try "
  340. "swapon -a.\n");
  341. return error;
  342. }
  343. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  344. error = snapshot_read_next(&snapshot, PAGE_SIZE);
  345. if (error < PAGE_SIZE) {
  346. if (error >= 0)
  347. error = -EFAULT;
  348. goto out;
  349. }
  350. header = (struct swsusp_info *)data_of(snapshot);
  351. if (!enough_swap(header->pages)) {
  352. printk(KERN_ERR "PM: Not enough free swap\n");
  353. error = -ENOSPC;
  354. goto out;
  355. }
  356. error = get_swap_writer(&handle);
  357. if (!error) {
  358. sector_t start = handle.cur_swap;
  359. error = swap_write_page(&handle, header, NULL);
  360. if (!error)
  361. error = save_image(&handle, &snapshot,
  362. header->pages - 1);
  363. if (!error) {
  364. flush_swap_writer(&handle);
  365. printk(KERN_INFO "PM: S");
  366. error = mark_swapfiles(start, flags);
  367. printk("|\n");
  368. }
  369. }
  370. if (error)
  371. free_all_swap_pages(root_swap);
  372. release_swap_writer(&handle);
  373. out:
  374. swsusp_close(FMODE_WRITE);
  375. return error;
  376. }
  377. /**
  378. * The following functions allow us to read data using a swap map
  379. * in a file-alike way
  380. */
  381. static void release_swap_reader(struct swap_map_handle *handle)
  382. {
  383. if (handle->cur)
  384. free_page((unsigned long)handle->cur);
  385. handle->cur = NULL;
  386. }
  387. static int get_swap_reader(struct swap_map_handle *handle, sector_t start)
  388. {
  389. int error;
  390. if (!start)
  391. return -EINVAL;
  392. handle->cur = (struct swap_map_page *)get_zeroed_page(__GFP_WAIT | __GFP_HIGH);
  393. if (!handle->cur)
  394. return -ENOMEM;
  395. error = bio_read_page(start, handle->cur, NULL);
  396. if (error) {
  397. release_swap_reader(handle);
  398. return error;
  399. }
  400. handle->k = 0;
  401. return 0;
  402. }
  403. static int swap_read_page(struct swap_map_handle *handle, void *buf,
  404. struct bio **bio_chain)
  405. {
  406. sector_t offset;
  407. int error;
  408. if (!handle->cur)
  409. return -EINVAL;
  410. offset = handle->cur->entries[handle->k];
  411. if (!offset)
  412. return -EFAULT;
  413. error = bio_read_page(offset, buf, bio_chain);
  414. if (error)
  415. return error;
  416. if (++handle->k >= MAP_PAGE_ENTRIES) {
  417. error = wait_on_bio_chain(bio_chain);
  418. handle->k = 0;
  419. offset = handle->cur->next_swap;
  420. if (!offset)
  421. release_swap_reader(handle);
  422. else if (!error)
  423. error = bio_read_page(offset, handle->cur, NULL);
  424. }
  425. return error;
  426. }
  427. /**
  428. * load_image - load the image using the swap map handle
  429. * @handle and the snapshot handle @snapshot
  430. * (assume there are @nr_pages pages to load)
  431. */
  432. static int load_image(struct swap_map_handle *handle,
  433. struct snapshot_handle *snapshot,
  434. unsigned int nr_to_read)
  435. {
  436. unsigned int m;
  437. int error = 0;
  438. struct timeval start;
  439. struct timeval stop;
  440. struct bio *bio;
  441. int err2;
  442. unsigned nr_pages;
  443. printk(KERN_INFO "PM: Loading image data pages (%u pages) ... ",
  444. nr_to_read);
  445. m = nr_to_read / 100;
  446. if (!m)
  447. m = 1;
  448. nr_pages = 0;
  449. bio = NULL;
  450. do_gettimeofday(&start);
  451. for ( ; ; ) {
  452. error = snapshot_write_next(snapshot, PAGE_SIZE);
  453. if (error <= 0)
  454. break;
  455. error = swap_read_page(handle, data_of(*snapshot), &bio);
  456. if (error)
  457. break;
  458. if (snapshot->sync_read)
  459. error = wait_on_bio_chain(&bio);
  460. if (error)
  461. break;
  462. if (!(nr_pages % m))
  463. printk("\b\b\b\b%3d%%", nr_pages / m);
  464. nr_pages++;
  465. }
  466. err2 = wait_on_bio_chain(&bio);
  467. do_gettimeofday(&stop);
  468. if (!error)
  469. error = err2;
  470. if (!error) {
  471. printk("\b\b\b\bdone\n");
  472. snapshot_write_finalize(snapshot);
  473. if (!snapshot_image_loaded(snapshot))
  474. error = -ENODATA;
  475. }
  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. blkdev_put(resume_bdev, FMODE_READ);
  507. if (!error)
  508. pr_debug("PM: Image successfully loaded\n");
  509. else
  510. pr_debug("PM: 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, PAGE_SIZE);
  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, 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);