2 * linux/kernel/power/swsusp.c
4 * This file provides code to write suspend image to swap and read it back.
6 * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
7 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
9 * This file is released under the GPLv2.
11 * I'd like to thank the following people for their work:
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.
17 * Steve Doddi <dirk@loth.demon.co.uk>:
18 * Support the possibility of hardware state restoring.
20 * Raph <grey.havens@earthling.net>:
21 * Support for preserving states of network devices and virtual console
22 * (including X and svgatextmode)
24 * Kurt Garloff <garloff@suse.de>:
25 * Straightened the critical function in order to prevent compilers from
26 * playing tricks with local variables.
28 * Andreas Mohr <a.mohr@mailto.de>
30 * Alex Badea <vampire@go.ro>:
33 * Andreas Steinmetz <ast@domdv.de>:
34 * Added encrypted suspend option
36 * More state savers are welcome. Especially for the scsi layer...
38 * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
41 #include <linux/module.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>
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>
64 #include <asm/uaccess.h>
65 #include <asm/mmu_context.h>
66 #include <asm/pgtable.h>
67 #include <asm/tlbflush.h>
70 #include <linux/random.h>
71 #include <linux/crypto.h>
72 #include <asm/scatterlist.h>
80 extern char resume_file[];
82 /* Local variables that should not be affected by save */
83 unsigned int nr_copy_pages __nosavedata = 0;
85 /* Suspend pagedir is allocated before final copy, therefore it
86 must be freed after resume
88 Warning: this is evil. There are actually two pagedirs at time of
89 resume. One is "pagedir_save", which is empty frame allocated at
90 time of suspend, that must be freed. Second is "pagedir_nosave",
91 allocated at time of resume, that travels through memory not to
92 collide with anything.
94 Warning: this is even more evil than it seems. Pagedirs this file
95 talks about are completely different from page directories used by
98 suspend_pagedir_t *pagedir_nosave __nosavedata = NULL;
99 suspend_pagedir_t *pagedir_save;
101 #define SWSUSP_SIG "S1SUSPEND"
103 static struct swsusp_header {
104 char reserved[PAGE_SIZE - 20 - MAXKEY - MAXIV - sizeof(swp_entry_t)];
105 u8 key_iv[MAXKEY+MAXIV];
106 swp_entry_t swsusp_info;
109 } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
111 static struct swsusp_info swsusp_info;
117 /* We memorize in swapfile_used what swap devices are used for suspension */
118 #define SWAPFILE_UNUSED 0
119 #define SWAPFILE_SUSPEND 1 /* This is the suspending device */
120 #define SWAPFILE_IGNORED 2 /* Those are other swap devices ignored for suspension */
122 static unsigned short swapfile_used[MAX_SWAPFILES];
123 static unsigned short root_swap;
125 static int write_page(unsigned long addr, swp_entry_t * loc);
126 static int bio_read_page(pgoff_t page_off, void * page);
128 static u8 key_iv[MAXKEY+MAXIV];
130 #ifdef CONFIG_SWSUSP_ENCRYPT
132 static int crypto_init(int mode, void **mem)
137 struct crypto_tfm *tfm;
139 modemsg = mode ? "suspend not possible" : "resume not possible";
141 tfm = crypto_alloc_tfm(CIPHER, CRYPTO_TFM_MODE_CBC);
143 printk(KERN_ERR "swsusp: no tfm, %s\n", modemsg);
148 if(MAXKEY < crypto_tfm_alg_min_keysize(tfm)) {
149 printk(KERN_ERR "swsusp: key buffer too small, %s\n", modemsg);
155 get_random_bytes(key_iv, MAXKEY+MAXIV);
157 len = crypto_tfm_alg_max_keysize(tfm);
161 if (crypto_cipher_setkey(tfm, key_iv, len)) {
162 printk(KERN_ERR "swsusp: key setup failure, %s\n", modemsg);
163 error = -EKEYREJECTED;
167 len = crypto_tfm_alg_ivsize(tfm);
170 printk(KERN_ERR "swsusp: iv buffer too small, %s\n", modemsg);
175 crypto_cipher_set_iv(tfm, key_iv+MAXKEY, len);
181 fail: crypto_free_tfm(tfm);
185 static __inline__ void crypto_exit(void *mem)
187 crypto_free_tfm((struct crypto_tfm *)mem);
190 static __inline__ int crypto_write(struct pbe *p, void *mem)
193 struct scatterlist src, dst;
195 src.page = virt_to_page(p->address);
197 src.length = PAGE_SIZE;
198 dst.page = virt_to_page((void *)&swsusp_header);
200 dst.length = PAGE_SIZE;
202 error = crypto_cipher_encrypt((struct crypto_tfm *)mem, &dst, &src,
206 error = write_page((unsigned long)&swsusp_header,
211 static __inline__ int crypto_read(struct pbe *p, void *mem)
214 struct scatterlist src, dst;
216 error = bio_read_page(swp_offset(p->swap_address), (void *)p->address);
219 src.length = PAGE_SIZE;
221 dst.length = PAGE_SIZE;
222 src.page = dst.page = virt_to_page((void *)p->address);
224 error = crypto_cipher_decrypt((struct crypto_tfm *)mem, &dst,
230 static __inline__ int crypto_init(int mode, void *mem)
235 static __inline__ void crypto_exit(void *mem)
239 static __inline__ int crypto_write(struct pbe *p, void *mem)
241 return write_page(p->address, &(p->swap_address));
244 static __inline__ int crypto_read(struct pbe *p, void *mem)
246 return bio_read_page(swp_offset(p->swap_address), (void *)p->address);
250 static int mark_swapfiles(swp_entry_t prev)
254 rw_swap_page_sync(READ,
255 swp_entry(root_swap, 0),
256 virt_to_page((unsigned long)&swsusp_header));
257 if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
258 !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
259 memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
260 memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
261 memcpy(swsusp_header.key_iv, key_iv, MAXKEY+MAXIV);
262 swsusp_header.swsusp_info = prev;
263 error = rw_swap_page_sync(WRITE,
264 swp_entry(root_swap, 0),
265 virt_to_page((unsigned long)
268 pr_debug("swsusp: Partition is not swap space.\n");
275 * Check whether the swap device is the specified resume
276 * device, irrespective of whether they are specified by
279 * (Thus, device inode aliasing is allowed. You can say /dev/hda4
280 * instead of /dev/ide/host0/bus0/target0/lun0/part4 [if using devfs]
281 * and they'll be considered the same device. This is *necessary* for
282 * devfs, since the resume code can only recognize the form /dev/hda4,
283 * but the suspend code would see the long name.)
285 static int is_resume_device(const struct swap_info_struct *swap_info)
287 struct file *file = swap_info->swap_file;
288 struct inode *inode = file->f_dentry->d_inode;
290 return S_ISBLK(inode->i_mode) &&
291 swsusp_resume_device == MKDEV(imajor(inode), iminor(inode));
294 static int swsusp_swap_check(void) /* This is called before saving image */
298 len=strlen(resume_file);
301 spin_lock(&swap_lock);
302 for (i=0; i<MAX_SWAPFILES; i++) {
303 if (!(swap_info[i].flags & SWP_WRITEOK)) {
304 swapfile_used[i]=SWAPFILE_UNUSED;
307 printk(KERN_WARNING "resume= option should be used to set suspend device" );
308 if (root_swap == 0xFFFF) {
309 swapfile_used[i] = SWAPFILE_SUSPEND;
312 swapfile_used[i] = SWAPFILE_IGNORED;
314 /* we ignore all swap devices that are not the resume_file */
315 if (is_resume_device(&swap_info[i])) {
316 swapfile_used[i] = SWAPFILE_SUSPEND;
319 swapfile_used[i] = SWAPFILE_IGNORED;
324 spin_unlock(&swap_lock);
325 return (root_swap != 0xffff) ? 0 : -ENODEV;
329 * This is called after saving image so modification
330 * will be lost after resume... and that's what we want.
331 * we make the device unusable. A new call to
332 * lock_swapdevices can unlock the devices.
334 static void lock_swapdevices(void)
338 spin_lock(&swap_lock);
339 for (i = 0; i< MAX_SWAPFILES; i++)
340 if (swapfile_used[i] == SWAPFILE_IGNORED) {
341 swap_info[i].flags ^= SWP_WRITEOK;
343 spin_unlock(&swap_lock);
347 * write_page - Write one page to a fresh swap location.
348 * @addr: Address we're writing.
349 * @loc: Place to store the entry we used.
351 * Allocate a new swap entry and 'sync' it. Note we discard -EIO
352 * errors. That is an artifact left over from swsusp. It did not
353 * check the return of rw_swap_page_sync() at all, since most pages
354 * written back to swap would return -EIO.
355 * This is a partial improvement, since we will at least return other
356 * errors, though we need to eventually fix the damn code.
358 static int write_page(unsigned long addr, swp_entry_t * loc)
363 entry = get_swap_page();
364 if (swp_offset(entry) &&
365 swapfile_used[swp_type(entry)] == SWAPFILE_SUSPEND) {
366 error = rw_swap_page_sync(WRITE, entry,
378 * data_free - Free the swap entries used by the saved image.
380 * Walk the list of used swap entries and free each one.
381 * This is only used for cleanup when suspend fails.
383 static void data_free(void)
388 for_each_pbe(p, pagedir_nosave) {
389 entry = p->swap_address;
398 * data_write - Write saved image to swap.
400 * Walk the list of pages in the image and sync each one to swap.
402 static int data_write(void)
404 int error = 0, i = 0;
405 unsigned int mod = nr_copy_pages / 100;
409 if ((error = crypto_init(1, &tfm)))
415 printk( "Writing data to swap (%d pages)... ", nr_copy_pages );
416 for_each_pbe (p, pagedir_nosave) {
418 printk( "\b\b\b\b%3d%%", i / mod );
419 if ((error = crypto_write(p, tfm))) {
425 printk("\b\b\b\bdone\n");
430 static void dump_info(void)
432 pr_debug(" swsusp: Version: %u\n",swsusp_info.version_code);
433 pr_debug(" swsusp: Num Pages: %ld\n",swsusp_info.num_physpages);
434 pr_debug(" swsusp: UTS Sys: %s\n",swsusp_info.uts.sysname);
435 pr_debug(" swsusp: UTS Node: %s\n",swsusp_info.uts.nodename);
436 pr_debug(" swsusp: UTS Release: %s\n",swsusp_info.uts.release);
437 pr_debug(" swsusp: UTS Version: %s\n",swsusp_info.uts.version);
438 pr_debug(" swsusp: UTS Machine: %s\n",swsusp_info.uts.machine);
439 pr_debug(" swsusp: UTS Domain: %s\n",swsusp_info.uts.domainname);
440 pr_debug(" swsusp: CPUs: %d\n",swsusp_info.cpus);
441 pr_debug(" swsusp: Image: %ld Pages\n",swsusp_info.image_pages);
442 pr_debug(" swsusp: Pagedir: %ld Pages\n",swsusp_info.pagedir_pages);
445 static void init_header(void)
447 memset(&swsusp_info, 0, sizeof(swsusp_info));
448 swsusp_info.version_code = LINUX_VERSION_CODE;
449 swsusp_info.num_physpages = num_physpages;
450 memcpy(&swsusp_info.uts, &system_utsname, sizeof(system_utsname));
452 swsusp_info.suspend_pagedir = pagedir_nosave;
453 swsusp_info.cpus = num_online_cpus();
454 swsusp_info.image_pages = nr_copy_pages;
457 static int close_swap(void)
463 error = write_page((unsigned long)&swsusp_info, &entry);
466 error = mark_swapfiles(entry);
473 * free_pagedir_entries - Free pages used by the page directory.
475 * This is used during suspend for error recovery.
478 static void free_pagedir_entries(void)
482 for (i = 0; i < swsusp_info.pagedir_pages; i++)
483 swap_free(swsusp_info.pagedir[i]);
488 * write_pagedir - Write the array of pages holding the page directory.
489 * @last: Last swap entry we write (needed for header).
492 static int write_pagedir(void)
498 printk( "Writing pagedir...");
499 for_each_pb_page (pbe, pagedir_nosave) {
500 if ((error = write_page((unsigned long)pbe, &swsusp_info.pagedir[n++])))
504 swsusp_info.pagedir_pages = n;
505 printk("done (%u pages)\n", n);
510 * write_suspend_image - Write entire image and metadata.
513 static int write_suspend_image(void)
518 if ((error = data_write()))
521 if ((error = write_pagedir()))
524 if ((error = close_swap()))
527 memset(key_iv, 0, MAXKEY+MAXIV);
530 free_pagedir_entries();
537 * enough_swap - Make sure we have enough swap to save the image.
539 * Returns TRUE or FALSE after checking the total amount of swap
542 * FIXME: si_swapinfo(&i) returns all swap devices information.
543 * We should only consider resume_device.
546 int enough_swap(unsigned nr_pages)
551 pr_debug("swsusp: available swap: %lu pages\n", i.freeswap);
552 return i.freeswap > (nr_pages + PAGES_FOR_IO +
553 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
557 /* It is important _NOT_ to umount filesystems at this point. We want
558 * them synced (in case something goes wrong) but we DO not want to mark
559 * filesystem clean: it is not. (And it does not matter, if we resume
560 * correctly, we'll mark system clean, anyway.)
562 int swsusp_write(void)
567 error = write_suspend_image();
568 /* This will unlock ignored swap devices since writing is finished */
576 int swsusp_suspend(void)
579 if ((error = arch_prepare_suspend()))
582 /* At this point, device_suspend() has been called, but *not*
583 * device_power_down(). We *must* device_power_down() now.
584 * Otherwise, drivers for some devices (e.g. interrupt controllers)
585 * become desynchronized with the actual state of the hardware
586 * at resume time, and evil weirdness ensues.
588 if ((error = device_power_down(PMSG_FREEZE))) {
589 printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
594 if ((error = swsusp_swap_check())) {
595 printk(KERN_ERR "swsusp: cannot find swap device, try swapon -a.\n");
601 save_processor_state();
602 if ((error = swsusp_arch_suspend()))
603 printk(KERN_ERR "Error %d suspending\n", error);
604 /* Restore control flow magically appears here */
605 restore_processor_state();
612 int swsusp_resume(void)
616 if (device_power_down(PMSG_FREEZE))
617 printk(KERN_ERR "Some devices failed to power down, very bad\n");
618 /* We'll ignore saved state, but this gets preempt count (etc) right */
619 save_processor_state();
620 error = swsusp_arch_resume();
621 /* Code below is only ever reached in case of failure. Otherwise
622 * execution continues at place where swsusp_arch_suspend was called
625 /* The only reason why swsusp_arch_resume() can fail is memory being
626 * very tight, so we have to free it as soon as we can to avoid
627 * subsequent failures
630 restore_processor_state();
632 touch_softlockup_watchdog();
639 * On resume, for storing the PBE list and the image,
640 * we can only use memory pages that do not conflict with the pages
641 * which had been used before suspend.
643 * We don't know which pages are usable until we allocate them.
645 * Allocated but unusable (ie eaten) memory pages are marked so that
646 * swsusp_free() can release them
649 unsigned long get_safe_page(gfp_t gfp_mask)
654 m = get_zeroed_page(gfp_mask);
655 if (m && PageNosaveFree(virt_to_page(m)))
656 /* This is for swsusp_free() */
657 SetPageNosave(virt_to_page(m));
658 } while (m && PageNosaveFree(virt_to_page(m)));
660 /* This is for swsusp_free() */
661 SetPageNosave(virt_to_page(m));
662 SetPageNosaveFree(virt_to_page(m));
668 * check_pagedir - We ensure here that pages that the PBEs point to
669 * won't collide with pages where we're going to restore from the loaded
673 static int check_pagedir(struct pbe *pblist)
677 /* This is necessary, so that we can free allocated pages
680 for_each_pbe (p, pblist)
683 for_each_pbe (p, pblist) {
684 p->address = get_safe_page(GFP_ATOMIC);
692 * swsusp_pagedir_relocate - It is possible, that some memory pages
693 * occupied by the list of PBEs collide with pages where we're going to
694 * restore from the loaded pages later. We relocate them here.
697 static struct pbe * swsusp_pagedir_relocate(struct pbe *pblist)
700 unsigned long zone_pfn;
701 struct pbe *pbpage, *tail, *p;
705 if (!pblist) /* a sanity check */
708 pr_debug("swsusp: Relocating pagedir (%lu pages to check)\n",
709 swsusp_info.pagedir_pages);
711 /* Clear page flags */
713 for_each_zone (zone) {
714 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
715 if (pfn_valid(zone_pfn + zone->zone_start_pfn))
716 ClearPageNosaveFree(pfn_to_page(zone_pfn +
717 zone->zone_start_pfn));
720 /* Mark orig addresses */
722 for_each_pbe (p, pblist)
723 SetPageNosaveFree(virt_to_page(p->orig_address));
725 tail = pblist + PB_PAGE_SKIP;
727 /* Relocate colliding pages */
729 for_each_pb_page (pbpage, pblist) {
730 if (PageNosaveFree(virt_to_page((unsigned long)pbpage))) {
731 m = (void *)get_safe_page(GFP_ATOMIC | __GFP_COLD);
734 memcpy(m, (void *)pbpage, PAGE_SIZE);
735 if (pbpage == pblist)
736 pblist = (struct pbe *)m;
738 tail->next = (struct pbe *)m;
739 pbpage = (struct pbe *)m;
741 /* We have to link the PBEs again */
742 for (p = pbpage; p < pbpage + PB_PAGE_SKIP; p++)
743 if (p->next) /* needed to save the end */
748 tail = pbpage + PB_PAGE_SKIP;
751 /* This is for swsusp_free() */
752 for_each_pb_page (pbpage, pblist) {
753 SetPageNosave(virt_to_page(pbpage));
754 SetPageNosaveFree(virt_to_page(pbpage));
757 printk("swsusp: Relocated %d pages\n", rel);
763 * Using bio to read from swap.
764 * This code requires a bit more work than just using buffer heads
765 * but, it is the recommended way for 2.5/2.6.
766 * The following are to signal the beginning and end of I/O. Bios
767 * finish asynchronously, while we want them to happen synchronously.
768 * A simple atomic_t, and a wait loop take care of this problem.
771 static atomic_t io_done = ATOMIC_INIT(0);
773 static int end_io(struct bio * bio, unsigned int num, int err)
775 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
776 panic("I/O error reading memory image");
777 atomic_set(&io_done, 0);
781 static struct block_device * resume_bdev;
784 * submit - submit BIO request.
785 * @rw: READ or WRITE.
786 * @off physical offset of page.
787 * @page: page we're reading or writing.
789 * Straight from the textbook - allocate and initialize the bio.
790 * If we're writing, make sure the page is marked as dirty.
791 * Then submit it and wait.
794 static int submit(int rw, pgoff_t page_off, void * page)
799 bio = bio_alloc(GFP_ATOMIC, 1);
802 bio->bi_sector = page_off * (PAGE_SIZE >> 9);
804 bio->bi_bdev = resume_bdev;
805 bio->bi_end_io = end_io;
807 if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
808 printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
814 bio_set_pages_dirty(bio);
816 atomic_set(&io_done, 1);
817 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
818 while (atomic_read(&io_done))
826 static int bio_read_page(pgoff_t page_off, void * page)
828 return submit(READ, page_off, page);
831 static int bio_write_page(pgoff_t page_off, void * page)
833 return submit(WRITE, page_off, page);
837 * Sanity check if this image makes sense with this kernel/swap context
838 * I really don't think that it's foolproof but more than nothing..
841 static const char * sanity_check(void)
844 if (swsusp_info.version_code != LINUX_VERSION_CODE)
845 return "kernel version";
846 if (swsusp_info.num_physpages != num_physpages)
847 return "memory size";
848 if (strcmp(swsusp_info.uts.sysname,system_utsname.sysname))
849 return "system type";
850 if (strcmp(swsusp_info.uts.release,system_utsname.release))
851 return "kernel release";
852 if (strcmp(swsusp_info.uts.version,system_utsname.version))
854 if (strcmp(swsusp_info.uts.machine,system_utsname.machine))
857 /* We can't use number of online CPUs when we use hotplug to remove them ;-))) */
858 if (swsusp_info.cpus != num_possible_cpus())
859 return "number of cpus";
865 static int check_header(void)
867 const char * reason = NULL;
870 if ((error = bio_read_page(swp_offset(swsusp_header.swsusp_info), &swsusp_info)))
873 /* Is this same machine? */
874 if ((reason = sanity_check())) {
875 printk(KERN_ERR "swsusp: Resume mismatch: %s\n",reason);
878 nr_copy_pages = swsusp_info.image_pages;
882 static int check_sig(void)
886 memset(&swsusp_header, 0, sizeof(swsusp_header));
887 if ((error = bio_read_page(0, &swsusp_header)))
889 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
890 memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
891 memcpy(key_iv, swsusp_header.key_iv, MAXKEY+MAXIV);
892 memset(swsusp_header.key_iv, 0, MAXKEY+MAXIV);
895 * Reset swap signature now.
897 error = bio_write_page(0, &swsusp_header);
902 pr_debug("swsusp: Signature found, resuming\n");
907 * data_read - Read image pages from swap.
909 * You do not need to check for overlaps, check_pagedir()
913 static int data_read(struct pbe *pblist)
918 int mod = swsusp_info.image_pages / 100;
921 if ((error = crypto_init(0, &tfm)))
927 printk("swsusp: Reading image data (%lu pages): ",
928 swsusp_info.image_pages);
930 for_each_pbe (p, pblist) {
932 printk("\b\b\b\b%3d%%", i / mod);
934 if ((error = crypto_read(p, tfm))) {
941 printk("\b\b\b\bdone\n");
947 * read_pagedir - Read page backup list pages from swap
950 static int read_pagedir(struct pbe *pblist)
952 struct pbe *pbpage, *p;
959 printk("swsusp: Reading pagedir (%lu pages)\n",
960 swsusp_info.pagedir_pages);
962 for_each_pb_page (pbpage, pblist) {
963 unsigned long offset = swp_offset(swsusp_info.pagedir[i++]);
967 p = (pbpage + PB_PAGE_SKIP)->next;
968 error = bio_read_page(offset, (void *)pbpage);
969 (pbpage + PB_PAGE_SKIP)->next = p;
976 BUG_ON(i != swsusp_info.pagedir_pages);
982 static int check_suspend_image(void)
986 if ((error = check_sig()))
989 if ((error = check_header()))
995 static int read_suspend_image(void)
1000 if (!(p = alloc_pagedir(nr_copy_pages)))
1003 if ((error = read_pagedir(p)))
1006 create_pbe_list(p, nr_copy_pages);
1008 if (!(pagedir_nosave = swsusp_pagedir_relocate(p)))
1011 /* Allocate memory for the image and read the data from swap */
1013 error = check_pagedir(pagedir_nosave);
1016 error = data_read(pagedir_nosave);
1022 * swsusp_check - Check for saved image in swap
1025 int swsusp_check(void)
1029 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
1030 if (!IS_ERR(resume_bdev)) {
1031 set_blocksize(resume_bdev, PAGE_SIZE);
1032 error = check_suspend_image();
1034 blkdev_put(resume_bdev);
1036 error = PTR_ERR(resume_bdev);
1039 pr_debug("swsusp: resume file found\n");
1041 pr_debug("swsusp: Error %d check for resume file\n", error);
1046 * swsusp_read - Read saved image from swap.
1049 int swsusp_read(void)
1053 if (IS_ERR(resume_bdev)) {
1054 pr_debug("swsusp: block device not initialised\n");
1055 return PTR_ERR(resume_bdev);
1058 error = read_suspend_image();
1059 blkdev_put(resume_bdev);
1060 memset(key_iv, 0, MAXKEY+MAXIV);
1063 pr_debug("swsusp: Reading resume file was successful\n");
1065 pr_debug("swsusp: Error %d resuming\n", error);
1070 * swsusp_close - close swap device.
1073 void swsusp_close(void)
1075 if (IS_ERR(resume_bdev)) {
1076 pr_debug("swsusp: block device not initialised\n");
1080 blkdev_put(resume_bdev);