]> bbs.cooldavid.org Git - net-next-2.6.git/blob - kernel/power/hibernate.c
hibernation: freeze swap at hibernation
[net-next-2.6.git] / kernel / power / hibernate.c
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  *
9  * This file is released under the GPLv2.
10  */
11
12 #include <linux/suspend.h>
13 #include <linux/syscalls.h>
14 #include <linux/reboot.h>
15 #include <linux/string.h>
16 #include <linux/device.h>
17 #include <linux/kmod.h>
18 #include <linux/delay.h>
19 #include <linux/fs.h>
20 #include <linux/mount.h>
21 #include <linux/pm.h>
22 #include <linux/console.h>
23 #include <linux/cpu.h>
24 #include <linux/freezer.h>
25 #include <linux/gfp.h>
26 #include <scsi/scsi_scan.h>
27 #include <asm/suspend.h>
28
29 #include "power.h"
30
31
32 static int noresume = 0;
33 static char resume_file[256] = CONFIG_PM_STD_PARTITION;
34 dev_t swsusp_resume_device;
35 sector_t swsusp_resume_block;
36 int in_suspend __nosavedata = 0;
37
38 enum {
39         HIBERNATION_INVALID,
40         HIBERNATION_PLATFORM,
41         HIBERNATION_TEST,
42         HIBERNATION_TESTPROC,
43         HIBERNATION_SHUTDOWN,
44         HIBERNATION_REBOOT,
45         /* keep last */
46         __HIBERNATION_AFTER_LAST
47 };
48 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
49 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
50
51 static int hibernation_mode = HIBERNATION_SHUTDOWN;
52
53 static struct platform_hibernation_ops *hibernation_ops;
54
55 /**
56  * hibernation_set_ops - set the global hibernate operations
57  * @ops: the hibernation operations to use in subsequent hibernation transitions
58  */
59
60 void hibernation_set_ops(struct platform_hibernation_ops *ops)
61 {
62         if (ops && !(ops->begin && ops->end &&  ops->pre_snapshot
63             && ops->prepare && ops->finish && ops->enter && ops->pre_restore
64             && ops->restore_cleanup)) {
65                 WARN_ON(1);
66                 return;
67         }
68         mutex_lock(&pm_mutex);
69         hibernation_ops = ops;
70         if (ops)
71                 hibernation_mode = HIBERNATION_PLATFORM;
72         else if (hibernation_mode == HIBERNATION_PLATFORM)
73                 hibernation_mode = HIBERNATION_SHUTDOWN;
74
75         mutex_unlock(&pm_mutex);
76 }
77
78 static bool entering_platform_hibernation;
79
80 bool system_entering_hibernation(void)
81 {
82         return entering_platform_hibernation;
83 }
84 EXPORT_SYMBOL(system_entering_hibernation);
85
86 #ifdef CONFIG_PM_DEBUG
87 static void hibernation_debug_sleep(void)
88 {
89         printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
90         mdelay(5000);
91 }
92
93 static int hibernation_testmode(int mode)
94 {
95         if (hibernation_mode == mode) {
96                 hibernation_debug_sleep();
97                 return 1;
98         }
99         return 0;
100 }
101
102 static int hibernation_test(int level)
103 {
104         if (pm_test_level == level) {
105                 hibernation_debug_sleep();
106                 return 1;
107         }
108         return 0;
109 }
110 #else /* !CONFIG_PM_DEBUG */
111 static int hibernation_testmode(int mode) { return 0; }
112 static int hibernation_test(int level) { return 0; }
113 #endif /* !CONFIG_PM_DEBUG */
114
115 /**
116  *      platform_begin - tell the platform driver that we're starting
117  *      hibernation
118  */
119
120 static int platform_begin(int platform_mode)
121 {
122         return (platform_mode && hibernation_ops) ?
123                 hibernation_ops->begin() : 0;
124 }
125
126 /**
127  *      platform_end - tell the platform driver that we've entered the
128  *      working state
129  */
130
131 static void platform_end(int platform_mode)
132 {
133         if (platform_mode && hibernation_ops)
134                 hibernation_ops->end();
135 }
136
137 /**
138  *      platform_pre_snapshot - prepare the machine for hibernation using the
139  *      platform driver if so configured and return an error code if it fails
140  */
141
142 static int platform_pre_snapshot(int platform_mode)
143 {
144         return (platform_mode && hibernation_ops) ?
145                 hibernation_ops->pre_snapshot() : 0;
146 }
147
148 /**
149  *      platform_leave - prepare the machine for switching to the normal mode
150  *      of operation using the platform driver (called with interrupts disabled)
151  */
152
153 static void platform_leave(int platform_mode)
154 {
155         if (platform_mode && hibernation_ops)
156                 hibernation_ops->leave();
157 }
158
159 /**
160  *      platform_finish - switch the machine to the normal mode of operation
161  *      using the platform driver (must be called after platform_prepare())
162  */
163
164 static void platform_finish(int platform_mode)
165 {
166         if (platform_mode && hibernation_ops)
167                 hibernation_ops->finish();
168 }
169
170 /**
171  *      platform_pre_restore - prepare the platform for the restoration from a
172  *      hibernation image.  If the restore fails after this function has been
173  *      called, platform_restore_cleanup() must be called.
174  */
175
176 static int platform_pre_restore(int platform_mode)
177 {
178         return (platform_mode && hibernation_ops) ?
179                 hibernation_ops->pre_restore() : 0;
180 }
181
182 /**
183  *      platform_restore_cleanup - switch the platform to the normal mode of
184  *      operation after a failing restore.  If platform_pre_restore() has been
185  *      called before the failing restore, this function must be called too,
186  *      regardless of the result of platform_pre_restore().
187  */
188
189 static void platform_restore_cleanup(int platform_mode)
190 {
191         if (platform_mode && hibernation_ops)
192                 hibernation_ops->restore_cleanup();
193 }
194
195 /**
196  *      platform_recover - recover the platform from a failure to suspend
197  *      devices.
198  */
199
200 static void platform_recover(int platform_mode)
201 {
202         if (platform_mode && hibernation_ops && hibernation_ops->recover)
203                 hibernation_ops->recover();
204 }
205
206 /**
207  *      swsusp_show_speed - print the time elapsed between two events.
208  *      @start: Starting event.
209  *      @stop: Final event.
210  *      @nr_pages -     number of pages processed between @start and @stop
211  *      @msg -          introductory message to print
212  */
213
214 void swsusp_show_speed(struct timeval *start, struct timeval *stop,
215                         unsigned nr_pages, char *msg)
216 {
217         s64 elapsed_centisecs64;
218         int centisecs;
219         int k;
220         int kps;
221
222         elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
223         do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
224         centisecs = elapsed_centisecs64;
225         if (centisecs == 0)
226                 centisecs = 1;  /* avoid div-by-zero */
227         k = nr_pages * (PAGE_SIZE / 1024);
228         kps = (k * 100) / centisecs;
229         printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
230                         msg, k,
231                         centisecs / 100, centisecs % 100,
232                         kps / 1000, (kps % 1000) / 10);
233 }
234
235 /**
236  *      create_image - freeze devices that need to be frozen with interrupts
237  *      off, create the hibernation image and thaw those devices.  Control
238  *      reappears in this routine after a restore.
239  */
240
241 static int create_image(int platform_mode)
242 {
243         int error;
244
245         error = arch_prepare_suspend();
246         if (error)
247                 return error;
248
249         /* At this point, dpm_suspend_start() has been called, but *not*
250          * dpm_suspend_noirq(). We *must* call dpm_suspend_noirq() now.
251          * Otherwise, drivers for some devices (e.g. interrupt controllers)
252          * become desynchronized with the actual state of the hardware
253          * at resume time, and evil weirdness ensues.
254          */
255         error = dpm_suspend_noirq(PMSG_FREEZE);
256         if (error) {
257                 printk(KERN_ERR "PM: Some devices failed to power down, "
258                         "aborting hibernation\n");
259                 return error;
260         }
261
262         error = platform_pre_snapshot(platform_mode);
263         if (error || hibernation_test(TEST_PLATFORM))
264                 goto Platform_finish;
265
266         error = disable_nonboot_cpus();
267         if (error || hibernation_test(TEST_CPUS)
268             || hibernation_testmode(HIBERNATION_TEST))
269                 goto Enable_cpus;
270
271         local_irq_disable();
272
273         error = sysdev_suspend(PMSG_FREEZE);
274         if (error) {
275                 printk(KERN_ERR "PM: Some system devices failed to power down, "
276                         "aborting hibernation\n");
277                 goto Enable_irqs;
278         }
279
280         if (hibernation_test(TEST_CORE) || !pm_check_wakeup_events())
281                 goto Power_up;
282
283         in_suspend = 1;
284         save_processor_state();
285         error = swsusp_arch_suspend();
286         if (error)
287                 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
288                         error);
289         /* Restore control flow magically appears here */
290         restore_processor_state();
291         if (!in_suspend) {
292                 events_check_enabled = false;
293                 platform_leave(platform_mode);
294         }
295
296  Power_up:
297         sysdev_resume();
298         /* NOTE:  dpm_resume_noirq() is just a resume() for devices
299          * that suspended with irqs off ... no overall powerup.
300          */
301
302  Enable_irqs:
303         local_irq_enable();
304
305  Enable_cpus:
306         enable_nonboot_cpus();
307
308  Platform_finish:
309         platform_finish(platform_mode);
310
311         dpm_resume_noirq(in_suspend ?
312                 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
313
314         return error;
315 }
316
317 /**
318  *      hibernation_snapshot - quiesce devices and create the hibernation
319  *      snapshot image.
320  *      @platform_mode - if set, use the platform driver, if available, to
321  *                       prepare the platform firmware for the power transition.
322  *
323  *      Must be called with pm_mutex held
324  */
325
326 int hibernation_snapshot(int platform_mode)
327 {
328         int error;
329         gfp_t saved_mask;
330
331         error = platform_begin(platform_mode);
332         if (error)
333                 goto Close;
334
335         /* Preallocate image memory before shutting down devices. */
336         error = hibernate_preallocate_memory();
337         if (error)
338                 goto Close;
339
340         suspend_console();
341         hibernation_freeze_swap();
342         saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
343         error = dpm_suspend_start(PMSG_FREEZE);
344         if (error)
345                 goto Recover_platform;
346
347         if (hibernation_test(TEST_DEVICES))
348                 goto Recover_platform;
349
350         error = create_image(platform_mode);
351         /* Control returns here after successful restore */
352
353  Resume_devices:
354         /* We may need to release the preallocated image pages here. */
355         if (error || !in_suspend)
356                 swsusp_free();
357
358         dpm_resume_end(in_suspend ?
359                 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
360         set_gfp_allowed_mask(saved_mask);
361         resume_console();
362  Close:
363         platform_end(platform_mode);
364         return error;
365
366  Recover_platform:
367         platform_recover(platform_mode);
368         goto Resume_devices;
369 }
370
371 /**
372  *      resume_target_kernel - prepare devices that need to be suspended with
373  *      interrupts off, restore the contents of highmem that have not been
374  *      restored yet from the image and run the low level code that will restore
375  *      the remaining contents of memory and switch to the just restored target
376  *      kernel.
377  */
378
379 static int resume_target_kernel(bool platform_mode)
380 {
381         int error;
382
383         error = dpm_suspend_noirq(PMSG_QUIESCE);
384         if (error) {
385                 printk(KERN_ERR "PM: Some devices failed to power down, "
386                         "aborting resume\n");
387                 return error;
388         }
389
390         error = platform_pre_restore(platform_mode);
391         if (error)
392                 goto Cleanup;
393
394         error = disable_nonboot_cpus();
395         if (error)
396                 goto Enable_cpus;
397
398         local_irq_disable();
399
400         error = sysdev_suspend(PMSG_QUIESCE);
401         if (error)
402                 goto Enable_irqs;
403
404         /* We'll ignore saved state, but this gets preempt count (etc) right */
405         save_processor_state();
406         error = restore_highmem();
407         if (!error) {
408                 error = swsusp_arch_resume();
409                 /*
410                  * The code below is only ever reached in case of a failure.
411                  * Otherwise execution continues at place where
412                  * swsusp_arch_suspend() was called
413                  */
414                 BUG_ON(!error);
415                 /* This call to restore_highmem() undos the previous one */
416                 restore_highmem();
417         }
418         /*
419          * The only reason why swsusp_arch_resume() can fail is memory being
420          * very tight, so we have to free it as soon as we can to avoid
421          * subsequent failures
422          */
423         swsusp_free();
424         restore_processor_state();
425         touch_softlockup_watchdog();
426
427         sysdev_resume();
428
429  Enable_irqs:
430         local_irq_enable();
431
432  Enable_cpus:
433         enable_nonboot_cpus();
434
435  Cleanup:
436         platform_restore_cleanup(platform_mode);
437
438         dpm_resume_noirq(PMSG_RECOVER);
439
440         return error;
441 }
442
443 /**
444  *      hibernation_restore - quiesce devices and restore the hibernation
445  *      snapshot image.  If successful, control returns in hibernation_snaphot()
446  *      @platform_mode - if set, use the platform driver, if available, to
447  *                       prepare the platform firmware for the transition.
448  *
449  *      Must be called with pm_mutex held
450  */
451
452 int hibernation_restore(int platform_mode)
453 {
454         int error;
455         gfp_t saved_mask;
456
457         pm_prepare_console();
458         suspend_console();
459         saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
460         error = dpm_suspend_start(PMSG_QUIESCE);
461         if (!error) {
462                 error = resume_target_kernel(platform_mode);
463                 dpm_resume_end(PMSG_RECOVER);
464         }
465         set_gfp_allowed_mask(saved_mask);
466         resume_console();
467         pm_restore_console();
468         return error;
469 }
470
471 /**
472  *      hibernation_platform_enter - enter the hibernation state using the
473  *      platform driver (if available)
474  */
475
476 int hibernation_platform_enter(void)
477 {
478         int error;
479         gfp_t saved_mask;
480
481         if (!hibernation_ops)
482                 return -ENOSYS;
483
484         /*
485          * We have cancelled the power transition by running
486          * hibernation_ops->finish() before saving the image, so we should let
487          * the firmware know that we're going to enter the sleep state after all
488          */
489         error = hibernation_ops->begin();
490         if (error)
491                 goto Close;
492
493         entering_platform_hibernation = true;
494         suspend_console();
495         saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
496         error = dpm_suspend_start(PMSG_HIBERNATE);
497         if (error) {
498                 if (hibernation_ops->recover)
499                         hibernation_ops->recover();
500                 goto Resume_devices;
501         }
502
503         error = dpm_suspend_noirq(PMSG_HIBERNATE);
504         if (error)
505                 goto Resume_devices;
506
507         error = hibernation_ops->prepare();
508         if (error)
509                 goto Platform_finish;
510
511         error = disable_nonboot_cpus();
512         if (error)
513                 goto Platform_finish;
514
515         local_irq_disable();
516         sysdev_suspend(PMSG_HIBERNATE);
517         if (!pm_check_wakeup_events()) {
518                 error = -EAGAIN;
519                 goto Power_up;
520         }
521
522         hibernation_ops->enter();
523         /* We should never get here */
524         while (1);
525
526  Power_up:
527         sysdev_resume();
528         local_irq_enable();
529         enable_nonboot_cpus();
530
531  Platform_finish:
532         hibernation_ops->finish();
533
534         dpm_resume_noirq(PMSG_RESTORE);
535
536  Resume_devices:
537         entering_platform_hibernation = false;
538         dpm_resume_end(PMSG_RESTORE);
539         set_gfp_allowed_mask(saved_mask);
540         resume_console();
541
542  Close:
543         hibernation_ops->end();
544
545         return error;
546 }
547
548 /**
549  *      power_down - Shut the machine down for hibernation.
550  *
551  *      Use the platform driver, if configured so; otherwise try
552  *      to power off or reboot.
553  */
554
555 static void power_down(void)
556 {
557         switch (hibernation_mode) {
558         case HIBERNATION_TEST:
559         case HIBERNATION_TESTPROC:
560                 break;
561         case HIBERNATION_REBOOT:
562                 kernel_restart(NULL);
563                 break;
564         case HIBERNATION_PLATFORM:
565                 hibernation_platform_enter();
566         case HIBERNATION_SHUTDOWN:
567                 kernel_power_off();
568                 break;
569         }
570         kernel_halt();
571         /*
572          * Valid image is on the disk, if we continue we risk serious data
573          * corruption after resume.
574          */
575         printk(KERN_CRIT "PM: Please power down manually\n");
576         while(1);
577 }
578
579 static int prepare_processes(void)
580 {
581         int error = 0;
582
583         if (freeze_processes()) {
584                 error = -EBUSY;
585                 thaw_processes();
586         }
587         return error;
588 }
589
590 /**
591  *      hibernate - The granpappy of the built-in hibernation management
592  */
593
594 int hibernate(void)
595 {
596         int error;
597
598         mutex_lock(&pm_mutex);
599         /* The snapshot device should not be opened while we're running */
600         if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
601                 error = -EBUSY;
602                 goto Unlock;
603         }
604
605         pm_prepare_console();
606         error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
607         if (error)
608                 goto Exit;
609
610         error = usermodehelper_disable();
611         if (error)
612                 goto Exit;
613
614         /* Allocate memory management structures */
615         error = create_basic_memory_bitmaps();
616         if (error)
617                 goto Exit;
618
619         printk(KERN_INFO "PM: Syncing filesystems ... ");
620         sys_sync();
621         printk("done.\n");
622
623         error = prepare_processes();
624         if (error)
625                 goto Finish;
626
627         if (hibernation_test(TEST_FREEZER))
628                 goto Thaw;
629
630         if (hibernation_testmode(HIBERNATION_TESTPROC))
631                 goto Thaw;
632
633         error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
634         if (error)
635                 goto Thaw;
636
637         if (in_suspend) {
638                 unsigned int flags = 0;
639
640                 if (hibernation_mode == HIBERNATION_PLATFORM)
641                         flags |= SF_PLATFORM_MODE;
642                 pr_debug("PM: writing image.\n");
643                 error = swsusp_write(flags);
644                 swsusp_free();
645                 if (!error)
646                         power_down();
647         } else {
648                 pr_debug("PM: Image restored successfully.\n");
649         }
650
651  Thaw:
652         thaw_processes();
653  Finish:
654         free_basic_memory_bitmaps();
655         usermodehelper_enable();
656  Exit:
657         pm_notifier_call_chain(PM_POST_HIBERNATION);
658         pm_restore_console();
659         atomic_inc(&snapshot_device_available);
660  Unlock:
661         mutex_unlock(&pm_mutex);
662         return error;
663 }
664
665
666 /**
667  *      software_resume - Resume from a saved image.
668  *
669  *      Called as a late_initcall (so all devices are discovered and
670  *      initialized), we call swsusp to see if we have a saved image or not.
671  *      If so, we quiesce devices, the restore the saved image. We will
672  *      return above (in hibernate() ) if everything goes well.
673  *      Otherwise, we fail gracefully and return to the normally
674  *      scheduled program.
675  *
676  */
677
678 static int software_resume(void)
679 {
680         int error;
681         unsigned int flags;
682
683         /*
684          * If the user said "noresume".. bail out early.
685          */
686         if (noresume)
687                 return 0;
688
689         /*
690          * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
691          * is configured into the kernel. Since the regular hibernate
692          * trigger path is via sysfs which takes a buffer mutex before
693          * calling hibernate functions (which take pm_mutex) this can
694          * cause lockdep to complain about a possible ABBA deadlock
695          * which cannot happen since we're in the boot code here and
696          * sysfs can't be invoked yet. Therefore, we use a subclass
697          * here to avoid lockdep complaining.
698          */
699         mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
700
701         if (swsusp_resume_device)
702                 goto Check_image;
703
704         if (!strlen(resume_file)) {
705                 error = -ENOENT;
706                 goto Unlock;
707         }
708
709         pr_debug("PM: Checking image partition %s\n", resume_file);
710
711         /* Check if the device is there */
712         swsusp_resume_device = name_to_dev_t(resume_file);
713         if (!swsusp_resume_device) {
714                 /*
715                  * Some device discovery might still be in progress; we need
716                  * to wait for this to finish.
717                  */
718                 wait_for_device_probe();
719                 /*
720                  * We can't depend on SCSI devices being available after loading
721                  * one of their modules until scsi_complete_async_scans() is
722                  * called and the resume device usually is a SCSI one.
723                  */
724                 scsi_complete_async_scans();
725
726                 swsusp_resume_device = name_to_dev_t(resume_file);
727                 if (!swsusp_resume_device) {
728                         error = -ENODEV;
729                         goto Unlock;
730                 }
731         }
732
733  Check_image:
734         pr_debug("PM: Resume from partition %d:%d\n",
735                 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
736
737         pr_debug("PM: Checking hibernation image.\n");
738         error = swsusp_check();
739         if (error)
740                 goto Unlock;
741
742         /* The snapshot device should not be opened while we're running */
743         if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
744                 error = -EBUSY;
745                 swsusp_close(FMODE_READ);
746                 goto Unlock;
747         }
748
749         pm_prepare_console();
750         error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
751         if (error)
752                 goto close_finish;
753
754         error = usermodehelper_disable();
755         if (error)
756                 goto close_finish;
757
758         error = create_basic_memory_bitmaps();
759         if (error)
760                 goto close_finish;
761
762         pr_debug("PM: Preparing processes for restore.\n");
763         error = prepare_processes();
764         if (error) {
765                 swsusp_close(FMODE_READ);
766                 goto Done;
767         }
768
769         pr_debug("PM: Reading hibernation image.\n");
770
771         error = swsusp_read(&flags);
772         swsusp_close(FMODE_READ);
773         if (!error)
774                 hibernation_restore(flags & SF_PLATFORM_MODE);
775
776         printk(KERN_ERR "PM: Restore failed, recovering.\n");
777         swsusp_free();
778         thaw_processes();
779  Done:
780         free_basic_memory_bitmaps();
781         usermodehelper_enable();
782  Finish:
783         pm_notifier_call_chain(PM_POST_RESTORE);
784         pm_restore_console();
785         atomic_inc(&snapshot_device_available);
786         /* For success case, the suspend path will release the lock */
787  Unlock:
788         mutex_unlock(&pm_mutex);
789         pr_debug("PM: Resume from disk failed.\n");
790         return error;
791 close_finish:
792         swsusp_close(FMODE_READ);
793         goto Finish;
794 }
795
796 late_initcall(software_resume);
797
798
799 static const char * const hibernation_modes[] = {
800         [HIBERNATION_PLATFORM]  = "platform",
801         [HIBERNATION_SHUTDOWN]  = "shutdown",
802         [HIBERNATION_REBOOT]    = "reboot",
803         [HIBERNATION_TEST]      = "test",
804         [HIBERNATION_TESTPROC]  = "testproc",
805 };
806
807 /**
808  *      disk - Control hibernation mode
809  *
810  *      Suspend-to-disk can be handled in several ways. We have a few options
811  *      for putting the system to sleep - using the platform driver (e.g. ACPI
812  *      or other hibernation_ops), powering off the system or rebooting the
813  *      system (for testing) as well as the two test modes.
814  *
815  *      The system can support 'platform', and that is known a priori (and
816  *      encoded by the presence of hibernation_ops). However, the user may
817  *      choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
818  *      test modes, 'test' or 'testproc'.
819  *
820  *      show() will display what the mode is currently set to.
821  *      store() will accept one of
822  *
823  *      'platform'
824  *      'shutdown'
825  *      'reboot'
826  *      'test'
827  *      'testproc'
828  *
829  *      It will only change to 'platform' if the system
830  *      supports it (as determined by having hibernation_ops).
831  */
832
833 static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
834                          char *buf)
835 {
836         int i;
837         char *start = buf;
838
839         for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
840                 if (!hibernation_modes[i])
841                         continue;
842                 switch (i) {
843                 case HIBERNATION_SHUTDOWN:
844                 case HIBERNATION_REBOOT:
845                 case HIBERNATION_TEST:
846                 case HIBERNATION_TESTPROC:
847                         break;
848                 case HIBERNATION_PLATFORM:
849                         if (hibernation_ops)
850                                 break;
851                         /* not a valid mode, continue with loop */
852                         continue;
853                 }
854                 if (i == hibernation_mode)
855                         buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
856                 else
857                         buf += sprintf(buf, "%s ", hibernation_modes[i]);
858         }
859         buf += sprintf(buf, "\n");
860         return buf-start;
861 }
862
863
864 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
865                           const char *buf, size_t n)
866 {
867         int error = 0;
868         int i;
869         int len;
870         char *p;
871         int mode = HIBERNATION_INVALID;
872
873         p = memchr(buf, '\n', n);
874         len = p ? p - buf : n;
875
876         mutex_lock(&pm_mutex);
877         for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
878                 if (len == strlen(hibernation_modes[i])
879                     && !strncmp(buf, hibernation_modes[i], len)) {
880                         mode = i;
881                         break;
882                 }
883         }
884         if (mode != HIBERNATION_INVALID) {
885                 switch (mode) {
886                 case HIBERNATION_SHUTDOWN:
887                 case HIBERNATION_REBOOT:
888                 case HIBERNATION_TEST:
889                 case HIBERNATION_TESTPROC:
890                         hibernation_mode = mode;
891                         break;
892                 case HIBERNATION_PLATFORM:
893                         if (hibernation_ops)
894                                 hibernation_mode = mode;
895                         else
896                                 error = -EINVAL;
897                 }
898         } else
899                 error = -EINVAL;
900
901         if (!error)
902                 pr_debug("PM: Hibernation mode set to '%s'\n",
903                          hibernation_modes[mode]);
904         mutex_unlock(&pm_mutex);
905         return error ? error : n;
906 }
907
908 power_attr(disk);
909
910 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
911                            char *buf)
912 {
913         return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
914                        MINOR(swsusp_resume_device));
915 }
916
917 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
918                             const char *buf, size_t n)
919 {
920         unsigned int maj, min;
921         dev_t res;
922         int ret = -EINVAL;
923
924         if (sscanf(buf, "%u:%u", &maj, &min) != 2)
925                 goto out;
926
927         res = MKDEV(maj,min);
928         if (maj != MAJOR(res) || min != MINOR(res))
929                 goto out;
930
931         mutex_lock(&pm_mutex);
932         swsusp_resume_device = res;
933         mutex_unlock(&pm_mutex);
934         printk(KERN_INFO "PM: Starting manual resume from disk\n");
935         noresume = 0;
936         software_resume();
937         ret = n;
938  out:
939         return ret;
940 }
941
942 power_attr(resume);
943
944 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
945                                char *buf)
946 {
947         return sprintf(buf, "%lu\n", image_size);
948 }
949
950 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
951                                 const char *buf, size_t n)
952 {
953         unsigned long size;
954
955         if (sscanf(buf, "%lu", &size) == 1) {
956                 image_size = size;
957                 return n;
958         }
959
960         return -EINVAL;
961 }
962
963 power_attr(image_size);
964
965 static struct attribute * g[] = {
966         &disk_attr.attr,
967         &resume_attr.attr,
968         &image_size_attr.attr,
969         NULL,
970 };
971
972
973 static struct attribute_group attr_group = {
974         .attrs = g,
975 };
976
977
978 static int __init pm_disk_init(void)
979 {
980         return sysfs_create_group(power_kobj, &attr_group);
981 }
982
983 core_initcall(pm_disk_init);
984
985
986 static int __init resume_setup(char *str)
987 {
988         if (noresume)
989                 return 1;
990
991         strncpy( resume_file, str, 255 );
992         return 1;
993 }
994
995 static int __init resume_offset_setup(char *str)
996 {
997         unsigned long long offset;
998
999         if (noresume)
1000                 return 1;
1001
1002         if (sscanf(str, "%llu", &offset) == 1)
1003                 swsusp_resume_block = offset;
1004
1005         return 1;
1006 }
1007
1008 static int __init noresume_setup(char *str)
1009 {
1010         noresume = 1;
1011         return 1;
1012 }
1013
1014 __setup("noresume", noresume_setup);
1015 __setup("resume_offset=", resume_offset_setup);
1016 __setup("resume=", resume_setup);