[PATCH] swsusp: move snapshot functionality to separate file
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / power / swsusp.c
blobfc50b5d2dd260d850e1f2993dc6c1ab9945f0f01
1 /*
2 * linux/kernel/power/swsusp.c
4 * This file is to realize architecture-independent
5 * machine suspend feature using pretty near only high-level routines
7 * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
8 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
10 * This file is released under the GPLv2.
12 * I'd like to thank the following people for their work:
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.
18 * Steve Doddi <dirk@loth.demon.co.uk>:
19 * Support the possibility of hardware state restoring.
21 * Raph <grey.havens@earthling.net>:
22 * Support for preserving states of network devices and virtual console
23 * (including X and svgatextmode)
25 * Kurt Garloff <garloff@suse.de>:
26 * Straightened the critical function in order to prevent compilers from
27 * playing tricks with local variables.
29 * Andreas Mohr <a.mohr@mailto.de>
31 * Alex Badea <vampire@go.ro>:
32 * Fixed runaway init
34 * Andreas Steinmetz <ast@domdv.de>:
35 * Added encrypted suspend option
37 * More state savers are welcome. Especially for the scsi layer...
39 * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
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>
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>
77 #include <linux/random.h>
78 #include <linux/crypto.h>
79 #include <asm/scatterlist.h>
81 #include "power.h"
83 #define CIPHER "aes"
84 #define MAXKEY 32
85 #define MAXIV 32
87 extern char resume_file[];
89 /* Local variables that should not be affected by save */
90 unsigned int nr_copy_pages __nosavedata = 0;
92 /* Suspend pagedir is allocated before final copy, therefore it
93 must be freed after resume
95 Warning: this is evil. There are actually two pagedirs at time of
96 resume. One is "pagedir_save", which is empty frame allocated at
97 time of suspend, that must be freed. Second is "pagedir_nosave",
98 allocated at time of resume, that travels through memory not to
99 collide with anything.
101 Warning: this is even more evil than it seems. Pagedirs this file
102 talks about are completely different from page directories used by
103 MMU hardware.
105 suspend_pagedir_t *pagedir_nosave __nosavedata = NULL;
106 suspend_pagedir_t *pagedir_save;
108 #define SWSUSP_SIG "S1SUSPEND"
110 static struct swsusp_header {
111 char reserved[PAGE_SIZE - 20 - MAXKEY - MAXIV - sizeof(swp_entry_t)];
112 u8 key_iv[MAXKEY+MAXIV];
113 swp_entry_t swsusp_info;
114 char orig_sig[10];
115 char sig[10];
116 } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
118 static struct swsusp_info swsusp_info;
121 * Saving part...
124 /* We memorize in swapfile_used what swap devices are used for suspension */
125 #define SWAPFILE_UNUSED 0
126 #define SWAPFILE_SUSPEND 1 /* This is the suspending device */
127 #define SWAPFILE_IGNORED 2 /* Those are other swap devices ignored for suspension */
129 static unsigned short swapfile_used[MAX_SWAPFILES];
130 static unsigned short root_swap;
132 static int write_page(unsigned long addr, swp_entry_t * loc);
133 static int bio_read_page(pgoff_t page_off, void * page);
135 static u8 key_iv[MAXKEY+MAXIV];
137 #ifdef CONFIG_SWSUSP_ENCRYPT
139 static int crypto_init(int mode, void **mem)
141 int error = 0;
142 int len;
143 char *modemsg;
144 struct crypto_tfm *tfm;
146 modemsg = mode ? "suspend not possible" : "resume not possible";
148 tfm = crypto_alloc_tfm(CIPHER, CRYPTO_TFM_MODE_CBC);
149 if(!tfm) {
150 printk(KERN_ERR "swsusp: no tfm, %s\n", modemsg);
151 error = -EINVAL;
152 goto out;
155 if(MAXKEY < crypto_tfm_alg_min_keysize(tfm)) {
156 printk(KERN_ERR "swsusp: key buffer too small, %s\n", modemsg);
157 error = -ENOKEY;
158 goto fail;
161 if (mode)
162 get_random_bytes(key_iv, MAXKEY+MAXIV);
164 len = crypto_tfm_alg_max_keysize(tfm);
165 if (len > MAXKEY)
166 len = MAXKEY;
168 if (crypto_cipher_setkey(tfm, key_iv, len)) {
169 printk(KERN_ERR "swsusp: key setup failure, %s\n", modemsg);
170 error = -EKEYREJECTED;
171 goto fail;
174 len = crypto_tfm_alg_ivsize(tfm);
176 if (MAXIV < len) {
177 printk(KERN_ERR "swsusp: iv buffer too small, %s\n", modemsg);
178 error = -EOVERFLOW;
179 goto fail;
182 crypto_cipher_set_iv(tfm, key_iv+MAXKEY, len);
184 *mem=(void *)tfm;
186 goto out;
188 fail: crypto_free_tfm(tfm);
189 out: return error;
192 static __inline__ void crypto_exit(void *mem)
194 crypto_free_tfm((struct crypto_tfm *)mem);
197 static __inline__ int crypto_write(struct pbe *p, void *mem)
199 int error = 0;
200 struct scatterlist src, dst;
202 src.page = virt_to_page(p->address);
203 src.offset = 0;
204 src.length = PAGE_SIZE;
205 dst.page = virt_to_page((void *)&swsusp_header);
206 dst.offset = 0;
207 dst.length = PAGE_SIZE;
209 error = crypto_cipher_encrypt((struct crypto_tfm *)mem, &dst, &src,
210 PAGE_SIZE);
212 if (!error)
213 error = write_page((unsigned long)&swsusp_header,
214 &(p->swap_address));
215 return error;
218 static __inline__ int crypto_read(struct pbe *p, void *mem)
220 int error = 0;
221 struct scatterlist src, dst;
223 error = bio_read_page(swp_offset(p->swap_address), (void *)p->address);
224 if (!error) {
225 src.offset = 0;
226 src.length = PAGE_SIZE;
227 dst.offset = 0;
228 dst.length = PAGE_SIZE;
229 src.page = dst.page = virt_to_page((void *)p->address);
231 error = crypto_cipher_decrypt((struct crypto_tfm *)mem, &dst,
232 &src, PAGE_SIZE);
234 return error;
236 #else
237 static __inline__ int crypto_init(int mode, void *mem)
239 return 0;
242 static __inline__ void crypto_exit(void *mem)
246 static __inline__ int crypto_write(struct pbe *p, void *mem)
248 return write_page(p->address, &(p->swap_address));
251 static __inline__ int crypto_read(struct pbe *p, void *mem)
253 return bio_read_page(swp_offset(p->swap_address), (void *)p->address);
255 #endif
257 static int mark_swapfiles(swp_entry_t prev)
259 int error;
261 rw_swap_page_sync(READ,
262 swp_entry(root_swap, 0),
263 virt_to_page((unsigned long)&swsusp_header));
264 if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
265 !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
266 memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
267 memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
268 memcpy(swsusp_header.key_iv, key_iv, MAXKEY+MAXIV);
269 swsusp_header.swsusp_info = prev;
270 error = rw_swap_page_sync(WRITE,
271 swp_entry(root_swap, 0),
272 virt_to_page((unsigned long)
273 &swsusp_header));
274 } else {
275 pr_debug("swsusp: Partition is not swap space.\n");
276 error = -ENODEV;
278 return error;
282 * Check whether the swap device is the specified resume
283 * device, irrespective of whether they are specified by
284 * identical names.
286 * (Thus, device inode aliasing is allowed. You can say /dev/hda4
287 * instead of /dev/ide/host0/bus0/target0/lun0/part4 [if using devfs]
288 * and they'll be considered the same device. This is *necessary* for
289 * devfs, since the resume code can only recognize the form /dev/hda4,
290 * but the suspend code would see the long name.)
292 static int is_resume_device(const struct swap_info_struct *swap_info)
294 struct file *file = swap_info->swap_file;
295 struct inode *inode = file->f_dentry->d_inode;
297 return S_ISBLK(inode->i_mode) &&
298 swsusp_resume_device == MKDEV(imajor(inode), iminor(inode));
301 static int swsusp_swap_check(void) /* This is called before saving image */
303 int i, len;
305 len=strlen(resume_file);
306 root_swap = 0xFFFF;
308 spin_lock(&swap_lock);
309 for (i=0; i<MAX_SWAPFILES; i++) {
310 if (!(swap_info[i].flags & SWP_WRITEOK)) {
311 swapfile_used[i]=SWAPFILE_UNUSED;
312 } else {
313 if (!len) {
314 printk(KERN_WARNING "resume= option should be used to set suspend device" );
315 if (root_swap == 0xFFFF) {
316 swapfile_used[i] = SWAPFILE_SUSPEND;
317 root_swap = i;
318 } else
319 swapfile_used[i] = SWAPFILE_IGNORED;
320 } else {
321 /* we ignore all swap devices that are not the resume_file */
322 if (is_resume_device(&swap_info[i])) {
323 swapfile_used[i] = SWAPFILE_SUSPEND;
324 root_swap = i;
325 } else {
326 swapfile_used[i] = SWAPFILE_IGNORED;
331 spin_unlock(&swap_lock);
332 return (root_swap != 0xffff) ? 0 : -ENODEV;
336 * This is called after saving image so modification
337 * will be lost after resume... and that's what we want.
338 * we make the device unusable. A new call to
339 * lock_swapdevices can unlock the devices.
341 static void lock_swapdevices(void)
343 int i;
345 spin_lock(&swap_lock);
346 for (i = 0; i< MAX_SWAPFILES; i++)
347 if (swapfile_used[i] == SWAPFILE_IGNORED) {
348 swap_info[i].flags ^= SWP_WRITEOK;
350 spin_unlock(&swap_lock);
354 * write_page - Write one page to a fresh swap location.
355 * @addr: Address we're writing.
356 * @loc: Place to store the entry we used.
358 * Allocate a new swap entry and 'sync' it. Note we discard -EIO
359 * errors. That is an artifact left over from swsusp. It did not
360 * check the return of rw_swap_page_sync() at all, since most pages
361 * written back to swap would return -EIO.
362 * This is a partial improvement, since we will at least return other
363 * errors, though we need to eventually fix the damn code.
365 static int write_page(unsigned long addr, swp_entry_t * loc)
367 swp_entry_t entry;
368 int error = 0;
370 entry = get_swap_page();
371 if (swp_offset(entry) &&
372 swapfile_used[swp_type(entry)] == SWAPFILE_SUSPEND) {
373 error = rw_swap_page_sync(WRITE, entry,
374 virt_to_page(addr));
375 if (error == -EIO)
376 error = 0;
377 if (!error)
378 *loc = entry;
379 } else
380 error = -ENOSPC;
381 return error;
385 * data_free - Free the swap entries used by the saved image.
387 * Walk the list of used swap entries and free each one.
388 * This is only used for cleanup when suspend fails.
390 static void data_free(void)
392 swp_entry_t entry;
393 struct pbe * p;
395 for_each_pbe(p, pagedir_nosave) {
396 entry = p->swap_address;
397 if (entry.val)
398 swap_free(entry);
399 else
400 break;
405 * data_write - Write saved image to swap.
407 * Walk the list of pages in the image and sync each one to swap.
409 static int data_write(void)
411 int error = 0, i = 0;
412 unsigned int mod = nr_copy_pages / 100;
413 struct pbe *p;
414 void *tfm;
416 if ((error = crypto_init(1, &tfm)))
417 return error;
419 if (!mod)
420 mod = 1;
422 printk( "Writing data to swap (%d pages)... ", nr_copy_pages );
423 for_each_pbe (p, pagedir_nosave) {
424 if (!(i%mod))
425 printk( "\b\b\b\b%3d%%", i / mod );
426 if ((error = crypto_write(p, tfm))) {
427 crypto_exit(tfm);
428 return error;
430 i++;
432 printk("\b\b\b\bdone\n");
433 crypto_exit(tfm);
434 return error;
437 static void dump_info(void)
439 pr_debug(" swsusp: Version: %u\n",swsusp_info.version_code);
440 pr_debug(" swsusp: Num Pages: %ld\n",swsusp_info.num_physpages);
441 pr_debug(" swsusp: UTS Sys: %s\n",swsusp_info.uts.sysname);
442 pr_debug(" swsusp: UTS Node: %s\n",swsusp_info.uts.nodename);
443 pr_debug(" swsusp: UTS Release: %s\n",swsusp_info.uts.release);
444 pr_debug(" swsusp: UTS Version: %s\n",swsusp_info.uts.version);
445 pr_debug(" swsusp: UTS Machine: %s\n",swsusp_info.uts.machine);
446 pr_debug(" swsusp: UTS Domain: %s\n",swsusp_info.uts.domainname);
447 pr_debug(" swsusp: CPUs: %d\n",swsusp_info.cpus);
448 pr_debug(" swsusp: Image: %ld Pages\n",swsusp_info.image_pages);
449 pr_debug(" swsusp: Pagedir: %ld Pages\n",swsusp_info.pagedir_pages);
452 static void init_header(void)
454 memset(&swsusp_info, 0, sizeof(swsusp_info));
455 swsusp_info.version_code = LINUX_VERSION_CODE;
456 swsusp_info.num_physpages = num_physpages;
457 memcpy(&swsusp_info.uts, &system_utsname, sizeof(system_utsname));
459 swsusp_info.suspend_pagedir = pagedir_nosave;
460 swsusp_info.cpus = num_online_cpus();
461 swsusp_info.image_pages = nr_copy_pages;
464 static int close_swap(void)
466 swp_entry_t entry;
467 int error;
469 dump_info();
470 error = write_page((unsigned long)&swsusp_info, &entry);
471 if (!error) {
472 printk( "S" );
473 error = mark_swapfiles(entry);
474 printk( "|\n" );
476 return error;
480 * free_pagedir_entries - Free pages used by the page directory.
482 * This is used during suspend for error recovery.
485 static void free_pagedir_entries(void)
487 int i;
489 for (i = 0; i < swsusp_info.pagedir_pages; i++)
490 swap_free(swsusp_info.pagedir[i]);
495 * write_pagedir - Write the array of pages holding the page directory.
496 * @last: Last swap entry we write (needed for header).
499 static int write_pagedir(void)
501 int error = 0;
502 unsigned n = 0;
503 struct pbe * pbe;
505 printk( "Writing pagedir...");
506 for_each_pb_page (pbe, pagedir_nosave) {
507 if ((error = write_page((unsigned long)pbe, &swsusp_info.pagedir[n++])))
508 return error;
511 swsusp_info.pagedir_pages = n;
512 printk("done (%u pages)\n", n);
513 return error;
517 * write_suspend_image - Write entire image and metadata.
520 static int write_suspend_image(void)
522 int error;
524 init_header();
525 if ((error = data_write()))
526 goto FreeData;
528 if ((error = write_pagedir()))
529 goto FreePagedir;
531 if ((error = close_swap()))
532 goto FreePagedir;
533 Done:
534 memset(key_iv, 0, MAXKEY+MAXIV);
535 return error;
536 FreePagedir:
537 free_pagedir_entries();
538 FreeData:
539 data_free();
540 goto Done;
544 * enough_swap - Make sure we have enough swap to save the image.
546 * Returns TRUE or FALSE after checking the total amount of swap
547 * space avaiable.
549 * FIXME: si_swapinfo(&i) returns all swap devices information.
550 * We should only consider resume_device.
553 int enough_swap(void)
555 struct sysinfo i;
557 si_swapinfo(&i);
558 pr_debug("swsusp: available swap: %lu pages\n", i.freeswap);
559 return i.freeswap > (nr_copy_pages + PAGES_FOR_IO +
560 nr_copy_pages/PBES_PER_PAGE + !!(nr_copy_pages%PBES_PER_PAGE));
564 /* It is important _NOT_ to umount filesystems at this point. We want
565 * them synced (in case something goes wrong) but we DO not want to mark
566 * filesystem clean: it is not. (And it does not matter, if we resume
567 * correctly, we'll mark system clean, anyway.)
569 int swsusp_write(void)
571 int error;
572 device_resume();
573 lock_swapdevices();
574 error = write_suspend_image();
575 /* This will unlock ignored swap devices since writing is finished */
576 lock_swapdevices();
577 return error;
583 int swsusp_suspend(void)
585 int error;
586 if ((error = arch_prepare_suspend()))
587 return error;
588 local_irq_disable();
589 /* At this point, device_suspend() has been called, but *not*
590 * device_power_down(). We *must* device_power_down() now.
591 * Otherwise, drivers for some devices (e.g. interrupt controllers)
592 * become desynchronized with the actual state of the hardware
593 * at resume time, and evil weirdness ensues.
595 if ((error = device_power_down(PMSG_FREEZE))) {
596 printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
597 local_irq_enable();
598 return error;
601 if ((error = swsusp_swap_check())) {
602 printk(KERN_ERR "swsusp: cannot find swap device, try swapon -a.\n");
603 device_power_up();
604 local_irq_enable();
605 return error;
608 save_processor_state();
609 if ((error = swsusp_arch_suspend()))
610 printk(KERN_ERR "Error %d suspending\n", error);
611 /* Restore control flow magically appears here */
612 restore_processor_state();
613 restore_highmem();
614 device_power_up();
615 local_irq_enable();
616 return error;
619 int swsusp_resume(void)
621 int error;
622 local_irq_disable();
623 if (device_power_down(PMSG_FREEZE))
624 printk(KERN_ERR "Some devices failed to power down, very bad\n");
625 /* We'll ignore saved state, but this gets preempt count (etc) right */
626 save_processor_state();
627 error = swsusp_arch_resume();
628 /* Code below is only ever reached in case of failure. Otherwise
629 * execution continues at place where swsusp_arch_suspend was called
631 BUG_ON(!error);
632 restore_processor_state();
633 restore_highmem();
634 touch_softlockup_watchdog();
635 device_power_up();
636 local_irq_enable();
637 return error;
641 * On resume, for storing the PBE list and the image,
642 * we can only use memory pages that do not conflict with the pages
643 * which had been used before suspend.
645 * We don't know which pages are usable until we allocate them.
647 * Allocated but unusable (ie eaten) memory pages are linked together
648 * to create a list, so that we can free them easily
650 * We could have used a type other than (void *)
651 * for this purpose, but ...
653 static void **eaten_memory = NULL;
655 static inline void eat_page(void *page)
657 void **c;
659 c = eaten_memory;
660 eaten_memory = page;
661 *eaten_memory = c;
664 unsigned long get_usable_page(gfp_t gfp_mask)
666 unsigned long m;
668 m = get_zeroed_page(gfp_mask);
669 while (!PageNosaveFree(virt_to_page(m))) {
670 eat_page((void *)m);
671 m = get_zeroed_page(gfp_mask);
672 if (!m)
673 break;
675 return m;
678 void free_eaten_memory(void)
680 unsigned long m;
681 void **c;
682 int i = 0;
684 c = eaten_memory;
685 while (c) {
686 m = (unsigned long)c;
687 c = *c;
688 free_page(m);
689 i++;
691 eaten_memory = NULL;
692 pr_debug("swsusp: %d unused pages freed\n", i);
696 * check_pagedir - We ensure here that pages that the PBEs point to
697 * won't collide with pages where we're going to restore from the loaded
698 * pages later
701 static int check_pagedir(struct pbe *pblist)
703 struct pbe *p;
705 /* This is necessary, so that we can free allocated pages
706 * in case of failure
708 for_each_pbe (p, pblist)
709 p->address = 0UL;
711 for_each_pbe (p, pblist) {
712 p->address = get_usable_page(GFP_ATOMIC);
713 if (!p->address)
714 return -ENOMEM;
716 return 0;
720 * swsusp_pagedir_relocate - It is possible, that some memory pages
721 * occupied by the list of PBEs collide with pages where we're going to
722 * restore from the loaded pages later. We relocate them here.
725 static struct pbe * swsusp_pagedir_relocate(struct pbe *pblist)
727 struct zone *zone;
728 unsigned long zone_pfn;
729 struct pbe *pbpage, *tail, *p;
730 void *m;
731 int rel = 0, error = 0;
733 if (!pblist) /* a sanity check */
734 return NULL;
736 pr_debug("swsusp: Relocating pagedir (%lu pages to check)\n",
737 swsusp_info.pagedir_pages);
739 /* Set page flags */
741 for_each_zone (zone) {
742 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
743 SetPageNosaveFree(pfn_to_page(zone_pfn +
744 zone->zone_start_pfn));
747 /* Clear orig addresses */
749 for_each_pbe (p, pblist)
750 ClearPageNosaveFree(virt_to_page(p->orig_address));
752 tail = pblist + PB_PAGE_SKIP;
754 /* Relocate colliding pages */
756 for_each_pb_page (pbpage, pblist) {
757 if (!PageNosaveFree(virt_to_page((unsigned long)pbpage))) {
758 m = (void *)get_usable_page(GFP_ATOMIC | __GFP_COLD);
759 if (!m) {
760 error = -ENOMEM;
761 break;
763 memcpy(m, (void *)pbpage, PAGE_SIZE);
764 if (pbpage == pblist)
765 pblist = (struct pbe *)m;
766 else
767 tail->next = (struct pbe *)m;
769 eat_page((void *)pbpage);
770 pbpage = (struct pbe *)m;
772 /* We have to link the PBEs again */
774 for (p = pbpage; p < pbpage + PB_PAGE_SKIP; p++)
775 if (p->next) /* needed to save the end */
776 p->next = p + 1;
778 rel++;
780 tail = pbpage + PB_PAGE_SKIP;
783 if (error) {
784 printk("\nswsusp: Out of memory\n\n");
785 free_pagedir(pblist);
786 free_eaten_memory();
787 pblist = NULL;
788 /* Is this even worth handling? It should never ever happen, and we
789 have just lost user's state, anyway... */
790 } else
791 printk("swsusp: Relocated %d pages\n", rel);
793 return pblist;
797 * Using bio to read from swap.
798 * This code requires a bit more work than just using buffer heads
799 * but, it is the recommended way for 2.5/2.6.
800 * The following are to signal the beginning and end of I/O. Bios
801 * finish asynchronously, while we want them to happen synchronously.
802 * A simple atomic_t, and a wait loop take care of this problem.
805 static atomic_t io_done = ATOMIC_INIT(0);
807 static int end_io(struct bio * bio, unsigned int num, int err)
809 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
810 panic("I/O error reading memory image");
811 atomic_set(&io_done, 0);
812 return 0;
815 static struct block_device * resume_bdev;
818 * submit - submit BIO request.
819 * @rw: READ or WRITE.
820 * @off physical offset of page.
821 * @page: page we're reading or writing.
823 * Straight from the textbook - allocate and initialize the bio.
824 * If we're writing, make sure the page is marked as dirty.
825 * Then submit it and wait.
828 static int submit(int rw, pgoff_t page_off, void * page)
830 int error = 0;
831 struct bio * bio;
833 bio = bio_alloc(GFP_ATOMIC, 1);
834 if (!bio)
835 return -ENOMEM;
836 bio->bi_sector = page_off * (PAGE_SIZE >> 9);
837 bio_get(bio);
838 bio->bi_bdev = resume_bdev;
839 bio->bi_end_io = end_io;
841 if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
842 printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
843 error = -EFAULT;
844 goto Done;
847 if (rw == WRITE)
848 bio_set_pages_dirty(bio);
850 atomic_set(&io_done, 1);
851 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
852 while (atomic_read(&io_done))
853 yield();
855 Done:
856 bio_put(bio);
857 return error;
860 static int bio_read_page(pgoff_t page_off, void * page)
862 return submit(READ, page_off, page);
865 static int bio_write_page(pgoff_t page_off, void * page)
867 return submit(WRITE, page_off, page);
871 * Sanity check if this image makes sense with this kernel/swap context
872 * I really don't think that it's foolproof but more than nothing..
875 static const char * sanity_check(void)
877 dump_info();
878 if (swsusp_info.version_code != LINUX_VERSION_CODE)
879 return "kernel version";
880 if (swsusp_info.num_physpages != num_physpages)
881 return "memory size";
882 if (strcmp(swsusp_info.uts.sysname,system_utsname.sysname))
883 return "system type";
884 if (strcmp(swsusp_info.uts.release,system_utsname.release))
885 return "kernel release";
886 if (strcmp(swsusp_info.uts.version,system_utsname.version))
887 return "version";
888 if (strcmp(swsusp_info.uts.machine,system_utsname.machine))
889 return "machine";
890 #if 0
891 /* We can't use number of online CPUs when we use hotplug to remove them ;-))) */
892 if (swsusp_info.cpus != num_possible_cpus())
893 return "number of cpus";
894 #endif
895 return NULL;
899 static int check_header(void)
901 const char * reason = NULL;
902 int error;
904 if ((error = bio_read_page(swp_offset(swsusp_header.swsusp_info), &swsusp_info)))
905 return error;
907 /* Is this same machine? */
908 if ((reason = sanity_check())) {
909 printk(KERN_ERR "swsusp: Resume mismatch: %s\n",reason);
910 return -EPERM;
912 nr_copy_pages = swsusp_info.image_pages;
913 return error;
916 static int check_sig(void)
918 int error;
920 memset(&swsusp_header, 0, sizeof(swsusp_header));
921 if ((error = bio_read_page(0, &swsusp_header)))
922 return error;
923 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
924 memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
925 memcpy(key_iv, swsusp_header.key_iv, MAXKEY+MAXIV);
926 memset(swsusp_header.key_iv, 0, MAXKEY+MAXIV);
929 * Reset swap signature now.
931 error = bio_write_page(0, &swsusp_header);
932 } else {
933 return -EINVAL;
935 if (!error)
936 pr_debug("swsusp: Signature found, resuming\n");
937 return error;
941 * data_read - Read image pages from swap.
943 * You do not need to check for overlaps, check_pagedir()
944 * already did that.
947 static int data_read(struct pbe *pblist)
949 struct pbe * p;
950 int error = 0;
951 int i = 0;
952 int mod = swsusp_info.image_pages / 100;
953 void *tfm;
955 if ((error = crypto_init(0, &tfm)))
956 return error;
958 if (!mod)
959 mod = 1;
961 printk("swsusp: Reading image data (%lu pages): ",
962 swsusp_info.image_pages);
964 for_each_pbe (p, pblist) {
965 if (!(i % mod))
966 printk("\b\b\b\b%3d%%", i / mod);
968 if ((error = crypto_read(p, tfm))) {
969 crypto_exit(tfm);
970 return error;
973 i++;
975 printk("\b\b\b\bdone\n");
976 crypto_exit(tfm);
977 return error;
981 * read_pagedir - Read page backup list pages from swap
984 static int read_pagedir(struct pbe *pblist)
986 struct pbe *pbpage, *p;
987 unsigned i = 0;
988 int error;
990 if (!pblist)
991 return -EFAULT;
993 printk("swsusp: Reading pagedir (%lu pages)\n",
994 swsusp_info.pagedir_pages);
996 for_each_pb_page (pbpage, pblist) {
997 unsigned long offset = swp_offset(swsusp_info.pagedir[i++]);
999 error = -EFAULT;
1000 if (offset) {
1001 p = (pbpage + PB_PAGE_SKIP)->next;
1002 error = bio_read_page(offset, (void *)pbpage);
1003 (pbpage + PB_PAGE_SKIP)->next = p;
1005 if (error)
1006 break;
1009 if (error)
1010 free_pagedir(pblist);
1011 else
1012 BUG_ON(i != swsusp_info.pagedir_pages);
1014 return error;
1018 static int check_suspend_image(void)
1020 int error = 0;
1022 if ((error = check_sig()))
1023 return error;
1025 if ((error = check_header()))
1026 return error;
1028 return 0;
1031 static int read_suspend_image(void)
1033 int error = 0;
1034 struct pbe *p;
1036 if (!(p = alloc_pagedir(nr_copy_pages)))
1037 return -ENOMEM;
1039 if ((error = read_pagedir(p)))
1040 return error;
1042 create_pbe_list(p, nr_copy_pages);
1044 if (!(pagedir_nosave = swsusp_pagedir_relocate(p)))
1045 return -ENOMEM;
1047 /* Allocate memory for the image and read the data from swap */
1049 error = check_pagedir(pagedir_nosave);
1051 if (!error)
1052 error = data_read(pagedir_nosave);
1054 if (error) { /* We fail cleanly */
1055 free_eaten_memory();
1056 for_each_pbe (p, pagedir_nosave)
1057 if (p->address) {
1058 free_page(p->address);
1059 p->address = 0UL;
1061 free_pagedir(pagedir_nosave);
1063 return error;
1067 * swsusp_check - Check for saved image in swap
1070 int swsusp_check(void)
1072 int error;
1074 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
1075 if (!IS_ERR(resume_bdev)) {
1076 set_blocksize(resume_bdev, PAGE_SIZE);
1077 error = check_suspend_image();
1078 if (error)
1079 blkdev_put(resume_bdev);
1080 } else
1081 error = PTR_ERR(resume_bdev);
1083 if (!error)
1084 pr_debug("swsusp: resume file found\n");
1085 else
1086 pr_debug("swsusp: Error %d check for resume file\n", error);
1087 return error;
1091 * swsusp_read - Read saved image from swap.
1094 int swsusp_read(void)
1096 int error;
1098 if (IS_ERR(resume_bdev)) {
1099 pr_debug("swsusp: block device not initialised\n");
1100 return PTR_ERR(resume_bdev);
1103 error = read_suspend_image();
1104 blkdev_put(resume_bdev);
1105 memset(key_iv, 0, MAXKEY+MAXIV);
1107 if (!error)
1108 pr_debug("swsusp: Reading resume file was successful\n");
1109 else
1110 pr_debug("swsusp: Error %d resuming\n", error);
1111 return error;
1115 * swsusp_close - close swap device.
1118 void swsusp_close(void)
1120 if (IS_ERR(resume_bdev)) {
1121 pr_debug("swsusp: block device not initialised\n");
1122 return;
1125 blkdev_put(resume_bdev);