GFS2: fix d_splice_alias() misuses
[deliverable/linux.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 * 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 <trace/events/power.h>
32
33 #include "power.h"
34
35
36 static int nocompress;
37 static int noresume;
38 static int nohibernate;
39 static int resume_wait;
40 static unsigned int resume_delay;
41 static char resume_file[256] = CONFIG_PM_STD_PARTITION;
42 dev_t swsusp_resume_device;
43 sector_t swsusp_resume_block;
44 __visible int in_suspend __nosavedata;
45
46 enum {
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
60 static int hibernation_mode = HIBERNATION_SHUTDOWN;
61
62 bool freezer_test_done;
63
64 static const struct platform_hibernation_ops *hibernation_ops;
65
66 bool 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 */
75 void 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 EXPORT_SYMBOL_GPL(hibernation_set_ops);
93
94 static bool entering_platform_hibernation;
95
96 bool system_entering_hibernation(void)
97 {
98 return entering_platform_hibernation;
99 }
100 EXPORT_SYMBOL(system_entering_hibernation);
101
102 #ifdef CONFIG_PM_DEBUG
103 static void hibernation_debug_sleep(void)
104 {
105 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
106 mdelay(5000);
107 }
108
109 static int hibernation_test(int level)
110 {
111 if (pm_test_level == level) {
112 hibernation_debug_sleep();
113 return 1;
114 }
115 return 0;
116 }
117 #else /* !CONFIG_PM_DEBUG */
118 static int hibernation_test(int level) { return 0; }
119 #endif /* !CONFIG_PM_DEBUG */
120
121 /**
122 * platform_begin - Call platform to start hibernation.
123 * @platform_mode: Whether or not to use the platform driver.
124 */
125 static int platform_begin(int platform_mode)
126 {
127 return (platform_mode && hibernation_ops) ?
128 hibernation_ops->begin() : 0;
129 }
130
131 /**
132 * platform_end - Call platform to finish transition to the working state.
133 * @platform_mode: Whether or not to use the platform driver.
134 */
135 static void platform_end(int platform_mode)
136 {
137 if (platform_mode && hibernation_ops)
138 hibernation_ops->end();
139 }
140
141 /**
142 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
143 * @platform_mode: Whether or not to use the platform driver.
144 *
145 * Use the platform driver to prepare the system for creating a hibernate image,
146 * if so configured, and return an error code if that fails.
147 */
148
149 static int platform_pre_snapshot(int platform_mode)
150 {
151 return (platform_mode && hibernation_ops) ?
152 hibernation_ops->pre_snapshot() : 0;
153 }
154
155 /**
156 * platform_leave - Call platform to prepare a transition to the working state.
157 * @platform_mode: Whether or not to use the platform driver.
158 *
159 * Use the platform driver prepare to prepare the machine for switching to the
160 * normal mode of operation.
161 *
162 * This routine is called on one CPU with interrupts disabled.
163 */
164 static void platform_leave(int platform_mode)
165 {
166 if (platform_mode && hibernation_ops)
167 hibernation_ops->leave();
168 }
169
170 /**
171 * platform_finish - Call platform to switch the system to the working state.
172 * @platform_mode: Whether or not to use the platform driver.
173 *
174 * Use the platform driver to switch the machine to the normal mode of
175 * operation.
176 *
177 * This routine must be called after platform_prepare().
178 */
179 static void platform_finish(int platform_mode)
180 {
181 if (platform_mode && hibernation_ops)
182 hibernation_ops->finish();
183 }
184
185 /**
186 * platform_pre_restore - Prepare for hibernate image restoration.
187 * @platform_mode: Whether or not to use the platform driver.
188 *
189 * Use the platform driver to prepare the system for resume from a hibernation
190 * image.
191 *
192 * If the restore fails after this function has been called,
193 * platform_restore_cleanup() must be called.
194 */
195 static int platform_pre_restore(int platform_mode)
196 {
197 return (platform_mode && hibernation_ops) ?
198 hibernation_ops->pre_restore() : 0;
199 }
200
201 /**
202 * platform_restore_cleanup - Switch to the working state after failing restore.
203 * @platform_mode: Whether or not to use the platform driver.
204 *
205 * Use the platform driver to switch the system to the normal mode of operation
206 * after a failing restore.
207 *
208 * If platform_pre_restore() has been called before the failing restore, this
209 * function must be called too, regardless of the result of
210 * platform_pre_restore().
211 */
212 static void platform_restore_cleanup(int platform_mode)
213 {
214 if (platform_mode && hibernation_ops)
215 hibernation_ops->restore_cleanup();
216 }
217
218 /**
219 * platform_recover - Recover from a failure to suspend devices.
220 * @platform_mode: Whether or not to use the platform driver.
221 */
222 static void platform_recover(int platform_mode)
223 {
224 if (platform_mode && hibernation_ops && hibernation_ops->recover)
225 hibernation_ops->recover();
226 }
227
228 /**
229 * swsusp_show_speed - Print time elapsed between two events during hibernation.
230 * @start: Starting event.
231 * @stop: Final event.
232 * @nr_pages: Number of memory pages processed between @start and @stop.
233 * @msg: Additional diagnostic message to print.
234 */
235 void swsusp_show_speed(struct timeval *start, struct timeval *stop,
236 unsigned nr_pages, char *msg)
237 {
238 u64 elapsed_centisecs64;
239 unsigned int centisecs;
240 unsigned int k;
241 unsigned int kps;
242
243 elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
244 /*
245 * If "(s64)elapsed_centisecs64 < 0", it will print long elapsed time,
246 * it is obvious enough for what went wrong.
247 */
248 do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
249 centisecs = elapsed_centisecs64;
250 if (centisecs == 0)
251 centisecs = 1; /* avoid div-by-zero */
252 k = nr_pages * (PAGE_SIZE / 1024);
253 kps = (k * 100) / centisecs;
254 printk(KERN_INFO "PM: %s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n",
255 msg, k,
256 centisecs / 100, centisecs % 100,
257 kps / 1000, (kps % 1000) / 10);
258 }
259
260 /**
261 * create_image - Create a hibernation image.
262 * @platform_mode: Whether or not to use the platform driver.
263 *
264 * Execute device drivers' "late" and "noirq" freeze callbacks, create a
265 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
266 *
267 * Control reappears in this routine after the subsequent restore.
268 */
269 static int create_image(int platform_mode)
270 {
271 int error;
272
273 error = dpm_suspend_end(PMSG_FREEZE);
274 if (error) {
275 printk(KERN_ERR "PM: Some devices failed to power down, "
276 "aborting hibernation\n");
277 return error;
278 }
279
280 error = platform_pre_snapshot(platform_mode);
281 if (error || hibernation_test(TEST_PLATFORM))
282 goto Platform_finish;
283
284 error = disable_nonboot_cpus();
285 if (error || hibernation_test(TEST_CPUS))
286 goto Enable_cpus;
287
288 local_irq_disable();
289
290 error = syscore_suspend();
291 if (error) {
292 printk(KERN_ERR "PM: Some system devices failed to power down, "
293 "aborting hibernation\n");
294 goto Enable_irqs;
295 }
296
297 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
298 goto Power_up;
299
300 in_suspend = 1;
301 save_processor_state();
302 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true);
303 error = swsusp_arch_suspend();
304 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false);
305 if (error)
306 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
307 error);
308 /* Restore control flow magically appears here */
309 restore_processor_state();
310 if (!in_suspend)
311 events_check_enabled = false;
312
313 platform_leave(platform_mode);
314
315 Power_up:
316 syscore_resume();
317
318 Enable_irqs:
319 local_irq_enable();
320
321 Enable_cpus:
322 enable_nonboot_cpus();
323
324 Platform_finish:
325 platform_finish(platform_mode);
326
327 dpm_resume_start(in_suspend ?
328 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
329
330 return error;
331 }
332
333 /**
334 * hibernation_snapshot - Quiesce devices and create a hibernation image.
335 * @platform_mode: If set, use platform driver to prepare for the transition.
336 *
337 * This routine must be called with pm_mutex held.
338 */
339 int hibernation_snapshot(int platform_mode)
340 {
341 pm_message_t msg;
342 int error;
343
344 error = platform_begin(platform_mode);
345 if (error)
346 goto Close;
347
348 /* Preallocate image memory before shutting down devices. */
349 error = hibernate_preallocate_memory();
350 if (error)
351 goto Close;
352
353 error = freeze_kernel_threads();
354 if (error)
355 goto Cleanup;
356
357 if (hibernation_test(TEST_FREEZER)) {
358
359 /*
360 * Indicate to the caller that we are returning due to a
361 * successful freezer test.
362 */
363 freezer_test_done = true;
364 goto Thaw;
365 }
366
367 error = dpm_prepare(PMSG_FREEZE);
368 if (error) {
369 dpm_complete(PMSG_RECOVER);
370 goto Thaw;
371 }
372
373 suspend_console();
374 pm_restrict_gfp_mask();
375
376 error = dpm_suspend(PMSG_FREEZE);
377
378 if (error || hibernation_test(TEST_DEVICES))
379 platform_recover(platform_mode);
380 else
381 error = create_image(platform_mode);
382
383 /*
384 * In the case that we call create_image() above, the control
385 * returns here (1) after the image has been created or the
386 * image creation has failed and (2) after a successful restore.
387 */
388
389 /* We may need to release the preallocated image pages here. */
390 if (error || !in_suspend)
391 swsusp_free();
392
393 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
394 dpm_resume(msg);
395
396 if (error || !in_suspend)
397 pm_restore_gfp_mask();
398
399 resume_console();
400 dpm_complete(msg);
401
402 Close:
403 platform_end(platform_mode);
404 return error;
405
406 Thaw:
407 thaw_kernel_threads();
408 Cleanup:
409 swsusp_free();
410 goto Close;
411 }
412
413 /**
414 * resume_target_kernel - Restore system state from a hibernation image.
415 * @platform_mode: Whether or not to use the platform driver.
416 *
417 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
418 * contents of highmem that have not been restored yet from the image and run
419 * the low-level code that will restore the remaining contents of memory and
420 * switch to the just restored target kernel.
421 */
422 static int resume_target_kernel(bool platform_mode)
423 {
424 int error;
425
426 error = dpm_suspend_end(PMSG_QUIESCE);
427 if (error) {
428 printk(KERN_ERR "PM: Some devices failed to power down, "
429 "aborting resume\n");
430 return error;
431 }
432
433 error = platform_pre_restore(platform_mode);
434 if (error)
435 goto Cleanup;
436
437 error = disable_nonboot_cpus();
438 if (error)
439 goto Enable_cpus;
440
441 local_irq_disable();
442
443 error = syscore_suspend();
444 if (error)
445 goto Enable_irqs;
446
447 save_processor_state();
448 error = restore_highmem();
449 if (!error) {
450 error = swsusp_arch_resume();
451 /*
452 * The code below is only ever reached in case of a failure.
453 * Otherwise, execution continues at the place where
454 * swsusp_arch_suspend() was called.
455 */
456 BUG_ON(!error);
457 /*
458 * This call to restore_highmem() reverts the changes made by
459 * the previous one.
460 */
461 restore_highmem();
462 }
463 /*
464 * The only reason why swsusp_arch_resume() can fail is memory being
465 * very tight, so we have to free it as soon as we can to avoid
466 * subsequent failures.
467 */
468 swsusp_free();
469 restore_processor_state();
470 touch_softlockup_watchdog();
471
472 syscore_resume();
473
474 Enable_irqs:
475 local_irq_enable();
476
477 Enable_cpus:
478 enable_nonboot_cpus();
479
480 Cleanup:
481 platform_restore_cleanup(platform_mode);
482
483 dpm_resume_start(PMSG_RECOVER);
484
485 return error;
486 }
487
488 /**
489 * hibernation_restore - Quiesce devices and restore from a hibernation image.
490 * @platform_mode: If set, use platform driver to prepare for the transition.
491 *
492 * This routine must be called with pm_mutex held. If it is successful, control
493 * reappears in the restored target kernel in hibernation_snapshot().
494 */
495 int hibernation_restore(int platform_mode)
496 {
497 int error;
498
499 pm_prepare_console();
500 suspend_console();
501 pm_restrict_gfp_mask();
502 error = dpm_suspend_start(PMSG_QUIESCE);
503 if (!error) {
504 error = resume_target_kernel(platform_mode);
505 dpm_resume_end(PMSG_RECOVER);
506 }
507 pm_restore_gfp_mask();
508 resume_console();
509 pm_restore_console();
510 return error;
511 }
512
513 /**
514 * hibernation_platform_enter - Power off the system using the platform driver.
515 */
516 int hibernation_platform_enter(void)
517 {
518 int error;
519
520 if (!hibernation_ops)
521 return -ENOSYS;
522
523 /*
524 * We have cancelled the power transition by running
525 * hibernation_ops->finish() before saving the image, so we should let
526 * the firmware know that we're going to enter the sleep state after all
527 */
528 error = hibernation_ops->begin();
529 if (error)
530 goto Close;
531
532 entering_platform_hibernation = true;
533 suspend_console();
534 error = dpm_suspend_start(PMSG_HIBERNATE);
535 if (error) {
536 if (hibernation_ops->recover)
537 hibernation_ops->recover();
538 goto Resume_devices;
539 }
540
541 error = dpm_suspend_end(PMSG_HIBERNATE);
542 if (error)
543 goto Resume_devices;
544
545 error = hibernation_ops->prepare();
546 if (error)
547 goto Platform_finish;
548
549 error = disable_nonboot_cpus();
550 if (error)
551 goto Platform_finish;
552
553 local_irq_disable();
554 syscore_suspend();
555 if (pm_wakeup_pending()) {
556 error = -EAGAIN;
557 goto Power_up;
558 }
559
560 hibernation_ops->enter();
561 /* We should never get here */
562 while (1);
563
564 Power_up:
565 syscore_resume();
566 local_irq_enable();
567 enable_nonboot_cpus();
568
569 Platform_finish:
570 hibernation_ops->finish();
571
572 dpm_resume_start(PMSG_RESTORE);
573
574 Resume_devices:
575 entering_platform_hibernation = false;
576 dpm_resume_end(PMSG_RESTORE);
577 resume_console();
578
579 Close:
580 hibernation_ops->end();
581
582 return error;
583 }
584
585 /**
586 * power_down - Shut the machine down for hibernation.
587 *
588 * Use the platform driver, if configured, to put the system into the sleep
589 * state corresponding to hibernation, or try to power it off or reboot,
590 * depending on the value of hibernation_mode.
591 */
592 static void power_down(void)
593 {
594 #ifdef CONFIG_SUSPEND
595 int error;
596 #endif
597
598 switch (hibernation_mode) {
599 case HIBERNATION_REBOOT:
600 kernel_restart(NULL);
601 break;
602 case HIBERNATION_PLATFORM:
603 hibernation_platform_enter();
604 case HIBERNATION_SHUTDOWN:
605 if (pm_power_off)
606 kernel_power_off();
607 break;
608 #ifdef CONFIG_SUSPEND
609 case HIBERNATION_SUSPEND:
610 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
611 if (error) {
612 if (hibernation_ops)
613 hibernation_mode = HIBERNATION_PLATFORM;
614 else
615 hibernation_mode = HIBERNATION_SHUTDOWN;
616 power_down();
617 }
618 /*
619 * Restore swap signature.
620 */
621 error = swsusp_unmark();
622 if (error)
623 printk(KERN_ERR "PM: Swap will be unusable! "
624 "Try swapon -a.\n");
625 return;
626 #endif
627 }
628 kernel_halt();
629 /*
630 * Valid image is on the disk, if we continue we risk serious data
631 * corruption after resume.
632 */
633 printk(KERN_CRIT "PM: Please power down manually\n");
634 while (1)
635 cpu_relax();
636 }
637
638 /**
639 * hibernate - Carry out system hibernation, including saving the image.
640 */
641 int hibernate(void)
642 {
643 int error;
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 */
734 static 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 || !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
864 late_initcall_sync(software_resume);
865
866
867 static 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
902 static 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 (!hibernation_available())
909 return sprintf(buf, "[disabled]\n");
910
911 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
912 if (!hibernation_modes[i])
913 continue;
914 switch (i) {
915 case HIBERNATION_SHUTDOWN:
916 case HIBERNATION_REBOOT:
917 #ifdef CONFIG_SUSPEND
918 case HIBERNATION_SUSPEND:
919 #endif
920 break;
921 case HIBERNATION_PLATFORM:
922 if (hibernation_ops)
923 break;
924 /* not a valid mode, continue with loop */
925 continue;
926 }
927 if (i == hibernation_mode)
928 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
929 else
930 buf += sprintf(buf, "%s ", hibernation_modes[i]);
931 }
932 buf += sprintf(buf, "\n");
933 return buf-start;
934 }
935
936 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
937 const char *buf, size_t n)
938 {
939 int error = 0;
940 int i;
941 int len;
942 char *p;
943 int mode = HIBERNATION_INVALID;
944
945 if (!hibernation_available())
946 return -EPERM;
947
948 p = memchr(buf, '\n', n);
949 len = p ? p - buf : n;
950
951 lock_system_sleep();
952 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
953 if (len == strlen(hibernation_modes[i])
954 && !strncmp(buf, hibernation_modes[i], len)) {
955 mode = i;
956 break;
957 }
958 }
959 if (mode != HIBERNATION_INVALID) {
960 switch (mode) {
961 case HIBERNATION_SHUTDOWN:
962 case HIBERNATION_REBOOT:
963 #ifdef CONFIG_SUSPEND
964 case HIBERNATION_SUSPEND:
965 #endif
966 hibernation_mode = mode;
967 break;
968 case HIBERNATION_PLATFORM:
969 if (hibernation_ops)
970 hibernation_mode = mode;
971 else
972 error = -EINVAL;
973 }
974 } else
975 error = -EINVAL;
976
977 if (!error)
978 pr_debug("PM: Hibernation mode set to '%s'\n",
979 hibernation_modes[mode]);
980 unlock_system_sleep();
981 return error ? error : n;
982 }
983
984 power_attr(disk);
985
986 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
987 char *buf)
988 {
989 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
990 MINOR(swsusp_resume_device));
991 }
992
993 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
994 const char *buf, size_t n)
995 {
996 dev_t res;
997 int len = n;
998 char *name;
999
1000 if (len && buf[len-1] == '\n')
1001 len--;
1002 name = kstrndup(buf, len, GFP_KERNEL);
1003 if (!name)
1004 return -ENOMEM;
1005
1006 res = name_to_dev_t(name);
1007 kfree(name);
1008 if (!res)
1009 return -EINVAL;
1010
1011 lock_system_sleep();
1012 swsusp_resume_device = res;
1013 unlock_system_sleep();
1014 printk(KERN_INFO "PM: Starting manual resume from disk\n");
1015 noresume = 0;
1016 software_resume();
1017 return n;
1018 }
1019
1020 power_attr(resume);
1021
1022 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1023 char *buf)
1024 {
1025 return sprintf(buf, "%lu\n", image_size);
1026 }
1027
1028 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1029 const char *buf, size_t n)
1030 {
1031 unsigned long size;
1032
1033 if (sscanf(buf, "%lu", &size) == 1) {
1034 image_size = size;
1035 return n;
1036 }
1037
1038 return -EINVAL;
1039 }
1040
1041 power_attr(image_size);
1042
1043 static ssize_t reserved_size_show(struct kobject *kobj,
1044 struct kobj_attribute *attr, char *buf)
1045 {
1046 return sprintf(buf, "%lu\n", reserved_size);
1047 }
1048
1049 static ssize_t reserved_size_store(struct kobject *kobj,
1050 struct kobj_attribute *attr,
1051 const char *buf, size_t n)
1052 {
1053 unsigned long size;
1054
1055 if (sscanf(buf, "%lu", &size) == 1) {
1056 reserved_size = size;
1057 return n;
1058 }
1059
1060 return -EINVAL;
1061 }
1062
1063 power_attr(reserved_size);
1064
1065 static struct attribute * g[] = {
1066 &disk_attr.attr,
1067 &resume_attr.attr,
1068 &image_size_attr.attr,
1069 &reserved_size_attr.attr,
1070 NULL,
1071 };
1072
1073
1074 static struct attribute_group attr_group = {
1075 .attrs = g,
1076 };
1077
1078
1079 static int __init pm_disk_init(void)
1080 {
1081 return sysfs_create_group(power_kobj, &attr_group);
1082 }
1083
1084 core_initcall(pm_disk_init);
1085
1086
1087 static int __init resume_setup(char *str)
1088 {
1089 if (noresume)
1090 return 1;
1091
1092 strncpy( resume_file, str, 255 );
1093 return 1;
1094 }
1095
1096 static int __init resume_offset_setup(char *str)
1097 {
1098 unsigned long long offset;
1099
1100 if (noresume)
1101 return 1;
1102
1103 if (sscanf(str, "%llu", &offset) == 1)
1104 swsusp_resume_block = offset;
1105
1106 return 1;
1107 }
1108
1109 static int __init hibernate_setup(char *str)
1110 {
1111 if (!strncmp(str, "noresume", 8))
1112 noresume = 1;
1113 else if (!strncmp(str, "nocompress", 10))
1114 nocompress = 1;
1115 else if (!strncmp(str, "no", 2)) {
1116 noresume = 1;
1117 nohibernate = 1;
1118 }
1119 return 1;
1120 }
1121
1122 static int __init noresume_setup(char *str)
1123 {
1124 noresume = 1;
1125 return 1;
1126 }
1127
1128 static int __init resumewait_setup(char *str)
1129 {
1130 resume_wait = 1;
1131 return 1;
1132 }
1133
1134 static int __init resumedelay_setup(char *str)
1135 {
1136 int rc = kstrtouint(str, 0, &resume_delay);
1137
1138 if (rc)
1139 return rc;
1140 return 1;
1141 }
1142
1143 static int __init nohibernate_setup(char *str)
1144 {
1145 noresume = 1;
1146 nohibernate = 1;
1147 return 1;
1148 }
1149
1150 static int __init kaslr_nohibernate_setup(char *str)
1151 {
1152 return nohibernate_setup(str);
1153 }
1154
1155 __setup("noresume", noresume_setup);
1156 __setup("resume_offset=", resume_offset_setup);
1157 __setup("resume=", resume_setup);
1158 __setup("hibernate=", hibernate_setup);
1159 __setup("resumewait", resumewait_setup);
1160 __setup("resumedelay=", resumedelay_setup);
1161 __setup("nohibernate", nohibernate_setup);
1162 __setup("kaslr", kaslr_nohibernate_setup);
This page took 0.054382 seconds and 5 git commands to generate.