Avoid beyond bounds copy while caching ACL
[zen-stable.git] / kernel / power / hibernate.c
bloba364144ad336fb5cf55bcb6f78dbb1e718080637
1 /*
2 * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support.
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.
9 * This file is released under the GPLv2.
12 #include <linux/export.h>
13 #include <linux/suspend.h>
14 #include <linux/syscalls.h>
15 #include <linux/reboot.h>
16 #include <linux/string.h>
17 #include <linux/device.h>
18 #include <linux/async.h>
19 #include <linux/delay.h>
20 #include <linux/fs.h>
21 #include <linux/mount.h>
22 #include <linux/pm.h>
23 #include <linux/console.h>
24 #include <linux/cpu.h>
25 #include <linux/freezer.h>
26 #include <linux/gfp.h>
27 #include <linux/syscore_ops.h>
28 #include <scsi/scsi_scan.h>
30 #include "power.h"
33 static int nocompress;
34 static int noresume;
35 static int resume_wait;
36 static int resume_delay;
37 static char resume_file[256] = CONFIG_PM_STD_PARTITION;
38 dev_t swsusp_resume_device;
39 sector_t swsusp_resume_block;
40 int in_suspend __nosavedata;
42 enum {
43 HIBERNATION_INVALID,
44 HIBERNATION_PLATFORM,
45 HIBERNATION_SHUTDOWN,
46 HIBERNATION_REBOOT,
47 /* keep last */
48 __HIBERNATION_AFTER_LAST
50 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
51 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
53 static int hibernation_mode = HIBERNATION_SHUTDOWN;
55 bool freezer_test_done;
57 static const struct platform_hibernation_ops *hibernation_ops;
59 /**
60 * hibernation_set_ops - Set the global hibernate operations.
61 * @ops: Hibernation operations to use in subsequent hibernation transitions.
63 void hibernation_set_ops(const struct platform_hibernation_ops *ops)
65 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
66 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
67 && ops->restore_cleanup && ops->leave)) {
68 WARN_ON(1);
69 return;
71 lock_system_sleep();
72 hibernation_ops = ops;
73 if (ops)
74 hibernation_mode = HIBERNATION_PLATFORM;
75 else if (hibernation_mode == HIBERNATION_PLATFORM)
76 hibernation_mode = HIBERNATION_SHUTDOWN;
78 unlock_system_sleep();
81 static bool entering_platform_hibernation;
83 bool system_entering_hibernation(void)
85 return entering_platform_hibernation;
87 EXPORT_SYMBOL(system_entering_hibernation);
89 #ifdef CONFIG_PM_DEBUG
90 static void hibernation_debug_sleep(void)
92 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
93 mdelay(5000);
96 static int hibernation_test(int level)
98 if (pm_test_level == level) {
99 hibernation_debug_sleep();
100 return 1;
102 return 0;
104 #else /* !CONFIG_PM_DEBUG */
105 static int hibernation_test(int level) { return 0; }
106 #endif /* !CONFIG_PM_DEBUG */
109 * platform_begin - Call platform to start hibernation.
110 * @platform_mode: Whether or not to use the platform driver.
112 static int platform_begin(int platform_mode)
114 return (platform_mode && hibernation_ops) ?
115 hibernation_ops->begin() : 0;
119 * platform_end - Call platform to finish transition to the working state.
120 * @platform_mode: Whether or not to use the platform driver.
122 static void platform_end(int platform_mode)
124 if (platform_mode && hibernation_ops)
125 hibernation_ops->end();
129 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
130 * @platform_mode: Whether or not to use the platform driver.
132 * Use the platform driver to prepare the system for creating a hibernate image,
133 * if so configured, and return an error code if that fails.
136 static int platform_pre_snapshot(int platform_mode)
138 return (platform_mode && hibernation_ops) ?
139 hibernation_ops->pre_snapshot() : 0;
143 * platform_leave - Call platform to prepare a transition to the working state.
144 * @platform_mode: Whether or not to use the platform driver.
146 * Use the platform driver prepare to prepare the machine for switching to the
147 * normal mode of operation.
149 * This routine is called on one CPU with interrupts disabled.
151 static void platform_leave(int platform_mode)
153 if (platform_mode && hibernation_ops)
154 hibernation_ops->leave();
158 * platform_finish - Call platform to switch the system to the working state.
159 * @platform_mode: Whether or not to use the platform driver.
161 * Use the platform driver to switch the machine to the normal mode of
162 * operation.
164 * This routine must be called after platform_prepare().
166 static void platform_finish(int platform_mode)
168 if (platform_mode && hibernation_ops)
169 hibernation_ops->finish();
173 * platform_pre_restore - Prepare for hibernate image restoration.
174 * @platform_mode: Whether or not to use the platform driver.
176 * Use the platform driver to prepare the system for resume from a hibernation
177 * image.
179 * If the restore fails after this function has been called,
180 * platform_restore_cleanup() must be called.
182 static int platform_pre_restore(int platform_mode)
184 return (platform_mode && hibernation_ops) ?
185 hibernation_ops->pre_restore() : 0;
189 * platform_restore_cleanup - Switch to the working state after failing restore.
190 * @platform_mode: Whether or not to use the platform driver.
192 * Use the platform driver to switch the system to the normal mode of operation
193 * after a failing restore.
195 * If platform_pre_restore() has been called before the failing restore, this
196 * function must be called too, regardless of the result of
197 * platform_pre_restore().
199 static void platform_restore_cleanup(int platform_mode)
201 if (platform_mode && hibernation_ops)
202 hibernation_ops->restore_cleanup();
206 * platform_recover - Recover from a failure to suspend devices.
207 * @platform_mode: Whether or not to use the platform driver.
209 static void platform_recover(int platform_mode)
211 if (platform_mode && hibernation_ops && hibernation_ops->recover)
212 hibernation_ops->recover();
216 * swsusp_show_speed - Print time elapsed between two events during hibernation.
217 * @start: Starting event.
218 * @stop: Final event.
219 * @nr_pages: Number of memory pages processed between @start and @stop.
220 * @msg: Additional diagnostic message to print.
222 void swsusp_show_speed(struct timeval *start, struct timeval *stop,
223 unsigned nr_pages, char *msg)
225 s64 elapsed_centisecs64;
226 int centisecs;
227 int k;
228 int kps;
230 elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
231 do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
232 centisecs = elapsed_centisecs64;
233 if (centisecs == 0)
234 centisecs = 1; /* avoid div-by-zero */
235 k = nr_pages * (PAGE_SIZE / 1024);
236 kps = (k * 100) / centisecs;
237 printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
238 msg, k,
239 centisecs / 100, centisecs % 100,
240 kps / 1000, (kps % 1000) / 10);
244 * create_image - Create a hibernation image.
245 * @platform_mode: Whether or not to use the platform driver.
247 * Execute device drivers' .freeze_noirq() callbacks, create a hibernation image
248 * and execute the drivers' .thaw_noirq() callbacks.
250 * Control reappears in this routine after the subsequent restore.
252 static int create_image(int platform_mode)
254 int error;
256 error = dpm_suspend_noirq(PMSG_FREEZE);
257 if (error) {
258 printk(KERN_ERR "PM: Some devices failed to power down, "
259 "aborting hibernation\n");
260 return error;
263 error = platform_pre_snapshot(platform_mode);
264 if (error || hibernation_test(TEST_PLATFORM))
265 goto Platform_finish;
267 error = disable_nonboot_cpus();
268 if (error || hibernation_test(TEST_CPUS))
269 goto Enable_cpus;
271 local_irq_disable();
273 error = syscore_suspend();
274 if (error) {
275 printk(KERN_ERR "PM: Some system devices failed to power down, "
276 "aborting hibernation\n");
277 goto Enable_irqs;
280 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
281 goto Power_up;
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);
296 Power_up:
297 syscore_resume();
299 Enable_irqs:
300 local_irq_enable();
302 Enable_cpus:
303 enable_nonboot_cpus();
305 Platform_finish:
306 platform_finish(platform_mode);
308 dpm_resume_noirq(in_suspend ?
309 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
311 return error;
315 * hibernation_snapshot - Quiesce devices and create a hibernation image.
316 * @platform_mode: If set, use platform driver to prepare for the transition.
318 * This routine must be called with pm_mutex held.
320 int hibernation_snapshot(int platform_mode)
322 pm_message_t msg;
323 int error;
325 error = platform_begin(platform_mode);
326 if (error)
327 goto Close;
329 /* Preallocate image memory before shutting down devices. */
330 error = hibernate_preallocate_memory();
331 if (error)
332 goto Close;
334 error = freeze_kernel_threads();
335 if (error)
336 goto Cleanup;
338 if (hibernation_test(TEST_FREEZER)) {
341 * Indicate to the caller that we are returning due to a
342 * successful freezer test.
344 freezer_test_done = true;
345 goto Cleanup;
348 error = dpm_prepare(PMSG_FREEZE);
349 if (error) {
350 dpm_complete(PMSG_RECOVER);
351 goto Cleanup;
354 suspend_console();
355 pm_restrict_gfp_mask();
357 error = dpm_suspend(PMSG_FREEZE);
359 if (error || hibernation_test(TEST_DEVICES))
360 platform_recover(platform_mode);
361 else
362 error = create_image(platform_mode);
365 * In the case that we call create_image() above, the control
366 * returns here (1) after the image has been created or the
367 * image creation has failed and (2) after a successful restore.
370 /* We may need to release the preallocated image pages here. */
371 if (error || !in_suspend)
372 swsusp_free();
374 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
375 dpm_resume(msg);
377 if (error || !in_suspend)
378 pm_restore_gfp_mask();
380 resume_console();
381 dpm_complete(msg);
383 Close:
384 platform_end(platform_mode);
385 return error;
387 Cleanup:
388 swsusp_free();
389 goto Close;
393 * resume_target_kernel - Restore system state from a hibernation image.
394 * @platform_mode: Whether or not to use the platform driver.
396 * Execute device drivers' .freeze_noirq() callbacks, restore the contents of
397 * highmem that have not been restored yet from the image and run the low-level
398 * code that will restore the remaining contents of memory and switch to the
399 * just restored target kernel.
401 static int resume_target_kernel(bool platform_mode)
403 int error;
405 error = dpm_suspend_noirq(PMSG_QUIESCE);
406 if (error) {
407 printk(KERN_ERR "PM: Some devices failed to power down, "
408 "aborting resume\n");
409 return error;
412 error = platform_pre_restore(platform_mode);
413 if (error)
414 goto Cleanup;
416 error = disable_nonboot_cpus();
417 if (error)
418 goto Enable_cpus;
420 local_irq_disable();
422 error = syscore_suspend();
423 if (error)
424 goto Enable_irqs;
426 save_processor_state();
427 error = restore_highmem();
428 if (!error) {
429 error = swsusp_arch_resume();
431 * The code below is only ever reached in case of a failure.
432 * Otherwise, execution continues at the place where
433 * swsusp_arch_suspend() was called.
435 BUG_ON(!error);
437 * This call to restore_highmem() reverts the changes made by
438 * the previous one.
440 restore_highmem();
443 * The only reason why swsusp_arch_resume() can fail is memory being
444 * very tight, so we have to free it as soon as we can to avoid
445 * subsequent failures.
447 swsusp_free();
448 restore_processor_state();
449 touch_softlockup_watchdog();
451 syscore_resume();
453 Enable_irqs:
454 local_irq_enable();
456 Enable_cpus:
457 enable_nonboot_cpus();
459 Cleanup:
460 platform_restore_cleanup(platform_mode);
462 dpm_resume_noirq(PMSG_RECOVER);
464 return error;
468 * hibernation_restore - Quiesce devices and restore from a hibernation image.
469 * @platform_mode: If set, use platform driver to prepare for the transition.
471 * This routine must be called with pm_mutex held. If it is successful, control
472 * reappears in the restored target kernel in hibernation_snapshot().
474 int hibernation_restore(int platform_mode)
476 int error;
478 pm_prepare_console();
479 suspend_console();
480 pm_restrict_gfp_mask();
481 error = dpm_suspend_start(PMSG_QUIESCE);
482 if (!error) {
483 error = resume_target_kernel(platform_mode);
484 dpm_resume_end(PMSG_RECOVER);
486 pm_restore_gfp_mask();
487 resume_console();
488 pm_restore_console();
489 return error;
493 * hibernation_platform_enter - Power off the system using the platform driver.
495 int hibernation_platform_enter(void)
497 int error;
499 if (!hibernation_ops)
500 return -ENOSYS;
503 * We have cancelled the power transition by running
504 * hibernation_ops->finish() before saving the image, so we should let
505 * the firmware know that we're going to enter the sleep state after all
507 error = hibernation_ops->begin();
508 if (error)
509 goto Close;
511 entering_platform_hibernation = true;
512 suspend_console();
513 error = dpm_suspend_start(PMSG_HIBERNATE);
514 if (error) {
515 if (hibernation_ops->recover)
516 hibernation_ops->recover();
517 goto Resume_devices;
520 error = dpm_suspend_noirq(PMSG_HIBERNATE);
521 if (error)
522 goto Resume_devices;
524 error = hibernation_ops->prepare();
525 if (error)
526 goto Platform_finish;
528 error = disable_nonboot_cpus();
529 if (error)
530 goto Platform_finish;
532 local_irq_disable();
533 syscore_suspend();
534 if (pm_wakeup_pending()) {
535 error = -EAGAIN;
536 goto Power_up;
539 hibernation_ops->enter();
540 /* We should never get here */
541 while (1);
543 Power_up:
544 syscore_resume();
545 local_irq_enable();
546 enable_nonboot_cpus();
548 Platform_finish:
549 hibernation_ops->finish();
551 dpm_resume_noirq(PMSG_RESTORE);
553 Resume_devices:
554 entering_platform_hibernation = false;
555 dpm_resume_end(PMSG_RESTORE);
556 resume_console();
558 Close:
559 hibernation_ops->end();
561 return error;
565 * power_down - Shut the machine down for hibernation.
567 * Use the platform driver, if configured, to put the system into the sleep
568 * state corresponding to hibernation, or try to power it off or reboot,
569 * depending on the value of hibernation_mode.
571 static void power_down(void)
573 switch (hibernation_mode) {
574 case HIBERNATION_REBOOT:
575 kernel_restart(NULL);
576 break;
577 case HIBERNATION_PLATFORM:
578 hibernation_platform_enter();
579 case HIBERNATION_SHUTDOWN:
580 kernel_power_off();
581 break;
583 kernel_halt();
585 * Valid image is on the disk, if we continue we risk serious data
586 * corruption after resume.
588 printk(KERN_CRIT "PM: Please power down manually\n");
589 while(1);
593 * hibernate - Carry out system hibernation, including saving the image.
595 int hibernate(void)
597 int error;
599 lock_system_sleep();
600 /* The snapshot device should not be opened while we're running */
601 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
602 error = -EBUSY;
603 goto Unlock;
606 pm_prepare_console();
607 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
608 if (error)
609 goto Exit;
611 /* Allocate memory management structures */
612 error = create_basic_memory_bitmaps();
613 if (error)
614 goto Exit;
616 printk(KERN_INFO "PM: Syncing filesystems ... ");
617 sys_sync();
618 printk("done.\n");
620 error = freeze_processes();
621 if (error)
622 goto Free_bitmaps;
624 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
625 if (error)
626 goto Thaw;
627 if (freezer_test_done) {
628 freezer_test_done = false;
629 goto Thaw;
632 if (in_suspend) {
633 unsigned int flags = 0;
635 if (hibernation_mode == HIBERNATION_PLATFORM)
636 flags |= SF_PLATFORM_MODE;
637 if (nocompress)
638 flags |= SF_NOCOMPRESS_MODE;
639 else
640 flags |= SF_CRC32_MODE;
642 pr_debug("PM: writing image.\n");
643 error = swsusp_write(flags);
644 swsusp_free();
645 if (!error)
646 power_down();
647 in_suspend = 0;
648 pm_restore_gfp_mask();
649 } else {
650 pr_debug("PM: Image restored successfully.\n");
653 Thaw:
654 thaw_processes();
655 Free_bitmaps:
656 free_basic_memory_bitmaps();
657 Exit:
658 pm_notifier_call_chain(PM_POST_HIBERNATION);
659 pm_restore_console();
660 atomic_inc(&snapshot_device_available);
661 Unlock:
662 unlock_system_sleep();
663 return error;
668 * software_resume - Resume from a saved hibernation image.
670 * This routine is called as a late initcall, when all devices have been
671 * discovered and initialized already.
673 * The image reading code is called to see if there is a hibernation image
674 * available for reading. If that is the case, devices are quiesced and the
675 * contents of memory is restored from the saved image.
677 * If this is successful, control reappears in the restored target kernel in
678 * hibernation_snaphot() which returns to hibernate(). Otherwise, the routine
679 * attempts to recover gracefully and make the kernel return to the normal mode
680 * of operation.
682 static int software_resume(void)
684 int error;
685 unsigned int flags;
688 * If the user said "noresume".. bail out early.
690 if (noresume)
691 return 0;
694 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
695 * is configured into the kernel. Since the regular hibernate
696 * trigger path is via sysfs which takes a buffer mutex before
697 * calling hibernate functions (which take pm_mutex) this can
698 * cause lockdep to complain about a possible ABBA deadlock
699 * which cannot happen since we're in the boot code here and
700 * sysfs can't be invoked yet. Therefore, we use a subclass
701 * here to avoid lockdep complaining.
703 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
705 if (swsusp_resume_device)
706 goto Check_image;
708 if (!strlen(resume_file)) {
709 error = -ENOENT;
710 goto Unlock;
713 pr_debug("PM: Checking hibernation image partition %s\n", resume_file);
715 if (resume_delay) {
716 printk(KERN_INFO "Waiting %dsec before reading resume device...\n",
717 resume_delay);
718 ssleep(resume_delay);
721 /* Check if the device is there */
722 swsusp_resume_device = name_to_dev_t(resume_file);
723 if (!swsusp_resume_device) {
725 * Some device discovery might still be in progress; we need
726 * to wait for this to finish.
728 wait_for_device_probe();
730 if (resume_wait) {
731 while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
732 msleep(10);
733 async_synchronize_full();
737 * We can't depend on SCSI devices being available after loading
738 * one of their modules until scsi_complete_async_scans() is
739 * called and the resume device usually is a SCSI one.
741 scsi_complete_async_scans();
743 swsusp_resume_device = name_to_dev_t(resume_file);
744 if (!swsusp_resume_device) {
745 error = -ENODEV;
746 goto Unlock;
750 Check_image:
751 pr_debug("PM: Hibernation image partition %d:%d present\n",
752 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
754 pr_debug("PM: Looking for hibernation image.\n");
755 error = swsusp_check();
756 if (error)
757 goto Unlock;
759 /* The snapshot device should not be opened while we're running */
760 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
761 error = -EBUSY;
762 swsusp_close(FMODE_READ);
763 goto Unlock;
766 pm_prepare_console();
767 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
768 if (error)
769 goto close_finish;
771 error = create_basic_memory_bitmaps();
772 if (error)
773 goto close_finish;
775 pr_debug("PM: Preparing processes for restore.\n");
776 error = freeze_processes();
777 if (error) {
778 swsusp_close(FMODE_READ);
779 goto Done;
782 pr_debug("PM: Loading hibernation image.\n");
784 error = swsusp_read(&flags);
785 swsusp_close(FMODE_READ);
786 if (!error)
787 hibernation_restore(flags & SF_PLATFORM_MODE);
789 printk(KERN_ERR "PM: Failed to load hibernation image, recovering.\n");
790 swsusp_free();
791 thaw_processes();
792 Done:
793 free_basic_memory_bitmaps();
794 Finish:
795 pm_notifier_call_chain(PM_POST_RESTORE);
796 pm_restore_console();
797 atomic_inc(&snapshot_device_available);
798 /* For success case, the suspend path will release the lock */
799 Unlock:
800 mutex_unlock(&pm_mutex);
801 pr_debug("PM: Hibernation image not present or could not be loaded.\n");
802 return error;
803 close_finish:
804 swsusp_close(FMODE_READ);
805 goto Finish;
808 late_initcall(software_resume);
811 static const char * const hibernation_modes[] = {
812 [HIBERNATION_PLATFORM] = "platform",
813 [HIBERNATION_SHUTDOWN] = "shutdown",
814 [HIBERNATION_REBOOT] = "reboot",
818 * /sys/power/disk - Control hibernation mode.
820 * Hibernation can be handled in several ways. There are a few different ways
821 * to put the system into the sleep state: using the platform driver (e.g. ACPI
822 * or other hibernation_ops), powering it off or rebooting it (for testing
823 * mostly).
825 * The sysfs file /sys/power/disk provides an interface for selecting the
826 * hibernation mode to use. Reading from this file causes the available modes
827 * to be printed. There are 3 modes that can be supported:
829 * 'platform'
830 * 'shutdown'
831 * 'reboot'
833 * If a platform hibernation driver is in use, 'platform' will be supported
834 * and will be used by default. Otherwise, 'shutdown' will be used by default.
835 * The selected option (i.e. the one corresponding to the current value of
836 * hibernation_mode) is enclosed by a square bracket.
838 * To select a given hibernation mode it is necessary to write the mode's
839 * string representation (as returned by reading from /sys/power/disk) back
840 * into /sys/power/disk.
843 static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
844 char *buf)
846 int i;
847 char *start = buf;
849 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
850 if (!hibernation_modes[i])
851 continue;
852 switch (i) {
853 case HIBERNATION_SHUTDOWN:
854 case HIBERNATION_REBOOT:
855 break;
856 case HIBERNATION_PLATFORM:
857 if (hibernation_ops)
858 break;
859 /* not a valid mode, continue with loop */
860 continue;
862 if (i == hibernation_mode)
863 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
864 else
865 buf += sprintf(buf, "%s ", hibernation_modes[i]);
867 buf += sprintf(buf, "\n");
868 return buf-start;
871 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
872 const char *buf, size_t n)
874 int error = 0;
875 int i;
876 int len;
877 char *p;
878 int mode = HIBERNATION_INVALID;
880 p = memchr(buf, '\n', n);
881 len = p ? p - buf : n;
883 lock_system_sleep();
884 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
885 if (len == strlen(hibernation_modes[i])
886 && !strncmp(buf, hibernation_modes[i], len)) {
887 mode = i;
888 break;
891 if (mode != HIBERNATION_INVALID) {
892 switch (mode) {
893 case HIBERNATION_SHUTDOWN:
894 case HIBERNATION_REBOOT:
895 hibernation_mode = mode;
896 break;
897 case HIBERNATION_PLATFORM:
898 if (hibernation_ops)
899 hibernation_mode = mode;
900 else
901 error = -EINVAL;
903 } else
904 error = -EINVAL;
906 if (!error)
907 pr_debug("PM: Hibernation mode set to '%s'\n",
908 hibernation_modes[mode]);
909 unlock_system_sleep();
910 return error ? error : n;
913 power_attr(disk);
915 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
916 char *buf)
918 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
919 MINOR(swsusp_resume_device));
922 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
923 const char *buf, size_t n)
925 unsigned int maj, min;
926 dev_t res;
927 int ret = -EINVAL;
929 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
930 goto out;
932 res = MKDEV(maj,min);
933 if (maj != MAJOR(res) || min != MINOR(res))
934 goto out;
936 lock_system_sleep();
937 swsusp_resume_device = res;
938 unlock_system_sleep();
939 printk(KERN_INFO "PM: Starting manual resume from disk\n");
940 noresume = 0;
941 software_resume();
942 ret = n;
943 out:
944 return ret;
947 power_attr(resume);
949 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
950 char *buf)
952 return sprintf(buf, "%lu\n", image_size);
955 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
956 const char *buf, size_t n)
958 unsigned long size;
960 if (sscanf(buf, "%lu", &size) == 1) {
961 image_size = size;
962 return n;
965 return -EINVAL;
968 power_attr(image_size);
970 static ssize_t reserved_size_show(struct kobject *kobj,
971 struct kobj_attribute *attr, char *buf)
973 return sprintf(buf, "%lu\n", reserved_size);
976 static ssize_t reserved_size_store(struct kobject *kobj,
977 struct kobj_attribute *attr,
978 const char *buf, size_t n)
980 unsigned long size;
982 if (sscanf(buf, "%lu", &size) == 1) {
983 reserved_size = size;
984 return n;
987 return -EINVAL;
990 power_attr(reserved_size);
992 static struct attribute * g[] = {
993 &disk_attr.attr,
994 &resume_attr.attr,
995 &image_size_attr.attr,
996 &reserved_size_attr.attr,
997 NULL,
1001 static struct attribute_group attr_group = {
1002 .attrs = g,
1006 static int __init pm_disk_init(void)
1008 return sysfs_create_group(power_kobj, &attr_group);
1011 core_initcall(pm_disk_init);
1014 static int __init resume_setup(char *str)
1016 if (noresume)
1017 return 1;
1019 strncpy( resume_file, str, 255 );
1020 return 1;
1023 static int __init resume_offset_setup(char *str)
1025 unsigned long long offset;
1027 if (noresume)
1028 return 1;
1030 if (sscanf(str, "%llu", &offset) == 1)
1031 swsusp_resume_block = offset;
1033 return 1;
1036 static int __init hibernate_setup(char *str)
1038 if (!strncmp(str, "noresume", 8))
1039 noresume = 1;
1040 else if (!strncmp(str, "nocompress", 10))
1041 nocompress = 1;
1042 return 1;
1045 static int __init noresume_setup(char *str)
1047 noresume = 1;
1048 return 1;
1051 static int __init resumewait_setup(char *str)
1053 resume_wait = 1;
1054 return 1;
1057 static int __init resumedelay_setup(char *str)
1059 resume_delay = simple_strtoul(str, NULL, 0);
1060 return 1;
1063 __setup("noresume", noresume_setup);
1064 __setup("resume_offset=", resume_offset_setup);
1065 __setup("resume=", resume_setup);
1066 __setup("hibernate=", hibernate_setup);
1067 __setup("resumewait", resumewait_setup);
1068 __setup("resumedelay=", resumedelay_setup);