swap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. 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(KERN_ERR "PM: Adding page to bio failed at %ld\n",
  65. 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(bio_rw, 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(bio_rw, 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, unsigned int flags)
  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. swsusp_header->flags = flags;
  131. error = bio_write_page(swsusp_resume_block,
  132. swsusp_header, NULL);
  133. } else {
  134. printk(KERN_ERR "PM: Swap header not found!\n");
  135. error = -ENODEV;
  136. }
  137. return error;
  138. }
  139. /**
  140. * swsusp_swap_check - check if the resume device is a swap device
  141. * and get its index (if so)
  142. */
  143. static int swsusp_swap_check(void) /* This is called before saving image */
  144. {
  145. int res;
  146. res = swap_type_of(swsusp_resume_device, swsusp_resume_block,
  147. &resume_bdev);
  148. if (res < 0)
  149. return res;
  150. root_swap = res;
  151. res = blkdev_get(resume_bdev, FMODE_WRITE);
  152. if (res)
  153. return res;
  154. res = set_blocksize(resume_bdev, PAGE_SIZE);
  155. if (res < 0)
  156. blkdev_put(resume_bdev, FMODE_WRITE);
  157. return res;
  158. }
  159. /**
  160. * write_page - Write one page to given swap location.
  161. * @buf: Address we're writing.
  162. * @offset: Offset of the swap page we're writing to.
  163. * @bio_chain: Link the next write BIO here
  164. */
  165. static int write_page(void *buf, sector_t offset, struct bio **bio_chain)
  166. {
  167. void *src;
  168. if (!offset)
  169. return -ENOSPC;
  170. if (bio_chain) {
  171. src = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
  172. if (src) {
  173. memcpy(src, buf, PAGE_SIZE);
  174. } else {
  175. WARN_ON_ONCE(1);
  176. bio_chain = NULL; /* Go synchronous */
  177. src = buf;
  178. }
  179. } else {
  180. src = buf;
  181. }
  182. return bio_write_page(offset, src, bio_chain);
  183. }
  184. /*
  185. * The swap map is a data structure used for keeping track of each page
  186. * written to a swap partition. It consists of many swap_map_page
  187. * structures that contain each an array of MAP_PAGE_SIZE swap entries.
  188. * These structures are stored on the swap and linked together with the
  189. * help of the .next_swap member.
  190. *
  191. * The swap map is created during suspend. The swap map pages are
  192. * allocated and populated one at a time, so we only need one memory
  193. * page to set up the entire structure.
  194. *
  195. * During resume we also only need to use one swap_map_page structure
  196. * at a time.
  197. */
  198. #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
  199. struct swap_map_page {
  200. sector_t entries[MAP_PAGE_ENTRIES];
  201. sector_t next_swap;
  202. };
  203. /**
  204. * The swap_map_handle structure is used for handling swap in
  205. * a file-alike way
  206. */
  207. struct swap_map_handle {
  208. struct swap_map_page *cur;
  209. sector_t cur_swap;
  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. }
  218. static int get_swap_writer(struct swap_map_handle *handle)
  219. {
  220. handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
  221. if (!handle->cur)
  222. return -ENOMEM;
  223. handle->cur_swap = alloc_swapdev_block(root_swap);
  224. if (!handle->cur_swap) {
  225. release_swap_writer(handle);
  226. return -ENOSPC;
  227. }
  228. handle->k = 0;
  229. return 0;
  230. }
  231. static int swap_write_page(struct swap_map_handle *handle, void *buf,
  232. struct bio **bio_chain)
  233. {
  234. int error = 0;
  235. sector_t offset;
  236. if (!handle->cur)
  237. return -EINVAL;
  238. offset = alloc_swapdev_block(root_swap);
  239. error = write_page(buf, offset, bio_chain);
  240. if (error)
  241. return error;
  242. handle->cur->entries[handle->k++] = offset;
  243. if (handle->k >= MAP_PAGE_ENTRIES) {
  244. error = wait_on_bio_chain(bio_chain);
  245. if (error)
  246. goto out;
  247. offset = alloc_swapdev_block(root_swap);
  248. if (!offset)
  249. return -ENOSPC;
  250. handle->cur->next_swap = offset;
  251. error = write_page(handle->cur, handle->cur_swap, NULL);
  252. if (error)
  253. goto out;
  254. memset(handle->cur, 0, PAGE_SIZE);
  255. handle->cur_swap = offset;
  256. handle->k = 0;
  257. }
  258. out:
  259. return error;
  260. }
  261. static int flush_swap_writer(struct swap_map_handle *handle)
  262. {
  263. if (handle->cur && handle->cur_swap)
  264. return write_page(handle->cur, handle->cur_swap, NULL);
  265. else
  266. return -EINVAL;
  267. }
  268. /**
  269. * save_image - save the suspend image data
  270. */
  271. static int save_image(struct swap_map_handle *handle,
  272. struct snapshot_handle *snapshot,
  273. unsigned int nr_to_write)
  274. {
  275. unsigned int m;
  276. int ret;
  277. int error = 0;
  278. int nr_pages;
  279. int err2;
  280. struct bio *bio;
  281. struct timeval start;
  282. struct timeval stop;
  283. printk(KERN_INFO "PM: Saving image data pages (%u pages) ... ",
  284. nr_to_write);
  285. m = nr_to_write / 100;
  286. if (!m)
  287. m = 1;
  288. nr_pages = 0;
  289. bio = NULL;
  290. do_gettimeofday(&start);
  291. do {
  292. ret = snapshot_read_next(snapshot, PAGE_SIZE);
  293. if (ret > 0) {
  294. error = swap_write_page(handle, data_of(*snapshot),
  295. &bio);
  296. if (error)
  297. break;
  298. if (!(nr_pages % m))
  299. printk("\b\b\b\b%3d%%", nr_pages / m);
  300. nr_pages++;
  301. }
  302. } while (ret > 0);
  303. err2 = wait_on_bio_chain(&bio);
  304. do_gettimeofday(&stop);
  305. if (!error)
  306. error = err2;
  307. if (!error)
  308. printk("\b\b\b\bdone\n");
  309. swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
  310. return error;
  311. }
  312. /**
  313. * enough_swap - Make sure we have enough swap to save the image.
  314. *
  315. * Returns TRUE or FALSE after checking the total amount of swap
  316. * space avaiable from the resume partition.
  317. */
  318. static int enough_swap(unsigned int nr_pages)
  319. {
  320. unsigned int free_swap = count_swap_pages(root_swap, 1);
  321. pr_debug("PM: Free swap pages: %u\n", free_swap);
  322. return free_swap > nr_pages + PAGES_FOR_IO;
  323. }
  324. /**
  325. * swsusp_write - Write entire image and metadata.
  326. * @flags: flags to pass to the "boot" kernel in the image header
  327. *
  328. * It is important _NOT_ to umount filesystems at this point. We want
  329. * them synced (in case something goes wrong) but we DO not want to mark
  330. * filesystem clean: it is not. (And it does not matter, if we resume
  331. * correctly, we'll mark system clean, anyway.)
  332. */
  333. int swsusp_write(unsigned int flags)
  334. {
  335. struct swap_map_handle handle;
  336. struct snapshot_handle snapshot;
  337. struct swsusp_info *header;
  338. int error;
  339. error = swsusp_swap_check();
  340. if (error) {
  341. printk(KERN_ERR "PM: Cannot find swap device, try "
  342. "swapon -a.\n");
  343. return error;
  344. }
  345. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  346. error = snapshot_read_next(&snapshot, PAGE_SIZE);
  347. if (error < PAGE_SIZE) {
  348. if (error >= 0)
  349. error = -EFAULT;
  350. goto out;
  351. }
  352. header = (struct swsusp_info *)data_of(snapshot);
  353. if (!enough_swap(header->pages)) {
  354. printk(KERN_ERR "PM: Not enough free swap\n");
  355. error = -ENOSPC;
  356. goto out;
  357. }
  358. error = get_swap_writer(&handle);
  359. if (!error) {
  360. sector_t start = handle.cur_swap;
  361. error = swap_write_page(&handle, header, NULL);
  362. if (!error)
  363. error = save_image(&handle, &snapshot,
  364. header->pages - 1);
  365. if (!error) {
  366. flush_swap_writer(&handle);
  367. printk(KERN_INFO "PM: S");
  368. error = mark_swapfiles(start, flags);
  369. printk("|\n");
  370. }
  371. }
  372. if (error)
  373. free_all_swap_pages(root_swap);
  374. release_swap_writer(&handle);
  375. out:
  376. swsusp_close(FMODE_WRITE);
  377. return error;
  378. }
  379. /**
  380. * The following functions allow us to read data using a swap map
  381. * in a file-alike way
  382. */
  383. static void release_swap_reader(struct swap_map_handle *handle)
  384. {
  385. if (handle->cur)
  386. free_page((unsigned long)handle->cur);
  387. handle->cur = NULL;
  388. }
  389. static int get_swap_reader(struct swap_map_handle *handle, sector_t start)
  390. {
  391. int error;
  392. if (!start)
  393. return -EINVAL;
  394. handle->cur = (struct swap_map_page *)get_zeroed_page(__GFP_WAIT | __GFP_HIGH);
  395. if (!handle->cur)
  396. return -ENOMEM;
  397. error = bio_read_page(start, handle->cur, NULL);
  398. if (error) {
  399. release_swap_reader(handle);
  400. return error;
  401. }
  402. handle->k = 0;
  403. return 0;
  404. }
  405. static int swap_read_page(struct swap_map_handle *handle, void *buf,
  406. struct bio **bio_chain)
  407. {
  408. sector_t offset;
  409. int error;
  410. if (!handle->cur)
  411. return -EINVAL;
  412. offset = handle->cur->entries[handle->k];
  413. if (!offset)
  414. return -EFAULT;
  415. error = bio_read_page(offset, buf, bio_chain);
  416. if (error)
  417. return error;
  418. if (++handle->k >= MAP_PAGE_ENTRIES) {
  419. error = wait_on_bio_chain(bio_chain);
  420. handle->k = 0;
  421. offset = handle->cur->next_swap;
  422. if (!offset)
  423. release_swap_reader(handle);
  424. else if (!error)
  425. error = bio_read_page(offset, handle->cur, NULL);
  426. }
  427. return error;
  428. }
  429. /**
  430. * load_image - load the image using the swap map handle
  431. * @handle and the snapshot handle @snapshot
  432. * (assume there are @nr_pages pages to load)
  433. */
  434. static int load_image(struct swap_map_handle *handle,
  435. struct snapshot_handle *snapshot,
  436. unsigned int nr_to_read)
  437. {
  438. unsigned int m;
  439. int error = 0;
  440. struct timeval start;
  441. struct timeval stop;
  442. struct bio *bio;
  443. int err2;
  444. unsigned nr_pages;
  445. printk(KERN_INFO "PM: Loading image data pages (%u pages) ... ",
  446. nr_to_read);
  447. m = nr_to_read / 100;
  448. if (!m)
  449. m = 1;
  450. nr_pages = 0;
  451. bio = NULL;
  452. do_gettimeofday(&start);
  453. for ( ; ; ) {
  454. error = snapshot_write_next(snapshot, PAGE_SIZE);
  455. if (error <= 0)
  456. break;
  457. error = swap_read_page(handle, data_of(*snapshot), &bio);
  458. if (error)
  459. break;
  460. if (snapshot->sync_read)
  461. error = wait_on_bio_chain(&bio);
  462. if (error)
  463. break;
  464. if (!(nr_pages % m))
  465. printk("\b\b\b\b%3d%%", nr_pages / m);
  466. nr_pages++;
  467. }
  468. err2 = wait_on_bio_chain(&bio);
  469. do_gettimeofday(&stop);
  470. if (!error)
  471. error = err2;
  472. if (!error) {
  473. printk("\b\b\b\bdone\n");
  474. snapshot_write_finalize(snapshot);
  475. if (!snapshot_image_loaded(snapshot))
  476. error = -ENODATA;
  477. }
  478. swsusp_show_speed(&start, &stop, nr_to_read, "Read");
  479. return error;
  480. }
  481. /**
  482. * swsusp_read - read the hibernation image.
  483. * @flags_p: flags passed by the "frozen" kernel in the image header should
  484. * be written into this memeory location
  485. */
  486. int swsusp_read(unsigned int *flags_p)
  487. {
  488. int error;
  489. struct swap_map_handle handle;
  490. struct snapshot_handle snapshot;
  491. struct swsusp_info *header;
  492. *flags_p = swsusp_header->flags;
  493. if (IS_ERR(resume_bdev)) {
  494. pr_debug("PM: Image device not initialised\n");
  495. return PTR_ERR(resume_bdev);
  496. }
  497. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  498. error = snapshot_write_next(&snapshot, PAGE_SIZE);
  499. if (error < PAGE_SIZE)
  500. return error < 0 ? error : -EFAULT;
  501. header = (struct swsusp_info *)data_of(snapshot);
  502. error = get_swap_reader(&handle, swsusp_header->image);
  503. if (!error)
  504. error = swap_read_page(&handle, header, NULL);
  505. if (!error)
  506. error = load_image(&handle, &snapshot, header->pages - 1);
  507. release_swap_reader(&handle);
  508. blkdev_put(resume_bdev, FMODE_READ);
  509. if (!error)
  510. pr_debug("PM: Image successfully loaded\n");
  511. else
  512. pr_debug("PM: Error %d resuming\n", error);
  513. return error;
  514. }
  515. /**
  516. * swsusp_check - Check for swsusp signature in the resume device
  517. */
  518. int swsusp_check(void)
  519. {
  520. int error;
  521. resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
  522. if (!IS_ERR(resume_bdev)) {
  523. set_blocksize(resume_bdev, PAGE_SIZE);
  524. memset(swsusp_header, 0, PAGE_SIZE);
  525. error = bio_read_page(swsusp_resume_block,
  526. swsusp_header, NULL);
  527. if (error)
  528. return error;
  529. if (!memcmp(SWSUSP_SIG, swsusp_header->sig, 10)) {
  530. memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
  531. /* Reset swap signature now */
  532. error = bio_write_page(swsusp_resume_block,
  533. swsusp_header, NULL);
  534. } else {
  535. return -EINVAL;
  536. }
  537. if (error)
  538. blkdev_put(resume_bdev, FMODE_READ);
  539. else
  540. pr_debug("PM: Signature found, resuming\n");
  541. } else {
  542. error = PTR_ERR(resume_bdev);
  543. }
  544. if (error)
  545. pr_debug("PM: Error %d checking image file\n", error);
  546. return error;
  547. }
  548. /**
  549. * swsusp_close - close swap device.
  550. */
  551. void swsusp_close(fmode_t mode)
  552. {
  553. if (IS_ERR(resume_bdev)) {
  554. pr_debug("PM: Image device not initialised\n");
  555. return;
  556. }
  557. blkdev_put(resume_bdev, mode);
  558. }
  559. static int swsusp_header_init(void)
  560. {
  561. swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
  562. if (!swsusp_header)
  563. panic("Could not allocate memory for swsusp_header\n");
  564. return 0;
  565. }
  566. core_initcall(swsusp_header_init);