swap.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  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/bio.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/swap.h>
  23. #include <linux/swapops.h>
  24. #include <linux/pm.h>
  25. #include <linux/slab.h>
  26. #include <linux/lzo.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/cpumask.h>
  29. #include <linux/atomic.h>
  30. #include <linux/kthread.h>
  31. #include <linux/crc32.h>
  32. #include "power.h"
  33. #define HIBERNATE_SIG "S1SUSPEND"
  34. /*
  35. * The swap map is a data structure used for keeping track of each page
  36. * written to a swap partition. It consists of many swap_map_page
  37. * structures that contain each an array of MAP_PAGE_ENTRIES swap entries.
  38. * These structures are stored on the swap and linked together with the
  39. * help of the .next_swap member.
  40. *
  41. * The swap map is created during suspend. The swap map pages are
  42. * allocated and populated one at a time, so we only need one memory
  43. * page to set up the entire structure.
  44. *
  45. * During resume we pick up all swap_map_page structures into a list.
  46. */
  47. #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
  48. struct swap_map_page {
  49. sector_t entries[MAP_PAGE_ENTRIES];
  50. sector_t next_swap;
  51. };
  52. struct swap_map_page_list {
  53. struct swap_map_page *map;
  54. struct swap_map_page_list *next;
  55. };
  56. /**
  57. * The swap_map_handle structure is used for handling swap in
  58. * a file-alike way
  59. */
  60. struct swap_map_handle {
  61. struct swap_map_page *cur;
  62. struct swap_map_page_list *maps;
  63. sector_t cur_swap;
  64. sector_t first_sector;
  65. unsigned int k;
  66. unsigned long nr_free_pages, written;
  67. u32 crc32;
  68. };
  69. struct swsusp_header {
  70. char reserved[PAGE_SIZE - 20 - sizeof(sector_t) - sizeof(int) -
  71. sizeof(u32)];
  72. u32 crc32;
  73. sector_t image;
  74. unsigned int flags; /* Flags to pass to the "boot" kernel */
  75. char orig_sig[10];
  76. char sig[10];
  77. } __attribute__((packed));
  78. static struct swsusp_header *swsusp_header;
  79. /**
  80. * The following functions are used for tracing the allocated
  81. * swap pages, so that they can be freed in case of an error.
  82. */
  83. struct swsusp_extent {
  84. struct rb_node node;
  85. unsigned long start;
  86. unsigned long end;
  87. };
  88. static struct rb_root swsusp_extents = RB_ROOT;
  89. static int swsusp_extents_insert(unsigned long swap_offset)
  90. {
  91. struct rb_node **new = &(swsusp_extents.rb_node);
  92. struct rb_node *parent = NULL;
  93. struct swsusp_extent *ext;
  94. /* Figure out where to put the new node */
  95. while (*new) {
  96. ext = container_of(*new, struct swsusp_extent, node);
  97. parent = *new;
  98. if (swap_offset < ext->start) {
  99. /* Try to merge */
  100. if (swap_offset == ext->start - 1) {
  101. ext->start--;
  102. return 0;
  103. }
  104. new = &((*new)->rb_left);
  105. } else if (swap_offset > ext->end) {
  106. /* Try to merge */
  107. if (swap_offset == ext->end + 1) {
  108. ext->end++;
  109. return 0;
  110. }
  111. new = &((*new)->rb_right);
  112. } else {
  113. /* It already is in the tree */
  114. return -EINVAL;
  115. }
  116. }
  117. /* Add the new node and rebalance the tree. */
  118. ext = kzalloc(sizeof(struct swsusp_extent), GFP_KERNEL);
  119. if (!ext)
  120. return -ENOMEM;
  121. ext->start = swap_offset;
  122. ext->end = swap_offset;
  123. rb_link_node(&ext->node, parent, new);
  124. rb_insert_color(&ext->node, &swsusp_extents);
  125. return 0;
  126. }
  127. /**
  128. * alloc_swapdev_block - allocate a swap page and register that it has
  129. * been allocated, so that it can be freed in case of an error.
  130. */
  131. sector_t alloc_swapdev_block(int swap)
  132. {
  133. unsigned long offset;
  134. offset = swp_offset(get_swap_page_of_type(swap));
  135. if (offset) {
  136. if (swsusp_extents_insert(offset))
  137. swap_free(swp_entry(swap, offset));
  138. else
  139. return swapdev_block(swap, offset);
  140. }
  141. return 0;
  142. }
  143. /**
  144. * free_all_swap_pages - free swap pages allocated for saving image data.
  145. * It also frees the extents used to register which swap entries had been
  146. * allocated.
  147. */
  148. void free_all_swap_pages(int swap)
  149. {
  150. struct rb_node *node;
  151. while ((node = swsusp_extents.rb_node)) {
  152. struct swsusp_extent *ext;
  153. unsigned long offset;
  154. ext = container_of(node, struct swsusp_extent, node);
  155. rb_erase(node, &swsusp_extents);
  156. for (offset = ext->start; offset <= ext->end; offset++)
  157. swap_free(swp_entry(swap, offset));
  158. kfree(ext);
  159. }
  160. }
  161. int swsusp_swap_in_use(void)
  162. {
  163. return (swsusp_extents.rb_node != NULL);
  164. }
  165. /*
  166. * General things
  167. */
  168. static unsigned short root_swap = 0xffff;
  169. struct block_device *hib_resume_bdev;
  170. /*
  171. * Saving part
  172. */
  173. static int mark_swapfiles(struct swap_map_handle *handle, unsigned int flags)
  174. {
  175. int error;
  176. hib_bio_read_page(swsusp_resume_block, swsusp_header, NULL);
  177. if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) ||
  178. !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) {
  179. memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10);
  180. memcpy(swsusp_header->sig, HIBERNATE_SIG, 10);
  181. swsusp_header->image = handle->first_sector;
  182. swsusp_header->flags = flags;
  183. if (flags & SF_CRC32_MODE)
  184. swsusp_header->crc32 = handle->crc32;
  185. error = hib_bio_write_page(swsusp_resume_block,
  186. swsusp_header, NULL);
  187. } else {
  188. printk(KERN_ERR "PM: Swap header not found!\n");
  189. error = -ENODEV;
  190. }
  191. return error;
  192. }
  193. /**
  194. * swsusp_swap_check - check if the resume device is a swap device
  195. * and get its index (if so)
  196. *
  197. * This is called before saving image
  198. */
  199. static int swsusp_swap_check(void)
  200. {
  201. int res;
  202. res = swap_type_of(swsusp_resume_device, swsusp_resume_block,
  203. &hib_resume_bdev);
  204. if (res < 0)
  205. return res;
  206. root_swap = res;
  207. res = blkdev_get(hib_resume_bdev, FMODE_WRITE, NULL);
  208. if (res)
  209. return res;
  210. res = set_blocksize(hib_resume_bdev, PAGE_SIZE);
  211. if (res < 0)
  212. blkdev_put(hib_resume_bdev, FMODE_WRITE);
  213. return res;
  214. }
  215. /**
  216. * write_page - Write one page to given swap location.
  217. * @buf: Address we're writing.
  218. * @offset: Offset of the swap page we're writing to.
  219. * @bio_chain: Link the next write BIO here
  220. */
  221. static int write_page(void *buf, sector_t offset, struct bio **bio_chain)
  222. {
  223. void *src;
  224. int ret;
  225. if (!offset)
  226. return -ENOSPC;
  227. if (bio_chain) {
  228. src = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
  229. if (src) {
  230. copy_page(src, buf);
  231. } else {
  232. ret = hib_wait_on_bio_chain(bio_chain); /* Free pages */
  233. if (ret)
  234. return ret;
  235. src = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
  236. if (src) {
  237. copy_page(src, buf);
  238. } else {
  239. WARN_ON_ONCE(1);
  240. bio_chain = NULL; /* Go synchronous */
  241. src = buf;
  242. }
  243. }
  244. } else {
  245. src = buf;
  246. }
  247. return hib_bio_write_page(offset, src, bio_chain);
  248. }
  249. static void release_swap_writer(struct swap_map_handle *handle)
  250. {
  251. if (handle->cur)
  252. free_page((unsigned long)handle->cur);
  253. handle->cur = NULL;
  254. }
  255. static int get_swap_writer(struct swap_map_handle *handle)
  256. {
  257. int ret;
  258. ret = swsusp_swap_check();
  259. if (ret) {
  260. if (ret != -ENOSPC)
  261. printk(KERN_ERR "PM: Cannot find swap device, try "
  262. "swapon -a.\n");
  263. return ret;
  264. }
  265. handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
  266. if (!handle->cur) {
  267. ret = -ENOMEM;
  268. goto err_close;
  269. }
  270. handle->cur_swap = alloc_swapdev_block(root_swap);
  271. if (!handle->cur_swap) {
  272. ret = -ENOSPC;
  273. goto err_rel;
  274. }
  275. handle->k = 0;
  276. handle->nr_free_pages = nr_free_pages() >> 1;
  277. handle->written = 0;
  278. handle->first_sector = handle->cur_swap;
  279. return 0;
  280. err_rel:
  281. release_swap_writer(handle);
  282. err_close:
  283. swsusp_close(FMODE_WRITE);
  284. return ret;
  285. }
  286. static int swap_write_page(struct swap_map_handle *handle, void *buf,
  287. struct bio **bio_chain)
  288. {
  289. int error = 0;
  290. sector_t offset;
  291. if (!handle->cur)
  292. return -EINVAL;
  293. offset = alloc_swapdev_block(root_swap);
  294. error = write_page(buf, offset, bio_chain);
  295. if (error)
  296. return error;
  297. handle->cur->entries[handle->k++] = offset;
  298. if (handle->k >= MAP_PAGE_ENTRIES) {
  299. offset = alloc_swapdev_block(root_swap);
  300. if (!offset)
  301. return -ENOSPC;
  302. handle->cur->next_swap = offset;
  303. error = write_page(handle->cur, handle->cur_swap, bio_chain);
  304. if (error)
  305. goto out;
  306. clear_page(handle->cur);
  307. handle->cur_swap = offset;
  308. handle->k = 0;
  309. }
  310. if (bio_chain && ++handle->written > handle->nr_free_pages) {
  311. error = hib_wait_on_bio_chain(bio_chain);
  312. if (error)
  313. goto out;
  314. handle->written = 0;
  315. }
  316. out:
  317. return error;
  318. }
  319. static int flush_swap_writer(struct swap_map_handle *handle)
  320. {
  321. if (handle->cur && handle->cur_swap)
  322. return write_page(handle->cur, handle->cur_swap, NULL);
  323. else
  324. return -EINVAL;
  325. }
  326. static int swap_writer_finish(struct swap_map_handle *handle,
  327. unsigned int flags, int error)
  328. {
  329. if (!error) {
  330. flush_swap_writer(handle);
  331. printk(KERN_INFO "PM: S");
  332. error = mark_swapfiles(handle, flags);
  333. printk("|\n");
  334. }
  335. if (error)
  336. free_all_swap_pages(root_swap);
  337. release_swap_writer(handle);
  338. swsusp_close(FMODE_WRITE);
  339. return error;
  340. }
  341. /* We need to remember how much compressed data we need to read. */
  342. #define LZO_HEADER sizeof(size_t)
  343. /* Number of pages/bytes we'll compress at one time. */
  344. #define LZO_UNC_PAGES 32
  345. #define LZO_UNC_SIZE (LZO_UNC_PAGES * PAGE_SIZE)
  346. /* Number of pages/bytes we need for compressed data (worst case). */
  347. #define LZO_CMP_PAGES DIV_ROUND_UP(lzo1x_worst_compress(LZO_UNC_SIZE) + \
  348. LZO_HEADER, PAGE_SIZE)
  349. #define LZO_CMP_SIZE (LZO_CMP_PAGES * PAGE_SIZE)
  350. /* Maximum number of threads for compression/decompression. */
  351. #define LZO_THREADS 3
  352. /* Maximum number of pages for read buffering. */
  353. #define LZO_READ_PAGES (MAP_PAGE_ENTRIES * 8)
  354. /**
  355. * save_image - save the suspend image data
  356. */
  357. static int save_image(struct swap_map_handle *handle,
  358. struct snapshot_handle *snapshot,
  359. unsigned int nr_to_write)
  360. {
  361. unsigned int m;
  362. int ret;
  363. int nr_pages;
  364. int err2;
  365. struct bio *bio;
  366. struct timeval start;
  367. struct timeval stop;
  368. printk(KERN_INFO "PM: Saving image data pages (%u pages) ... ",
  369. nr_to_write);
  370. m = nr_to_write / 100;
  371. if (!m)
  372. m = 1;
  373. nr_pages = 0;
  374. bio = NULL;
  375. do_gettimeofday(&start);
  376. while (1) {
  377. ret = snapshot_read_next(snapshot);
  378. if (ret <= 0)
  379. break;
  380. ret = swap_write_page(handle, data_of(*snapshot), &bio);
  381. if (ret)
  382. break;
  383. if (!(nr_pages % m))
  384. printk(KERN_CONT "\b\b\b\b%3d%%", nr_pages / m);
  385. nr_pages++;
  386. }
  387. err2 = hib_wait_on_bio_chain(&bio);
  388. do_gettimeofday(&stop);
  389. if (!ret)
  390. ret = err2;
  391. if (!ret)
  392. printk(KERN_CONT "\b\b\b\bdone\n");
  393. else
  394. printk(KERN_CONT "\n");
  395. swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
  396. return ret;
  397. }
  398. /**
  399. * Structure used for CRC32.
  400. */
  401. struct crc_data {
  402. struct task_struct *thr; /* thread */
  403. atomic_t ready; /* ready to start flag */
  404. atomic_t stop; /* ready to stop flag */
  405. unsigned run_threads; /* nr current threads */
  406. wait_queue_head_t go; /* start crc update */
  407. wait_queue_head_t done; /* crc update done */
  408. u32 *crc32; /* points to handle's crc32 */
  409. size_t *unc_len[LZO_THREADS]; /* uncompressed lengths */
  410. unsigned char *unc[LZO_THREADS]; /* uncompressed data */
  411. };
  412. /**
  413. * CRC32 update function that runs in its own thread.
  414. */
  415. static int crc32_threadfn(void *data)
  416. {
  417. struct crc_data *d = data;
  418. unsigned i;
  419. while (1) {
  420. wait_event(d->go, atomic_read(&d->ready) ||
  421. kthread_should_stop());
  422. if (kthread_should_stop()) {
  423. d->thr = NULL;
  424. atomic_set(&d->stop, 1);
  425. wake_up(&d->done);
  426. break;
  427. }
  428. atomic_set(&d->ready, 0);
  429. for (i = 0; i < d->run_threads; i++)
  430. *d->crc32 = crc32_le(*d->crc32,
  431. d->unc[i], *d->unc_len[i]);
  432. atomic_set(&d->stop, 1);
  433. wake_up(&d->done);
  434. }
  435. return 0;
  436. }
  437. /**
  438. * Structure used for LZO data compression.
  439. */
  440. struct cmp_data {
  441. struct task_struct *thr; /* thread */
  442. atomic_t ready; /* ready to start flag */
  443. atomic_t stop; /* ready to stop flag */
  444. int ret; /* return code */
  445. wait_queue_head_t go; /* start compression */
  446. wait_queue_head_t done; /* compression done */
  447. size_t unc_len; /* uncompressed length */
  448. size_t cmp_len; /* compressed length */
  449. unsigned char unc[LZO_UNC_SIZE]; /* uncompressed buffer */
  450. unsigned char cmp[LZO_CMP_SIZE]; /* compressed buffer */
  451. unsigned char wrk[LZO1X_1_MEM_COMPRESS]; /* compression workspace */
  452. };
  453. /**
  454. * Compression function that runs in its own thread.
  455. */
  456. static int lzo_compress_threadfn(void *data)
  457. {
  458. struct cmp_data *d = data;
  459. while (1) {
  460. wait_event(d->go, atomic_read(&d->ready) ||
  461. kthread_should_stop());
  462. if (kthread_should_stop()) {
  463. d->thr = NULL;
  464. d->ret = -1;
  465. atomic_set(&d->stop, 1);
  466. wake_up(&d->done);
  467. break;
  468. }
  469. atomic_set(&d->ready, 0);
  470. d->ret = lzo1x_1_compress(d->unc, d->unc_len,
  471. d->cmp + LZO_HEADER, &d->cmp_len,
  472. d->wrk);
  473. atomic_set(&d->stop, 1);
  474. wake_up(&d->done);
  475. }
  476. return 0;
  477. }
  478. /**
  479. * save_image_lzo - Save the suspend image data compressed with LZO.
  480. * @handle: Swap mam handle to use for saving the image.
  481. * @snapshot: Image to read data from.
  482. * @nr_to_write: Number of pages to save.
  483. */
  484. static int save_image_lzo(struct swap_map_handle *handle,
  485. struct snapshot_handle *snapshot,
  486. unsigned int nr_to_write)
  487. {
  488. unsigned int m;
  489. int ret = 0;
  490. int nr_pages;
  491. int err2;
  492. struct bio *bio;
  493. struct timeval start;
  494. struct timeval stop;
  495. size_t off;
  496. unsigned thr, run_threads, nr_threads;
  497. unsigned char *page = NULL;
  498. struct cmp_data *data = NULL;
  499. struct crc_data *crc = NULL;
  500. /*
  501. * We'll limit the number of threads for compression to limit memory
  502. * footprint.
  503. */
  504. nr_threads = num_online_cpus() - 1;
  505. nr_threads = clamp_val(nr_threads, 1, LZO_THREADS);
  506. page = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
  507. if (!page) {
  508. printk(KERN_ERR "PM: Failed to allocate LZO page\n");
  509. ret = -ENOMEM;
  510. goto out_clean;
  511. }
  512. data = vmalloc(sizeof(*data) * nr_threads);
  513. if (!data) {
  514. printk(KERN_ERR "PM: Failed to allocate LZO data\n");
  515. ret = -ENOMEM;
  516. goto out_clean;
  517. }
  518. for (thr = 0; thr < nr_threads; thr++)
  519. memset(&data[thr], 0, offsetof(struct cmp_data, go));
  520. crc = kmalloc(sizeof(*crc), GFP_KERNEL);
  521. if (!crc) {
  522. printk(KERN_ERR "PM: Failed to allocate crc\n");
  523. ret = -ENOMEM;
  524. goto out_clean;
  525. }
  526. memset(crc, 0, offsetof(struct crc_data, go));
  527. /*
  528. * Start the compression threads.
  529. */
  530. for (thr = 0; thr < nr_threads; thr++) {
  531. init_waitqueue_head(&data[thr].go);
  532. init_waitqueue_head(&data[thr].done);
  533. data[thr].thr = kthread_run(lzo_compress_threadfn,
  534. &data[thr],
  535. "image_compress/%u", thr);
  536. if (IS_ERR(data[thr].thr)) {
  537. data[thr].thr = NULL;
  538. printk(KERN_ERR
  539. "PM: Cannot start compression threads\n");
  540. ret = -ENOMEM;
  541. goto out_clean;
  542. }
  543. }
  544. /*
  545. * Adjust number of free pages after all allocations have been done.
  546. * We don't want to run out of pages when writing.
  547. */
  548. handle->nr_free_pages = nr_free_pages() >> 1;
  549. /*
  550. * Start the CRC32 thread.
  551. */
  552. init_waitqueue_head(&crc->go);
  553. init_waitqueue_head(&crc->done);
  554. handle->crc32 = 0;
  555. crc->crc32 = &handle->crc32;
  556. for (thr = 0; thr < nr_threads; thr++) {
  557. crc->unc[thr] = data[thr].unc;
  558. crc->unc_len[thr] = &data[thr].unc_len;
  559. }
  560. crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
  561. if (IS_ERR(crc->thr)) {
  562. crc->thr = NULL;
  563. printk(KERN_ERR "PM: Cannot start CRC32 thread\n");
  564. ret = -ENOMEM;
  565. goto out_clean;
  566. }
  567. printk(KERN_INFO
  568. "PM: Using %u thread(s) for compression.\n"
  569. "PM: Compressing and saving image data (%u pages) ... ",
  570. nr_threads, nr_to_write);
  571. m = nr_to_write / 100;
  572. if (!m)
  573. m = 1;
  574. nr_pages = 0;
  575. bio = NULL;
  576. do_gettimeofday(&start);
  577. for (;;) {
  578. for (thr = 0; thr < nr_threads; thr++) {
  579. for (off = 0; off < LZO_UNC_SIZE; off += PAGE_SIZE) {
  580. ret = snapshot_read_next(snapshot);
  581. if (ret < 0)
  582. goto out_finish;
  583. if (!ret)
  584. break;
  585. memcpy(data[thr].unc + off,
  586. data_of(*snapshot), PAGE_SIZE);
  587. if (!(nr_pages % m))
  588. printk(KERN_CONT "\b\b\b\b%3d%%",
  589. nr_pages / m);
  590. nr_pages++;
  591. }
  592. if (!off)
  593. break;
  594. data[thr].unc_len = off;
  595. atomic_set(&data[thr].ready, 1);
  596. wake_up(&data[thr].go);
  597. }
  598. if (!thr)
  599. break;
  600. crc->run_threads = thr;
  601. atomic_set(&crc->ready, 1);
  602. wake_up(&crc->go);
  603. for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
  604. wait_event(data[thr].done,
  605. atomic_read(&data[thr].stop));
  606. atomic_set(&data[thr].stop, 0);
  607. ret = data[thr].ret;
  608. if (ret < 0) {
  609. printk(KERN_ERR "PM: LZO compression failed\n");
  610. goto out_finish;
  611. }
  612. if (unlikely(!data[thr].cmp_len ||
  613. data[thr].cmp_len >
  614. lzo1x_worst_compress(data[thr].unc_len))) {
  615. printk(KERN_ERR
  616. "PM: Invalid LZO compressed length\n");
  617. ret = -1;
  618. goto out_finish;
  619. }
  620. *(size_t *)data[thr].cmp = data[thr].cmp_len;
  621. /*
  622. * Given we are writing one page at a time to disk, we
  623. * copy that much from the buffer, although the last
  624. * bit will likely be smaller than full page. This is
  625. * OK - we saved the length of the compressed data, so
  626. * any garbage at the end will be discarded when we
  627. * read it.
  628. */
  629. for (off = 0;
  630. off < LZO_HEADER + data[thr].cmp_len;
  631. off += PAGE_SIZE) {
  632. memcpy(page, data[thr].cmp + off, PAGE_SIZE);
  633. ret = swap_write_page(handle, page, &bio);
  634. if (ret)
  635. goto out_finish;
  636. }
  637. }
  638. wait_event(crc->done, atomic_read(&crc->stop));
  639. atomic_set(&crc->stop, 0);
  640. }
  641. out_finish:
  642. err2 = hib_wait_on_bio_chain(&bio);
  643. do_gettimeofday(&stop);
  644. if (!ret)
  645. ret = err2;
  646. if (!ret) {
  647. printk(KERN_CONT "\b\b\b\bdone\n");
  648. } else {
  649. printk(KERN_CONT "\n");
  650. }
  651. swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
  652. out_clean:
  653. if (crc) {
  654. if (crc->thr)
  655. kthread_stop(crc->thr);
  656. kfree(crc);
  657. }
  658. if (data) {
  659. for (thr = 0; thr < nr_threads; thr++)
  660. if (data[thr].thr)
  661. kthread_stop(data[thr].thr);
  662. vfree(data);
  663. }
  664. if (page) free_page((unsigned long)page);
  665. return ret;
  666. }
  667. /**
  668. * enough_swap - Make sure we have enough swap to save the image.
  669. *
  670. * Returns TRUE or FALSE after checking the total amount of swap
  671. * space avaiable from the resume partition.
  672. */
  673. static int enough_swap(unsigned int nr_pages, unsigned int flags)
  674. {
  675. unsigned int free_swap = count_swap_pages(root_swap, 1);
  676. unsigned int required;
  677. pr_debug("PM: Free swap pages: %u\n", free_swap);
  678. required = PAGES_FOR_IO + ((flags & SF_NOCOMPRESS_MODE) ?
  679. nr_pages : (nr_pages * LZO_CMP_PAGES) / LZO_UNC_PAGES + 1);
  680. return free_swap > required;
  681. }
  682. /**
  683. * swsusp_write - Write entire image and metadata.
  684. * @flags: flags to pass to the "boot" kernel in the image header
  685. *
  686. * It is important _NOT_ to umount filesystems at this point. We want
  687. * them synced (in case something goes wrong) but we DO not want to mark
  688. * filesystem clean: it is not. (And it does not matter, if we resume
  689. * correctly, we'll mark system clean, anyway.)
  690. */
  691. int swsusp_write(unsigned int flags)
  692. {
  693. struct swap_map_handle handle;
  694. struct snapshot_handle snapshot;
  695. struct swsusp_info *header;
  696. unsigned long pages;
  697. int error;
  698. pages = snapshot_get_image_size();
  699. error = get_swap_writer(&handle);
  700. if (error) {
  701. printk(KERN_ERR "PM: Cannot get swap writer\n");
  702. return error;
  703. }
  704. if (!enough_swap(pages, flags)) {
  705. printk(KERN_ERR "PM: Not enough free swap\n");
  706. error = -ENOSPC;
  707. goto out_finish;
  708. }
  709. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  710. error = snapshot_read_next(&snapshot);
  711. if (error < PAGE_SIZE) {
  712. if (error >= 0)
  713. error = -EFAULT;
  714. goto out_finish;
  715. }
  716. header = (struct swsusp_info *)data_of(snapshot);
  717. error = swap_write_page(&handle, header, NULL);
  718. if (!error) {
  719. error = (flags & SF_NOCOMPRESS_MODE) ?
  720. save_image(&handle, &snapshot, pages - 1) :
  721. save_image_lzo(&handle, &snapshot, pages - 1);
  722. }
  723. out_finish:
  724. error = swap_writer_finish(&handle, flags, error);
  725. return error;
  726. }
  727. /**
  728. * The following functions allow us to read data using a swap map
  729. * in a file-alike way
  730. */
  731. static void release_swap_reader(struct swap_map_handle *handle)
  732. {
  733. struct swap_map_page_list *tmp;
  734. while (handle->maps) {
  735. if (handle->maps->map)
  736. free_page((unsigned long)handle->maps->map);
  737. tmp = handle->maps;
  738. handle->maps = handle->maps->next;
  739. kfree(tmp);
  740. }
  741. handle->cur = NULL;
  742. }
  743. static int get_swap_reader(struct swap_map_handle *handle,
  744. unsigned int *flags_p)
  745. {
  746. int error;
  747. struct swap_map_page_list *tmp, *last;
  748. sector_t offset;
  749. *flags_p = swsusp_header->flags;
  750. if (!swsusp_header->image) /* how can this happen? */
  751. return -EINVAL;
  752. handle->cur = NULL;
  753. last = handle->maps = NULL;
  754. offset = swsusp_header->image;
  755. while (offset) {
  756. tmp = kmalloc(sizeof(*handle->maps), GFP_KERNEL);
  757. if (!tmp) {
  758. release_swap_reader(handle);
  759. return -ENOMEM;
  760. }
  761. memset(tmp, 0, sizeof(*tmp));
  762. if (!handle->maps)
  763. handle->maps = tmp;
  764. if (last)
  765. last->next = tmp;
  766. last = tmp;
  767. tmp->map = (struct swap_map_page *)
  768. __get_free_page(__GFP_WAIT | __GFP_HIGH);
  769. if (!tmp->map) {
  770. release_swap_reader(handle);
  771. return -ENOMEM;
  772. }
  773. error = hib_bio_read_page(offset, tmp->map, NULL);
  774. if (error) {
  775. release_swap_reader(handle);
  776. return error;
  777. }
  778. offset = tmp->map->next_swap;
  779. }
  780. handle->k = 0;
  781. handle->cur = handle->maps->map;
  782. return 0;
  783. }
  784. static int swap_read_page(struct swap_map_handle *handle, void *buf,
  785. struct bio **bio_chain)
  786. {
  787. sector_t offset;
  788. int error;
  789. struct swap_map_page_list *tmp;
  790. if (!handle->cur)
  791. return -EINVAL;
  792. offset = handle->cur->entries[handle->k];
  793. if (!offset)
  794. return -EFAULT;
  795. error = hib_bio_read_page(offset, buf, bio_chain);
  796. if (error)
  797. return error;
  798. if (++handle->k >= MAP_PAGE_ENTRIES) {
  799. handle->k = 0;
  800. free_page((unsigned long)handle->maps->map);
  801. tmp = handle->maps;
  802. handle->maps = handle->maps->next;
  803. kfree(tmp);
  804. if (!handle->maps)
  805. release_swap_reader(handle);
  806. else
  807. handle->cur = handle->maps->map;
  808. }
  809. return error;
  810. }
  811. static int swap_reader_finish(struct swap_map_handle *handle)
  812. {
  813. release_swap_reader(handle);
  814. return 0;
  815. }
  816. /**
  817. * load_image - load the image using the swap map handle
  818. * @handle and the snapshot handle @snapshot
  819. * (assume there are @nr_pages pages to load)
  820. */
  821. static int load_image(struct swap_map_handle *handle,
  822. struct snapshot_handle *snapshot,
  823. unsigned int nr_to_read)
  824. {
  825. unsigned int m;
  826. int ret = 0;
  827. struct timeval start;
  828. struct timeval stop;
  829. struct bio *bio;
  830. int err2;
  831. unsigned nr_pages;
  832. printk(KERN_INFO "PM: Loading image data pages (%u pages) ... ",
  833. nr_to_read);
  834. m = nr_to_read / 100;
  835. if (!m)
  836. m = 1;
  837. nr_pages = 0;
  838. bio = NULL;
  839. do_gettimeofday(&start);
  840. for ( ; ; ) {
  841. ret = snapshot_write_next(snapshot);
  842. if (ret <= 0)
  843. break;
  844. ret = swap_read_page(handle, data_of(*snapshot), &bio);
  845. if (ret)
  846. break;
  847. if (snapshot->sync_read)
  848. ret = hib_wait_on_bio_chain(&bio);
  849. if (ret)
  850. break;
  851. if (!(nr_pages % m))
  852. printk("\b\b\b\b%3d%%", nr_pages / m);
  853. nr_pages++;
  854. }
  855. err2 = hib_wait_on_bio_chain(&bio);
  856. do_gettimeofday(&stop);
  857. if (!ret)
  858. ret = err2;
  859. if (!ret) {
  860. printk("\b\b\b\bdone\n");
  861. snapshot_write_finalize(snapshot);
  862. if (!snapshot_image_loaded(snapshot))
  863. ret = -ENODATA;
  864. } else
  865. printk("\n");
  866. swsusp_show_speed(&start, &stop, nr_to_read, "Read");
  867. return ret;
  868. }
  869. /**
  870. * Structure used for LZO data decompression.
  871. */
  872. struct dec_data {
  873. struct task_struct *thr; /* thread */
  874. atomic_t ready; /* ready to start flag */
  875. atomic_t stop; /* ready to stop flag */
  876. int ret; /* return code */
  877. wait_queue_head_t go; /* start decompression */
  878. wait_queue_head_t done; /* decompression done */
  879. size_t unc_len; /* uncompressed length */
  880. size_t cmp_len; /* compressed length */
  881. unsigned char unc[LZO_UNC_SIZE]; /* uncompressed buffer */
  882. unsigned char cmp[LZO_CMP_SIZE]; /* compressed buffer */
  883. };
  884. /**
  885. * Deompression function that runs in its own thread.
  886. */
  887. static int lzo_decompress_threadfn(void *data)
  888. {
  889. struct dec_data *d = data;
  890. while (1) {
  891. wait_event(d->go, atomic_read(&d->ready) ||
  892. kthread_should_stop());
  893. if (kthread_should_stop()) {
  894. d->thr = NULL;
  895. d->ret = -1;
  896. atomic_set(&d->stop, 1);
  897. wake_up(&d->done);
  898. break;
  899. }
  900. atomic_set(&d->ready, 0);
  901. d->unc_len = LZO_UNC_SIZE;
  902. d->ret = lzo1x_decompress_safe(d->cmp + LZO_HEADER, d->cmp_len,
  903. d->unc, &d->unc_len);
  904. atomic_set(&d->stop, 1);
  905. wake_up(&d->done);
  906. }
  907. return 0;
  908. }
  909. /**
  910. * load_image_lzo - Load compressed image data and decompress them with LZO.
  911. * @handle: Swap map handle to use for loading data.
  912. * @snapshot: Image to copy uncompressed data into.
  913. * @nr_to_read: Number of pages to load.
  914. */
  915. static int load_image_lzo(struct swap_map_handle *handle,
  916. struct snapshot_handle *snapshot,
  917. unsigned int nr_to_read)
  918. {
  919. unsigned int m;
  920. int ret = 0;
  921. int eof = 0;
  922. struct bio *bio;
  923. struct timeval start;
  924. struct timeval stop;
  925. unsigned nr_pages;
  926. size_t off;
  927. unsigned i, thr, run_threads, nr_threads;
  928. unsigned ring = 0, pg = 0, ring_size = 0,
  929. have = 0, want, need, asked = 0;
  930. unsigned long read_pages;
  931. unsigned char **page = NULL;
  932. struct dec_data *data = NULL;
  933. struct crc_data *crc = NULL;
  934. /*
  935. * We'll limit the number of threads for decompression to limit memory
  936. * footprint.
  937. */
  938. nr_threads = num_online_cpus() - 1;
  939. nr_threads = clamp_val(nr_threads, 1, LZO_THREADS);
  940. page = vmalloc(sizeof(*page) * LZO_READ_PAGES);
  941. if (!page) {
  942. printk(KERN_ERR "PM: Failed to allocate LZO page\n");
  943. ret = -ENOMEM;
  944. goto out_clean;
  945. }
  946. data = vmalloc(sizeof(*data) * nr_threads);
  947. if (!data) {
  948. printk(KERN_ERR "PM: Failed to allocate LZO data\n");
  949. ret = -ENOMEM;
  950. goto out_clean;
  951. }
  952. for (thr = 0; thr < nr_threads; thr++)
  953. memset(&data[thr], 0, offsetof(struct dec_data, go));
  954. crc = kmalloc(sizeof(*crc), GFP_KERNEL);
  955. if (!crc) {
  956. printk(KERN_ERR "PM: Failed to allocate crc\n");
  957. ret = -ENOMEM;
  958. goto out_clean;
  959. }
  960. memset(crc, 0, offsetof(struct crc_data, go));
  961. /*
  962. * Start the decompression threads.
  963. */
  964. for (thr = 0; thr < nr_threads; thr++) {
  965. init_waitqueue_head(&data[thr].go);
  966. init_waitqueue_head(&data[thr].done);
  967. data[thr].thr = kthread_run(lzo_decompress_threadfn,
  968. &data[thr],
  969. "image_decompress/%u", thr);
  970. if (IS_ERR(data[thr].thr)) {
  971. data[thr].thr = NULL;
  972. printk(KERN_ERR
  973. "PM: Cannot start decompression threads\n");
  974. ret = -ENOMEM;
  975. goto out_clean;
  976. }
  977. }
  978. /*
  979. * Start the CRC32 thread.
  980. */
  981. init_waitqueue_head(&crc->go);
  982. init_waitqueue_head(&crc->done);
  983. handle->crc32 = 0;
  984. crc->crc32 = &handle->crc32;
  985. for (thr = 0; thr < nr_threads; thr++) {
  986. crc->unc[thr] = data[thr].unc;
  987. crc->unc_len[thr] = &data[thr].unc_len;
  988. }
  989. crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
  990. if (IS_ERR(crc->thr)) {
  991. crc->thr = NULL;
  992. printk(KERN_ERR "PM: Cannot start CRC32 thread\n");
  993. ret = -ENOMEM;
  994. goto out_clean;
  995. }
  996. /*
  997. * Adjust number of pages for read buffering, in case we are short.
  998. */
  999. read_pages = (nr_free_pages() - snapshot_get_image_size()) >> 1;
  1000. read_pages = clamp_val(read_pages, LZO_CMP_PAGES, LZO_READ_PAGES);
  1001. for (i = 0; i < read_pages; i++) {
  1002. page[i] = (void *)__get_free_page(i < LZO_CMP_PAGES ?
  1003. __GFP_WAIT | __GFP_HIGH :
  1004. __GFP_WAIT);
  1005. if (!page[i]) {
  1006. if (i < LZO_CMP_PAGES) {
  1007. ring_size = i;
  1008. printk(KERN_ERR
  1009. "PM: Failed to allocate LZO pages\n");
  1010. ret = -ENOMEM;
  1011. goto out_clean;
  1012. } else {
  1013. break;
  1014. }
  1015. }
  1016. }
  1017. want = ring_size = i;
  1018. printk(KERN_INFO
  1019. "PM: Using %u thread(s) for decompression.\n"
  1020. "PM: Loading and decompressing image data (%u pages) ... ",
  1021. nr_threads, nr_to_read);
  1022. m = nr_to_read / 100;
  1023. if (!m)
  1024. m = 1;
  1025. nr_pages = 0;
  1026. bio = NULL;
  1027. do_gettimeofday(&start);
  1028. ret = snapshot_write_next(snapshot);
  1029. if (ret <= 0)
  1030. goto out_finish;
  1031. for(;;) {
  1032. for (i = 0; !eof && i < want; i++) {
  1033. ret = swap_read_page(handle, page[ring], &bio);
  1034. if (ret) {
  1035. /*
  1036. * On real read error, finish. On end of data,
  1037. * set EOF flag and just exit the read loop.
  1038. */
  1039. if (handle->cur &&
  1040. handle->cur->entries[handle->k]) {
  1041. goto out_finish;
  1042. } else {
  1043. eof = 1;
  1044. break;
  1045. }
  1046. }
  1047. if (++ring >= ring_size)
  1048. ring = 0;
  1049. }
  1050. asked += i;
  1051. want -= i;
  1052. /*
  1053. * We are out of data, wait for some more.
  1054. */
  1055. if (!have) {
  1056. if (!asked)
  1057. break;
  1058. ret = hib_wait_on_bio_chain(&bio);
  1059. if (ret)
  1060. goto out_finish;
  1061. have += asked;
  1062. asked = 0;
  1063. if (eof)
  1064. eof = 2;
  1065. }
  1066. if (crc->run_threads) {
  1067. wait_event(crc->done, atomic_read(&crc->stop));
  1068. atomic_set(&crc->stop, 0);
  1069. crc->run_threads = 0;
  1070. }
  1071. for (thr = 0; have && thr < nr_threads; thr++) {
  1072. data[thr].cmp_len = *(size_t *)page[pg];
  1073. if (unlikely(!data[thr].cmp_len ||
  1074. data[thr].cmp_len >
  1075. lzo1x_worst_compress(LZO_UNC_SIZE))) {
  1076. printk(KERN_ERR
  1077. "PM: Invalid LZO compressed length\n");
  1078. ret = -1;
  1079. goto out_finish;
  1080. }
  1081. need = DIV_ROUND_UP(data[thr].cmp_len + LZO_HEADER,
  1082. PAGE_SIZE);
  1083. if (need > have) {
  1084. if (eof > 1) {
  1085. ret = -1;
  1086. goto out_finish;
  1087. }
  1088. break;
  1089. }
  1090. for (off = 0;
  1091. off < LZO_HEADER + data[thr].cmp_len;
  1092. off += PAGE_SIZE) {
  1093. memcpy(data[thr].cmp + off,
  1094. page[pg], PAGE_SIZE);
  1095. have--;
  1096. want++;
  1097. if (++pg >= ring_size)
  1098. pg = 0;
  1099. }
  1100. atomic_set(&data[thr].ready, 1);
  1101. wake_up(&data[thr].go);
  1102. }
  1103. /*
  1104. * Wait for more data while we are decompressing.
  1105. */
  1106. if (have < LZO_CMP_PAGES && asked) {
  1107. ret = hib_wait_on_bio_chain(&bio);
  1108. if (ret)
  1109. goto out_finish;
  1110. have += asked;
  1111. asked = 0;
  1112. if (eof)
  1113. eof = 2;
  1114. }
  1115. for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
  1116. wait_event(data[thr].done,
  1117. atomic_read(&data[thr].stop));
  1118. atomic_set(&data[thr].stop, 0);
  1119. ret = data[thr].ret;
  1120. if (ret < 0) {
  1121. printk(KERN_ERR
  1122. "PM: LZO decompression failed\n");
  1123. goto out_finish;
  1124. }
  1125. if (unlikely(!data[thr].unc_len ||
  1126. data[thr].unc_len > LZO_UNC_SIZE ||
  1127. data[thr].unc_len & (PAGE_SIZE - 1))) {
  1128. printk(KERN_ERR
  1129. "PM: Invalid LZO uncompressed length\n");
  1130. ret = -1;
  1131. goto out_finish;
  1132. }
  1133. for (off = 0;
  1134. off < data[thr].unc_len; off += PAGE_SIZE) {
  1135. memcpy(data_of(*snapshot),
  1136. data[thr].unc + off, PAGE_SIZE);
  1137. if (!(nr_pages % m))
  1138. printk("\b\b\b\b%3d%%", nr_pages / m);
  1139. nr_pages++;
  1140. ret = snapshot_write_next(snapshot);
  1141. if (ret <= 0) {
  1142. crc->run_threads = thr + 1;
  1143. atomic_set(&crc->ready, 1);
  1144. wake_up(&crc->go);
  1145. goto out_finish;
  1146. }
  1147. }
  1148. }
  1149. crc->run_threads = thr;
  1150. atomic_set(&crc->ready, 1);
  1151. wake_up(&crc->go);
  1152. }
  1153. out_finish:
  1154. if (crc->run_threads) {
  1155. wait_event(crc->done, atomic_read(&crc->stop));
  1156. atomic_set(&crc->stop, 0);
  1157. }
  1158. do_gettimeofday(&stop);
  1159. if (!ret) {
  1160. printk("\b\b\b\bdone\n");
  1161. snapshot_write_finalize(snapshot);
  1162. if (!snapshot_image_loaded(snapshot))
  1163. ret = -ENODATA;
  1164. if (!ret) {
  1165. if (swsusp_header->flags & SF_CRC32_MODE) {
  1166. if(handle->crc32 != swsusp_header->crc32) {
  1167. printk(KERN_ERR
  1168. "PM: Invalid image CRC32!\n");
  1169. ret = -ENODATA;
  1170. }
  1171. }
  1172. }
  1173. } else
  1174. printk("\n");
  1175. swsusp_show_speed(&start, &stop, nr_to_read, "Read");
  1176. out_clean:
  1177. for (i = 0; i < ring_size; i++)
  1178. free_page((unsigned long)page[i]);
  1179. if (crc) {
  1180. if (crc->thr)
  1181. kthread_stop(crc->thr);
  1182. kfree(crc);
  1183. }
  1184. if (data) {
  1185. for (thr = 0; thr < nr_threads; thr++)
  1186. if (data[thr].thr)
  1187. kthread_stop(data[thr].thr);
  1188. vfree(data);
  1189. }
  1190. if (page) vfree(page);
  1191. return ret;
  1192. }
  1193. /**
  1194. * swsusp_read - read the hibernation image.
  1195. * @flags_p: flags passed by the "frozen" kernel in the image header should
  1196. * be written into this memory location
  1197. */
  1198. int swsusp_read(unsigned int *flags_p)
  1199. {
  1200. int error;
  1201. struct swap_map_handle handle;
  1202. struct snapshot_handle snapshot;
  1203. struct swsusp_info *header;
  1204. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  1205. error = snapshot_write_next(&snapshot);
  1206. if (error < PAGE_SIZE)
  1207. return error < 0 ? error : -EFAULT;
  1208. header = (struct swsusp_info *)data_of(snapshot);
  1209. error = get_swap_reader(&handle, flags_p);
  1210. if (error)
  1211. goto end;
  1212. if (!error)
  1213. error = swap_read_page(&handle, header, NULL);
  1214. if (!error) {
  1215. error = (*flags_p & SF_NOCOMPRESS_MODE) ?
  1216. load_image(&handle, &snapshot, header->pages - 1) :
  1217. load_image_lzo(&handle, &snapshot, header->pages - 1);
  1218. }
  1219. swap_reader_finish(&handle);
  1220. end:
  1221. if (!error)
  1222. pr_debug("PM: Image successfully loaded\n");
  1223. else
  1224. pr_debug("PM: Error %d resuming\n", error);
  1225. return error;
  1226. }
  1227. /**
  1228. * swsusp_check - Check for swsusp signature in the resume device
  1229. */
  1230. int swsusp_check(void)
  1231. {
  1232. int error;
  1233. hib_resume_bdev = blkdev_get_by_dev(swsusp_resume_device,
  1234. FMODE_READ, NULL);
  1235. if (!IS_ERR(hib_resume_bdev)) {
  1236. set_blocksize(hib_resume_bdev, PAGE_SIZE);
  1237. clear_page(swsusp_header);
  1238. error = hib_bio_read_page(swsusp_resume_block,
  1239. swsusp_header, NULL);
  1240. if (error)
  1241. goto put;
  1242. if (!memcmp(HIBERNATE_SIG, swsusp_header->sig, 10)) {
  1243. memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
  1244. /* Reset swap signature now */
  1245. error = hib_bio_write_page(swsusp_resume_block,
  1246. swsusp_header, NULL);
  1247. } else {
  1248. error = -EINVAL;
  1249. }
  1250. put:
  1251. if (error)
  1252. blkdev_put(hib_resume_bdev, FMODE_READ);
  1253. else
  1254. pr_debug("PM: Image signature found, resuming\n");
  1255. } else {
  1256. error = PTR_ERR(hib_resume_bdev);
  1257. }
  1258. if (error)
  1259. pr_debug("PM: Image not found (code %d)\n", error);
  1260. return error;
  1261. }
  1262. /**
  1263. * swsusp_close - close swap device.
  1264. */
  1265. void swsusp_close(fmode_t mode)
  1266. {
  1267. if (IS_ERR(hib_resume_bdev)) {
  1268. pr_debug("PM: Image device not initialised\n");
  1269. return;
  1270. }
  1271. blkdev_put(hib_resume_bdev, mode);
  1272. }
  1273. static int swsusp_header_init(void)
  1274. {
  1275. swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
  1276. if (!swsusp_header)
  1277. panic("Could not allocate memory for swsusp_header\n");
  1278. return 0;
  1279. }
  1280. core_initcall(swsusp_header_init);