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
;
45 static unsigned short root_swap
= 0xffff;
46 static struct block_device
*resume_bdev
;
49 * submit - submit BIO request.
51 * @off physical offset of page.
52 * @page: page we're reading or writing.
53 * @bio_chain: list of pending biod (for async reading)
55 * Straight from the textbook - allocate and initialize the bio.
56 * If we're reading, make sure the page is marked as dirty.
57 * Then submit it and, if @bio_chain == NULL, wait.
59 static int submit(int rw
, pgoff_t page_off
, struct page
*page
,
60 struct bio
**bio_chain
)
62 const int bio_rw
= rw
| (1 << BIO_RW_SYNCIO
) | (1 << BIO_RW_UNPLUG
);
65 bio
= bio_alloc(__GFP_WAIT
| __GFP_HIGH
, 1);
66 bio
->bi_sector
= page_off
* (PAGE_SIZE
>> 9);
67 bio
->bi_bdev
= resume_bdev
;
68 bio
->bi_end_io
= end_swap_bio_read
;
70 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
71 printk(KERN_ERR
"PM: Adding page to bio failed at %ld\n",
80 if (bio_chain
== NULL
) {
81 submit_bio(bio_rw
, bio
);
82 wait_on_page_locked(page
);
84 bio_set_pages_dirty(bio
);
88 get_page(page
); /* These pages are freed later */
89 bio
->bi_private
= *bio_chain
;
91 submit_bio(bio_rw
, bio
);
96 static int bio_read_page(pgoff_t page_off
, void *addr
, struct bio
**bio_chain
)
98 return submit(READ
, page_off
, virt_to_page(addr
), bio_chain
);
101 static int bio_write_page(pgoff_t page_off
, void *addr
, struct bio
**bio_chain
)
103 return submit(WRITE
, page_off
, virt_to_page(addr
), bio_chain
);
106 static int wait_on_bio_chain(struct bio
**bio_chain
)
109 struct bio
*next_bio
;
112 if (bio_chain
== NULL
)
121 next_bio
= bio
->bi_private
;
122 page
= bio
->bi_io_vec
[0].bv_page
;
123 wait_on_page_locked(page
);
124 if (!PageUptodate(page
) || PageError(page
))
138 static int mark_swapfiles(sector_t start
, unsigned int flags
)
142 bio_read_page(swsusp_resume_block
, swsusp_header
, NULL
);
143 if (!memcmp("SWAP-SPACE",swsusp_header
->sig
, 10) ||
144 !memcmp("SWAPSPACE2",swsusp_header
->sig
, 10)) {
145 memcpy(swsusp_header
->orig_sig
,swsusp_header
->sig
, 10);
146 memcpy(swsusp_header
->sig
,SWSUSP_SIG
, 10);
147 swsusp_header
->image
= start
;
148 swsusp_header
->flags
= flags
;
149 error
= bio_write_page(swsusp_resume_block
,
150 swsusp_header
, NULL
);
152 printk(KERN_ERR
"PM: Swap header not found!\n");
159 * swsusp_swap_check - check if the resume device is a swap device
160 * and get its index (if so)
163 static int swsusp_swap_check(void) /* This is called before saving image */
167 res
= swap_type_of(swsusp_resume_device
, swsusp_resume_block
,
173 res
= blkdev_get(resume_bdev
, FMODE_WRITE
);
177 res
= set_blocksize(resume_bdev
, PAGE_SIZE
);
179 blkdev_put(resume_bdev
, FMODE_WRITE
);
185 * write_page - Write one page to given swap location.
186 * @buf: Address we're writing.
187 * @offset: Offset of the swap page we're writing to.
188 * @bio_chain: Link the next write BIO here
191 static int write_page(void *buf
, sector_t offset
, struct bio
**bio_chain
)
199 src
= (void *)__get_free_page(__GFP_WAIT
| __GFP_HIGH
);
201 memcpy(src
, buf
, PAGE_SIZE
);
204 bio_chain
= NULL
; /* Go synchronous */
210 return bio_write_page(offset
, src
, bio_chain
);
214 * The swap map is a data structure used for keeping track of each page
215 * written to a swap partition. It consists of many swap_map_page
216 * structures that contain each an array of MAP_PAGE_SIZE swap entries.
217 * These structures are stored on the swap and linked together with the
218 * help of the .next_swap member.
220 * The swap map is created during suspend. The swap map pages are
221 * allocated and populated one at a time, so we only need one memory
222 * page to set up the entire structure.
224 * During resume we also only need to use one swap_map_page structure
228 #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
230 struct swap_map_page
{
231 sector_t entries
[MAP_PAGE_ENTRIES
];
236 * The swap_map_handle structure is used for handling swap in
240 struct swap_map_handle
{
241 struct swap_map_page
*cur
;
246 static void release_swap_writer(struct swap_map_handle
*handle
)
249 free_page((unsigned long)handle
->cur
);
253 static int get_swap_writer(struct swap_map_handle
*handle
)
255 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(GFP_KERNEL
);
258 handle
->cur_swap
= alloc_swapdev_block(root_swap
);
259 if (!handle
->cur_swap
) {
260 release_swap_writer(handle
);
267 static int swap_write_page(struct swap_map_handle
*handle
, void *buf
,
268 struct bio
**bio_chain
)
275 offset
= alloc_swapdev_block(root_swap
);
276 error
= write_page(buf
, offset
, bio_chain
);
279 handle
->cur
->entries
[handle
->k
++] = offset
;
280 if (handle
->k
>= MAP_PAGE_ENTRIES
) {
281 error
= wait_on_bio_chain(bio_chain
);
284 offset
= alloc_swapdev_block(root_swap
);
287 handle
->cur
->next_swap
= offset
;
288 error
= write_page(handle
->cur
, handle
->cur_swap
, NULL
);
291 memset(handle
->cur
, 0, PAGE_SIZE
);
292 handle
->cur_swap
= offset
;
299 static int flush_swap_writer(struct swap_map_handle
*handle
)
301 if (handle
->cur
&& handle
->cur_swap
)
302 return write_page(handle
->cur
, handle
->cur_swap
, NULL
);
308 * save_image - save the suspend image data
311 static int save_image(struct swap_map_handle
*handle
,
312 struct snapshot_handle
*snapshot
,
313 unsigned int nr_to_write
)
321 struct timeval start
;
324 printk(KERN_INFO
"PM: Saving image data pages (%u pages) ... ",
326 m
= nr_to_write
/ 100;
331 do_gettimeofday(&start
);
333 ret
= snapshot_read_next(snapshot
, PAGE_SIZE
);
335 error
= swap_write_page(handle
, data_of(*snapshot
),
340 printk("\b\b\b\b%3d%%", nr_pages
/ m
);
344 err2
= wait_on_bio_chain(&bio
);
345 do_gettimeofday(&stop
);
349 printk("\b\b\b\bdone\n");
350 swsusp_show_speed(&start
, &stop
, nr_to_write
, "Wrote");
355 * enough_swap - Make sure we have enough swap to save the image.
357 * Returns TRUE or FALSE after checking the total amount of swap
358 * space avaiable from the resume partition.
361 static int enough_swap(unsigned int nr_pages
)
363 unsigned int free_swap
= count_swap_pages(root_swap
, 1);
365 pr_debug("PM: Free swap pages: %u\n", free_swap
);
366 return free_swap
> nr_pages
+ PAGES_FOR_IO
;
370 * swsusp_write - Write entire image and metadata.
371 * @flags: flags to pass to the "boot" kernel in the image header
373 * It is important _NOT_ to umount filesystems at this point. We want
374 * them synced (in case something goes wrong) but we DO not want to mark
375 * filesystem clean: it is not. (And it does not matter, if we resume
376 * correctly, we'll mark system clean, anyway.)
379 int swsusp_write(unsigned int flags
)
381 struct swap_map_handle handle
;
382 struct snapshot_handle snapshot
;
383 struct swsusp_info
*header
;
386 error
= swsusp_swap_check();
388 printk(KERN_ERR
"PM: Cannot find swap device, try "
392 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
393 error
= snapshot_read_next(&snapshot
, PAGE_SIZE
);
394 if (error
< PAGE_SIZE
) {
400 header
= (struct swsusp_info
*)data_of(snapshot
);
401 if (!enough_swap(header
->pages
)) {
402 printk(KERN_ERR
"PM: Not enough free swap\n");
406 error
= get_swap_writer(&handle
);
408 sector_t start
= handle
.cur_swap
;
410 error
= swap_write_page(&handle
, header
, NULL
);
412 error
= save_image(&handle
, &snapshot
,
416 flush_swap_writer(&handle
);
417 printk(KERN_INFO
"PM: S");
418 error
= mark_swapfiles(start
, flags
);
423 free_all_swap_pages(root_swap
);
425 release_swap_writer(&handle
);
427 swsusp_close(FMODE_WRITE
);
432 * The following functions allow us to read data using a swap map
433 * in a file-alike way
436 static void release_swap_reader(struct swap_map_handle
*handle
)
439 free_page((unsigned long)handle
->cur
);
443 static int get_swap_reader(struct swap_map_handle
*handle
, sector_t start
)
450 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(__GFP_WAIT
| __GFP_HIGH
);
454 error
= bio_read_page(start
, handle
->cur
, NULL
);
456 release_swap_reader(handle
);
463 static int swap_read_page(struct swap_map_handle
*handle
, void *buf
,
464 struct bio
**bio_chain
)
471 offset
= handle
->cur
->entries
[handle
->k
];
474 error
= bio_read_page(offset
, buf
, bio_chain
);
477 if (++handle
->k
>= MAP_PAGE_ENTRIES
) {
478 error
= wait_on_bio_chain(bio_chain
);
480 offset
= handle
->cur
->next_swap
;
482 release_swap_reader(handle
);
484 error
= bio_read_page(offset
, handle
->cur
, NULL
);
490 * load_image - load the image using the swap map handle
491 * @handle and the snapshot handle @snapshot
492 * (assume there are @nr_pages pages to load)
495 static int load_image(struct swap_map_handle
*handle
,
496 struct snapshot_handle
*snapshot
,
497 unsigned int nr_to_read
)
501 struct timeval start
;
507 printk(KERN_INFO
"PM: Loading image data pages (%u pages) ... ",
509 m
= nr_to_read
/ 100;
514 do_gettimeofday(&start
);
516 error
= snapshot_write_next(snapshot
, PAGE_SIZE
);
519 error
= swap_read_page(handle
, data_of(*snapshot
), &bio
);
522 if (snapshot
->sync_read
)
523 error
= wait_on_bio_chain(&bio
);
527 printk("\b\b\b\b%3d%%", nr_pages
/ m
);
530 err2
= wait_on_bio_chain(&bio
);
531 do_gettimeofday(&stop
);
535 printk("\b\b\b\bdone\n");
536 snapshot_write_finalize(snapshot
);
537 if (!snapshot_image_loaded(snapshot
))
540 swsusp_show_speed(&start
, &stop
, nr_to_read
, "Read");
545 * swsusp_read - read the hibernation image.
546 * @flags_p: flags passed by the "frozen" kernel in the image header should
547 * be written into this memeory location
550 int swsusp_read(unsigned int *flags_p
)
553 struct swap_map_handle handle
;
554 struct snapshot_handle snapshot
;
555 struct swsusp_info
*header
;
557 *flags_p
= swsusp_header
->flags
;
558 if (IS_ERR(resume_bdev
)) {
559 pr_debug("PM: Image device not initialised\n");
560 return PTR_ERR(resume_bdev
);
563 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
564 error
= snapshot_write_next(&snapshot
, PAGE_SIZE
);
565 if (error
< PAGE_SIZE
)
566 return error
< 0 ? error
: -EFAULT
;
567 header
= (struct swsusp_info
*)data_of(snapshot
);
568 error
= get_swap_reader(&handle
, swsusp_header
->image
);
570 error
= swap_read_page(&handle
, header
, NULL
);
572 error
= load_image(&handle
, &snapshot
, header
->pages
- 1);
573 release_swap_reader(&handle
);
575 blkdev_put(resume_bdev
, FMODE_READ
);
578 pr_debug("PM: Image successfully loaded\n");
580 pr_debug("PM: Error %d resuming\n", error
);
585 * swsusp_check - Check for swsusp signature in the resume device
588 int swsusp_check(void)
592 resume_bdev
= open_by_devnum(swsusp_resume_device
, FMODE_READ
);
593 if (!IS_ERR(resume_bdev
)) {
594 set_blocksize(resume_bdev
, PAGE_SIZE
);
595 memset(swsusp_header
, 0, PAGE_SIZE
);
596 error
= bio_read_page(swsusp_resume_block
,
597 swsusp_header
, NULL
);
601 if (!memcmp(SWSUSP_SIG
, swsusp_header
->sig
, 10)) {
602 memcpy(swsusp_header
->sig
, swsusp_header
->orig_sig
, 10);
603 /* Reset swap signature now */
604 error
= bio_write_page(swsusp_resume_block
,
605 swsusp_header
, NULL
);
610 blkdev_put(resume_bdev
, FMODE_READ
);
612 pr_debug("PM: Signature found, resuming\n");
614 error
= PTR_ERR(resume_bdev
);
618 pr_debug("PM: Error %d checking image file\n", error
);
624 * swsusp_close - close swap device.
627 void swsusp_close(fmode_t mode
)
629 if (IS_ERR(resume_bdev
)) {
630 pr_debug("PM: Image device not initialised\n");
634 blkdev_put(resume_bdev
, mode
);
637 static int swsusp_header_init(void)
639 swsusp_header
= (struct swsusp_header
*) __get_free_page(GFP_KERNEL
);
641 panic("Could not allocate memory for swsusp_header\n");
645 core_initcall(swsusp_header_init
);