swsusp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /*
  2. * linux/kernel/power/swsusp.c
  3. *
  4. * This file provides code to write suspend image to swap and read it back.
  5. *
  6. * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
  7. * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
  8. *
  9. * This file is released under the GPLv2.
  10. *
  11. * I'd like to thank the following people for their work:
  12. *
  13. * Pavel Machek <pavel@ucw.cz>:
  14. * Modifications, defectiveness pointing, being with me at the very beginning,
  15. * suspend to swap space, stop all tasks. Port to 2.4.18-ac and 2.5.17.
  16. *
  17. * Steve Doddi <dirk@loth.demon.co.uk>:
  18. * Support the possibility of hardware state restoring.
  19. *
  20. * Raph <grey.havens@earthling.net>:
  21. * Support for preserving states of network devices and virtual console
  22. * (including X and svgatextmode)
  23. *
  24. * Kurt Garloff <garloff@suse.de>:
  25. * Straightened the critical function in order to prevent compilers from
  26. * playing tricks with local variables.
  27. *
  28. * Andreas Mohr <a.mohr@mailto.de>
  29. *
  30. * Alex Badea <vampire@go.ro>:
  31. * Fixed runaway init
  32. *
  33. * Rafael J. Wysocki <rjw@sisk.pl>
  34. * Added the swap map data structure and reworked the handling of swap
  35. *
  36. * More state savers are welcome. Especially for the scsi layer...
  37. *
  38. * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
  39. */
  40. #include <linux/module.h>
  41. #include <linux/mm.h>
  42. #include <linux/suspend.h>
  43. #include <linux/smp_lock.h>
  44. #include <linux/file.h>
  45. #include <linux/utsname.h>
  46. #include <linux/version.h>
  47. #include <linux/delay.h>
  48. #include <linux/bitops.h>
  49. #include <linux/spinlock.h>
  50. #include <linux/genhd.h>
  51. #include <linux/kernel.h>
  52. #include <linux/major.h>
  53. #include <linux/swap.h>
  54. #include <linux/pm.h>
  55. #include <linux/device.h>
  56. #include <linux/buffer_head.h>
  57. #include <linux/swapops.h>
  58. #include <linux/bootmem.h>
  59. #include <linux/syscalls.h>
  60. #include <linux/highmem.h>
  61. #include <linux/bio.h>
  62. #include <asm/uaccess.h>
  63. #include <asm/mmu_context.h>
  64. #include <asm/pgtable.h>
  65. #include <asm/tlbflush.h>
  66. #include <asm/io.h>
  67. #include "power.h"
  68. /*
  69. * Preferred image size in bytes (tunable via /sys/power/image_size).
  70. * When it is set to N, swsusp will do its best to ensure the image
  71. * size will not exceed N bytes, but if that is impossible, it will
  72. * try to create the smallest image possible.
  73. */
  74. unsigned long image_size = 500 * 1024 * 1024;
  75. int in_suspend __nosavedata = 0;
  76. #ifdef CONFIG_HIGHMEM
  77. unsigned int count_highmem_pages(void);
  78. int save_highmem(void);
  79. int restore_highmem(void);
  80. #else
  81. static int save_highmem(void) { return 0; }
  82. static int restore_highmem(void) { return 0; }
  83. static unsigned int count_highmem_pages(void) { return 0; }
  84. #endif
  85. extern char resume_file[];
  86. #define SWSUSP_SIG "S1SUSPEND"
  87. static struct swsusp_header {
  88. char reserved[PAGE_SIZE - 20 - sizeof(swp_entry_t)];
  89. swp_entry_t image;
  90. char orig_sig[10];
  91. char sig[10];
  92. } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
  93. /*
  94. * Saving part...
  95. */
  96. static unsigned short root_swap = 0xffff;
  97. static int mark_swapfiles(swp_entry_t start)
  98. {
  99. int error;
  100. rw_swap_page_sync(READ,
  101. swp_entry(root_swap, 0),
  102. virt_to_page((unsigned long)&swsusp_header));
  103. if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
  104. !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
  105. memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
  106. memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
  107. swsusp_header.image = start;
  108. error = rw_swap_page_sync(WRITE,
  109. swp_entry(root_swap, 0),
  110. virt_to_page((unsigned long)
  111. &swsusp_header));
  112. } else {
  113. pr_debug("swsusp: Partition is not swap space.\n");
  114. error = -ENODEV;
  115. }
  116. return error;
  117. }
  118. /**
  119. * swsusp_swap_check - check if the resume device is a swap device
  120. * and get its index (if so)
  121. */
  122. static int swsusp_swap_check(void) /* This is called before saving image */
  123. {
  124. int res = swap_type_of(swsusp_resume_device);
  125. if (res >= 0) {
  126. root_swap = res;
  127. return 0;
  128. }
  129. return res;
  130. }
  131. /**
  132. * The bitmap is used for tracing allocated swap pages
  133. *
  134. * The entire bitmap consists of a number of bitmap_page
  135. * structures linked with the help of the .next member.
  136. * Thus each page can be allocated individually, so we only
  137. * need to make 0-order memory allocations to create
  138. * the bitmap.
  139. */
  140. #define BITMAP_PAGE_SIZE (PAGE_SIZE - sizeof(void *))
  141. #define BITMAP_PAGE_CHUNKS (BITMAP_PAGE_SIZE / sizeof(long))
  142. #define BITS_PER_CHUNK (sizeof(long) * 8)
  143. #define BITMAP_PAGE_BITS (BITMAP_PAGE_CHUNKS * BITS_PER_CHUNK)
  144. struct bitmap_page {
  145. unsigned long chunks[BITMAP_PAGE_CHUNKS];
  146. struct bitmap_page *next;
  147. };
  148. /**
  149. * The following functions are used for tracing the allocated
  150. * swap pages, so that they can be freed in case of an error.
  151. *
  152. * The functions operate on a linked bitmap structure defined
  153. * above
  154. */
  155. static void free_bitmap(struct bitmap_page *bitmap)
  156. {
  157. struct bitmap_page *bp;
  158. while (bitmap) {
  159. bp = bitmap->next;
  160. free_page((unsigned long)bitmap);
  161. bitmap = bp;
  162. }
  163. }
  164. static struct bitmap_page *alloc_bitmap(unsigned int nr_bits)
  165. {
  166. struct bitmap_page *bitmap, *bp;
  167. unsigned int n;
  168. if (!nr_bits)
  169. return NULL;
  170. bitmap = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL);
  171. bp = bitmap;
  172. for (n = BITMAP_PAGE_BITS; n < nr_bits; n += BITMAP_PAGE_BITS) {
  173. bp->next = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL);
  174. bp = bp->next;
  175. if (!bp) {
  176. free_bitmap(bitmap);
  177. return NULL;
  178. }
  179. }
  180. return bitmap;
  181. }
  182. static int bitmap_set(struct bitmap_page *bitmap, unsigned long bit)
  183. {
  184. unsigned int n;
  185. n = BITMAP_PAGE_BITS;
  186. while (bitmap && n <= bit) {
  187. n += BITMAP_PAGE_BITS;
  188. bitmap = bitmap->next;
  189. }
  190. if (!bitmap)
  191. return -EINVAL;
  192. n -= BITMAP_PAGE_BITS;
  193. bit -= n;
  194. n = 0;
  195. while (bit >= BITS_PER_CHUNK) {
  196. bit -= BITS_PER_CHUNK;
  197. n++;
  198. }
  199. bitmap->chunks[n] |= (1UL << bit);
  200. return 0;
  201. }
  202. static unsigned long alloc_swap_page(int swap, struct bitmap_page *bitmap)
  203. {
  204. unsigned long offset;
  205. offset = swp_offset(get_swap_page_of_type(swap));
  206. if (offset) {
  207. if (bitmap_set(bitmap, offset)) {
  208. swap_free(swp_entry(swap, offset));
  209. offset = 0;
  210. }
  211. }
  212. return offset;
  213. }
  214. static void free_all_swap_pages(int swap, struct bitmap_page *bitmap)
  215. {
  216. unsigned int bit, n;
  217. unsigned long test;
  218. bit = 0;
  219. while (bitmap) {
  220. for (n = 0; n < BITMAP_PAGE_CHUNKS; n++)
  221. for (test = 1UL; test; test <<= 1) {
  222. if (bitmap->chunks[n] & test)
  223. swap_free(swp_entry(swap, bit));
  224. bit++;
  225. }
  226. bitmap = bitmap->next;
  227. }
  228. }
  229. /**
  230. * write_page - Write one page to given swap location.
  231. * @buf: Address we're writing.
  232. * @offset: Offset of the swap page we're writing to.
  233. */
  234. static int write_page(void *buf, unsigned long offset)
  235. {
  236. swp_entry_t entry;
  237. int error = -ENOSPC;
  238. if (offset) {
  239. entry = swp_entry(root_swap, offset);
  240. error = rw_swap_page_sync(WRITE, entry, virt_to_page(buf));
  241. }
  242. return error;
  243. }
  244. /*
  245. * The swap map is a data structure used for keeping track of each page
  246. * written to a swap partition. It consists of many swap_map_page
  247. * structures that contain each an array of MAP_PAGE_SIZE swap entries.
  248. * These structures are stored on the swap and linked together with the
  249. * help of the .next_swap member.
  250. *
  251. * The swap map is created during suspend. The swap map pages are
  252. * allocated and populated one at a time, so we only need one memory
  253. * page to set up the entire structure.
  254. *
  255. * During resume we also only need to use one swap_map_page structure
  256. * at a time.
  257. */
  258. #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(long) - 1)
  259. struct swap_map_page {
  260. unsigned long entries[MAP_PAGE_ENTRIES];
  261. unsigned long next_swap;
  262. };
  263. /**
  264. * The swap_map_handle structure is used for handling swap in
  265. * a file-alike way
  266. */
  267. struct swap_map_handle {
  268. struct swap_map_page *cur;
  269. unsigned long cur_swap;
  270. struct bitmap_page *bitmap;
  271. unsigned int k;
  272. };
  273. static void release_swap_writer(struct swap_map_handle *handle)
  274. {
  275. if (handle->cur)
  276. free_page((unsigned long)handle->cur);
  277. handle->cur = NULL;
  278. if (handle->bitmap)
  279. free_bitmap(handle->bitmap);
  280. handle->bitmap = NULL;
  281. }
  282. static int get_swap_writer(struct swap_map_handle *handle)
  283. {
  284. handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
  285. if (!handle->cur)
  286. return -ENOMEM;
  287. handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0));
  288. if (!handle->bitmap) {
  289. release_swap_writer(handle);
  290. return -ENOMEM;
  291. }
  292. handle->cur_swap = alloc_swap_page(root_swap, handle->bitmap);
  293. if (!handle->cur_swap) {
  294. release_swap_writer(handle);
  295. return -ENOSPC;
  296. }
  297. handle->k = 0;
  298. return 0;
  299. }
  300. static int swap_write_page(struct swap_map_handle *handle, void *buf)
  301. {
  302. int error;
  303. unsigned long offset;
  304. if (!handle->cur)
  305. return -EINVAL;
  306. offset = alloc_swap_page(root_swap, handle->bitmap);
  307. error = write_page(buf, offset);
  308. if (error)
  309. return error;
  310. handle->cur->entries[handle->k++] = offset;
  311. if (handle->k >= MAP_PAGE_ENTRIES) {
  312. offset = alloc_swap_page(root_swap, handle->bitmap);
  313. if (!offset)
  314. return -ENOSPC;
  315. handle->cur->next_swap = offset;
  316. error = write_page(handle->cur, handle->cur_swap);
  317. if (error)
  318. return error;
  319. memset(handle->cur, 0, PAGE_SIZE);
  320. handle->cur_swap = offset;
  321. handle->k = 0;
  322. }
  323. return 0;
  324. }
  325. static int flush_swap_writer(struct swap_map_handle *handle)
  326. {
  327. if (handle->cur && handle->cur_swap)
  328. return write_page(handle->cur, handle->cur_swap);
  329. else
  330. return -EINVAL;
  331. }
  332. /**
  333. * save_image - save the suspend image data
  334. */
  335. static int save_image(struct swap_map_handle *handle,
  336. struct snapshot_handle *snapshot,
  337. unsigned int nr_pages)
  338. {
  339. unsigned int m;
  340. int ret;
  341. int error = 0;
  342. printk("Saving image data pages (%u pages) ... ", nr_pages);
  343. m = nr_pages / 100;
  344. if (!m)
  345. m = 1;
  346. nr_pages = 0;
  347. do {
  348. ret = snapshot_read_next(snapshot, PAGE_SIZE);
  349. if (ret > 0) {
  350. error = swap_write_page(handle, data_of(*snapshot));
  351. if (error)
  352. break;
  353. if (!(nr_pages % m))
  354. printk("\b\b\b\b%3d%%", nr_pages / m);
  355. nr_pages++;
  356. }
  357. } while (ret > 0);
  358. if (!error)
  359. printk("\b\b\b\bdone\n");
  360. return error;
  361. }
  362. /**
  363. * enough_swap - Make sure we have enough swap to save the image.
  364. *
  365. * Returns TRUE or FALSE after checking the total amount of swap
  366. * space avaiable from the resume partition.
  367. */
  368. static int enough_swap(unsigned int nr_pages)
  369. {
  370. unsigned int free_swap = count_swap_pages(root_swap, 1);
  371. pr_debug("swsusp: free swap pages: %u\n", free_swap);
  372. return free_swap > (nr_pages + PAGES_FOR_IO +
  373. (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
  374. }
  375. /**
  376. * swsusp_write - Write entire image and metadata.
  377. *
  378. * It is important _NOT_ to umount filesystems at this point. We want
  379. * them synced (in case something goes wrong) but we DO not want to mark
  380. * filesystem clean: it is not. (And it does not matter, if we resume
  381. * correctly, we'll mark system clean, anyway.)
  382. */
  383. int swsusp_write(void)
  384. {
  385. struct swap_map_handle handle;
  386. struct snapshot_handle snapshot;
  387. struct swsusp_info *header;
  388. unsigned long start;
  389. int error;
  390. if ((error = swsusp_swap_check())) {
  391. printk(KERN_ERR "swsusp: Cannot find swap device, try swapon -a.\n");
  392. return error;
  393. }
  394. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  395. error = snapshot_read_next(&snapshot, PAGE_SIZE);
  396. if (error < PAGE_SIZE)
  397. return error < 0 ? error : -EFAULT;
  398. header = (struct swsusp_info *)data_of(snapshot);
  399. if (!enough_swap(header->pages)) {
  400. printk(KERN_ERR "swsusp: Not enough free swap\n");
  401. return -ENOSPC;
  402. }
  403. error = get_swap_writer(&handle);
  404. if (!error) {
  405. start = handle.cur_swap;
  406. error = swap_write_page(&handle, header);
  407. }
  408. if (!error)
  409. error = save_image(&handle, &snapshot, header->pages - 1);
  410. if (!error) {
  411. flush_swap_writer(&handle);
  412. printk("S");
  413. error = mark_swapfiles(swp_entry(root_swap, start));
  414. printk("|\n");
  415. }
  416. if (error)
  417. free_all_swap_pages(root_swap, handle.bitmap);
  418. release_swap_writer(&handle);
  419. return error;
  420. }
  421. /**
  422. * swsusp_shrink_memory - Try to free as much memory as needed
  423. *
  424. * ... but do not OOM-kill anyone
  425. *
  426. * Notice: all userland should be stopped before it is called, or
  427. * livelock is possible.
  428. */
  429. #define SHRINK_BITE 10000
  430. int swsusp_shrink_memory(void)
  431. {
  432. long size, tmp;
  433. struct zone *zone;
  434. unsigned long pages = 0;
  435. unsigned int i = 0;
  436. char *p = "-\\|/";
  437. printk("Shrinking memory... ");
  438. do {
  439. size = 2 * count_highmem_pages();
  440. size += size / 50 + count_data_pages();
  441. size += (size + PBES_PER_PAGE - 1) / PBES_PER_PAGE +
  442. PAGES_FOR_IO;
  443. tmp = size;
  444. for_each_zone (zone)
  445. if (!is_highmem(zone))
  446. tmp -= zone->free_pages;
  447. if (tmp > 0) {
  448. tmp = shrink_all_memory(SHRINK_BITE);
  449. if (!tmp)
  450. return -ENOMEM;
  451. pages += tmp;
  452. } else if (size > image_size / PAGE_SIZE) {
  453. tmp = shrink_all_memory(SHRINK_BITE);
  454. pages += tmp;
  455. }
  456. printk("\b%c", p[i++%4]);
  457. } while (tmp > 0);
  458. printk("\bdone (%lu pages freed)\n", pages);
  459. return 0;
  460. }
  461. int swsusp_suspend(void)
  462. {
  463. int error;
  464. if ((error = arch_prepare_suspend()))
  465. return error;
  466. local_irq_disable();
  467. /* At this point, device_suspend() has been called, but *not*
  468. * device_power_down(). We *must* device_power_down() now.
  469. * Otherwise, drivers for some devices (e.g. interrupt controllers)
  470. * become desynchronized with the actual state of the hardware
  471. * at resume time, and evil weirdness ensues.
  472. */
  473. if ((error = device_power_down(PMSG_FREEZE))) {
  474. printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
  475. goto Enable_irqs;
  476. }
  477. if ((error = save_highmem())) {
  478. printk(KERN_ERR "swsusp: Not enough free pages for highmem\n");
  479. goto Restore_highmem;
  480. }
  481. save_processor_state();
  482. if ((error = swsusp_arch_suspend()))
  483. printk(KERN_ERR "Error %d suspending\n", error);
  484. /* Restore control flow magically appears here */
  485. restore_processor_state();
  486. Restore_highmem:
  487. restore_highmem();
  488. device_power_up();
  489. Enable_irqs:
  490. local_irq_enable();
  491. return error;
  492. }
  493. int swsusp_resume(void)
  494. {
  495. int error;
  496. local_irq_disable();
  497. if (device_power_down(PMSG_FREEZE))
  498. printk(KERN_ERR "Some devices failed to power down, very bad\n");
  499. /* We'll ignore saved state, but this gets preempt count (etc) right */
  500. save_processor_state();
  501. error = swsusp_arch_resume();
  502. /* Code below is only ever reached in case of failure. Otherwise
  503. * execution continues at place where swsusp_arch_suspend was called
  504. */
  505. BUG_ON(!error);
  506. /* The only reason why swsusp_arch_resume() can fail is memory being
  507. * very tight, so we have to free it as soon as we can to avoid
  508. * subsequent failures
  509. */
  510. swsusp_free();
  511. restore_processor_state();
  512. restore_highmem();
  513. touch_softlockup_watchdog();
  514. device_power_up();
  515. local_irq_enable();
  516. return error;
  517. }
  518. /*
  519. * Using bio to read from swap.
  520. * This code requires a bit more work than just using buffer heads
  521. * but, it is the recommended way for 2.5/2.6.
  522. * The following are to signal the beginning and end of I/O. Bios
  523. * finish asynchronously, while we want them to happen synchronously.
  524. * A simple atomic_t, and a wait loop take care of this problem.
  525. */
  526. static atomic_t io_done = ATOMIC_INIT(0);
  527. static int end_io(struct bio *bio, unsigned int num, int err)
  528. {
  529. if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  530. panic("I/O error reading memory image");
  531. atomic_set(&io_done, 0);
  532. return 0;
  533. }
  534. static struct block_device *resume_bdev;
  535. /**
  536. * submit - submit BIO request.
  537. * @rw: READ or WRITE.
  538. * @off physical offset of page.
  539. * @page: page we're reading or writing.
  540. *
  541. * Straight from the textbook - allocate and initialize the bio.
  542. * If we're writing, make sure the page is marked as dirty.
  543. * Then submit it and wait.
  544. */
  545. static int submit(int rw, pgoff_t page_off, void *page)
  546. {
  547. int error = 0;
  548. struct bio *bio;
  549. bio = bio_alloc(GFP_ATOMIC, 1);
  550. if (!bio)
  551. return -ENOMEM;
  552. bio->bi_sector = page_off * (PAGE_SIZE >> 9);
  553. bio->bi_bdev = resume_bdev;
  554. bio->bi_end_io = end_io;
  555. if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
  556. printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
  557. error = -EFAULT;
  558. goto Done;
  559. }
  560. atomic_set(&io_done, 1);
  561. submit_bio(rw | (1 << BIO_RW_SYNC), bio);
  562. while (atomic_read(&io_done))
  563. yield();
  564. if (rw == READ)
  565. bio_set_pages_dirty(bio);
  566. Done:
  567. bio_put(bio);
  568. return error;
  569. }
  570. static int bio_read_page(pgoff_t page_off, void *page)
  571. {
  572. return submit(READ, page_off, page);
  573. }
  574. static int bio_write_page(pgoff_t page_off, void *page)
  575. {
  576. return submit(WRITE, page_off, page);
  577. }
  578. /**
  579. * The following functions allow us to read data using a swap map
  580. * in a file-alike way
  581. */
  582. static void release_swap_reader(struct swap_map_handle *handle)
  583. {
  584. if (handle->cur)
  585. free_page((unsigned long)handle->cur);
  586. handle->cur = NULL;
  587. }
  588. static int get_swap_reader(struct swap_map_handle *handle,
  589. swp_entry_t start)
  590. {
  591. int error;
  592. if (!swp_offset(start))
  593. return -EINVAL;
  594. handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
  595. if (!handle->cur)
  596. return -ENOMEM;
  597. error = bio_read_page(swp_offset(start), handle->cur);
  598. if (error) {
  599. release_swap_reader(handle);
  600. return error;
  601. }
  602. handle->k = 0;
  603. return 0;
  604. }
  605. static int swap_read_page(struct swap_map_handle *handle, void *buf)
  606. {
  607. unsigned long offset;
  608. int error;
  609. if (!handle->cur)
  610. return -EINVAL;
  611. offset = handle->cur->entries[handle->k];
  612. if (!offset)
  613. return -EFAULT;
  614. error = bio_read_page(offset, buf);
  615. if (error)
  616. return error;
  617. if (++handle->k >= MAP_PAGE_ENTRIES) {
  618. handle->k = 0;
  619. offset = handle->cur->next_swap;
  620. if (!offset)
  621. release_swap_reader(handle);
  622. else
  623. error = bio_read_page(offset, handle->cur);
  624. }
  625. return error;
  626. }
  627. /**
  628. * load_image - load the image using the swap map handle
  629. * @handle and the snapshot handle @snapshot
  630. * (assume there are @nr_pages pages to load)
  631. */
  632. static int load_image(struct swap_map_handle *handle,
  633. struct snapshot_handle *snapshot,
  634. unsigned int nr_pages)
  635. {
  636. unsigned int m;
  637. int ret;
  638. int error = 0;
  639. printk("Loading image data pages (%u pages) ... ", nr_pages);
  640. m = nr_pages / 100;
  641. if (!m)
  642. m = 1;
  643. nr_pages = 0;
  644. do {
  645. ret = snapshot_write_next(snapshot, PAGE_SIZE);
  646. if (ret > 0) {
  647. error = swap_read_page(handle, data_of(*snapshot));
  648. if (error)
  649. break;
  650. if (!(nr_pages % m))
  651. printk("\b\b\b\b%3d%%", nr_pages / m);
  652. nr_pages++;
  653. }
  654. } while (ret > 0);
  655. if (!error)
  656. printk("\b\b\b\bdone\n");
  657. if (!snapshot_image_loaded(snapshot))
  658. error = -ENODATA;
  659. return error;
  660. }
  661. int swsusp_read(void)
  662. {
  663. int error;
  664. struct swap_map_handle handle;
  665. struct snapshot_handle snapshot;
  666. struct swsusp_info *header;
  667. unsigned int nr_pages;
  668. if (IS_ERR(resume_bdev)) {
  669. pr_debug("swsusp: block device not initialised\n");
  670. return PTR_ERR(resume_bdev);
  671. }
  672. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  673. error = snapshot_write_next(&snapshot, PAGE_SIZE);
  674. if (error < PAGE_SIZE)
  675. return error < 0 ? error : -EFAULT;
  676. header = (struct swsusp_info *)data_of(snapshot);
  677. error = get_swap_reader(&handle, swsusp_header.image);
  678. if (!error)
  679. error = swap_read_page(&handle, header);
  680. if (!error) {
  681. nr_pages = header->image_pages;
  682. error = load_image(&handle, &snapshot, nr_pages);
  683. }
  684. release_swap_reader(&handle);
  685. blkdev_put(resume_bdev);
  686. if (!error)
  687. pr_debug("swsusp: Reading resume file was successful\n");
  688. else
  689. pr_debug("swsusp: Error %d resuming\n", error);
  690. return error;
  691. }
  692. /**
  693. * swsusp_check - Check for swsusp signature in the resume device
  694. */
  695. int swsusp_check(void)
  696. {
  697. int error;
  698. resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
  699. if (!IS_ERR(resume_bdev)) {
  700. set_blocksize(resume_bdev, PAGE_SIZE);
  701. memset(&swsusp_header, 0, sizeof(swsusp_header));
  702. if ((error = bio_read_page(0, &swsusp_header)))
  703. return error;
  704. if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
  705. memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
  706. /* Reset swap signature now */
  707. error = bio_write_page(0, &swsusp_header);
  708. } else {
  709. return -EINVAL;
  710. }
  711. if (error)
  712. blkdev_put(resume_bdev);
  713. else
  714. pr_debug("swsusp: Signature found, resuming\n");
  715. } else {
  716. error = PTR_ERR(resume_bdev);
  717. }
  718. if (error)
  719. pr_debug("swsusp: Error %d check for resume file\n", error);
  720. return error;
  721. }
  722. /**
  723. * swsusp_close - close swap device.
  724. */
  725. void swsusp_close(void)
  726. {
  727. if (IS_ERR(resume_bdev)) {
  728. pr_debug("swsusp: block device not initialised\n");
  729. return;
  730. }
  731. blkdev_put(resume_bdev);
  732. }