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