linux/kernel/power/hibernate.c
<<
>>
Prefs
   1/*
   2 * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support.
   3 *
   4 * Copyright (c) 2003 Patrick Mochel
   5 * Copyright (c) 2003 Open Source Development Lab
   6 * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz>
   7 * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
   8 * Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com>
   9 *
  10 * This file is released under the GPLv2.
  11 */
  12
  13#include <linux/export.h>
  14#include <linux/suspend.h>
  15#include <linux/syscalls.h>
  16#include <linux/reboot.h>
  17#include <linux/string.h>
  18#include <linux/device.h>
  19#include <linux/async.h>
  20#include <linux/delay.h>
  21#include <linux/fs.h>
  22#include <linux/mount.h>
  23#include <linux/pm.h>
  24#include <linux/console.h>
  25#include <linux/cpu.h>
  26#include <linux/freezer.h>
  27#include <linux/gfp.h>
  28#include <linux/syscore_ops.h>
  29#include <linux/ctype.h>
  30#include <linux/genhd.h>
  31#include <linux/security.h>
  32
  33#include "power.h"
  34
  35
  36static int nocompress;
  37static int noresume;
  38static int nohibernate;
  39static int resume_wait;
  40static int resume_delay;
  41static char resume_file[256] = CONFIG_PM_STD_PARTITION;
  42dev_t swsusp_resume_device;
  43sector_t swsusp_resume_block;
  44int in_suspend __nosavedata;
  45
  46enum {
  47        HIBERNATION_INVALID,
  48        HIBERNATION_PLATFORM,
  49        HIBERNATION_SHUTDOWN,
  50        HIBERNATION_REBOOT,
  51#ifdef CONFIG_SUSPEND
  52        HIBERNATION_SUSPEND,
  53#endif
  54        /* keep last */
  55        __HIBERNATION_AFTER_LAST
  56};
  57#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
  58#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
  59
  60static int hibernation_mode = HIBERNATION_SHUTDOWN;
  61
  62bool freezer_test_done;
  63
  64static const struct platform_hibernation_ops *hibernation_ops;
  65
  66bool hibernation_available(void)
  67{
  68        return (nohibernate == 0);
  69}
  70
  71/**
  72 * hibernation_set_ops - Set the global hibernate operations.
  73 * @ops: Hibernation operations to use in subsequent hibernation transitions.
  74 */
  75void hibernation_set_ops(const struct platform_hibernation_ops *ops)
  76{
  77        if (ops && !(ops->begin && ops->end &&  ops->pre_snapshot
  78            && ops->prepare && ops->finish && ops->enter && ops->pre_restore
  79            && ops->restore_cleanup && ops->leave)) {
  80                WARN_ON(1);
  81                return;
  82        }
  83        lock_system_sleep();
  84        hibernation_ops = ops;
  85        if (ops)
  86                hibernation_mode = HIBERNATION_PLATFORM;
  87        else if (hibernation_mode == HIBERNATION_PLATFORM)
  88                hibernation_mode = HIBERNATION_SHUTDOWN;
  89
  90        unlock_system_sleep();
  91}
  92
  93static bool entering_platform_hibernation;
  94
  95bool system_entering_hibernation(void)
  96{
  97        return entering_platform_hibernation;
  98}
  99EXPORT_SYMBOL(system_entering_hibernation);
 100
 101#ifdef CONFIG_PM_DEBUG
 102static void hibernation_debug_sleep(void)
 103{
 104        printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
 105        mdelay(5000);
 106}
 107
 108static int hibernation_test(int level)
 109{
 110        if (pm_test_level == level) {
 111                hibernation_debug_sleep();
 112                return 1;
 113        }
 114        return 0;
 115}
 116#else /* !CONFIG_PM_DEBUG */
 117static int hibernation_test(int level) { return 0; }
 118#endif /* !CONFIG_PM_DEBUG */
 119
 120/**
 121 * platform_begin - Call platform to start hibernation.
 122 * @platform_mode: Whether or not to use the platform driver.
 123 */
 124static int platform_begin(int platform_mode)
 125{
 126        return (platform_mode && hibernation_ops) ?
 127                hibernation_ops->begin() : 0;
 128}
 129
 130/**
 131 * platform_end - Call platform to finish transition to the working state.
 132 * @platform_mode: Whether or not to use the platform driver.
 133 */
 134static void platform_end(int platform_mode)
 135{
 136        if (platform_mode && hibernation_ops)
 137                hibernation_ops->end();
 138}
 139
 140/**
 141 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
 142 * @platform_mode: Whether or not to use the platform driver.
 143 *
 144 * Use the platform driver to prepare the system for creating a hibernate image,
 145 * if so configured, and return an error code if that fails.
 146 */
 147
 148static int platform_pre_snapshot(int platform_mode)
 149{
 150        return (platform_mode && hibernation_ops) ?
 151                hibernation_ops->pre_snapshot() : 0;
 152}
 153
 154/**
 155 * platform_leave - Call platform to prepare a transition to the working state.
 156 * @platform_mode: Whether or not to use the platform driver.
 157 *
 158 * Use the platform driver prepare to prepare the machine for switching to the
 159 * normal mode of operation.
 160 *
 161 * This routine is called on one CPU with interrupts disabled.
 162 */
 163static void platform_leave(int platform_mode)
 164{
 165        if (platform_mode && hibernation_ops)
 166                hibernation_ops->leave();
 167}
 168
 169/**
 170 * platform_finish - Call platform to switch the system to the working state.
 171 * @platform_mode: Whether or not to use the platform driver.
 172 *
 173 * Use the platform driver to switch the machine to the normal mode of
 174 * operation.
 175 *
 176 * This routine must be called after platform_prepare().
 177 */
 178static void platform_finish(int platform_mode)
 179{
 180        if (platform_mode && hibernation_ops)
 181                hibernation_ops->finish();
 182}
 183
 184/**
 185 * platform_pre_restore - Prepare for hibernate image restoration.
 186 * @platform_mode: Whether or not to use the platform driver.
 187 *
 188 * Use the platform driver to prepare the system for resume from a hibernation
 189 * image.
 190 *
 191 * If the restore fails after this function has been called,
 192 * platform_restore_cleanup() must be called.
 193 */
 194static int platform_pre_restore(int platform_mode)
 195{
 196        return (platform_mode && hibernation_ops) ?
 197                hibernation_ops->pre_restore() : 0;
 198}
 199
 200/**
 201 * platform_restore_cleanup - Switch to the working state after failing restore.
 202 * @platform_mode: Whether or not to use the platform driver.
 203 *
 204 * Use the platform driver to switch the system to the normal mode of operation
 205 * after a failing restore.
 206 *
 207 * If platform_pre_restore() has been called before the failing restore, this
 208 * function must be called too, regardless of the result of
 209 * platform_pre_restore().
 210 */
 211static void platform_restore_cleanup(int platform_mode)
 212{
 213        if (platform_mode && hibernation_ops)
 214                hibernation_ops->restore_cleanup();
 215}
 216
 217/**
 218 * platform_recover - Recover from a failure to suspend devices.
 219 * @platform_mode: Whether or not to use the platform driver.
 220 */
 221static void platform_recover(int platform_mode)
 222{
 223        if (platform_mode && hibernation_ops && hibernation_ops->recover)
 224                hibernation_ops->recover();
 225}
 226
 227/**
 228 * swsusp_show_speed - Print time elapsed between two events during hibernation.
 229 * @start: Starting event.
 230 * @stop: Final event.
 231 * @nr_pages: Number of memory pages processed between @start and @stop.
 232 * @msg: Additional diagnostic message to print.
 233 */
 234void swsusp_show_speed(struct timeval *start, struct timeval *stop,
 235                        unsigned nr_pages, char *msg)
 236{
 237        s64 elapsed_centisecs64;
 238        int centisecs;
 239        int k;
 240        int kps;
 241
 242        elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
 243        do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
 244        centisecs = elapsed_centisecs64;
 245        if (centisecs == 0)
 246                centisecs = 1;  /* avoid div-by-zero */
 247        k = nr_pages * (PAGE_SIZE / 1024);
 248        kps = (k * 100) / centisecs;
 249        printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
 250                        msg, k,
 251                        centisecs / 100, centisecs % 100,
 252                        kps / 1000, (kps % 1000) / 10);
 253}
 254
 255/**
 256 * create_image - Create a hibernation image.
 257 * @platform_mode: Whether or not to use the platform driver.
 258 *
 259 * Execute device drivers' "late" and "noirq" freeze callbacks, create a
 260 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
 261 *
 262 * Control reappears in this routine after the subsequent restore.
 263 */
 264static int create_image(int platform_mode)
 265{
 266        int error;
 267
 268        error = dpm_suspend_end(PMSG_FREEZE);
 269        if (error) {
 270                printk(KERN_ERR "PM: Some devices failed to power down, "
 271                        "aborting hibernation\n");
 272                return error;
 273        }
 274
 275        error = platform_pre_snapshot(platform_mode);
 276        if (error || hibernation_test(TEST_PLATFORM))
 277                goto Platform_finish;
 278
 279        error = disable_nonboot_cpus();
 280        if (error || hibernation_test(TEST_CPUS))
 281                goto Enable_cpus;
 282
 283        local_irq_disable();
 284
 285        error = syscore_suspend();
 286        if (error) {
 287                printk(KERN_ERR "PM: Some system devices failed to power down, "
 288                        "aborting hibernation\n");
 289                goto Enable_irqs;
 290        }
 291
 292        if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
 293                goto Power_up;
 294
 295        in_suspend = 1;
 296        save_processor_state();
 297        error = swsusp_arch_suspend();
 298        /* Restore control flow magically appears here */
 299        restore_processor_state();
 300        if (error)
 301                printk(KERN_ERR "PM: Error %d creating hibernation image\n",
 302                        error);
 303        if (!in_suspend) {
 304                events_check_enabled = false;
 305                platform_leave(platform_mode);
 306        }
 307
 308 Power_up:
 309        syscore_resume();
 310
 311 Enable_irqs:
 312        local_irq_enable();
 313
 314 Enable_cpus:
 315        enable_nonboot_cpus();
 316
 317 Platform_finish:
 318        platform_finish(platform_mode);
 319
 320        dpm_resume_start(in_suspend ?
 321                (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
 322
 323        return error;
 324}
 325
 326/**
 327 * hibernation_snapshot - Quiesce devices and create a hibernation image.
 328 * @platform_mode: If set, use platform driver to prepare for the transition.
 329 *
 330 * This routine must be called with pm_mutex held.
 331 */
 332int hibernation_snapshot(int platform_mode)
 333{
 334        pm_message_t msg;
 335        int error;
 336
 337        error = platform_begin(platform_mode);
 338        if (error)
 339                goto Close;
 340
 341        /* Preallocate image memory before shutting down devices. */
 342        error = hibernate_preallocate_memory();
 343        if (error)
 344                goto Close;
 345
 346        error = freeze_kernel_threads();
 347        if (error)
 348                goto Cleanup;
 349
 350        if (hibernation_test(TEST_FREEZER)) {
 351
 352                /*
 353                 * Indicate to the caller that we are returning due to a
 354                 * successful freezer test.
 355                 */
 356                freezer_test_done = true;
 357                goto Thaw;
 358        }
 359
 360        error = dpm_prepare(PMSG_FREEZE);
 361        if (error) {
 362                dpm_complete(PMSG_RECOVER);
 363                goto Thaw;
 364        }
 365
 366        suspend_console();
 367        pm_restrict_gfp_mask();
 368
 369        error = dpm_suspend(PMSG_FREEZE);
 370
 371        if (error || hibernation_test(TEST_DEVICES))
 372                platform_recover(platform_mode);
 373        else
 374                error = create_image(platform_mode);
 375
 376        /*
 377         * In the case that we call create_image() above, the control
 378         * returns here (1) after the image has been created or the
 379         * image creation has failed and (2) after a successful restore.
 380         */
 381
 382        /* We may need to release the preallocated image pages here. */
 383        if (error || !in_suspend)
 384                swsusp_free();
 385
 386        msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
 387        dpm_resume(msg);
 388
 389        if (error || !in_suspend)
 390                pm_restore_gfp_mask();
 391
 392        resume_console();
 393        dpm_complete(msg);
 394
 395 Close:
 396        platform_end(platform_mode);
 397        return error;
 398
 399 Thaw:
 400        thaw_kernel_threads();
 401 Cleanup:
 402        swsusp_free();
 403        goto Close;
 404}
 405
 406int __weak hibernate_resume_nonboot_cpu_disable(void)
 407{
 408        return disable_nonboot_cpus();
 409}
 410
 411/**
 412 * resume_target_kernel - Restore system state from a hibernation image.
 413 * @platform_mode: Whether or not to use the platform driver.
 414 *
 415 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
 416 * contents of highmem that have not been restored yet from the image and run
 417 * the low-level code that will restore the remaining contents of memory and
 418 * switch to the just restored target kernel.
 419 */
 420static int resume_target_kernel(bool platform_mode)
 421{
 422        int error;
 423
 424        error = dpm_suspend_end(PMSG_QUIESCE);
 425        if (error) {
 426                printk(KERN_ERR "PM: Some devices failed to power down, "
 427                        "aborting resume\n");
 428                return error;
 429        }
 430
 431        error = platform_pre_restore(platform_mode);
 432        if (error)
 433                goto Cleanup;
 434
 435        error = hibernate_resume_nonboot_cpu_disable();
 436        if (error)
 437                goto Enable_cpus;
 438
 439        local_irq_disable();
 440
 441        error = syscore_suspend();
 442        if (error)
 443                goto Enable_irqs;
 444
 445        save_processor_state();
 446        error = restore_highmem();
 447        if (!error) {
 448                error = swsusp_arch_resume();
 449                /*
 450                 * The code below is only ever reached in case of a failure.
 451                 * Otherwise, execution continues at the place where
 452                 * swsusp_arch_suspend() was called.
 453                 */
 454                BUG_ON(!error);
 455                /*
 456                 * This call to restore_highmem() reverts the changes made by
 457                 * the previous one.
 458                 */
 459                restore_highmem();
 460        }
 461        /*
 462         * The only reason why swsusp_arch_resume() can fail is memory being
 463         * very tight, so we have to free it as soon as we can to avoid
 464         * subsequent failures.
 465         */
 466        swsusp_free();
 467        restore_processor_state();
 468        touch_softlockup_watchdog();
 469
 470        syscore_resume();
 471
 472 Enable_irqs:
 473        local_irq_enable();
 474
 475 Enable_cpus:
 476        enable_nonboot_cpus();
 477
 478 Cleanup:
 479        platform_restore_cleanup(platform_mode);
 480
 481        dpm_resume_start(PMSG_RECOVER);
 482
 483        return error;
 484}
 485
 486/**
 487 * hibernation_restore - Quiesce devices and restore from a hibernation image.
 488 * @platform_mode: If set, use platform driver to prepare for the transition.
 489 *
 490 * This routine must be called with pm_mutex held.  If it is successful, control
 491 * reappears in the restored target kernel in hibernation_snapshot().
 492 */
 493int hibernation_restore(int platform_mode)
 494{
 495        int error;
 496
 497        pm_prepare_console();
 498        suspend_console();
 499        pm_restrict_gfp_mask();
 500        error = dpm_suspend_start(PMSG_QUIESCE);
 501        if (!error) {
 502                error = resume_target_kernel(platform_mode);
 503                dpm_resume_end(PMSG_RECOVER);
 504        }
 505        pm_restore_gfp_mask();
 506        resume_console();
 507        pm_restore_console();
 508        return error;
 509}
 510
 511/**
 512 * hibernation_platform_enter - Power off the system using the platform driver.
 513 */
 514int hibernation_platform_enter(void)
 515{
 516        int error;
 517
 518        if (!hibernation_ops)
 519                return -ENOSYS;
 520
 521        /*
 522         * We have cancelled the power transition by running
 523         * hibernation_ops->finish() before saving the image, so we should let
 524         * the firmware know that we're going to enter the sleep state after all
 525         */
 526        error = hibernation_ops->begin();
 527        if (error)
 528                goto Close;
 529
 530        entering_platform_hibernation = true;
 531        suspend_console();
 532        error = dpm_suspend_start(PMSG_HIBERNATE);
 533        if (error) {
 534                if (hibernation_ops->recover)
 535                        hibernation_ops->recover();
 536                goto Resume_devices;
 537        }
 538
 539        error = dpm_suspend_end(PMSG_HIBERNATE);
 540        if (error)
 541                goto Resume_devices;
 542
 543        error = hibernation_ops->prepare();
 544        if (error)
 545                goto Platform_finish;
 546
 547        error = disable_nonboot_cpus();
 548        if (error)
 549                goto Platform_finish;
 550
 551        local_irq_disable();
 552        syscore_suspend();
 553        if (pm_wakeup_pending()) {
 554                error = -EAGAIN;
 555                goto Power_up;
 556        }
 557
 558        hibernation_ops->enter();
 559        /* We should never get here */
 560        while (1);
 561
 562 Power_up:
 563        syscore_resume();
 564        local_irq_enable();
 565        enable_nonboot_cpus();
 566
 567 Platform_finish:
 568        hibernation_ops->finish();
 569
 570        dpm_resume_start(PMSG_RESTORE);
 571
 572 Resume_devices:
 573        entering_platform_hibernation = false;
 574        dpm_resume_end(PMSG_RESTORE);
 575        resume_console();
 576
 577 Close:
 578        hibernation_ops->end();
 579
 580        return error;
 581}
 582
 583/**
 584 * power_down - Shut the machine down for hibernation.
 585 *
 586 * Use the platform driver, if configured, to put the system into the sleep
 587 * state corresponding to hibernation, or try to power it off or reboot,
 588 * depending on the value of hibernation_mode.
 589 */
 590static void power_down(void)
 591{
 592#ifdef CONFIG_SUSPEND
 593        int error;
 594#endif
 595
 596        switch (hibernation_mode) {
 597        case HIBERNATION_REBOOT:
 598                kernel_restart(NULL);
 599                break;
 600        case HIBERNATION_PLATFORM:
 601                hibernation_platform_enter();
 602        case HIBERNATION_SHUTDOWN:
 603                kernel_power_off();
 604                break;
 605#ifdef CONFIG_SUSPEND
 606        case HIBERNATION_SUSPEND:
 607                error = suspend_devices_and_enter(PM_SUSPEND_MEM);
 608                if (error) {
 609                        if (hibernation_ops)
 610                                hibernation_mode = HIBERNATION_PLATFORM;
 611                        else
 612                                hibernation_mode = HIBERNATION_SHUTDOWN;
 613                        power_down();
 614                }
 615                /*
 616                 * Restore swap signature.
 617                 */
 618                error = swsusp_unmark();
 619                if (error)
 620                        printk(KERN_ERR "PM: Swap will be unusable! "
 621                                        "Try swapon -a.\n");
 622                return;
 623#endif
 624        }
 625        kernel_halt();
 626        /*
 627         * Valid image is on the disk, if we continue we risk serious data
 628         * corruption after resume.
 629         */
 630        printk(KERN_CRIT "PM: Please power down manually\n");
 631        while(1);
 632}
 633
 634/**
 635 * hibernate - Carry out system hibernation, including saving the image.
 636 */
 637int hibernate(void)
 638{
 639        int error;
 640
 641        if (get_securelevel() > 0) {
 642                return -EPERM;
 643        }
 644
 645        if (!hibernation_available()) {
 646                pr_debug("PM: Hibernation not available.\n");
 647                return -EPERM;
 648        }
 649
 650        lock_system_sleep();
 651        /* The snapshot device should not be opened while we're running */
 652        if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
 653                error = -EBUSY;
 654                goto Unlock;
 655        }
 656
 657        pm_prepare_console();
 658        error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
 659        if (error)
 660                goto Exit;
 661
 662        printk(KERN_INFO "PM: Syncing filesystems ... ");
 663        sys_sync();
 664        printk("done.\n");
 665
 666        error = freeze_processes();
 667        if (error)
 668                goto Exit;
 669
 670        lock_device_hotplug();
 671        /* Allocate memory management structures */
 672        error = create_basic_memory_bitmaps();
 673        if (error)
 674                goto Thaw;
 675
 676        error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
 677        if (error || freezer_test_done)
 678                goto Free_bitmaps;
 679
 680        if (in_suspend) {
 681                unsigned int flags = 0;
 682
 683                if (hibernation_mode == HIBERNATION_PLATFORM)
 684                        flags |= SF_PLATFORM_MODE;
 685                if (nocompress)
 686                        flags |= SF_NOCOMPRESS_MODE;
 687                else
 688                        flags |= SF_CRC32_MODE;
 689
 690                pr_debug("PM: writing image.\n");
 691                error = swsusp_write(flags);
 692                swsusp_free();
 693                if (!error)
 694                        power_down();
 695                in_suspend = 0;
 696                pm_restore_gfp_mask();
 697        } else {
 698                pr_debug("PM: Image restored successfully.\n");
 699        }
 700
 701 Free_bitmaps:
 702        free_basic_memory_bitmaps();
 703 Thaw:
 704        unlock_device_hotplug();
 705        thaw_processes();
 706
 707        /* Don't bother checking whether freezer_test_done is true */
 708        freezer_test_done = false;
 709 Exit:
 710        pm_notifier_call_chain(PM_POST_HIBERNATION);
 711        pm_restore_console();
 712        atomic_inc(&snapshot_device_available);
 713 Unlock:
 714        unlock_system_sleep();
 715        return error;
 716}
 717
 718
 719/**
 720 * software_resume - Resume from a saved hibernation image.
 721 *
 722 * This routine is called as a late initcall, when all devices have been
 723 * discovered and initialized already.
 724 *
 725 * The image reading code is called to see if there is a hibernation image
 726 * available for reading.  If that is the case, devices are quiesced and the
 727 * contents of memory is restored from the saved image.
 728 *
 729 * If this is successful, control reappears in the restored target kernel in
 730 * hibernation_snaphot() which returns to hibernate().  Otherwise, the routine
 731 * attempts to recover gracefully and make the kernel return to the normal mode
 732 * of operation.
 733 */
 734static int software_resume(void)
 735{
 736        int error;
 737        unsigned int flags;
 738
 739        /*
 740         * If the user said "noresume".. bail out early.
 741         */
 742        if (noresume || (get_securelevel() > 0) || !hibernation_available())
 743                return 0;
 744
 745        /*
 746         * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
 747         * is configured into the kernel. Since the regular hibernate
 748         * trigger path is via sysfs which takes a buffer mutex before
 749         * calling hibernate functions (which take pm_mutex) this can
 750         * cause lockdep to complain about a possible ABBA deadlock
 751         * which cannot happen since we're in the boot code here and
 752         * sysfs can't be invoked yet. Therefore, we use a subclass
 753         * here to avoid lockdep complaining.
 754         */
 755        mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
 756
 757        if (swsusp_resume_device)
 758                goto Check_image;
 759
 760        if (!strlen(resume_file)) {
 761                error = -ENOENT;
 762                goto Unlock;
 763        }
 764
 765        pr_debug("PM: Checking hibernation image partition %s\n", resume_file);
 766
 767        if (resume_delay) {
 768                printk(KERN_INFO "Waiting %dsec before reading resume device...\n",
 769                        resume_delay);
 770                ssleep(resume_delay);
 771        }
 772
 773        /* Check if the device is there */
 774        swsusp_resume_device = name_to_dev_t(resume_file);
 775
 776        /*
 777         * name_to_dev_t is ineffective to verify parition if resume_file is in
 778         * integer format. (e.g. major:minor)
 779         */
 780        if (isdigit(resume_file[0]) && resume_wait) {
 781                int partno;
 782                while (!get_gendisk(swsusp_resume_device, &partno))
 783                        msleep(10);
 784        }
 785
 786        if (!swsusp_resume_device) {
 787                /*
 788                 * Some device discovery might still be in progress; we need
 789                 * to wait for this to finish.
 790                 */
 791                wait_for_device_probe();
 792
 793                if (resume_wait) {
 794                        while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
 795                                msleep(10);
 796                        async_synchronize_full();
 797                }
 798
 799                swsusp_resume_device = name_to_dev_t(resume_file);
 800                if (!swsusp_resume_device) {
 801                        error = -ENODEV;
 802                        goto Unlock;
 803                }
 804        }
 805
 806 Check_image:
 807        pr_debug("PM: Hibernation image partition %d:%d present\n",
 808                MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
 809
 810        pr_debug("PM: Looking for hibernation image.\n");
 811        error = swsusp_check();
 812        if (error)
 813                goto Unlock;
 814
 815        /* The snapshot device should not be opened while we're running */
 816        if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
 817                error = -EBUSY;
 818                swsusp_close(FMODE_READ);
 819                goto Unlock;
 820        }
 821
 822        pm_prepare_console();
 823        error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
 824        if (error)
 825                goto Close_Finish;
 826
 827        pr_debug("PM: Preparing processes for restore.\n");
 828        error = freeze_processes();
 829        if (error)
 830                goto Close_Finish;
 831
 832        pr_debug("PM: Loading hibernation image.\n");
 833
 834        lock_device_hotplug();
 835        error = create_basic_memory_bitmaps();
 836        if (error)
 837                goto Thaw;
 838
 839        error = swsusp_read(&flags);
 840        swsusp_close(FMODE_READ);
 841        if (!error)
 842                hibernation_restore(flags & SF_PLATFORM_MODE);
 843
 844        printk(KERN_ERR "PM: Failed to load hibernation image, recovering.\n");
 845        swsusp_free();
 846        free_basic_memory_bitmaps();
 847 Thaw:
 848        unlock_device_hotplug();
 849        thaw_processes();
 850 Finish:
 851        pm_notifier_call_chain(PM_POST_RESTORE);
 852        pm_restore_console();
 853        atomic_inc(&snapshot_device_available);
 854        /* For success case, the suspend path will release the lock */
 855 Unlock:
 856        mutex_unlock(&pm_mutex);
 857        pr_debug("PM: Hibernation image not present or could not be loaded.\n");
 858        return error;
 859 Close_Finish:
 860        swsusp_close(FMODE_READ);
 861        goto Finish;
 862}
 863
 864late_initcall(software_resume);
 865
 866
 867static const char * const hibernation_modes[] = {
 868        [HIBERNATION_PLATFORM]  = "platform",
 869        [HIBERNATION_SHUTDOWN]  = "shutdown",
 870        [HIBERNATION_REBOOT]    = "reboot",
 871#ifdef CONFIG_SUSPEND
 872        [HIBERNATION_SUSPEND]   = "suspend",
 873#endif
 874};
 875
 876/*
 877 * /sys/power/disk - Control hibernation mode.
 878 *
 879 * Hibernation can be handled in several ways.  There are a few different ways
 880 * to put the system into the sleep state: using the platform driver (e.g. ACPI
 881 * or other hibernation_ops), powering it off or rebooting it (for testing
 882 * mostly).
 883 *
 884 * The sysfs file /sys/power/disk provides an interface for selecting the
 885 * hibernation mode to use.  Reading from this file causes the available modes
 886 * to be printed.  There are 3 modes that can be supported:
 887 *
 888 *      'platform'
 889 *      'shutdown'
 890 *      'reboot'
 891 *
 892 * If a platform hibernation driver is in use, 'platform' will be supported
 893 * and will be used by default.  Otherwise, 'shutdown' will be used by default.
 894 * The selected option (i.e. the one corresponding to the current value of
 895 * hibernation_mode) is enclosed by a square bracket.
 896 *
 897 * To select a given hibernation mode it is necessary to write the mode's
 898 * string representation (as returned by reading from /sys/power/disk) back
 899 * into /sys/power/disk.
 900 */
 901
 902static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
 903                         char *buf)
 904{
 905        int i;
 906        char *start = buf;
 907
 908        if (get_securelevel() > 0) {
 909                buf += sprintf(buf, "[%s]\n", "disabled");
 910                return buf-start;
 911        }
 912
 913        if (!hibernation_available())
 914                return sprintf(buf, "[disabled]\n");
 915
 916        for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
 917                if (!hibernation_modes[i])
 918                        continue;
 919                switch (i) {
 920                case HIBERNATION_SHUTDOWN:
 921                case HIBERNATION_REBOOT:
 922#ifdef CONFIG_SUSPEND
 923                case HIBERNATION_SUSPEND:
 924#endif
 925                        break;
 926                case HIBERNATION_PLATFORM:
 927                        if (hibernation_ops)
 928                                break;
 929                        /* not a valid mode, continue with loop */
 930                        continue;
 931                }
 932                if (i == hibernation_mode)
 933                        buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
 934                else
 935                        buf += sprintf(buf, "%s ", hibernation_modes[i]);
 936        }
 937        buf += sprintf(buf, "\n");
 938        return buf-start;
 939}
 940
 941static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
 942                          const char *buf, size_t n)
 943{
 944        int error = 0;
 945        int i;
 946        int len;
 947        char *p;
 948        int mode = HIBERNATION_INVALID;
 949
 950        if (get_securelevel() > 0)
 951                return -EPERM;
 952
 953        if (!hibernation_available())
 954                return -EPERM;
 955
 956        p = memchr(buf, '\n', n);
 957        len = p ? p - buf : n;
 958
 959        lock_system_sleep();
 960        for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
 961                if (len == strlen(hibernation_modes[i])
 962                    && !strncmp(buf, hibernation_modes[i], len)) {
 963                        mode = i;
 964                        break;
 965                }
 966        }
 967        if (mode != HIBERNATION_INVALID) {
 968                switch (mode) {
 969                case HIBERNATION_SHUTDOWN:
 970                case HIBERNATION_REBOOT:
 971#ifdef CONFIG_SUSPEND
 972                case HIBERNATION_SUSPEND:
 973#endif
 974                        hibernation_mode = mode;
 975                        break;
 976                case HIBERNATION_PLATFORM:
 977                        if (hibernation_ops)
 978                                hibernation_mode = mode;
 979                        else
 980                                error = -EINVAL;
 981                }
 982        } else
 983                error = -EINVAL;
 984
 985        if (!error)
 986                pr_debug("PM: Hibernation mode set to '%s'\n",
 987                         hibernation_modes[mode]);
 988        unlock_system_sleep();
 989        return error ? error : n;
 990}
 991
 992power_attr(disk);
 993
 994static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
 995                           char *buf)
 996{
 997        return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
 998                       MINOR(swsusp_resume_device));
 999}
