]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/power/disk.c
[PATCH] swsusp: use block device offsets to identify swap locations
[net-next-2.6.git] / kernel / power / disk.c
CommitLineData
1da177e4
LT
1/*
2 * kernel/power/disk.c - 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@suse.cz>
7 *
8 * This file is released under the GPLv2.
9 *
10 */
11
12#include <linux/suspend.h>
13#include <linux/syscalls.h>
14#include <linux/reboot.h>
15#include <linux/string.h>
16#include <linux/device.h>
17#include <linux/delay.h>
18#include <linux/fs.h>
d53d9f16 19#include <linux/mount.h>
88d10bba 20#include <linux/pm.h>
97c7801c 21#include <linux/console.h>
e3920fb4 22#include <linux/cpu.h>
d53d9f16 23
1da177e4
LT
24#include "power.h"
25
26
1da177e4
LT
27static int noresume = 0;
28char resume_file[256] = CONFIG_PM_STD_PARTITION;
29dev_t swsusp_resume_device;
30
31/**
32 * power_down - Shut machine down for hibernate.
33 * @mode: Suspend-to-disk mode
34 *
35 * Use the platform driver, if configured so, and return gracefully if it
36 * fails.
37 * Otherwise, try to power off and reboot. If they fail, halt the machine,
38 * there ain't no turning back.
39 */
40
41static void power_down(suspend_disk_method_t mode)
42{
1da177e4
LT
43 int error = 0;
44
1da177e4
LT
45 switch(mode) {
46 case PM_DISK_PLATFORM:
729b4d4c 47 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
1da177e4
LT
48 error = pm_ops->enter(PM_SUSPEND_DISK);
49 break;
50 case PM_DISK_SHUTDOWN:
fdde86ac 51 kernel_power_off();
1da177e4
LT
52 break;
53 case PM_DISK_REBOOT:
fdde86ac 54 kernel_restart(NULL);
1da177e4
LT
55 break;
56 }
fdde86ac 57 kernel_halt();
1da177e4
LT
58 /* Valid image is on the disk, if we continue we risk serious data corruption
59 after resume. */
60 printk(KERN_CRIT "Please power me down manually\n");
61 while(1);
62}
63
1da177e4
LT
64static inline void platform_finish(void)
65{
66 if (pm_disk_mode == PM_DISK_PLATFORM) {
67 if (pm_ops && pm_ops->finish)
68 pm_ops->finish(PM_SUSPEND_DISK);
69 }
70}
71
1da177e4
LT
72static int prepare_processes(void)
73{
b918f6e6 74 int error = 0;
1da177e4
LT
75
76 pm_prepare_console();
e3920fb4
RW
77
78 error = disable_nonboot_cpus();
79 if (error)
80 goto enable_cpus;
5a72e04d 81
1da177e4
LT
82 if (freeze_processes()) {
83 error = -EBUSY;
5a72e04d 84 goto thaw;
1da177e4
LT
85 }
86
b918f6e6
RW
87 if (pm_disk_mode == PM_DISK_TESTPROC) {
88 printk("swsusp debug: Waiting for 5 seconds.\n");
89 mdelay(5000);
90 goto thaw;
91 }
92
1da177e4 93 /* Free memory before shutting down devices. */
72a97e08
RW
94 if (!(error = swsusp_shrink_memory()))
95 return 0;
5a72e04d
LS
96thaw:
97 thaw_processes();
e3920fb4 98enable_cpus:
5a72e04d
LS
99 enable_nonboot_cpus();
100 pm_restore_console();
101 return error;
1da177e4
LT
102}
103
104static void unprepare_processes(void)
105{
5a72e04d 106 platform_finish();
1da177e4 107 thaw_processes();
5a72e04d 108 enable_nonboot_cpus();
1da177e4
LT
109 pm_restore_console();
110}
111
1da177e4 112/**
f1cc0a89 113 * pm_suspend_disk - The granpappy of hibernation power management.
1da177e4
LT
114 *
115 * If we're going through the firmware, then get it over with quickly.
116 *
117 * If not, then call swsusp to do its thing, then figure out how
118 * to power down the system.
119 */
120
121int pm_suspend_disk(void)
122{
123 int error;
124
125 error = prepare_processes();
5a72e04d
LS
126 if (error)
127 return error;
1da177e4 128
b918f6e6
RW
129 if (pm_disk_mode == PM_DISK_TESTPROC)
130 goto Thaw;
131
97c7801c 132 suspend_console();
99dc7d63 133 error = device_suspend(PMSG_FREEZE);
1da177e4 134 if (error) {
97c7801c 135 resume_console();
99dc7d63 136 printk("Some devices failed to suspend\n");
b918f6e6
RW
137 goto Thaw;
138 }
139
140 if (pm_disk_mode == PM_DISK_TEST) {
141 printk("swsusp debug: Waiting for 5 seconds.\n");
142 mdelay(5000);
143 goto Done;
1da177e4
LT
144 }
145
1da177e4
LT
146 pr_debug("PM: snapshotting memory.\n");
147 in_suspend = 1;
148 if ((error = swsusp_suspend()))
149 goto Done;
150
151 if (in_suspend) {
0245b3e7 152 device_resume();
97c7801c 153 resume_console();
1da177e4 154 pr_debug("PM: writing image.\n");
f577eb30 155 error = swsusp_write();
1da177e4
LT
156 if (!error)
157 power_down(pm_disk_mode);
99dc7d63 158 else {
99dc7d63 159 swsusp_free();
b918f6e6 160 goto Thaw;
99dc7d63 161 }
b918f6e6 162 } else {
1da177e4 163 pr_debug("PM: Image restored successfully.\n");
b918f6e6 164 }
99dc7d63 165
1da177e4
LT
166 swsusp_free();
167 Done:
99dc7d63 168 device_resume();
97c7801c 169 resume_console();
b918f6e6 170 Thaw:
99dc7d63 171 unprepare_processes();
1da177e4
LT
172 return error;
173}
174
175
176/**
177 * software_resume - Resume from a saved image.
178 *
179 * Called as a late_initcall (so all devices are discovered and
180 * initialized), we call swsusp to see if we have a saved image or not.
181 * If so, we quiesce devices, the restore the saved image. We will
182 * return above (in pm_suspend_disk() ) if everything goes well.
183 * Otherwise, we fail gracefully and return to the normally
184 * scheduled program.
185 *
186 */
187
188static int software_resume(void)
189{
190 int error;
191
dd5d666b 192 down(&pm_sem);
3efa147a 193 if (!swsusp_resume_device) {
dd5d666b
SL
194 if (!strlen(resume_file)) {
195 up(&pm_sem);
3efa147a 196 return -ENOENT;
dd5d666b 197 }
3efa147a
PM
198 swsusp_resume_device = name_to_dev_t(resume_file);
199 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
200 } else {
201 pr_debug("swsusp: Resume From Partition %d:%d\n",
202 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
203 }
204
1da177e4
LT
205 if (noresume) {
206 /**
207 * FIXME: If noresume is specified, we need to find the partition
208 * and reset it back to normal swap space.
209 */
dd5d666b 210 up(&pm_sem);
1da177e4
LT
211 return 0;
212 }
213
214 pr_debug("PM: Checking swsusp image.\n");
215
216 if ((error = swsusp_check()))
217 goto Done;
218
219 pr_debug("PM: Preparing processes for restore.\n");
220
221 if ((error = prepare_processes())) {
222 swsusp_close();
5a72e04d 223 goto Done;
1da177e4
LT
224 }
225
226 pr_debug("PM: Reading swsusp image.\n");
227
f577eb30 228 if ((error = swsusp_read())) {
2c1b4a5c
RW
229 swsusp_free();
230 goto Thaw;
231 }
1da177e4
LT
232
233 pr_debug("PM: Preparing devices for restore.\n");
234
97c7801c 235 suspend_console();
f1cc0a89 236 if ((error = device_suspend(PMSG_PRETHAW))) {
97c7801c 237 resume_console();
99dc7d63 238 printk("Some devices failed to suspend\n");
2c1b4a5c
RW
239 swsusp_free();
240 goto Thaw;
99dc7d63 241 }
1da177e4
LT
242
243 mb();
244
245 pr_debug("PM: Restoring saved image.\n");
246 swsusp_resume();
247 pr_debug("PM: Restore failed, recovering.n");
99dc7d63 248 device_resume();
97c7801c 249 resume_console();
2c1b4a5c 250 Thaw:
1da177e4
LT
251 unprepare_processes();
252 Done:
dd5d666b
SL
253 /* For success case, the suspend path will release the lock */
254 up(&pm_sem);
1da177e4
LT
255 pr_debug("PM: Resume from disk failed.\n");
256 return 0;
257}
258
259late_initcall(software_resume);
260
261
3b364b8d 262static const char * const pm_disk_modes[] = {
1da177e4
LT
263 [PM_DISK_FIRMWARE] = "firmware",
264 [PM_DISK_PLATFORM] = "platform",
265 [PM_DISK_SHUTDOWN] = "shutdown",
266 [PM_DISK_REBOOT] = "reboot",
b918f6e6
RW
267 [PM_DISK_TEST] = "test",
268 [PM_DISK_TESTPROC] = "testproc",
1da177e4
LT
269};
270
271/**
272 * disk - Control suspend-to-disk mode
273 *
274 * Suspend-to-disk can be handled in several ways. The greatest
275 * distinction is who writes memory to disk - the firmware or the OS.
276 * If the firmware does it, we assume that it also handles suspending
277 * the system.
278 * If the OS does it, then we have three options for putting the system
279 * to sleep - using the platform driver (e.g. ACPI or other PM registers),
280 * powering off the system or rebooting the system (for testing).
281 *
282 * The system will support either 'firmware' or 'platform', and that is
283 * known a priori (and encoded in pm_ops). But, the user may choose
284 * 'shutdown' or 'reboot' as alternatives.
285 *
286 * show() will display what the mode is currently set to.
287 * store() will accept one of
288 *
289 * 'firmware'
290 * 'platform'
291 * 'shutdown'
292 * 'reboot'
293 *
294 * It will only change to 'firmware' or 'platform' if the system
295 * supports it (as determined from pm_ops->pm_disk_mode).
296 */
297
298static ssize_t disk_show(struct subsystem * subsys, char * buf)
299{
300 return sprintf(buf, "%s\n", pm_disk_modes[pm_disk_mode]);
301}
302
303
304static ssize_t disk_store(struct subsystem * s, const char * buf, size_t n)
305{
306 int error = 0;
307 int i;
308 int len;
309 char *p;
310 suspend_disk_method_t mode = 0;
311
312 p = memchr(buf, '\n', n);
313 len = p ? p - buf : n;
314
315 down(&pm_sem);
316 for (i = PM_DISK_FIRMWARE; i < PM_DISK_MAX; i++) {
317 if (!strncmp(buf, pm_disk_modes[i], len)) {
318 mode = i;
319 break;
320 }
321 }
322 if (mode) {
b918f6e6
RW
323 if (mode == PM_DISK_SHUTDOWN || mode == PM_DISK_REBOOT ||
324 mode == PM_DISK_TEST || mode == PM_DISK_TESTPROC) {
1da177e4 325 pm_disk_mode = mode;
b918f6e6 326 } else {
1da177e4
LT
327 if (pm_ops && pm_ops->enter &&
328 (mode == pm_ops->pm_disk_mode))
329 pm_disk_mode = mode;
330 else
331 error = -EINVAL;
332 }
b918f6e6 333 } else {
1da177e4 334 error = -EINVAL;
b918f6e6 335 }
1da177e4
LT
336
337 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
338 pm_disk_modes[mode]);
339 up(&pm_sem);
340 return error ? error : n;
341}
342
343power_attr(disk);
344
345static ssize_t resume_show(struct subsystem * subsys, char *buf)
346{
347 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
348 MINOR(swsusp_resume_device));
349}
350
a576219a 351static ssize_t resume_store(struct subsystem *subsys, const char *buf, size_t n)
1da177e4 352{
1da177e4 353 unsigned int maj, min;
1da177e4 354 dev_t res;
a576219a 355 int ret = -EINVAL;
1da177e4 356
a576219a
AM
357 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
358 goto out;
1da177e4 359
a576219a
AM
360 res = MKDEV(maj,min);
361 if (maj != MAJOR(res) || min != MINOR(res))
362 goto out;
1da177e4 363
a576219a
AM
364 down(&pm_sem);
365 swsusp_resume_device = res;
366 up(&pm_sem);
367 printk("Attempting manual resume\n");
368 noresume = 0;
369 software_resume();
370 ret = n;
371out:
372 return ret;
1da177e4
LT
373}
374
375power_attr(resume);
376
ca0aec0f
RW
377static ssize_t image_size_show(struct subsystem * subsys, char *buf)
378{
853609b6 379 return sprintf(buf, "%lu\n", image_size);
ca0aec0f
RW
380}
381
382static ssize_t image_size_store(struct subsystem * subsys, const char * buf, size_t n)
383{
853609b6 384 unsigned long size;
ca0aec0f 385
853609b6 386 if (sscanf(buf, "%lu", &size) == 1) {
ca0aec0f
RW
387 image_size = size;
388 return n;
389 }
390
391 return -EINVAL;
392}
393
394power_attr(image_size);
395
1da177e4
LT
396static struct attribute * g[] = {
397 &disk_attr.attr,
398 &resume_attr.attr,
ca0aec0f 399 &image_size_attr.attr,
1da177e4
LT
400 NULL,
401};
402
403
404static struct attribute_group attr_group = {
405 .attrs = g,
406};
407
408
409static int __init pm_disk_init(void)
410{
411 return sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
412}
413
414core_initcall(pm_disk_init);
415
416
417static int __init resume_setup(char *str)
418{
419 if (noresume)
420 return 1;
421
422 strncpy( resume_file, str, 255 );
423 return 1;
424}
425
426static int __init noresume_setup(char *str)
427{
428 noresume = 1;
429 return 1;
430}
431
432__setup("noresume", noresume_setup);
433__setup("resume=", resume_setup);