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>
29 #define SWSUSP_SIG "S1SUSPEND"
31 struct swsusp_header
{
32 char reserved
[PAGE_SIZE
- 20 - sizeof(sector_t
) - sizeof(int)];
34 unsigned int flags
; /* Flags to pass to the "boot" kernel */
37 } __attribute__((packed
));
39 static struct swsusp_header
*swsusp_header
;
42 * The following functions are used for tracing the allocated
43 * swap pages, so that they can be freed in case of an error.
46 struct swsusp_extent
{
52 static struct rb_root swsusp_extents
= RB_ROOT
;
54 static int swsusp_extents_insert(unsigned long swap_offset
)
56 struct rb_node
**new = &(swsusp_extents
.rb_node
);
57 struct rb_node
*parent
= NULL
;
58 struct swsusp_extent
*ext
;
60 /* Figure out where to put the new node */
62 ext
= container_of(*new, struct swsusp_extent
, node
);
64 if (swap_offset
< ext
->start
) {
66 if (swap_offset
== ext
->start
- 1) {
70 new = &((*new)->rb_left
);
71 } else if (swap_offset
> ext
->end
) {
73 if (swap_offset
== ext
->end
+ 1) {
77 new = &((*new)->rb_right
);
79 /* It already is in the tree */
83 /* Add the new node and rebalance the tree. */
84 ext
= kzalloc(sizeof(struct swsusp_extent
), GFP_KERNEL
);
88 ext
->start
= swap_offset
;
89 ext
->end
= swap_offset
;
90 rb_link_node(&ext
->node
, parent
, new);
91 rb_insert_color(&ext
->node
, &swsusp_extents
);
96 * alloc_swapdev_block - allocate a swap page and register that it has
97 * been allocated, so that it can be freed in case of an error.
100 sector_t
alloc_swapdev_block(int swap
)
102 unsigned long offset
;
104 offset
= swp_offset(get_swap_page_of_type(swap
));
106 if (swsusp_extents_insert(offset
))
107 swap_free(swp_entry(swap
, offset
));
109 return swapdev_block(swap
, offset
);
115 * free_all_swap_pages - free swap pages allocated for saving image data.
116 * It also frees the extents used to register which swap entres had been
120 void free_all_swap_pages(int swap
)
122 struct rb_node
*node
;
124 while ((node
= swsusp_extents
.rb_node
)) {
125 struct swsusp_extent
*ext
;
126 unsigned long offset
;
128 ext
= container_of(node
, struct swsusp_extent
, node
);
129 rb_erase(node
, &swsusp_extents
);
130 for (offset
= ext
->start
; offset
<= ext
->end
; offset
++)
131 swap_free(swp_entry(swap
, offset
));
137 int swsusp_swap_in_use(void)
139 return (swsusp_extents
.rb_node
!= NULL
);
146 static unsigned short root_swap
= 0xffff;
147 static struct block_device
*resume_bdev
;
150 * submit - submit BIO request.
151 * @rw: READ or WRITE.
152 * @off physical offset of page.
153 * @page: page we're reading or writing.
154 * @bio_chain: list of pending biod (for async reading)
156 * Straight from the textbook - allocate and initialize the bio.
157 * If we're reading, make sure the page is marked as dirty.
158 * Then submit it and, if @bio_chain == NULL, wait.
160 static int submit(int rw
, pgoff_t page_off
, struct page
*page
,
161 struct bio
**bio_chain
)
163 const int bio_rw
= rw
| (1 << BIO_RW_SYNCIO
) | (1 << BIO_RW_UNPLUG
);
166 bio
= bio_alloc(__GFP_WAIT
| __GFP_HIGH
, 1);
167 bio
->bi_sector
= page_off
* (PAGE_SIZE
>> 9);
168 bio
->bi_bdev
= resume_bdev
;
169 bio
->bi_end_io
= end_swap_bio_read
;
171 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
172 printk(KERN_ERR
"PM: Adding page to bio failed at %ld\n",
181 if (bio_chain
== NULL
) {
182 submit_bio(bio_rw
, bio
);
183 wait_on_page_locked(page
);
185 bio_set_pages_dirty(bio
);
189 get_page(page
); /* These pages are freed later */
190 bio
->bi_private
= *bio_chain
;
192 submit_bio(bio_rw
, bio
);
197 static int bio_read_page(pgoff_t page_off
, void *addr
, struct bio
**bio_chain
)
199 return submit(READ
, page_off
, virt_to_page(addr
), bio_chain
);
202 static int bio_write_page(pgoff_t page_off
, void *addr
, struct bio
**bio_chain
)
204 return submit(WRITE
, page_off
, virt_to_page(addr
), bio_chain
);
207 static int wait_on_bio_chain(struct bio
**bio_chain
)
210 struct bio
*next_bio
;
213 if (bio_chain
== NULL
)
222 next_bio
= bio
->bi_private
;
223 page
= bio
->bi_io_vec
[0].bv_page
;
224 wait_on_page_locked(page
);
225 if (!PageUptodate(page
) || PageError(page
))
239 static int mark_swapfiles(sector_t start
, unsigned int flags
)
243 bio_read_page(swsusp_resume_block
, swsusp_header
, NULL
);
244 if (!memcmp("SWAP-SPACE",swsusp_header
->sig
, 10) ||
245 !memcmp("SWAPSPACE2",swsusp_header
->sig
, 10)) {
246 memcpy(swsusp_header
->orig_sig
,swsusp_header
->sig
, 10);
247 memcpy(swsusp_header
->sig
,SWSUSP_SIG
, 10);
248 swsusp_header
->image
= start
;
249 swsusp_header
->flags
= flags
;
250 error
= bio_write_page(swsusp_resume_block
,
251 swsusp_header
, NULL
);
253 printk(KERN_ERR
"PM: Swap header not found!\n");
260 * swsusp_swap_check - check if the resume device is a swap device
261 * and get its index (if so)
264 static int swsusp_swap_check(void) /* This is called before saving image */
268 res
= swap_type_of(swsusp_resume_device
, swsusp_resume_block
,
274 res
= blkdev_get(resume_bdev
, FMODE_WRITE
);
278 res
= set_blocksize(resume_bdev
, PAGE_SIZE
);
280 blkdev_put(resume_bdev
, FMODE_WRITE
);
286 * write_page - Write one page to given swap location.
287 * @buf: Address we're writing.
288 * @offset: Offset of the swap page we're writing to.
289 * @bio_chain: Link the next write BIO here
292 static int write_page(void *buf
, sector_t offset
, struct bio
**bio_chain
)
300 src
= (void *)__get_free_page(__GFP_WAIT
| __GFP_HIGH
);
302 memcpy(src
, buf
, PAGE_SIZE
);
305 bio_chain
= NULL
; /* Go synchronous */
311 return bio_write_page(offset
, src
, bio_chain
);
315 * The swap map is a data structure used for keeping track of each page
316 * written to a swap partition. It consists of many swap_map_page
317 * structures that contain each an array of MAP_PAGE_SIZE swap entries.
318 * These structures are stored on the swap and linked together with the
319 * help of the .next_swap member.
321 * The swap map is created during suspend. The swap map pages are
322 * allocated and populated one at a time, so we only need one memory
323 * page to set up the entire structure.
325 * During resume we also only need to use one swap_map_page structure
329 #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
331 struct swap_map_page
{
332 sector_t entries
[MAP_PAGE_ENTRIES
];
337 * The swap_map_handle structure is used for handling swap in
341 struct swap_map_handle
{
342 struct swap_map_page
*cur
;
347 static void release_swap_writer(struct swap_map_handle
*handle
)
350 free_page((unsigned long)handle
->cur
);
354 static int get_swap_writer(struct swap_map_handle
*handle
)
356 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(GFP_KERNEL
);
359 handle
->cur_swap
= alloc_swapdev_block(root_swap
);
360 if (!handle
->cur_swap
) {
361 release_swap_writer(handle
);
368 static int swap_write_page(struct swap_map_handle
*handle
, void *buf
,
369 struct bio
**bio_chain
)
376 offset
= alloc_swapdev_block(root_swap
);
377 error
= write_page(buf
, offset
, bio_chain
);
380 handle
->cur
->entries
[handle
->k
++] = offset
;
381 if (handle
->k
>= MAP_PAGE_ENTRIES
) {
382 error
= wait_on_bio_chain(bio_chain
);
385 offset
= alloc_swapdev_block(root_swap
);
388 handle
->cur
->next_swap
= offset
;
389 error
= write_page(handle
->cur
, handle
->cur_swap
, NULL
);
392 memset(handle
->cur
, 0, PAGE_SIZE
);
393 handle
->cur_swap
= offset
;
400 static int flush_swap_writer(struct swap_map_handle
*handle
)
402 if (handle
->cur
&& handle
->cur_swap
)
403 return write_page(handle
->cur
, handle
->cur_swap
, NULL
);
409 * save_image - save the suspend image data
412 static int save_image(struct swap_map_handle
*handle
,
413 struct snapshot_handle
*snapshot
,
414 unsigned int nr_to_write
)
422 struct timeval start
;
425 printk(KERN_INFO
"PM: Saving image data pages (%u pages) ... ",
427 m
= nr_to_write
/ 100;
432 do_gettimeofday(&start
);
434 ret
= snapshot_read_next(snapshot
, PAGE_SIZE
);
436 error
= swap_write_page(handle
, data_of(*snapshot
),
441 printk("\b\b\b\b%3d%%", nr_pages
/ m
);
445 err2
= wait_on_bio_chain(&bio
);
446 do_gettimeofday(&stop
);
450 printk("\b\b\b\bdone\n");
451 swsusp_show_speed(&start
, &stop
, nr_to_write
, "Wrote");
456 * enough_swap - Make sure we have enough swap to save the image.
458 * Returns TRUE or FALSE after checking the total amount of swap
459 * space avaiable from the resume partition.
462 static int enough_swap(unsigned int nr_pages
)
464 unsigned int free_swap
= count_swap_pages(root_swap
, 1);
466 pr_debug("PM: Free swap pages: %u\n", free_swap
);
467 return free_swap
> nr_pages
+ PAGES_FOR_IO
;
471 * swsusp_write - Write entire image and metadata.
472 * @flags: flags to pass to the "boot" kernel in the image header
474 * It is important _NOT_ to umount filesystems at this point. We want
475 * them synced (in case something goes wrong) but we DO not want to mark
476 * filesystem clean: it is not. (And it does not matter, if we resume
477 * correctly, we'll mark system clean, anyway.)
480 int swsusp_write(unsigned int flags
)
482 struct swap_map_handle handle
;
483 struct snapshot_handle snapshot
;
484 struct swsusp_info
*header
;
487 error
= swsusp_swap_check();
489 printk(KERN_ERR
"PM: Cannot find swap device, try "
493 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
494 error
= snapshot_read_next(&snapshot
, PAGE_SIZE
);
495 if (error
< PAGE_SIZE
) {
501 header
= (struct swsusp_info
*)data_of(snapshot
);
502 if (!enough_swap(header
->pages
)) {
503 printk(KERN_ERR
"PM: Not enough free swap\n");
507 error
= get_swap_writer(&handle
);
509 sector_t start
= handle
.cur_swap
;
511 error
= swap_write_page(&handle
, header
, NULL
);
513 error
= save_image(&handle
, &snapshot
,
517 flush_swap_writer(&handle
);
518 printk(KERN_INFO
"PM: S");
519 error
= mark_swapfiles(start
, flags
);
524 free_all_swap_pages(root_swap
);
526 release_swap_writer(&handle
);
528 swsusp_close(FMODE_WRITE
);
533 * The following functions allow us to read data using a swap map
534 * in a file-alike way
537 static void release_swap_reader(struct swap_map_handle
*handle
)
540 free_page((unsigned long)handle
->cur
);
544 static int get_swap_reader(struct swap_map_handle
*handle
, sector_t start
)
551 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(__GFP_WAIT
| __GFP_HIGH
);
555 error
= bio_read_page(start
, handle
->cur
, NULL
);
557 release_swap_reader(handle
);
564 static int swap_read_page(struct swap_map_handle
*handle
, void *buf
,
565 struct bio
**bio_chain
)
572 offset
= handle
->cur
->entries
[handle
->k
];
575 error
= bio_read_page(offset
, buf
, bio_chain
);
578 if (++handle
->k
>= MAP_PAGE_ENTRIES
) {
579 error
= wait_on_bio_chain(bio_chain
);
581 offset
= handle
->cur
->next_swap
;
583 release_swap_reader(handle
);
585 error
= bio_read_page(offset
, handle
->cur
, NULL
);
591 * load_image - load the image using the swap map handle
592 * @handle and the snapshot handle @snapshot
593 * (assume there are @nr_pages pages to load)
596 static int load_image(struct swap_map_handle
*handle
,
597 struct snapshot_handle
*snapshot
,
598 unsigned int nr_to_read
)
602 struct timeval start
;
608 printk(KERN_INFO
"PM: Loading image data pages (%u pages) ... ",
610 m
= nr_to_read
/ 100;
615 do_gettimeofday(&start
);
617 error
= snapshot_write_next(snapshot
, PAGE_SIZE
);
620 error
= swap_read_page(handle
, data_of(*snapshot
), &bio
);
623 if (snapshot
->sync_read
)
624 error
= wait_on_bio_chain(&bio
);
628 printk("\b\b\b\b%3d%%", nr_pages
/ m
);
631 err2
= wait_on_bio_chain(&bio
);
632 do_gettimeofday(&stop
);
636 printk("\b\b\b\bdone\n");
637 snapshot_write_finalize(snapshot
);
638 if (!snapshot_image_loaded(snapshot
))
641 swsusp_show_speed(&start
, &stop
, nr_to_read
, "Read");
646 * swsusp_read - read the hibernation image.
647 * @flags_p: flags passed by the "frozen" kernel in the image header should
648 * be written into this memeory location
651 int swsusp_read(unsigned int *flags_p
)
654 struct swap_map_handle handle
;
655 struct snapshot_handle snapshot
;
656 struct swsusp_info
*header
;
658 *flags_p
= swsusp_header
->flags
;
659 if (IS_ERR(resume_bdev
)) {
660 pr_debug("PM: Image device not initialised\n");
661 return PTR_ERR(resume_bdev
);
664 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
665 error
= snapshot_write_next(&snapshot
, PAGE_SIZE
);
666 if (error
< PAGE_SIZE
)
667 return error
< 0 ? error
: -EFAULT
;
668 header
= (struct swsusp_info
*)data_of(snapshot
);
669 error
= get_swap_reader(&handle
, swsusp_header
->image
);
671 error
= swap_read_page(&handle
, header
, NULL
);
673 error
= load_image(&handle
, &snapshot
, header
->pages
- 1);
674 release_swap_reader(&handle
);
677 pr_debug("PM: Image successfully loaded\n");
679 pr_debug("PM: Error %d resuming\n", error
);
684 * swsusp_check - Check for swsusp signature in the resume device
687 int swsusp_check(void)
691 resume_bdev
= open_by_devnum(swsusp_resume_device
, FMODE_READ
);
692 if (!IS_ERR(resume_bdev
)) {
693 set_blocksize(resume_bdev
, PAGE_SIZE
);
694 memset(swsusp_header
, 0, PAGE_SIZE
);
695 error
= bio_read_page(swsusp_resume_block
,
696 swsusp_header
, NULL
);
700 if (!memcmp(SWSUSP_SIG
, swsusp_header
->sig
, 10)) {
701 memcpy(swsusp_header
->sig
, swsusp_header
->orig_sig
, 10);
702 /* Reset swap signature now */
703 error
= bio_write_page(swsusp_resume_block
,
704 swsusp_header
, NULL
);
711 blkdev_put(resume_bdev
, FMODE_READ
);
713 pr_debug("PM: Signature found, resuming\n");
715 error
= PTR_ERR(resume_bdev
);
719 pr_debug("PM: Error %d checking image file\n", error
);
725 * swsusp_close - close swap device.
728 void swsusp_close(fmode_t mode
)
730 if (IS_ERR(resume_bdev
)) {
731 pr_debug("PM: Image device not initialised\n");
735 blkdev_put(resume_bdev
, mode
);
738 static int swsusp_header_init(void)
740 swsusp_header
= (struct swsusp_header
*) __get_free_page(GFP_KERNEL
);
742 panic("Could not allocate memory for swsusp_header\n");
746 core_initcall(swsusp_header_init
);