swap.c 35 KB

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