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>:
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>
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>
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>
77 #include <linux/random.h>
78 #include <linux/crypto.h>
79 #include <asm/scatterlist.h>
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
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
;
116 } __attribute__((packed
, aligned(PAGE_SIZE
))) swsusp_header
;
118 static struct swsusp_info swsusp_info
;
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
)
144 struct crypto_tfm
*tfm
;
146 modemsg
= mode
? "suspend not possible" : "resume not possible";
148 tfm
= crypto_alloc_tfm(CIPHER
, CRYPTO_TFM_MODE_CBC
);
150 printk(KERN_ERR
"swsusp: no tfm, %s\n", modemsg
);
155 if(MAXKEY
< crypto_tfm_alg_min_keysize(tfm
)) {
156 printk(KERN_ERR
"swsusp: key buffer too small, %s\n", modemsg
);
162 get_random_bytes(key_iv
, MAXKEY
+MAXIV
);
164 len
= crypto_tfm_alg_max_keysize(tfm
);
168 if (crypto_cipher_setkey(tfm
, key_iv
, len
)) {
169 printk(KERN_ERR
"swsusp: key setup failure, %s\n", modemsg
);
170 error
= -EKEYREJECTED
;
174 len
= crypto_tfm_alg_ivsize(tfm
);
177 printk(KERN_ERR
"swsusp: iv buffer too small, %s\n", modemsg
);
182 crypto_cipher_set_iv(tfm
, key_iv
+MAXKEY
, len
);
188 fail
: crypto_free_tfm(tfm
);
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
)
200 struct scatterlist src
, dst
;
202 src
.page
= virt_to_page(p
->address
);
204 src
.length
= PAGE_SIZE
;
205 dst
.page
= virt_to_page((void *)&swsusp_header
);
207 dst
.length
= PAGE_SIZE
;
209 error
= crypto_cipher_encrypt((struct crypto_tfm
*)mem
, &dst
, &src
,
213 error
= write_page((unsigned long)&swsusp_header
,
218 static __inline__
int crypto_read(struct pbe
*p
, void *mem
)
221 struct scatterlist src
, dst
;
223 error
= bio_read_page(swp_offset(p
->swap_address
), (void *)p
->address
);
226 src
.length
= PAGE_SIZE
;
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
,
237 static __inline__
int crypto_init(int mode
, void *mem
)
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
);
257 static int mark_swapfiles(swp_entry_t prev
)
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)
275 pr_debug("swsusp: Partition is not swap space.\n");
282 * Check whether the swap device is the specified resume
283 * device, irrespective of whether they are specified by
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 */
305 len
=strlen(resume_file
);
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
;
314 printk(KERN_WARNING
"resume= option should be used to set suspend device" );
315 if (root_swap
== 0xFFFF) {
316 swapfile_used
[i
] = SWAPFILE_SUSPEND
;
319 swapfile_used
[i
] = SWAPFILE_IGNORED
;
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
;
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)
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
)
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
,
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)
395 for_each_pbe(p
, pagedir_nosave
) {
396 entry
= p
->swap_address
;
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;
416 if ((error
= crypto_init(1, &tfm
)))
422 printk( "Writing data to swap (%d pages)... ", nr_copy_pages
);
423 for_each_pbe (p
, pagedir_nosave
) {
425 printk( "\b\b\b\b%3d%%", i
/ mod
);
426 if ((error
= crypto_write(p
, tfm
))) {
432 printk("\b\b\b\bdone\n");
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)
470 error
= write_page((unsigned long)&swsusp_info
, &entry
);
473 error
= mark_swapfiles(entry
);
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)
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)
505 printk( "Writing pagedir...");
506 for_each_pb_page (pbe
, pagedir_nosave
) {
507 if ((error
= write_page((unsigned long)pbe
, &swsusp_info
.pagedir
[n
++])))
511 swsusp_info
.pagedir_pages
= n
;
512 printk("done (%u pages)\n", n
);
517 * write_suspend_image - Write entire image and metadata.
520 static int write_suspend_image(void)
525 if ((error
= data_write()))
528 if ((error
= write_pagedir()))
531 if ((error
= close_swap()))
534 memset(key_iv
, 0, MAXKEY
+MAXIV
);
537 free_pagedir_entries();
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
549 * FIXME: si_swapinfo(&i) returns all swap devices information.
550 * We should only consider resume_device.
553 int enough_swap(unsigned nr_pages
)
558 pr_debug("swsusp: available swap: %lu pages\n", i
.freeswap
);
559 return i
.freeswap
> (nr_pages
+ PAGES_FOR_IO
+
560 (nr_pages
+ PBES_PER_PAGE
- 1) / 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)
574 error
= write_suspend_image();
575 /* This will unlock ignored swap devices since writing is finished */
583 int swsusp_suspend(void)
586 if ((error
= arch_prepare_suspend()))
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");
601 if ((error
= swsusp_swap_check())) {
602 printk(KERN_ERR
"swsusp: cannot find swap device, try swapon -a.\n");
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();
619 int swsusp_resume(void)
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
632 /* The only reason why swsusp_arch_resume() can fail is memory being
633 * very tight, so we have to free it as soon as we can to avoid
634 * subsequent failures
637 restore_processor_state();
639 touch_softlockup_watchdog();
646 * On resume, for storing the PBE list and the image,
647 * we can only use memory pages that do not conflict with the pages
648 * which had been used before suspend.
650 * We don't know which pages are usable until we allocate them.
652 * Allocated but unusable (ie eaten) memory pages are marked so that
653 * swsusp_free() can release them
656 unsigned long get_safe_page(gfp_t gfp_mask
)
661 m
= get_zeroed_page(gfp_mask
);
662 if (m
&& PageNosaveFree(virt_to_page(m
)))
663 /* This is for swsusp_free() */
664 SetPageNosave(virt_to_page(m
));
665 } while (m
&& PageNosaveFree(virt_to_page(m
)));
667 /* This is for swsusp_free() */
668 SetPageNosave(virt_to_page(m
));
669 SetPageNosaveFree(virt_to_page(m
));
675 * check_pagedir - We ensure here that pages that the PBEs point to
676 * won't collide with pages where we're going to restore from the loaded
680 static int check_pagedir(struct pbe
*pblist
)
684 /* This is necessary, so that we can free allocated pages
687 for_each_pbe (p
, pblist
)
690 for_each_pbe (p
, pblist
) {
691 p
->address
= get_safe_page(GFP_ATOMIC
);
699 * swsusp_pagedir_relocate - It is possible, that some memory pages
700 * occupied by the list of PBEs collide with pages where we're going to
701 * restore from the loaded pages later. We relocate them here.
704 static struct pbe
* swsusp_pagedir_relocate(struct pbe
*pblist
)
707 unsigned long zone_pfn
;
708 struct pbe
*pbpage
, *tail
, *p
;
712 if (!pblist
) /* a sanity check */
715 pr_debug("swsusp: Relocating pagedir (%lu pages to check)\n",
716 swsusp_info
.pagedir_pages
);
718 /* Clear page flags */
720 for_each_zone (zone
) {
721 for (zone_pfn
= 0; zone_pfn
< zone
->spanned_pages
; ++zone_pfn
)
722 if (pfn_valid(zone_pfn
+ zone
->zone_start_pfn
))
723 ClearPageNosaveFree(pfn_to_page(zone_pfn
+
724 zone
->zone_start_pfn
));
727 /* Mark orig addresses */
729 for_each_pbe (p
, pblist
)
730 SetPageNosaveFree(virt_to_page(p
->orig_address
));
732 tail
= pblist
+ PB_PAGE_SKIP
;
734 /* Relocate colliding pages */
736 for_each_pb_page (pbpage
, pblist
) {
737 if (PageNosaveFree(virt_to_page((unsigned long)pbpage
))) {
738 m
= (void *)get_safe_page(GFP_ATOMIC
| __GFP_COLD
);
741 memcpy(m
, (void *)pbpage
, PAGE_SIZE
);
742 if (pbpage
== pblist
)
743 pblist
= (struct pbe
*)m
;
745 tail
->next
= (struct pbe
*)m
;
746 pbpage
= (struct pbe
*)m
;
748 /* We have to link the PBEs again */
749 for (p
= pbpage
; p
< pbpage
+ PB_PAGE_SKIP
; p
++)
750 if (p
->next
) /* needed to save the end */
755 tail
= pbpage
+ PB_PAGE_SKIP
;
758 /* This is for swsusp_free() */
759 for_each_pb_page (pbpage
, pblist
) {
760 SetPageNosave(virt_to_page(pbpage
));
761 SetPageNosaveFree(virt_to_page(pbpage
));
764 printk("swsusp: Relocated %d pages\n", rel
);
770 * Using bio to read from swap.
771 * This code requires a bit more work than just using buffer heads
772 * but, it is the recommended way for 2.5/2.6.
773 * The following are to signal the beginning and end of I/O. Bios
774 * finish asynchronously, while we want them to happen synchronously.
775 * A simple atomic_t, and a wait loop take care of this problem.
778 static atomic_t io_done
= ATOMIC_INIT(0);
780 static int end_io(struct bio
* bio
, unsigned int num
, int err
)
782 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
783 panic("I/O error reading memory image");
784 atomic_set(&io_done
, 0);
788 static struct block_device
* resume_bdev
;
791 * submit - submit BIO request.
792 * @rw: READ or WRITE.
793 * @off physical offset of page.
794 * @page: page we're reading or writing.
796 * Straight from the textbook - allocate and initialize the bio.
797 * If we're writing, make sure the page is marked as dirty.
798 * Then submit it and wait.
801 static int submit(int rw
, pgoff_t page_off
, void * page
)
806 bio
= bio_alloc(GFP_ATOMIC
, 1);
809 bio
->bi_sector
= page_off
* (PAGE_SIZE
>> 9);
811 bio
->bi_bdev
= resume_bdev
;
812 bio
->bi_end_io
= end_io
;
814 if (bio_add_page(bio
, virt_to_page(page
), PAGE_SIZE
, 0) < PAGE_SIZE
) {
815 printk("swsusp: ERROR: adding page to bio at %ld\n",page_off
);
821 bio_set_pages_dirty(bio
);
823 atomic_set(&io_done
, 1);
824 submit_bio(rw
| (1 << BIO_RW_SYNC
), bio
);
825 while (atomic_read(&io_done
))
833 static int bio_read_page(pgoff_t page_off
, void * page
)
835 return submit(READ
, page_off
, page
);
838 static int bio_write_page(pgoff_t page_off
, void * page
)
840 return submit(WRITE
, page_off
, page
);
844 * Sanity check if this image makes sense with this kernel/swap context
845 * I really don't think that it's foolproof but more than nothing..
848 static const char * sanity_check(void)
851 if (swsusp_info
.version_code
!= LINUX_VERSION_CODE
)
852 return "kernel version";
853 if (swsusp_info
.num_physpages
!= num_physpages
)
854 return "memory size";
855 if (strcmp(swsusp_info
.uts
.sysname
,system_utsname
.sysname
))
856 return "system type";
857 if (strcmp(swsusp_info
.uts
.release
,system_utsname
.release
))
858 return "kernel release";
859 if (strcmp(swsusp_info
.uts
.version
,system_utsname
.version
))
861 if (strcmp(swsusp_info
.uts
.machine
,system_utsname
.machine
))
864 /* We can't use number of online CPUs when we use hotplug to remove them ;-))) */
865 if (swsusp_info
.cpus
!= num_possible_cpus())
866 return "number of cpus";
872 static int check_header(void)
874 const char * reason
= NULL
;
877 if ((error
= bio_read_page(swp_offset(swsusp_header
.swsusp_info
), &swsusp_info
)))
880 /* Is this same machine? */
881 if ((reason
= sanity_check())) {
882 printk(KERN_ERR
"swsusp: Resume mismatch: %s\n",reason
);
885 nr_copy_pages
= swsusp_info
.image_pages
;
889 static int check_sig(void)
893 memset(&swsusp_header
, 0, sizeof(swsusp_header
));
894 if ((error
= bio_read_page(0, &swsusp_header
)))
896 if (!memcmp(SWSUSP_SIG
, swsusp_header
.sig
, 10)) {
897 memcpy(swsusp_header
.sig
, swsusp_header
.orig_sig
, 10);
898 memcpy(key_iv
, swsusp_header
.key_iv
, MAXKEY
+MAXIV
);
899 memset(swsusp_header
.key_iv
, 0, MAXKEY
+MAXIV
);
902 * Reset swap signature now.
904 error
= bio_write_page(0, &swsusp_header
);
909 pr_debug("swsusp: Signature found, resuming\n");
914 * data_read - Read image pages from swap.
916 * You do not need to check for overlaps, check_pagedir()
920 static int data_read(struct pbe
*pblist
)
925 int mod
= swsusp_info
.image_pages
/ 100;
928 if ((error
= crypto_init(0, &tfm
)))
934 printk("swsusp: Reading image data (%lu pages): ",
935 swsusp_info
.image_pages
);
937 for_each_pbe (p
, pblist
) {
939 printk("\b\b\b\b%3d%%", i
/ mod
);
941 if ((error
= crypto_read(p
, tfm
))) {
948 printk("\b\b\b\bdone\n");
954 * read_pagedir - Read page backup list pages from swap
957 static int read_pagedir(struct pbe
*pblist
)
959 struct pbe
*pbpage
, *p
;
966 printk("swsusp: Reading pagedir (%lu pages)\n",
967 swsusp_info
.pagedir_pages
);
969 for_each_pb_page (pbpage
, pblist
) {
970 unsigned long offset
= swp_offset(swsusp_info
.pagedir
[i
++]);
974 p
= (pbpage
+ PB_PAGE_SKIP
)->next
;
975 error
= bio_read_page(offset
, (void *)pbpage
);
976 (pbpage
+ PB_PAGE_SKIP
)->next
= p
;
983 BUG_ON(i
!= swsusp_info
.pagedir_pages
);
989 static int check_suspend_image(void)
993 if ((error
= check_sig()))
996 if ((error
= check_header()))
1002 static int read_suspend_image(void)
1007 if (!(p
= alloc_pagedir(nr_copy_pages
)))
1010 if ((error
= read_pagedir(p
)))
1013 create_pbe_list(p
, nr_copy_pages
);
1015 if (!(pagedir_nosave
= swsusp_pagedir_relocate(p
)))
1018 /* Allocate memory for the image and read the data from swap */
1020 error
= check_pagedir(pagedir_nosave
);
1023 error
= data_read(pagedir_nosave
);
1029 * swsusp_check - Check for saved image in swap
1032 int swsusp_check(void)
1036 resume_bdev
= open_by_devnum(swsusp_resume_device
, FMODE_READ
);
1037 if (!IS_ERR(resume_bdev
)) {
1038 set_blocksize(resume_bdev
, PAGE_SIZE
);
1039 error
= check_suspend_image();
1041 blkdev_put(resume_bdev
);
1043 error
= PTR_ERR(resume_bdev
);
1046 pr_debug("swsusp: resume file found\n");
1048 pr_debug("swsusp: Error %d check for resume file\n", error
);
1053 * swsusp_read - Read saved image from swap.
1056 int swsusp_read(void)
1060 if (IS_ERR(resume_bdev
)) {
1061 pr_debug("swsusp: block device not initialised\n");
1062 return PTR_ERR(resume_bdev
);
1065 error
= read_suspend_image();
1066 blkdev_put(resume_bdev
);
1067 memset(key_iv
, 0, MAXKEY
+MAXIV
);
1070 pr_debug("swsusp: Reading resume file was successful\n");
1072 pr_debug("swsusp: Error %d resuming\n", error
);
1077 * swsusp_close - close swap device.
1080 void swsusp_close(void)
1082 if (IS_ERR(resume_bdev
)) {
1083 pr_debug("swsusp: block device not initialised\n");
1087 blkdev_put(resume_bdev
);