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 * Rafael J. Wysocki <rjw@sisk.pl>
34 * Added the swap map data structure and reworked the handling of swap
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>
73 * Preferred image size in MB (tunable via /sys/power/image_size).
74 * When it is set to N, swsusp will do its best to ensure the image
75 * size will not exceed N MB, but if that is impossible, it will
76 * try to create the smallest image possible.
78 unsigned int image_size = 500;
81 unsigned int count_highmem_pages(void);
82 int save_highmem(void);
83 int restore_highmem(void);
85 static int save_highmem(void) { return 0; }
86 static int restore_highmem(void) { return 0; }
87 static unsigned int count_highmem_pages(void) { return 0; }
90 extern char resume_file[];
92 #define SWSUSP_SIG "S1SUSPEND"
94 static struct swsusp_header {
95 char reserved[PAGE_SIZE - 20 - sizeof(swp_entry_t)];
96 swp_entry_t swsusp_info;
99 } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
101 static struct swsusp_info swsusp_info;
107 /* We memorize in swapfile_used what swap devices are used for suspension */
108 #define SWAPFILE_UNUSED 0
109 #define SWAPFILE_SUSPEND 1 /* This is the suspending device */
110 #define SWAPFILE_IGNORED 2 /* Those are other swap devices ignored for suspension */
112 static unsigned short swapfile_used[MAX_SWAPFILES];
113 static unsigned short root_swap;
115 static int mark_swapfiles(swp_entry_t prev)
119 rw_swap_page_sync(READ,
120 swp_entry(root_swap, 0),
121 virt_to_page((unsigned long)&swsusp_header));
122 if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
123 !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
124 memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
125 memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
126 swsusp_header.swsusp_info = prev;
127 error = rw_swap_page_sync(WRITE,
128 swp_entry(root_swap, 0),
129 virt_to_page((unsigned long)
132 pr_debug("swsusp: Partition is not swap space.\n");
139 * Check whether the swap device is the specified resume
140 * device, irrespective of whether they are specified by
143 * (Thus, device inode aliasing is allowed. You can say /dev/hda4
144 * instead of /dev/ide/host0/bus0/target0/lun0/part4 [if using devfs]
145 * and they'll be considered the same device. This is *necessary* for
146 * devfs, since the resume code can only recognize the form /dev/hda4,
147 * but the suspend code would see the long name.)
149 static int is_resume_device(const struct swap_info_struct *swap_info)
151 struct file *file = swap_info->swap_file;
152 struct inode *inode = file->f_dentry->d_inode;
154 return S_ISBLK(inode->i_mode) &&
155 swsusp_resume_device == MKDEV(imajor(inode), iminor(inode));
158 static int swsusp_swap_check(void) /* This is called before saving image */
162 len=strlen(resume_file);
165 spin_lock(&swap_lock);
166 for (i=0; i<MAX_SWAPFILES; i++) {
167 if (!(swap_info[i].flags & SWP_WRITEOK)) {
168 swapfile_used[i]=SWAPFILE_UNUSED;
171 printk(KERN_WARNING "resume= option should be used to set suspend device" );
172 if (root_swap == 0xFFFF) {
173 swapfile_used[i] = SWAPFILE_SUSPEND;
176 swapfile_used[i] = SWAPFILE_IGNORED;
178 /* we ignore all swap devices that are not the resume_file */
179 if (is_resume_device(&swap_info[i])) {
180 swapfile_used[i] = SWAPFILE_SUSPEND;
183 swapfile_used[i] = SWAPFILE_IGNORED;
188 spin_unlock(&swap_lock);
189 return (root_swap != 0xffff) ? 0 : -ENODEV;
193 * This is called after saving image so modification
194 * will be lost after resume... and that's what we want.
195 * we make the device unusable. A new call to
196 * lock_swapdevices can unlock the devices.
198 static void lock_swapdevices(void)
202 spin_lock(&swap_lock);
203 for (i = 0; i< MAX_SWAPFILES; i++)
204 if (swapfile_used[i] == SWAPFILE_IGNORED) {
205 swap_info[i].flags ^= SWP_WRITEOK;
207 spin_unlock(&swap_lock);
211 * write_page - Write one page to a fresh swap location.
212 * @addr: Address we're writing.
213 * @loc: Place to store the entry we used.
215 * Allocate a new swap entry and 'sync' it. Note we discard -EIO
216 * errors. That is an artifact left over from swsusp. It did not
217 * check the return of rw_swap_page_sync() at all, since most pages
218 * written back to swap would return -EIO.
219 * This is a partial improvement, since we will at least return other
220 * errors, though we need to eventually fix the damn code.
222 static int write_page(unsigned long addr, swp_entry_t *loc)
227 entry = get_swap_page();
228 if (swp_offset(entry) &&
229 swapfile_used[swp_type(entry)] == SWAPFILE_SUSPEND) {
230 error = rw_swap_page_sync(WRITE, entry,
242 * Swap map-handling functions
244 * The swap map is a data structure used for keeping track of each page
245 * written to the swap. It consists of many swap_map_page structures
246 * that contain each an array of MAP_PAGE_SIZE swap entries.
247 * These structures are linked together with the help of either the
248 * .next (in memory) or the .next_swap (in swap) member.
250 * The swap map is created during suspend. At that time we need to keep
251 * it in memory, because we have to free all of the allocated swap
252 * entries if an error occurs. The memory needed is preallocated
253 * so that we know in advance if there's enough of it.
255 * The first swap_map_page structure is filled with the swap entries that
256 * correspond to the first MAP_PAGE_SIZE data pages written to swap and
257 * so on. After the all of the data pages have been written, the order
258 * of the swap_map_page structures in the map is reversed so that they
259 * can be read from swap in the original order. This causes the data
260 * pages to be loaded in exactly the same order in which they have been
263 * During resume we only need to use one swap_map_page structure
264 * at a time, which means that we only need to use two memory pages for
265 * reading the image - one for reading the swap_map_page structures
266 * and the second for reading the data pages from swap.
269 #define MAP_PAGE_SIZE ((PAGE_SIZE - sizeof(swp_entry_t) - sizeof(void *)) \
270 / sizeof(swp_entry_t))
272 struct swap_map_page {
273 swp_entry_t entries[MAP_PAGE_SIZE];
274 swp_entry_t next_swap;
275 struct swap_map_page *next;
278 static inline void free_swap_map(struct swap_map_page *swap_map)
280 struct swap_map_page *swp;
283 swp = swap_map->next;
284 free_page((unsigned long)swap_map);
289 static struct swap_map_page *alloc_swap_map(unsigned int nr_pages)
291 struct swap_map_page *swap_map, *swp;
297 pr_debug("alloc_swap_map(): nr_pages = %d\n", nr_pages);
298 swap_map = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
300 for (n = MAP_PAGE_SIZE; n < nr_pages; n += MAP_PAGE_SIZE) {
301 swp->next = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
304 free_swap_map(swap_map);
312 * reverse_swap_map - reverse the order of pages in the swap map
316 static inline struct swap_map_page *reverse_swap_map(struct swap_map_page *swap_map)
318 struct swap_map_page *prev, *next;
322 next = swap_map->next;
323 swap_map->next = prev;
331 * free_swap_map_entries - free the swap entries allocated to store
332 * the swap map @swap_map (this is only called in case of an error)
334 static inline void free_swap_map_entries(struct swap_map_page *swap_map)
337 if (swap_map->next_swap.val)
338 swap_free(swap_map->next_swap);
339 swap_map = swap_map->next;
344 * save_swap_map - save the swap map used for tracing the data pages
348 static int save_swap_map(struct swap_map_page *swap_map, swp_entry_t *start)
350 swp_entry_t entry = (swp_entry_t){0};
354 swap_map->next_swap = entry;
355 if ((error = write_page((unsigned long)swap_map, &entry)))
357 swap_map = swap_map->next;
364 * free_image_entries - free the swap entries allocated to store
365 * the image data pages (this is only called in case of an error)
368 static inline void free_image_entries(struct swap_map_page *swp)
373 for (k = 0; k < MAP_PAGE_SIZE; k++)
374 if (swp->entries[k].val)
375 swap_free(swp->entries[k]);
381 * The swap_map_handle structure is used for handling the swap map in
385 struct swap_map_handle {
386 struct swap_map_page *cur;
390 static inline void init_swap_map_handle(struct swap_map_handle *handle,
391 struct swap_map_page *map)
397 static inline int swap_map_write_page(struct swap_map_handle *handle,
402 error = write_page(addr, handle->cur->entries + handle->k);
405 if (++handle->k >= MAP_PAGE_SIZE) {
406 handle->cur = handle->cur->next;
413 * save_image_data - save the data pages pointed to by the PBEs
414 * from the list @pblist using the swap map handle @handle
415 * (assume there are @nr_pages data pages to save)
418 static int save_image_data(struct pbe *pblist,
419 struct swap_map_handle *handle,
420 unsigned int nr_pages)
426 printk("Saving image data pages (%u pages) ... ", nr_pages);
431 for_each_pbe (p, pblist) {
432 error = swap_map_write_page(handle, p->address);
436 printk("\b\b\b\b%3d%%", nr_pages / m);
440 printk("\b\b\b\bdone\n");
444 static void dump_info(void)
446 pr_debug(" swsusp: Version: %u\n",swsusp_info.version_code);
447 pr_debug(" swsusp: Num Pages: %ld\n",swsusp_info.num_physpages);
448 pr_debug(" swsusp: UTS Sys: %s\n",swsusp_info.uts.sysname);
449 pr_debug(" swsusp: UTS Node: %s\n",swsusp_info.uts.nodename);
450 pr_debug(" swsusp: UTS Release: %s\n",swsusp_info.uts.release);
451 pr_debug(" swsusp: UTS Version: %s\n",swsusp_info.uts.version);
452 pr_debug(" swsusp: UTS Machine: %s\n",swsusp_info.uts.machine);
453 pr_debug(" swsusp: UTS Domain: %s\n",swsusp_info.uts.domainname);
454 pr_debug(" swsusp: CPUs: %d\n",swsusp_info.cpus);
455 pr_debug(" swsusp: Image: %ld Pages\n",swsusp_info.image_pages);
456 pr_debug(" swsusp: Total: %ld Pages\n", swsusp_info.pages);
459 static void init_header(unsigned int nr_pages)
461 memset(&swsusp_info, 0, sizeof(swsusp_info));
462 swsusp_info.version_code = LINUX_VERSION_CODE;
463 swsusp_info.num_physpages = num_physpages;
464 memcpy(&swsusp_info.uts, &system_utsname, sizeof(system_utsname));
466 swsusp_info.cpus = num_online_cpus();
467 swsusp_info.image_pages = nr_pages;
468 swsusp_info.pages = nr_pages +
469 ((nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT);
472 static int close_swap(void)
478 error = write_page((unsigned long)&swsusp_info, &entry);
481 error = mark_swapfiles(entry);
488 * pack_orig_addresses - the .orig_address fields of the PBEs from the
489 * list starting at @pbe are stored in the array @buf[] (1 page)
492 static inline struct pbe *pack_orig_addresses(unsigned long *buf,
497 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
498 buf[j] = pbe->orig_address;
502 for (; j < PAGE_SIZE / sizeof(long); j++)
508 * save_image_metadata - save the .orig_address fields of the PBEs
509 * from the list @pblist using the swap map handle @handle
512 static int save_image_metadata(struct pbe *pblist,
513 struct swap_map_handle *handle)
520 printk("Saving image metadata ... ");
521 buf = (unsigned long *)get_zeroed_page(GFP_ATOMIC);
526 p = pack_orig_addresses(buf, p);
527 error = swap_map_write_page(handle, (unsigned long)buf);
532 free_page((unsigned long)buf);
534 printk("done (%u pages saved)\n", n);
539 * enough_swap - Make sure we have enough swap to save the image.
541 * Returns TRUE or FALSE after checking the total amount of swap
544 * FIXME: si_swapinfo(&i) returns all swap devices information.
545 * We should only consider resume_device.
548 static int enough_swap(unsigned int nr_pages)
553 pr_debug("swsusp: available swap: %lu pages\n", i.freeswap);
554 return i.freeswap > (nr_pages + PAGES_FOR_IO +
555 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
559 * write_suspend_image - Write entire image and metadata.
561 static int write_suspend_image(struct pbe *pblist, unsigned int nr_pages)
563 struct swap_map_page *swap_map;
564 struct swap_map_handle handle;
567 if (!enough_swap(nr_pages)) {
568 printk(KERN_ERR "swsusp: Not enough free swap\n");
572 init_header(nr_pages);
573 swap_map = alloc_swap_map(swsusp_info.pages);
576 init_swap_map_handle(&handle, swap_map);
578 error = save_image_metadata(pblist, &handle);
580 error = save_image_data(pblist, &handle, nr_pages);
582 goto Free_image_entries;
584 swap_map = reverse_swap_map(swap_map);
585 error = save_swap_map(swap_map, &swsusp_info.start);
587 goto Free_map_entries;
589 error = close_swap();
591 goto Free_map_entries;
594 free_swap_map(swap_map);
598 free_swap_map_entries(swap_map);
600 free_image_entries(swap_map);
604 /* It is important _NOT_ to umount filesystems at this point. We want
605 * them synced (in case something goes wrong) but we DO not want to mark
606 * filesystem clean: it is not. (And it does not matter, if we resume
607 * correctly, we'll mark system clean, anyway.)
609 int swsusp_write(struct pbe *pblist, unsigned int nr_pages)
613 if ((error = swsusp_swap_check())) {
614 printk(KERN_ERR "swsusp: cannot find swap device, try swapon -a.\n");
618 error = write_suspend_image(pblist, nr_pages);
619 /* This will unlock ignored swap devices since writing is finished */
625 * swsusp_shrink_memory - Try to free as much memory as needed
627 * ... but do not OOM-kill anyone
629 * Notice: all userland should be stopped before it is called, or
630 * livelock is possible.
633 #define SHRINK_BITE 10000
635 int swsusp_shrink_memory(void)
639 unsigned long pages = 0;
643 printk("Shrinking memory... ");
645 size = 2 * count_highmem_pages();
646 size += size / 50 + count_data_pages();
647 size += (size + PBES_PER_PAGE - 1) / PBES_PER_PAGE +
651 if (!is_highmem(zone))
652 tmp -= zone->free_pages;
654 tmp = shrink_all_memory(SHRINK_BITE);
658 } else if (size > (image_size * 1024 * 1024) / PAGE_SIZE) {
659 tmp = shrink_all_memory(SHRINK_BITE);
662 printk("\b%c", p[i++%4]);
664 printk("\bdone (%lu pages freed)\n", pages);
669 int swsusp_suspend(void)
673 if ((error = arch_prepare_suspend()))
676 /* At this point, device_suspend() has been called, but *not*
677 * device_power_down(). We *must* device_power_down() now.
678 * Otherwise, drivers for some devices (e.g. interrupt controllers)
679 * become desynchronized with the actual state of the hardware
680 * at resume time, and evil weirdness ensues.
682 if ((error = device_power_down(PMSG_FREEZE))) {
683 printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
687 if ((error = save_highmem())) {
688 printk(KERN_ERR "swsusp: Not enough free pages for highmem\n");
689 goto Restore_highmem;
692 save_processor_state();
693 if ((error = swsusp_arch_suspend()))
694 printk(KERN_ERR "Error %d suspending\n", error);
695 /* Restore control flow magically appears here */
696 restore_processor_state();
705 int swsusp_resume(void)
709 if (device_power_down(PMSG_FREEZE))
710 printk(KERN_ERR "Some devices failed to power down, very bad\n");
711 /* We'll ignore saved state, but this gets preempt count (etc) right */
712 save_processor_state();
713 error = swsusp_arch_resume();
714 /* Code below is only ever reached in case of failure. Otherwise
715 * execution continues at place where swsusp_arch_suspend was called
718 /* The only reason why swsusp_arch_resume() can fail is memory being
719 * very tight, so we have to free it as soon as we can to avoid
720 * subsequent failures
723 restore_processor_state();
725 touch_softlockup_watchdog();
732 * mark_unsafe_pages - mark the pages that cannot be used for storing
733 * the image during resume, because they conflict with the pages that
734 * had been used before suspend
737 static void mark_unsafe_pages(struct pbe *pblist)
740 unsigned long zone_pfn;
743 if (!pblist) /* a sanity check */
746 /* Clear page flags */
747 for_each_zone (zone) {
748 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
749 if (pfn_valid(zone_pfn + zone->zone_start_pfn))
750 ClearPageNosaveFree(pfn_to_page(zone_pfn +
751 zone->zone_start_pfn));
754 /* Mark orig addresses */
755 for_each_pbe (p, pblist)
756 SetPageNosaveFree(virt_to_page(p->orig_address));
760 static void copy_page_backup_list(struct pbe *dst, struct pbe *src)
762 /* We assume both lists contain the same number of elements */
764 dst->orig_address = src->orig_address;
771 * Using bio to read from swap.
772 * This code requires a bit more work than just using buffer heads
773 * but, it is the recommended way for 2.5/2.6.
774 * The following are to signal the beginning and end of I/O. Bios
775 * finish asynchronously, while we want them to happen synchronously.
776 * A simple atomic_t, and a wait loop take care of this problem.
779 static atomic_t io_done = ATOMIC_INIT(0);
781 static int end_io(struct bio *bio, unsigned int num, int err)
783 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
784 panic("I/O error reading memory image");
785 atomic_set(&io_done, 0);
789 static struct block_device *resume_bdev;
792 * submit - submit BIO request.
793 * @rw: READ or WRITE.
794 * @off physical offset of page.
795 * @page: page we're reading or writing.
797 * Straight from the textbook - allocate and initialize the bio.
798 * If we're writing, make sure the page is marked as dirty.
799 * Then submit it and wait.
802 static int submit(int rw, pgoff_t page_off, void *page)
807 bio = bio_alloc(GFP_ATOMIC, 1);
810 bio->bi_sector = page_off * (PAGE_SIZE >> 9);
812 bio->bi_bdev = resume_bdev;
813 bio->bi_end_io = end_io;
815 if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
816 printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
822 bio_set_pages_dirty(bio);
824 atomic_set(&io_done, 1);
825 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
826 while (atomic_read(&io_done))
834 static int bio_read_page(pgoff_t page_off, void *page)
836 return submit(READ, page_off, page);
839 static int bio_write_page(pgoff_t page_off, void *page)
841 return submit(WRITE, page_off, page);
845 * The following functions allow us to read data using a swap map
846 * in a file-alike way
849 static inline void release_swap_map_reader(struct swap_map_handle *handle)
852 free_page((unsigned long)handle->cur);
856 static inline int get_swap_map_reader(struct swap_map_handle *handle,
861 if (!swp_offset(start))
863 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
866 error = bio_read_page(swp_offset(start), handle->cur);
868 release_swap_map_reader(handle);
875 static inline int swap_map_read_page(struct swap_map_handle *handle, void *buf)
877 unsigned long offset;
882 offset = swp_offset(handle->cur->entries[handle->k]);
885 error = bio_read_page(offset, buf);
888 if (++handle->k >= MAP_PAGE_SIZE) {
890 offset = swp_offset(handle->cur->next_swap);
892 release_swap_map_reader(handle);
894 error = bio_read_page(offset, handle->cur);
900 * Sanity check if this image makes sense with this kernel/swap context
901 * I really don't think that it's foolproof but more than nothing..
904 static const char *sanity_check(void)
907 if (swsusp_info.version_code != LINUX_VERSION_CODE)
908 return "kernel version";
909 if (swsusp_info.num_physpages != num_physpages)
910 return "memory size";
911 if (strcmp(swsusp_info.uts.sysname,system_utsname.sysname))
912 return "system type";
913 if (strcmp(swsusp_info.uts.release,system_utsname.release))
914 return "kernel release";
915 if (strcmp(swsusp_info.uts.version,system_utsname.version))
917 if (strcmp(swsusp_info.uts.machine,system_utsname.machine))
920 /* We can't use number of online CPUs when we use hotplug to remove them ;-))) */
921 if (swsusp_info.cpus != num_possible_cpus())
922 return "number of cpus";
927 static int check_header(void)
929 const char *reason = NULL;
932 if ((error = bio_read_page(swp_offset(swsusp_header.swsusp_info), &swsusp_info)))
935 /* Is this same machine? */
936 if ((reason = sanity_check())) {
937 printk(KERN_ERR "swsusp: Resume mismatch: %s\n",reason);
943 static int check_sig(void)
947 memset(&swsusp_header, 0, sizeof(swsusp_header));
948 if ((error = bio_read_page(0, &swsusp_header)))
950 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
951 memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
954 * Reset swap signature now.
956 error = bio_write_page(0, &swsusp_header);
961 pr_debug("swsusp: Signature found, resuming\n");
966 * load_image_data - load the image data using the swap map handle
967 * @handle and store them using the page backup list @pblist
968 * (assume there are @nr_pages pages to load)
971 static int load_image_data(struct pbe *pblist,
972 struct swap_map_handle *handle,
973 unsigned int nr_pages)
981 printk("Loading image data pages (%u pages) ... ", nr_pages);
988 error = swap_map_read_page(handle, (void *)p->address);
993 printk("\b\b\b\b%3d%%", nr_pages / m);
997 printk("\b\b\b\bdone\n");
1002 * unpack_orig_addresses - copy the elements of @buf[] (1 page) to
1003 * the PBEs in the list starting at @pbe
1006 static inline struct pbe *unpack_orig_addresses(unsigned long *buf,
1011 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
1012 pbe->orig_address = buf[j];
1019 * load_image_metadata - load the image metadata using the swap map
1020 * handle @handle and put them into the PBEs in the list @pblist
1023 static int load_image_metadata(struct pbe *pblist, struct swap_map_handle *handle)
1030 printk("Loading image metadata ... ");
1031 buf = (unsigned long *)get_zeroed_page(GFP_ATOMIC);
1036 error = swap_map_read_page(handle, buf);
1039 p = unpack_orig_addresses(buf, p);
1042 free_page((unsigned long)buf);
1044 printk("done (%u pages loaded)\n", n);
1048 static int check_suspend_image(void)
1052 if ((error = check_sig()))
1055 if ((error = check_header()))
1061 static int read_suspend_image(struct pbe **pblist_ptr)
1064 struct pbe *p, *pblist;
1065 struct swap_map_handle handle;
1066 unsigned int nr_pages = swsusp_info.image_pages;
1068 p = alloc_pagedir(nr_pages, GFP_ATOMIC, 0);
1071 error = get_swap_map_reader(&handle, swsusp_info.start);
1073 /* The PBE list at p will be released by swsusp_free() */
1075 error = load_image_metadata(p, &handle);
1077 mark_unsafe_pages(p);
1078 pblist = alloc_pagedir(nr_pages, GFP_ATOMIC, 1);
1080 copy_page_backup_list(pblist, p);
1085 /* Allocate memory for the image and read the data from swap */
1087 error = alloc_data_pages(pblist, GFP_ATOMIC, 1);
1089 release_eaten_pages();
1090 error = load_image_data(pblist, &handle, nr_pages);
1093 *pblist_ptr = pblist;
1095 release_swap_map_reader(&handle);
1100 * swsusp_check - Check for saved image in swap
1103 int swsusp_check(void)
1107 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
1108 if (!IS_ERR(resume_bdev)) {
1109 set_blocksize(resume_bdev, PAGE_SIZE);
1110 error = check_suspend_image();
1112 blkdev_put(resume_bdev);
1114 error = PTR_ERR(resume_bdev);
1117 pr_debug("swsusp: resume file found\n");
1119 pr_debug("swsusp: Error %d check for resume file\n", error);
1124 * swsusp_read - Read saved image from swap.
1127 int swsusp_read(struct pbe **pblist_ptr)
1131 if (IS_ERR(resume_bdev)) {
1132 pr_debug("swsusp: block device not initialised\n");
1133 return PTR_ERR(resume_bdev);
1136 error = read_suspend_image(pblist_ptr);
1137 blkdev_put(resume_bdev);
1140 pr_debug("swsusp: Reading resume file was successful\n");
1142 pr_debug("swsusp: Error %d resuming\n", error);
1147 * swsusp_close - close swap device.
1150 void swsusp_close(void)
1152 if (IS_ERR(resume_bdev)) {
1153 pr_debug("swsusp: block device not initialised\n");
1157 blkdev_put(resume_bdev);