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 unsigned int count_highmem_pages(void);
74 int save_highmem(void);
75 int restore_highmem(void);
77 static int save_highmem(void) { return 0; }
78 static int restore_highmem(void) { return 0; }
79 static unsigned int count_highmem_pages(void) { return 0; }
82 extern char resume_file[];
84 #define SWSUSP_SIG "S1SUSPEND"
86 static struct swsusp_header {
87 char reserved[PAGE_SIZE - 20 - sizeof(swp_entry_t)];
88 swp_entry_t swsusp_info;
91 } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
93 static struct swsusp_info swsusp_info;
99 /* We memorize in swapfile_used what swap devices are used for suspension */
100 #define SWAPFILE_UNUSED 0
101 #define SWAPFILE_SUSPEND 1 /* This is the suspending device */
102 #define SWAPFILE_IGNORED 2 /* Those are other swap devices ignored for suspension */
104 static unsigned short swapfile_used[MAX_SWAPFILES];
105 static unsigned short root_swap;
107 static int mark_swapfiles(swp_entry_t prev)
111 rw_swap_page_sync(READ,
112 swp_entry(root_swap, 0),
113 virt_to_page((unsigned long)&swsusp_header));
114 if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
115 !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
116 memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
117 memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
118 swsusp_header.swsusp_info = prev;
119 error = rw_swap_page_sync(WRITE,
120 swp_entry(root_swap, 0),
121 virt_to_page((unsigned long)
124 pr_debug("swsusp: Partition is not swap space.\n");
131 * Check whether the swap device is the specified resume
132 * device, irrespective of whether they are specified by
135 * (Thus, device inode aliasing is allowed. You can say /dev/hda4
136 * instead of /dev/ide/host0/bus0/target0/lun0/part4 [if using devfs]
137 * and they'll be considered the same device. This is *necessary* for
138 * devfs, since the resume code can only recognize the form /dev/hda4,
139 * but the suspend code would see the long name.)
141 static int is_resume_device(const struct swap_info_struct *swap_info)
143 struct file *file = swap_info->swap_file;
144 struct inode *inode = file->f_dentry->d_inode;
146 return S_ISBLK(inode->i_mode) &&
147 swsusp_resume_device == MKDEV(imajor(inode), iminor(inode));
150 static int swsusp_swap_check(void) /* This is called before saving image */
154 len=strlen(resume_file);
157 spin_lock(&swap_lock);
158 for (i=0; i<MAX_SWAPFILES; i++) {
159 if (!(swap_info[i].flags & SWP_WRITEOK)) {
160 swapfile_used[i]=SWAPFILE_UNUSED;
163 printk(KERN_WARNING "resume= option should be used to set suspend device" );
164 if (root_swap == 0xFFFF) {
165 swapfile_used[i] = SWAPFILE_SUSPEND;
168 swapfile_used[i] = SWAPFILE_IGNORED;
170 /* we ignore all swap devices that are not the resume_file */
171 if (is_resume_device(&swap_info[i])) {
172 swapfile_used[i] = SWAPFILE_SUSPEND;
175 swapfile_used[i] = SWAPFILE_IGNORED;
180 spin_unlock(&swap_lock);
181 return (root_swap != 0xffff) ? 0 : -ENODEV;
185 * This is called after saving image so modification
186 * will be lost after resume... and that's what we want.
187 * we make the device unusable. A new call to
188 * lock_swapdevices can unlock the devices.
190 static void lock_swapdevices(void)
194 spin_lock(&swap_lock);
195 for (i = 0; i< MAX_SWAPFILES; i++)
196 if (swapfile_used[i] == SWAPFILE_IGNORED) {
197 swap_info[i].flags ^= SWP_WRITEOK;
199 spin_unlock(&swap_lock);
203 * write_page - Write one page to a fresh swap location.
204 * @addr: Address we're writing.
205 * @loc: Place to store the entry we used.
207 * Allocate a new swap entry and 'sync' it. Note we discard -EIO
208 * errors. That is an artifact left over from swsusp. It did not
209 * check the return of rw_swap_page_sync() at all, since most pages
210 * written back to swap would return -EIO.
211 * This is a partial improvement, since we will at least return other
212 * errors, though we need to eventually fix the damn code.
214 static int write_page(unsigned long addr, swp_entry_t *loc)
219 entry = get_swap_page();
220 if (swp_offset(entry) &&
221 swapfile_used[swp_type(entry)] == SWAPFILE_SUSPEND) {
222 error = rw_swap_page_sync(WRITE, entry,
234 * Swap map-handling functions
236 * The swap map is a data structure used for keeping track of each page
237 * written to the swap. It consists of many swap_map_page structures
238 * that contain each an array of MAP_PAGE_SIZE swap entries.
239 * These structures are linked together with the help of either the
240 * .next (in memory) or the .next_swap (in swap) member.
242 * The swap map is created during suspend. At that time we need to keep
243 * it in memory, because we have to free all of the allocated swap
244 * entries if an error occurs. The memory needed is preallocated
245 * so that we know in advance if there's enough of it.
247 * The first swap_map_page structure is filled with the swap entries that
248 * correspond to the first MAP_PAGE_SIZE data pages written to swap and
249 * so on. After the all of the data pages have been written, the order
250 * of the swap_map_page structures in the map is reversed so that they
251 * can be read from swap in the original order. This causes the data
252 * pages to be loaded in exactly the same order in which they have been
255 * During resume we only need to use one swap_map_page structure
256 * at a time, which means that we only need to use two memory pages for
257 * reading the image - one for reading the swap_map_page structures
258 * and the second for reading the data pages from swap.
261 #define MAP_PAGE_SIZE ((PAGE_SIZE - sizeof(swp_entry_t) - sizeof(void *)) \
262 / sizeof(swp_entry_t))
264 struct swap_map_page {
265 swp_entry_t entries[MAP_PAGE_SIZE];
266 swp_entry_t next_swap;
267 struct swap_map_page *next;
270 static inline void free_swap_map(struct swap_map_page *swap_map)
272 struct swap_map_page *swp;
275 swp = swap_map->next;
276 free_page((unsigned long)swap_map);
281 static struct swap_map_page *alloc_swap_map(unsigned int nr_pages)
283 struct swap_map_page *swap_map, *swp;
289 pr_debug("alloc_swap_map(): nr_pages = %d\n", nr_pages);
290 swap_map = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
292 for (n = MAP_PAGE_SIZE; n < nr_pages; n += MAP_PAGE_SIZE) {
293 swp->next = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
296 free_swap_map(swap_map);
304 * reverse_swap_map - reverse the order of pages in the swap map
308 static inline struct swap_map_page *reverse_swap_map(struct swap_map_page *swap_map)
310 struct swap_map_page *prev, *next;
314 next = swap_map->next;
315 swap_map->next = prev;
323 * free_swap_map_entries - free the swap entries allocated to store
324 * the swap map @swap_map (this is only called in case of an error)
326 static inline void free_swap_map_entries(struct swap_map_page *swap_map)
329 if (swap_map->next_swap.val)
330 swap_free(swap_map->next_swap);
331 swap_map = swap_map->next;
336 * save_swap_map - save the swap map used for tracing the data pages
340 static int save_swap_map(struct swap_map_page *swap_map, swp_entry_t *start)
342 swp_entry_t entry = (swp_entry_t){0};
346 swap_map->next_swap = entry;
347 if ((error = write_page((unsigned long)swap_map, &entry)))
349 swap_map = swap_map->next;
356 * free_image_entries - free the swap entries allocated to store
357 * the image data pages (this is only called in case of an error)
360 static inline void free_image_entries(struct swap_map_page *swp)
365 for (k = 0; k < MAP_PAGE_SIZE; k++)
366 if (swp->entries[k].val)
367 swap_free(swp->entries[k]);
373 * The swap_map_handle structure is used for handling the swap map in
377 struct swap_map_handle {
378 struct swap_map_page *cur;
382 static inline void init_swap_map_handle(struct swap_map_handle *handle,
383 struct swap_map_page *map)
389 static inline int swap_map_write_page(struct swap_map_handle *handle,
394 error = write_page(addr, handle->cur->entries + handle->k);
397 if (++handle->k >= MAP_PAGE_SIZE) {
398 handle->cur = handle->cur->next;
405 * save_image_data - save the data pages pointed to by the PBEs
406 * from the list @pblist using the swap map handle @handle
407 * (assume there are @nr_pages data pages to save)
410 static int save_image_data(struct pbe *pblist,
411 struct swap_map_handle *handle,
412 unsigned int nr_pages)
418 printk("Saving image data pages (%u pages) ... ", nr_pages);
423 for_each_pbe (p, pblist) {
424 error = swap_map_write_page(handle, p->address);
428 printk("\b\b\b\b%3d%%", nr_pages / m);
432 printk("\b\b\b\bdone\n");
436 static void dump_info(void)
438 pr_debug(" swsusp: Version: %u\n",swsusp_info.version_code);
439 pr_debug(" swsusp: Num Pages: %ld\n",swsusp_info.num_physpages);
440 pr_debug(" swsusp: UTS Sys: %s\n",swsusp_info.uts.sysname);
441 pr_debug(" swsusp: UTS Node: %s\n",swsusp_info.uts.nodename);
442 pr_debug(" swsusp: UTS Release: %s\n",swsusp_info.uts.release);
443 pr_debug(" swsusp: UTS Version: %s\n",swsusp_info.uts.version);
444 pr_debug(" swsusp: UTS Machine: %s\n",swsusp_info.uts.machine);
445 pr_debug(" swsusp: UTS Domain: %s\n",swsusp_info.uts.domainname);
446 pr_debug(" swsusp: CPUs: %d\n",swsusp_info.cpus);
447 pr_debug(" swsusp: Image: %ld Pages\n",swsusp_info.image_pages);
448 pr_debug(" swsusp: Total: %ld Pages\n", swsusp_info.pages);
451 static void init_header(unsigned int nr_pages)
453 memset(&swsusp_info, 0, sizeof(swsusp_info));
454 swsusp_info.version_code = LINUX_VERSION_CODE;
455 swsusp_info.num_physpages = num_physpages;
456 memcpy(&swsusp_info.uts, &system_utsname, sizeof(system_utsname));
458 swsusp_info.cpus = num_online_cpus();
459 swsusp_info.image_pages = nr_pages;
460 swsusp_info.pages = nr_pages +
461 ((nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT);
464 static int close_swap(void)
470 error = write_page((unsigned long)&swsusp_info, &entry);
473 error = mark_swapfiles(entry);
480 * pack_orig_addresses - the .orig_address fields of the PBEs from the
481 * list starting at @pbe are stored in the array @buf[] (1 page)
484 static inline struct pbe *pack_orig_addresses(unsigned long *buf,
489 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
490 buf[j] = pbe->orig_address;
494 for (; j < PAGE_SIZE / sizeof(long); j++)
500 * save_image_metadata - save the .orig_address fields of the PBEs
501 * from the list @pblist using the swap map handle @handle
504 static int save_image_metadata(struct pbe *pblist,
505 struct swap_map_handle *handle)
512 printk("Saving image metadata ... ");
513 buf = (unsigned long *)get_zeroed_page(GFP_ATOMIC);
518 p = pack_orig_addresses(buf, p);
519 error = swap_map_write_page(handle, (unsigned long)buf);
524 free_page((unsigned long)buf);
526 printk("done (%u pages saved)\n", n);
531 * enough_swap - Make sure we have enough swap to save the image.
533 * Returns TRUE or FALSE after checking the total amount of swap
536 * FIXME: si_swapinfo(&i) returns all swap devices information.
537 * We should only consider resume_device.
540 static int enough_swap(unsigned int nr_pages)
545 pr_debug("swsusp: available swap: %lu pages\n", i.freeswap);
546 return i.freeswap > (nr_pages + PAGES_FOR_IO +
547 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
551 * write_suspend_image - Write entire image and metadata.
553 static int write_suspend_image(struct pbe *pblist, unsigned int nr_pages)
555 struct swap_map_page *swap_map;
556 struct swap_map_handle handle;
559 if (!enough_swap(nr_pages)) {
560 printk(KERN_ERR "swsusp: Not enough free swap\n");
564 init_header(nr_pages);
565 swap_map = alloc_swap_map(swsusp_info.pages);
568 init_swap_map_handle(&handle, swap_map);
570 error = save_image_metadata(pblist, &handle);
572 error = save_image_data(pblist, &handle, nr_pages);
574 goto Free_image_entries;
576 swap_map = reverse_swap_map(swap_map);
577 error = save_swap_map(swap_map, &swsusp_info.start);
579 goto Free_map_entries;
581 error = close_swap();
583 goto Free_map_entries;
586 free_swap_map(swap_map);
590 free_swap_map_entries(swap_map);
592 free_image_entries(swap_map);
596 /* It is important _NOT_ to umount filesystems at this point. We want
597 * them synced (in case something goes wrong) but we DO not want to mark
598 * filesystem clean: it is not. (And it does not matter, if we resume
599 * correctly, we'll mark system clean, anyway.)
601 int swsusp_write(struct pbe *pblist, unsigned int nr_pages)
605 if ((error = swsusp_swap_check())) {
606 printk(KERN_ERR "swsusp: cannot find swap device, try swapon -a.\n");
610 error = write_suspend_image(pblist, nr_pages);
611 /* This will unlock ignored swap devices since writing is finished */
617 * swsusp_shrink_memory - Try to free as much memory as needed
619 * ... but do not OOM-kill anyone
621 * Notice: all userland should be stopped before it is called, or
622 * livelock is possible.
625 #define SHRINK_BITE 10000
627 int swsusp_shrink_memory(void)
631 unsigned long pages = 0;
635 printk("Shrinking memory... ");
638 tmp = 2 * count_highmem_pages();
639 tmp += tmp / 50 + count_data_pages();
640 tmp += (tmp + PBES_PER_PAGE - 1) / PBES_PER_PAGE +
643 if (!is_highmem(zone))
644 tmp -= zone->free_pages;
646 tmp = shrink_all_memory(SHRINK_BITE);
652 tmp = shrink_all_memory(SHRINK_BITE);
655 printk("\b%c", p[i++%4]);
657 printk("\bdone (%lu pages freed)\n", pages);
662 int swsusp_suspend(void)
666 if ((error = arch_prepare_suspend()))
669 /* At this point, device_suspend() has been called, but *not*
670 * device_power_down(). We *must* device_power_down() now.
671 * Otherwise, drivers for some devices (e.g. interrupt controllers)
672 * become desynchronized with the actual state of the hardware
673 * at resume time, and evil weirdness ensues.
675 if ((error = device_power_down(PMSG_FREEZE))) {
676 printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
680 if ((error = save_highmem())) {
681 printk(KERN_ERR "swsusp: Not enough free pages for highmem\n");
682 goto Restore_highmem;
685 save_processor_state();
686 if ((error = swsusp_arch_suspend()))
687 printk(KERN_ERR "Error %d suspending\n", error);
688 /* Restore control flow magically appears here */
689 restore_processor_state();
698 int swsusp_resume(void)
702 if (device_power_down(PMSG_FREEZE))
703 printk(KERN_ERR "Some devices failed to power down, very bad\n");
704 /* We'll ignore saved state, but this gets preempt count (etc) right */
705 save_processor_state();
706 error = swsusp_arch_resume();
707 /* Code below is only ever reached in case of failure. Otherwise
708 * execution continues at place where swsusp_arch_suspend was called
711 /* The only reason why swsusp_arch_resume() can fail is memory being
712 * very tight, so we have to free it as soon as we can to avoid
713 * subsequent failures
716 restore_processor_state();
718 touch_softlockup_watchdog();
725 * mark_unsafe_pages - mark the pages that cannot be used for storing
726 * the image during resume, because they conflict with the pages that
727 * had been used before suspend
730 static void mark_unsafe_pages(struct pbe *pblist)
733 unsigned long zone_pfn;
736 if (!pblist) /* a sanity check */
739 /* Clear page flags */
740 for_each_zone (zone) {
741 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
742 if (pfn_valid(zone_pfn + zone->zone_start_pfn))
743 ClearPageNosaveFree(pfn_to_page(zone_pfn +
744 zone->zone_start_pfn));
747 /* Mark orig addresses */
748 for_each_pbe (p, pblist)
749 SetPageNosaveFree(virt_to_page(p->orig_address));
753 static void copy_page_backup_list(struct pbe *dst, struct pbe *src)
755 /* We assume both lists contain the same number of elements */
757 dst->orig_address = src->orig_address;
764 * Using bio to read from swap.
765 * This code requires a bit more work than just using buffer heads
766 * but, it is the recommended way for 2.5/2.6.
767 * The following are to signal the beginning and end of I/O. Bios
768 * finish asynchronously, while we want them to happen synchronously.
769 * A simple atomic_t, and a wait loop take care of this problem.
772 static atomic_t io_done = ATOMIC_INIT(0);
774 static int end_io(struct bio *bio, unsigned int num, int err)
776 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
777 panic("I/O error reading memory image");
778 atomic_set(&io_done, 0);
782 static struct block_device *resume_bdev;
785 * submit - submit BIO request.
786 * @rw: READ or WRITE.
787 * @off physical offset of page.
788 * @page: page we're reading or writing.
790 * Straight from the textbook - allocate and initialize the bio.
791 * If we're writing, make sure the page is marked as dirty.
792 * Then submit it and wait.
795 static int submit(int rw, pgoff_t page_off, void *page)
800 bio = bio_alloc(GFP_ATOMIC, 1);
803 bio->bi_sector = page_off * (PAGE_SIZE >> 9);
805 bio->bi_bdev = resume_bdev;
806 bio->bi_end_io = end_io;
808 if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
809 printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
815 bio_set_pages_dirty(bio);
817 atomic_set(&io_done, 1);
818 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
819 while (atomic_read(&io_done))
827 static int bio_read_page(pgoff_t page_off, void *page)
829 return submit(READ, page_off, page);
832 static int bio_write_page(pgoff_t page_off, void *page)
834 return submit(WRITE, page_off, page);
838 * The following functions allow us to read data using a swap map
839 * in a file-alike way
842 static inline void release_swap_map_reader(struct swap_map_handle *handle)
845 free_page((unsigned long)handle->cur);
849 static inline int get_swap_map_reader(struct swap_map_handle *handle,
854 if (!swp_offset(start))
856 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
859 error = bio_read_page(swp_offset(start), handle->cur);
861 release_swap_map_reader(handle);
868 static inline int swap_map_read_page(struct swap_map_handle *handle, void *buf)
870 unsigned long offset;
875 offset = swp_offset(handle->cur->entries[handle->k]);
878 error = bio_read_page(offset, buf);
881 if (++handle->k >= MAP_PAGE_SIZE) {
883 offset = swp_offset(handle->cur->next_swap);
885 release_swap_map_reader(handle);
887 error = bio_read_page(offset, handle->cur);
893 * Sanity check if this image makes sense with this kernel/swap context
894 * I really don't think that it's foolproof but more than nothing..
897 static const char *sanity_check(void)
900 if (swsusp_info.version_code != LINUX_VERSION_CODE)
901 return "kernel version";
902 if (swsusp_info.num_physpages != num_physpages)
903 return "memory size";
904 if (strcmp(swsusp_info.uts.sysname,system_utsname.sysname))
905 return "system type";
906 if (strcmp(swsusp_info.uts.release,system_utsname.release))
907 return "kernel release";
908 if (strcmp(swsusp_info.uts.version,system_utsname.version))
910 if (strcmp(swsusp_info.uts.machine,system_utsname.machine))
913 /* We can't use number of online CPUs when we use hotplug to remove them ;-))) */
914 if (swsusp_info.cpus != num_possible_cpus())
915 return "number of cpus";
920 static int check_header(void)
922 const char *reason = NULL;
925 if ((error = bio_read_page(swp_offset(swsusp_header.swsusp_info), &swsusp_info)))
928 /* Is this same machine? */
929 if ((reason = sanity_check())) {
930 printk(KERN_ERR "swsusp: Resume mismatch: %s\n",reason);
936 static int check_sig(void)
940 memset(&swsusp_header, 0, sizeof(swsusp_header));
941 if ((error = bio_read_page(0, &swsusp_header)))
943 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
944 memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
947 * Reset swap signature now.
949 error = bio_write_page(0, &swsusp_header);
954 pr_debug("swsusp: Signature found, resuming\n");
959 * load_image_data - load the image data using the swap map handle
960 * @handle and store them using the page backup list @pblist
961 * (assume there are @nr_pages pages to load)
964 static int load_image_data(struct pbe *pblist,
965 struct swap_map_handle *handle,
966 unsigned int nr_pages)
974 printk("Loading image data pages (%u pages) ... ", nr_pages);
981 error = swap_map_read_page(handle, (void *)p->address);
986 printk("\b\b\b\b%3d%%", nr_pages / m);
990 printk("\b\b\b\bdone\n");
995 * unpack_orig_addresses - copy the elements of @buf[] (1 page) to
996 * the PBEs in the list starting at @pbe
999 static inline struct pbe *unpack_orig_addresses(unsigned long *buf,
1004 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
1005 pbe->orig_address = buf[j];
1012 * load_image_metadata - load the image metadata using the swap map
1013 * handle @handle and put them into the PBEs in the list @pblist
1016 static int load_image_metadata(struct pbe *pblist, struct swap_map_handle *handle)
1023 printk("Loading image metadata ... ");
1024 buf = (unsigned long *)get_zeroed_page(GFP_ATOMIC);
1029 error = swap_map_read_page(handle, buf);
1032 p = unpack_orig_addresses(buf, p);
1035 free_page((unsigned long)buf);
1037 printk("done (%u pages loaded)\n", n);
1041 static int check_suspend_image(void)
1045 if ((error = check_sig()))
1048 if ((error = check_header()))
1054 static int read_suspend_image(struct pbe **pblist_ptr)
1057 struct pbe *p, *pblist;
1058 struct swap_map_handle handle;
1059 unsigned int nr_pages = swsusp_info.image_pages;
1061 p = alloc_pagedir(nr_pages, GFP_ATOMIC, 0);
1064 error = get_swap_map_reader(&handle, swsusp_info.start);
1066 /* The PBE list at p will be released by swsusp_free() */
1068 error = load_image_metadata(p, &handle);
1070 mark_unsafe_pages(p);
1071 pblist = alloc_pagedir(nr_pages, GFP_ATOMIC, 1);
1073 copy_page_backup_list(pblist, p);
1078 /* Allocate memory for the image and read the data from swap */
1080 error = alloc_data_pages(pblist, GFP_ATOMIC, 1);
1082 release_eaten_pages();
1083 error = load_image_data(pblist, &handle, nr_pages);
1086 *pblist_ptr = pblist;
1088 release_swap_map_reader(&handle);
1093 * swsusp_check - Check for saved image in swap
1096 int swsusp_check(void)
1100 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
1101 if (!IS_ERR(resume_bdev)) {
1102 set_blocksize(resume_bdev, PAGE_SIZE);
1103 error = check_suspend_image();
1105 blkdev_put(resume_bdev);
1107 error = PTR_ERR(resume_bdev);
1110 pr_debug("swsusp: resume file found\n");
1112 pr_debug("swsusp: Error %d check for resume file\n", error);
1117 * swsusp_read - Read saved image from swap.
1120 int swsusp_read(struct pbe **pblist_ptr)
1124 if (IS_ERR(resume_bdev)) {
1125 pr_debug("swsusp: block device not initialised\n");
1126 return PTR_ERR(resume_bdev);
1129 error = read_suspend_image(pblist_ptr);
1130 blkdev_put(resume_bdev);
1133 pr_debug("swsusp: Reading resume file was successful\n");
1135 pr_debug("swsusp: Error %d resuming\n", error);
1140 * swsusp_close - close swap device.
1143 void swsusp_close(void)
1145 if (IS_ERR(resume_bdev)) {
1146 pr_debug("swsusp: block device not initialised\n");
1150 blkdev_put(resume_bdev);