blob: a456ffe7a3c84084c48205a6eb20b51a10cb3bd4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/power/swsusp.c
3 *
Pavel Machek96bc7ae2005-10-30 14:59:58 -08004 * This file provides code to write suspend image to swap and read it back.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08007 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This file is released under the GPLv2.
10 *
11 * I'd like to thank the following people for their work:
Pavel Machek2e4d5822005-06-25 14:55:12 -070012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Pavel Machek <pavel@ucw.cz>:
14 * Modifications, defectiveness pointing, being with me at the very beginning,
15 * suspend to swap space, stop all tasks. Port to 2.4.18-ac and 2.5.17.
16 *
Pavel Machek2e4d5822005-06-25 14:55:12 -070017 * Steve Doddi <dirk@loth.demon.co.uk>:
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * Support the possibility of hardware state restoring.
19 *
20 * Raph <grey.havens@earthling.net>:
21 * Support for preserving states of network devices and virtual console
22 * (including X and svgatextmode)
23 *
24 * Kurt Garloff <garloff@suse.de>:
25 * Straightened the critical function in order to prevent compilers from
26 * playing tricks with local variables.
27 *
28 * Andreas Mohr <a.mohr@mailto.de>
29 *
30 * Alex Badea <vampire@go.ro>:
31 * Fixed runaway init
32 *
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -070033 * Andreas Steinmetz <ast@domdv.de>:
34 * Added encrypted suspend option
35 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * More state savers are welcome. Especially for the scsi layer...
37 *
38 * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
39 */
40
41#include <linux/module.h>
42#include <linux/mm.h>
43#include <linux/suspend.h>
44#include <linux/smp_lock.h>
45#include <linux/file.h>
46#include <linux/utsname.h>
47#include <linux/version.h>
48#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/spinlock.h>
51#include <linux/genhd.h>
52#include <linux/kernel.h>
53#include <linux/major.h>
54#include <linux/swap.h>
55#include <linux/pm.h>
56#include <linux/device.h>
57#include <linux/buffer_head.h>
58#include <linux/swapops.h>
59#include <linux/bootmem.h>
60#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/highmem.h>
62#include <linux/bio.h>
63
64#include <asm/uaccess.h>
65#include <asm/mmu_context.h>
66#include <asm/pgtable.h>
67#include <asm/tlbflush.h>
68#include <asm/io.h>
69
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -070070#include <linux/random.h>
71#include <linux/crypto.h>
72#include <asm/scatterlist.h>
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include "power.h"
75
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -070076#define CIPHER "aes"
77#define MAXKEY 32
78#define MAXIV 32
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080extern char resume_file[];
81
82/* Local variables that should not be affected by save */
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080083unsigned int nr_copy_pages __nosavedata = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/* Suspend pagedir is allocated before final copy, therefore it
Pavel Machek2e4d5822005-06-25 14:55:12 -070086 must be freed after resume
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 Warning: this is even more evil than it seems. Pagedirs this file
89 talks about are completely different from page directories used by
90 MMU hardware.
91 */
92suspend_pagedir_t *pagedir_nosave __nosavedata = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94#define SWSUSP_SIG "S1SUSPEND"
95
96static struct swsusp_header {
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -070097 char reserved[PAGE_SIZE - 20 - MAXKEY - MAXIV - sizeof(swp_entry_t)];
98 u8 key_iv[MAXKEY+MAXIV];
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 swp_entry_t swsusp_info;
100 char orig_sig[10];
101 char sig[10];
102} __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
103
104static struct swsusp_info swsusp_info;
105
106/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * Saving part...
108 */
109
110/* We memorize in swapfile_used what swap devices are used for suspension */
111#define SWAPFILE_UNUSED 0
112#define SWAPFILE_SUSPEND 1 /* This is the suspending device */
113#define SWAPFILE_IGNORED 2 /* Those are other swap devices ignored for suspension */
114
115static unsigned short swapfile_used[MAX_SWAPFILES];
116static unsigned short root_swap;
117
Pavel Machekdc19d502005-11-07 00:58:40 -0800118static int write_page(unsigned long addr, swp_entry_t *loc);
119static int bio_read_page(pgoff_t page_off, void *page);
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700120
121static u8 key_iv[MAXKEY+MAXIV];
122
123#ifdef CONFIG_SWSUSP_ENCRYPT
124
125static int crypto_init(int mode, void **mem)
126{
127 int error = 0;
128 int len;
129 char *modemsg;
130 struct crypto_tfm *tfm;
131
132 modemsg = mode ? "suspend not possible" : "resume not possible";
133
134 tfm = crypto_alloc_tfm(CIPHER, CRYPTO_TFM_MODE_CBC);
135 if(!tfm) {
136 printk(KERN_ERR "swsusp: no tfm, %s\n", modemsg);
137 error = -EINVAL;
138 goto out;
139 }
140
141 if(MAXKEY < crypto_tfm_alg_min_keysize(tfm)) {
142 printk(KERN_ERR "swsusp: key buffer too small, %s\n", modemsg);
143 error = -ENOKEY;
144 goto fail;
145 }
146
147 if (mode)
148 get_random_bytes(key_iv, MAXKEY+MAXIV);
149
150 len = crypto_tfm_alg_max_keysize(tfm);
151 if (len > MAXKEY)
152 len = MAXKEY;
153
154 if (crypto_cipher_setkey(tfm, key_iv, len)) {
155 printk(KERN_ERR "swsusp: key setup failure, %s\n", modemsg);
156 error = -EKEYREJECTED;
157 goto fail;
158 }
159
160 len = crypto_tfm_alg_ivsize(tfm);
161
162 if (MAXIV < len) {
163 printk(KERN_ERR "swsusp: iv buffer too small, %s\n", modemsg);
164 error = -EOVERFLOW;
165 goto fail;
166 }
167
168 crypto_cipher_set_iv(tfm, key_iv+MAXKEY, len);
169
170 *mem=(void *)tfm;
171
172 goto out;
173
174fail: crypto_free_tfm(tfm);
175out: return error;
176}
177
178static __inline__ void crypto_exit(void *mem)
179{
180 crypto_free_tfm((struct crypto_tfm *)mem);
181}
182
183static __inline__ int crypto_write(struct pbe *p, void *mem)
184{
185 int error = 0;
186 struct scatterlist src, dst;
187
188 src.page = virt_to_page(p->address);
189 src.offset = 0;
190 src.length = PAGE_SIZE;
191 dst.page = virt_to_page((void *)&swsusp_header);
192 dst.offset = 0;
193 dst.length = PAGE_SIZE;
194
195 error = crypto_cipher_encrypt((struct crypto_tfm *)mem, &dst, &src,
196 PAGE_SIZE);
197
198 if (!error)
199 error = write_page((unsigned long)&swsusp_header,
200 &(p->swap_address));
201 return error;
202}
203
204static __inline__ int crypto_read(struct pbe *p, void *mem)
205{
206 int error = 0;
207 struct scatterlist src, dst;
208
209 error = bio_read_page(swp_offset(p->swap_address), (void *)p->address);
210 if (!error) {
211 src.offset = 0;
212 src.length = PAGE_SIZE;
213 dst.offset = 0;
214 dst.length = PAGE_SIZE;
215 src.page = dst.page = virt_to_page((void *)p->address);
216
217 error = crypto_cipher_decrypt((struct crypto_tfm *)mem, &dst,
218 &src, PAGE_SIZE);
219 }
220 return error;
221}
222#else
223static __inline__ int crypto_init(int mode, void *mem)
224{
225 return 0;
226}
227
228static __inline__ void crypto_exit(void *mem)
229{
230}
231
232static __inline__ int crypto_write(struct pbe *p, void *mem)
233{
234 return write_page(p->address, &(p->swap_address));
235}
236
237static __inline__ int crypto_read(struct pbe *p, void *mem)
238{
239 return bio_read_page(swp_offset(p->swap_address), (void *)p->address);
240}
241#endif
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243static int mark_swapfiles(swp_entry_t prev)
244{
245 int error;
246
Pavel Machek2e4d5822005-06-25 14:55:12 -0700247 rw_swap_page_sync(READ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 swp_entry(root_swap, 0),
249 virt_to_page((unsigned long)&swsusp_header));
250 if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
251 !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
252 memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
253 memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700254 memcpy(swsusp_header.key_iv, key_iv, MAXKEY+MAXIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 swsusp_header.swsusp_info = prev;
Pavel Machek2e4d5822005-06-25 14:55:12 -0700256 error = rw_swap_page_sync(WRITE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 swp_entry(root_swap, 0),
258 virt_to_page((unsigned long)
259 &swsusp_header));
260 } else {
261 pr_debug("swsusp: Partition is not swap space.\n");
262 error = -ENODEV;
263 }
264 return error;
265}
266
267/*
268 * Check whether the swap device is the specified resume
269 * device, irrespective of whether they are specified by
270 * identical names.
271 *
272 * (Thus, device inode aliasing is allowed. You can say /dev/hda4
273 * instead of /dev/ide/host0/bus0/target0/lun0/part4 [if using devfs]
274 * and they'll be considered the same device. This is *necessary* for
275 * devfs, since the resume code can only recognize the form /dev/hda4,
276 * but the suspend code would see the long name.)
277 */
278static int is_resume_device(const struct swap_info_struct *swap_info)
279{
280 struct file *file = swap_info->swap_file;
281 struct inode *inode = file->f_dentry->d_inode;
282
283 return S_ISBLK(inode->i_mode) &&
284 swsusp_resume_device == MKDEV(imajor(inode), iminor(inode));
285}
286
287static int swsusp_swap_check(void) /* This is called before saving image */
288{
289 int i, len;
Pavel Machek2e4d5822005-06-25 14:55:12 -0700290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 len=strlen(resume_file);
292 root_swap = 0xFFFF;
Pavel Machek2e4d5822005-06-25 14:55:12 -0700293
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700294 spin_lock(&swap_lock);
Pavel Machek2e4d5822005-06-25 14:55:12 -0700295 for (i=0; i<MAX_SWAPFILES; i++) {
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700296 if (!(swap_info[i].flags & SWP_WRITEOK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 swapfile_used[i]=SWAPFILE_UNUSED;
298 } else {
Pavel Machek2e4d5822005-06-25 14:55:12 -0700299 if (!len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 printk(KERN_WARNING "resume= option should be used to set suspend device" );
Pavel Machek2e4d5822005-06-25 14:55:12 -0700301 if (root_swap == 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 swapfile_used[i] = SWAPFILE_SUSPEND;
303 root_swap = i;
304 } else
Pavel Machek2e4d5822005-06-25 14:55:12 -0700305 swapfile_used[i] = SWAPFILE_IGNORED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 } else {
307 /* we ignore all swap devices that are not the resume_file */
308 if (is_resume_device(&swap_info[i])) {
309 swapfile_used[i] = SWAPFILE_SUSPEND;
310 root_swap = i;
311 } else {
312 swapfile_used[i] = SWAPFILE_IGNORED;
313 }
314 }
315 }
316 }
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700317 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return (root_swap != 0xffff) ? 0 : -ENODEV;
319}
320
321/**
322 * This is called after saving image so modification
323 * will be lost after resume... and that's what we want.
324 * we make the device unusable. A new call to
Pavel Machek2e4d5822005-06-25 14:55:12 -0700325 * lock_swapdevices can unlock the devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
327static void lock_swapdevices(void)
328{
329 int i;
330
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700331 spin_lock(&swap_lock);
Pavel Machek2e4d5822005-06-25 14:55:12 -0700332 for (i = 0; i< MAX_SWAPFILES; i++)
333 if (swapfile_used[i] == SWAPFILE_IGNORED) {
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700334 swap_info[i].flags ^= SWP_WRITEOK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700336 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339/**
Pavel Machek8686bcd2005-09-22 21:44:11 -0700340 * write_page - Write one page to a fresh swap location.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 * @addr: Address we're writing.
342 * @loc: Place to store the entry we used.
343 *
344 * Allocate a new swap entry and 'sync' it. Note we discard -EIO
Pavel Machek2e4d5822005-06-25 14:55:12 -0700345 * errors. That is an artifact left over from swsusp. It did not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 * check the return of rw_swap_page_sync() at all, since most pages
347 * written back to swap would return -EIO.
348 * This is a partial improvement, since we will at least return other
349 * errors, though we need to eventually fix the damn code.
350 */
Pavel Machekdc19d502005-11-07 00:58:40 -0800351static int write_page(unsigned long addr, swp_entry_t *loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 swp_entry_t entry;
354 int error = 0;
355
356 entry = get_swap_page();
Pavel Machek2e4d5822005-06-25 14:55:12 -0700357 if (swp_offset(entry) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 swapfile_used[swp_type(entry)] == SWAPFILE_SUSPEND) {
359 error = rw_swap_page_sync(WRITE, entry,
360 virt_to_page(addr));
361 if (error == -EIO)
362 error = 0;
363 if (!error)
364 *loc = entry;
365 } else
366 error = -ENOSPC;
367 return error;
368}
369
370/**
371 * data_free - Free the swap entries used by the saved image.
372 *
Pavel Machek2e4d5822005-06-25 14:55:12 -0700373 * Walk the list of used swap entries and free each one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * This is only used for cleanup when suspend fails.
375 */
376static void data_free(void)
377{
378 swp_entry_t entry;
Pavel Machekdc19d502005-11-07 00:58:40 -0800379 struct pbe *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Pavel Machek47b90ff2005-11-07 00:58:41 -0800381 for_each_pbe (p, pagedir_nosave) {
Rafael J. Wysocki254b5472005-09-27 21:45:31 -0700382 entry = p->swap_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (entry.val)
384 swap_free(entry);
385 else
386 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388}
389
390/**
391 * data_write - Write saved image to swap.
392 *
393 * Walk the list of pages in the image and sync each one to swap.
394 */
395static int data_write(void)
396{
397 int error = 0, i = 0;
398 unsigned int mod = nr_copy_pages / 100;
399 struct pbe *p;
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700400 void *tfm;
401
402 if ((error = crypto_init(1, &tfm)))
403 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if (!mod)
406 mod = 1;
407
408 printk( "Writing data to swap (%d pages)... ", nr_copy_pages );
Pavel Machek2e4d5822005-06-25 14:55:12 -0700409 for_each_pbe (p, pagedir_nosave) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (!(i%mod))
411 printk( "\b\b\b\b%3d%%", i / mod );
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700412 if ((error = crypto_write(p, tfm))) {
413 crypto_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return error;
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 i++;
417 }
418 printk("\b\b\b\bdone\n");
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700419 crypto_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return error;
421}
422
423static void dump_info(void)
424{
425 pr_debug(" swsusp: Version: %u\n",swsusp_info.version_code);
426 pr_debug(" swsusp: Num Pages: %ld\n",swsusp_info.num_physpages);
427 pr_debug(" swsusp: UTS Sys: %s\n",swsusp_info.uts.sysname);
428 pr_debug(" swsusp: UTS Node: %s\n",swsusp_info.uts.nodename);
429 pr_debug(" swsusp: UTS Release: %s\n",swsusp_info.uts.release);
430 pr_debug(" swsusp: UTS Version: %s\n",swsusp_info.uts.version);
431 pr_debug(" swsusp: UTS Machine: %s\n",swsusp_info.uts.machine);
432 pr_debug(" swsusp: UTS Domain: %s\n",swsusp_info.uts.domainname);
433 pr_debug(" swsusp: CPUs: %d\n",swsusp_info.cpus);
434 pr_debug(" swsusp: Image: %ld Pages\n",swsusp_info.image_pages);
435 pr_debug(" swsusp: Pagedir: %ld Pages\n",swsusp_info.pagedir_pages);
436}
437
438static void init_header(void)
439{
440 memset(&swsusp_info, 0, sizeof(swsusp_info));
441 swsusp_info.version_code = LINUX_VERSION_CODE;
442 swsusp_info.num_physpages = num_physpages;
443 memcpy(&swsusp_info.uts, &system_utsname, sizeof(system_utsname));
444
445 swsusp_info.suspend_pagedir = pagedir_nosave;
446 swsusp_info.cpus = num_online_cpus();
447 swsusp_info.image_pages = nr_copy_pages;
448}
449
450static int close_swap(void)
451{
452 swp_entry_t entry;
453 int error;
454
455 dump_info();
456 error = write_page((unsigned long)&swsusp_info, &entry);
Pavel Machek2e4d5822005-06-25 14:55:12 -0700457 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 printk( "S" );
459 error = mark_swapfiles(entry);
460 printk( "|\n" );
461 }
462 return error;
463}
464
465/**
466 * free_pagedir_entries - Free pages used by the page directory.
467 *
468 * This is used during suspend for error recovery.
469 */
470
471static void free_pagedir_entries(void)
472{
473 int i;
474
475 for (i = 0; i < swsusp_info.pagedir_pages; i++)
476 swap_free(swsusp_info.pagedir[i]);
477}
478
479
480/**
481 * write_pagedir - Write the array of pages holding the page directory.
482 * @last: Last swap entry we write (needed for header).
483 */
484
485static int write_pagedir(void)
486{
487 int error = 0;
Pavel Machekdc19d502005-11-07 00:58:40 -0800488 unsigned int n = 0;
489 struct pbe *pbe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 printk( "Writing pagedir...");
Pavel Machek2e4d5822005-06-25 14:55:12 -0700492 for_each_pb_page (pbe, pagedir_nosave) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if ((error = write_page((unsigned long)pbe, &swsusp_info.pagedir[n++])))
494 return error;
495 }
496
497 swsusp_info.pagedir_pages = n;
498 printk("done (%u pages)\n", n);
499 return error;
500}
501
502/**
503 * write_suspend_image - Write entire image and metadata.
504 *
505 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506static int write_suspend_image(void)
507{
508 int error;
509
510 init_header();
511 if ((error = data_write()))
512 goto FreeData;
513
514 if ((error = write_pagedir()))
515 goto FreePagedir;
516
517 if ((error = close_swap()))
518 goto FreePagedir;
519 Done:
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700520 memset(key_iv, 0, MAXKEY+MAXIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return error;
522 FreePagedir:
523 free_pagedir_entries();
524 FreeData:
525 data_free();
526 goto Done;
527}
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529/**
530 * enough_swap - Make sure we have enough swap to save the image.
531 *
Pavel Machek2e4d5822005-06-25 14:55:12 -0700532 * Returns TRUE or FALSE after checking the total amount of swap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 * space avaiable.
534 *
535 * FIXME: si_swapinfo(&i) returns all swap devices information.
Pavel Machek2e4d5822005-06-25 14:55:12 -0700536 * We should only consider resume_device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 */
538
Pavel Machekdc19d502005-11-07 00:58:40 -0800539int enough_swap(unsigned int nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 struct sysinfo i;
542
543 si_swapinfo(&i);
Rafael J. Wysocki351619b2005-10-30 14:59:55 -0800544 pr_debug("swsusp: available swap: %lu pages\n", i.freeswap);
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800545 return i.freeswap > (nr_pages + PAGES_FOR_IO +
546 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550/* It is important _NOT_ to umount filesystems at this point. We want
551 * them synced (in case something goes wrong) but we DO not want to mark
552 * filesystem clean: it is not. (And it does not matter, if we resume
553 * correctly, we'll mark system clean, anyway.)
554 */
555int swsusp_write(void)
556{
557 int error;
Rafael J. Wysocki0245b3e2005-10-30 15:00:01 -0800558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 lock_swapdevices();
560 error = write_suspend_image();
561 /* This will unlock ignored swap devices since writing is finished */
562 lock_swapdevices();
563 return error;
564
565}
566
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569int swsusp_suspend(void)
570{
571 int error;
572 if ((error = arch_prepare_suspend()))
573 return error;
574 local_irq_disable();
575 /* At this point, device_suspend() has been called, but *not*
576 * device_power_down(). We *must* device_power_down() now.
577 * Otherwise, drivers for some devices (e.g. interrupt controllers)
578 * become desynchronized with the actual state of the hardware
579 * at resume time, and evil weirdness ensues.
580 */
581 if ((error = device_power_down(PMSG_FREEZE))) {
Pavel Machek99dc7d62005-09-03 15:57:05 -0700582 printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return error;
585 }
Pavel Machek47b724f2005-07-07 17:56:44 -0700586
587 if ((error = swsusp_swap_check())) {
Pavel Machek99dc7d62005-09-03 15:57:05 -0700588 printk(KERN_ERR "swsusp: cannot find swap device, try swapon -a.\n");
589 device_power_up();
Pavel Machek47b724f2005-07-07 17:56:44 -0700590 local_irq_enable();
591 return error;
592 }
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 save_processor_state();
595 if ((error = swsusp_arch_suspend()))
Pavel Machek99dc7d62005-09-03 15:57:05 -0700596 printk(KERN_ERR "Error %d suspending\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 /* Restore control flow magically appears here */
598 restore_processor_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 restore_highmem();
600 device_power_up();
601 local_irq_enable();
602 return error;
603}
604
605int swsusp_resume(void)
606{
607 int error;
608 local_irq_disable();
609 if (device_power_down(PMSG_FREEZE))
610 printk(KERN_ERR "Some devices failed to power down, very bad\n");
611 /* We'll ignore saved state, but this gets preempt count (etc) right */
612 save_processor_state();
613 error = swsusp_arch_resume();
614 /* Code below is only ever reached in case of failure. Otherwise
615 * execution continues at place where swsusp_arch_suspend was called
616 */
617 BUG_ON(!error);
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800618 /* The only reason why swsusp_arch_resume() can fail is memory being
619 * very tight, so we have to free it as soon as we can to avoid
620 * subsequent failures
621 */
622 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 restore_processor_state();
624 restore_highmem();
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700625 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 device_power_up();
627 local_irq_enable();
628 return error;
629}
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 * swsusp_pagedir_relocate - It is possible, that some memory pages
633 * occupied by the list of PBEs collide with pages where we're going to
634 * restore from the loaded pages later. We relocate them here.
635 */
636
Pavel Machekdc19d502005-11-07 00:58:40 -0800637static struct pbe *swsusp_pagedir_relocate(struct pbe *pblist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 struct zone *zone;
640 unsigned long zone_pfn;
641 struct pbe *pbpage, *tail, *p;
642 void *m;
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800643 int rel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 if (!pblist) /* a sanity check */
646 return NULL;
647
648 pr_debug("swsusp: Relocating pagedir (%lu pages to check)\n",
649 swsusp_info.pagedir_pages);
650
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800651 /* Clear page flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Pavel Machek2e4d5822005-06-25 14:55:12 -0700653 for_each_zone (zone) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800655 if (pfn_valid(zone_pfn + zone->zone_start_pfn))
656 ClearPageNosaveFree(pfn_to_page(zone_pfn +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 zone->zone_start_pfn));
658 }
659
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800660 /* Mark orig addresses */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 for_each_pbe (p, pblist)
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800663 SetPageNosaveFree(virt_to_page(p->orig_address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 tail = pblist + PB_PAGE_SKIP;
666
667 /* Relocate colliding pages */
668
669 for_each_pb_page (pbpage, pblist) {
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800670 if (PageNosaveFree(virt_to_page((unsigned long)pbpage))) {
671 m = (void *)get_safe_page(GFP_ATOMIC | __GFP_COLD);
672 if (!m)
673 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 memcpy(m, (void *)pbpage, PAGE_SIZE);
675 if (pbpage == pblist)
676 pblist = (struct pbe *)m;
677 else
678 tail->next = (struct pbe *)m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 pbpage = (struct pbe *)m;
680
681 /* We have to link the PBEs again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 for (p = pbpage; p < pbpage + PB_PAGE_SKIP; p++)
683 if (p->next) /* needed to save the end */
684 p->next = p + 1;
685
686 rel++;
687 }
688 tail = pbpage + PB_PAGE_SKIP;
689 }
690
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800691 /* This is for swsusp_free() */
692 for_each_pb_page (pbpage, pblist) {
693 SetPageNosave(virt_to_page(pbpage));
694 SetPageNosaveFree(virt_to_page(pbpage));
695 }
696
697 printk("swsusp: Relocated %d pages\n", rel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 return pblist;
700}
701
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700702/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 * Using bio to read from swap.
704 * This code requires a bit more work than just using buffer heads
705 * but, it is the recommended way for 2.5/2.6.
706 * The following are to signal the beginning and end of I/O. Bios
707 * finish asynchronously, while we want them to happen synchronously.
708 * A simple atomic_t, and a wait loop take care of this problem.
709 */
710
711static atomic_t io_done = ATOMIC_INIT(0);
712
Pavel Machekdc19d502005-11-07 00:58:40 -0800713static int end_io(struct bio *bio, unsigned int num, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
716 panic("I/O error reading memory image");
717 atomic_set(&io_done, 0);
718 return 0;
719}
720
Pavel Machekdc19d502005-11-07 00:58:40 -0800721static struct block_device *resume_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723/**
724 * submit - submit BIO request.
725 * @rw: READ or WRITE.
726 * @off physical offset of page.
727 * @page: page we're reading or writing.
728 *
729 * Straight from the textbook - allocate and initialize the bio.
730 * If we're writing, make sure the page is marked as dirty.
731 * Then submit it and wait.
732 */
733
Pavel Machekdc19d502005-11-07 00:58:40 -0800734static int submit(int rw, pgoff_t page_off, void *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
736 int error = 0;
Pavel Machekdc19d502005-11-07 00:58:40 -0800737 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 bio = bio_alloc(GFP_ATOMIC, 1);
740 if (!bio)
741 return -ENOMEM;
742 bio->bi_sector = page_off * (PAGE_SIZE >> 9);
743 bio_get(bio);
744 bio->bi_bdev = resume_bdev;
745 bio->bi_end_io = end_io;
746
747 if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
748 printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
749 error = -EFAULT;
750 goto Done;
751 }
752
753 if (rw == WRITE)
754 bio_set_pages_dirty(bio);
755
756 atomic_set(&io_done, 1);
757 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
758 while (atomic_read(&io_done))
759 yield();
760
761 Done:
762 bio_put(bio);
763 return error;
764}
765
Pavel Machekdc19d502005-11-07 00:58:40 -0800766static int bio_read_page(pgoff_t page_off, void *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 return submit(READ, page_off, page);
769}
770
Pavel Machekdc19d502005-11-07 00:58:40 -0800771static int bio_write_page(pgoff_t page_off, void *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 return submit(WRITE, page_off, page);
774}
775
776/*
777 * Sanity check if this image makes sense with this kernel/swap context
778 * I really don't think that it's foolproof but more than nothing..
779 */
780
Pavel Machekdc19d502005-11-07 00:58:40 -0800781static const char *sanity_check(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 dump_info();
Pavel Machek47b724f2005-07-07 17:56:44 -0700784 if (swsusp_info.version_code != LINUX_VERSION_CODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return "kernel version";
Pavel Machek47b724f2005-07-07 17:56:44 -0700786 if (swsusp_info.num_physpages != num_physpages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return "memory size";
788 if (strcmp(swsusp_info.uts.sysname,system_utsname.sysname))
789 return "system type";
790 if (strcmp(swsusp_info.uts.release,system_utsname.release))
791 return "kernel release";
792 if (strcmp(swsusp_info.uts.version,system_utsname.version))
793 return "version";
794 if (strcmp(swsusp_info.uts.machine,system_utsname.machine))
795 return "machine";
Li Shaohua5a72e042005-06-25 14:55:06 -0700796#if 0
Pavel Machek99dc7d62005-09-03 15:57:05 -0700797 /* We can't use number of online CPUs when we use hotplug to remove them ;-))) */
798 if (swsusp_info.cpus != num_possible_cpus())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return "number of cpus";
Li Shaohua5a72e042005-06-25 14:55:06 -0700800#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return NULL;
802}
803
804
805static int check_header(void)
806{
Pavel Machekdc19d502005-11-07 00:58:40 -0800807 const char *reason = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 int error;
809
810 if ((error = bio_read_page(swp_offset(swsusp_header.swsusp_info), &swsusp_info)))
811 return error;
812
813 /* Is this same machine? */
814 if ((reason = sanity_check())) {
815 printk(KERN_ERR "swsusp: Resume mismatch: %s\n",reason);
816 return -EPERM;
817 }
818 nr_copy_pages = swsusp_info.image_pages;
819 return error;
820}
821
822static int check_sig(void)
823{
824 int error;
825
826 memset(&swsusp_header, 0, sizeof(swsusp_header));
827 if ((error = bio_read_page(0, &swsusp_header)))
828 return error;
829 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
830 memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700831 memcpy(key_iv, swsusp_header.key_iv, MAXKEY+MAXIV);
832 memset(swsusp_header.key_iv, 0, MAXKEY+MAXIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 /*
835 * Reset swap signature now.
836 */
837 error = bio_write_page(0, &swsusp_header);
838 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return -EINVAL;
840 }
841 if (!error)
842 pr_debug("swsusp: Signature found, resuming\n");
843 return error;
844}
845
846/**
847 * data_read - Read image pages from swap.
848 *
849 * You do not need to check for overlaps, check_pagedir()
850 * already did that.
851 */
852
853static int data_read(struct pbe *pblist)
854{
Pavel Machekdc19d502005-11-07 00:58:40 -0800855 struct pbe *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 int error = 0;
857 int i = 0;
858 int mod = swsusp_info.image_pages / 100;
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700859 void *tfm;
860
861 if ((error = crypto_init(0, &tfm)))
862 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 if (!mod)
865 mod = 1;
866
867 printk("swsusp: Reading image data (%lu pages): ",
868 swsusp_info.image_pages);
869
870 for_each_pbe (p, pblist) {
871 if (!(i % mod))
872 printk("\b\b\b\b%3d%%", i / mod);
873
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700874 if ((error = crypto_read(p, tfm))) {
875 crypto_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return error;
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 i++;
880 }
881 printk("\b\b\b\bdone\n");
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700882 crypto_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return error;
884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886/**
887 * read_pagedir - Read page backup list pages from swap
888 */
889
890static int read_pagedir(struct pbe *pblist)
891{
892 struct pbe *pbpage, *p;
Pavel Machekdc19d502005-11-07 00:58:40 -0800893 unsigned int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 int error;
895
896 if (!pblist)
897 return -EFAULT;
898
899 printk("swsusp: Reading pagedir (%lu pages)\n",
900 swsusp_info.pagedir_pages);
901
902 for_each_pb_page (pbpage, pblist) {
903 unsigned long offset = swp_offset(swsusp_info.pagedir[i++]);
904
905 error = -EFAULT;
906 if (offset) {
907 p = (pbpage + PB_PAGE_SKIP)->next;
908 error = bio_read_page(offset, (void *)pbpage);
909 (pbpage + PB_PAGE_SKIP)->next = p;
910 }
911 if (error)
912 break;
913 }
914
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800915 if (!error)
Rafael J. Wysockif2d61372005-09-27 21:45:32 -0700916 BUG_ON(i != swsusp_info.pagedir_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 return error;
919}
920
921
922static int check_suspend_image(void)
923{
924 int error = 0;
925
926 if ((error = check_sig()))
927 return error;
928
929 if ((error = check_header()))
930 return error;
931
932 return 0;
933}
934
935static int read_suspend_image(void)
936{
937 int error = 0;
938 struct pbe *p;
939
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800940 if (!(p = alloc_pagedir(nr_copy_pages, GFP_ATOMIC, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return -ENOMEM;
942
943 if ((error = read_pagedir(p)))
944 return error;
945
946 create_pbe_list(p, nr_copy_pages);
947
948 if (!(pagedir_nosave = swsusp_pagedir_relocate(p)))
949 return -ENOMEM;
950
951 /* Allocate memory for the image and read the data from swap */
952
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800953 error = alloc_data_pages(pagedir_nosave, GFP_ATOMIC, 1);
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (!error)
956 error = data_read(pagedir_nosave);
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return error;
959}
960
961/**
962 * swsusp_check - Check for saved image in swap
963 */
964
965int swsusp_check(void)
966{
967 int error;
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
970 if (!IS_ERR(resume_bdev)) {
971 set_blocksize(resume_bdev, PAGE_SIZE);
972 error = check_suspend_image();
973 if (error)
974 blkdev_put(resume_bdev);
975 } else
976 error = PTR_ERR(resume_bdev);
977
978 if (!error)
979 pr_debug("swsusp: resume file found\n");
980 else
981 pr_debug("swsusp: Error %d check for resume file\n", error);
982 return error;
983}
984
985/**
986 * swsusp_read - Read saved image from swap.
987 */
988
989int swsusp_read(void)
990{
991 int error;
992
993 if (IS_ERR(resume_bdev)) {
994 pr_debug("swsusp: block device not initialised\n");
995 return PTR_ERR(resume_bdev);
996 }
997
998 error = read_suspend_image();
999 blkdev_put(resume_bdev);
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -07001000 memset(key_iv, 0, MAXKEY+MAXIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 if (!error)
1003 pr_debug("swsusp: Reading resume file was successful\n");
1004 else
1005 pr_debug("swsusp: Error %d resuming\n", error);
1006 return error;
1007}
1008
1009/**
1010 * swsusp_close - close swap device.
1011 */
1012
1013void swsusp_close(void)
1014{
1015 if (IS_ERR(resume_bdev)) {
1016 pr_debug("swsusp: block device not initialised\n");
1017 return;
1018 }
1019
1020 blkdev_put(resume_bdev);
1021}