1000
1001static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1002                            const char *buf, size_t n)
1003{
1004        unsigned int maj, min;
1005        dev_t res;
1006        int ret = -EINVAL;
1007
1008        if (sscanf(buf, "%u:%u", &maj, &min) != 2)
1009                goto out;
1010
1011        res = MKDEV(maj,min);
1012        if (maj != MAJOR(res) || min != MINOR(res))
1013                goto out;
1014
1015        lock_system_sleep();
1016        swsusp_resume_device = res;
1017        unlock_system_sleep();
1018        printk(KERN_INFO "PM: Starting manual resume from disk\n");
1019        noresume = 0;
1020        software_resume();
1021        ret = n;
1022 out:
1023        return ret;
1024}
1025
1026power_attr(resume);
1027
1028static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1029                               char *buf)
1030{
1031        return sprintf(buf, "%lu\n", image_size);
1032}
1033
1034static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1035                                const char *buf, size_t n)
1036{
1037        unsigned long size;
1038
1039        if (sscanf(buf, "%lu", &size) == 1) {
1040                image_size = size;
1041                return n;
1042        }
1043
1044        return -EINVAL;
1045}
1046
1047power_attr(image_size);
1048
1049static ssize_t reserved_size_show(struct kobject *kobj,
1050                                  struct kobj_attribute *attr, char *buf)
1051{
1052        return sprintf(buf, "%lu\n", reserved_size);
1053}
1054
1055static ssize_t reserved_size_store(struct kobject *kobj,
1056                                   struct kobj_attribute *attr,
1057                                   const char *buf, size_t n)
1058{
1059        unsigned long size;
1060
1061        if (sscanf(buf, "%lu", &size) == 1) {
1062                reserved_size = size;
1063                return n;
1064        }
1065
1066        return -EINVAL;
1067}
1068
1069power_attr(reserved_size);
1070
1071static struct attribute * g[] = {
1072        &disk_attr.attr,
1073        &resume_attr.attr,
1074        &image_size_attr.attr,
1075        &reserved_size_attr.attr,
1076        NULL,
1077};
1078
1079
1080static struct attribute_group attr_group = {
1081        .attrs = g,
1082};
1083
1084
1085static int __init pm_disk_init(void)
1086{
1087        return sysfs_create_group(power_kobj, &attr_group);
1088}
1089
1090core_initcall(pm_disk_init);
1091
1092
1093static int __init resume_setup(char *str)
1094{
1095        if (noresume)
1096                return 1;
1097
1098        strncpy( resume_file, str, 255 );
1099        return 1;
1100}
1101
1102static int __init resume_offset_setup(char *str)
1103{
1104        unsigned long long offset;
1105
1106        if (noresume)
1107                return 1;
1108
1109        if (sscanf(str, "%llu", &offset) == 1)
1110                swsusp_resume_block = offset;
1111
1112        return 1;
1113}
1114
1115static int __init hibernate_setup(char *str)
1116{
1117        if (!strncmp(str, "noresume", 8))
1118                noresume = 1;
1119        else if (!strncmp(str, "nocompress", 10))
1120                nocompress = 1;
1121        else if (!strncmp(str, "no", 2)) {
1122                noresume = 1;
1123                nohibernate = 1;
1124        }
1125        return 1;
1126}
1127
1128static int __init noresume_setup(char *str)
1129{
1130        noresume = 1;
1131        return 1;
1132}
1133
1134static int __init resumewait_setup(char *str)
1135{
1136        resume_wait = 1;
1137        return 1;
1138}
1139
1140static int __init resumedelay_setup(char *str)
1141{
1142        resume_delay = simple_strtoul(str, NULL, 0);
1143        return 1;
1144}
1145
1146static int __init nohibernate_setup(char *str)
1147{
1148        noresume = 1;
1149        nohibernate = 1;
1150        return 1;
1151}
1152
1153
1154__setup("noresume", noresume_setup);
1155__setup("resume_offset=", resume_offset_setup);
1156__setup("resume=", resume_setup);
1157__setup("hibernate=", hibernate_setup);
1158__setup("resumewait", resumewait_setup);
1159__setup("resumedelay=", resumedelay_setup);
1160__setup("nohibernate", nohibernate_setup);
1161