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