Freeze processes prior to reading pageset1.
[linux-2.6/suspend2-2.6.18.git] / kernel / power / suspend.c
blob416ad70f9da26600bfbe61ee6e5f23ee336e535d
1 /*
2 * kernel/power/suspend.c
3 */
4 /** \mainpage Suspend2.
6 * Suspend2 provides support for saving and restoring an image of
7 * system memory to an arbitrary storage device, either on the local computer,
8 * or across some network. The support is entirely OS based, so Suspend2
9 * works without requiring BIOS, APM or ACPI support. The vast majority of the
10 * code is also architecture independant, so it should be very easy to port
11 * the code to new architectures. Suspend includes support for SMP, 4G HighMem
12 * and preemption. Initramfses and initrds are also supported.
14 * Suspend2 uses a modular design, in which the method of storing the image is
15 * completely abstracted from the core code, as are transformations on the data
16 * such as compression and/or encryption (multiple 'modules' can be used to
17 * provide arbitrary combinations of functionality). The user interface is also
18 * modular, so that arbitrarily simple or complex interfaces can be used to
19 * provide anything from debugging information through to eye candy.
21 * \section Copyright
23 * Suspend2 is released under the GPLv2.
25 * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu><BR>
26 * Copyright (C) 1998,2001,2002 Pavel Machek <pavel@suse.cz><BR>
27 * Copyright (C) 2002-2003 Florent Chabaud <fchabaud@free.fr><BR>
28 * Copyright (C) 2002-2007 Nigel Cunningham (nigel at suspend2 net)<BR>
30 * \section Credits
32 * Nigel would like to thank the following people for their work:
34 * Bernard Blackham <bernard@blackham.com.au><BR>
35 * Web page & Wiki administration, some coding. A person without whom
36 * Suspend would not be where it is.
38 * Michael Frank <mhf@linuxmail.org><BR>
39 * Extensive testing and help with improving stability. I was constantly
40 * amazed by the quality and quantity of Michael's help.
42 * Pavel Machek <pavel@ucw.cz><BR>
43 * Modifications, defectiveness pointing, being with Gabor at the very beginning,
44 * suspend to swap space, stop all tasks. Port to 2.4.18-ac and 2.5.17. Even
45 * though Pavel and I disagree on the direction suspend to disk should take, I
46 * appreciate the valuable work he did in helping Gabor get the concept working.
48 * ..and of course the myriads of Suspend2 users who have helped diagnose
49 * and fix bugs, made suggestions on how to improve the code, proofread
50 * documentation, and donated time and money.
52 * Thanks also to corporate sponsors:
54 * <B>Redhat.</B>Sometime employer from May 2006 (my fault, not Redhat's!).
56 * <B>Cyclades.com.</B> Nigel's employers from Dec 2004 until May 2006, who
57 * allowed him to work on Suspend and PM related issues on company time.
59 * <B>LinuxFund.org.</B> Sponsored Nigel's work on Suspend for four months Oct 2003
60 * to Jan 2004.
62 * <B>LAC Linux.</B> Donated P4 hardware that enabled development and ongoing
63 * maintenance of SMP and Highmem support.
65 * <B>OSDL.</B> Provided access to various hardware configurations, make occasional
66 * small donations to the project.
69 #include <linux/suspend.h>
70 #include <linux/module.h>
71 #include <linux/console.h>
72 #include <linux/version.h>
73 #include <linux/reboot.h>
74 #include <linux/mm.h>
75 #include <linux/highmem.h>
76 #include <linux/writeback.h>
77 #include <linux/freezer.h>
78 #include <linux/utsrelease.h>
79 #include <linux/swap.h> /* For LRU unlinking prototypes */
80 #include <linux/cpu.h>
81 #include <linux/resume-trace.h>
82 #include <asm/uaccess.h>
83 #include <asm/setup.h>
85 #include "modules.h"
86 #include "sysfs.h"
87 #include "pageflags.h"
88 #include "prepare_image.h"
89 #include "io.h"
90 #include "ui.h"
91 #include "extent.h"
92 #include "power_off.h"
93 #include "storage.h"
94 #include "checksum.h"
95 #include "cluster.h"
96 #include "suspend_builtin.h"
98 #ifdef CONFIG_X86
99 #include <asm/i387.h> /* for kernel_fpu_end */
100 #endif
102 static int had_pmsem = 0;
104 /* Variables to be preserved over suspend */
105 int image_size_limit = 0;
107 static int orig_system_state;
109 int do_suspend2_step(int step);
112 * --- Variables -----
114 * The following are used by the arch specific low level routines
115 * and only needed if suspend2 is compiled in. Other variables,
116 * used by the freezer even if suspend2 is not compiled in, are
117 * found in process.c
120 /*! Pageset metadata. */
121 struct pagedir pagedir2 = {2};
123 static mm_segment_t oldfs;
125 static atomic_t actions_running;
127 int block_dump_save;
130 * Basic clean-up routine.
132 void suspend_finish_anything(int suspend_or_resume)
134 if (!atomic_dec_and_test(&actions_running))
135 return;
137 suspend_cleanup_modules(suspend_or_resume);
138 suspend_put_modules();
139 clear_suspend_state(SUSPEND_RUNNING);
140 set_fs(oldfs);
141 if (suspend_or_resume) {
142 block_dump = block_dump_save;
143 set_cpus_allowed(current, CPU_MASK_ALL);
148 * Basic set-up routine.
150 int suspend_start_anything(int suspend_or_resume)
152 if (atomic_add_return(1, &actions_running) != 1) {
153 if (suspend_or_resume) {
154 printk("Can't start a cycle when actions are "
155 "already running.\n");
156 atomic_dec(&actions_running);
157 return -EBUSY;
158 } else
159 return 0;
162 oldfs = get_fs();
163 set_fs(KERNEL_DS);
165 if (!suspendActiveAllocator) {
166 /* Be quiet if we're not trying to suspend or resume */
167 if (suspend_or_resume)
168 printk("No storage allocator is currently active. "
169 "Rechecking whether we can use one.\n");
170 suspend_attempt_to_parse_resume_device(!suspend_or_resume);
173 set_suspend_state(SUSPEND_RUNNING);
175 if (suspend_get_modules()) {
176 printk("Suspend2: Get modules failed!\n");
177 goto out_err;
180 if (suspend_initialise_modules(suspend_or_resume)) {
181 printk("Suspend2: Initialise modules failed!\n");
182 goto out_err;
185 if (suspend_or_resume) {
186 block_dump_save = block_dump;
187 block_dump = 0;
188 set_cpus_allowed(current, CPU_MASK_CPU0);
191 return 0;
193 out_err:
194 if (suspend_or_resume)
195 block_dump_save = block_dump;
196 suspend_finish_anything(suspend_or_resume);
197 return -EBUSY;
200 static int allocate_bitmaps(void)
202 if (allocate_dyn_pageflags(&pageset1_map) ||
203 allocate_dyn_pageflags(&pageset1_copy_map) ||
204 allocate_dyn_pageflags(&pageset2_map) ||
205 allocate_dyn_pageflags(&io_map) ||
206 allocate_dyn_pageflags(&page_resave_map))
207 return 1;
209 return 0;
212 static void free_metadata(void)
214 free_dyn_pageflags(&pageset1_map);
215 free_dyn_pageflags(&pageset1_copy_map);
216 free_dyn_pageflags(&pageset2_map);
217 free_dyn_pageflags(&io_map);
218 free_dyn_pageflags(&page_resave_map);
221 static int io_MB_per_second(int read_write)
223 return (suspend_io_time[read_write][1]) ?
224 MB((unsigned long) suspend_io_time[read_write][0]) * HZ /
225 suspend_io_time[read_write][1] : 0;
228 /* get_debug_info
229 * Functionality: Store debug info in a buffer.
231 #define SNPRINTF(a...) len += snprintf_used(((char *)buffer) + len, \
232 count - len - 1, ## a)
233 static int get_suspend_debug_info(const char *buffer, int count)
235 int len = 0;
237 SNPRINTF("Suspend2 debugging info:\n");
238 SNPRINTF("- Suspend core : %s\n", SUSPEND_CORE_VERSION);
239 SNPRINTF("- Kernel Version : %s\n", UTS_RELEASE);
240 SNPRINTF("- Compiler vers. : %d.%d\n", __GNUC__, __GNUC_MINOR__);
241 SNPRINTF("- Attempt number : %d\n", nr_suspends);
242 SNPRINTF("- Parameters : %ld %ld %ld %d %d %ld\n",
243 suspend_result,
244 suspend_action,
245 suspend_debug_state,
246 suspend_default_console_level,
247 image_size_limit,
248 suspend_powerdown_method);
249 SNPRINTF("- Overall expected compression percentage: %d.\n",
250 100 - suspend_expected_compression_ratio());
251 len+= suspend_print_module_debug_info(((char *) buffer) + len,
252 PAGE_SIZE - len - 1);
253 if (suspend_io_time[0][1]) {
254 if ((io_MB_per_second(0) < 5) || (io_MB_per_second(1) < 5)) {
255 SNPRINTF("- I/O speed: Write %d KB/s",
256 (KB((unsigned long) suspend_io_time[0][0]) * HZ /
257 suspend_io_time[0][1]));
258 if (suspend_io_time[1][1])
259 SNPRINTF(", Read %d KB/s",
260 (KB((unsigned long) suspend_io_time[1][0]) * HZ /
261 suspend_io_time[1][1]));
262 } else {
263 SNPRINTF("- I/O speed: Write %d MB/s",
264 (MB((unsigned long) suspend_io_time[0][0]) * HZ /
265 suspend_io_time[0][1]));
266 if (suspend_io_time[1][1])
267 SNPRINTF(", Read %d MB/s",
268 (MB((unsigned long) suspend_io_time[1][0]) * HZ /
269 suspend_io_time[1][1]));
271 SNPRINTF(".\n");
273 else
274 SNPRINTF("- No I/O speed stats available.\n");
275 SNPRINTF("- Extra pages : %d used/%d.\n",
276 extra_pd1_pages_used, extra_pd1_pages_allowance);
278 return len;
281 static void suspend_cleanup(void)
283 int i = 0;
284 char *buffer;;
286 free_checksum_pages();
288 buffer = (char *) get_zeroed_page(GFP_ATOMIC);
290 if (buffer)
291 i = get_suspend_debug_info(buffer, PAGE_SIZE);
293 suspend_free_extra_pagedir_memory();
295 pagedir1.size = pagedir2.size = 0;
296 set_highmem_size(pagedir1, 0);
297 set_highmem_size(pagedir2, 0);
299 restore_avenrun();
301 system_state = orig_system_state;
303 thaw_processes();
305 #ifdef CONFIG_SUSPEND2_KEEP_IMAGE
306 if (test_action_state(SUSPEND_KEEP_IMAGE) &&
307 !test_result_state(SUSPEND_ABORTED)) {
308 suspend_message(SUSPEND_ANY_SECTION, SUSPEND_LOW, 1,
309 "Suspend2: Not invalidating the image due "
310 "to Keep Image being enabled.\n");
311 set_result_state(SUSPEND_KEPT_IMAGE);
312 } else
313 #endif
314 if (suspendActiveAllocator)
315 suspendActiveAllocator->invalidate_image();
317 free_metadata();
319 if (buffer && i) {
320 /* Printk can only handle 1023 bytes, including
321 * its level mangling. */
322 for (i = 0; i < 3; i++)
323 printk("%s", buffer + (1023 * i));
324 free_page((unsigned long) buffer);
327 if (!test_action_state(SUSPEND_LATE_CPU_HOTPLUG))
328 enable_nonboot_cpus();
329 suspend_cleanup_console();
331 clear_suspend_state(SUSPEND_IGNORE_LOGLEVEL);
332 clear_suspend_state(SUSPEND_TRYING_TO_RESUME);
333 clear_suspend_state(SUSPEND_NOW_RESUMING);
335 if (!had_pmsem)
336 up(&pm_sem);
340 * __save_image
341 * Functionality : High level routine which performs the steps necessary
342 * to save the image after preparatory steps have been taken.
343 * Key Assumptions : Processes frozen, sufficient memory available, drivers
344 * suspended.
346 static int __save_image(void)
348 int temp_result;
350 suspend_prepare_status(DONT_CLEAR_BAR, "Starting to save the image..");
352 suspend_message(SUSPEND_ANY_SECTION, SUSPEND_LOW, 1,
353 " - Final values: %d and %d.\n",
354 pagedir1.size, pagedir2.size);
356 suspend_cond_pause(1, "About to write pagedir2.");
358 calculate_check_checksums(0);
360 temp_result = write_pageset(&pagedir2);
362 if (temp_result == -1 || test_result_state(SUSPEND_ABORTED))
363 return 1;
365 suspend_cond_pause(1, "About to copy pageset 1.");
367 if (test_result_state(SUSPEND_ABORTED))
368 return 1;
370 suspend_deactivate_storage(1);
372 suspend_prepare_status(DONT_CLEAR_BAR, "Doing atomic copy.");
374 suspend2_in_suspend = 1;
376 if (device_suspend(PMSG_FREEZE)) {
377 set_result_state(SUSPEND_DEVICE_REFUSED);
378 set_result_state(SUSPEND_ABORTED);
379 return 1;
382 if (test_action_state(SUSPEND_LATE_CPU_HOTPLUG))
383 disable_nonboot_cpus();
385 temp_result = suspend2_suspend();
387 if (pm_ops && pm_ops->finish && suspend_powerdown_method > 3)
388 pm_ops->finish(suspend_powerdown_method);
390 if (test_action_state(SUSPEND_LATE_CPU_HOTPLUG))
391 enable_nonboot_cpus();
393 device_resume();
395 if (temp_result)
396 return 1;
398 /* Resume time? */
399 if (!suspend2_in_suspend) {
400 copyback_post();
401 return 0;
404 /* Nope. Suspending. So, see if we can save the image... */
406 if (suspend_activate_storage(1))
407 panic("Failed to reactivate our storage.");
409 suspend_update_status(pagedir2.size,
410 pagedir1.size + pagedir2.size,
411 NULL);
413 if (test_result_state(SUSPEND_ABORTED))
414 goto abort_reloading_pagedir_two;
416 suspend_cond_pause(1, "About to write pageset1.");
418 suspend_message(SUSPEND_ANY_SECTION, SUSPEND_LOW, 1,
419 "-- Writing pageset1\n");
421 temp_result = write_pageset(&pagedir1);
423 /* We didn't overwrite any memory, so no reread needs to be done. */
424 if (test_action_state(SUSPEND_TEST_FILTER_SPEED))
425 return 1;
427 if (temp_result == 1 || test_result_state(SUSPEND_ABORTED))
428 goto abort_reloading_pagedir_two;
430 suspend_cond_pause(1, "About to write header.");
432 if (test_result_state(SUSPEND_ABORTED))
433 goto abort_reloading_pagedir_two;
435 temp_result = write_image_header();
437 if (test_action_state(SUSPEND_TEST_BIO))
438 return 1;
440 if (!temp_result && !test_result_state(SUSPEND_ABORTED))
441 return 0;
443 abort_reloading_pagedir_two:
444 temp_result = read_pageset2(1);
446 /* If that failed, we're sunk. Panic! */
447 if (temp_result)
448 panic("Attempt to reload pagedir 2 while aborting "
449 "a suspend failed.");
451 return 1;
454 static int check_still_keeping_image(void)
456 if (test_action_state(SUSPEND_KEEP_IMAGE)) {
457 printk("Image already stored: powering down immediately.");
458 do_suspend2_step(STEP_SUSPEND_POWERDOWN);
459 return 1; /* Just in case we're using S3 */
462 printk("Invalidating previous image.\n");
463 suspendActiveAllocator->invalidate_image();
465 return 0;
468 static int suspend_init(void)
470 suspend_result = 0;
472 printk(KERN_INFO "Suspend2: Initiating a software suspend cycle.\n");
474 nr_suspends++;
476 orig_system_state = system_state;
478 save_avenrun();
480 suspend_io_time[0][0] = suspend_io_time[0][1] =
481 suspend_io_time[1][0] = suspend_io_time[1][1] = 0;
483 free_metadata(); /* We might have kept it */
485 if (!test_suspend_state(SUSPEND_CAN_SUSPEND) ||
486 allocate_bitmaps())
487 return 0;
489 suspend_prepare_console();
490 if (!test_action_state(SUSPEND_LATE_CPU_HOTPLUG))
491 disable_nonboot_cpus();
493 return 1;
496 static int can_suspend(void)
498 if (!had_pmsem && down_trylock(&pm_sem)) {
499 printk("Suspend2: Failed to obtain pm_semaphore.\n");
500 set_result_state(SUSPEND_ABORTED);
501 set_result_state(SUSPEND_PM_SEM);
502 return 0;
505 if (!test_suspend_state(SUSPEND_CAN_SUSPEND))
506 suspend_attempt_to_parse_resume_device(0);
508 if (!test_suspend_state(SUSPEND_CAN_SUSPEND)) {
509 printk("Suspend2: Software suspend is disabled.\n"
510 "This may be because you haven't put something along "
511 "the lines of\n\nresume2=swap:/dev/hda1\n\n"
512 "in lilo.conf or equivalent. (Where /dev/hda1 is your "
513 "swap partition).\n");
514 set_result_state(SUSPEND_ABORTED);
515 if (!had_pmsem)
516 up(&pm_sem);
517 return 0;
520 return 1;
524 * do_cleanup
527 static void do_cleanup(void)
529 suspend_prepare_status(DONT_CLEAR_BAR, "Cleaning up...");
530 relink_lru_lists();
531 suspend_cleanup();
532 suspend_deactivate_storage(0);
535 static int do_power_down(void)
537 /* If switching images fails, do normal powerdown */
538 if (poweroff_resume2[0])
539 do_suspend2_step(STEP_RESUME_ALT_IMAGE);
541 suspend_cond_pause(1, "About to power down or reboot.");
542 suspend_power_down();
544 /* If we return, it's because we suspended to ram */
545 if (read_pageset2(1))
546 panic("Attempt to reload pagedir 2 failed. Try rebooting.");
548 barrier();
549 mb();
550 do_cleanup();
551 return 0;
555 * do_save_image
557 * Save the prepared image.
560 static int do_save_image(void)
562 int result;
564 unlink_lru_lists();
565 result = __save_image();
566 if (!suspend2_in_suspend || result)
567 do_cleanup();
568 return result;
572 /* do_prepare_image
574 * Seek to initialise and prepare an image to be saved. On failure,
575 * cleanup.
578 static int do_prepare_image(void)
580 if (suspend_activate_storage(0))
581 return 1;
583 if (!can_suspend())
584 goto cleanup_deactivate_storage;
587 * If kept image and still keeping image and suspending to RAM, we will
588 * return 1 after suspending and resuming (provided the power doesn't
589 * run out.
592 if (test_result_state(SUSPEND_KEPT_IMAGE) && check_still_keeping_image())
593 goto cleanup;
595 if (suspend_init() && !suspend_prepare_image() &&
596 !test_result_state(SUSPEND_ABORTED))
597 return 0;
599 cleanup:
600 suspend_cleanup();
601 cleanup_deactivate_storage:
602 suspend_deactivate_storage(0);
603 return 1;
606 static int do_check_can_resume(void)
608 char *buf = (char *) get_zeroed_page(GFP_KERNEL);
609 int result = 0;
611 if (!buf)
612 return 0;
614 /* Only interested in first byte, so throw away return code. */
615 image_exists_read(buf, PAGE_SIZE);
617 if (buf[0] == '1')
618 result = 1;
620 free_page((unsigned long) buf);
621 return result;
625 * We check if we have an image and if so we try to resume.
627 static int do_load_atomic_copy(void)
629 int read_image_result = 0;
631 if (sizeof(swp_entry_t) != sizeof(long)) {
632 printk(KERN_WARNING "Suspend2: "
633 "The size of swp_entry_t != size of long. "
634 "Please report this!\n");
635 return 1;
638 if (!resume2_file[0])
639 printk(KERN_WARNING "Suspend2: "
640 "You need to use a resume2= command line parameter to "
641 "tell Suspend2 where to look for an image.\n");
643 suspend_activate_storage(0);
645 if (!(test_suspend_state(SUSPEND_RESUME_DEVICE_OK)) &&
646 !suspend_attempt_to_parse_resume_device(0)) {
648 * Without a usable storage device we can do nothing -
649 * even if noresume is given
652 if (!suspendNumAllocators)
653 printk(KERN_ALERT "Suspend2: "
654 "No storage allocators have been registered.\n");
655 else
656 printk(KERN_ALERT "Suspend2: "
657 "Missing or invalid storage location "
658 "(resume2= parameter). Please correct and "
659 "rerun lilo (or equivalent) before "
660 "suspending.\n");
661 suspend_deactivate_storage(0);
662 return 1;
665 read_image_result = read_pageset1(); /* non fatal error ignored */
667 if (test_suspend_state(SUSPEND_NORESUME_SPECIFIED)) {
668 printk(KERN_WARNING "Suspend2: Resuming disabled as requested.\n");
669 clear_suspend_state(SUSPEND_NORESUME_SPECIFIED);
672 suspend_deactivate_storage(0);
674 if (read_image_result)
675 return 1;
677 return 0;
680 void prepare_restore_load_alt_image(int prepare)
682 static dyn_pageflags_t pageset1_map_save, pageset1_copy_map_save;
684 if (prepare) {
685 pageset1_map_save = pageset1_map;
686 pageset1_map = NULL;
687 pageset1_copy_map_save = pageset1_copy_map;
688 pageset1_copy_map = NULL;
689 set_suspend_state(SUSPEND_LOADING_ALT_IMAGE);
690 suspend_reset_alt_image_pageset2_pfn();
691 } else {
692 if (pageset1_map)
693 free_dyn_pageflags(&pageset1_map);
694 pageset1_map = pageset1_map_save;
695 if (pageset1_copy_map)
696 free_dyn_pageflags(&pageset1_copy_map);
697 pageset1_copy_map = pageset1_copy_map_save;
698 clear_suspend_state(SUSPEND_NOW_RESUMING);
699 clear_suspend_state(SUSPEND_LOADING_ALT_IMAGE);
703 int pre_resume_freeze(void)
705 suspend_prepare_status(DONT_CLEAR_BAR, "Prepare console");
707 if (test_action_state(SUSPEND_PM_PREPARE_CONSOLE))
708 pm_prepare_console();
710 if (!test_action_state(SUSPEND_LATE_CPU_HOTPLUG)) {
711 suspend_prepare_status(DONT_CLEAR_BAR, "Disable nonboot cpus.");
712 disable_nonboot_cpus();
715 suspend_prepare_status(DONT_CLEAR_BAR, "Freeze processes.");
717 if (freeze_processes()) {
718 printk("Some processes failed to suspend\n");
719 return 1;
722 return 0;
725 void post_resume_thaw(void)
727 thaw_processes();
728 if (!test_action_state(SUSPEND_LATE_CPU_HOTPLUG))
729 enable_nonboot_cpus();
731 if (test_action_state(SUSPEND_PM_PREPARE_CONSOLE))
732 pm_restore_console();
735 int do_suspend2_step(int step)
737 int result;
739 switch (step) {
740 case STEP_SUSPEND_PREPARE_IMAGE:
741 return do_prepare_image();
742 case STEP_SUSPEND_SAVE_IMAGE:
743 return do_save_image();
744 case STEP_SUSPEND_POWERDOWN:
745 return do_power_down();
746 case STEP_RESUME_CAN_RESUME:
747 return do_check_can_resume();
748 case STEP_RESUME_LOAD_PS1:
749 pre_resume_freeze();
750 return do_load_atomic_copy();
751 case STEP_RESUME_DO_RESTORE:
753 * If we succeed, this doesn't return.
754 * Instead, we return from do_save_image() in the
755 * suspended kernel.
757 result = suspend_atomic_restore();
758 if (result)
759 post_resume_thaw();
760 return result;
761 case STEP_RESUME_ALT_IMAGE:
762 printk("Trying to resume alternate image.\n");
763 suspend2_in_suspend = 0;
764 replace_restore_resume2(1, 0);
765 prepare_restore_load_alt_image(1);
766 if (!do_check_can_resume()) {
767 printk("Nothing to resume from.\n");
768 goto out;
770 if (!do_load_atomic_copy()) {
771 printk("Failed to load image.\n");
772 suspend_atomic_restore();
774 out:
775 prepare_restore_load_alt_image(0);
776 replace_restore_resume2(0, 0);
777 break;
780 return 0;
783 /* -- Functions for kickstarting a suspend or resume --- */
786 * Check if we have an image and if so try to resume.
788 void __suspend_try_resume(void)
790 set_suspend_state(SUSPEND_TRYING_TO_RESUME);
791 clear_suspend_state(SUSPEND_RESUME_NOT_DONE);
793 if (do_suspend2_step(STEP_RESUME_CAN_RESUME) &&
794 !do_suspend2_step(STEP_RESUME_LOAD_PS1) &&
795 do_suspend2_step(STEP_RESUME_DO_RESTORE))
796 do_cleanup();
798 clear_suspend_state(SUSPEND_IGNORE_LOGLEVEL);
799 clear_suspend_state(SUSPEND_TRYING_TO_RESUME);
800 clear_suspend_state(SUSPEND_NOW_RESUMING);
803 /* Wrapper for when called from init/do_mounts.c */
804 void __suspend2_try_resume(void)
806 clear_suspend_state(SUSPEND_RESUME_NOT_DONE);
808 if (suspend_start_anything(SYSFS_RESUMING))
809 return;
811 down(&pm_sem);
812 __suspend_try_resume();
815 * For initramfs, we have to clear the boot time
816 * flag after trying to resume
818 clear_suspend_state(SUSPEND_BOOT_TIME);
820 up(&pm_sem);
822 suspend_finish_anything(SYSFS_RESUMING);
826 * suspend2_try_suspend
827 * Functionality :
828 * Called From : drivers/acpi/sleep/main.c
829 * kernel/reboot.c
831 int __suspend2_try_suspend(int have_pmsem)
833 int result = 0, sys_power_disk = 0;
835 if (!atomic_read(&actions_running)) {
836 /* Came in via /sys/power/disk */
837 if (suspend_start_anything(SYSFS_SUSPENDING))
838 return -EBUSY;
839 sys_power_disk = 1;
842 had_pmsem = have_pmsem;
844 if (strlen(poweroff_resume2)) {
845 attempt_to_parse_po_resume_device2();
847 if (!strlen(poweroff_resume2)) {
848 printk("Poweroff resume2 now invalid. Aborting.\n");
849 goto out;
853 if ((result = do_suspend2_step(STEP_SUSPEND_PREPARE_IMAGE)))
854 goto out;
856 if (test_action_state(SUSPEND_FREEZER_TEST)) {
857 do_cleanup();
858 goto out;
861 if ((result = do_suspend2_step(STEP_SUSPEND_SAVE_IMAGE)))
862 goto out;
864 /* This code runs at resume time too! */
865 if (suspend2_in_suspend)
866 result = do_suspend2_step(STEP_SUSPEND_POWERDOWN);
867 out:
868 if (sys_power_disk)
869 suspend_finish_anything(SYSFS_SUSPENDING);
870 return result;
874 * This array contains entries that are automatically registered at
875 * boot. Modules and the console code register their own entries separately.
877 static struct suspend_sysfs_data sysfs_params[] = {
878 { SUSPEND2_ATTR("extra_pages_allowance", SYSFS_RW),
879 SYSFS_INT(&extra_pd1_pages_allowance, 0, INT_MAX, 0)
882 { SUSPEND2_ATTR("image_exists", SYSFS_RW),
883 SYSFS_CUSTOM(image_exists_read, image_exists_write,
884 SYSFS_NEEDS_SM_FOR_BOTH)
887 { SUSPEND2_ATTR("resume2", SYSFS_RW),
888 SYSFS_STRING(resume2_file, 255, SYSFS_NEEDS_SM_FOR_WRITE),
889 .write_side_effect = attempt_to_parse_resume_device2,
892 { SUSPEND2_ATTR("poweroff_resume2", SYSFS_RW),
893 SYSFS_STRING(poweroff_resume2, 255, SYSFS_NEEDS_SM_FOR_WRITE),
894 .write_side_effect = attempt_to_parse_po_resume_device2,
896 { SUSPEND2_ATTR("debug_info", SYSFS_READONLY),
897 SYSFS_CUSTOM(get_suspend_debug_info, NULL, 0)
900 { SUSPEND2_ATTR("ignore_rootfs", SYSFS_RW),
901 SYSFS_BIT(&suspend_action, SUSPEND_IGNORE_ROOTFS, 0)
904 { SUSPEND2_ATTR("image_size_limit", SYSFS_RW),
905 SYSFS_INT(&image_size_limit, -2, INT_MAX, 0)
908 { SUSPEND2_ATTR("last_result", SYSFS_RW),
909 SYSFS_UL(&suspend_result, 0, 0, 0)
912 { SUSPEND2_ATTR("no_multithreaded_io", SYSFS_RW),
913 SYSFS_BIT(&suspend_action, SUSPEND_NO_MULTITHREADED_IO, 0)
916 { SUSPEND2_ATTR("full_pageset2", SYSFS_RW),
917 SYSFS_BIT(&suspend_action, SUSPEND_PAGESET2_FULL, 0)
920 { SUSPEND2_ATTR("reboot", SYSFS_RW),
921 SYSFS_BIT(&suspend_action, SUSPEND_REBOOT, 0)
924 #ifdef CONFIG_SOFTWARE_SUSPEND
925 { SUSPEND2_ATTR("replace_swsusp", SYSFS_RW),
926 SYSFS_BIT(&suspend_action, SUSPEND_REPLACE_SWSUSP, 0)
928 #endif
930 { SUSPEND2_ATTR("resume_commandline", SYSFS_RW),
931 SYSFS_STRING(suspend2_nosave_commandline, COMMAND_LINE_SIZE, 0)
934 { SUSPEND2_ATTR("version", SYSFS_READONLY),
935 SYSFS_STRING(SUSPEND_CORE_VERSION, 0, 0)
938 { SUSPEND2_ATTR("no_load_direct", SYSFS_RW),
939 SYSFS_BIT(&suspend_action, SUSPEND_NO_DIRECT_LOAD, 0)
942 { SUSPEND2_ATTR("freezer_test", SYSFS_RW),
943 SYSFS_BIT(&suspend_action, SUSPEND_FREEZER_TEST, 0)
946 { SUSPEND2_ATTR("test_bio", SYSFS_RW),
947 SYSFS_BIT(&suspend_action, SUSPEND_TEST_BIO, 0)
950 { SUSPEND2_ATTR("test_filter_speed", SYSFS_RW),
951 SYSFS_BIT(&suspend_action, SUSPEND_TEST_FILTER_SPEED, 0)
954 { SUSPEND2_ATTR("slow", SYSFS_RW),
955 SYSFS_BIT(&suspend_action, SUSPEND_SLOW, 0)
958 { SUSPEND2_ATTR("no_pageset2", SYSFS_RW),
959 SYSFS_BIT(&suspend_action, SUSPEND_NO_PAGESET2, 0)
962 { SUSPEND2_ATTR("late_cpu_hotplug", SYSFS_RW),
963 SYSFS_BIT(&suspend_action, SUSPEND_LATE_CPU_HOTPLUG, 0)
966 #if defined(CONFIG_ACPI)
967 { SUSPEND2_ATTR("powerdown_method", SYSFS_RW),
968 SYSFS_UL(&suspend_powerdown_method, 0, 5, 0)
970 #endif
972 #ifdef CONFIG_SUSPEND2_KEEP_IMAGE
973 { SUSPEND2_ATTR("keep_image", SYSFS_RW),
974 SYSFS_BIT(&suspend_action, SUSPEND_KEEP_IMAGE, 0)
976 #endif
979 struct suspend2_core_fns my_fns = {
980 .get_nonconflicting_page = __suspend_get_nonconflicting_page,
981 .post_context_save = __suspend_post_context_save,
982 .try_suspend = __suspend2_try_suspend,
983 .try_resume = __suspend2_try_resume,
986 static __init int core_load(void)
988 int i,
989 numfiles = sizeof(sysfs_params) / sizeof(struct suspend_sysfs_data);
991 printk("Suspend v" SUSPEND_CORE_VERSION "\n");
993 if (s2_sysfs_init())
994 return 1;
996 for (i=0; i< numfiles; i++)
997 suspend_register_sysfs_file(&suspend2_subsys.kset.kobj,
998 &sysfs_params[i]);
1000 s2_core_fns = &my_fns;
1002 if (s2_checksum_init())
1003 return 1;
1004 if (s2_cluster_init())
1005 return 1;
1006 if (s2_usm_init())
1007 return 1;
1008 if (s2_ui_init())
1009 return 1;
1011 #ifdef CONFIG_SOFTWARE_SUSPEND
1012 /* Overriding resume2= with resume=? */
1013 if (test_action_state(SUSPEND_REPLACE_SWSUSP) && resume_file[0])
1014 strncpy(resume2_file, resume_file, 256);
1015 #endif
1017 return 0;
1020 #ifdef MODULE
1021 static __exit void core_unload(void)
1023 int i,
1024 numfiles = sizeof(sysfs_params) / sizeof(struct suspend_sysfs_data);
1026 s2_ui_exit();
1027 s2_checksum_exit();
1028 s2_cluster_exit();
1029 s2_usm_exit();
1031 for (i=0; i< numfiles; i++)
1032 suspend_unregister_sysfs_file(&suspend2_subsys.kset.kobj,
1033 &sysfs_params[i]);
1035 s2_core_fns = NULL;
1037 s2_sysfs_exit();
1039 MODULE_LICENSE("GPL");
1040 module_init(core_load);
1041 module_exit(core_unload);
1042 #else
1043 late_initcall(core_load);
1044 #endif
1046 #ifdef CONFIG_SUSPEND2_EXPORTS
1047 EXPORT_SYMBOL_GPL(pagedir2);
1048 #endif