blob: b041cea2e87812e57571c5fe5708acfb08814743 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/power/swsusp.c
3 *
4 * This file is to realize architecture-independent
5 * machine suspend feature using pretty near only high-level routines
6 *
7 * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
8 * Copyright (C) 1998,2001-2004 Pavel Machek <pavel@suse.cz>
9 *
10 * This file is released under the GPLv2.
11 *
12 * I'd like to thank the following people for their work:
Pavel Machek2e4d5822005-06-25 14:55:12 -070013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Pavel Machek <pavel@ucw.cz>:
15 * Modifications, defectiveness pointing, being with me at the very beginning,
16 * suspend to swap space, stop all tasks. Port to 2.4.18-ac and 2.5.17.
17 *
Pavel Machek2e4d5822005-06-25 14:55:12 -070018 * Steve Doddi <dirk@loth.demon.co.uk>:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Support the possibility of hardware state restoring.
20 *
21 * Raph <grey.havens@earthling.net>:
22 * Support for preserving states of network devices and virtual console
23 * (including X and svgatextmode)
24 *
25 * Kurt Garloff <garloff@suse.de>:
26 * Straightened the critical function in order to prevent compilers from
27 * playing tricks with local variables.
28 *
29 * Andreas Mohr <a.mohr@mailto.de>
30 *
31 * Alex Badea <vampire@go.ro>:
32 * Fixed runaway init
33 *
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -070034 * Andreas Steinmetz <ast@domdv.de>:
35 * Added encrypted suspend option
36 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * More state savers are welcome. Especially for the scsi layer...
38 *
39 * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
40 */
41
42#include <linux/module.h>
43#include <linux/mm.h>
44#include <linux/suspend.h>
45#include <linux/smp_lock.h>
46#include <linux/file.h>
47#include <linux/utsname.h>
48#include <linux/version.h>
49#include <linux/delay.h>
50#include <linux/reboot.h>
51#include <linux/bitops.h>
52#include <linux/vt_kern.h>
53#include <linux/kbd_kern.h>
54#include <linux/keyboard.h>
55#include <linux/spinlock.h>
56#include <linux/genhd.h>
57#include <linux/kernel.h>
58#include <linux/major.h>
59#include <linux/swap.h>
60#include <linux/pm.h>
61#include <linux/device.h>
62#include <linux/buffer_head.h>
63#include <linux/swapops.h>
64#include <linux/bootmem.h>
65#include <linux/syscalls.h>
66#include <linux/console.h>
67#include <linux/highmem.h>
68#include <linux/bio.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070069#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71#include <asm/uaccess.h>
72#include <asm/mmu_context.h>
73#include <asm/pgtable.h>
74#include <asm/tlbflush.h>
75#include <asm/io.h>
76
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -070077#include <linux/random.h>
78#include <linux/crypto.h>
79#include <asm/scatterlist.h>
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include "power.h"
82
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -070083#define CIPHER "aes"
84#define MAXKEY 32
85#define MAXIV 32
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* References to section boundaries */
88extern const void __nosave_begin, __nosave_end;
89
90/* Variables to be preserved over suspend */
91static int nr_copy_pages_check;
92
93extern char resume_file[];
94
95/* Local variables that should not be affected by save */
Adrian Bunk52c1da32005-06-23 22:05:33 -070096static unsigned int nr_copy_pages __nosavedata = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/* Suspend pagedir is allocated before final copy, therefore it
Pavel Machek2e4d5822005-06-25 14:55:12 -070099 must be freed after resume
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 Warning: this is evil. There are actually two pagedirs at time of
102 resume. One is "pagedir_save", which is empty frame allocated at
Pavel Machek2e4d5822005-06-25 14:55:12 -0700103 time of suspend, that must be freed. Second is "pagedir_nosave",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 allocated at time of resume, that travels through memory not to
105 collide with anything.
106
107 Warning: this is even more evil than it seems. Pagedirs this file
108 talks about are completely different from page directories used by
109 MMU hardware.
110 */
111suspend_pagedir_t *pagedir_nosave __nosavedata = NULL;
112static suspend_pagedir_t *pagedir_save;
113
114#define SWSUSP_SIG "S1SUSPEND"
115
116static struct swsusp_header {
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700117 char reserved[PAGE_SIZE - 20 - MAXKEY - MAXIV - sizeof(swp_entry_t)];
118 u8 key_iv[MAXKEY+MAXIV];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 swp_entry_t swsusp_info;
120 char orig_sig[10];
121 char sig[10];
122} __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
123
124static struct swsusp_info swsusp_info;
125
126/*
127 * XXX: We try to keep some more pages free so that I/O operations succeed
128 * without paging. Might this be more?
129 */
130#define PAGES_FOR_IO 512
131
132/*
133 * Saving part...
134 */
135
136/* We memorize in swapfile_used what swap devices are used for suspension */
137#define SWAPFILE_UNUSED 0
138#define SWAPFILE_SUSPEND 1 /* This is the suspending device */
139#define SWAPFILE_IGNORED 2 /* Those are other swap devices ignored for suspension */
140
141static unsigned short swapfile_used[MAX_SWAPFILES];
142static unsigned short root_swap;
143
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700144static int write_page(unsigned long addr, swp_entry_t * loc);
145static int bio_read_page(pgoff_t page_off, void * page);
146
147static u8 key_iv[MAXKEY+MAXIV];
148
149#ifdef CONFIG_SWSUSP_ENCRYPT
150
151static int crypto_init(int mode, void **mem)
152{
153 int error = 0;
154 int len;
155 char *modemsg;
156 struct crypto_tfm *tfm;
157
158 modemsg = mode ? "suspend not possible" : "resume not possible";
159
160 tfm = crypto_alloc_tfm(CIPHER, CRYPTO_TFM_MODE_CBC);
161 if(!tfm) {
162 printk(KERN_ERR "swsusp: no tfm, %s\n", modemsg);
163 error = -EINVAL;
164 goto out;
165 }
166
167 if(MAXKEY < crypto_tfm_alg_min_keysize(tfm)) {
168 printk(KERN_ERR "swsusp: key buffer too small, %s\n", modemsg);
169 error = -ENOKEY;
170 goto fail;
171 }
172
173 if (mode)
174 get_random_bytes(key_iv, MAXKEY+MAXIV);
175
176 len = crypto_tfm_alg_max_keysize(tfm);
177 if (len > MAXKEY)
178 len = MAXKEY;
179
180 if (crypto_cipher_setkey(tfm, key_iv, len)) {
181 printk(KERN_ERR "swsusp: key setup failure, %s\n", modemsg);
182 error = -EKEYREJECTED;
183 goto fail;
184 }
185
186 len = crypto_tfm_alg_ivsize(tfm);
187
188 if (MAXIV < len) {
189 printk(KERN_ERR "swsusp: iv buffer too small, %s\n", modemsg);
190 error = -EOVERFLOW;
191 goto fail;
192 }
193
194 crypto_cipher_set_iv(tfm, key_iv+MAXKEY, len);
195
196 *mem=(void *)tfm;
197
198 goto out;
199
200fail: crypto_free_tfm(tfm);
201out: return error;
202}
203
204static __inline__ void crypto_exit(void *mem)
205{
206 crypto_free_tfm((struct crypto_tfm *)mem);
207}
208
209static __inline__ int crypto_write(struct pbe *p, void *mem)
210{
211 int error = 0;
212 struct scatterlist src, dst;
213
214 src.page = virt_to_page(p->address);
215 src.offset = 0;
216 src.length = PAGE_SIZE;
217 dst.page = virt_to_page((void *)&swsusp_header);
218 dst.offset = 0;
219 dst.length = PAGE_SIZE;
220
221 error = crypto_cipher_encrypt((struct crypto_tfm *)mem, &dst, &src,
222 PAGE_SIZE);
223
224 if (!error)
225 error = write_page((unsigned long)&swsusp_header,
226 &(p->swap_address));
227 return error;
228}
229
230static __inline__ int crypto_read(struct pbe *p, void *mem)
231{
232 int error = 0;
233 struct scatterlist src, dst;
234
235 error = bio_read_page(swp_offset(p->swap_address), (void *)p->address);
236 if (!error) {
237 src.offset = 0;
238 src.length = PAGE_SIZE;
239 dst.offset = 0;
240 dst.length = PAGE_SIZE;
241 src.page = dst.page = virt_to_page((void *)p->address);
242
243 error = crypto_cipher_decrypt((struct crypto_tfm *)mem, &dst,
244 &src, PAGE_SIZE);
245 }
246 return error;
247}
248#else
249static __inline__ int crypto_init(int mode, void *mem)
250{
251 return 0;
252}
253
254static __inline__ void crypto_exit(void *mem)
255{
256}
257
258static __inline__ int crypto_write(struct pbe *p, void *mem)
259{
260 return write_page(p->address, &(p->swap_address));
261}
262
263static __inline__ int crypto_read(struct pbe *p, void *mem)
264{
265 return bio_read_page(swp_offset(p->swap_address), (void *)p->address);
266}
267#endif
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269static int mark_swapfiles(swp_entry_t prev)
270{
271 int error;
272
Pavel Machek2e4d5822005-06-25 14:55:12 -0700273 rw_swap_page_sync(READ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 swp_entry(root_swap, 0),
275 virt_to_page((unsigned long)&swsusp_header));
276 if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
277 !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
278 memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
279 memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700280 memcpy(swsusp_header.key_iv, key_iv, MAXKEY+MAXIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 swsusp_header.swsusp_info = prev;
Pavel Machek2e4d5822005-06-25 14:55:12 -0700282 error = rw_swap_page_sync(WRITE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 swp_entry(root_swap, 0),
284 virt_to_page((unsigned long)
285 &swsusp_header));
286 } else {
287 pr_debug("swsusp: Partition is not swap space.\n");
288 error = -ENODEV;
289 }
290 return error;
291}
292
293/*
294 * Check whether the swap device is the specified resume
295 * device, irrespective of whether they are specified by
296 * identical names.
297 *
298 * (Thus, device inode aliasing is allowed. You can say /dev/hda4
299 * instead of /dev/ide/host0/bus0/target0/lun0/part4 [if using devfs]
300 * and they'll be considered the same device. This is *necessary* for
301 * devfs, since the resume code can only recognize the form /dev/hda4,
302 * but the suspend code would see the long name.)
303 */
304static int is_resume_device(const struct swap_info_struct *swap_info)
305{
306 struct file *file = swap_info->swap_file;
307 struct inode *inode = file->f_dentry->d_inode;
308
309 return S_ISBLK(inode->i_mode) &&
310 swsusp_resume_device == MKDEV(imajor(inode), iminor(inode));
311}
312
313static int swsusp_swap_check(void) /* This is called before saving image */
314{
315 int i, len;
Pavel Machek2e4d5822005-06-25 14:55:12 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 len=strlen(resume_file);
318 root_swap = 0xFFFF;
Pavel Machek2e4d5822005-06-25 14:55:12 -0700319
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700320 spin_lock(&swap_lock);
Pavel Machek2e4d5822005-06-25 14:55:12 -0700321 for (i=0; i<MAX_SWAPFILES; i++) {
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700322 if (!(swap_info[i].flags & SWP_WRITEOK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 swapfile_used[i]=SWAPFILE_UNUSED;
324 } else {
Pavel Machek2e4d5822005-06-25 14:55:12 -0700325 if (!len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 printk(KERN_WARNING "resume= option should be used to set suspend device" );
Pavel Machek2e4d5822005-06-25 14:55:12 -0700327 if (root_swap == 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 swapfile_used[i] = SWAPFILE_SUSPEND;
329 root_swap = i;
330 } else
Pavel Machek2e4d5822005-06-25 14:55:12 -0700331 swapfile_used[i] = SWAPFILE_IGNORED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 } else {
333 /* we ignore all swap devices that are not the resume_file */
334 if (is_resume_device(&swap_info[i])) {
335 swapfile_used[i] = SWAPFILE_SUSPEND;
336 root_swap = i;
337 } else {
338 swapfile_used[i] = SWAPFILE_IGNORED;
339 }
340 }
341 }
342 }
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700343 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return (root_swap != 0xffff) ? 0 : -ENODEV;
345}
346
347/**
348 * This is called after saving image so modification
349 * will be lost after resume... and that's what we want.
350 * we make the device unusable. A new call to
Pavel Machek2e4d5822005-06-25 14:55:12 -0700351 * lock_swapdevices can unlock the devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 */
353static void lock_swapdevices(void)
354{
355 int i;
356
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700357 spin_lock(&swap_lock);
Pavel Machek2e4d5822005-06-25 14:55:12 -0700358 for (i = 0; i< MAX_SWAPFILES; i++)
359 if (swapfile_used[i] == SWAPFILE_IGNORED) {
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700360 swap_info[i].flags ^= SWP_WRITEOK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700362 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365/**
366 * write_swap_page - Write one page to a fresh swap location.
367 * @addr: Address we're writing.
368 * @loc: Place to store the entry we used.
369 *
370 * Allocate a new swap entry and 'sync' it. Note we discard -EIO
Pavel Machek2e4d5822005-06-25 14:55:12 -0700371 * errors. That is an artifact left over from swsusp. It did not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 * check the return of rw_swap_page_sync() at all, since most pages
373 * written back to swap would return -EIO.
374 * This is a partial improvement, since we will at least return other
375 * errors, though we need to eventually fix the damn code.
376 */
377static int write_page(unsigned long addr, swp_entry_t * loc)
378{
379 swp_entry_t entry;
380 int error = 0;
381
382 entry = get_swap_page();
Pavel Machek2e4d5822005-06-25 14:55:12 -0700383 if (swp_offset(entry) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 swapfile_used[swp_type(entry)] == SWAPFILE_SUSPEND) {
385 error = rw_swap_page_sync(WRITE, entry,
386 virt_to_page(addr));
387 if (error == -EIO)
388 error = 0;
389 if (!error)
390 *loc = entry;
391 } else
392 error = -ENOSPC;
393 return error;
394}
395
396/**
397 * data_free - Free the swap entries used by the saved image.
398 *
Pavel Machek2e4d5822005-06-25 14:55:12 -0700399 * Walk the list of used swap entries and free each one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 * This is only used for cleanup when suspend fails.
401 */
402static void data_free(void)
403{
404 swp_entry_t entry;
405 int i;
406
407 for (i = 0; i < nr_copy_pages; i++) {
408 entry = (pagedir_nosave + i)->swap_address;
409 if (entry.val)
410 swap_free(entry);
411 else
412 break;
413 (pagedir_nosave + i)->swap_address = (swp_entry_t){0};
414 }
415}
416
417/**
418 * data_write - Write saved image to swap.
419 *
420 * Walk the list of pages in the image and sync each one to swap.
421 */
422static int data_write(void)
423{
424 int error = 0, i = 0;
425 unsigned int mod = nr_copy_pages / 100;
426 struct pbe *p;
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700427 void *tfm;
428
429 if ((error = crypto_init(1, &tfm)))
430 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 if (!mod)
433 mod = 1;
434
435 printk( "Writing data to swap (%d pages)... ", nr_copy_pages );
Pavel Machek2e4d5822005-06-25 14:55:12 -0700436 for_each_pbe (p, pagedir_nosave) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (!(i%mod))
438 printk( "\b\b\b\b%3d%%", i / mod );
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700439 if ((error = crypto_write(p, tfm))) {
440 crypto_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return error;
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 i++;
444 }
445 printk("\b\b\b\bdone\n");
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700446 crypto_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return error;
448}
449
450static void dump_info(void)
451{
452 pr_debug(" swsusp: Version: %u\n",swsusp_info.version_code);
453 pr_debug(" swsusp: Num Pages: %ld\n",swsusp_info.num_physpages);
454 pr_debug(" swsusp: UTS Sys: %s\n",swsusp_info.uts.sysname);
455 pr_debug(" swsusp: UTS Node: %s\n",swsusp_info.uts.nodename);
456 pr_debug(" swsusp: UTS Release: %s\n",swsusp_info.uts.release);
457 pr_debug(" swsusp: UTS Version: %s\n",swsusp_info.uts.version);
458 pr_debug(" swsusp: UTS Machine: %s\n",swsusp_info.uts.machine);
459 pr_debug(" swsusp: UTS Domain: %s\n",swsusp_info.uts.domainname);
460 pr_debug(" swsusp: CPUs: %d\n",swsusp_info.cpus);
461 pr_debug(" swsusp: Image: %ld Pages\n",swsusp_info.image_pages);
462 pr_debug(" swsusp: Pagedir: %ld Pages\n",swsusp_info.pagedir_pages);
463}
464
465static void init_header(void)
466{
467 memset(&swsusp_info, 0, sizeof(swsusp_info));
468 swsusp_info.version_code = LINUX_VERSION_CODE;
469 swsusp_info.num_physpages = num_physpages;
470 memcpy(&swsusp_info.uts, &system_utsname, sizeof(system_utsname));
471
472 swsusp_info.suspend_pagedir = pagedir_nosave;
473 swsusp_info.cpus = num_online_cpus();
474 swsusp_info.image_pages = nr_copy_pages;
475}
476
477static int close_swap(void)
478{
479 swp_entry_t entry;
480 int error;
481
482 dump_info();
483 error = write_page((unsigned long)&swsusp_info, &entry);
Pavel Machek2e4d5822005-06-25 14:55:12 -0700484 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 printk( "S" );
486 error = mark_swapfiles(entry);
487 printk( "|\n" );
488 }
489 return error;
490}
491
492/**
493 * free_pagedir_entries - Free pages used by the page directory.
494 *
495 * This is used during suspend for error recovery.
496 */
497
498static void free_pagedir_entries(void)
499{
500 int i;
501
502 for (i = 0; i < swsusp_info.pagedir_pages; i++)
503 swap_free(swsusp_info.pagedir[i]);
504}
505
506
507/**
508 * write_pagedir - Write the array of pages holding the page directory.
509 * @last: Last swap entry we write (needed for header).
510 */
511
512static int write_pagedir(void)
513{
514 int error = 0;
515 unsigned n = 0;
516 struct pbe * pbe;
517
518 printk( "Writing pagedir...");
Pavel Machek2e4d5822005-06-25 14:55:12 -0700519 for_each_pb_page (pbe, pagedir_nosave) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if ((error = write_page((unsigned long)pbe, &swsusp_info.pagedir[n++])))
521 return error;
522 }
523
524 swsusp_info.pagedir_pages = n;
525 printk("done (%u pages)\n", n);
526 return error;
527}
528
529/**
530 * write_suspend_image - Write entire image and metadata.
531 *
532 */
533
534static int write_suspend_image(void)
535{
536 int error;
537
538 init_header();
539 if ((error = data_write()))
540 goto FreeData;
541
542 if ((error = write_pagedir()))
543 goto FreePagedir;
544
545 if ((error = close_swap()))
546 goto FreePagedir;
547 Done:
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700548 memset(key_iv, 0, MAXKEY+MAXIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return error;
550 FreePagedir:
551 free_pagedir_entries();
552 FreeData:
553 data_free();
554 goto Done;
555}
556
557
558#ifdef CONFIG_HIGHMEM
559struct highmem_page {
560 char *data;
561 struct page *page;
562 struct highmem_page *next;
563};
564
565static struct highmem_page *highmem_copy;
566
567static int save_highmem_zone(struct zone *zone)
568{
569 unsigned long zone_pfn;
570 mark_free_pages(zone);
571 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
572 struct page *page;
573 struct highmem_page *save;
574 void *kaddr;
575 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
576
577 if (!(pfn%1000))
578 printk(".");
579 if (!pfn_valid(pfn))
580 continue;
581 page = pfn_to_page(pfn);
582 /*
583 * This condition results from rvmalloc() sans vmalloc_32()
584 * and architectural memory reservations. This should be
585 * corrected eventually when the cases giving rise to this
586 * are better understood.
587 */
588 if (PageReserved(page)) {
589 printk("highmem reserved page?!\n");
590 continue;
591 }
592 BUG_ON(PageNosave(page));
593 if (PageNosaveFree(page))
594 continue;
595 save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
596 if (!save)
597 return -ENOMEM;
598 save->next = highmem_copy;
599 save->page = page;
600 save->data = (void *) get_zeroed_page(GFP_ATOMIC);
601 if (!save->data) {
602 kfree(save);
603 return -ENOMEM;
604 }
605 kaddr = kmap_atomic(page, KM_USER0);
606 memcpy(save->data, kaddr, PAGE_SIZE);
607 kunmap_atomic(kaddr, KM_USER0);
608 highmem_copy = save;
609 }
610 return 0;
611}
612#endif /* CONFIG_HIGHMEM */
613
614
615static int save_highmem(void)
616{
617#ifdef CONFIG_HIGHMEM
618 struct zone *zone;
619 int res = 0;
620
621 pr_debug("swsusp: Saving Highmem\n");
Pavel Machek2e4d5822005-06-25 14:55:12 -0700622 for_each_zone (zone) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (is_highmem(zone))
624 res = save_highmem_zone(zone);
625 if (res)
626 return res;
627 }
628#endif
629 return 0;
630}
631
632static int restore_highmem(void)
633{
634#ifdef CONFIG_HIGHMEM
635 printk("swsusp: Restoring Highmem\n");
636 while (highmem_copy) {
637 struct highmem_page *save = highmem_copy;
638 void *kaddr;
639 highmem_copy = save->next;
640
641 kaddr = kmap_atomic(save->page, KM_USER0);
642 memcpy(kaddr, save->data, PAGE_SIZE);
643 kunmap_atomic(kaddr, KM_USER0);
644 free_page((long) save->data);
645 kfree(save);
646 }
647#endif
648 return 0;
649}
650
651
652static int pfn_is_nosave(unsigned long pfn)
653{
654 unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
655 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
656 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
657}
658
659/**
660 * saveable - Determine whether a page should be cloned or not.
661 * @pfn: The page
662 *
663 * We save a page if it's Reserved, and not in the range of pages
664 * statically defined as 'unsaveable', or if it isn't reserved, and
665 * isn't part of a free chunk of pages.
666 */
667
668static int saveable(struct zone * zone, unsigned long * zone_pfn)
669{
670 unsigned long pfn = *zone_pfn + zone->zone_start_pfn;
671 struct page * page;
672
673 if (!pfn_valid(pfn))
674 return 0;
675
676 page = pfn_to_page(pfn);
677 BUG_ON(PageReserved(page) && PageNosave(page));
678 if (PageNosave(page))
679 return 0;
680 if (PageReserved(page) && pfn_is_nosave(pfn)) {
681 pr_debug("[nosave pfn 0x%lx]", pfn);
682 return 0;
683 }
684 if (PageNosaveFree(page))
685 return 0;
686
687 return 1;
688}
689
690static void count_data_pages(void)
691{
692 struct zone *zone;
693 unsigned long zone_pfn;
694
695 nr_copy_pages = 0;
696
Pavel Machek2e4d5822005-06-25 14:55:12 -0700697 for_each_zone (zone) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (is_highmem(zone))
699 continue;
700 mark_free_pages(zone);
701 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
702 nr_copy_pages += saveable(zone, &zone_pfn);
703 }
704}
705
706
707static void copy_data_pages(void)
708{
709 struct zone *zone;
710 unsigned long zone_pfn;
711 struct pbe * pbe = pagedir_nosave;
Pavel Machek2e4d5822005-06-25 14:55:12 -0700712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 pr_debug("copy_data_pages(): pages to copy: %d\n", nr_copy_pages);
Pavel Machek2e4d5822005-06-25 14:55:12 -0700714 for_each_zone (zone) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (is_highmem(zone))
716 continue;
717 mark_free_pages(zone);
718 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
719 if (saveable(zone, &zone_pfn)) {
720 struct page * page;
721 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
722 BUG_ON(!pbe);
723 pbe->orig_address = (long) page_address(page);
724 /* copy_page is not usable for copying task structs. */
725 memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE);
726 pbe = pbe->next;
727 }
728 }
729 }
730 BUG_ON(pbe);
731}
732
733
734/**
735 * calc_nr - Determine the number of pages needed for a pbe list.
736 */
737
738static int calc_nr(int nr_copy)
739{
740 int extra = 0;
741 int mod = !!(nr_copy % PBES_PER_PAGE);
742 int diff = (nr_copy / PBES_PER_PAGE) + mod;
743
744 do {
745 extra += diff;
746 nr_copy += diff;
747 mod = !!(nr_copy % PBES_PER_PAGE);
748 diff = (nr_copy / PBES_PER_PAGE) + mod - extra;
749 } while (diff > 0);
750
751 return nr_copy;
752}
753
754/**
755 * free_pagedir - free pages allocated with alloc_pagedir()
756 */
757
758static inline void free_pagedir(struct pbe *pblist)
759{
760 struct pbe *pbe;
761
762 while (pblist) {
763 pbe = (pblist + PB_PAGE_SKIP)->next;
764 free_page((unsigned long)pblist);
765 pblist = pbe;
766 }
767}
768
769/**
770 * fill_pb_page - Create a list of PBEs on a given memory page
771 */
772
773static inline void fill_pb_page(struct pbe *pbpage)
774{
775 struct pbe *p;
776
777 p = pbpage;
778 pbpage += PB_PAGE_SKIP;
779 do
780 p->next = p + 1;
781 while (++p < pbpage);
782}
783
784/**
785 * create_pbe_list - Create a list of PBEs on top of a given chain
786 * of memory pages allocated with alloc_pagedir()
787 */
788
789static void create_pbe_list(struct pbe *pblist, unsigned nr_pages)
790{
791 struct pbe *pbpage, *p;
792 unsigned num = PBES_PER_PAGE;
793
794 for_each_pb_page (pbpage, pblist) {
795 if (num >= nr_pages)
796 break;
797
798 fill_pb_page(pbpage);
799 num += PBES_PER_PAGE;
800 }
801 if (pbpage) {
802 for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
803 p->next = p + 1;
804 p->next = NULL;
805 }
806 pr_debug("create_pbe_list(): initialized %d PBEs\n", num);
807}
808
809/**
810 * alloc_pagedir - Allocate the page directory.
811 *
812 * First, determine exactly how many pages we need and
813 * allocate them.
814 *
815 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
816 * struct pbe elements (pbes) and the last element in the page points
817 * to the next page.
818 *
819 * On each page we set up a list of struct_pbe elements.
820 */
821
822static struct pbe * alloc_pagedir(unsigned nr_pages)
823{
824 unsigned num;
825 struct pbe *pblist, *pbe;
826
827 if (!nr_pages)
828 return NULL;
829
830 pr_debug("alloc_pagedir(): nr_pages = %d\n", nr_pages);
831 pblist = (struct pbe *)get_zeroed_page(GFP_ATOMIC | __GFP_COLD);
832 for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
833 pbe = pbe->next, num += PBES_PER_PAGE) {
834 pbe += PB_PAGE_SKIP;
835 pbe->next = (struct pbe *)get_zeroed_page(GFP_ATOMIC | __GFP_COLD);
836 }
837 if (!pbe) { /* get_zeroed_page() failed */
838 free_pagedir(pblist);
839 pblist = NULL;
840 }
841 return pblist;
842}
843
844/**
845 * free_image_pages - Free pages allocated for snapshot
846 */
847
848static void free_image_pages(void)
849{
850 struct pbe * p;
851
Pavel Machek2e4d5822005-06-25 14:55:12 -0700852 for_each_pbe (p, pagedir_save) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (p->address) {
854 ClearPageNosave(virt_to_page(p->address));
855 free_page(p->address);
856 p->address = 0;
857 }
858 }
859}
860
861/**
862 * alloc_image_pages - Allocate pages for the snapshot.
863 */
864
865static int alloc_image_pages(void)
866{
867 struct pbe * p;
868
Pavel Machek2e4d5822005-06-25 14:55:12 -0700869 for_each_pbe (p, pagedir_save) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 p->address = get_zeroed_page(GFP_ATOMIC | __GFP_COLD);
871 if (!p->address)
872 return -ENOMEM;
873 SetPageNosave(virt_to_page(p->address));
874 }
875 return 0;
876}
877
878void swsusp_free(void)
879{
880 BUG_ON(PageNosave(virt_to_page(pagedir_save)));
881 BUG_ON(PageNosaveFree(virt_to_page(pagedir_save)));
882 free_image_pages();
883 free_pagedir(pagedir_save);
884}
885
886
887/**
888 * enough_free_mem - Make sure we enough free memory to snapshot.
889 *
Pavel Machek2e4d5822005-06-25 14:55:12 -0700890 * Returns TRUE or FALSE after checking the number of available
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 * free pages.
892 */
893
894static int enough_free_mem(void)
895{
896 if (nr_free_pages() < (nr_copy_pages + PAGES_FOR_IO)) {
897 pr_debug("swsusp: Not enough free pages: Have %d\n",
898 nr_free_pages());
899 return 0;
900 }
901 return 1;
902}
903
904
905/**
906 * enough_swap - Make sure we have enough swap to save the image.
907 *
Pavel Machek2e4d5822005-06-25 14:55:12 -0700908 * Returns TRUE or FALSE after checking the total amount of swap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 * space avaiable.
910 *
911 * FIXME: si_swapinfo(&i) returns all swap devices information.
Pavel Machek2e4d5822005-06-25 14:55:12 -0700912 * We should only consider resume_device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 */
914
915static int enough_swap(void)
916{
917 struct sysinfo i;
918
919 si_swapinfo(&i);
920 if (i.freeswap < (nr_copy_pages + PAGES_FOR_IO)) {
921 pr_debug("swsusp: Not enough swap. Need %ld\n",i.freeswap);
922 return 0;
923 }
924 return 1;
925}
926
927static int swsusp_alloc(void)
928{
929 int error;
930
Pavel Machekc61978b2005-06-25 14:55:14 -0700931 pagedir_nosave = NULL;
932 nr_copy_pages = calc_nr(nr_copy_pages);
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 pr_debug("suspend: (pages needed: %d + %d free: %d)\n",
935 nr_copy_pages, PAGES_FOR_IO, nr_free_pages());
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (!enough_free_mem())
938 return -ENOMEM;
939
940 if (!enough_swap())
941 return -ENOSPC;
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (!(pagedir_save = alloc_pagedir(nr_copy_pages))) {
944 printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
945 return -ENOMEM;
946 }
947 create_pbe_list(pagedir_save, nr_copy_pages);
948 pagedir_nosave = pagedir_save;
949 if ((error = alloc_image_pages())) {
950 printk(KERN_ERR "suspend: Allocating image pages failed.\n");
951 swsusp_free();
952 return error;
953 }
954
955 nr_copy_pages_check = nr_copy_pages;
956 return 0;
957}
958
959static int suspend_prepare_image(void)
960{
961 int error;
962
963 pr_debug("swsusp: critical section: \n");
964 if (save_highmem()) {
965 printk(KERN_CRIT "Suspend machine: Not enough free pages for highmem\n");
966 restore_highmem();
967 return -ENOMEM;
968 }
969
970 drain_local_pages();
971 count_data_pages();
972 printk("swsusp: Need to copy %u pages\n", nr_copy_pages);
973
974 error = swsusp_alloc();
975 if (error)
976 return error;
Pavel Machek2e4d5822005-06-25 14:55:12 -0700977
978 /* During allocating of suspend pagedir, new cold pages may appear.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 * Kill them.
980 */
981 drain_local_pages();
982 copy_data_pages();
983
984 /*
985 * End of critical section. From now on, we can write to memory,
986 * but we should not touch disk. This specially means we must _not_
987 * touch swap space! Except we must write out our image of course.
988 */
989
990 printk("swsusp: critical section/: done (%d pages copied)\n", nr_copy_pages );
991 return 0;
992}
993
994
995/* It is important _NOT_ to umount filesystems at this point. We want
996 * them synced (in case something goes wrong) but we DO not want to mark
997 * filesystem clean: it is not. (And it does not matter, if we resume
998 * correctly, we'll mark system clean, anyway.)
999 */
1000int swsusp_write(void)
1001{
1002 int error;
1003 device_resume();
1004 lock_swapdevices();
1005 error = write_suspend_image();
1006 /* This will unlock ignored swap devices since writing is finished */
1007 lock_swapdevices();
1008 return error;
1009
1010}
1011
1012
1013extern asmlinkage int swsusp_arch_suspend(void);
1014extern asmlinkage int swsusp_arch_resume(void);
1015
1016
1017asmlinkage int swsusp_save(void)
1018{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return suspend_prepare_image();
1020}
1021
1022int swsusp_suspend(void)
1023{
1024 int error;
1025 if ((error = arch_prepare_suspend()))
1026 return error;
1027 local_irq_disable();
1028 /* At this point, device_suspend() has been called, but *not*
1029 * device_power_down(). We *must* device_power_down() now.
1030 * Otherwise, drivers for some devices (e.g. interrupt controllers)
1031 * become desynchronized with the actual state of the hardware
1032 * at resume time, and evil weirdness ensues.
1033 */
1034 if ((error = device_power_down(PMSG_FREEZE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return error;
1037 }
Pavel Machek47b724f2005-07-07 17:56:44 -07001038
1039 if ((error = swsusp_swap_check())) {
1040 printk(KERN_ERR "swsusp: FATAL: cannot find swap device, try "
1041 "swapon -a!\n");
1042 local_irq_enable();
1043 return error;
1044 }
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 save_processor_state();
1047 if ((error = swsusp_arch_suspend()))
Pavel Machek47b724f2005-07-07 17:56:44 -07001048 printk("Error %d suspending\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /* Restore control flow magically appears here */
1050 restore_processor_state();
1051 BUG_ON (nr_copy_pages_check != nr_copy_pages);
1052 restore_highmem();
1053 device_power_up();
1054 local_irq_enable();
1055 return error;
1056}
1057
1058int swsusp_resume(void)
1059{
1060 int error;
1061 local_irq_disable();
1062 if (device_power_down(PMSG_FREEZE))
1063 printk(KERN_ERR "Some devices failed to power down, very bad\n");
1064 /* We'll ignore saved state, but this gets preempt count (etc) right */
1065 save_processor_state();
1066 error = swsusp_arch_resume();
1067 /* Code below is only ever reached in case of failure. Otherwise
1068 * execution continues at place where swsusp_arch_suspend was called
1069 */
1070 BUG_ON(!error);
1071 restore_processor_state();
1072 restore_highmem();
1073 device_power_up();
1074 local_irq_enable();
1075 return error;
1076}
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078/**
1079 * On resume, for storing the PBE list and the image,
1080 * we can only use memory pages that do not conflict with the pages
1081 * which had been used before suspend.
1082 *
1083 * We don't know which pages are usable until we allocate them.
1084 *
1085 * Allocated but unusable (ie eaten) memory pages are linked together
1086 * to create a list, so that we can free them easily
1087 *
1088 * We could have used a type other than (void *)
1089 * for this purpose, but ...
1090 */
1091static void **eaten_memory = NULL;
1092
1093static inline void eat_page(void *page)
1094{
1095 void **c;
1096
1097 c = eaten_memory;
1098 eaten_memory = page;
1099 *eaten_memory = c;
1100}
1101
1102static unsigned long get_usable_page(unsigned gfp_mask)
1103{
1104 unsigned long m;
1105
1106 m = get_zeroed_page(gfp_mask);
Pavel Machek8f9bdf12005-06-25 14:55:12 -07001107 while (!PageNosaveFree(virt_to_page(m))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 eat_page((void *)m);
1109 m = get_zeroed_page(gfp_mask);
1110 if (!m)
1111 break;
1112 }
1113 return m;
1114}
1115
1116static void free_eaten_memory(void)
1117{
1118 unsigned long m;
1119 void **c;
1120 int i = 0;
1121
1122 c = eaten_memory;
1123 while (c) {
1124 m = (unsigned long)c;
1125 c = *c;
1126 free_page(m);
1127 i++;
1128 }
1129 eaten_memory = NULL;
1130 pr_debug("swsusp: %d unused pages freed\n", i);
1131}
1132
1133/**
1134 * check_pagedir - We ensure here that pages that the PBEs point to
1135 * won't collide with pages where we're going to restore from the loaded
1136 * pages later
1137 */
1138
1139static int check_pagedir(struct pbe *pblist)
1140{
1141 struct pbe *p;
1142
1143 /* This is necessary, so that we can free allocated pages
1144 * in case of failure
1145 */
1146 for_each_pbe (p, pblist)
1147 p->address = 0UL;
1148
1149 for_each_pbe (p, pblist) {
1150 p->address = get_usable_page(GFP_ATOMIC);
1151 if (!p->address)
1152 return -ENOMEM;
1153 }
1154 return 0;
1155}
1156
1157/**
1158 * swsusp_pagedir_relocate - It is possible, that some memory pages
1159 * occupied by the list of PBEs collide with pages where we're going to
1160 * restore from the loaded pages later. We relocate them here.
1161 */
1162
1163static struct pbe * swsusp_pagedir_relocate(struct pbe *pblist)
1164{
1165 struct zone *zone;
1166 unsigned long zone_pfn;
1167 struct pbe *pbpage, *tail, *p;
1168 void *m;
1169 int rel = 0, error = 0;
1170
1171 if (!pblist) /* a sanity check */
1172 return NULL;
1173
1174 pr_debug("swsusp: Relocating pagedir (%lu pages to check)\n",
1175 swsusp_info.pagedir_pages);
1176
1177 /* Set page flags */
1178
Pavel Machek2e4d5822005-06-25 14:55:12 -07001179 for_each_zone (zone) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
1181 SetPageNosaveFree(pfn_to_page(zone_pfn +
1182 zone->zone_start_pfn));
1183 }
1184
1185 /* Clear orig addresses */
1186
1187 for_each_pbe (p, pblist)
1188 ClearPageNosaveFree(virt_to_page(p->orig_address));
1189
1190 tail = pblist + PB_PAGE_SKIP;
1191
1192 /* Relocate colliding pages */
1193
1194 for_each_pb_page (pbpage, pblist) {
Pavel Machek8f9bdf12005-06-25 14:55:12 -07001195 if (!PageNosaveFree(virt_to_page((unsigned long)pbpage))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 m = (void *)get_usable_page(GFP_ATOMIC | __GFP_COLD);
1197 if (!m) {
1198 error = -ENOMEM;
1199 break;
1200 }
1201 memcpy(m, (void *)pbpage, PAGE_SIZE);
1202 if (pbpage == pblist)
1203 pblist = (struct pbe *)m;
1204 else
1205 tail->next = (struct pbe *)m;
1206
1207 eat_page((void *)pbpage);
1208 pbpage = (struct pbe *)m;
1209
1210 /* We have to link the PBEs again */
1211
1212 for (p = pbpage; p < pbpage + PB_PAGE_SKIP; p++)
1213 if (p->next) /* needed to save the end */
1214 p->next = p + 1;
1215
1216 rel++;
1217 }
1218 tail = pbpage + PB_PAGE_SKIP;
1219 }
1220
1221 if (error) {
1222 printk("\nswsusp: Out of memory\n\n");
1223 free_pagedir(pblist);
1224 free_eaten_memory();
1225 pblist = NULL;
1226 }
1227 else
1228 printk("swsusp: Relocated %d pages\n", rel);
1229
1230 return pblist;
1231}
1232
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001233/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 * Using bio to read from swap.
1235 * This code requires a bit more work than just using buffer heads
1236 * but, it is the recommended way for 2.5/2.6.
1237 * The following are to signal the beginning and end of I/O. Bios
1238 * finish asynchronously, while we want them to happen synchronously.
1239 * A simple atomic_t, and a wait loop take care of this problem.
1240 */
1241
1242static atomic_t io_done = ATOMIC_INIT(0);
1243
1244static int end_io(struct bio * bio, unsigned int num, int err)
1245{
1246 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
1247 panic("I/O error reading memory image");
1248 atomic_set(&io_done, 0);
1249 return 0;
1250}
1251
1252static struct block_device * resume_bdev;
1253
1254/**
1255 * submit - submit BIO request.
1256 * @rw: READ or WRITE.
1257 * @off physical offset of page.
1258 * @page: page we're reading or writing.
1259 *
1260 * Straight from the textbook - allocate and initialize the bio.
1261 * If we're writing, make sure the page is marked as dirty.
1262 * Then submit it and wait.
1263 */
1264
1265static int submit(int rw, pgoff_t page_off, void * page)
1266{
1267 int error = 0;
1268 struct bio * bio;
1269
1270 bio = bio_alloc(GFP_ATOMIC, 1);
1271 if (!bio)
1272 return -ENOMEM;
1273 bio->bi_sector = page_off * (PAGE_SIZE >> 9);
1274 bio_get(bio);
1275 bio->bi_bdev = resume_bdev;
1276 bio->bi_end_io = end_io;
1277
1278 if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
1279 printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
1280 error = -EFAULT;
1281 goto Done;
1282 }
1283
1284 if (rw == WRITE)
1285 bio_set_pages_dirty(bio);
1286
1287 atomic_set(&io_done, 1);
1288 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
1289 while (atomic_read(&io_done))
1290 yield();
1291
1292 Done:
1293 bio_put(bio);
1294 return error;
1295}
1296
1297static int bio_read_page(pgoff_t page_off, void * page)
1298{
1299 return submit(READ, page_off, page);
1300}
1301
1302static int bio_write_page(pgoff_t page_off, void * page)
1303{
1304 return submit(WRITE, page_off, page);
1305}
1306
1307/*
1308 * Sanity check if this image makes sense with this kernel/swap context
1309 * I really don't think that it's foolproof but more than nothing..
1310 */
1311
1312static const char * sanity_check(void)
1313{
1314 dump_info();
Pavel Machek47b724f2005-07-07 17:56:44 -07001315 if (swsusp_info.version_code != LINUX_VERSION_CODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return "kernel version";
Pavel Machek47b724f2005-07-07 17:56:44 -07001317 if (swsusp_info.num_physpages != num_physpages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return "memory size";
1319 if (strcmp(swsusp_info.uts.sysname,system_utsname.sysname))
1320 return "system type";
1321 if (strcmp(swsusp_info.uts.release,system_utsname.release))
1322 return "kernel release";
1323 if (strcmp(swsusp_info.uts.version,system_utsname.version))
1324 return "version";
1325 if (strcmp(swsusp_info.uts.machine,system_utsname.machine))
1326 return "machine";
Li Shaohua5a72e042005-06-25 14:55:06 -07001327#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if(swsusp_info.cpus != num_online_cpus())
1329 return "number of cpus";
Li Shaohua5a72e042005-06-25 14:55:06 -07001330#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return NULL;
1332}
1333
1334
1335static int check_header(void)
1336{
1337 const char * reason = NULL;
1338 int error;
1339
1340 if ((error = bio_read_page(swp_offset(swsusp_header.swsusp_info), &swsusp_info)))
1341 return error;
1342
1343 /* Is this same machine? */
1344 if ((reason = sanity_check())) {
1345 printk(KERN_ERR "swsusp: Resume mismatch: %s\n",reason);
1346 return -EPERM;
1347 }
1348 nr_copy_pages = swsusp_info.image_pages;
1349 return error;
1350}
1351
1352static int check_sig(void)
1353{
1354 int error;
1355
1356 memset(&swsusp_header, 0, sizeof(swsusp_header));
1357 if ((error = bio_read_page(0, &swsusp_header)))
1358 return error;
1359 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
1360 memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -07001361 memcpy(key_iv, swsusp_header.key_iv, MAXKEY+MAXIV);
1362 memset(swsusp_header.key_iv, 0, MAXKEY+MAXIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 /*
1365 * Reset swap signature now.
1366 */
1367 error = bio_write_page(0, &swsusp_header);
1368 } else {
1369 printk(KERN_ERR "swsusp: Suspend partition has wrong signature?\n");
1370 return -EINVAL;
1371 }
1372 if (!error)
1373 pr_debug("swsusp: Signature found, resuming\n");
1374 return error;
1375}
1376
1377/**
1378 * data_read - Read image pages from swap.
1379 *
1380 * You do not need to check for overlaps, check_pagedir()
1381 * already did that.
1382 */
1383
1384static int data_read(struct pbe *pblist)
1385{
1386 struct pbe * p;
1387 int error = 0;
1388 int i = 0;
1389 int mod = swsusp_info.image_pages / 100;
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -07001390 void *tfm;
1391
1392 if ((error = crypto_init(0, &tfm)))
1393 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 if (!mod)
1396 mod = 1;
1397
1398 printk("swsusp: Reading image data (%lu pages): ",
1399 swsusp_info.image_pages);
1400
1401 for_each_pbe (p, pblist) {
1402 if (!(i % mod))
1403 printk("\b\b\b\b%3d%%", i / mod);
1404
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -07001405 if ((error = crypto_read(p, tfm))) {
1406 crypto_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return error;
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -07001408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 i++;
1411 }
1412 printk("\b\b\b\bdone\n");
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -07001413 crypto_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return error;
1415}
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417/**
1418 * read_pagedir - Read page backup list pages from swap
1419 */
1420
1421static int read_pagedir(struct pbe *pblist)
1422{
1423 struct pbe *pbpage, *p;
1424 unsigned i = 0;
1425 int error;
1426
1427 if (!pblist)
1428 return -EFAULT;
1429
1430 printk("swsusp: Reading pagedir (%lu pages)\n",
1431 swsusp_info.pagedir_pages);
1432
1433 for_each_pb_page (pbpage, pblist) {
1434 unsigned long offset = swp_offset(swsusp_info.pagedir[i++]);
1435
1436 error = -EFAULT;
1437 if (offset) {
1438 p = (pbpage + PB_PAGE_SKIP)->next;
1439 error = bio_read_page(offset, (void *)pbpage);
1440 (pbpage + PB_PAGE_SKIP)->next = p;
1441 }
1442 if (error)
1443 break;
1444 }
1445
1446 if (error)
1447 free_page((unsigned long)pblist);
1448
1449 BUG_ON(i != swsusp_info.pagedir_pages);
1450
1451 return error;
1452}
1453
1454
1455static int check_suspend_image(void)
1456{
1457 int error = 0;
1458
1459 if ((error = check_sig()))
1460 return error;
1461
1462 if ((error = check_header()))
1463 return error;
1464
1465 return 0;
1466}
1467
1468static int read_suspend_image(void)
1469{
1470 int error = 0;
1471 struct pbe *p;
1472
1473 if (!(p = alloc_pagedir(nr_copy_pages)))
1474 return -ENOMEM;
1475
1476 if ((error = read_pagedir(p)))
1477 return error;
1478
1479 create_pbe_list(p, nr_copy_pages);
1480
1481 if (!(pagedir_nosave = swsusp_pagedir_relocate(p)))
1482 return -ENOMEM;
1483
1484 /* Allocate memory for the image and read the data from swap */
1485
1486 error = check_pagedir(pagedir_nosave);
1487 free_eaten_memory();
1488 if (!error)
1489 error = data_read(pagedir_nosave);
1490
1491 if (error) { /* We fail cleanly */
1492 for_each_pbe (p, pagedir_nosave)
1493 if (p->address) {
1494 free_page(p->address);
1495 p->address = 0UL;
1496 }
1497 free_pagedir(pagedir_nosave);
1498 }
1499 return error;
1500}
1501
1502/**
1503 * swsusp_check - Check for saved image in swap
1504 */
1505
1506int swsusp_check(void)
1507{
1508 int error;
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
1511 if (!IS_ERR(resume_bdev)) {
1512 set_blocksize(resume_bdev, PAGE_SIZE);
1513 error = check_suspend_image();
1514 if (error)
1515 blkdev_put(resume_bdev);
1516 } else
1517 error = PTR_ERR(resume_bdev);
1518
1519 if (!error)
1520 pr_debug("swsusp: resume file found\n");
1521 else
1522 pr_debug("swsusp: Error %d check for resume file\n", error);
1523 return error;
1524}
1525
1526/**
1527 * swsusp_read - Read saved image from swap.
1528 */
1529
1530int swsusp_read(void)
1531{
1532 int error;
1533
1534 if (IS_ERR(resume_bdev)) {
1535 pr_debug("swsusp: block device not initialised\n");
1536 return PTR_ERR(resume_bdev);
1537 }
1538
1539 error = read_suspend_image();
1540 blkdev_put(resume_bdev);
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -07001541 memset(key_iv, 0, MAXKEY+MAXIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543 if (!error)
1544 pr_debug("swsusp: Reading resume file was successful\n");
1545 else
1546 pr_debug("swsusp: Error %d resuming\n", error);
1547 return error;
1548}
1549
1550/**
1551 * swsusp_close - close swap device.
1552 */
1553
1554void swsusp_close(void)
1555{
1556 if (IS_ERR(resume_bdev)) {
1557 pr_debug("swsusp: block device not initialised\n");
1558 return;
1559 }
1560
1561 blkdev_put(resume_bdev);
1562}