swap.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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@ucw.cz>
  8. * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
  9. * Copyright (C) 2010 Bojan Smojver <bojan@rexursive.com>
  10. *
  11. * This file is released under the GPLv2.
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/file.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 <linux/slab.h>
  27. #include <linux/lzo.h>
  28. #include <linux/vmalloc.h>
  29. #include "power.h"
  30. #define HIBERNATE_SIG "S1SUSPEND"
  31. /*
  32. * The swap map is a data structure used for keeping track of each page
  33. * written to a swap partition. It consists of many swap_map_page
  34. * structures that contain each an array of MAP_PAGE_ENTRIES swap entries.
  35. * These structures are stored on the swap and linked together with the
  36. * help of the .next_swap member.
  37. *
  38. * The swap map is created during suspend. The swap map pages are
  39. * allocated and populated one at a time, so we only need one memory
  40. * page to set up the entire structure.
  41. *
  42. * During resume we also only need to use one swap_map_page structure
  43. * at a time.
  44. */
  45. #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
  46. struct swap_map_page {
  47. sector_t entries[MAP_PAGE_ENTRIES];
  48. sector_t next_swap;
  49. };
  50. /**
  51. * The swap_map_handle structure is used for handling swap in
  52. * a file-alike way
  53. */
  54. struct swap_map_handle {
  55. struct swap_map_page *cur;
  56. sector_t cur_swap;
  57. sector_t first_sector;
  58. unsigned int k;
  59. };
  60. struct swsusp_header {
  61. char reserved[PAGE_SIZE - 20 - sizeof(sector_t) - sizeof(int)];
  62. sector_t image;
  63. unsigned int flags; /* Flags to pass to the "boot" kernel */
  64. char orig_sig[10];
  65. char sig[10];
  66. } __attribute__((packed));
  67. static struct swsusp_header *swsusp_header;
  68. /**
  69. * The following functions are used for tracing the allocated
  70. * swap pages, so that they can be freed in case of an error.
  71. */
  72. struct swsusp_extent {
  73. struct rb_node node;
  74. unsigned long start;
  75. unsigned long end;
  76. };
  77. static struct rb_root swsusp_extents = RB_ROOT;
  78. static int swsusp_extents_insert(unsigned long swap_offset)
  79. {
  80. struct rb_node **new = &(swsusp_extents.rb_node);
  81. struct rb_node *parent = NULL;
  82. struct swsusp_extent *ext;
  83. /* Figure out where to put the new node */
  84. while (*new) {
  85. ext = container_of(*new, struct swsusp_extent, node);
  86. parent = *new;
  87. if (swap_offset < ext->start) {
  88. /* Try to merge */
  89. if (swap_offset == ext->start - 1) {
  90. ext->start--;
  91. return 0;
  92. }
  93. new = &((*new)->rb_left);
  94. } else if (swap_offset > ext->end) {
  95. /* Try to merge */
  96. if (swap_offset == ext->end + 1) {
  97. ext->end++;
  98. return 0;
  99. }
  100. new = &((*new)->rb_right);
  101. } else {
  102. /* It already is in the tree */
  103. return -EINVAL;
  104. }
  105. }
  106. /* Add the new node and rebalance the tree. */
  107. ext = kzalloc(sizeof(struct swsusp_extent), GFP_KERNEL);
  108. if (!ext)
  109. return -ENOMEM;
  110. ext->start = swap_offset;
  111. ext->end = swap_offset;
  112. rb_link_node(&ext->node, parent, new);
  113. rb_insert_color(&ext->node, &swsusp_extents);
  114. return 0;
  115. }
  116. /**
  117. * alloc_swapdev_block - allocate a swap page and register that it has
  118. * been allocated, so that it can be freed in case of an error.
  119. */
  120. sector_t alloc_swapdev_block(int swap)
  121. {
  122. unsigned long offset;
  123. offset = swp_offset(get_swap_page_of_type(swap));
  124. if (offset) {
  125. if (swsusp_extents_insert(offset))
  126. swap_free(swp_entry(swap, offset));
  127. else
  128. return swapdev_block(swap, offset);
  129. }
  130. return 0;
  131. }
  132. /**
  133. * free_all_swap_pages - free swap pages allocated for saving image data.
  134. * It also frees the extents used to register which swap entries had been
  135. * allocated.
  136. */
  137. void free_all_swap_pages(int swap)
  138. {
  139. struct rb_node *node;
  140. while ((node = swsusp_extents.rb_node)) {
  141. struct swsusp_extent *ext;
  142. unsigned long offset;
  143. ext = container_of(node, struct swsusp_extent, node);
  144. rb_erase(node, &swsusp_extents);
  145. for (offset = ext->start; offset <= ext->end; offset++)
  146. swap_free(swp_entry(swap, offset));
  147. kfree(ext);
  148. }
  149. }
  150. int swsusp_swap_in_use(void)
  151. {
  152. return (swsusp_extents.rb_node != NULL);
  153. }
  154. /*
  155. * General things
  156. */
  157. static unsigned short root_swap = 0xffff;
  158. struct block_device *hib_resume_bdev;
  159. /*
  160. * Saving part
  161. */
  162. static int mark_swapfiles(struct swap_map_handle *handle, unsigned int flags)
  163. {
  164. int error;
  165. hib_bio_read_page(swsusp_resume_block, swsusp_header, NULL);
  166. if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) ||
  167. !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) {
  168. memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10);
  169. memcpy(swsusp_header->sig, HIBERNATE_SIG, 10);
  170. swsusp_header->image = handle->first_sector;
  171. swsusp_header->flags = flags;
  172. error = hib_bio_write_page(swsusp_resume_block,
  173. swsusp_header, NULL);
  174. } else {
  175. printk(KERN_ERR "PM: Swap header not found!\n");
  176. error = -ENODEV;
  177. }
  178. return error;
  179. }
  180. /**
  181. * swsusp_swap_check - check if the resume device is a swap device
  182. * and get its index (if so)
  183. *
  184. * This is called before saving image
  185. */
  186. static int swsusp_swap_check(void)
  187. {
  188. int res;
  189. res = swap_type_of(swsusp_resume_device, swsusp_resume_block,
  190. &hib_resume_bdev);
  191. if (res < 0)
  192. return res;
  193. root_swap = res;
  194. res = blkdev_get(hib_resume_bdev, FMODE_WRITE, NULL);
  195. if (res)
  196. return res;
  197. res = set_blocksize(hib_resume_bdev, PAGE_SIZE);
  198. if (res < 0)
  199. blkdev_put(hib_resume_bdev, FMODE_WRITE);
  200. return res;
  201. }
  202. /**
  203. * write_page - Write one page to given swap location.
  204. * @buf: Address we're writing.
  205. * @offset: Offset of the swap page we're writing to.
  206. * @bio_chain: Link the next write BIO here
  207. */
  208. static int write_page(void *buf, sector_t offset, struct bio **bio_chain)
  209. {
  210. void *src;
  211. if (!offset)
  212. return -ENOSPC;
  213. if (bio_chain) {
  214. src = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
  215. if (src) {
  216. copy_page(src, buf);
  217. } else {
  218. WARN_ON_ONCE(1);
  219. bio_chain = NULL; /* Go synchronous */
  220. src = buf;
  221. }
  222. } else {
  223. src = buf;
  224. }
  225. return hib_bio_write_page(offset, src, bio_chain);
  226. }
  227. static void release_swap_writer(struct swap_map_handle *handle)
  228. {
  229. if (handle->cur)
  230. free_page((unsigned long)handle->cur);
  231. handle->cur = NULL;
  232. }
  233. static int get_swap_writer(struct swap_map_handle *handle)
  234. {
  235. int ret;
  236. ret = swsusp_swap_check();
  237. if (ret) {
  238. if (ret != -ENOSPC)
  239. printk(KERN_ERR "PM: Cannot find swap device, try "
  240. "swapon -a.\n");
  241. return ret;
  242. }
  243. handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
  244. if (!handle->cur) {
  245. ret = -ENOMEM;
  246. goto err_close;
  247. }
  248. handle->cur_swap = alloc_swapdev_block(root_swap);
  249. if (!handle->cur_swap) {
  250. ret = -ENOSPC;
  251. goto err_rel;
  252. }
  253. handle->k = 0;
  254. handle->first_sector = handle->cur_swap;
  255. return 0;
  256. err_rel:
  257. release_swap_writer(handle);
  258. err_close:
  259. swsusp_close(FMODE_WRITE);
  260. return ret;
  261. }
  262. static int swap_write_page(struct swap_map_handle *handle, void *buf,
  263. struct bio **bio_chain)
  264. {
  265. int error = 0;
  266. sector_t offset;
  267. if (!handle->cur)
  268. return -EINVAL;
  269. offset = alloc_swapdev_block(root_swap);
  270. error = write_page(buf, offset, bio_chain);
  271. if (error)
  272. return error;
  273. handle->cur->entries[handle->k++] = offset;
  274. if (handle->k >= MAP_PAGE_ENTRIES) {
  275. error = hib_wait_on_bio_chain(bio_chain);
  276. if (error)
  277. goto out;
  278. offset = alloc_swapdev_block(root_swap);
  279. if (!offset)
  280. return -ENOSPC;
  281. handle->cur->next_swap = offset;
  282. error = write_page(handle->cur, handle->cur_swap, NULL);
  283. if (error)
  284. goto out;
  285. clear_page(handle->cur);
  286. handle->cur_swap = offset;
  287. handle->k = 0;
  288. }
  289. out:
  290. return error;
  291. }
  292. static int flush_swap_writer(struct swap_map_handle *handle)
  293. {
  294. if (handle->cur && handle->cur_swap)
  295. return write_page(handle->cur, handle->cur_swap, NULL);
  296. else
  297. return -EINVAL;
  298. }
  299. static int swap_writer_finish(struct swap_map_handle *handle,
  300. unsigned int flags, int error)
  301. {
  302. if (!error) {
  303. flush_swap_writer(handle);
  304. printk(KERN_INFO "PM: S");
  305. error = mark_swapfiles(handle, flags);
  306. printk("|\n");
  307. }
  308. if (error)
  309. free_all_swap_pages(root_swap);
  310. release_swap_writer(handle);
  311. swsusp_close(FMODE_WRITE);
  312. return error;
  313. }
  314. /* We need to remember how much compressed data we need to read. */
  315. #define LZO_HEADER sizeof(size_t)
  316. /* Number of pages/bytes we'll compress at one time. */
  317. #define LZO_UNC_PAGES 32
  318. #define LZO_UNC_SIZE (LZO_UNC_PAGES * PAGE_SIZE)
  319. /* Number of pages/bytes we need for compressed data (worst case). */
  320. #define LZO_CMP_PAGES DIV_ROUND_UP(lzo1x_worst_compress(LZO_UNC_SIZE) + \
  321. LZO_HEADER, PAGE_SIZE)
  322. #define LZO_CMP_SIZE (LZO_CMP_PAGES * PAGE_SIZE)
  323. /**
  324. * save_image - save the suspend image data
  325. */
  326. static int save_image(struct swap_map_handle *handle,
  327. struct snapshot_handle *snapshot,
  328. unsigned int nr_to_write)
  329. {
  330. unsigned int m;
  331. int ret;
  332. int nr_pages;
  333. int err2;
  334. struct bio *bio;
  335. struct timeval start;
  336. struct timeval stop;
  337. printk(KERN_INFO "PM: Saving image data pages (%u pages) ... ",
  338. nr_to_write);
  339. m = nr_to_write / 100;
  340. if (!m)
  341. m = 1;
  342. nr_pages = 0;
  343. bio = NULL;
  344. do_gettimeofday(&start);
  345. while (1) {
  346. ret = snapshot_read_next(snapshot);
  347. if (ret <= 0)
  348. break;
  349. ret = swap_write_page(handle, data_of(*snapshot), &bio);
  350. if (ret)
  351. break;
  352. if (!(nr_pages % m))
  353. printk(KERN_CONT "\b\b\b\b%3d%%", nr_pages / m);
  354. nr_pages++;
  355. }
  356. err2 = hib_wait_on_bio_chain(&bio);
  357. do_gettimeofday(&stop);
  358. if (!ret)
  359. ret = err2;
  360. if (!ret)
  361. printk(KERN_CONT "\b\b\b\bdone\n");
  362. else
  363. printk(KERN_CONT "\n");
  364. swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
  365. return ret;
  366. }
  367. /**
  368. * save_image_lzo - Save the suspend image data compressed with LZO.
  369. * @handle: Swap mam handle to use for saving the image.
  370. * @snapshot: Image to read data from.
  371. * @nr_to_write: Number of pages to save.
  372. */
  373. static int save_image_lzo(struct swap_map_handle *handle,
  374. struct snapshot_handle *snapshot,
  375. unsigned int nr_to_write)
  376. {
  377. unsigned int m;
  378. int ret = 0;
  379. int nr_pages;
  380. int err2;
  381. struct bio *bio;
  382. struct timeval start;
  383. struct timeval stop;
  384. size_t off, unc_len, cmp_len;
  385. unsigned char *unc, *cmp, *wrk, *page;
  386. page = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
  387. if (!page) {
  388. printk(KERN_ERR "PM: Failed to allocate LZO page\n");
  389. return -ENOMEM;
  390. }
  391. wrk = vmalloc(LZO1X_1_MEM_COMPRESS);
  392. if (!wrk) {
  393. printk(KERN_ERR "PM: Failed to allocate LZO workspace\n");
  394. free_page((unsigned long)page);
  395. return -ENOMEM;
  396. }
  397. unc = vmalloc(LZO_UNC_SIZE);
  398. if (!unc) {
  399. printk(KERN_ERR "PM: Failed to allocate LZO uncompressed\n");
  400. vfree(wrk);
  401. free_page((unsigned long)page);
  402. return -ENOMEM;
  403. }
  404. cmp = vmalloc(LZO_CMP_SIZE);
  405. if (!cmp) {
  406. printk(KERN_ERR "PM: Failed to allocate LZO compressed\n");
  407. vfree(unc);
  408. vfree(wrk);
  409. free_page((unsigned long)page);
  410. return -ENOMEM;
  411. }
  412. printk(KERN_INFO
  413. "PM: Compressing and saving image data (%u pages) ... ",
  414. nr_to_write);
  415. m = nr_to_write / 100;
  416. if (!m)
  417. m = 1;
  418. nr_pages = 0;
  419. bio = NULL;
  420. do_gettimeofday(&start);
  421. for (;;) {
  422. for (off = 0; off < LZO_UNC_SIZE; off += PAGE_SIZE) {
  423. ret = snapshot_read_next(snapshot);
  424. if (ret < 0)
  425. goto out_finish;
  426. if (!ret)
  427. break;
  428. memcpy(unc + off, data_of(*snapshot), PAGE_SIZE);
  429. if (!(nr_pages % m))
  430. printk(KERN_CONT "\b\b\b\b%3d%%", nr_pages / m);
  431. nr_pages++;
  432. }
  433. if (!off)
  434. break;
  435. unc_len = off;
  436. ret = lzo1x_1_compress(unc, unc_len,
  437. cmp + LZO_HEADER, &cmp_len, wrk);
  438. if (ret < 0) {
  439. printk(KERN_ERR "PM: LZO compression failed\n");
  440. break;
  441. }
  442. if (unlikely(!cmp_len ||
  443. cmp_len > lzo1x_worst_compress(unc_len))) {
  444. printk(KERN_ERR "PM: Invalid LZO compressed length\n");
  445. ret = -1;
  446. break;
  447. }
  448. *(size_t *)cmp = cmp_len;
  449. /*
  450. * Given we are writing one page at a time to disk, we copy
  451. * that much from the buffer, although the last bit will likely
  452. * be smaller than full page. This is OK - we saved the length
  453. * of the compressed data, so any garbage at the end will be
  454. * discarded when we read it.
  455. */
  456. for (off = 0; off < LZO_HEADER + cmp_len; off += PAGE_SIZE) {
  457. memcpy(page, cmp + off, PAGE_SIZE);
  458. ret = swap_write_page(handle, page, &bio);
  459. if (ret)
  460. goto out_finish;
  461. }
  462. }
  463. out_finish:
  464. err2 = hib_wait_on_bio_chain(&bio);
  465. do_gettimeofday(&stop);
  466. if (!ret)
  467. ret = err2;
  468. if (!ret)
  469. printk(KERN_CONT "\b\b\b\bdone\n");
  470. else
  471. printk(KERN_CONT "\n");
  472. swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
  473. vfree(cmp);
  474. vfree(unc);
  475. vfree(wrk);
  476. free_page((unsigned long)page);
  477. return ret;
  478. }
  479. /**
  480. * enough_swap - Make sure we have enough swap to save the image.
  481. *
  482. * Returns TRUE or FALSE after checking the total amount of swap
  483. * space avaiable from the resume partition.
  484. */
  485. static int enough_swap(unsigned int nr_pages, unsigned int flags)
  486. {
  487. unsigned int free_swap = count_swap_pages(root_swap, 1);
  488. unsigned int required;
  489. pr_debug("PM: Free swap pages: %u\n", free_swap);
  490. required = PAGES_FOR_IO + ((flags & SF_NOCOMPRESS_MODE) ?
  491. nr_pages : (nr_pages * LZO_CMP_PAGES) / LZO_UNC_PAGES + 1);
  492. return free_swap > required;
  493. }
  494. /**
  495. * swsusp_write - Write entire image and metadata.
  496. * @flags: flags to pass to the "boot" kernel in the image header
  497. *
  498. * It is important _NOT_ to umount filesystems at this point. We want
  499. * them synced (in case something goes wrong) but we DO not want to mark
  500. * filesystem clean: it is not. (And it does not matter, if we resume
  501. * correctly, we'll mark system clean, anyway.)
  502. */
  503. int swsusp_write(unsigned int flags)
  504. {
  505. struct swap_map_handle handle;
  506. struct snapshot_handle snapshot;
  507. struct swsusp_info *header;
  508. unsigned long pages;
  509. int error;
  510. pages = snapshot_get_image_size();
  511. error = get_swap_writer(&handle);
  512. if (error) {
  513. printk(KERN_ERR "PM: Cannot get swap writer\n");
  514. return error;
  515. }
  516. if (!enough_swap(pages, flags)) {
  517. printk(KERN_ERR "PM: Not enough free swap\n");
  518. error = -ENOSPC;
  519. goto out_finish;
  520. }
  521. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  522. error = snapshot_read_next(&snapshot);
  523. if (error < PAGE_SIZE) {
  524. if (error >= 0)
  525. error = -EFAULT;
  526. goto out_finish;
  527. }
  528. header = (struct swsusp_info *)data_of(snapshot);
  529. error = swap_write_page(&handle, header, NULL);
  530. if (!error) {
  531. error = (flags & SF_NOCOMPRESS_MODE) ?
  532. save_image(&handle, &snapshot, pages - 1) :
  533. save_image_lzo(&handle, &snapshot, pages - 1);
  534. }
  535. out_finish:
  536. error = swap_writer_finish(&handle, flags, error);
  537. return error;
  538. }
  539. /**
  540. * The following functions allow us to read data using a swap map
  541. * in a file-alike way
  542. */
  543. static void release_swap_reader(struct swap_map_handle *handle)
  544. {
  545. if (handle->cur)
  546. free_page((unsigned long)handle->cur);
  547. handle->cur = NULL;
  548. }
  549. static int get_swap_reader(struct swap_map_handle *handle,
  550. unsigned int *flags_p)
  551. {
  552. int error;
  553. *flags_p = swsusp_header->flags;
  554. if (!swsusp_header->image) /* how can this happen? */
  555. return -EINVAL;
  556. handle->cur = (struct swap_map_page *)get_zeroed_page(__GFP_WAIT | __GFP_HIGH);
  557. if (!handle->cur)
  558. return -ENOMEM;
  559. error = hib_bio_read_page(swsusp_header->image, handle->cur, NULL);
  560. if (error) {
  561. release_swap_reader(handle);
  562. return error;
  563. }
  564. handle->k = 0;
  565. return 0;
  566. }
  567. static int swap_read_page(struct swap_map_handle *handle, void *buf,
  568. struct bio **bio_chain)
  569. {
  570. sector_t offset;
  571. int error;
  572. if (!handle->cur)
  573. return -EINVAL;
  574. offset = handle->cur->entries[handle->k];
  575. if (!offset)
  576. return -EFAULT;
  577. error = hib_bio_read_page(offset, buf, bio_chain);
  578. if (error)
  579. return error;
  580. if (++handle->k >= MAP_PAGE_ENTRIES) {
  581. error = hib_wait_on_bio_chain(bio_chain);
  582. handle->k = 0;
  583. offset = handle->cur->next_swap;
  584. if (!offset)
  585. release_swap_reader(handle);
  586. else if (!error)
  587. error = hib_bio_read_page(offset, handle->cur, NULL);
  588. }
  589. return error;
  590. }
  591. static int swap_reader_finish(struct swap_map_handle *handle)
  592. {
  593. release_swap_reader(handle);
  594. return 0;
  595. }
  596. /**
  597. * load_image - load the image using the swap map handle
  598. * @handle and the snapshot handle @snapshot
  599. * (assume there are @nr_pages pages to load)
  600. */
  601. static int load_image(struct swap_map_handle *handle,
  602. struct snapshot_handle *snapshot,
  603. unsigned int nr_to_read)
  604. {
  605. unsigned int m;
  606. int error = 0;
  607. struct timeval start;
  608. struct timeval stop;
  609. struct bio *bio;
  610. int err2;
  611. unsigned nr_pages;
  612. printk(KERN_INFO "PM: Loading image data pages (%u pages) ... ",
  613. nr_to_read);
  614. m = nr_to_read / 100;
  615. if (!m)
  616. m = 1;
  617. nr_pages = 0;
  618. bio = NULL;
  619. do_gettimeofday(&start);
  620. for ( ; ; ) {
  621. error = snapshot_write_next(snapshot);
  622. if (error <= 0)
  623. break;
  624. error = swap_read_page(handle, data_of(*snapshot), &bio);
  625. if (error)
  626. break;
  627. if (snapshot->sync_read)
  628. error = hib_wait_on_bio_chain(&bio);
  629. if (error)
  630. break;
  631. if (!(nr_pages % m))
  632. printk("\b\b\b\b%3d%%", nr_pages / m);
  633. nr_pages++;
  634. }
  635. err2 = hib_wait_on_bio_chain(&bio);
  636. do_gettimeofday(&stop);
  637. if (!error)
  638. error = err2;
  639. if (!error) {
  640. printk("\b\b\b\bdone\n");
  641. snapshot_write_finalize(snapshot);
  642. if (!snapshot_image_loaded(snapshot))
  643. error = -ENODATA;
  644. } else
  645. printk("\n");
  646. swsusp_show_speed(&start, &stop, nr_to_read, "Read");
  647. return error;
  648. }
  649. /**
  650. * load_image_lzo - Load compressed image data and decompress them with LZO.
  651. * @handle: Swap map handle to use for loading data.
  652. * @snapshot: Image to copy uncompressed data into.
  653. * @nr_to_read: Number of pages to load.
  654. */
  655. static int load_image_lzo(struct swap_map_handle *handle,
  656. struct snapshot_handle *snapshot,
  657. unsigned int nr_to_read)
  658. {
  659. unsigned int m;
  660. int error = 0;
  661. struct bio *bio;
  662. struct timeval start;
  663. struct timeval stop;
  664. unsigned nr_pages;
  665. size_t i, off, unc_len, cmp_len;
  666. unsigned char *unc, *cmp, *page[LZO_CMP_PAGES];
  667. for (i = 0; i < LZO_CMP_PAGES; i++) {
  668. page[i] = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
  669. if (!page[i]) {
  670. printk(KERN_ERR "PM: Failed to allocate LZO page\n");
  671. while (i)
  672. free_page((unsigned long)page[--i]);
  673. return -ENOMEM;
  674. }
  675. }
  676. unc = vmalloc(LZO_UNC_SIZE);
  677. if (!unc) {
  678. printk(KERN_ERR "PM: Failed to allocate LZO uncompressed\n");
  679. for (i = 0; i < LZO_CMP_PAGES; i++)
  680. free_page((unsigned long)page[i]);
  681. return -ENOMEM;
  682. }
  683. cmp = vmalloc(LZO_CMP_SIZE);
  684. if (!cmp) {
  685. printk(KERN_ERR "PM: Failed to allocate LZO compressed\n");
  686. vfree(unc);
  687. for (i = 0; i < LZO_CMP_PAGES; i++)
  688. free_page((unsigned long)page[i]);
  689. return -ENOMEM;
  690. }
  691. printk(KERN_INFO
  692. "PM: Loading and decompressing image data (%u pages) ... ",
  693. nr_to_read);
  694. m = nr_to_read / 100;
  695. if (!m)
  696. m = 1;
  697. nr_pages = 0;
  698. bio = NULL;
  699. do_gettimeofday(&start);
  700. error = snapshot_write_next(snapshot);
  701. if (error <= 0)
  702. goto out_finish;
  703. for (;;) {
  704. error = swap_read_page(handle, page[0], NULL); /* sync */
  705. if (error)
  706. break;
  707. cmp_len = *(size_t *)page[0];
  708. if (unlikely(!cmp_len ||
  709. cmp_len > lzo1x_worst_compress(LZO_UNC_SIZE))) {
  710. printk(KERN_ERR "PM: Invalid LZO compressed length\n");
  711. error = -1;
  712. break;
  713. }
  714. for (off = PAGE_SIZE, i = 1;
  715. off < LZO_HEADER + cmp_len; off += PAGE_SIZE, i++) {
  716. error = swap_read_page(handle, page[i], &bio);
  717. if (error)
  718. goto out_finish;
  719. }
  720. error = hib_wait_on_bio_chain(&bio); /* need all data now */
  721. if (error)
  722. goto out_finish;
  723. for (off = 0, i = 0;
  724. off < LZO_HEADER + cmp_len; off += PAGE_SIZE, i++) {
  725. memcpy(cmp + off, page[i], PAGE_SIZE);
  726. }
  727. unc_len = LZO_UNC_SIZE;
  728. error = lzo1x_decompress_safe(cmp + LZO_HEADER, cmp_len,
  729. unc, &unc_len);
  730. if (error < 0) {
  731. printk(KERN_ERR "PM: LZO decompression failed\n");
  732. break;
  733. }
  734. if (unlikely(!unc_len ||
  735. unc_len > LZO_UNC_SIZE ||
  736. unc_len & (PAGE_SIZE - 1))) {
  737. printk(KERN_ERR "PM: Invalid LZO uncompressed length\n");
  738. error = -1;
  739. break;
  740. }
  741. for (off = 0; off < unc_len; off += PAGE_SIZE) {
  742. memcpy(data_of(*snapshot), unc + off, PAGE_SIZE);
  743. if (!(nr_pages % m))
  744. printk("\b\b\b\b%3d%%", nr_pages / m);
  745. nr_pages++;
  746. error = snapshot_write_next(snapshot);
  747. if (error <= 0)
  748. goto out_finish;
  749. }
  750. }
  751. out_finish:
  752. do_gettimeofday(&stop);
  753. if (!error) {
  754. printk("\b\b\b\bdone\n");
  755. snapshot_write_finalize(snapshot);
  756. if (!snapshot_image_loaded(snapshot))
  757. error = -ENODATA;
  758. } else
  759. printk("\n");
  760. swsusp_show_speed(&start, &stop, nr_to_read, "Read");
  761. vfree(cmp);
  762. vfree(unc);
  763. for (i = 0; i < LZO_CMP_PAGES; i++)
  764. free_page((unsigned long)page[i]);
  765. return error;
  766. }
  767. /**
  768. * swsusp_read - read the hibernation image.
  769. * @flags_p: flags passed by the "frozen" kernel in the image header should
  770. * be written into this memory location
  771. */
  772. int swsusp_read(unsigned int *flags_p)
  773. {
  774. int error;
  775. struct swap_map_handle handle;
  776. struct snapshot_handle snapshot;
  777. struct swsusp_info *header;
  778. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  779. error = snapshot_write_next(&snapshot);
  780. if (error < PAGE_SIZE)
  781. return error < 0 ? error : -EFAULT;
  782. header = (struct swsusp_info *)data_of(snapshot);
  783. error = get_swap_reader(&handle, flags_p);
  784. if (error)
  785. goto end;
  786. if (!error)
  787. error = swap_read_page(&handle, header, NULL);
  788. if (!error) {
  789. error = (*flags_p & SF_NOCOMPRESS_MODE) ?
  790. load_image(&handle, &snapshot, header->pages - 1) :
  791. load_image_lzo(&handle, &snapshot, header->pages - 1);
  792. }
  793. swap_reader_finish(&handle);
  794. end:
  795. if (!error)
  796. pr_debug("PM: Image successfully loaded\n");
  797. else
  798. pr_debug("PM: Error %d resuming\n", error);
  799. return error;
  800. }
  801. /**
  802. * swsusp_check - Check for swsusp signature in the resume device
  803. */
  804. int swsusp_check(void)
  805. {
  806. int error;
  807. hib_resume_bdev = blkdev_get_by_dev(swsusp_resume_device,
  808. FMODE_READ, NULL);
  809. if (!IS_ERR(hib_resume_bdev)) {
  810. set_blocksize(hib_resume_bdev, PAGE_SIZE);
  811. clear_page(swsusp_header);
  812. error = hib_bio_read_page(swsusp_resume_block,
  813. swsusp_header, NULL);
  814. if (error)
  815. goto put;
  816. if (!memcmp(HIBERNATE_SIG, swsusp_header->sig, 10)) {
  817. memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
  818. /* Reset swap signature now */
  819. error = hib_bio_write_page(swsusp_resume_block,
  820. swsusp_header, NULL);
  821. } else {
  822. error = -EINVAL;
  823. }
  824. put:
  825. if (error)
  826. blkdev_put(hib_resume_bdev, FMODE_READ);
  827. else
  828. pr_debug("PM: Image signature found, resuming\n");
  829. } else {
  830. error = PTR_ERR(hib_resume_bdev);
  831. }
  832. if (error)
  833. pr_debug("PM: Image not found (code %d)\n", error);
  834. return error;
  835. }
  836. /**
  837. * swsusp_close - close swap device.
  838. */
  839. void swsusp_close(fmode_t mode)
  840. {
  841. if (IS_ERR(hib_resume_bdev)) {
  842. pr_debug("PM: Image device not initialised\n");
  843. return;
  844. }
  845. blkdev_put(hib_resume_bdev, mode);
  846. }
  847. static int swsusp_header_init(void)
  848. {
  849. swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
  850. if (!swsusp_header)
  851. panic("Could not allocate memory for swsusp_header\n");
  852. return 0;
  853. }
  854. core_initcall(swsusp_header_init);