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