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/smp_lock.h>
16 #include <linux/file.h>
17 #include <linux/utsname.h>
18 #include <linux/version.h>
19 #include <linux/delay.h>
20 #include <linux/bitops.h>
21 #include <linux/genhd.h>
22 #include <linux/device.h>
23 #include <linux/buffer_head.h>
24 #include <linux/bio.h>
25 #include <linux/blkdev.h>
26 #include <linux/swap.h>
27 #include <linux/swapops.h>
32 extern char resume_file
[];
34 #define SWSUSP_SIG "S1SUSPEND"
36 struct swsusp_header
{
37 char reserved
[PAGE_SIZE
- 20 - sizeof(sector_t
)];
41 } __attribute__((packed
));
43 static struct swsusp_header
*swsusp_header
;
49 static unsigned short root_swap
= 0xffff;
50 static struct block_device
*resume_bdev
;
53 * submit - submit BIO request.
55 * @off physical offset of page.
56 * @page: page we're reading or writing.
57 * @bio_chain: list of pending biod (for async reading)
59 * Straight from the textbook - allocate and initialize the bio.
60 * If we're reading, make sure the page is marked as dirty.
61 * Then submit it and, if @bio_chain == NULL, wait.
63 static int submit(int rw
, pgoff_t page_off
, struct page
*page
,
64 struct bio
**bio_chain
)
68 bio
= bio_alloc(__GFP_WAIT
| __GFP_HIGH
, 1);
71 bio
->bi_sector
= page_off
* (PAGE_SIZE
>> 9);
72 bio
->bi_bdev
= resume_bdev
;
73 bio
->bi_end_io
= end_swap_bio_read
;
75 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
76 printk("swsusp: ERROR: adding page to bio at %ld\n", page_off
);
84 if (bio_chain
== NULL
) {
85 submit_bio(rw
| (1 << BIO_RW_SYNC
), bio
);
86 wait_on_page_locked(page
);
88 bio_set_pages_dirty(bio
);
92 get_page(page
); /* These pages are freed later */
93 bio
->bi_private
= *bio_chain
;
95 submit_bio(rw
| (1 << BIO_RW_SYNC
), bio
);
100 static int bio_read_page(pgoff_t page_off
, void *addr
, struct bio
**bio_chain
)
102 return submit(READ
, page_off
, virt_to_page(addr
), bio_chain
);
105 static int bio_write_page(pgoff_t page_off
, void *addr
, struct bio
**bio_chain
)
107 return submit(WRITE
, page_off
, virt_to_page(addr
), bio_chain
);
110 static int wait_on_bio_chain(struct bio
**bio_chain
)
113 struct bio
*next_bio
;
116 if (bio_chain
== NULL
)
125 next_bio
= bio
->bi_private
;
126 page
= bio
->bi_io_vec
[0].bv_page
;
127 wait_on_page_locked(page
);
128 if (!PageUptodate(page
) || PageError(page
))
142 static int mark_swapfiles(sector_t start
)
146 bio_read_page(swsusp_resume_block
, swsusp_header
, NULL
);
147 if (!memcmp("SWAP-SPACE",swsusp_header
->sig
, 10) ||
148 !memcmp("SWAPSPACE2",swsusp_header
->sig
, 10)) {
149 memcpy(swsusp_header
->orig_sig
,swsusp_header
->sig
, 10);
150 memcpy(swsusp_header
->sig
,SWSUSP_SIG
, 10);
151 swsusp_header
->image
= start
;
152 error
= bio_write_page(swsusp_resume_block
,
153 swsusp_header
, NULL
);
155 printk(KERN_ERR
"swsusp: Swap header not found!\n");
162 * swsusp_swap_check - check if the resume device is a swap device
163 * and get its index (if so)
166 static int swsusp_swap_check(void) /* This is called before saving image */
170 res
= swap_type_of(swsusp_resume_device
, swsusp_resume_block
,
176 res
= blkdev_get(resume_bdev
, FMODE_WRITE
, O_RDWR
);
180 res
= set_blocksize(resume_bdev
, PAGE_SIZE
);
182 blkdev_put(resume_bdev
);
188 * write_page - Write one page to given swap location.
189 * @buf: Address we're writing.
190 * @offset: Offset of the swap page we're writing to.
191 * @bio_chain: Link the next write BIO here
194 static int write_page(void *buf
, sector_t offset
, struct bio
**bio_chain
)
202 src
= (void *)__get_free_page(__GFP_WAIT
| __GFP_HIGH
);
204 memcpy(src
, buf
, PAGE_SIZE
);
207 bio_chain
= NULL
; /* Go synchronous */
213 return bio_write_page(offset
, src
, bio_chain
);
217 * The swap map is a data structure used for keeping track of each page
218 * written to a swap partition. It consists of many swap_map_page
219 * structures that contain each an array of MAP_PAGE_SIZE swap entries.
220 * These structures are stored on the swap and linked together with the
221 * help of the .next_swap member.
223 * The swap map is created during suspend. The swap map pages are
224 * allocated and populated one at a time, so we only need one memory
225 * page to set up the entire structure.
227 * During resume we also only need to use one swap_map_page structure
231 #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
233 struct swap_map_page
{
234 sector_t entries
[MAP_PAGE_ENTRIES
];
239 * The swap_map_handle structure is used for handling swap in
243 struct swap_map_handle
{
244 struct swap_map_page
*cur
;
246 struct bitmap_page
*bitmap
;
250 static void release_swap_writer(struct swap_map_handle
*handle
)
253 free_page((unsigned long)handle
->cur
);
256 free_bitmap(handle
->bitmap
);
257 handle
->bitmap
= NULL
;
260 static int get_swap_writer(struct swap_map_handle
*handle
)
262 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(GFP_KERNEL
);
265 handle
->bitmap
= alloc_bitmap(count_swap_pages(root_swap
, 0));
266 if (!handle
->bitmap
) {
267 release_swap_writer(handle
);
270 handle
->cur_swap
= alloc_swapdev_block(root_swap
, handle
->bitmap
);
271 if (!handle
->cur_swap
) {
272 release_swap_writer(handle
);
279 static int swap_write_page(struct swap_map_handle
*handle
, void *buf
,
280 struct bio
**bio_chain
)
287 offset
= alloc_swapdev_block(root_swap
, handle
->bitmap
);
288 error
= write_page(buf
, offset
, bio_chain
);
291 handle
->cur
->entries
[handle
->k
++] = offset
;
292 if (handle
->k
>= MAP_PAGE_ENTRIES
) {
293 error
= wait_on_bio_chain(bio_chain
);
296 offset
= alloc_swapdev_block(root_swap
, handle
->bitmap
);
299 handle
->cur
->next_swap
= offset
;
300 error
= write_page(handle
->cur
, handle
->cur_swap
, NULL
);
303 memset(handle
->cur
, 0, PAGE_SIZE
);
304 handle
->cur_swap
= offset
;
311 static int flush_swap_writer(struct swap_map_handle
*handle
)
313 if (handle
->cur
&& handle
->cur_swap
)
314 return write_page(handle
->cur
, handle
->cur_swap
, NULL
);
320 * save_image - save the suspend image data
323 static int save_image(struct swap_map_handle
*handle
,
324 struct snapshot_handle
*snapshot
,
325 unsigned int nr_to_write
)
333 struct timeval start
;
336 printk("Saving image data pages (%u pages) ... ", nr_to_write
);
337 m
= nr_to_write
/ 100;
342 do_gettimeofday(&start
);
344 ret
= snapshot_read_next(snapshot
, PAGE_SIZE
);
346 error
= swap_write_page(handle
, data_of(*snapshot
),
351 printk("\b\b\b\b%3d%%", nr_pages
/ m
);
355 err2
= wait_on_bio_chain(&bio
);
356 do_gettimeofday(&stop
);
360 printk("\b\b\b\bdone\n");
361 swsusp_show_speed(&start
, &stop
, nr_to_write
, "Wrote");
366 * enough_swap - Make sure we have enough swap to save the image.
368 * Returns TRUE or FALSE after checking the total amount of swap
369 * space avaiable from the resume partition.
372 static int enough_swap(unsigned int nr_pages
)
374 unsigned int free_swap
= count_swap_pages(root_swap
, 1);
376 pr_debug("swsusp: free swap pages: %u\n", free_swap
);
377 return free_swap
> nr_pages
+ PAGES_FOR_IO
;
381 * swsusp_write - Write entire image and metadata.
383 * It is important _NOT_ to umount filesystems at this point. We want
384 * them synced (in case something goes wrong) but we DO not want to mark
385 * filesystem clean: it is not. (And it does not matter, if we resume
386 * correctly, we'll mark system clean, anyway.)
389 int swsusp_write(void)
391 struct swap_map_handle handle
;
392 struct snapshot_handle snapshot
;
393 struct swsusp_info
*header
;
396 error
= swsusp_swap_check();
398 printk(KERN_ERR
"swsusp: Cannot find swap device, try "
402 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
403 error
= snapshot_read_next(&snapshot
, PAGE_SIZE
);
404 if (error
< PAGE_SIZE
) {
410 header
= (struct swsusp_info
*)data_of(snapshot
);
411 if (!enough_swap(header
->pages
)) {
412 printk(KERN_ERR
"swsusp: Not enough free swap\n");
416 error
= get_swap_writer(&handle
);
418 sector_t start
= handle
.cur_swap
;
420 error
= swap_write_page(&handle
, header
, NULL
);
422 error
= save_image(&handle
, &snapshot
,
426 flush_swap_writer(&handle
);
428 error
= mark_swapfiles(start
);
433 free_all_swap_pages(root_swap
, handle
.bitmap
);
434 release_swap_writer(&handle
);
441 * The following functions allow us to read data using a swap map
442 * in a file-alike way
445 static void release_swap_reader(struct swap_map_handle
*handle
)
448 free_page((unsigned long)handle
->cur
);
452 static int get_swap_reader(struct swap_map_handle
*handle
, sector_t start
)
459 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(__GFP_WAIT
| __GFP_HIGH
);
463 error
= bio_read_page(start
, handle
->cur
, NULL
);
465 release_swap_reader(handle
);
472 static int swap_read_page(struct swap_map_handle
*handle
, void *buf
,
473 struct bio
**bio_chain
)
480 offset
= handle
->cur
->entries
[handle
->k
];
483 error
= bio_read_page(offset
, buf
, bio_chain
);
486 if (++handle
->k
>= MAP_PAGE_ENTRIES
) {
487 error
= wait_on_bio_chain(bio_chain
);
489 offset
= handle
->cur
->next_swap
;
491 release_swap_reader(handle
);
493 error
= bio_read_page(offset
, handle
->cur
, NULL
);
499 * load_image - load the image using the swap map handle
500 * @handle and the snapshot handle @snapshot
501 * (assume there are @nr_pages pages to load)
504 static int load_image(struct swap_map_handle
*handle
,
505 struct snapshot_handle
*snapshot
,
506 unsigned int nr_to_read
)
510 struct timeval start
;
516 printk("Loading image data pages (%u pages) ... ", nr_to_read
);
517 m
= nr_to_read
/ 100;
522 do_gettimeofday(&start
);
524 error
= snapshot_write_next(snapshot
, PAGE_SIZE
);
527 error
= swap_read_page(handle
, data_of(*snapshot
), &bio
);
530 if (snapshot
->sync_read
)
531 error
= wait_on_bio_chain(&bio
);
535 printk("\b\b\b\b%3d%%", nr_pages
/ m
);
538 err2
= wait_on_bio_chain(&bio
);
539 do_gettimeofday(&stop
);
543 printk("\b\b\b\bdone\n");
544 snapshot_write_finalize(snapshot
);
545 if (!snapshot_image_loaded(snapshot
))
548 swsusp_show_speed(&start
, &stop
, nr_to_read
, "Read");
552 int swsusp_read(void)
555 struct swap_map_handle handle
;
556 struct snapshot_handle snapshot
;
557 struct swsusp_info
*header
;
559 if (IS_ERR(resume_bdev
)) {
560 pr_debug("swsusp: block device not initialised\n");
561 return PTR_ERR(resume_bdev
);
564 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
565 error
= snapshot_write_next(&snapshot
, PAGE_SIZE
);
566 if (error
< PAGE_SIZE
)
567 return error
< 0 ? error
: -EFAULT
;
568 header
= (struct swsusp_info
*)data_of(snapshot
);
569 error
= get_swap_reader(&handle
, swsusp_header
->image
);
571 error
= swap_read_page(&handle
, header
, NULL
);
573 error
= load_image(&handle
, &snapshot
, header
->pages
- 1);
574 release_swap_reader(&handle
);
576 blkdev_put(resume_bdev
);
579 pr_debug("swsusp: Reading resume file was successful\n");
581 pr_debug("swsusp: Error %d resuming\n", error
);
586 * swsusp_check - Check for swsusp signature in the resume device
589 int swsusp_check(void)
593 resume_bdev
= open_by_devnum(swsusp_resume_device
, FMODE_READ
);
594 if (!IS_ERR(resume_bdev
)) {
595 set_blocksize(resume_bdev
, PAGE_SIZE
);
596 memset(swsusp_header
, 0, sizeof(PAGE_SIZE
));
597 error
= bio_read_page(swsusp_resume_block
,
598 swsusp_header
, NULL
);
602 if (!memcmp(SWSUSP_SIG
, swsusp_header
->sig
, 10)) {
603 memcpy(swsusp_header
->sig
, swsusp_header
->orig_sig
, 10);
604 /* Reset swap signature now */
605 error
= bio_write_page(swsusp_resume_block
,
606 swsusp_header
, NULL
);
611 blkdev_put(resume_bdev
);
613 pr_debug("swsusp: Signature found, resuming\n");
615 error
= PTR_ERR(resume_bdev
);
619 pr_debug("swsusp: Error %d check for resume file\n", error
);
625 * swsusp_close - close swap device.
628 void swsusp_close(void)
630 if (IS_ERR(resume_bdev
)) {
631 pr_debug("swsusp: block device not initialised\n");
635 blkdev_put(resume_bdev
);
638 static int swsusp_header_init(void)
640 swsusp_header
= (struct swsusp_header
*) __get_free_page(GFP_KERNEL
);
642 panic("Could not allocate memory for swsusp_header\n");
646 core_initcall(swsusp_header_init
);