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 static struct block_device
*resume_bdev
;
151 * submit - submit BIO request.
152 * @rw: READ or WRITE.
153 * @off physical offset of page.
154 * @page: page we're reading or writing.
155 * @bio_chain: list of pending biod (for async reading)
157 * Straight from the textbook - allocate and initialize the bio.
158 * If we're reading, make sure the page is marked as dirty.
159 * Then submit it and, if @bio_chain == NULL, wait.
161 static int submit(int rw
, pgoff_t page_off
, struct page
*page
,
162 struct bio
**bio_chain
)
164 const int bio_rw
= rw
| (1 << BIO_RW_SYNCIO
) | (1 << BIO_RW_UNPLUG
);
167 bio
= bio_alloc(__GFP_WAIT
| __GFP_HIGH
, 1);
168 bio
->bi_sector
= page_off
* (PAGE_SIZE
>> 9);
169 bio
->bi_bdev
= resume_bdev
;
170 bio
->bi_end_io
= end_swap_bio_read
;
172 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
173 printk(KERN_ERR
"PM: Adding page to bio failed at %ld\n",
182 if (bio_chain
== NULL
) {
183 submit_bio(bio_rw
, bio
);
184 wait_on_page_locked(page
);
186 bio_set_pages_dirty(bio
);
190 get_page(page
); /* These pages are freed later */
191 bio
->bi_private
= *bio_chain
;
193 submit_bio(bio_rw
, bio
);
198 static int bio_read_page(pgoff_t page_off
, void *addr
, struct bio
**bio_chain
)
200 return submit(READ
, page_off
, virt_to_page(addr
), bio_chain
);
203 static int bio_write_page(pgoff_t page_off
, void *addr
, struct bio
**bio_chain
)
205 return submit(WRITE
, page_off
, virt_to_page(addr
), bio_chain
);
208 static int wait_on_bio_chain(struct bio
**bio_chain
)
211 struct bio
*next_bio
;
214 if (bio_chain
== NULL
)
223 next_bio
= bio
->bi_private
;
224 page
= bio
->bi_io_vec
[0].bv_page
;
225 wait_on_page_locked(page
);
226 if (!PageUptodate(page
) || PageError(page
))
240 static int mark_swapfiles(sector_t start
, unsigned int flags
)
244 bio_read_page(swsusp_resume_block
, swsusp_header
, NULL
);
245 if (!memcmp("SWAP-SPACE",swsusp_header
->sig
, 10) ||
246 !memcmp("SWAPSPACE2",swsusp_header
->sig
, 10)) {
247 memcpy(swsusp_header
->orig_sig
,swsusp_header
->sig
, 10);
248 memcpy(swsusp_header
->sig
,SWSUSP_SIG
, 10);
249 swsusp_header
->image
= start
;
250 swsusp_header
->flags
= flags
;
251 error
= bio_write_page(swsusp_resume_block
,
252 swsusp_header
, NULL
);
254 printk(KERN_ERR
"PM: Swap header not found!\n");
261 * swsusp_swap_check - check if the resume device is a swap device
262 * and get its index (if so)
265 static int swsusp_swap_check(void) /* This is called before saving image */
269 res
= swap_type_of(swsusp_resume_device
, swsusp_resume_block
,
275 res
= blkdev_get(resume_bdev
, FMODE_WRITE
);
279 res
= set_blocksize(resume_bdev
, PAGE_SIZE
);
281 blkdev_put(resume_bdev
, FMODE_WRITE
);
287 * write_page - Write one page to given swap location.
288 * @buf: Address we're writing.
289 * @offset: Offset of the swap page we're writing to.
290 * @bio_chain: Link the next write BIO here
293 static int write_page(void *buf
, sector_t offset
, struct bio
**bio_chain
)
301 src
= (void *)__get_free_page(__GFP_WAIT
| __GFP_HIGH
);
303 memcpy(src
, buf
, PAGE_SIZE
);
306 bio_chain
= NULL
; /* Go synchronous */
312 return bio_write_page(offset
, src
, bio_chain
);
316 * The swap map is a data structure used for keeping track of each page
317 * written to a swap partition. It consists of many swap_map_page
318 * structures that contain each an array of MAP_PAGE_SIZE swap entries.
319 * These structures are stored on the swap and linked together with the
320 * help of the .next_swap member.
322 * The swap map is created during suspend. The swap map pages are
323 * allocated and populated one at a time, so we only need one memory
324 * page to set up the entire structure.
326 * During resume we also only need to use one swap_map_page structure
330 #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
332 struct swap_map_page
{
333 sector_t entries
[MAP_PAGE_ENTRIES
];
338 * The swap_map_handle structure is used for handling swap in
342 struct swap_map_handle
{
343 struct swap_map_page
*cur
;
348 static void release_swap_writer(struct swap_map_handle
*handle
)
351 free_page((unsigned long)handle
->cur
);
355 static int get_swap_writer(struct swap_map_handle
*handle
)
357 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(GFP_KERNEL
);
360 handle
->cur_swap
= alloc_swapdev_block(root_swap
);
361 if (!handle
->cur_swap
) {
362 release_swap_writer(handle
);
369 static int swap_write_page(struct swap_map_handle
*handle
, void *buf
,
370 struct bio
**bio_chain
)
377 offset
= alloc_swapdev_block(root_swap
);
378 error
= write_page(buf
, offset
, bio_chain
);
381 handle
->cur
->entries
[handle
->k
++] = offset
;
382 if (handle
->k
>= MAP_PAGE_ENTRIES
) {
383 error
= wait_on_bio_chain(bio_chain
);
386 offset
= alloc_swapdev_block(root_swap
);
389 handle
->cur
->next_swap
= offset
;
390 error
= write_page(handle
->cur
, handle
->cur_swap
, NULL
);
393 memset(handle
->cur
, 0, PAGE_SIZE
);
394 handle
->cur_swap
= offset
;
401 static int flush_swap_writer(struct swap_map_handle
*handle
)
403 if (handle
->cur
&& handle
->cur_swap
)
404 return write_page(handle
->cur
, handle
->cur_swap
, NULL
);
410 * save_image - save the suspend image data
413 static int save_image(struct swap_map_handle
*handle
,
414 struct snapshot_handle
*snapshot
,
415 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
);
437 ret
= swap_write_page(handle
, data_of(*snapshot
), &bio
);
441 printk(KERN_CONT
"\b\b\b\b%3d%%", nr_pages
/ m
);
444 err2
= wait_on_bio_chain(&bio
);
445 do_gettimeofday(&stop
);
449 printk(KERN_CONT
"\b\b\b\bdone\n");
451 printk(KERN_CONT
"\n");
452 swsusp_show_speed(&start
, &stop
, nr_to_write
, "Wrote");
457 * enough_swap - Make sure we have enough swap to save the image.
459 * Returns TRUE or FALSE after checking the total amount of swap
460 * space avaiable from the resume partition.
463 static int enough_swap(unsigned int nr_pages
)
465 unsigned int free_swap
= count_swap_pages(root_swap
, 1);
467 pr_debug("PM: Free swap pages: %u\n", free_swap
);
468 return free_swap
> nr_pages
+ PAGES_FOR_IO
;
472 * swsusp_write - Write entire image and metadata.
473 * @flags: flags to pass to the "boot" kernel in the image header
475 * It is important _NOT_ to umount filesystems at this point. We want
476 * them synced (in case something goes wrong) but we DO not want to mark
477 * filesystem clean: it is not. (And it does not matter, if we resume
478 * correctly, we'll mark system clean, anyway.)
481 int swsusp_write(unsigned int flags
)
483 struct swap_map_handle handle
;
484 struct snapshot_handle snapshot
;
485 struct swsusp_info
*header
;
488 error
= swsusp_swap_check();
490 printk(KERN_ERR
"PM: Cannot find swap device, try "
494 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
495 error
= snapshot_read_next(&snapshot
, PAGE_SIZE
);
496 if (error
< PAGE_SIZE
) {
502 header
= (struct swsusp_info
*)data_of(snapshot
);
503 if (!enough_swap(header
->pages
)) {
504 printk(KERN_ERR
"PM: Not enough free swap\n");
508 error
= get_swap_writer(&handle
);
510 sector_t start
= handle
.cur_swap
;
512 error
= swap_write_page(&handle
, header
, NULL
);
514 error
= save_image(&handle
, &snapshot
,
518 flush_swap_writer(&handle
);
519 printk(KERN_INFO
"PM: S");
520 error
= mark_swapfiles(start
, flags
);
525 free_all_swap_pages(root_swap
);
527 release_swap_writer(&handle
);
529 swsusp_close(FMODE_WRITE
);
534 * The following functions allow us to read data using a swap map
535 * in a file-alike way
538 static void release_swap_reader(struct swap_map_handle
*handle
)
541 free_page((unsigned long)handle
->cur
);
545 static int get_swap_reader(struct swap_map_handle
*handle
, sector_t start
)
552 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(__GFP_WAIT
| __GFP_HIGH
);
556 error
= bio_read_page(start
, handle
->cur
, NULL
);
558 release_swap_reader(handle
);
565 static int swap_read_page(struct swap_map_handle
*handle
, void *buf
,
566 struct bio
**bio_chain
)
573 offset
= handle
->cur
->entries
[handle
->k
];
576 error
= bio_read_page(offset
, buf
, bio_chain
);
579 if (++handle
->k
>= MAP_PAGE_ENTRIES
) {
580 error
= wait_on_bio_chain(bio_chain
);
582 offset
= handle
->cur
->next_swap
;
584 release_swap_reader(handle
);
586 error
= bio_read_page(offset
, handle
->cur
, NULL
);
592 * load_image - load the image using the swap map handle
593 * @handle and the snapshot handle @snapshot
594 * (assume there are @nr_pages pages to load)
597 static int load_image(struct swap_map_handle
*handle
,
598 struct snapshot_handle
*snapshot
,
599 unsigned int nr_to_read
)
603 struct timeval start
;
609 printk(KERN_INFO
"PM: Loading image data pages (%u pages) ... ",
611 m
= nr_to_read
/ 100;
616 do_gettimeofday(&start
);
618 error
= snapshot_write_next(snapshot
, PAGE_SIZE
);
621 error
= swap_read_page(handle
, data_of(*snapshot
), &bio
);
624 if (snapshot
->sync_read
)
625 error
= wait_on_bio_chain(&bio
);
629 printk("\b\b\b\b%3d%%", nr_pages
/ m
);
632 err2
= wait_on_bio_chain(&bio
);
633 do_gettimeofday(&stop
);
637 printk("\b\b\b\bdone\n");
638 snapshot_write_finalize(snapshot
);
639 if (!snapshot_image_loaded(snapshot
))
643 swsusp_show_speed(&start
, &stop
, nr_to_read
, "Read");
648 * swsusp_read - read the hibernation image.
649 * @flags_p: flags passed by the "frozen" kernel in the image header should
650 * be written into this memeory location
653 int swsusp_read(unsigned int *flags_p
)
656 struct swap_map_handle handle
;
657 struct snapshot_handle snapshot
;
658 struct swsusp_info
*header
;
660 *flags_p
= swsusp_header
->flags
;
662 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
663 error
= snapshot_write_next(&snapshot
, PAGE_SIZE
);
664 if (error
< PAGE_SIZE
)
665 return error
< 0 ? error
: -EFAULT
;
666 header
= (struct swsusp_info
*)data_of(snapshot
);
667 error
= get_swap_reader(&handle
, swsusp_header
->image
);
669 error
= swap_read_page(&handle
, header
, NULL
);
671 error
= load_image(&handle
, &snapshot
, header
->pages
- 1);
672 release_swap_reader(&handle
);
675 pr_debug("PM: Image successfully loaded\n");
677 pr_debug("PM: Error %d resuming\n", error
);
682 * swsusp_check - Check for swsusp signature in the resume device
685 int swsusp_check(void)
689 resume_bdev
= open_by_devnum(swsusp_resume_device
, FMODE_READ
);
690 if (!IS_ERR(resume_bdev
)) {
691 set_blocksize(resume_bdev
, PAGE_SIZE
);
692 memset(swsusp_header
, 0, PAGE_SIZE
);
693 error
= bio_read_page(swsusp_resume_block
,
694 swsusp_header
, NULL
);
698 if (!memcmp(SWSUSP_SIG
, swsusp_header
->sig
, 10)) {
699 memcpy(swsusp_header
->sig
, swsusp_header
->orig_sig
, 10);
700 /* Reset swap signature now */
701 error
= bio_write_page(swsusp_resume_block
,
702 swsusp_header
, NULL
);
709 blkdev_put(resume_bdev
, FMODE_READ
);
711 pr_debug("PM: Signature found, resuming\n");
713 error
= PTR_ERR(resume_bdev
);
717 pr_debug("PM: Error %d checking image file\n", error
);
723 * swsusp_close - close swap device.
726 void swsusp_close(fmode_t mode
)
728 if (IS_ERR(resume_bdev
)) {
729 pr_debug("PM: Image device not initialised\n");
733 blkdev_put(resume_bdev
, mode
);
736 static int swsusp_header_init(void)
738 swsusp_header
= (struct swsusp_header
*) __get_free_page(GFP_KERNEL
);
740 panic("Could not allocate memory for swsusp_header\n");
744 core_initcall(swsusp_header_init
);