[PATCH] swsusp: rework image freeing
[deliverable/linux.git] / kernel / power / swsusp.c
CommitLineData
1da177e4
LT
1/*
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:
2e4d5822 13 *
1da177e4
LT
14 * 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 *
2e4d5822 18 * Steve Doddi <dirk@loth.demon.co.uk>:
1da177e4
LT
19 * 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 *
c2ff18f4
AS
34 * Andreas Steinmetz <ast@domdv.de>:
35 * Added encrypted suspend option
36 *
1da177e4
LT
37 * 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>
d53d9f16 69#include <linux/mount.h>
1da177e4
LT
70
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
c2ff18f4
AS
77#include <linux/random.h>
78#include <linux/crypto.h>
79#include <asm/scatterlist.h>
80
1da177e4
LT
81#include "power.h"
82
c2ff18f4
AS
83#define CIPHER "aes"
84#define MAXKEY 32
85#define MAXIV 32
86
1da177e4
LT
87/* 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 */
52c1da39 96static unsigned int nr_copy_pages __nosavedata = 0;
1da177e4
LT
97
98/* Suspend pagedir is allocated before final copy, therefore it
2e4d5822 99 must be freed after resume
1da177e4
LT
100
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
2e4d5822 103 time of suspend, that must be freed. Second is "pagedir_nosave",
1da177e4
LT
104 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 {
c2ff18f4
AS
117 char reserved[PAGE_SIZE - 20 - MAXKEY - MAXIV - sizeof(swp_entry_t)];
118 u8 key_iv[MAXKEY+MAXIV];
1da177e4
LT
119 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
c2ff18f4
AS
144static 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
1da177e4
LT
269static int mark_swapfiles(swp_entry_t prev)
270{
271 int error;
272
2e4d5822 273 rw_swap_page_sync(READ,
1da177e4
LT
274 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);
c2ff18f4 280 memcpy(swsusp_header.key_iv, key_iv, MAXKEY+MAXIV);
1da177e4 281 swsusp_header.swsusp_info = prev;
2e4d5822 282 error = rw_swap_page_sync(WRITE,
1da177e4
LT
283 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;
2e4d5822 316
1da177e4
LT
317 len=strlen(resume_file);
318 root_swap = 0xFFFF;
2e4d5822 319
dae06ac4 320 spin_lock(&swap_lock);
2e4d5822 321 for (i=0; i<MAX_SWAPFILES; i++) {
dae06ac4 322 if (!(swap_info[i].flags & SWP_WRITEOK)) {
1da177e4
LT
323 swapfile_used[i]=SWAPFILE_UNUSED;
324 } else {
2e4d5822 325 if (!len) {
1da177e4 326 printk(KERN_WARNING "resume= option should be used to set suspend device" );
2e4d5822 327 if (root_swap == 0xFFFF) {
1da177e4
LT
328 swapfile_used[i] = SWAPFILE_SUSPEND;
329 root_swap = i;
330 } else
2e4d5822 331 swapfile_used[i] = SWAPFILE_IGNORED;
1da177e4
LT
332 } 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 }
dae06ac4 343 spin_unlock(&swap_lock);
1da177e4
LT
344 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
2e4d5822 351 * lock_swapdevices can unlock the devices.
1da177e4
LT
352 */
353static void lock_swapdevices(void)
354{
355 int i;
356
dae06ac4 357 spin_lock(&swap_lock);
2e4d5822
PM
358 for (i = 0; i< MAX_SWAPFILES; i++)
359 if (swapfile_used[i] == SWAPFILE_IGNORED) {
dae06ac4 360 swap_info[i].flags ^= SWP_WRITEOK;
1da177e4 361 }
dae06ac4 362 spin_unlock(&swap_lock);
1da177e4
LT
363}
364
365/**
8686bcd0 366 * write_page - Write one page to a fresh swap location.
1da177e4
LT
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
2e4d5822 371 * errors. That is an artifact left over from swsusp. It did not
1da177e4
LT
372 * 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();
2e4d5822 383 if (swp_offset(entry) &&
1da177e4
LT
384 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 *
2e4d5822 399 * Walk the list of used swap entries and free each one.
1da177e4
LT
400 * This is only used for cleanup when suspend fails.
401 */
402static void data_free(void)
403{
404 swp_entry_t entry;
254b5477 405 struct pbe * p;
1da177e4 406
254b5477
RW
407 for_each_pbe(p, pagedir_nosave) {
408 entry = p->swap_address;
1da177e4
LT
409 if (entry.val)
410 swap_free(entry);
411 else
412 break;
1da177e4
LT
413 }
414}
415
416/**
417 * data_write - Write saved image to swap.
418 *
419 * Walk the list of pages in the image and sync each one to swap.
420 */
421static int data_write(void)
422{
423 int error = 0, i = 0;
424 unsigned int mod = nr_copy_pages / 100;
425 struct pbe *p;
c2ff18f4
AS
426 void *tfm;
427
428 if ((error = crypto_init(1, &tfm)))
429 return error;
1da177e4
LT
430
431 if (!mod)
432 mod = 1;
433
434 printk( "Writing data to swap (%d pages)... ", nr_copy_pages );
2e4d5822 435 for_each_pbe (p, pagedir_nosave) {
1da177e4
LT
436 if (!(i%mod))
437 printk( "\b\b\b\b%3d%%", i / mod );
c2ff18f4
AS
438 if ((error = crypto_write(p, tfm))) {
439 crypto_exit(tfm);
1da177e4 440 return error;
c2ff18f4 441 }
1da177e4
LT
442 i++;
443 }
444 printk("\b\b\b\bdone\n");
c2ff18f4 445 crypto_exit(tfm);
1da177e4
LT
446 return error;
447}
448
449static void dump_info(void)
450{
451 pr_debug(" swsusp: Version: %u\n",swsusp_info.version_code);
452 pr_debug(" swsusp: Num Pages: %ld\n",swsusp_info.num_physpages);
453 pr_debug(" swsusp: UTS Sys: %s\n",swsusp_info.uts.sysname);
454 pr_debug(" swsusp: UTS Node: %s\n",swsusp_info.uts.nodename);
455 pr_debug(" swsusp: UTS Release: %s\n",swsusp_info.uts.release);
456 pr_debug(" swsusp: UTS Version: %s\n",swsusp_info.uts.version);
457 pr_debug(" swsusp: UTS Machine: %s\n",swsusp_info.uts.machine);
458 pr_debug(" swsusp: UTS Domain: %s\n",swsusp_info.uts.domainname);
459 pr_debug(" swsusp: CPUs: %d\n",swsusp_info.cpus);
460 pr_debug(" swsusp: Image: %ld Pages\n",swsusp_info.image_pages);
461 pr_debug(" swsusp: Pagedir: %ld Pages\n",swsusp_info.pagedir_pages);
462}
463
464static void init_header(void)
465{
466 memset(&swsusp_info, 0, sizeof(swsusp_info));
467 swsusp_info.version_code = LINUX_VERSION_CODE;
468 swsusp_info.num_physpages = num_physpages;
469 memcpy(&swsusp_info.uts, &system_utsname, sizeof(system_utsname));
470
471 swsusp_info.suspend_pagedir = pagedir_nosave;
472 swsusp_info.cpus = num_online_cpus();
473 swsusp_info.image_pages = nr_copy_pages;
474}
475
476static int close_swap(void)
477{
478 swp_entry_t entry;
479 int error;
480
481 dump_info();
482 error = write_page((unsigned long)&swsusp_info, &entry);
2e4d5822 483 if (!error) {
1da177e4
LT
484 printk( "S" );
485 error = mark_swapfiles(entry);
486 printk( "|\n" );
487 }
488 return error;
489}
490
491/**
492 * free_pagedir_entries - Free pages used by the page directory.
493 *
494 * This is used during suspend for error recovery.
495 */
496
497static void free_pagedir_entries(void)
498{
499 int i;
500
501 for (i = 0; i < swsusp_info.pagedir_pages; i++)
502 swap_free(swsusp_info.pagedir[i]);
503}
504
505
506/**
507 * write_pagedir - Write the array of pages holding the page directory.
508 * @last: Last swap entry we write (needed for header).
509 */
510
511static int write_pagedir(void)
512{
513 int error = 0;
514 unsigned n = 0;
515 struct pbe * pbe;
516
517 printk( "Writing pagedir...");
2e4d5822 518 for_each_pb_page (pbe, pagedir_nosave) {
1da177e4
LT
519 if ((error = write_page((unsigned long)pbe, &swsusp_info.pagedir[n++])))
520 return error;
521 }
522
523 swsusp_info.pagedir_pages = n;
524 printk("done (%u pages)\n", n);
525 return error;
526}
527
528/**
529 * write_suspend_image - Write entire image and metadata.
530 *
531 */
1da177e4
LT
532static int write_suspend_image(void)
533{
534 int error;
535
536 init_header();
537 if ((error = data_write()))
538 goto FreeData;
539
540 if ((error = write_pagedir()))
541 goto FreePagedir;
542
543 if ((error = close_swap()))
544 goto FreePagedir;
545 Done:
c2ff18f4 546 memset(key_iv, 0, MAXKEY+MAXIV);
1da177e4
LT
547 return error;
548 FreePagedir:
549 free_pagedir_entries();
550 FreeData:
551 data_free();
552 goto Done;
553}
554
555
556#ifdef CONFIG_HIGHMEM
557struct highmem_page {
558 char *data;
559 struct page *page;
560 struct highmem_page *next;
561};
562
563static struct highmem_page *highmem_copy;
564
565static int save_highmem_zone(struct zone *zone)
566{
567 unsigned long zone_pfn;
568 mark_free_pages(zone);
569 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
570 struct page *page;
571 struct highmem_page *save;
572 void *kaddr;
573 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
574
575 if (!(pfn%1000))
576 printk(".");
577 if (!pfn_valid(pfn))
578 continue;
579 page = pfn_to_page(pfn);
580 /*
b5810039
NP
581 * PageReserved results from rvmalloc() sans vmalloc_32()
582 * and architectural memory reservations.
583 *
584 * rvmalloc should not cause this, because all implementations
585 * appear to always be using vmalloc_32 on architectures with
586 * highmem. This is a good thing, because we would like to save
587 * rvmalloc pages.
588 *
589 * It appears to be triggered by pages which do not point to
590 * valid memory (see arch/i386/mm/init.c:one_highpage_init(),
591 * which sets PageReserved if the page does not point to valid
592 * RAM.
593 *
594 * XXX: must remove usage of PageReserved!
1da177e4 595 */
b5810039 596 if (PageReserved(page))
1da177e4 597 continue;
1da177e4
LT
598 BUG_ON(PageNosave(page));
599 if (PageNosaveFree(page))
600 continue;
601 save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
602 if (!save)
603 return -ENOMEM;
604 save->next = highmem_copy;
605 save->page = page;
606 save->data = (void *) get_zeroed_page(GFP_ATOMIC);
607 if (!save->data) {
608 kfree(save);
609 return -ENOMEM;
610 }
611 kaddr = kmap_atomic(page, KM_USER0);
612 memcpy(save->data, kaddr, PAGE_SIZE);
613 kunmap_atomic(kaddr, KM_USER0);
614 highmem_copy = save;
615 }
616 return 0;
617}
618#endif /* CONFIG_HIGHMEM */
619
620
621static int save_highmem(void)
622{
623#ifdef CONFIG_HIGHMEM
624 struct zone *zone;
625 int res = 0;
626
627 pr_debug("swsusp: Saving Highmem\n");
2e4d5822 628 for_each_zone (zone) {
1da177e4
LT
629 if (is_highmem(zone))
630 res = save_highmem_zone(zone);
631 if (res)
632 return res;
633 }
634#endif
635 return 0;
636}
637
638static int restore_highmem(void)
639{
640#ifdef CONFIG_HIGHMEM
641 printk("swsusp: Restoring Highmem\n");
642 while (highmem_copy) {
643 struct highmem_page *save = highmem_copy;
644 void *kaddr;
645 highmem_copy = save->next;
646
647 kaddr = kmap_atomic(save->page, KM_USER0);
648 memcpy(kaddr, save->data, PAGE_SIZE);
649 kunmap_atomic(kaddr, KM_USER0);
650 free_page((long) save->data);
651 kfree(save);
652 }
653#endif
654 return 0;
655}
656
657
658static int pfn_is_nosave(unsigned long pfn)
659{
660 unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
661 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
662 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
663}
664
665/**
666 * saveable - Determine whether a page should be cloned or not.
667 * @pfn: The page
668 *
669 * We save a page if it's Reserved, and not in the range of pages
670 * statically defined as 'unsaveable', or if it isn't reserved, and
671 * isn't part of a free chunk of pages.
672 */
673
674static int saveable(struct zone * zone, unsigned long * zone_pfn)
675{
676 unsigned long pfn = *zone_pfn + zone->zone_start_pfn;
677 struct page * page;
678
679 if (!pfn_valid(pfn))
680 return 0;
681
682 page = pfn_to_page(pfn);
1da177e4
LT
683 if (PageNosave(page))
684 return 0;
b5810039 685 if (pfn_is_nosave(pfn)) {
1da177e4
LT
686 pr_debug("[nosave pfn 0x%lx]", pfn);
687 return 0;
688 }
689 if (PageNosaveFree(page))
690 return 0;
691
692 return 1;
693}
694
695static void count_data_pages(void)
696{
697 struct zone *zone;
698 unsigned long zone_pfn;
699
700 nr_copy_pages = 0;
701
2e4d5822 702 for_each_zone (zone) {
1da177e4
LT
703 if (is_highmem(zone))
704 continue;
705 mark_free_pages(zone);
706 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
707 nr_copy_pages += saveable(zone, &zone_pfn);
708 }
709}
710
1da177e4
LT
711static void copy_data_pages(void)
712{
713 struct zone *zone;
714 unsigned long zone_pfn;
351619ba 715 struct pbe *pbe = pagedir_nosave, *p;
2e4d5822 716
1da177e4 717 pr_debug("copy_data_pages(): pages to copy: %d\n", nr_copy_pages);
2e4d5822 718 for_each_zone (zone) {
1da177e4
LT
719 if (is_highmem(zone))
720 continue;
721 mark_free_pages(zone);
351619ba
RW
722 /* This is necessary for swsusp_free() */
723 for_each_pb_page (p, pagedir_nosave)
724 SetPageNosaveFree(virt_to_page(p));
725 for_each_pbe(p, pagedir_nosave)
726 SetPageNosaveFree(virt_to_page(p->address));
1da177e4
LT
727 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
728 if (saveable(zone, &zone_pfn)) {
729 struct page * page;
730 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
731 BUG_ON(!pbe);
351619ba 732 pbe->orig_address = (unsigned long)page_address(page);
1da177e4
LT
733 /* copy_page is not usable for copying task structs. */
734 memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE);
735 pbe = pbe->next;
736 }
737 }
738 }
739 BUG_ON(pbe);
740}
741
742
1da177e4
LT
743/**
744 * free_pagedir - free pages allocated with alloc_pagedir()
745 */
746
747static inline void free_pagedir(struct pbe *pblist)
748{
749 struct pbe *pbe;
750
751 while (pblist) {
752 pbe = (pblist + PB_PAGE_SKIP)->next;
351619ba
RW
753 ClearPageNosave(virt_to_page(pblist));
754 ClearPageNosaveFree(virt_to_page(pblist));
1da177e4
LT
755 free_page((unsigned long)pblist);
756 pblist = pbe;
757 }
758}
759
760/**
761 * fill_pb_page - Create a list of PBEs on a given memory page
762 */
763
764static inline void fill_pb_page(struct pbe *pbpage)
765{
766 struct pbe *p;
767
768 p = pbpage;
769 pbpage += PB_PAGE_SKIP;
770 do
771 p->next = p + 1;
772 while (++p < pbpage);
773}
774
775/**
776 * create_pbe_list - Create a list of PBEs on top of a given chain
777 * of memory pages allocated with alloc_pagedir()
778 */
779
780static void create_pbe_list(struct pbe *pblist, unsigned nr_pages)
781{
782 struct pbe *pbpage, *p;
783 unsigned num = PBES_PER_PAGE;
784
785 for_each_pb_page (pbpage, pblist) {
786 if (num >= nr_pages)
787 break;
788
789 fill_pb_page(pbpage);
790 num += PBES_PER_PAGE;
791 }
792 if (pbpage) {
793 for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
794 p->next = p + 1;
795 p->next = NULL;
796 }
797 pr_debug("create_pbe_list(): initialized %d PBEs\n", num);
798}
799
351619ba
RW
800static void *alloc_image_page(void)
801{
802 void *res = (void *)get_zeroed_page(GFP_ATOMIC | __GFP_COLD);
803 if (res) {
804 SetPageNosave(virt_to_page(res));
805 SetPageNosaveFree(virt_to_page(res));
806 }
807 return res;
808}
809
1da177e4
LT
810/**
811 * alloc_pagedir - Allocate the page directory.
812 *
813 * First, determine exactly how many pages we need and
814 * allocate them.
815 *
816 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
817 * struct pbe elements (pbes) and the last element in the page points
818 * to the next page.
819 *
820 * On each page we set up a list of struct_pbe elements.
821 */
822
823static struct pbe * alloc_pagedir(unsigned nr_pages)
824{
825 unsigned num;
826 struct pbe *pblist, *pbe;
827
828 if (!nr_pages)
829 return NULL;
830
831 pr_debug("alloc_pagedir(): nr_pages = %d\n", nr_pages);
351619ba 832 pblist = (struct pbe *)alloc_image_page();
1da177e4
LT
833 for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
834 pbe = pbe->next, num += PBES_PER_PAGE) {
835 pbe += PB_PAGE_SKIP;
351619ba 836 pbe->next = (struct pbe *)alloc_image_page();
1da177e4
LT
837 }
838 if (!pbe) { /* get_zeroed_page() failed */
839 free_pagedir(pblist);
840 pblist = NULL;
841 }
842 return pblist;
843}
844
845/**
351619ba
RW
846 * Free pages we allocated for suspend. Suspend pages are alocated
847 * before atomic copy, so we need to free them after resume.
1da177e4
LT
848 */
849
351619ba 850void swsusp_free(void)
1da177e4 851{
351619ba
RW
852 struct zone *zone;
853 unsigned long zone_pfn;
1da177e4 854
351619ba
RW
855 for_each_zone(zone) {
856 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
857 if (pfn_valid(zone_pfn + zone->zone_start_pfn)) {
858 struct page * page;
859 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
860 if (PageNosave(page) && PageNosaveFree(page)) {
861 ClearPageNosave(page);
862 ClearPageNosaveFree(page);
863 free_page((long) page_address(page));
864 }
865 }
1da177e4 866 }
1da177e4
LT
867}
868
1da177e4
LT
869/**
870 * enough_free_mem - Make sure we enough free memory to snapshot.
871 *
2e4d5822 872 * Returns TRUE or FALSE after checking the number of available
1da177e4
LT
873 * free pages.
874 */
875
876static int enough_free_mem(void)
877{
351619ba
RW
878 pr_debug("swsusp: available memory: %u pages\n", nr_free_pages());
879 return nr_free_pages() > (nr_copy_pages + PAGES_FOR_IO +
880 nr_copy_pages/PBES_PER_PAGE + !!(nr_copy_pages%PBES_PER_PAGE));
1da177e4
LT
881}
882
883
884/**
885 * enough_swap - Make sure we have enough swap to save the image.
886 *
2e4d5822 887 * Returns TRUE or FALSE after checking the total amount of swap
1da177e4
LT
888 * space avaiable.
889 *
890 * FIXME: si_swapinfo(&i) returns all swap devices information.
2e4d5822 891 * We should only consider resume_device.
1da177e4
LT
892 */
893
894static int enough_swap(void)
895{
896 struct sysinfo i;
897
898 si_swapinfo(&i);
351619ba
RW
899 pr_debug("swsusp: available swap: %lu pages\n", i.freeswap);
900 return i.freeswap > (nr_copy_pages + PAGES_FOR_IO +
901 nr_copy_pages/PBES_PER_PAGE + !!(nr_copy_pages%PBES_PER_PAGE));
1da177e4
LT
902}
903
904static int swsusp_alloc(void)
905{
351619ba 906 struct pbe * p;
1da177e4 907
c61978b3 908 pagedir_nosave = NULL;
0f7347c2 909
1da177e4
LT
910 if (!(pagedir_save = alloc_pagedir(nr_copy_pages))) {
911 printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
912 return -ENOMEM;
913 }
914 create_pbe_list(pagedir_save, nr_copy_pages);
915 pagedir_nosave = pagedir_save;
351619ba
RW
916
917 for_each_pbe (p, pagedir_save) {
918 p->address = (unsigned long)alloc_image_page();
919 if (!p->address) {
920 printk(KERN_ERR "suspend: Allocating image pages failed.\n");
921 swsusp_free();
922 return -ENOMEM;
923 }
1da177e4
LT
924 }
925
1da177e4
LT
926 return 0;
927}
928
929static int suspend_prepare_image(void)
930{
931 int error;
932
933 pr_debug("swsusp: critical section: \n");
934 if (save_highmem()) {
351619ba 935 printk(KERN_CRIT "swsusp: Not enough free pages for highmem\n");
1da177e4
LT
936 restore_highmem();
937 return -ENOMEM;
938 }
939
940 drain_local_pages();
941 count_data_pages();
942 printk("swsusp: Need to copy %u pages\n", nr_copy_pages);
351619ba
RW
943 nr_copy_pages_check = nr_copy_pages;
944
945 pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
946 nr_copy_pages,
947 nr_copy_pages/PBES_PER_PAGE + !!(nr_copy_pages%PBES_PER_PAGE),
948 PAGES_FOR_IO, nr_free_pages());
949
950 if (!enough_free_mem()) {
951 printk(KERN_ERR "swsusp: Not enough free memory\n");
952 return -ENOMEM;
953 }
954
955 if (MAX_PBES < nr_copy_pages / PBES_PER_PAGE +
956 !!(nr_copy_pages % PBES_PER_PAGE)) {
957 printk(KERN_ERR "swsusp: Too many image pages\n");
958 return -ENOSPC;
959 }
960
961 if (!enough_swap()) {
962 printk(KERN_ERR "swsusp: Not enough free swap\n");
963 return -ENOSPC;
964 }
1da177e4
LT
965
966 error = swsusp_alloc();
967 if (error)
968 return error;
2e4d5822
PM
969
970 /* During allocating of suspend pagedir, new cold pages may appear.
1da177e4
LT
971 * Kill them.
972 */
973 drain_local_pages();
974 copy_data_pages();
975
976 /*
977 * End of critical section. From now on, we can write to memory,
978 * but we should not touch disk. This specially means we must _not_
979 * touch swap space! Except we must write out our image of course.
980 */
981
982 printk("swsusp: critical section/: done (%d pages copied)\n", nr_copy_pages );
983 return 0;
984}
985
986
987/* It is important _NOT_ to umount filesystems at this point. We want
988 * them synced (in case something goes wrong) but we DO not want to mark
989 * filesystem clean: it is not. (And it does not matter, if we resume
990 * correctly, we'll mark system clean, anyway.)
991 */
992int swsusp_write(void)
993{
994 int error;
995 device_resume();
996 lock_swapdevices();
997 error = write_suspend_image();
998 /* This will unlock ignored swap devices since writing is finished */
999 lock_swapdevices();
1000 return error;
1001
1002}
1003
1004
1005extern asmlinkage int swsusp_arch_suspend(void);
1006extern asmlinkage int swsusp_arch_resume(void);
1007
1008
1009asmlinkage int swsusp_save(void)
1010{
1da177e4
LT
1011 return suspend_prepare_image();
1012}
1013
1014int swsusp_suspend(void)
1015{
1016 int error;
1017 if ((error = arch_prepare_suspend()))
1018 return error;
1019 local_irq_disable();
1020 /* At this point, device_suspend() has been called, but *not*
1021 * device_power_down(). We *must* device_power_down() now.
1022 * Otherwise, drivers for some devices (e.g. interrupt controllers)
1023 * become desynchronized with the actual state of the hardware
1024 * at resume time, and evil weirdness ensues.
1025 */
1026 if ((error = device_power_down(PMSG_FREEZE))) {
99dc7d63 1027 printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
1da177e4 1028 local_irq_enable();
1da177e4
LT
1029 return error;
1030 }
47b724f3
PM
1031
1032 if ((error = swsusp_swap_check())) {
99dc7d63
PM
1033 printk(KERN_ERR "swsusp: cannot find swap device, try swapon -a.\n");
1034 device_power_up();
47b724f3
PM
1035 local_irq_enable();
1036 return error;
1037 }
1038
1da177e4
LT
1039 save_processor_state();
1040 if ((error = swsusp_arch_suspend()))
99dc7d63 1041 printk(KERN_ERR "Error %d suspending\n", error);
1da177e4
LT
1042 /* Restore control flow magically appears here */
1043 restore_processor_state();
1044 BUG_ON (nr_copy_pages_check != nr_copy_pages);
1045 restore_highmem();
1046 device_power_up();
1047 local_irq_enable();
1048 return error;
1049}
1050
1051int swsusp_resume(void)
1052{
1053 int error;
1054 local_irq_disable();
1055 if (device_power_down(PMSG_FREEZE))
1056 printk(KERN_ERR "Some devices failed to power down, very bad\n");
1057 /* We'll ignore saved state, but this gets preempt count (etc) right */
1058 save_processor_state();
1059 error = swsusp_arch_resume();
1060 /* Code below is only ever reached in case of failure. Otherwise
1061 * execution continues at place where swsusp_arch_suspend was called
1062 */
1063 BUG_ON(!error);
1064 restore_processor_state();
1065 restore_highmem();
8446f1d3 1066 touch_softlockup_watchdog();
1da177e4
LT
1067 device_power_up();
1068 local_irq_enable();
1069 return error;
1070}
1071
1da177e4
LT
1072/**
1073 * On resume, for storing the PBE list and the image,
1074 * we can only use memory pages that do not conflict with the pages
1075 * which had been used before suspend.
1076 *
1077 * We don't know which pages are usable until we allocate them.
1078 *
1079 * Allocated but unusable (ie eaten) memory pages are linked together
1080 * to create a list, so that we can free them easily
1081 *
1082 * We could have used a type other than (void *)
1083 * for this purpose, but ...
1084 */
1085static void **eaten_memory = NULL;
1086
1087static inline void eat_page(void *page)
1088{
1089 void **c;
1090
1091 c = eaten_memory;
1092 eaten_memory = page;
1093 *eaten_memory = c;
1094}
1095
9796fdd8 1096unsigned long get_usable_page(gfp_t gfp_mask)
1da177e4
LT
1097{
1098 unsigned long m;
1099
1100 m = get_zeroed_page(gfp_mask);
8f9bdf15 1101 while (!PageNosaveFree(virt_to_page(m))) {
1da177e4
LT
1102 eat_page((void *)m);
1103 m = get_zeroed_page(gfp_mask);
1104 if (!m)
1105 break;
1106 }
1107 return m;
1108}
1109
3dd08325 1110void free_eaten_memory(void)
1da177e4
LT
1111{
1112 unsigned long m;
1113 void **c;
1114 int i = 0;
1115
1116 c = eaten_memory;
1117 while (c) {
1118 m = (unsigned long)c;
1119 c = *c;
1120 free_page(m);
1121 i++;
1122 }
1123 eaten_memory = NULL;
1124 pr_debug("swsusp: %d unused pages freed\n", i);
1125}
1126
1127/**
1128 * check_pagedir - We ensure here that pages that the PBEs point to
1129 * won't collide with pages where we're going to restore from the loaded
1130 * pages later
1131 */
1132
1133static int check_pagedir(struct pbe *pblist)
1134{
1135 struct pbe *p;
1136
1137 /* This is necessary, so that we can free allocated pages
1138 * in case of failure
1139 */
1140 for_each_pbe (p, pblist)
1141 p->address = 0UL;
1142
1143 for_each_pbe (p, pblist) {
1144 p->address = get_usable_page(GFP_ATOMIC);
1145 if (!p->address)
1146 return -ENOMEM;
1147 }
1148 return 0;
1149}
1150
1151/**
1152 * swsusp_pagedir_relocate - It is possible, that some memory pages
1153 * occupied by the list of PBEs collide with pages where we're going to
1154 * restore from the loaded pages later. We relocate them here.
1155 */
1156
1157static struct pbe * swsusp_pagedir_relocate(struct pbe *pblist)
1158{
1159 struct zone *zone;
1160 unsigned long zone_pfn;
1161 struct pbe *pbpage, *tail, *p;
1162 void *m;
1163 int rel = 0, error = 0;
1164
1165 if (!pblist) /* a sanity check */
1166 return NULL;
1167
1168 pr_debug("swsusp: Relocating pagedir (%lu pages to check)\n",
1169 swsusp_info.pagedir_pages);
1170
1171 /* Set page flags */
1172
2e4d5822 1173 for_each_zone (zone) {
1da177e4
LT
1174 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
1175 SetPageNosaveFree(pfn_to_page(zone_pfn +
1176 zone->zone_start_pfn));
1177 }
1178
1179 /* Clear orig addresses */
1180
1181 for_each_pbe (p, pblist)
1182 ClearPageNosaveFree(virt_to_page(p->orig_address));
1183
1184 tail = pblist + PB_PAGE_SKIP;
1185
1186 /* Relocate colliding pages */
1187
1188 for_each_pb_page (pbpage, pblist) {
8f9bdf15 1189 if (!PageNosaveFree(virt_to_page((unsigned long)pbpage))) {
1da177e4
LT
1190 m = (void *)get_usable_page(GFP_ATOMIC | __GFP_COLD);
1191 if (!m) {
1192 error = -ENOMEM;
1193 break;
1194 }
1195 memcpy(m, (void *)pbpage, PAGE_SIZE);
1196 if (pbpage == pblist)
1197 pblist = (struct pbe *)m;
1198 else
1199 tail->next = (struct pbe *)m;
1200
1201 eat_page((void *)pbpage);
1202 pbpage = (struct pbe *)m;
1203
1204 /* We have to link the PBEs again */
1205
1206 for (p = pbpage; p < pbpage + PB_PAGE_SKIP; p++)
1207 if (p->next) /* needed to save the end */
1208 p->next = p + 1;
1209
1210 rel++;
1211 }
1212 tail = pbpage + PB_PAGE_SKIP;
1213 }
1214
1215 if (error) {
1216 printk("\nswsusp: Out of memory\n\n");
1217 free_pagedir(pblist);
1218 free_eaten_memory();
1219 pblist = NULL;
8686bcd0
PM
1220 /* Is this even worth handling? It should never ever happen, and we
1221 have just lost user's state, anyway... */
1222 } else
1da177e4
LT
1223 printk("swsusp: Relocated %d pages\n", rel);
1224
1225 return pblist;
1226}
1227
4dc3b16b 1228/*
1da177e4
LT
1229 * Using bio to read from swap.
1230 * This code requires a bit more work than just using buffer heads
1231 * but, it is the recommended way for 2.5/2.6.
1232 * The following are to signal the beginning and end of I/O. Bios
1233 * finish asynchronously, while we want them to happen synchronously.
1234 * A simple atomic_t, and a wait loop take care of this problem.
1235 */
1236
1237static atomic_t io_done = ATOMIC_INIT(0);
1238
1239static int end_io(struct bio * bio, unsigned int num, int err)
1240{
1241 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
1242 panic("I/O error reading memory image");
1243 atomic_set(&io_done, 0);
1244 return 0;
1245}
1246
1247static struct block_device * resume_bdev;
1248
1249/**
1250 * submit - submit BIO request.
1251 * @rw: READ or WRITE.
1252 * @off physical offset of page.
1253 * @page: page we're reading or writing.
1254 *
1255 * Straight from the textbook - allocate and initialize the bio.
1256 * If we're writing, make sure the page is marked as dirty.
1257 * Then submit it and wait.
1258 */
1259
1260static int submit(int rw, pgoff_t page_off, void * page)
1261{
1262 int error = 0;
1263 struct bio * bio;
1264
1265 bio = bio_alloc(GFP_ATOMIC, 1);
1266 if (!bio)
1267 return -ENOMEM;
1268 bio->bi_sector = page_off * (PAGE_SIZE >> 9);
1269 bio_get(bio);
1270 bio->bi_bdev = resume_bdev;
1271 bio->bi_end_io = end_io;
1272
1273 if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
1274 printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
1275 error = -EFAULT;
1276 goto Done;
1277 }
1278
1279 if (rw == WRITE)
1280 bio_set_pages_dirty(bio);
1281
1282 atomic_set(&io_done, 1);
1283 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
1284 while (atomic_read(&io_done))
1285 yield();
1286
1287 Done:
1288 bio_put(bio);
1289 return error;
1290}
1291
1292static int bio_read_page(pgoff_t page_off, void * page)
1293{
1294 return submit(READ, page_off, page);
1295}
1296
1297static int bio_write_page(pgoff_t page_off, void * page)
1298{
1299 return submit(WRITE, page_off, page);
1300}
1301
1302/*
1303 * Sanity check if this image makes sense with this kernel/swap context
1304 * I really don't think that it's foolproof but more than nothing..
1305 */
1306
1307static const char * sanity_check(void)
1308{
1309 dump_info();
47b724f3 1310 if (swsusp_info.version_code != LINUX_VERSION_CODE)
1da177e4 1311 return "kernel version";
47b724f3 1312 if (swsusp_info.num_physpages != num_physpages)
1da177e4
LT
1313 return "memory size";
1314 if (strcmp(swsusp_info.uts.sysname,system_utsname.sysname))
1315 return "system type";
1316 if (strcmp(swsusp_info.uts.release,system_utsname.release))
1317 return "kernel release";
1318 if (strcmp(swsusp_info.uts.version,system_utsname.version))
1319 return "version";
1320 if (strcmp(swsusp_info.uts.machine,system_utsname.machine))
1321 return "machine";
5a72e04d 1322#if 0
99dc7d63
PM
1323 /* We can't use number of online CPUs when we use hotplug to remove them ;-))) */
1324 if (swsusp_info.cpus != num_possible_cpus())
1da177e4 1325 return "number of cpus";
5a72e04d 1326#endif
1da177e4
LT
1327 return NULL;
1328}
1329
1330
1331static int check_header(void)
1332{
1333 const char * reason = NULL;
1334 int error;
1335
1336 if ((error = bio_read_page(swp_offset(swsusp_header.swsusp_info), &swsusp_info)))
1337 return error;
1338
1339 /* Is this same machine? */
1340 if ((reason = sanity_check())) {
1341 printk(KERN_ERR "swsusp: Resume mismatch: %s\n",reason);
1342 return -EPERM;
1343 }
1344 nr_copy_pages = swsusp_info.image_pages;
1345 return error;
1346}
1347
1348static int check_sig(void)
1349{
1350 int error;
1351
1352 memset(&swsusp_header, 0, sizeof(swsusp_header));
1353 if ((error = bio_read_page(0, &swsusp_header)))
1354 return error;
1355 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
1356 memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
c2ff18f4
AS
1357 memcpy(key_iv, swsusp_header.key_iv, MAXKEY+MAXIV);
1358 memset(swsusp_header.key_iv, 0, MAXKEY+MAXIV);
1da177e4
LT
1359
1360 /*
1361 * Reset swap signature now.
1362 */
1363 error = bio_write_page(0, &swsusp_header);
1364 } else {
1da177e4
LT
1365 return -EINVAL;
1366 }
1367 if (!error)
1368 pr_debug("swsusp: Signature found, resuming\n");
1369 return error;
1370}
1371
1372/**
1373 * data_read - Read image pages from swap.
1374 *
1375 * You do not need to check for overlaps, check_pagedir()
1376 * already did that.
1377 */
1378
1379static int data_read(struct pbe *pblist)
1380{
1381 struct pbe * p;
1382 int error = 0;
1383 int i = 0;
1384 int mod = swsusp_info.image_pages / 100;
c2ff18f4
AS
1385 void *tfm;
1386
1387 if ((error = crypto_init(0, &tfm)))
1388 return error;
1da177e4
LT
1389
1390 if (!mod)
1391 mod = 1;
1392
1393 printk("swsusp: Reading image data (%lu pages): ",
1394 swsusp_info.image_pages);
1395
1396 for_each_pbe (p, pblist) {
1397 if (!(i % mod))
1398 printk("\b\b\b\b%3d%%", i / mod);
1399
c2ff18f4
AS
1400 if ((error = crypto_read(p, tfm))) {
1401 crypto_exit(tfm);
1da177e4 1402 return error;
c2ff18f4 1403 }
1da177e4
LT
1404
1405 i++;
1406 }
1407 printk("\b\b\b\bdone\n");
c2ff18f4 1408 crypto_exit(tfm);
1da177e4
LT
1409 return error;
1410}
1411
1da177e4
LT
1412/**
1413 * read_pagedir - Read page backup list pages from swap
1414 */
1415
1416static int read_pagedir(struct pbe *pblist)
1417{
1418 struct pbe *pbpage, *p;
1419 unsigned i = 0;
1420 int error;
1421
1422 if (!pblist)
1423 return -EFAULT;
1424
1425 printk("swsusp: Reading pagedir (%lu pages)\n",
1426 swsusp_info.pagedir_pages);
1427
1428 for_each_pb_page (pbpage, pblist) {
1429 unsigned long offset = swp_offset(swsusp_info.pagedir[i++]);
1430
1431 error = -EFAULT;
1432 if (offset) {
1433 p = (pbpage + PB_PAGE_SKIP)->next;
1434 error = bio_read_page(offset, (void *)pbpage);
1435 (pbpage + PB_PAGE_SKIP)->next = p;
1436 }
1437 if (error)
1438 break;
1439 }
1440
1441 if (error)
f2d61379
RW
1442 free_pagedir(pblist);
1443 else
1444 BUG_ON(i != swsusp_info.pagedir_pages);
1da177e4
LT
1445
1446 return error;
1447}
1448
1449
1450static int check_suspend_image(void)
1451{
1452 int error = 0;
1453
1454 if ((error = check_sig()))
1455 return error;
1456
1457 if ((error = check_header()))
1458 return error;
1459
1460 return 0;
1461}
1462
1463static int read_suspend_image(void)
1464{
1465 int error = 0;
1466 struct pbe *p;
1467
1468 if (!(p = alloc_pagedir(nr_copy_pages)))
1469 return -ENOMEM;
1470
1471 if ((error = read_pagedir(p)))
1472 return error;
1473
1474 create_pbe_list(p, nr_copy_pages);
1475
1476 if (!(pagedir_nosave = swsusp_pagedir_relocate(p)))
1477 return -ENOMEM;
1478
1479 /* Allocate memory for the image and read the data from swap */
1480
1481 error = check_pagedir(pagedir_nosave);
3dd08325 1482
1da177e4
LT
1483 if (!error)
1484 error = data_read(pagedir_nosave);
1485
1486 if (error) { /* We fail cleanly */
3dd08325 1487 free_eaten_memory();
1da177e4
LT
1488 for_each_pbe (p, pagedir_nosave)
1489 if (p->address) {
1490 free_page(p->address);
1491 p->address = 0UL;
1492 }
1493 free_pagedir(pagedir_nosave);
1494 }
1495 return error;
1496}
1497
1498/**
1499 * swsusp_check - Check for saved image in swap
1500 */
1501
1502int swsusp_check(void)
1503{
1504 int error;
1505
1da177e4
LT
1506 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
1507 if (!IS_ERR(resume_bdev)) {
1508 set_blocksize(resume_bdev, PAGE_SIZE);
1509 error = check_suspend_image();
1510 if (error)
1511 blkdev_put(resume_bdev);
1512 } else
1513 error = PTR_ERR(resume_bdev);
1514
1515 if (!error)
1516 pr_debug("swsusp: resume file found\n");
1517 else
1518 pr_debug("swsusp: Error %d check for resume file\n", error);
1519 return error;
1520}
1521
1522/**
1523 * swsusp_read - Read saved image from swap.
1524 */
1525
1526int swsusp_read(void)
1527{
1528 int error;
1529
1530 if (IS_ERR(resume_bdev)) {
1531 pr_debug("swsusp: block device not initialised\n");
1532 return PTR_ERR(resume_bdev);
1533 }
1534
1535 error = read_suspend_image();
1536 blkdev_put(resume_bdev);
c2ff18f4 1537 memset(key_iv, 0, MAXKEY+MAXIV);
1da177e4
LT
1538
1539 if (!error)
1540 pr_debug("swsusp: Reading resume file was successful\n");
1541 else
1542 pr_debug("swsusp: Error %d resuming\n", error);
1543 return error;
1544}
1545
1546/**
1547 * swsusp_close - close swap device.
1548 */
1549
1550void swsusp_close(void)
1551{
1552 if (IS_ERR(resume_bdev)) {
1553 pr_debug("swsusp: block device not initialised\n");
1554 return;
1555 }
1556
1557 blkdev_put(resume_bdev);
1558}
This page took 0.141739 seconds and 5 git commands to generate.