swap.c 36 KB

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