2 * linux/kernel/power/swap.c
4 * This file provides functions for reading the suspend image from
5 * and writing it to a swap partition.
7 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
8 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
10 * This file is released under the GPLv2.
14 #include <linux/module.h>
15 #include <linux/file.h>
16 #include <linux/delay.h>
17 #include <linux/bitops.h>
18 #include <linux/genhd.h>
19 #include <linux/device.h>
20 #include <linux/buffer_head.h>
21 #include <linux/bio.h>
22 #include <linux/blkdev.h>
23 #include <linux/swap.h>
24 #include <linux/swapops.h>
26 #include <linux/slab.h>
30 #define SWSUSP_SIG "S1SUSPEND"
33 * The swap map is a data structure used for keeping track of each page
34 * written to a swap partition. It consists of many swap_map_page
35 * structures that contain each an array of MAP_PAGE_SIZE swap entries.
36 * These structures are stored on the swap and linked together with the
37 * help of the .next_swap member.
39 * The swap map is created during suspend. The swap map pages are
40 * allocated and populated one at a time, so we only need one memory
41 * page to set up the entire structure.
43 * During resume we also only need to use one swap_map_page structure
47 #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
49 struct swap_map_page
{
50 sector_t entries
[MAP_PAGE_ENTRIES
];
55 * The swap_map_handle structure is used for handling swap in
59 struct swap_map_handle
{
60 struct swap_map_page
*cur
;
62 sector_t first_sector
;
66 struct swsusp_header
{
67 char reserved
[PAGE_SIZE
- 20 - sizeof(sector_t
) - sizeof(int)];
69 unsigned int flags
; /* Flags to pass to the "boot" kernel */
72 } __attribute__((packed
));
74 static struct swsusp_header
*swsusp_header
;
77 * The following functions are used for tracing the allocated
78 * swap pages, so that they can be freed in case of an error.
81 struct swsusp_extent
{
87 static struct rb_root swsusp_extents
= RB_ROOT
;
89 static int swsusp_extents_insert(unsigned long swap_offset
)
91 struct rb_node
**new = &(swsusp_extents
.rb_node
);
92 struct rb_node
*parent
= NULL
;
93 struct swsusp_extent
*ext
;
95 /* Figure out where to put the new node */
97 ext
= container_of(*new, struct swsusp_extent
, node
);
99 if (swap_offset
< ext
->start
) {
101 if (swap_offset
== ext
->start
- 1) {
105 new = &((*new)->rb_left
);
106 } else if (swap_offset
> ext
->end
) {
108 if (swap_offset
== ext
->end
+ 1) {
112 new = &((*new)->rb_right
);
114 /* It already is in the tree */
118 /* Add the new node and rebalance the tree. */
119 ext
= kzalloc(sizeof(struct swsusp_extent
), GFP_KERNEL
);
123 ext
->start
= swap_offset
;
124 ext
->end
= swap_offset
;
125 rb_link_node(&ext
->node
, parent
, new);
126 rb_insert_color(&ext
->node
, &swsusp_extents
);
131 * alloc_swapdev_block - allocate a swap page and register that it has
132 * been allocated, so that it can be freed in case of an error.
135 sector_t
alloc_swapdev_block(int swap
)
137 unsigned long offset
;
139 offset
= swp_offset(get_swap_page_of_type(swap
));
141 if (swsusp_extents_insert(offset
))
142 swap_free(swp_entry(swap
, offset
));
144 return swapdev_block(swap
, offset
);
150 * free_all_swap_pages - free swap pages allocated for saving image data.
151 * It also frees the extents used to register which swap entres had been
155 void free_all_swap_pages(int swap
)
157 struct rb_node
*node
;
159 while ((node
= swsusp_extents
.rb_node
)) {
160 struct swsusp_extent
*ext
;
161 unsigned long offset
;
163 ext
= container_of(node
, struct swsusp_extent
, node
);
164 rb_erase(node
, &swsusp_extents
);
165 for (offset
= ext
->start
; offset
<= ext
->end
; offset
++)
166 swap_free(swp_entry(swap
, offset
));
172 int swsusp_swap_in_use(void)
174 return (swsusp_extents
.rb_node
!= NULL
);
181 static unsigned short root_swap
= 0xffff;
182 struct block_device
*hib_resume_bdev
;
188 static int mark_swapfiles(struct swap_map_handle
*handle
, unsigned int flags
)
192 hib_bio_read_page(swsusp_resume_block
, swsusp_header
, NULL
);
193 if (!memcmp("SWAP-SPACE",swsusp_header
->sig
, 10) ||
194 !memcmp("SWAPSPACE2",swsusp_header
->sig
, 10)) {
195 memcpy(swsusp_header
->orig_sig
,swsusp_header
->sig
, 10);
196 memcpy(swsusp_header
->sig
,SWSUSP_SIG
, 10);
197 swsusp_header
->image
= handle
->first_sector
;
198 swsusp_header
->flags
= flags
;
199 error
= hib_bio_write_page(swsusp_resume_block
,
200 swsusp_header
, NULL
);
202 printk(KERN_ERR
"PM: Swap header not found!\n");
209 * swsusp_swap_check - check if the resume device is a swap device
210 * and get its index (if so)
212 * This is called before saving image
214 static int swsusp_swap_check(void)
218 res
= swap_type_of(swsusp_resume_device
, swsusp_resume_block
,
224 res
= blkdev_get(hib_resume_bdev
, FMODE_WRITE
);
228 res
= set_blocksize(hib_resume_bdev
, PAGE_SIZE
);
230 blkdev_put(hib_resume_bdev
, FMODE_WRITE
);
236 * write_page - Write one page to given swap location.
237 * @buf: Address we're writing.
238 * @offset: Offset of the swap page we're writing to.
239 * @bio_chain: Link the next write BIO here
242 static int write_page(void *buf
, sector_t offset
, struct bio
**bio_chain
)
250 src
= (void *)__get_free_page(__GFP_WAIT
| __GFP_HIGH
);
252 memcpy(src
, buf
, PAGE_SIZE
);
255 bio_chain
= NULL
; /* Go synchronous */
261 return hib_bio_write_page(offset
, src
, bio_chain
);
264 static void release_swap_writer(struct swap_map_handle
*handle
)
267 free_page((unsigned long)handle
->cur
);
271 static int get_swap_writer(struct swap_map_handle
*handle
)
275 ret
= swsusp_swap_check();
278 printk(KERN_ERR
"PM: Cannot find swap device, try "
282 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(GFP_KERNEL
);
287 handle
->cur_swap
= alloc_swapdev_block(root_swap
);
288 if (!handle
->cur_swap
) {
293 handle
->first_sector
= handle
->cur_swap
;
296 release_swap_writer(handle
);
298 swsusp_close(FMODE_WRITE
);
302 static int swap_write_page(struct swap_map_handle
*handle
, void *buf
,
303 struct bio
**bio_chain
)
310 offset
= alloc_swapdev_block(root_swap
);
311 error
= write_page(buf
, offset
, bio_chain
);
314 handle
->cur
->entries
[handle
->k
++] = offset
;
315 if (handle
->k
>= MAP_PAGE_ENTRIES
) {
316 error
= hib_wait_on_bio_chain(bio_chain
);
319 offset
= alloc_swapdev_block(root_swap
);
322 handle
->cur
->next_swap
= offset
;
323 error
= write_page(handle
->cur
, handle
->cur_swap
, NULL
);
326 memset(handle
->cur
, 0, PAGE_SIZE
);
327 handle
->cur_swap
= offset
;
334 static int flush_swap_writer(struct swap_map_handle
*handle
)
336 if (handle
->cur
&& handle
->cur_swap
)
337 return write_page(handle
->cur
, handle
->cur_swap
, NULL
);
342 static int swap_writer_finish(struct swap_map_handle
*handle
,
343 unsigned int flags
, int error
)
346 flush_swap_writer(handle
);
347 printk(KERN_INFO
"PM: S");
348 error
= mark_swapfiles(handle
, flags
);
353 free_all_swap_pages(root_swap
);
354 release_swap_writer(handle
);
355 swsusp_close(FMODE_WRITE
);
361 * save_image - save the suspend image data
364 static int save_image(struct swap_map_handle
*handle
,
365 struct snapshot_handle
*snapshot
,
366 unsigned int nr_to_write
)
373 struct timeval start
;
376 printk(KERN_INFO
"PM: Saving image data pages (%u pages) ... ",
378 m
= nr_to_write
/ 100;
383 do_gettimeofday(&start
);
385 ret
= snapshot_read_next(snapshot
);
388 ret
= swap_write_page(handle
, data_of(*snapshot
), &bio
);
392 printk(KERN_CONT
"\b\b\b\b%3d%%", nr_pages
/ m
);
395 err2
= hib_wait_on_bio_chain(&bio
);
396 do_gettimeofday(&stop
);
400 printk(KERN_CONT
"\b\b\b\bdone\n");
402 printk(KERN_CONT
"\n");
403 swsusp_show_speed(&start
, &stop
, nr_to_write
, "Wrote");
408 * enough_swap - Make sure we have enough swap to save the image.
410 * Returns TRUE or FALSE after checking the total amount of swap
411 * space avaiable from the resume partition.
414 static int enough_swap(unsigned int nr_pages
)
416 unsigned int free_swap
= count_swap_pages(root_swap
, 1);
418 pr_debug("PM: Free swap pages: %u\n", free_swap
);
419 return free_swap
> nr_pages
+ PAGES_FOR_IO
;
423 * swsusp_write - Write entire image and metadata.
424 * @flags: flags to pass to the "boot" kernel in the image header
426 * It is important _NOT_ to umount filesystems at this point. We want
427 * them synced (in case something goes wrong) but we DO not want to mark
428 * filesystem clean: it is not. (And it does not matter, if we resume
429 * correctly, we'll mark system clean, anyway.)
432 int swsusp_write(unsigned int flags
)
434 struct swap_map_handle handle
;
435 struct snapshot_handle snapshot
;
436 struct swsusp_info
*header
;
440 pages
= snapshot_get_image_size();
441 error
= get_swap_writer(&handle
);
443 printk(KERN_ERR
"PM: Cannot get swap writer\n");
446 if (!enough_swap(pages
)) {
447 printk(KERN_ERR
"PM: Not enough free swap\n");
451 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
452 error
= snapshot_read_next(&snapshot
);
453 if (error
< PAGE_SIZE
) {
459 header
= (struct swsusp_info
*)data_of(snapshot
);
460 error
= swap_write_page(&handle
, header
, NULL
);
462 error
= save_image(&handle
, &snapshot
, pages
- 1);
464 error
= swap_writer_finish(&handle
, flags
, error
);
469 * The following functions allow us to read data using a swap map
470 * in a file-alike way
473 static void release_swap_reader(struct swap_map_handle
*handle
)
476 free_page((unsigned long)handle
->cur
);
480 static int get_swap_reader(struct swap_map_handle
*handle
,
481 unsigned int *flags_p
)
485 *flags_p
= swsusp_header
->flags
;
487 if (!swsusp_header
->image
) /* how can this happen? */
490 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(__GFP_WAIT
| __GFP_HIGH
);
494 error
= hib_bio_read_page(swsusp_header
->image
, handle
->cur
, NULL
);
496 release_swap_reader(handle
);
503 static int swap_read_page(struct swap_map_handle
*handle
, void *buf
,
504 struct bio
**bio_chain
)
511 offset
= handle
->cur
->entries
[handle
->k
];
514 error
= hib_bio_read_page(offset
, buf
, bio_chain
);
517 if (++handle
->k
>= MAP_PAGE_ENTRIES
) {
518 error
= hib_wait_on_bio_chain(bio_chain
);
520 offset
= handle
->cur
->next_swap
;
522 release_swap_reader(handle
);
524 error
= hib_bio_read_page(offset
, handle
->cur
, NULL
);
529 static int swap_reader_finish(struct swap_map_handle
*handle
)
531 release_swap_reader(handle
);
537 * load_image - load the image using the swap map handle
538 * @handle and the snapshot handle @snapshot
539 * (assume there are @nr_pages pages to load)
542 static int load_image(struct swap_map_handle
*handle
,
543 struct snapshot_handle
*snapshot
,
544 unsigned int nr_to_read
)
548 struct timeval start
;
554 printk(KERN_INFO
"PM: Loading image data pages (%u pages) ... ",
556 m
= nr_to_read
/ 100;
561 do_gettimeofday(&start
);
563 error
= snapshot_write_next(snapshot
);
566 error
= swap_read_page(handle
, data_of(*snapshot
), &bio
);
569 if (snapshot
->sync_read
)
570 error
= hib_wait_on_bio_chain(&bio
);
574 printk("\b\b\b\b%3d%%", nr_pages
/ m
);
577 err2
= hib_wait_on_bio_chain(&bio
);
578 do_gettimeofday(&stop
);
582 printk("\b\b\b\bdone\n");
583 snapshot_write_finalize(snapshot
);
584 if (!snapshot_image_loaded(snapshot
))
588 swsusp_show_speed(&start
, &stop
, nr_to_read
, "Read");
593 * swsusp_read - read the hibernation image.
594 * @flags_p: flags passed by the "frozen" kernel in the image header should
595 * be written into this memeory location
598 int swsusp_read(unsigned int *flags_p
)
601 struct swap_map_handle handle
;
602 struct snapshot_handle snapshot
;
603 struct swsusp_info
*header
;
605 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
606 error
= snapshot_write_next(&snapshot
);
607 if (error
< PAGE_SIZE
)
608 return error
< 0 ? error
: -EFAULT
;
609 header
= (struct swsusp_info
*)data_of(snapshot
);
610 error
= get_swap_reader(&handle
, flags_p
);
614 error
= swap_read_page(&handle
, header
, NULL
);
616 error
= load_image(&handle
, &snapshot
, header
->pages
- 1);
617 swap_reader_finish(&handle
);
620 pr_debug("PM: Image successfully loaded\n");
622 pr_debug("PM: Error %d resuming\n", error
);
627 * swsusp_check - Check for swsusp signature in the resume device
630 int swsusp_check(void)
634 hib_resume_bdev
= open_by_devnum(swsusp_resume_device
, FMODE_READ
);
635 if (!IS_ERR(hib_resume_bdev
)) {
636 set_blocksize(hib_resume_bdev
, PAGE_SIZE
);
637 memset(swsusp_header
, 0, PAGE_SIZE
);
638 error
= hib_bio_read_page(swsusp_resume_block
,
639 swsusp_header
, NULL
);
643 if (!memcmp(SWSUSP_SIG
, swsusp_header
->sig
, 10)) {
644 memcpy(swsusp_header
->sig
, swsusp_header
->orig_sig
, 10);
645 /* Reset swap signature now */
646 error
= hib_bio_write_page(swsusp_resume_block
,
647 swsusp_header
, NULL
);
654 blkdev_put(hib_resume_bdev
, FMODE_READ
);
656 pr_debug("PM: Signature found, resuming\n");
658 error
= PTR_ERR(hib_resume_bdev
);
662 pr_debug("PM: Error %d checking image file\n", error
);
668 * swsusp_close - close swap device.
671 void swsusp_close(fmode_t mode
)
673 if (IS_ERR(hib_resume_bdev
)) {
674 pr_debug("PM: Image device not initialised\n");
678 blkdev_put(hib_resume_bdev
, mode
);
681 static int swsusp_header_init(void)
683 swsusp_header
= (struct swsusp_header
*) __get_free_page(GFP_KERNEL
);
685 panic("Could not allocate memory for swsusp_header\n");
689 core_initcall(swsusp_header_init
);