swap.c 34 KB

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