]> git.karo-electronics.de Git - karo-tx-linux.git/blob - kernel/power/swsusp.c
[PATCH] swsusp: reduce code duplication
[karo-tx-linux.git] / kernel / power / swsusp.c
1 /*
2  * linux/kernel/power/swsusp.c
3  *
4  * This file provides code to write suspend image to swap and read it back.
5  *
6  * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
7  * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
8  *
9  * This file is released under the GPLv2.
10  *
11  * I'd like to thank the following people for their work:
12  *
13  * Pavel Machek <pavel@ucw.cz>:
14  * Modifications, defectiveness pointing, being with me at the very beginning,
15  * suspend to swap space, stop all tasks. Port to 2.4.18-ac and 2.5.17.
16  *
17  * Steve Doddi <dirk@loth.demon.co.uk>:
18  * Support the possibility of hardware state restoring.
19  *
20  * Raph <grey.havens@earthling.net>:
21  * Support for preserving states of network devices and virtual console
22  * (including X and svgatextmode)
23  *
24  * Kurt Garloff <garloff@suse.de>:
25  * Straightened the critical function in order to prevent compilers from
26  * playing tricks with local variables.
27  *
28  * Andreas Mohr <a.mohr@mailto.de>
29  *
30  * Alex Badea <vampire@go.ro>:
31  * Fixed runaway init
32  *
33  * Andreas Steinmetz <ast@domdv.de>:
34  * Added encrypted suspend option
35  *
36  * More state savers are welcome. Especially for the scsi layer...
37  *
38  * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
39  */
40
41 #include <linux/module.h>
42 #include <linux/mm.h>
43 #include <linux/suspend.h>
44 #include <linux/smp_lock.h>
45 #include <linux/file.h>
46 #include <linux/utsname.h>
47 #include <linux/version.h>
48 #include <linux/delay.h>
49 #include <linux/bitops.h>
50 #include <linux/spinlock.h>
51 #include <linux/genhd.h>
52 #include <linux/kernel.h>
53 #include <linux/major.h>
54 #include <linux/swap.h>
55 #include <linux/pm.h>
56 #include <linux/device.h>
57 #include <linux/buffer_head.h>
58 #include <linux/swapops.h>
59 #include <linux/bootmem.h>
60 #include <linux/syscalls.h>
61 #include <linux/highmem.h>
62 #include <linux/bio.h>
63
64 #include <asm/uaccess.h>
65 #include <asm/mmu_context.h>
66 #include <asm/pgtable.h>
67 #include <asm/tlbflush.h>
68 #include <asm/io.h>
69
70 #include <linux/random.h>
71 #include <linux/crypto.h>
72 #include <asm/scatterlist.h>
73
74 #include "power.h"
75
76 #define CIPHER "aes"
77 #define MAXKEY 32
78 #define MAXIV  32
79
80 extern char resume_file[];
81
82 /* Local variables that should not be affected by save */
83 unsigned int nr_copy_pages __nosavedata = 0;
84
85 /* Suspend pagedir is allocated before final copy, therefore it
86    must be freed after resume
87
88    Warning: this is even more evil than it seems. Pagedirs this file
89    talks about are completely different from page directories used by
90    MMU hardware.
91  */
92 suspend_pagedir_t *pagedir_nosave __nosavedata = NULL;
93
94 #define SWSUSP_SIG      "S1SUSPEND"
95
96 static struct swsusp_header {
97         char reserved[PAGE_SIZE - 20 - MAXKEY - MAXIV - sizeof(swp_entry_t)];
98         u8 key_iv[MAXKEY+MAXIV];
99         swp_entry_t swsusp_info;
100         char    orig_sig[10];
101         char    sig[10];
102 } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
103
104 static struct swsusp_info swsusp_info;
105
106 /*
107  * Saving part...
108  */
109
110 /* We memorize in swapfile_used what swap devices are used for suspension */
111 #define SWAPFILE_UNUSED    0
112 #define SWAPFILE_SUSPEND   1    /* This is the suspending device */
113 #define SWAPFILE_IGNORED   2    /* Those are other swap devices ignored for suspension */
114
115 static unsigned short swapfile_used[MAX_SWAPFILES];
116 static unsigned short root_swap;
117
118 static int write_page(unsigned long addr, swp_entry_t *loc);
119 static int bio_read_page(pgoff_t page_off, void *page);
120
121 static u8 key_iv[MAXKEY+MAXIV];
122
123 #ifdef CONFIG_SWSUSP_ENCRYPT
124
125 static int crypto_init(int mode, void **mem)
126 {
127         int error = 0;
128         int len;
129         char *modemsg;
130         struct crypto_tfm *tfm;
131
132         modemsg = mode ? "suspend not possible" : "resume not possible";
133
134         tfm = crypto_alloc_tfm(CIPHER, CRYPTO_TFM_MODE_CBC);
135         if(!tfm) {
136                 printk(KERN_ERR "swsusp: no tfm, %s\n", modemsg);
137                 error = -EINVAL;
138                 goto out;
139         }
140
141         if(MAXKEY < crypto_tfm_alg_min_keysize(tfm)) {
142                 printk(KERN_ERR "swsusp: key buffer too small, %s\n", modemsg);
143                 error = -ENOKEY;
144                 goto fail;
145         }
146
147         if (mode)
148                 get_random_bytes(key_iv, MAXKEY+MAXIV);
149
150         len = crypto_tfm_alg_max_keysize(tfm);
151         if (len > MAXKEY)
152                 len = MAXKEY;
153
154         if (crypto_cipher_setkey(tfm, key_iv, len)) {
155                 printk(KERN_ERR "swsusp: key setup failure, %s\n", modemsg);
156                 error = -EKEYREJECTED;
157                 goto fail;
158         }
159
160         len = crypto_tfm_alg_ivsize(tfm);
161
162         if (MAXIV < len) {
163                 printk(KERN_ERR "swsusp: iv buffer too small, %s\n", modemsg);
164                 error = -EOVERFLOW;
165                 goto fail;
166         }
167
168         crypto_cipher_set_iv(tfm, key_iv+MAXKEY, len);
169
170         *mem=(void *)tfm;
171
172         goto out;
173
174 fail:   crypto_free_tfm(tfm);
175 out:    return error;
176 }
177
178 static __inline__ void crypto_exit(void *mem)
179 {
180         crypto_free_tfm((struct crypto_tfm *)mem);
181 }
182
183 static __inline__ int crypto_write(struct pbe *p, void *mem)
184 {
185         int error = 0;
186         struct scatterlist src, dst;
187
188         src.page   = virt_to_page(p->address);
189         src.offset = 0;
190         src.length = PAGE_SIZE;
191         dst.page   = virt_to_page((void *)&swsusp_header);
192         dst.offset = 0;
193         dst.length = PAGE_SIZE;
194
195         error = crypto_cipher_encrypt((struct crypto_tfm *)mem, &dst, &src,
196                                         PAGE_SIZE);
197
198         if (!error)
199                 error = write_page((unsigned long)&swsusp_header,
200                                 &(p->swap_address));
201         return error;
202 }
203
204 static __inline__ int crypto_read(struct pbe *p, void *mem)
205 {
206         int error = 0;
207         struct scatterlist src, dst;
208
209         error = bio_read_page(swp_offset(p->swap_address), (void *)p->address);
210         if (!error) {
211                 src.offset = 0;
212                 src.length = PAGE_SIZE;
213                 dst.offset = 0;
214                 dst.length = PAGE_SIZE;
215                 src.page = dst.page = virt_to_page((void *)p->address);
216
217                 error = crypto_cipher_decrypt((struct crypto_tfm *)mem, &dst,
218                                                 &src, PAGE_SIZE);
219         }
220         return error;
221 }
222 #else
223 static __inline__ int crypto_init(int mode, void *mem)
224 {
225         return 0;
226 }
227
228 static __inline__ void crypto_exit(void *mem)
229 {
230 }
231
232 static __inline__ int crypto_write(struct pbe *p, void *mem)
233 {
234         return write_page(p->address, &(p->swap_address));
235 }
236
237 static __inline__ int crypto_read(struct pbe *p, void *mem)
238 {
239         return bio_read_page(swp_offset(p->swap_address), (void *)p->address);
240 }
241 #endif
242
243 static int mark_swapfiles(swp_entry_t prev)
244 {
245         int error;
246
247         rw_swap_page_sync(READ,
248                           swp_entry(root_swap, 0),
249                           virt_to_page((unsigned long)&swsusp_header));
250         if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
251             !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
252                 memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
253                 memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
254                 memcpy(swsusp_header.key_iv, key_iv, MAXKEY+MAXIV);
255                 swsusp_header.swsusp_info = prev;
256                 error = rw_swap_page_sync(WRITE,
257                                           swp_entry(root_swap, 0),
258                                           virt_to_page((unsigned long)
259                                                        &swsusp_header));
260         } else {
261                 pr_debug("swsusp: Partition is not swap space.\n");
262                 error = -ENODEV;
263         }
264         return error;
265 }
266
267 /*
268  * Check whether the swap device is the specified resume
269  * device, irrespective of whether they are specified by
270  * identical names.
271  *
272  * (Thus, device inode aliasing is allowed.  You can say /dev/hda4
273  * instead of /dev/ide/host0/bus0/target0/lun0/part4 [if using devfs]
274  * and they'll be considered the same device.  This is *necessary* for
275  * devfs, since the resume code can only recognize the form /dev/hda4,
276  * but the suspend code would see the long name.)
277  */
278 static int is_resume_device(const struct swap_info_struct *swap_info)
279 {
280         struct file *file = swap_info->swap_file;
281         struct inode *inode = file->f_dentry->d_inode;
282
283         return S_ISBLK(inode->i_mode) &&
284                 swsusp_resume_device == MKDEV(imajor(inode), iminor(inode));
285 }
286
287 static int swsusp_swap_check(void) /* This is called before saving image */
288 {
289         int i, len;
290
291         len=strlen(resume_file);
292         root_swap = 0xFFFF;
293
294         spin_lock(&swap_lock);
295         for (i=0; i<MAX_SWAPFILES; i++) {
296                 if (!(swap_info[i].flags & SWP_WRITEOK)) {
297                         swapfile_used[i]=SWAPFILE_UNUSED;
298                 } else {
299                         if (!len) {
300                                 printk(KERN_WARNING "resume= option should be used to set suspend device" );
301                                 if (root_swap == 0xFFFF) {
302                                         swapfile_used[i] = SWAPFILE_SUSPEND;
303                                         root_swap = i;
304                                 } else
305                                         swapfile_used[i] = SWAPFILE_IGNORED;
306                         } else {
307                                 /* we ignore all swap devices that are not the resume_file */
308                                 if (is_resume_device(&swap_info[i])) {
309                                         swapfile_used[i] = SWAPFILE_SUSPEND;
310                                         root_swap = i;
311                                 } else {
312                                         swapfile_used[i] = SWAPFILE_IGNORED;
313                                 }
314                         }
315                 }
316         }
317         spin_unlock(&swap_lock);
318         return (root_swap != 0xffff) ? 0 : -ENODEV;
319 }
320
321 /**
322  * This is called after saving image so modification
323  * will be lost after resume... and that's what we want.
324  * we make the device unusable. A new call to
325  * lock_swapdevices can unlock the devices.
326  */
327 static void lock_swapdevices(void)
328 {
329         int i;
330
331         spin_lock(&swap_lock);
332         for (i = 0; i< MAX_SWAPFILES; i++)
333                 if (swapfile_used[i] == SWAPFILE_IGNORED) {
334                         swap_info[i].flags ^= SWP_WRITEOK;
335                 }
336         spin_unlock(&swap_lock);
337 }
338
339 /**
340  *      write_page - Write one page to a fresh swap location.
341  *      @addr:  Address we're writing.
342  *      @loc:   Place to store the entry we used.
343  *
344  *      Allocate a new swap entry and 'sync' it. Note we discard -EIO
345  *      errors. That is an artifact left over from swsusp. It did not
346  *      check the return of rw_swap_page_sync() at all, since most pages
347  *      written back to swap would return -EIO.
348  *      This is a partial improvement, since we will at least return other
349  *      errors, though we need to eventually fix the damn code.
350  */
351 static int write_page(unsigned long addr, swp_entry_t *loc)
352 {
353         swp_entry_t entry;
354         int error = 0;
355
356         entry = get_swap_page();
357         if (swp_offset(entry) &&
358             swapfile_used[swp_type(entry)] == SWAPFILE_SUSPEND) {
359                 error = rw_swap_page_sync(WRITE, entry,
360                                           virt_to_page(addr));
361                 if (error == -EIO)
362                         error = 0;
363                 if (!error)
364                         *loc = entry;
365         } else
366                 error = -ENOSPC;
367         return error;
368 }
369
370 /**
371  *      data_free - Free the swap entries used by the saved image.
372  *
373  *      Walk the list of used swap entries and free each one.
374  *      This is only used for cleanup when suspend fails.
375  */
376 static void data_free(void)
377 {
378         swp_entry_t entry;
379         struct pbe *p;
380
381         for_each_pbe (p, pagedir_nosave) {
382                 entry = p->swap_address;
383                 if (entry.val)
384                         swap_free(entry);
385                 else
386                         break;
387         }
388 }
389
390 /**
391  *      data_write - Write saved image to swap.
392  *
393  *      Walk the list of pages in the image and sync each one to swap.
394  */
395 static int data_write(void)
396 {
397         int error = 0, i = 0;
398         unsigned int mod = nr_copy_pages / 100;
399         struct pbe *p;
400         void *tfm;
401
402         if ((error = crypto_init(1, &tfm)))
403                 return error;
404
405         if (!mod)
406                 mod = 1;
407
408         printk( "Writing data to swap (%d pages)...     ", nr_copy_pages );
409         for_each_pbe (p, pagedir_nosave) {
410                 if (!(i%mod))
411                         printk( "\b\b\b\b%3d%%", i / mod );
412                 if ((error = crypto_write(p, tfm))) {
413                         crypto_exit(tfm);
414                         return error;
415                 }
416                 i++;
417         }
418         printk("\b\b\b\bdone\n");
419         crypto_exit(tfm);
420         return error;
421 }
422
423 static void dump_info(void)
424 {
425         pr_debug(" swsusp: Version: %u\n",swsusp_info.version_code);
426         pr_debug(" swsusp: Num Pages: %ld\n",swsusp_info.num_physpages);
427         pr_debug(" swsusp: UTS Sys: %s\n",swsusp_info.uts.sysname);
428         pr_debug(" swsusp: UTS Node: %s\n",swsusp_info.uts.nodename);
429         pr_debug(" swsusp: UTS Release: %s\n",swsusp_info.uts.release);
430         pr_debug(" swsusp: UTS Version: %s\n",swsusp_info.uts.version);
431         pr_debug(" swsusp: UTS Machine: %s\n",swsusp_info.uts.machine);
432         pr_debug(" swsusp: UTS Domain: %s\n",swsusp_info.uts.domainname);
433         pr_debug(" swsusp: CPUs: %d\n",swsusp_info.cpus);
434         pr_debug(" swsusp: Image: %ld Pages\n",swsusp_info.image_pages);
435         pr_debug(" swsusp: Pagedir: %ld Pages\n",swsusp_info.pagedir_pages);
436 }
437
438 static void init_header(void)
439 {
440         memset(&swsusp_info, 0, sizeof(swsusp_info));
441         swsusp_info.version_code = LINUX_VERSION_CODE;
442         swsusp_info.num_physpages = num_physpages;
443         memcpy(&swsusp_info.uts, &system_utsname, sizeof(system_utsname));
444
445         swsusp_info.suspend_pagedir = pagedir_nosave;
446         swsusp_info.cpus = num_online_cpus();
447         swsusp_info.image_pages = nr_copy_pages;
448 }
449
450 static int close_swap(void)
451 {
452         swp_entry_t entry;
453         int error;
454
455         dump_info();
456         error = write_page((unsigned long)&swsusp_info, &entry);
457         if (!error) {
458                 printk( "S" );
459                 error = mark_swapfiles(entry);
460                 printk( "|\n" );
461         }
462         return error;
463 }
464
465 /**
466  *      free_pagedir_entries - Free pages used by the page directory.
467  *
468  *      This is used during suspend for error recovery.
469  */
470
471 static void free_pagedir_entries(void)
472 {
473         int i;
474
475         for (i = 0; i < swsusp_info.pagedir_pages; i++)
476                 swap_free(swsusp_info.pagedir[i]);
477 }
478
479
480 /**
481  *      write_pagedir - Write the array of pages holding the page directory.
482  *      @last:  Last swap entry we write (needed for header).
483  */
484
485 static int write_pagedir(void)
486 {
487         int error = 0;
488         unsigned int n = 0;
489         struct pbe *pbe;
490
491         printk( "Writing pagedir...");
492         for_each_pb_page (pbe, pagedir_nosave) {
493                 if ((error = write_page((unsigned long)pbe, &swsusp_info.pagedir[n++])))
494                         return error;
495         }
496
497         swsusp_info.pagedir_pages = n;
498         printk("done (%u pages)\n", n);
499         return error;
500 }
501
502 /**
503  *      write_suspend_image - Write entire image and metadata.
504  *
505  */
506 static int write_suspend_image(void)
507 {
508         int error;
509
510         init_header();
511         if ((error = data_write()))
512                 goto FreeData;
513
514         if ((error = write_pagedir()))
515                 goto FreePagedir;
516
517         if ((error = close_swap()))
518                 goto FreePagedir;
519  Done:
520         memset(key_iv, 0, MAXKEY+MAXIV);
521         return error;
522  FreePagedir:
523         free_pagedir_entries();
524  FreeData:
525         data_free();
526         goto Done;
527 }
528
529 /**
530  *      enough_swap - Make sure we have enough swap to save the image.
531  *
532  *      Returns TRUE or FALSE after checking the total amount of swap
533  *      space avaiable.
534  *
535  *      FIXME: si_swapinfo(&i) returns all swap devices information.
536  *      We should only consider resume_device.
537  */
538
539 int enough_swap(unsigned int nr_pages)
540 {
541         struct sysinfo i;
542
543         si_swapinfo(&i);
544         pr_debug("swsusp: available swap: %lu pages\n", i.freeswap);
545         return i.freeswap > (nr_pages + PAGES_FOR_IO +
546                 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
547 }
548
549
550 /* It is important _NOT_ to umount filesystems at this point. We want
551  * them synced (in case something goes wrong) but we DO not want to mark
552  * filesystem clean: it is not. (And it does not matter, if we resume
553  * correctly, we'll mark system clean, anyway.)
554  */
555 int swsusp_write(void)
556 {
557         int error;
558
559         lock_swapdevices();
560         error = write_suspend_image();
561         /* This will unlock ignored swap devices since writing is finished */
562         lock_swapdevices();
563         return error;
564
565 }
566
567
568
569 int swsusp_suspend(void)
570 {
571         int error;
572         if ((error = arch_prepare_suspend()))
573                 return error;
574         local_irq_disable();
575         /* At this point, device_suspend() has been called, but *not*
576          * device_power_down(). We *must* device_power_down() now.
577          * Otherwise, drivers for some devices (e.g. interrupt controllers)
578          * become desynchronized with the actual state of the hardware
579          * at resume time, and evil weirdness ensues.
580          */
581         if ((error = device_power_down(PMSG_FREEZE))) {
582                 printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
583                 local_irq_enable();
584                 return error;
585         }
586
587         if ((error = swsusp_swap_check())) {
588                 printk(KERN_ERR "swsusp: cannot find swap device, try swapon -a.\n");
589                 device_power_up();
590                 local_irq_enable();
591                 return error;
592         }
593
594         save_processor_state();
595         if ((error = swsusp_arch_suspend()))
596                 printk(KERN_ERR "Error %d suspending\n", error);
597         /* Restore control flow magically appears here */
598         restore_processor_state();
599         restore_highmem();
600         device_power_up();
601         local_irq_enable();
602         return error;
603 }
604
605 int swsusp_resume(void)
606 {
607         int error;
608         local_irq_disable();
609         if (device_power_down(PMSG_FREEZE))
610                 printk(KERN_ERR "Some devices failed to power down, very bad\n");
611         /* We'll ignore saved state, but this gets preempt count (etc) right */
612         save_processor_state();
613         error = swsusp_arch_resume();
614         /* Code below is only ever reached in case of failure. Otherwise
615          * execution continues at place where swsusp_arch_suspend was called
616          */
617         BUG_ON(!error);
618         /* The only reason why swsusp_arch_resume() can fail is memory being
619          * very tight, so we have to free it as soon as we can to avoid
620          * subsequent failures
621          */
622         swsusp_free();
623         restore_processor_state();
624         restore_highmem();
625         touch_softlockup_watchdog();
626         device_power_up();
627         local_irq_enable();
628         return error;
629 }
630
631 /**
632  *      swsusp_pagedir_relocate - It is possible, that some memory pages
633  *      occupied by the list of PBEs collide with pages where we're going to
634  *      restore from the loaded pages later.  We relocate them here.
635  */
636
637 static struct pbe *swsusp_pagedir_relocate(struct pbe *pblist)
638 {
639         struct zone *zone;
640         unsigned long zone_pfn;
641         struct pbe *pbpage, *tail, *p;
642         void *m;
643         int rel = 0;
644
645         if (!pblist) /* a sanity check */
646                 return NULL;
647
648         pr_debug("swsusp: Relocating pagedir (%lu pages to check)\n",
649                         swsusp_info.pagedir_pages);
650
651         /* Clear page flags */
652
653         for_each_zone (zone) {
654                 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
655                         if (pfn_valid(zone_pfn + zone->zone_start_pfn))
656                                 ClearPageNosaveFree(pfn_to_page(zone_pfn +
657                                         zone->zone_start_pfn));
658         }
659
660         /* Mark orig addresses */
661
662         for_each_pbe (p, pblist)
663                 SetPageNosaveFree(virt_to_page(p->orig_address));
664
665         tail = pblist + PB_PAGE_SKIP;
666
667         /* Relocate colliding pages */
668
669         for_each_pb_page (pbpage, pblist) {
670                 if (PageNosaveFree(virt_to_page((unsigned long)pbpage))) {
671                         m = (void *)get_safe_page(GFP_ATOMIC | __GFP_COLD);
672                         if (!m)
673                                 return NULL;
674                         memcpy(m, (void *)pbpage, PAGE_SIZE);
675                         if (pbpage == pblist)
676                                 pblist = (struct pbe *)m;
677                         else
678                                 tail->next = (struct pbe *)m;
679                         pbpage = (struct pbe *)m;
680
681                         /* We have to link the PBEs again */
682                         for (p = pbpage; p < pbpage + PB_PAGE_SKIP; p++)
683                                 if (p->next) /* needed to save the end */
684                                         p->next = p + 1;
685
686                         rel++;
687                 }
688                 tail = pbpage + PB_PAGE_SKIP;
689         }
690
691         /* This is for swsusp_free() */
692         for_each_pb_page (pbpage, pblist) {
693                 SetPageNosave(virt_to_page(pbpage));
694                 SetPageNosaveFree(virt_to_page(pbpage));
695         }
696
697         printk("swsusp: Relocated %d pages\n", rel);
698
699         return pblist;
700 }
701
702 /*
703  *      Using bio to read from swap.
704  *      This code requires a bit more work than just using buffer heads
705  *      but, it is the recommended way for 2.5/2.6.
706  *      The following are to signal the beginning and end of I/O. Bios
707  *      finish asynchronously, while we want them to happen synchronously.
708  *      A simple atomic_t, and a wait loop take care of this problem.
709  */
710
711 static atomic_t io_done = ATOMIC_INIT(0);
712
713 static int end_io(struct bio *bio, unsigned int num, int err)
714 {
715         if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
716                 panic("I/O error reading memory image");
717         atomic_set(&io_done, 0);
718         return 0;
719 }
720
721 static struct block_device *resume_bdev;
722
723 /**
724  *      submit - submit BIO request.
725  *      @rw:    READ or WRITE.
726  *      @off    physical offset of page.
727  *      @page:  page we're reading or writing.
728  *
729  *      Straight from the textbook - allocate and initialize the bio.
730  *      If we're writing, make sure the page is marked as dirty.
731  *      Then submit it and wait.
732  */
733
734 static int submit(int rw, pgoff_t page_off, void *page)
735 {
736         int error = 0;
737         struct bio *bio;
738
739         bio = bio_alloc(GFP_ATOMIC, 1);
740         if (!bio)
741                 return -ENOMEM;
742         bio->bi_sector = page_off * (PAGE_SIZE >> 9);
743         bio_get(bio);
744         bio->bi_bdev = resume_bdev;
745         bio->bi_end_io = end_io;
746
747         if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
748                 printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
749                 error = -EFAULT;
750                 goto Done;
751         }
752
753         if (rw == WRITE)
754                 bio_set_pages_dirty(bio);
755
756         atomic_set(&io_done, 1);
757         submit_bio(rw | (1 << BIO_RW_SYNC), bio);
758         while (atomic_read(&io_done))
759                 yield();
760
761  Done:
762         bio_put(bio);
763         return error;
764 }
765
766 static int bio_read_page(pgoff_t page_off, void *page)
767 {
768         return submit(READ, page_off, page);
769 }
770
771 static int bio_write_page(pgoff_t page_off, void *page)
772 {
773         return submit(WRITE, page_off, page);
774 }
775
776 /*
777  * Sanity check if this image makes sense with this kernel/swap context
778  * I really don't think that it's foolproof but more than nothing..
779  */
780
781 static const char *sanity_check(void)
782 {
783         dump_info();
784         if (swsusp_info.version_code != LINUX_VERSION_CODE)
785                 return "kernel version";
786         if (swsusp_info.num_physpages != num_physpages)
787                 return "memory size";
788         if (strcmp(swsusp_info.uts.sysname,system_utsname.sysname))
789                 return "system type";
790         if (strcmp(swsusp_info.uts.release,system_utsname.release))
791                 return "kernel release";
792         if (strcmp(swsusp_info.uts.version,system_utsname.version))
793                 return "version";
794         if (strcmp(swsusp_info.uts.machine,system_utsname.machine))
795                 return "machine";
796 #if 0
797         /* We can't use number of online CPUs when we use hotplug to remove them ;-))) */
798         if (swsusp_info.cpus != num_possible_cpus())
799                 return "number of cpus";
800 #endif
801         return NULL;
802 }
803
804
805 static int check_header(void)
806 {
807         const char *reason = NULL;
808         int error;
809
810         if ((error = bio_read_page(swp_offset(swsusp_header.swsusp_info), &swsusp_info)))
811                 return error;
812
813         /* Is this same machine? */
814         if ((reason = sanity_check())) {
815                 printk(KERN_ERR "swsusp: Resume mismatch: %s\n",reason);
816                 return -EPERM;
817         }
818         nr_copy_pages = swsusp_info.image_pages;
819         return error;
820 }
821
822 static int check_sig(void)
823 {
824         int error;
825
826         memset(&swsusp_header, 0, sizeof(swsusp_header));
827         if ((error = bio_read_page(0, &swsusp_header)))
828                 return error;
829         if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
830                 memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
831                 memcpy(key_iv, swsusp_header.key_iv, MAXKEY+MAXIV);
832                 memset(swsusp_header.key_iv, 0, MAXKEY+MAXIV);
833
834                 /*
835                  * Reset swap signature now.
836                  */
837                 error = bio_write_page(0, &swsusp_header);
838         } else { 
839                 return -EINVAL;
840         }
841         if (!error)
842                 pr_debug("swsusp: Signature found, resuming\n");
843         return error;
844 }
845
846 /**
847  *      data_read - Read image pages from swap.
848  *
849  *      You do not need to check for overlaps, check_pagedir()
850  *      already did that.
851  */
852
853 static int data_read(struct pbe *pblist)
854 {
855         struct pbe *p;
856         int error = 0;
857         int i = 0;
858         int mod = swsusp_info.image_pages / 100;
859         void *tfm;
860
861         if ((error = crypto_init(0, &tfm)))
862                 return error;
863
864         if (!mod)
865                 mod = 1;
866
867         printk("swsusp: Reading image data (%lu pages):     ",
868                         swsusp_info.image_pages);
869
870         for_each_pbe (p, pblist) {
871                 if (!(i % mod))
872                         printk("\b\b\b\b%3d%%", i / mod);
873
874                 if ((error = crypto_read(p, tfm))) {
875                         crypto_exit(tfm);
876                         return error;
877                 }
878
879                 i++;
880         }
881         printk("\b\b\b\bdone\n");
882         crypto_exit(tfm);
883         return error;
884 }
885
886 /**
887  *      read_pagedir - Read page backup list pages from swap
888  */
889
890 static int read_pagedir(struct pbe *pblist)
891 {
892         struct pbe *pbpage, *p;
893         unsigned int i = 0;
894         int error;
895
896         if (!pblist)
897                 return -EFAULT;
898
899         printk("swsusp: Reading pagedir (%lu pages)\n",
900                         swsusp_info.pagedir_pages);
901
902         for_each_pb_page (pbpage, pblist) {
903                 unsigned long offset = swp_offset(swsusp_info.pagedir[i++]);
904
905                 error = -EFAULT;
906                 if (offset) {
907                         p = (pbpage + PB_PAGE_SKIP)->next;
908                         error = bio_read_page(offset, (void *)pbpage);
909                         (pbpage + PB_PAGE_SKIP)->next = p;
910                 }
911                 if (error)
912                         break;
913         }
914
915         if (!error)
916                 BUG_ON(i != swsusp_info.pagedir_pages);
917
918         return error;
919 }
920
921
922 static int check_suspend_image(void)
923 {
924         int error = 0;
925
926         if ((error = check_sig()))
927                 return error;
928
929         if ((error = check_header()))
930                 return error;
931
932         return 0;
933 }
934
935 static int read_suspend_image(void)
936 {
937         int error = 0;
938         struct pbe *p;
939
940         if (!(p = alloc_pagedir(nr_copy_pages, GFP_ATOMIC, 0)))
941                 return -ENOMEM;
942
943         if ((error = read_pagedir(p)))
944                 return error;
945
946         create_pbe_list(p, nr_copy_pages);
947
948         if (!(pagedir_nosave = swsusp_pagedir_relocate(p)))
949                 return -ENOMEM;
950
951         /* Allocate memory for the image and read the data from swap */
952
953         error = alloc_data_pages(pagedir_nosave, GFP_ATOMIC, 1);
954
955         if (!error)
956                 error = data_read(pagedir_nosave);
957
958         return error;
959 }
960
961 /**
962  *      swsusp_check - Check for saved image in swap
963  */
964
965 int swsusp_check(void)
966 {
967         int error;
968
969         resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
970         if (!IS_ERR(resume_bdev)) {
971                 set_blocksize(resume_bdev, PAGE_SIZE);
972                 error = check_suspend_image();
973                 if (error)
974                     blkdev_put(resume_bdev);
975         } else
976                 error = PTR_ERR(resume_bdev);
977
978         if (!error)
979                 pr_debug("swsusp: resume file found\n");
980         else
981                 pr_debug("swsusp: Error %d check for resume file\n", error);
982         return error;
983 }
984
985 /**
986  *      swsusp_read - Read saved image from swap.
987  */
988
989 int swsusp_read(void)
990 {
991         int error;
992
993         if (IS_ERR(resume_bdev)) {
994                 pr_debug("swsusp: block device not initialised\n");
995                 return PTR_ERR(resume_bdev);
996         }
997
998         error = read_suspend_image();
999         blkdev_put(resume_bdev);
1000         memset(key_iv, 0, MAXKEY+MAXIV);
1001
1002         if (!error)
1003                 pr_debug("swsusp: Reading resume file was successful\n");
1004         else
1005                 pr_debug("swsusp: Error %d resuming\n", error);
1006         return error;
1007 }
1008
1009 /**
1010  *      swsusp_close - close swap device.
1011  */
1012
1013 void swsusp_close(void)
1014 {
1015         if (IS_ERR(resume_bdev)) {
1016                 pr_debug("swsusp: block device not initialised\n");
1017                 return;
1018         }
1019
1020         blkdev_put(resume_bdev);
1021 }