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/utsname.h>
17 #include <linux/delay.h>
18 #include <linux/bitops.h>
19 #include <linux/genhd.h>
20 #include <linux/device.h>
21 #include <linux/buffer_head.h>
22 #include <linux/bio.h>
23 #include <linux/blkdev.h>
24 #include <linux/swap.h>
25 #include <linux/swapops.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
;
46 static unsigned short root_swap
= 0xffff;
47 static struct block_device
*resume_bdev
;
50 * submit - submit BIO request.
52 * @off physical offset of page.
53 * @page: page we're reading or writing.
54 * @bio_chain: list of pending biod (for async reading)
56 * Straight from the textbook - allocate and initialize the bio.
57 * If we're reading, make sure the page is marked as dirty.
58 * Then submit it and, if @bio_chain == NULL, wait.
60 static int submit(int rw
, pgoff_t page_off
, struct page
*page
,
61 struct bio
**bio_chain
)
63 const int bio_rw
= rw
| (1 << BIO_RW_SYNCIO
) | (1 << BIO_RW_UNPLUG
);
66 bio
= bio_alloc(__GFP_WAIT
| __GFP_HIGH
, 1);
69 bio
->bi_sector
= page_off
* (PAGE_SIZE
>> 9);
70 bio
->bi_bdev
= resume_bdev
;
71 bio
->bi_end_io
= end_swap_bio_read
;
73 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
74 printk(KERN_ERR
"PM: Adding page to bio failed at %ld\n",
83 if (bio_chain
== NULL
) {
84 submit_bio(bio_rw
, bio
);
85 wait_on_page_locked(page
);
87 bio_set_pages_dirty(bio
);
91 get_page(page
); /* These pages are freed later */
92 bio
->bi_private
= *bio_chain
;
94 submit_bio(bio_rw
, bio
);
99 static int bio_read_page(pgoff_t page_off
, void *addr
, struct bio
**bio_chain
)
101 return submit(READ
, page_off
, virt_to_page(addr
), bio_chain
);
104 static int bio_write_page(pgoff_t page_off
, void *addr
, struct bio
**bio_chain
)
106 return submit(WRITE
, page_off
, virt_to_page(addr
), bio_chain
);
109 static int wait_on_bio_chain(struct bio
**bio_chain
)
112 struct bio
*next_bio
;
115 if (bio_chain
== NULL
)
124 next_bio
= bio
->bi_private
;
125 page
= bio
->bi_io_vec
[0].bv_page
;
126 wait_on_page_locked(page
);
127 if (!PageUptodate(page
) || PageError(page
))
141 static int mark_swapfiles(sector_t start
, unsigned int flags
)
145 bio_read_page(swsusp_resume_block
, swsusp_header
, NULL
);
146 if (!memcmp("SWAP-SPACE",swsusp_header
->sig
, 10) ||
147 !memcmp("SWAPSPACE2",swsusp_header
->sig
, 10)) {
148 memcpy(swsusp_header
->orig_sig
,swsusp_header
->sig
, 10);
149 memcpy(swsusp_header
->sig
,SWSUSP_SIG
, 10);
150 swsusp_header
->image
= start
;
151 swsusp_header
->flags
= flags
;
152 error
= bio_write_page(swsusp_resume_block
,
153 swsusp_header
, NULL
);
155 printk(KERN_ERR
"PM: 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
);
180 res
= set_blocksize(resume_bdev
, PAGE_SIZE
);
182 blkdev_put(resume_bdev
, FMODE_WRITE
);
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
;
249 static void release_swap_writer(struct swap_map_handle
*handle
)
252 free_page((unsigned long)handle
->cur
);
256 static int get_swap_writer(struct swap_map_handle
*handle
)
258 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(GFP_KERNEL
);
261 handle
->cur_swap
= alloc_swapdev_block(root_swap
);
262 if (!handle
->cur_swap
) {
263 release_swap_writer(handle
);
270 static int swap_write_page(struct swap_map_handle
*handle
, void *buf
,
271 struct bio
**bio_chain
)
278 offset
= alloc_swapdev_block(root_swap
);
279 error
= write_page(buf
, offset
, bio_chain
);
282 handle
->cur
->entries
[handle
->k
++] = offset
;
283 if (handle
->k
>= MAP_PAGE_ENTRIES
) {
284 error
= wait_on_bio_chain(bio_chain
);
287 offset
= alloc_swapdev_block(root_swap
);
290 handle
->cur
->next_swap
= offset
;
291 error
= write_page(handle
->cur
, handle
->cur_swap
, NULL
);
294 memset(handle
->cur
, 0, PAGE_SIZE
);
295 handle
->cur_swap
= offset
;
302 static int flush_swap_writer(struct swap_map_handle
*handle
)
304 if (handle
->cur
&& handle
->cur_swap
)
305 return write_page(handle
->cur
, handle
->cur_swap
, NULL
);
311 * save_image - save the suspend image data
314 static int save_image(struct swap_map_handle
*handle
,
315 struct snapshot_handle
*snapshot
,
316 unsigned int nr_to_write
)
324 struct timeval start
;
327 printk(KERN_INFO
"PM: Saving image data pages (%u pages) ... ",
329 m
= nr_to_write
/ 100;
334 do_gettimeofday(&start
);
336 ret
= snapshot_read_next(snapshot
, PAGE_SIZE
);
338 error
= swap_write_page(handle
, data_of(*snapshot
),
343 printk("\b\b\b\b%3d%%", nr_pages
/ m
);
347 err2
= wait_on_bio_chain(&bio
);
348 do_gettimeofday(&stop
);
352 printk("\b\b\b\bdone\n");
353 swsusp_show_speed(&start
, &stop
, nr_to_write
, "Wrote");
358 * enough_swap - Make sure we have enough swap to save the image.
360 * Returns TRUE or FALSE after checking the total amount of swap
361 * space avaiable from the resume partition.
364 static int enough_swap(unsigned int nr_pages
)
366 unsigned int free_swap
= count_swap_pages(root_swap
, 1);
368 pr_debug("PM: Free swap pages: %u\n", free_swap
);
369 return free_swap
> nr_pages
+ PAGES_FOR_IO
;
373 * swsusp_write - Write entire image and metadata.
374 * @flags: flags to pass to the "boot" kernel in the image header
376 * It is important _NOT_ to umount filesystems at this point. We want
377 * them synced (in case something goes wrong) but we DO not want to mark
378 * filesystem clean: it is not. (And it does not matter, if we resume
379 * correctly, we'll mark system clean, anyway.)
382 int swsusp_write(unsigned int flags
)
384 struct swap_map_handle handle
;
385 struct snapshot_handle snapshot
;
386 struct swsusp_info
*header
;
389 error
= swsusp_swap_check();
391 printk(KERN_ERR
"PM: Cannot find swap device, try "
395 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
396 error
= snapshot_read_next(&snapshot
, PAGE_SIZE
);
397 if (error
< PAGE_SIZE
) {
403 header
= (struct swsusp_info
*)data_of(snapshot
);
404 if (!enough_swap(header
->pages
)) {
405 printk(KERN_ERR
"PM: Not enough free swap\n");
409 error
= get_swap_writer(&handle
);
411 sector_t start
= handle
.cur_swap
;
413 error
= swap_write_page(&handle
, header
, NULL
);
415 error
= save_image(&handle
, &snapshot
,
419 flush_swap_writer(&handle
);
420 printk(KERN_INFO
"PM: S");
421 error
= mark_swapfiles(start
, flags
);
426 free_all_swap_pages(root_swap
);
428 release_swap_writer(&handle
);
430 swsusp_close(FMODE_WRITE
);
435 * The following functions allow us to read data using a swap map
436 * in a file-alike way
439 static void release_swap_reader(struct swap_map_handle
*handle
)
442 free_page((unsigned long)handle
->cur
);
446 static int get_swap_reader(struct swap_map_handle
*handle
, sector_t start
)
453 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(__GFP_WAIT
| __GFP_HIGH
);
457 error
= bio_read_page(start
, handle
->cur
, NULL
);
459 release_swap_reader(handle
);
466 static int swap_read_page(struct swap_map_handle
*handle
, void *buf
,
467 struct bio
**bio_chain
)
474 offset
= handle
->cur
->entries
[handle
->k
];
477 error
= bio_read_page(offset
, buf
, bio_chain
);
480 if (++handle
->k
>= MAP_PAGE_ENTRIES
) {
481 error
= wait_on_bio_chain(bio_chain
);
483 offset
= handle
->cur
->next_swap
;
485 release_swap_reader(handle
);
487 error
= bio_read_page(offset
, handle
->cur
, NULL
);
493 * load_image - load the image using the swap map handle
494 * @handle and the snapshot handle @snapshot
495 * (assume there are @nr_pages pages to load)
498 static int load_image(struct swap_map_handle
*handle
,
499 struct snapshot_handle
*snapshot
,
500 unsigned int nr_to_read
)
504 struct timeval start
;
510 printk(KERN_INFO
"PM: Loading image data pages (%u pages) ... ",
512 m
= nr_to_read
/ 100;
517 do_gettimeofday(&start
);
519 error
= snapshot_write_next(snapshot
, PAGE_SIZE
);
522 error
= swap_read_page(handle
, data_of(*snapshot
), &bio
);
525 if (snapshot
->sync_read
)
526 error
= wait_on_bio_chain(&bio
);
530 printk("\b\b\b\b%3d%%", nr_pages
/ m
);
533 err2
= wait_on_bio_chain(&bio
);
534 do_gettimeofday(&stop
);
538 printk("\b\b\b\bdone\n");
539 snapshot_write_finalize(snapshot
);
540 if (!snapshot_image_loaded(snapshot
))
543 swsusp_show_speed(&start
, &stop
, nr_to_read
, "Read");
548 * swsusp_read - read the hibernation image.
549 * @flags_p: flags passed by the "frozen" kernel in the image header should
550 * be written into this memeory location
553 int swsusp_read(unsigned int *flags_p
)
556 struct swap_map_handle handle
;
557 struct snapshot_handle snapshot
;
558 struct swsusp_info
*header
;
560 *flags_p
= swsusp_header
->flags
;
561 if (IS_ERR(resume_bdev
)) {
562 pr_debug("PM: Image device not initialised\n");
563 return PTR_ERR(resume_bdev
);
566 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
567 error
= snapshot_write_next(&snapshot
, PAGE_SIZE
);
568 if (error
< PAGE_SIZE
)
569 return error
< 0 ? error
: -EFAULT
;
570 header
= (struct swsusp_info
*)data_of(snapshot
);
571 error
= get_swap_reader(&handle
, swsusp_header
->image
);
573 error
= swap_read_page(&handle
, header
, NULL
);
575 error
= load_image(&handle
, &snapshot
, header
->pages
- 1);
576 release_swap_reader(&handle
);
578 blkdev_put(resume_bdev
, FMODE_READ
);
581 pr_debug("PM: Image successfully loaded\n");
583 pr_debug("PM: Error %d resuming\n", error
);
588 * swsusp_check - Check for swsusp signature in the resume device
591 int swsusp_check(void)
595 resume_bdev
= open_by_devnum(swsusp_resume_device
, FMODE_READ
);
596 if (!IS_ERR(resume_bdev
)) {
597 set_blocksize(resume_bdev
, PAGE_SIZE
);
598 memset(swsusp_header
, 0, PAGE_SIZE
);
599 error
= bio_read_page(swsusp_resume_block
,
600 swsusp_header
, NULL
);
604 if (!memcmp(SWSUSP_SIG
, swsusp_header
->sig
, 10)) {
605 memcpy(swsusp_header
->sig
, swsusp_header
->orig_sig
, 10);
606 /* Reset swap signature now */
607 error
= bio_write_page(swsusp_resume_block
,
608 swsusp_header
, NULL
);
613 blkdev_put(resume_bdev
, FMODE_READ
);
615 pr_debug("PM: Signature found, resuming\n");
617 error
= PTR_ERR(resume_bdev
);
621 pr_debug("PM: Error %d checking image file\n", error
);
627 * swsusp_close - close swap device.
630 void swsusp_close(fmode_t mode
)
632 if (IS_ERR(resume_bdev
)) {
633 pr_debug("PM: Image device not initialised\n");
637 blkdev_put(resume_bdev
, mode
);
640 static int swsusp_header_init(void)
642 swsusp_header
= (struct swsusp_header
*) __get_free_page(GFP_KERNEL
);
644 panic("Could not allocate memory for swsusp_header\n");
648 core_initcall(swsusp_header_init
);