swap.c 15 KB

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