swap.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  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. } __attribute__((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)...\n",
  391. nr_to_write);
  392. m = nr_to_write / 10;
  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_INFO "PM: Image saving progress: %3d%%\n",
  407. nr_pages / m * 10);
  408. nr_pages++;
  409. }
  410. err2 = hib_wait_on_bio_chain(&bio);
  411. do_gettimeofday(&stop);
  412. if (!ret)
  413. ret = err2;
  414. if (!ret)
  415. printk(KERN_INFO "PM: Image saving done.\n");
  416. swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
  417. return ret;
  418. }
  419. /**
  420. * Structure used for CRC32.
  421. */
  422. struct crc_data {
  423. struct task_struct *thr; /* thread */
  424. atomic_t ready; /* ready to start flag */
  425. atomic_t stop; /* ready to stop flag */
  426. unsigned run_threads; /* nr current threads */
  427. wait_queue_head_t go; /* start crc update */
  428. wait_queue_head_t done; /* crc update done */
  429. u32 *crc32; /* points to handle's crc32 */
  430. size_t *unc_len[LZO_THREADS]; /* uncompressed lengths */
  431. unsigned char *unc[LZO_THREADS]; /* uncompressed data */
  432. };
  433. /**
  434. * CRC32 update function that runs in its own thread.
  435. */
  436. static int crc32_threadfn(void *data)
  437. {
  438. struct crc_data *d = data;
  439. unsigned i;
  440. while (1) {
  441. wait_event(d->go, atomic_read(&d->ready) ||
  442. kthread_should_stop());
  443. if (kthread_should_stop()) {
  444. d->thr = NULL;
  445. atomic_set(&d->stop, 1);
  446. wake_up(&d->done);
  447. break;
  448. }
  449. atomic_set(&d->ready, 0);
  450. for (i = 0; i < d->run_threads; i++)
  451. *d->crc32 = crc32_le(*d->crc32,
  452. d->unc[i], *d->unc_len[i]);
  453. atomic_set(&d->stop, 1);
  454. wake_up(&d->done);
  455. }
  456. return 0;
  457. }
  458. /**
  459. * Structure used for LZO data compression.
  460. */
  461. struct cmp_data {
  462. struct task_struct *thr; /* thread */
  463. atomic_t ready; /* ready to start flag */
  464. atomic_t stop; /* ready to stop flag */
  465. int ret; /* return code */
  466. wait_queue_head_t go; /* start compression */
  467. wait_queue_head_t done; /* compression done */
  468. size_t unc_len; /* uncompressed length */
  469. size_t cmp_len; /* compressed length */
  470. unsigned char unc[LZO_UNC_SIZE]; /* uncompressed buffer */
  471. unsigned char cmp[LZO_CMP_SIZE]; /* compressed buffer */
  472. unsigned char wrk[LZO1X_1_MEM_COMPRESS]; /* compression workspace */
  473. };
  474. /**
  475. * Compression function that runs in its own thread.
  476. */
  477. static int lzo_compress_threadfn(void *data)
  478. {
  479. struct cmp_data *d = data;
  480. while (1) {
  481. wait_event(d->go, atomic_read(&d->ready) ||
  482. kthread_should_stop());
  483. if (kthread_should_stop()) {
  484. d->thr = NULL;
  485. d->ret = -1;
  486. atomic_set(&d->stop, 1);
  487. wake_up(&d->done);
  488. break;
  489. }
  490. atomic_set(&d->ready, 0);
  491. d->ret = lzo1x_1_compress(d->unc, d->unc_len,
  492. d->cmp + LZO_HEADER, &d->cmp_len,
  493. d->wrk);
  494. atomic_set(&d->stop, 1);
  495. wake_up(&d->done);
  496. }
  497. return 0;
  498. }
  499. /**
  500. * save_image_lzo - Save the suspend image data compressed with LZO.
  501. * @handle: Swap mam handle to use for saving the image.
  502. * @snapshot: Image to read data from.
  503. * @nr_to_write: Number of pages to save.
  504. */
  505. static int save_image_lzo(struct swap_map_handle *handle,
  506. struct snapshot_handle *snapshot,
  507. unsigned int nr_to_write)
  508. {
  509. unsigned int m;
  510. int ret = 0;
  511. int nr_pages;
  512. int err2;
  513. struct bio *bio;
  514. struct timeval start;
  515. struct timeval stop;
  516. size_t off;
  517. unsigned thr, run_threads, nr_threads;
  518. unsigned char *page = NULL;
  519. struct cmp_data *data = NULL;
  520. struct crc_data *crc = NULL;
  521. /*
  522. * We'll limit the number of threads for compression to limit memory
  523. * footprint.
  524. */
  525. nr_threads = num_online_cpus() - 1;
  526. nr_threads = clamp_val(nr_threads, 1, LZO_THREADS);
  527. page = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
  528. if (!page) {
  529. printk(KERN_ERR "PM: Failed to allocate LZO page\n");
  530. ret = -ENOMEM;
  531. goto out_clean;
  532. }
  533. data = vmalloc(sizeof(*data) * nr_threads);
  534. if (!data) {
  535. printk(KERN_ERR "PM: Failed to allocate LZO data\n");
  536. ret = -ENOMEM;
  537. goto out_clean;
  538. }
  539. for (thr = 0; thr < nr_threads; thr++)
  540. memset(&data[thr], 0, offsetof(struct cmp_data, go));
  541. crc = kmalloc(sizeof(*crc), GFP_KERNEL);
  542. if (!crc) {
  543. printk(KERN_ERR "PM: Failed to allocate crc\n");
  544. ret = -ENOMEM;
  545. goto out_clean;
  546. }
  547. memset(crc, 0, offsetof(struct crc_data, go));
  548. /*
  549. * Start the compression threads.
  550. */
  551. for (thr = 0; thr < nr_threads; thr++) {
  552. init_waitqueue_head(&data[thr].go);
  553. init_waitqueue_head(&data[thr].done);
  554. data[thr].thr = kthread_run(lzo_compress_threadfn,
  555. &data[thr],
  556. "image_compress/%u", thr);
  557. if (IS_ERR(data[thr].thr)) {
  558. data[thr].thr = NULL;
  559. printk(KERN_ERR
  560. "PM: Cannot start compression threads\n");
  561. ret = -ENOMEM;
  562. goto out_clean;
  563. }
  564. }
  565. /*
  566. * Start the CRC32 thread.
  567. */
  568. init_waitqueue_head(&crc->go);
  569. init_waitqueue_head(&crc->done);
  570. handle->crc32 = 0;
  571. crc->crc32 = &handle->crc32;
  572. for (thr = 0; thr < nr_threads; thr++) {
  573. crc->unc[thr] = data[thr].unc;
  574. crc->unc_len[thr] = &data[thr].unc_len;
  575. }
  576. crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
  577. if (IS_ERR(crc->thr)) {
  578. crc->thr = NULL;
  579. printk(KERN_ERR "PM: Cannot start CRC32 thread\n");
  580. ret = -ENOMEM;
  581. goto out_clean;
  582. }
  583. /*
  584. * Adjust the number of required free pages after all allocations have
  585. * been done. We don't want to run out of pages when writing.
  586. */
  587. handle->reqd_free_pages = reqd_free_pages();
  588. printk(KERN_INFO
  589. "PM: Using %u thread(s) for compression.\n"
  590. "PM: Compressing and saving image data (%u pages)...\n",
  591. nr_threads, nr_to_write);
  592. m = nr_to_write / 10;
  593. if (!m)
  594. m = 1;
  595. nr_pages = 0;
  596. bio = NULL;
  597. do_gettimeofday(&start);
  598. for (;;) {
  599. for (thr = 0; thr < nr_threads; thr++) {
  600. for (off = 0; off < LZO_UNC_SIZE; off += PAGE_SIZE) {
  601. ret = snapshot_read_next(snapshot);
  602. if (ret < 0)
  603. goto out_finish;
  604. if (!ret)
  605. break;
  606. memcpy(data[thr].unc + off,
  607. data_of(*snapshot), PAGE_SIZE);
  608. if (!(nr_pages % m))
  609. printk(KERN_INFO
  610. "PM: Image saving progress: "
  611. "%3d%%\n",
  612. nr_pages / m * 10);
  613. nr_pages++;
  614. }
  615. if (!off)
  616. break;
  617. data[thr].unc_len = off;
  618. atomic_set(&data[thr].ready, 1);
  619. wake_up(&data[thr].go);
  620. }
  621. if (!thr)
  622. break;
  623. crc->run_threads = thr;
  624. atomic_set(&crc->ready, 1);
  625. wake_up(&crc->go);
  626. for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
  627. wait_event(data[thr].done,
  628. atomic_read(&data[thr].stop));
  629. atomic_set(&data[thr].stop, 0);
  630. ret = data[thr].ret;
  631. if (ret < 0) {
  632. printk(KERN_ERR "PM: LZO compression failed\n");
  633. goto out_finish;
  634. }
  635. if (unlikely(!data[thr].cmp_len ||
  636. data[thr].cmp_len >
  637. lzo1x_worst_compress(data[thr].unc_len))) {
  638. printk(KERN_ERR
  639. "PM: Invalid LZO compressed length\n");
  640. ret = -1;
  641. goto out_finish;
  642. }
  643. *(size_t *)data[thr].cmp = data[thr].cmp_len;
  644. /*
  645. * Given we are writing one page at a time to disk, we
  646. * copy that much from the buffer, although the last
  647. * bit will likely be smaller than full page. This is
  648. * OK - we saved the length of the compressed data, so
  649. * any garbage at the end will be discarded when we
  650. * read it.
  651. */
  652. for (off = 0;
  653. off < LZO_HEADER + data[thr].cmp_len;
  654. off += PAGE_SIZE) {
  655. memcpy(page, data[thr].cmp + off, PAGE_SIZE);
  656. ret = swap_write_page(handle, page, &bio);
  657. if (ret)
  658. goto out_finish;
  659. }
  660. }
  661. wait_event(crc->done, atomic_read(&crc->stop));
  662. atomic_set(&crc->stop, 0);
  663. }
  664. out_finish:
  665. err2 = hib_wait_on_bio_chain(&bio);
  666. do_gettimeofday(&stop);
  667. if (!ret)
  668. ret = err2;
  669. if (!ret)
  670. printk(KERN_INFO "PM: Image saving done.\n");
  671. swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
  672. out_clean:
  673. if (crc) {
  674. if (crc->thr)
  675. kthread_stop(crc->thr);
  676. kfree(crc);
  677. }
  678. if (data) {
  679. for (thr = 0; thr < nr_threads; thr++)
  680. if (data[thr].thr)
  681. kthread_stop(data[thr].thr);
  682. vfree(data);
  683. }
  684. if (page) free_page((unsigned long)page);
  685. return ret;
  686. }
  687. /**
  688. * enough_swap - Make sure we have enough swap to save the image.
  689. *
  690. * Returns TRUE or FALSE after checking the total amount of swap
  691. * space avaiable from the resume partition.
  692. */
  693. static int enough_swap(unsigned int nr_pages, unsigned int flags)
  694. {
  695. unsigned int free_swap = count_swap_pages(root_swap, 1);
  696. unsigned int required;
  697. pr_debug("PM: Free swap pages: %u\n", free_swap);
  698. required = PAGES_FOR_IO + nr_pages;
  699. return free_swap > required;
  700. }
  701. /**
  702. * swsusp_write - Write entire image and metadata.
  703. * @flags: flags to pass to the "boot" kernel in the image header
  704. *
  705. * It is important _NOT_ to umount filesystems at this point. We want
  706. * them synced (in case something goes wrong) but we DO not want to mark
  707. * filesystem clean: it is not. (And it does not matter, if we resume
  708. * correctly, we'll mark system clean, anyway.)
  709. */
  710. int swsusp_write(unsigned int flags)
  711. {
  712. struct swap_map_handle handle;
  713. struct snapshot_handle snapshot;
  714. struct swsusp_info *header;
  715. unsigned long pages;
  716. int error;
  717. pages = snapshot_get_image_size();
  718. error = get_swap_writer(&handle);
  719. if (error) {
  720. printk(KERN_ERR "PM: Cannot get swap writer\n");
  721. return error;
  722. }
  723. if (flags & SF_NOCOMPRESS_MODE) {
  724. if (!enough_swap(pages, flags)) {
  725. printk(KERN_ERR "PM: Not enough free swap\n");
  726. error = -ENOSPC;
  727. goto out_finish;
  728. }
  729. }
  730. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  731. error = snapshot_read_next(&snapshot);
  732. if (error < PAGE_SIZE) {
  733. if (error >= 0)
  734. error = -EFAULT;
  735. goto out_finish;
  736. }
  737. header = (struct swsusp_info *)data_of(snapshot);
  738. error = swap_write_page(&handle, header, NULL);
  739. if (!error) {
  740. error = (flags & SF_NOCOMPRESS_MODE) ?
  741. save_image(&handle, &snapshot, pages - 1) :
  742. save_image_lzo(&handle, &snapshot, pages - 1);
  743. }
  744. out_finish:
  745. error = swap_writer_finish(&handle, flags, error);
  746. return error;
  747. }
  748. /**
  749. * The following functions allow us to read data using a swap map
  750. * in a file-alike way
  751. */
  752. static void release_swap_reader(struct swap_map_handle *handle)
  753. {
  754. struct swap_map_page_list *tmp;
  755. while (handle->maps) {
  756. if (handle->maps->map)
  757. free_page((unsigned long)handle->maps->map);
  758. tmp = handle->maps;
  759. handle->maps = handle->maps->next;
  760. kfree(tmp);
  761. }
  762. handle->cur = NULL;
  763. }
  764. static int get_swap_reader(struct swap_map_handle *handle,
  765. unsigned int *flags_p)
  766. {
  767. int error;
  768. struct swap_map_page_list *tmp, *last;
  769. sector_t offset;
  770. *flags_p = swsusp_header->flags;
  771. if (!swsusp_header->image) /* how can this happen? */
  772. return -EINVAL;
  773. handle->cur = NULL;
  774. last = handle->maps = NULL;
  775. offset = swsusp_header->image;
  776. while (offset) {
  777. tmp = kmalloc(sizeof(*handle->maps), GFP_KERNEL);
  778. if (!tmp) {
  779. release_swap_reader(handle);
  780. return -ENOMEM;
  781. }
  782. memset(tmp, 0, sizeof(*tmp));
  783. if (!handle->maps)
  784. handle->maps = tmp;
  785. if (last)
  786. last->next = tmp;
  787. last = tmp;
  788. tmp->map = (struct swap_map_page *)
  789. __get_free_page(__GFP_WAIT | __GFP_HIGH);
  790. if (!tmp->map) {
  791. release_swap_reader(handle);
  792. return -ENOMEM;
  793. }
  794. error = hib_bio_read_page(offset, tmp->map, NULL);
  795. if (error) {
  796. release_swap_reader(handle);
  797. return error;
  798. }
  799. offset = tmp->map->next_swap;
  800. }
  801. handle->k = 0;
  802. handle->cur = handle->maps->map;
  803. return 0;
  804. }
  805. static int swap_read_page(struct swap_map_handle *handle, void *buf,
  806. struct bio **bio_chain)
  807. {
  808. sector_t offset;
  809. int error;
  810. struct swap_map_page_list *tmp;
  811. if (!handle->cur)
  812. return -EINVAL;
  813. offset = handle->cur->entries[handle->k];
  814. if (!offset)
  815. return -EFAULT;
  816. error = hib_bio_read_page(offset, buf, bio_chain);
  817. if (error)
  818. return error;
  819. if (++handle->k >= MAP_PAGE_ENTRIES) {
  820. handle->k = 0;
  821. free_page((unsigned long)handle->maps->map);
  822. tmp = handle->maps;
  823. handle->maps = handle->maps->next;
  824. kfree(tmp);
  825. if (!handle->maps)
  826. release_swap_reader(handle);
  827. else
  828. handle->cur = handle->maps->map;
  829. }
  830. return error;
  831. }
  832. static int swap_reader_finish(struct swap_map_handle *handle)
  833. {
  834. release_swap_reader(handle);
  835. return 0;
  836. }
  837. /**
  838. * load_image - load the image using the swap map handle
  839. * @handle and the snapshot handle @snapshot
  840. * (assume there are @nr_pages pages to load)
  841. */
  842. static int load_image(struct swap_map_handle *handle,
  843. struct snapshot_handle *snapshot,
  844. unsigned int nr_to_read)
  845. {
  846. unsigned int m;
  847. int ret = 0;
  848. struct timeval start;
  849. struct timeval stop;
  850. struct bio *bio;
  851. int err2;
  852. unsigned nr_pages;
  853. printk(KERN_INFO "PM: Loading image data pages (%u pages)...\n",
  854. nr_to_read);
  855. m = nr_to_read / 10;
  856. if (!m)
  857. m = 1;
  858. nr_pages = 0;
  859. bio = NULL;
  860. do_gettimeofday(&start);
  861. for ( ; ; ) {
  862. ret = snapshot_write_next(snapshot);
  863. if (ret <= 0)
  864. break;
  865. ret = swap_read_page(handle, data_of(*snapshot), &bio);
  866. if (ret)
  867. break;
  868. if (snapshot->sync_read)
  869. ret = hib_wait_on_bio_chain(&bio);
  870. if (ret)
  871. break;
  872. if (!(nr_pages % m))
  873. printk(KERN_INFO "PM: Image loading progress: %3d%%\n",
  874. nr_pages / m * 10);
  875. nr_pages++;
  876. }
  877. err2 = hib_wait_on_bio_chain(&bio);
  878. do_gettimeofday(&stop);
  879. if (!ret)
  880. ret = err2;
  881. if (!ret) {
  882. printk(KERN_INFO "PM: Image loading done.\n");
  883. snapshot_write_finalize(snapshot);
  884. if (!snapshot_image_loaded(snapshot))
  885. ret = -ENODATA;
  886. }
  887. swsusp_show_speed(&start, &stop, nr_to_read, "Read");
  888. return ret;
  889. }
  890. /**
  891. * Structure used for LZO data decompression.
  892. */
  893. struct dec_data {
  894. struct task_struct *thr; /* thread */
  895. atomic_t ready; /* ready to start flag */
  896. atomic_t stop; /* ready to stop flag */
  897. int ret; /* return code */
  898. wait_queue_head_t go; /* start decompression */
  899. wait_queue_head_t done; /* decompression done */
  900. size_t unc_len; /* uncompressed length */
  901. size_t cmp_len; /* compressed length */
  902. unsigned char unc[LZO_UNC_SIZE]; /* uncompressed buffer */
  903. unsigned char cmp[LZO_CMP_SIZE]; /* compressed buffer */
  904. };
  905. /**
  906. * Deompression function that runs in its own thread.
  907. */
  908. static int lzo_decompress_threadfn(void *data)
  909. {
  910. struct dec_data *d = data;
  911. while (1) {
  912. wait_event(d->go, atomic_read(&d->ready) ||
  913. kthread_should_stop());
  914. if (kthread_should_stop()) {
  915. d->thr = NULL;
  916. d->ret = -1;
  917. atomic_set(&d->stop, 1);
  918. wake_up(&d->done);
  919. break;
  920. }
  921. atomic_set(&d->ready, 0);
  922. d->unc_len = LZO_UNC_SIZE;
  923. d->ret = lzo1x_decompress_safe(d->cmp + LZO_HEADER, d->cmp_len,
  924. d->unc, &d->unc_len);
  925. atomic_set(&d->stop, 1);
  926. wake_up(&d->done);
  927. }
  928. return 0;
  929. }
  930. /**
  931. * load_image_lzo - Load compressed image data and decompress them with LZO.
  932. * @handle: Swap map handle to use for loading data.
  933. * @snapshot: Image to copy uncompressed data into.
  934. * @nr_to_read: Number of pages to load.
  935. */
  936. static int load_image_lzo(struct swap_map_handle *handle,
  937. struct snapshot_handle *snapshot,
  938. unsigned int nr_to_read)
  939. {
  940. unsigned int m;
  941. int ret = 0;
  942. int eof = 0;
  943. struct bio *bio;
  944. struct timeval start;
  945. struct timeval stop;
  946. unsigned nr_pages;
  947. size_t off;
  948. unsigned i, thr, run_threads, nr_threads;
  949. unsigned ring = 0, pg = 0, ring_size = 0,
  950. have = 0, want, need, asked = 0;
  951. unsigned long read_pages = 0;
  952. unsigned char **page = NULL;
  953. struct dec_data *data = NULL;
  954. struct crc_data *crc = NULL;
  955. /*
  956. * We'll limit the number of threads for decompression to limit memory
  957. * footprint.
  958. */
  959. nr_threads = num_online_cpus() - 1;
  960. nr_threads = clamp_val(nr_threads, 1, LZO_THREADS);
  961. page = vmalloc(sizeof(*page) * LZO_MAX_RD_PAGES);
  962. if (!page) {
  963. printk(KERN_ERR "PM: Failed to allocate LZO page\n");
  964. ret = -ENOMEM;
  965. goto out_clean;
  966. }
  967. data = vmalloc(sizeof(*data) * nr_threads);
  968. if (!data) {
  969. printk(KERN_ERR "PM: Failed to allocate LZO data\n");
  970. ret = -ENOMEM;
  971. goto out_clean;
  972. }
  973. for (thr = 0; thr < nr_threads; thr++)
  974. memset(&data[thr], 0, offsetof(struct dec_data, go));
  975. crc = kmalloc(sizeof(*crc), GFP_KERNEL);
  976. if (!crc) {
  977. printk(KERN_ERR "PM: Failed to allocate crc\n");
  978. ret = -ENOMEM;
  979. goto out_clean;
  980. }
  981. memset(crc, 0, offsetof(struct crc_data, go));
  982. /*
  983. * Start the decompression threads.
  984. */
  985. for (thr = 0; thr < nr_threads; thr++) {
  986. init_waitqueue_head(&data[thr].go);
  987. init_waitqueue_head(&data[thr].done);
  988. data[thr].thr = kthread_run(lzo_decompress_threadfn,
  989. &data[thr],
  990. "image_decompress/%u", thr);
  991. if (IS_ERR(data[thr].thr)) {
  992. data[thr].thr = NULL;
  993. printk(KERN_ERR
  994. "PM: Cannot start decompression threads\n");
  995. ret = -ENOMEM;
  996. goto out_clean;
  997. }
  998. }
  999. /*
  1000. * Start the CRC32 thread.
  1001. */
  1002. init_waitqueue_head(&crc->go);
  1003. init_waitqueue_head(&crc->done);
  1004. handle->crc32 = 0;
  1005. crc->crc32 = &handle->crc32;
  1006. for (thr = 0; thr < nr_threads; thr++) {
  1007. crc->unc[thr] = data[thr].unc;
  1008. crc->unc_len[thr] = &data[thr].unc_len;
  1009. }
  1010. crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
  1011. if (IS_ERR(crc->thr)) {
  1012. crc->thr = NULL;
  1013. printk(KERN_ERR "PM: Cannot start CRC32 thread\n");
  1014. ret = -ENOMEM;
  1015. goto out_clean;
  1016. }
  1017. /*
  1018. * Set the number of pages for read buffering.
  1019. * This is complete guesswork, because we'll only know the real
  1020. * picture once prepare_image() is called, which is much later on
  1021. * during the image load phase. We'll assume the worst case and
  1022. * say that none of the image pages are from high memory.
  1023. */
  1024. if (low_free_pages() > snapshot_get_image_size())
  1025. read_pages = (low_free_pages() - snapshot_get_image_size()) / 2;
  1026. read_pages = clamp_val(read_pages, LZO_MIN_RD_PAGES, LZO_MAX_RD_PAGES);
  1027. for (i = 0; i < read_pages; i++) {
  1028. page[i] = (void *)__get_free_page(i < LZO_CMP_PAGES ?
  1029. __GFP_WAIT | __GFP_HIGH :
  1030. __GFP_WAIT | __GFP_NOWARN |
  1031. __GFP_NORETRY);
  1032. if (!page[i]) {
  1033. if (i < LZO_CMP_PAGES) {
  1034. ring_size = i;
  1035. printk(KERN_ERR
  1036. "PM: Failed to allocate LZO pages\n");
  1037. ret = -ENOMEM;
  1038. goto out_clean;
  1039. } else {
  1040. break;
  1041. }
  1042. }
  1043. }
  1044. want = ring_size = i;
  1045. printk(KERN_INFO
  1046. "PM: Using %u thread(s) for decompression.\n"
  1047. "PM: Loading and decompressing image data (%u pages)...\n",
  1048. nr_threads, nr_to_read);
  1049. m = nr_to_read / 10;
  1050. if (!m)
  1051. m = 1;
  1052. nr_pages = 0;
  1053. bio = NULL;
  1054. do_gettimeofday(&start);
  1055. ret = snapshot_write_next(snapshot);
  1056. if (ret <= 0)
  1057. goto out_finish;
  1058. for(;;) {
  1059. for (i = 0; !eof && i < want; i++) {
  1060. ret = swap_read_page(handle, page[ring], &bio);
  1061. if (ret) {
  1062. /*
  1063. * On real read error, finish. On end of data,
  1064. * set EOF flag and just exit the read loop.
  1065. */
  1066. if (handle->cur &&
  1067. handle->cur->entries[handle->k]) {
  1068. goto out_finish;
  1069. } else {
  1070. eof = 1;
  1071. break;
  1072. }
  1073. }
  1074. if (++ring >= ring_size)
  1075. ring = 0;
  1076. }
  1077. asked += i;
  1078. want -= i;
  1079. /*
  1080. * We are out of data, wait for some more.
  1081. */
  1082. if (!have) {
  1083. if (!asked)
  1084. break;
  1085. ret = hib_wait_on_bio_chain(&bio);
  1086. if (ret)
  1087. goto out_finish;
  1088. have += asked;
  1089. asked = 0;
  1090. if (eof)
  1091. eof = 2;
  1092. }
  1093. if (crc->run_threads) {
  1094. wait_event(crc->done, atomic_read(&crc->stop));
  1095. atomic_set(&crc->stop, 0);
  1096. crc->run_threads = 0;
  1097. }
  1098. for (thr = 0; have && thr < nr_threads; thr++) {
  1099. data[thr].cmp_len = *(size_t *)page[pg];
  1100. if (unlikely(!data[thr].cmp_len ||
  1101. data[thr].cmp_len >
  1102. lzo1x_worst_compress(LZO_UNC_SIZE))) {
  1103. printk(KERN_ERR
  1104. "PM: Invalid LZO compressed length\n");
  1105. ret = -1;
  1106. goto out_finish;
  1107. }
  1108. need = DIV_ROUND_UP(data[thr].cmp_len + LZO_HEADER,
  1109. PAGE_SIZE);
  1110. if (need > have) {
  1111. if (eof > 1) {
  1112. ret = -1;
  1113. goto out_finish;
  1114. }
  1115. break;
  1116. }
  1117. for (off = 0;
  1118. off < LZO_HEADER + data[thr].cmp_len;
  1119. off += PAGE_SIZE) {
  1120. memcpy(data[thr].cmp + off,
  1121. page[pg], PAGE_SIZE);
  1122. have--;
  1123. want++;
  1124. if (++pg >= ring_size)
  1125. pg = 0;
  1126. }
  1127. atomic_set(&data[thr].ready, 1);
  1128. wake_up(&data[thr].go);
  1129. }
  1130. /*
  1131. * Wait for more data while we are decompressing.
  1132. */
  1133. if (have < LZO_CMP_PAGES && asked) {
  1134. ret = hib_wait_on_bio_chain(&bio);
  1135. if (ret)
  1136. goto out_finish;
  1137. have += asked;
  1138. asked = 0;
  1139. if (eof)
  1140. eof = 2;
  1141. }
  1142. for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
  1143. wait_event(data[thr].done,
  1144. atomic_read(&data[thr].stop));
  1145. atomic_set(&data[thr].stop, 0);
  1146. ret = data[thr].ret;
  1147. if (ret < 0) {
  1148. printk(KERN_ERR
  1149. "PM: LZO decompression failed\n");
  1150. goto out_finish;
  1151. }
  1152. if (unlikely(!data[thr].unc_len ||
  1153. data[thr].unc_len > LZO_UNC_SIZE ||
  1154. data[thr].unc_len & (PAGE_SIZE - 1))) {
  1155. printk(KERN_ERR
  1156. "PM: Invalid LZO uncompressed length\n");
  1157. ret = -1;
  1158. goto out_finish;
  1159. }
  1160. for (off = 0;
  1161. off < data[thr].unc_len; off += PAGE_SIZE) {
  1162. memcpy(data_of(*snapshot),
  1163. data[thr].unc + off, PAGE_SIZE);
  1164. if (!(nr_pages % m))
  1165. printk(KERN_INFO
  1166. "PM: Image loading progress: "
  1167. "%3d%%\n",
  1168. nr_pages / m * 10);
  1169. nr_pages++;
  1170. ret = snapshot_write_next(snapshot);
  1171. if (ret <= 0) {
  1172. crc->run_threads = thr + 1;
  1173. atomic_set(&crc->ready, 1);
  1174. wake_up(&crc->go);
  1175. goto out_finish;
  1176. }
  1177. }
  1178. }
  1179. crc->run_threads = thr;
  1180. atomic_set(&crc->ready, 1);
  1181. wake_up(&crc->go);
  1182. }
  1183. out_finish:
  1184. if (crc->run_threads) {
  1185. wait_event(crc->done, atomic_read(&crc->stop));
  1186. atomic_set(&crc->stop, 0);
  1187. }
  1188. do_gettimeofday(&stop);
  1189. if (!ret) {
  1190. printk(KERN_INFO "PM: Image loading done.\n");
  1191. snapshot_write_finalize(snapshot);
  1192. if (!snapshot_image_loaded(snapshot))
  1193. ret = -ENODATA;
  1194. if (!ret) {
  1195. if (swsusp_header->flags & SF_CRC32_MODE) {
  1196. if(handle->crc32 != swsusp_header->crc32) {
  1197. printk(KERN_ERR
  1198. "PM: Invalid image CRC32!\n");
  1199. ret = -ENODATA;
  1200. }
  1201. }
  1202. }
  1203. }
  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. /**
  1303. * swsusp_unmark - Unmark swsusp signature in the resume device
  1304. */
  1305. #ifdef CONFIG_SUSPEND
  1306. int swsusp_unmark(void)
  1307. {
  1308. int error;
  1309. hib_bio_read_page(swsusp_resume_block, swsusp_header, NULL);
  1310. if (!memcmp(HIBERNATE_SIG,swsusp_header->sig, 10)) {
  1311. memcpy(swsusp_header->sig,swsusp_header->orig_sig, 10);
  1312. error = hib_bio_write_page(swsusp_resume_block,
  1313. swsusp_header, NULL);
  1314. } else {
  1315. printk(KERN_ERR "PM: Cannot find swsusp signature!\n");
  1316. error = -ENODEV;
  1317. }
  1318. /*
  1319. * We just returned from suspend, we don't need the image any more.
  1320. */
  1321. free_all_swap_pages(root_swap);
  1322. return error;
  1323. }
  1324. #endif
  1325. static int swsusp_header_init(void)
  1326. {
  1327. swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
  1328. if (!swsusp_header)
  1329. panic("Could not allocate memory for swsusp_header\n");
  1330. return 0;
  1331. }
  1332. core_initcall(swsusp_header_init);