swap.c 34 KB

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