swap.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  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 + nr_pages;
  679. return free_swap > required;
  680. }
  681. /**
  682. * swsusp_write - Write entire image and metadata.
  683. * @flags: flags to pass to the "boot" kernel in the image header
  684. *
  685. * It is important _NOT_ to umount filesystems at this point. We want
  686. * them synced (in case something goes wrong) but we DO not want to mark
  687. * filesystem clean: it is not. (And it does not matter, if we resume
  688. * correctly, we'll mark system clean, anyway.)
  689. */
  690. int swsusp_write(unsigned int flags)
  691. {
  692. struct swap_map_handle handle;
  693. struct snapshot_handle snapshot;
  694. struct swsusp_info *header;
  695. unsigned long pages;
  696. int error;
  697. pages = snapshot_get_image_size();
  698. error = get_swap_writer(&handle);
  699. if (error) {
  700. printk(KERN_ERR "PM: Cannot get swap writer\n");
  701. return error;
  702. }
  703. if (flags & SF_NOCOMPRESS_MODE) {
  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. }
  710. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  711. error = snapshot_read_next(&snapshot);
  712. if (error < PAGE_SIZE) {
  713. if (error >= 0)
  714. error = -EFAULT;
  715. goto out_finish;
  716. }
  717. header = (struct swsusp_info *)data_of(snapshot);
  718. error = swap_write_page(&handle, header, NULL);
  719. if (!error) {
  720. error = (flags & SF_NOCOMPRESS_MODE) ?
  721. save_image(&handle, &snapshot, pages - 1) :
  722. save_image_lzo(&handle, &snapshot, pages - 1);
  723. }
  724. out_finish:
  725. error = swap_writer_finish(&handle, flags, error);
  726. return error;
  727. }
  728. /**
  729. * The following functions allow us to read data using a swap map
  730. * in a file-alike way
  731. */
  732. static void release_swap_reader(struct swap_map_handle *handle)
  733. {
  734. struct swap_map_page_list *tmp;
  735. while (handle->maps) {
  736. if (handle->maps->map)
  737. free_page((unsigned long)handle->maps->map);
  738. tmp = handle->maps;
  739. handle->maps = handle->maps->next;
  740. kfree(tmp);
  741. }
  742. handle->cur = NULL;
  743. }
  744. static int get_swap_reader(struct swap_map_handle *handle,
  745. unsigned int *flags_p)
  746. {
  747. int error;
  748. struct swap_map_page_list *tmp, *last;
  749. sector_t offset;
  750. *flags_p = swsusp_header->flags;
  751. if (!swsusp_header->image) /* how can this happen? */
  752. return -EINVAL;
  753. handle->cur = NULL;
  754. last = handle->maps = NULL;
  755. offset = swsusp_header->image;
  756. while (offset) {
  757. tmp = kmalloc(sizeof(*handle->maps), GFP_KERNEL);
  758. if (!tmp) {
  759. release_swap_reader(handle);
  760. return -ENOMEM;
  761. }
  762. memset(tmp, 0, sizeof(*tmp));
  763. if (!handle->maps)
  764. handle->maps = tmp;
  765. if (last)
  766. last->next = tmp;
  767. last = tmp;
  768. tmp->map = (struct swap_map_page *)
  769. __get_free_page(__GFP_WAIT | __GFP_HIGH);
  770. if (!tmp->map) {
  771. release_swap_reader(handle);
  772. return -ENOMEM;
  773. }
  774. error = hib_bio_read_page(offset, tmp->map, NULL);
  775. if (error) {
  776. release_swap_reader(handle);
  777. return error;
  778. }
  779. offset = tmp->map->next_swap;
  780. }
  781. handle->k = 0;
  782. handle->cur = handle->maps->map;
  783. return 0;
  784. }
  785. static int swap_read_page(struct swap_map_handle *handle, void *buf,
  786. struct bio **bio_chain)
  787. {
  788. sector_t offset;
  789. int error;
  790. struct swap_map_page_list *tmp;
  791. if (!handle->cur)
  792. return -EINVAL;
  793. offset = handle->cur->entries[handle->k];
  794. if (!offset)
  795. return -EFAULT;
  796. error = hib_bio_read_page(offset, buf, bio_chain);
  797. if (error)
  798. return error;
  799. if (++handle->k >= MAP_PAGE_ENTRIES) {
  800. handle->k = 0;
  801. free_page((unsigned long)handle->maps->map);
  802. tmp = handle->maps;
  803. handle->maps = handle->maps->next;
  804. kfree(tmp);
  805. if (!handle->maps)
  806. release_swap_reader(handle);
  807. else
  808. handle->cur = handle->maps->map;
  809. }
  810. return error;
  811. }
  812. static int swap_reader_finish(struct swap_map_handle *handle)
  813. {
  814. release_swap_reader(handle);
  815. return 0;
  816. }
  817. /**
  818. * load_image - load the image using the swap map handle
  819. * @handle and the snapshot handle @snapshot
  820. * (assume there are @nr_pages pages to load)
  821. */
  822. static int load_image(struct swap_map_handle *handle,
  823. struct snapshot_handle *snapshot,
  824. unsigned int nr_to_read)
  825. {
  826. unsigned int m;
  827. int ret = 0;
  828. struct timeval start;
  829. struct timeval stop;
  830. struct bio *bio;
  831. int err2;
  832. unsigned nr_pages;
  833. printk(KERN_INFO "PM: Loading image data pages (%u pages) ... ",
  834. nr_to_read);
  835. m = nr_to_read / 100;
  836. if (!m)
  837. m = 1;
  838. nr_pages = 0;
  839. bio = NULL;
  840. do_gettimeofday(&start);
  841. for ( ; ; ) {
  842. ret = snapshot_write_next(snapshot);
  843. if (ret <= 0)
  844. break;
  845. ret = swap_read_page(handle, data_of(*snapshot), &bio);
  846. if (ret)
  847. break;
  848. if (snapshot->sync_read)
  849. ret = hib_wait_on_bio_chain(&bio);
  850. if (ret)
  851. break;
  852. if (!(nr_pages % m))
  853. printk("\b\b\b\b%3d%%", nr_pages / m);
  854. nr_pages++;
  855. }
  856. err2 = hib_wait_on_bio_chain(&bio);
  857. do_gettimeofday(&stop);
  858. if (!ret)
  859. ret = err2;
  860. if (!ret) {
  861. printk("\b\b\b\bdone\n");
  862. snapshot_write_finalize(snapshot);
  863. if (!snapshot_image_loaded(snapshot))
  864. ret = -ENODATA;
  865. } else
  866. printk("\n");
  867. swsusp_show_speed(&start, &stop, nr_to_read, "Read");
  868. return ret;
  869. }
  870. /**
  871. * Structure used for LZO data decompression.
  872. */
  873. struct dec_data {
  874. struct task_struct *thr; /* thread */
  875. atomic_t ready; /* ready to start flag */
  876. atomic_t stop; /* ready to stop flag */
  877. int ret; /* return code */
  878. wait_queue_head_t go; /* start decompression */
  879. wait_queue_head_t done; /* decompression done */
  880. size_t unc_len; /* uncompressed length */
  881. size_t cmp_len; /* compressed length */
  882. unsigned char unc[LZO_UNC_SIZE]; /* uncompressed buffer */
  883. unsigned char cmp[LZO_CMP_SIZE]; /* compressed buffer */
  884. };
  885. /**
  886. * Deompression function that runs in its own thread.
  887. */
  888. static int lzo_decompress_threadfn(void *data)
  889. {
  890. struct dec_data *d = data;
  891. while (1) {
  892. wait_event(d->go, atomic_read(&d->ready) ||
  893. kthread_should_stop());
  894. if (kthread_should_stop()) {
  895. d->thr = NULL;
  896. d->ret = -1;
  897. atomic_set(&d->stop, 1);
  898. wake_up(&d->done);
  899. break;
  900. }
  901. atomic_set(&d->ready, 0);
  902. d->unc_len = LZO_UNC_SIZE;
  903. d->ret = lzo1x_decompress_safe(d->cmp + LZO_HEADER, d->cmp_len,
  904. d->unc, &d->unc_len);
  905. atomic_set(&d->stop, 1);
  906. wake_up(&d->done);
  907. }
  908. return 0;
  909. }
  910. /**
  911. * load_image_lzo - Load compressed image data and decompress them with LZO.
  912. * @handle: Swap map handle to use for loading data.
  913. * @snapshot: Image to copy uncompressed data into.
  914. * @nr_to_read: Number of pages to load.
  915. */
  916. static int load_image_lzo(struct swap_map_handle *handle,
  917. struct snapshot_handle *snapshot,
  918. unsigned int nr_to_read)
  919. {
  920. unsigned int m;
  921. int ret = 0;
  922. int eof = 0;
  923. struct bio *bio;
  924. struct timeval start;
  925. struct timeval stop;
  926. unsigned nr_pages;
  927. size_t off;
  928. unsigned i, thr, run_threads, nr_threads;
  929. unsigned ring = 0, pg = 0, ring_size = 0,
  930. have = 0, want, need, asked = 0;
  931. unsigned long read_pages;
  932. unsigned char **page = NULL;
  933. struct dec_data *data = NULL;
  934. struct crc_data *crc = NULL;
  935. /*
  936. * We'll limit the number of threads for decompression to limit memory
  937. * footprint.
  938. */
  939. nr_threads = num_online_cpus() - 1;
  940. nr_threads = clamp_val(nr_threads, 1, LZO_THREADS);
  941. page = vmalloc(sizeof(*page) * LZO_READ_PAGES);
  942. if (!page) {
  943. printk(KERN_ERR "PM: Failed to allocate LZO page\n");
  944. ret = -ENOMEM;
  945. goto out_clean;
  946. }
  947. data = vmalloc(sizeof(*data) * nr_threads);
  948. if (!data) {
  949. printk(KERN_ERR "PM: Failed to allocate LZO data\n");
  950. ret = -ENOMEM;
  951. goto out_clean;
  952. }
  953. for (thr = 0; thr < nr_threads; thr++)
  954. memset(&data[thr], 0, offsetof(struct dec_data, go));
  955. crc = kmalloc(sizeof(*crc), GFP_KERNEL);
  956. if (!crc) {
  957. printk(KERN_ERR "PM: Failed to allocate crc\n");
  958. ret = -ENOMEM;
  959. goto out_clean;
  960. }
  961. memset(crc, 0, offsetof(struct crc_data, go));
  962. /*
  963. * Start the decompression threads.
  964. */
  965. for (thr = 0; thr < nr_threads; thr++) {
  966. init_waitqueue_head(&data[thr].go);
  967. init_waitqueue_head(&data[thr].done);
  968. data[thr].thr = kthread_run(lzo_decompress_threadfn,
  969. &data[thr],
  970. "image_decompress/%u", thr);
  971. if (IS_ERR(data[thr].thr)) {
  972. data[thr].thr = NULL;
  973. printk(KERN_ERR
  974. "PM: Cannot start decompression threads\n");
  975. ret = -ENOMEM;
  976. goto out_clean;
  977. }
  978. }
  979. /*
  980. * Start the CRC32 thread.
  981. */
  982. init_waitqueue_head(&crc->go);
  983. init_waitqueue_head(&crc->done);
  984. handle->crc32 = 0;
  985. crc->crc32 = &handle->crc32;
  986. for (thr = 0; thr < nr_threads; thr++) {
  987. crc->unc[thr] = data[thr].unc;
  988. crc->unc_len[thr] = &data[thr].unc_len;
  989. }
  990. crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
  991. if (IS_ERR(crc->thr)) {
  992. crc->thr = NULL;
  993. printk(KERN_ERR "PM: Cannot start CRC32 thread\n");
  994. ret = -ENOMEM;
  995. goto out_clean;
  996. }
  997. /*
  998. * Adjust number of pages for read buffering, in case we are short.
  999. */
  1000. read_pages = (nr_free_pages() - snapshot_get_image_size()) >> 1;
  1001. read_pages = clamp_val(read_pages, LZO_CMP_PAGES, LZO_READ_PAGES);
  1002. for (i = 0; i < read_pages; i++) {
  1003. page[i] = (void *)__get_free_page(i < LZO_CMP_PAGES ?
  1004. __GFP_WAIT | __GFP_HIGH :
  1005. __GFP_WAIT);
  1006. if (!page[i]) {
  1007. if (i < LZO_CMP_PAGES) {
  1008. ring_size = i;
  1009. printk(KERN_ERR
  1010. "PM: Failed to allocate LZO pages\n");
  1011. ret = -ENOMEM;
  1012. goto out_clean;
  1013. } else {
  1014. break;
  1015. }
  1016. }
  1017. }
  1018. want = ring_size = i;
  1019. printk(KERN_INFO
  1020. "PM: Using %u thread(s) for decompression.\n"
  1021. "PM: Loading and decompressing image data (%u pages) ... ",
  1022. nr_threads, nr_to_read);
  1023. m = nr_to_read / 100;
  1024. if (!m)
  1025. m = 1;
  1026. nr_pages = 0;
  1027. bio = NULL;
  1028. do_gettimeofday(&start);
  1029. ret = snapshot_write_next(snapshot);
  1030. if (ret <= 0)
  1031. goto out_finish;
  1032. for(;;) {
  1033. for (i = 0; !eof && i < want; i++) {
  1034. ret = swap_read_page(handle, page[ring], &bio);
  1035. if (ret) {
  1036. /*
  1037. * On real read error, finish. On end of data,
  1038. * set EOF flag and just exit the read loop.
  1039. */
  1040. if (handle->cur &&
  1041. handle->cur->entries[handle->k]) {
  1042. goto out_finish;
  1043. } else {
  1044. eof = 1;
  1045. break;
  1046. }
  1047. }
  1048. if (++ring >= ring_size)
  1049. ring = 0;
  1050. }
  1051. asked += i;
  1052. want -= i;
  1053. /*
  1054. * We are out of data, wait for some more.
  1055. */
  1056. if (!have) {
  1057. if (!asked)
  1058. break;
  1059. ret = hib_wait_on_bio_chain(&bio);
  1060. if (ret)
  1061. goto out_finish;
  1062. have += asked;
  1063. asked = 0;
  1064. if (eof)
  1065. eof = 2;
  1066. }
  1067. if (crc->run_threads) {
  1068. wait_event(crc->done, atomic_read(&crc->stop));
  1069. atomic_set(&crc->stop, 0);
  1070. crc->run_threads = 0;
  1071. }
  1072. for (thr = 0; have && thr < nr_threads; thr++) {
  1073. data[thr].cmp_len = *(size_t *)page[pg];
  1074. if (unlikely(!data[thr].cmp_len ||
  1075. data[thr].cmp_len >
  1076. lzo1x_worst_compress(LZO_UNC_SIZE))) {
  1077. printk(KERN_ERR
  1078. "PM: Invalid LZO compressed length\n");
  1079. ret = -1;
  1080. goto out_finish;
  1081. }
  1082. need = DIV_ROUND_UP(data[thr].cmp_len + LZO_HEADER,
  1083. PAGE_SIZE);
  1084. if (need > have) {
  1085. if (eof > 1) {
  1086. ret = -1;
  1087. goto out_finish;
  1088. }
  1089. break;
  1090. }
  1091. for (off = 0;
  1092. off < LZO_HEADER + data[thr].cmp_len;
  1093. off += PAGE_SIZE) {
  1094. memcpy(data[thr].cmp + off,
  1095. page[pg], PAGE_SIZE);
  1096. have--;
  1097. want++;
  1098. if (++pg >= ring_size)
  1099. pg = 0;
  1100. }
  1101. atomic_set(&data[thr].ready, 1);
  1102. wake_up(&data[thr].go);
  1103. }
  1104. /*
  1105. * Wait for more data while we are decompressing.
  1106. */
  1107. if (have < LZO_CMP_PAGES && asked) {
  1108. ret = hib_wait_on_bio_chain(&bio);
  1109. if (ret)
  1110. goto out_finish;
  1111. have += asked;
  1112. asked = 0;
  1113. if (eof)
  1114. eof = 2;
  1115. }
  1116. for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
  1117. wait_event(data[thr].done,
  1118. atomic_read(&data[thr].stop));
  1119. atomic_set(&data[thr].stop, 0);
  1120. ret = data[thr].ret;
  1121. if (ret < 0) {
  1122. printk(KERN_ERR
  1123. "PM: LZO decompression failed\n");
  1124. goto out_finish;
  1125. }
  1126. if (unlikely(!data[thr].unc_len ||
  1127. data[thr].unc_len > LZO_UNC_SIZE ||
  1128. data[thr].unc_len & (PAGE_SIZE - 1))) {
  1129. printk(KERN_ERR
  1130. "PM: Invalid LZO uncompressed length\n");
  1131. ret = -1;
  1132. goto out_finish;
  1133. }
  1134. for (off = 0;
  1135. off < data[thr].unc_len; off += PAGE_SIZE) {
  1136. memcpy(data_of(*snapshot),
  1137. data[thr].unc + off, PAGE_SIZE);
  1138. if (!(nr_pages % m))
  1139. printk("\b\b\b\b%3d%%", nr_pages / m);
  1140. nr_pages++;
  1141. ret = snapshot_write_next(snapshot);
  1142. if (ret <= 0) {
  1143. crc->run_threads = thr + 1;
  1144. atomic_set(&crc->ready, 1);
  1145. wake_up(&crc->go);
  1146. goto out_finish;
  1147. }
  1148. }
  1149. }
  1150. crc->run_threads = thr;
  1151. atomic_set(&crc->ready, 1);
  1152. wake_up(&crc->go);
  1153. }
  1154. out_finish:
  1155. if (crc->run_threads) {
  1156. wait_event(crc->done, atomic_read(&crc->stop));
  1157. atomic_set(&crc->stop, 0);
  1158. }
  1159. do_gettimeofday(&stop);
  1160. if (!ret) {
  1161. printk("\b\b\b\bdone\n");
  1162. snapshot_write_finalize(snapshot);
  1163. if (!snapshot_image_loaded(snapshot))
  1164. ret = -ENODATA;
  1165. if (!ret) {
  1166. if (swsusp_header->flags & SF_CRC32_MODE) {
  1167. if(handle->crc32 != swsusp_header->crc32) {
  1168. printk(KERN_ERR
  1169. "PM: Invalid image CRC32!\n");
  1170. ret = -ENODATA;
  1171. }
  1172. }
  1173. }
  1174. } else
  1175. printk("\n");
  1176. swsusp_show_speed(&start, &stop, nr_to_read, "Read");
  1177. out_clean:
  1178. for (i = 0; i < ring_size; i++)
  1179. free_page((unsigned long)page[i]);
  1180. if (crc) {
  1181. if (crc->thr)
  1182. kthread_stop(crc->thr);
  1183. kfree(crc);
  1184. }
  1185. if (data) {
  1186. for (thr = 0; thr < nr_threads; thr++)
  1187. if (data[thr].thr)
  1188. kthread_stop(data[thr].thr);
  1189. vfree(data);
  1190. }
  1191. if (page) vfree(page);
  1192. return ret;
  1193. }
  1194. /**
  1195. * swsusp_read - read the hibernation image.
  1196. * @flags_p: flags passed by the "frozen" kernel in the image header should
  1197. * be written into this memory location
  1198. */
  1199. int swsusp_read(unsigned int *flags_p)
  1200. {
  1201. int error;
  1202. struct swap_map_handle handle;
  1203. struct snapshot_handle snapshot;
  1204. struct swsusp_info *header;
  1205. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  1206. error = snapshot_write_next(&snapshot);
  1207. if (error < PAGE_SIZE)
  1208. return error < 0 ? error : -EFAULT;
  1209. header = (struct swsusp_info *)data_of(snapshot);
  1210. error = get_swap_reader(&handle, flags_p);
  1211. if (error)
  1212. goto end;
  1213. if (!error)
  1214. error = swap_read_page(&handle, header, NULL);
  1215. if (!error) {
  1216. error = (*flags_p & SF_NOCOMPRESS_MODE) ?
  1217. load_image(&handle, &snapshot, header->pages - 1) :
  1218. load_image_lzo(&handle, &snapshot, header->pages - 1);
  1219. }
  1220. swap_reader_finish(&handle);
  1221. end:
  1222. if (!error)
  1223. pr_debug("PM: Image successfully loaded\n");
  1224. else
  1225. pr_debug("PM: Error %d resuming\n", error);
  1226. return error;
  1227. }
  1228. /**
  1229. * swsusp_check - Check for swsusp signature in the resume device
  1230. */
  1231. int swsusp_check(void)
  1232. {
  1233. int error;
  1234. hib_resume_bdev = blkdev_get_by_dev(swsusp_resume_device,
  1235. FMODE_READ, NULL);
  1236. if (!IS_ERR(hib_resume_bdev)) {
  1237. set_blocksize(hib_resume_bdev, PAGE_SIZE);
  1238. clear_page(swsusp_header);
  1239. error = hib_bio_read_page(swsusp_resume_block,
  1240. swsusp_header, NULL);
  1241. if (error)
  1242. goto put;
  1243. if (!memcmp(HIBERNATE_SIG, swsusp_header->sig, 10)) {
  1244. memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
  1245. /* Reset swap signature now */
  1246. error = hib_bio_write_page(swsusp_resume_block,
  1247. swsusp_header, NULL);
  1248. } else {
  1249. error = -EINVAL;
  1250. }
  1251. put:
  1252. if (error)
  1253. blkdev_put(hib_resume_bdev, FMODE_READ);
  1254. else
  1255. pr_debug("PM: Image signature found, resuming\n");
  1256. } else {
  1257. error = PTR_ERR(hib_resume_bdev);
  1258. }
  1259. if (error)
  1260. pr_debug("PM: Image not found (code %d)\n", error);
  1261. return error;
  1262. }
  1263. /**
  1264. * swsusp_close - close swap device.
  1265. */
  1266. void swsusp_close(fmode_t mode)
  1267. {
  1268. if (IS_ERR(hib_resume_bdev)) {
  1269. pr_debug("PM: Image device not initialised\n");
  1270. return;
  1271. }
  1272. blkdev_put(hib_resume_bdev, mode);
  1273. }
  1274. static int swsusp_header_init(void)
  1275. {
  1276. swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
  1277. if (!swsusp_header)
  1278. panic("Could not allocate memory for swsusp_header\n");
  1279. return 0;
  1280. }
  1281. core_initcall(swsusp_header_init);