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"
32 struct swsusp_header
{
33 char reserved
[PAGE_SIZE
- 20 - sizeof(sector_t
) - sizeof(int)];
35 unsigned int flags
; /* Flags to pass to the "boot" kernel */
38 } __attribute__((packed
));
40 static struct swsusp_header
*swsusp_header
;
43 * The following functions are used for tracing the allocated
44 * swap pages, so that they can be freed in case of an error.
47 struct swsusp_extent
{
53 static struct rb_root swsusp_extents
= RB_ROOT
;
55 static int swsusp_extents_insert(unsigned long swap_offset
)
57 struct rb_node
**new = &(swsusp_extents
.rb_node
);
58 struct rb_node
*parent
= NULL
;
59 struct swsusp_extent
*ext
;
61 /* Figure out where to put the new node */
63 ext
= container_of(*new, struct swsusp_extent
, node
);
65 if (swap_offset
< ext
->start
) {
67 if (swap_offset
== ext
->start
- 1) {
71 new = &((*new)->rb_left
);
72 } else if (swap_offset
> ext
->end
) {
74 if (swap_offset
== ext
->end
+ 1) {
78 new = &((*new)->rb_right
);
80 /* It already is in the tree */
84 /* Add the new node and rebalance the tree. */
85 ext
= kzalloc(sizeof(struct swsusp_extent
), GFP_KERNEL
);
89 ext
->start
= swap_offset
;
90 ext
->end
= swap_offset
;
91 rb_link_node(&ext
->node
, parent
, new);
92 rb_insert_color(&ext
->node
, &swsusp_extents
);
97 * alloc_swapdev_block - allocate a swap page and register that it has
98 * been allocated, so that it can be freed in case of an error.
101 sector_t
alloc_swapdev_block(int swap
)
103 unsigned long offset
;
105 offset
= swp_offset(get_swap_page_of_type(swap
));
107 if (swsusp_extents_insert(offset
))
108 swap_free(swp_entry(swap
, offset
));
110 return swapdev_block(swap
, offset
);
116 * free_all_swap_pages - free swap pages allocated for saving image data.
117 * It also frees the extents used to register which swap entres had been
121 void free_all_swap_pages(int swap
)
123 struct rb_node
*node
;
125 while ((node
= swsusp_extents
.rb_node
)) {
126 struct swsusp_extent
*ext
;
127 unsigned long offset
;
129 ext
= container_of(node
, struct swsusp_extent
, node
);
130 rb_erase(node
, &swsusp_extents
);
131 for (offset
= ext
->start
; offset
<= ext
->end
; offset
++)
132 swap_free(swp_entry(swap
, offset
));
138 int swsusp_swap_in_use(void)
140 return (swsusp_extents
.rb_node
!= NULL
);
147 static unsigned short root_swap
= 0xffff;
148 struct block_device
*hib_resume_bdev
;
154 static int mark_swapfiles(sector_t start
, unsigned int flags
)
158 hib_bio_read_page(swsusp_resume_block
, swsusp_header
, NULL
);
159 if (!memcmp("SWAP-SPACE",swsusp_header
->sig
, 10) ||
160 !memcmp("SWAPSPACE2",swsusp_header
->sig
, 10)) {
161 memcpy(swsusp_header
->orig_sig
,swsusp_header
->sig
, 10);
162 memcpy(swsusp_header
->sig
,SWSUSP_SIG
, 10);
163 swsusp_header
->image
= start
;
164 swsusp_header
->flags
= flags
;
165 error
= hib_bio_write_page(swsusp_resume_block
,
166 swsusp_header
, NULL
);
168 printk(KERN_ERR
"PM: Swap header not found!\n");
175 * swsusp_swap_check - check if the resume device is a swap device
176 * and get its index (if so)
179 static int swsusp_swap_check(void) /* This is called before saving image */
183 res
= swap_type_of(swsusp_resume_device
, swsusp_resume_block
,
189 res
= blkdev_get(hib_resume_bdev
, FMODE_WRITE
);
193 res
= set_blocksize(hib_resume_bdev
, PAGE_SIZE
);
195 blkdev_put(hib_resume_bdev
, FMODE_WRITE
);
201 * write_page - Write one page to given swap location.
202 * @buf: Address we're writing.
203 * @offset: Offset of the swap page we're writing to.
204 * @bio_chain: Link the next write BIO here
207 static int write_page(void *buf
, sector_t offset
, struct bio
**bio_chain
)
215 src
= (void *)__get_free_page(__GFP_WAIT
| __GFP_HIGH
);
217 memcpy(src
, buf
, PAGE_SIZE
);
220 bio_chain
= NULL
; /* Go synchronous */
226 return hib_bio_write_page(offset
, src
, bio_chain
);
230 * The swap map is a data structure used for keeping track of each page
231 * written to a swap partition. It consists of many swap_map_page
232 * structures that contain each an array of MAP_PAGE_SIZE swap entries.
233 * These structures are stored on the swap and linked together with the
234 * help of the .next_swap member.
236 * The swap map is created during suspend. The swap map pages are
237 * allocated and populated one at a time, so we only need one memory
238 * page to set up the entire structure.
240 * During resume we also only need to use one swap_map_page structure
244 #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
246 struct swap_map_page
{
247 sector_t entries
[MAP_PAGE_ENTRIES
];
252 * The swap_map_handle structure is used for handling swap in
256 struct swap_map_handle
{
257 struct swap_map_page
*cur
;
262 static void release_swap_writer(struct swap_map_handle
*handle
)
265 free_page((unsigned long)handle
->cur
);
269 static int get_swap_writer(struct swap_map_handle
*handle
)
271 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(GFP_KERNEL
);
274 handle
->cur_swap
= alloc_swapdev_block(root_swap
);
275 if (!handle
->cur_swap
) {
276 release_swap_writer(handle
);
283 static int swap_write_page(struct swap_map_handle
*handle
, void *buf
,
284 struct bio
**bio_chain
)
291 offset
= alloc_swapdev_block(root_swap
);
292 error
= write_page(buf
, offset
, bio_chain
);
295 handle
->cur
->entries
[handle
->k
++] = offset
;
296 if (handle
->k
>= MAP_PAGE_ENTRIES
) {
297 error
= hib_wait_on_bio_chain(bio_chain
);
300 offset
= alloc_swapdev_block(root_swap
);
303 handle
->cur
->next_swap
= offset
;
304 error
= write_page(handle
->cur
, handle
->cur_swap
, NULL
);
307 memset(handle
->cur
, 0, PAGE_SIZE
);
308 handle
->cur_swap
= offset
;
315 static int flush_swap_writer(struct swap_map_handle
*handle
)
317 if (handle
->cur
&& handle
->cur_swap
)
318 return write_page(handle
->cur
, handle
->cur_swap
, NULL
);
324 * save_image - save the suspend image data
327 static int save_image(struct swap_map_handle
*handle
,
328 struct snapshot_handle
*snapshot
,
329 unsigned int nr_to_write
)
336 struct timeval start
;
339 printk(KERN_INFO
"PM: Saving image data pages (%u pages) ... ",
341 m
= nr_to_write
/ 100;
346 do_gettimeofday(&start
);
348 ret
= snapshot_read_next(snapshot
);
351 ret
= swap_write_page(handle
, data_of(*snapshot
), &bio
);
355 printk(KERN_CONT
"\b\b\b\b%3d%%", nr_pages
/ m
);
358 err2
= hib_wait_on_bio_chain(&bio
);
359 do_gettimeofday(&stop
);
363 printk(KERN_CONT
"\b\b\b\bdone\n");
365 printk(KERN_CONT
"\n");
366 swsusp_show_speed(&start
, &stop
, nr_to_write
, "Wrote");
371 * enough_swap - Make sure we have enough swap to save the image.
373 * Returns TRUE or FALSE after checking the total amount of swap
374 * space avaiable from the resume partition.
377 static int enough_swap(unsigned int nr_pages
)
379 unsigned int free_swap
= count_swap_pages(root_swap
, 1);
381 pr_debug("PM: Free swap pages: %u\n", free_swap
);
382 return free_swap
> nr_pages
+ PAGES_FOR_IO
;
386 * swsusp_write - Write entire image and metadata.
387 * @flags: flags to pass to the "boot" kernel in the image header
389 * It is important _NOT_ to umount filesystems at this point. We want
390 * them synced (in case something goes wrong) but we DO not want to mark
391 * filesystem clean: it is not. (And it does not matter, if we resume
392 * correctly, we'll mark system clean, anyway.)
395 int swsusp_write(unsigned int flags
)
397 struct swap_map_handle handle
;
398 struct snapshot_handle snapshot
;
399 struct swsusp_info
*header
;
402 error
= swsusp_swap_check();
404 printk(KERN_ERR
"PM: Cannot find swap device, try "
408 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
409 error
= snapshot_read_next(&snapshot
);
410 if (error
< PAGE_SIZE
) {
416 header
= (struct swsusp_info
*)data_of(snapshot
);
417 if (!enough_swap(header
->pages
)) {
418 printk(KERN_ERR
"PM: Not enough free swap\n");
422 error
= get_swap_writer(&handle
);
424 sector_t start
= handle
.cur_swap
;
426 error
= swap_write_page(&handle
, header
, NULL
);
428 error
= save_image(&handle
, &snapshot
,
432 flush_swap_writer(&handle
);
433 printk(KERN_INFO
"PM: S");
434 error
= mark_swapfiles(start
, flags
);
439 free_all_swap_pages(root_swap
);
441 release_swap_writer(&handle
);
443 swsusp_close(FMODE_WRITE
);
448 * The following functions allow us to read data using a swap map
449 * in a file-alike way
452 static void release_swap_reader(struct swap_map_handle
*handle
)
455 free_page((unsigned long)handle
->cur
);
459 static int get_swap_reader(struct swap_map_handle
*handle
, sector_t start
)
466 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(__GFP_WAIT
| __GFP_HIGH
);
470 error
= hib_bio_read_page(start
, handle
->cur
, NULL
);
472 release_swap_reader(handle
);
479 static int swap_read_page(struct swap_map_handle
*handle
, void *buf
,
480 struct bio
**bio_chain
)
487 offset
= handle
->cur
->entries
[handle
->k
];
490 error
= hib_bio_read_page(offset
, buf
, bio_chain
);
493 if (++handle
->k
>= MAP_PAGE_ENTRIES
) {
494 error
= hib_wait_on_bio_chain(bio_chain
);
496 offset
= handle
->cur
->next_swap
;
498 release_swap_reader(handle
);
500 error
= hib_bio_read_page(offset
, handle
->cur
, NULL
);
506 * load_image - load the image using the swap map handle
507 * @handle and the snapshot handle @snapshot
508 * (assume there are @nr_pages pages to load)
511 static int load_image(struct swap_map_handle
*handle
,
512 struct snapshot_handle
*snapshot
,
513 unsigned int nr_to_read
)
517 struct timeval start
;
523 printk(KERN_INFO
"PM: Loading image data pages (%u pages) ... ",
525 m
= nr_to_read
/ 100;
530 do_gettimeofday(&start
);
532 error
= snapshot_write_next(snapshot
);
535 error
= swap_read_page(handle
, data_of(*snapshot
), &bio
);
538 if (snapshot
->sync_read
)
539 error
= hib_wait_on_bio_chain(&bio
);
543 printk("\b\b\b\b%3d%%", nr_pages
/ m
);
546 err2
= hib_wait_on_bio_chain(&bio
);
547 do_gettimeofday(&stop
);
551 printk("\b\b\b\bdone\n");
552 snapshot_write_finalize(snapshot
);
553 if (!snapshot_image_loaded(snapshot
))
557 swsusp_show_speed(&start
, &stop
, nr_to_read
, "Read");
562 * swsusp_read - read the hibernation image.
563 * @flags_p: flags passed by the "frozen" kernel in the image header should
564 * be written into this memeory location
567 int swsusp_read(unsigned int *flags_p
)
570 struct swap_map_handle handle
;
571 struct snapshot_handle snapshot
;
572 struct swsusp_info
*header
;
574 *flags_p
= swsusp_header
->flags
;
576 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
577 error
= snapshot_write_next(&snapshot
);
578 if (error
< PAGE_SIZE
)
579 return error
< 0 ? error
: -EFAULT
;
580 header
= (struct swsusp_info
*)data_of(snapshot
);
581 error
= get_swap_reader(&handle
, swsusp_header
->image
);
583 error
= swap_read_page(&handle
, header
, NULL
);
585 error
= load_image(&handle
, &snapshot
, header
->pages
- 1);
586 release_swap_reader(&handle
);
589 pr_debug("PM: Image successfully loaded\n");
591 pr_debug("PM: Error %d resuming\n", error
);
596 * swsusp_check - Check for swsusp signature in the resume device
599 int swsusp_check(void)
603 hib_resume_bdev
= open_by_devnum(swsusp_resume_device
, FMODE_READ
);
604 if (!IS_ERR(hib_resume_bdev
)) {
605 set_blocksize(hib_resume_bdev
, PAGE_SIZE
);
606 memset(swsusp_header
, 0, PAGE_SIZE
);
607 error
= hib_bio_read_page(swsusp_resume_block
,
608 swsusp_header
, NULL
);
612 if (!memcmp(SWSUSP_SIG
, swsusp_header
->sig
, 10)) {
613 memcpy(swsusp_header
->sig
, swsusp_header
->orig_sig
, 10);
614 /* Reset swap signature now */
615 error
= hib_bio_write_page(swsusp_resume_block
,
616 swsusp_header
, NULL
);
623 blkdev_put(hib_resume_bdev
, FMODE_READ
);
625 pr_debug("PM: Signature found, resuming\n");
627 error
= PTR_ERR(hib_resume_bdev
);
631 pr_debug("PM: Error %d checking image file\n", error
);
637 * swsusp_close - close swap device.
640 void swsusp_close(fmode_t mode
)
642 if (IS_ERR(hib_resume_bdev
)) {
643 pr_debug("PM: Image device not initialised\n");
647 blkdev_put(hib_resume_bdev
, mode
);
650 static int swsusp_header_init(void)
652 swsusp_header
= (struct swsusp_header
*) __get_free_page(GFP_KERNEL
);
654 panic("Could not allocate memory for swsusp_header\n");
658 core_initcall(swsusp_header_init
);