f133d4a6d817bacfc2219476d72be55fb5d8face
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 static struct swsusp_header
{
37 char reserved
[PAGE_SIZE
- 20 - sizeof(sector_t
)];
41 } __attribute__((packed
, aligned(PAGE_SIZE
))) swsusp_header
;
47 static unsigned short root_swap
= 0xffff;
48 static struct block_device
*resume_bdev
;
51 * submit - submit BIO request.
53 * @off physical offset of page.
54 * @page: page we're reading or writing.
55 * @bio_chain: list of pending biod (for async reading)
57 * Straight from the textbook - allocate and initialize the bio.
58 * If we're reading, make sure the page is marked as dirty.
59 * Then submit it and, if @bio_chain == NULL, wait.
61 static int submit(int rw
, pgoff_t page_off
, struct page
*page
,
62 struct bio
**bio_chain
)
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("swsusp: ERROR: adding page to bio at %ld\n", page_off
);
82 if (bio_chain
== NULL
) {
83 submit_bio(rw
| (1 << BIO_RW_SYNC
), bio
);
84 wait_on_page_locked(page
);
86 bio_set_pages_dirty(bio
);
90 get_page(page
); /* These pages are freed later */
91 bio
->bi_private
= *bio_chain
;
93 submit_bio(rw
| (1 << BIO_RW_SYNC
), bio
);
98 static int bio_read_page(pgoff_t page_off
, void *addr
, struct bio
**bio_chain
)
100 return submit(READ
, page_off
, virt_to_page(addr
), bio_chain
);
103 static int bio_write_page(pgoff_t page_off
, void *addr
, struct bio
**bio_chain
)
105 return submit(WRITE
, page_off
, virt_to_page(addr
), bio_chain
);
108 static int wait_on_bio_chain(struct bio
**bio_chain
)
111 struct bio
*next_bio
;
114 if (bio_chain
== NULL
)
123 next_bio
= bio
->bi_private
;
124 page
= bio
->bi_io_vec
[0].bv_page
;
125 wait_on_page_locked(page
);
126 if (!PageUptodate(page
) || PageError(page
))
140 static int mark_swapfiles(sector_t start
)
144 bio_read_page(swsusp_resume_block
, &swsusp_header
, NULL
);
145 if (!memcmp("SWAP-SPACE",swsusp_header
.sig
, 10) ||
146 !memcmp("SWAPSPACE2",swsusp_header
.sig
, 10)) {
147 memcpy(swsusp_header
.orig_sig
,swsusp_header
.sig
, 10);
148 memcpy(swsusp_header
.sig
,SWSUSP_SIG
, 10);
149 swsusp_header
.image
= start
;
150 error
= bio_write_page(swsusp_resume_block
,
151 &swsusp_header
, NULL
);
153 printk(KERN_ERR
"swsusp: Swap header not found!\n");
160 * swsusp_swap_check - check if the resume device is a swap device
161 * and get its index (if so)
164 static int swsusp_swap_check(void) /* This is called before saving image */
168 res
= swap_type_of(swsusp_resume_device
, swsusp_resume_block
);
173 resume_bdev
= open_by_devnum(swsusp_resume_device
, FMODE_WRITE
);
174 if (IS_ERR(resume_bdev
))
175 return PTR_ERR(resume_bdev
);
177 res
= set_blocksize(resume_bdev
, PAGE_SIZE
);
179 blkdev_put(resume_bdev
);
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
;
243 struct bitmap_page
*bitmap
;
247 static void release_swap_writer(struct swap_map_handle
*handle
)
250 free_page((unsigned long)handle
->cur
);
253 free_bitmap(handle
->bitmap
);
254 handle
->bitmap
= NULL
;
257 static int get_swap_writer(struct swap_map_handle
*handle
)
259 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(GFP_KERNEL
);
262 handle
->bitmap
= alloc_bitmap(count_swap_pages(root_swap
, 0));
263 if (!handle
->bitmap
) {
264 release_swap_writer(handle
);
267 handle
->cur_swap
= alloc_swapdev_block(root_swap
, handle
->bitmap
);
268 if (!handle
->cur_swap
) {
269 release_swap_writer(handle
);
276 static int swap_write_page(struct swap_map_handle
*handle
, void *buf
,
277 struct bio
**bio_chain
)
284 offset
= alloc_swapdev_block(root_swap
, handle
->bitmap
);
285 error
= write_page(buf
, offset
, bio_chain
);
288 handle
->cur
->entries
[handle
->k
++] = offset
;
289 if (handle
->k
>= MAP_PAGE_ENTRIES
) {
290 error
= wait_on_bio_chain(bio_chain
);
293 offset
= alloc_swapdev_block(root_swap
, handle
->bitmap
);
296 handle
->cur
->next_swap
= offset
;
297 error
= write_page(handle
->cur
, handle
->cur_swap
, NULL
);
300 memset(handle
->cur
, 0, PAGE_SIZE
);
301 handle
->cur_swap
= offset
;
308 static int flush_swap_writer(struct swap_map_handle
*handle
)
310 if (handle
->cur
&& handle
->cur_swap
)
311 return write_page(handle
->cur
, handle
->cur_swap
, NULL
);
317 * save_image - save the suspend image data
320 static int save_image(struct swap_map_handle
*handle
,
321 struct snapshot_handle
*snapshot
,
322 unsigned int nr_to_write
)
330 struct timeval start
;
333 printk("Saving image data pages (%u pages) ... ", nr_to_write
);
334 m
= nr_to_write
/ 100;
339 do_gettimeofday(&start
);
341 ret
= snapshot_read_next(snapshot
, PAGE_SIZE
);
343 error
= swap_write_page(handle
, data_of(*snapshot
),
348 printk("\b\b\b\b%3d%%", nr_pages
/ m
);
352 err2
= wait_on_bio_chain(&bio
);
353 do_gettimeofday(&stop
);
357 printk("\b\b\b\bdone\n");
358 swsusp_show_speed(&start
, &stop
, nr_to_write
, "Wrote");
363 * enough_swap - Make sure we have enough swap to save the image.
365 * Returns TRUE or FALSE after checking the total amount of swap
366 * space avaiable from the resume partition.
369 static int enough_swap(unsigned int nr_pages
)
371 unsigned int free_swap
= count_swap_pages(root_swap
, 1);
373 pr_debug("swsusp: free swap pages: %u\n", free_swap
);
374 return free_swap
> nr_pages
+ PAGES_FOR_IO
;
378 * swsusp_write - Write entire image and metadata.
380 * It is important _NOT_ to umount filesystems at this point. We want
381 * them synced (in case something goes wrong) but we DO not want to mark
382 * filesystem clean: it is not. (And it does not matter, if we resume
383 * correctly, we'll mark system clean, anyway.)
386 int swsusp_write(void)
388 struct swap_map_handle handle
;
389 struct snapshot_handle snapshot
;
390 struct swsusp_info
*header
;
393 error
= swsusp_swap_check();
395 printk(KERN_ERR
"swsusp: Cannot find swap device, try "
399 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
400 error
= snapshot_read_next(&snapshot
, PAGE_SIZE
);
401 if (error
< PAGE_SIZE
) {
407 header
= (struct swsusp_info
*)data_of(snapshot
);
408 if (!enough_swap(header
->pages
)) {
409 printk(KERN_ERR
"swsusp: Not enough free swap\n");
413 error
= get_swap_writer(&handle
);
415 sector_t start
= handle
.cur_swap
;
417 error
= swap_write_page(&handle
, header
, NULL
);
419 error
= save_image(&handle
, &snapshot
,
423 flush_swap_writer(&handle
);
425 error
= mark_swapfiles(start
);
430 free_all_swap_pages(root_swap
, handle
.bitmap
);
431 release_swap_writer(&handle
);
438 * The following functions allow us to read data using a swap map
439 * in a file-alike way
442 static void release_swap_reader(struct swap_map_handle
*handle
)
445 free_page((unsigned long)handle
->cur
);
449 static int get_swap_reader(struct swap_map_handle
*handle
, sector_t start
)
456 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(__GFP_WAIT
| __GFP_HIGH
);
460 error
= bio_read_page(start
, handle
->cur
, NULL
);
462 release_swap_reader(handle
);
469 static int swap_read_page(struct swap_map_handle
*handle
, void *buf
,
470 struct bio
**bio_chain
)
477 offset
= handle
->cur
->entries
[handle
->k
];
480 error
= bio_read_page(offset
, buf
, bio_chain
);
483 if (++handle
->k
>= MAP_PAGE_ENTRIES
) {
484 error
= wait_on_bio_chain(bio_chain
);
486 offset
= handle
->cur
->next_swap
;
488 release_swap_reader(handle
);
490 error
= bio_read_page(offset
, handle
->cur
, NULL
);
496 * load_image - load the image using the swap map handle
497 * @handle and the snapshot handle @snapshot
498 * (assume there are @nr_pages pages to load)
501 static int load_image(struct swap_map_handle
*handle
,
502 struct snapshot_handle
*snapshot
,
503 unsigned int nr_to_read
)
507 struct timeval start
;
513 printk("Loading image data pages (%u pages) ... ", nr_to_read
);
514 m
= nr_to_read
/ 100;
519 do_gettimeofday(&start
);
521 error
= snapshot_write_next(snapshot
, PAGE_SIZE
);
524 error
= swap_read_page(handle
, data_of(*snapshot
), &bio
);
527 if (snapshot
->sync_read
)
528 error
= wait_on_bio_chain(&bio
);
532 printk("\b\b\b\b%3d%%", nr_pages
/ m
);
535 err2
= wait_on_bio_chain(&bio
);
536 do_gettimeofday(&stop
);
540 printk("\b\b\b\bdone\n");
541 snapshot_write_finalize(snapshot
);
542 if (!snapshot_image_loaded(snapshot
))
545 swsusp_show_speed(&start
, &stop
, nr_to_read
, "Read");
549 int swsusp_read(void)
552 struct swap_map_handle handle
;
553 struct snapshot_handle snapshot
;
554 struct swsusp_info
*header
;
556 if (IS_ERR(resume_bdev
)) {
557 pr_debug("swsusp: block device not initialised\n");
558 return PTR_ERR(resume_bdev
);
561 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
562 error
= snapshot_write_next(&snapshot
, PAGE_SIZE
);
563 if (error
< PAGE_SIZE
)
564 return error
< 0 ? error
: -EFAULT
;
565 header
= (struct swsusp_info
*)data_of(snapshot
);
566 error
= get_swap_reader(&handle
, swsusp_header
.image
);
568 error
= swap_read_page(&handle
, header
, NULL
);
570 error
= load_image(&handle
, &snapshot
, header
->pages
- 1);
571 release_swap_reader(&handle
);
573 blkdev_put(resume_bdev
);
576 pr_debug("swsusp: Reading resume file was successful\n");
578 pr_debug("swsusp: Error %d resuming\n", error
);
583 * swsusp_check - Check for swsusp signature in the resume device
586 int swsusp_check(void)
590 resume_bdev
= open_by_devnum(swsusp_resume_device
, FMODE_READ
);
591 if (!IS_ERR(resume_bdev
)) {
592 set_blocksize(resume_bdev
, PAGE_SIZE
);
593 memset(&swsusp_header
, 0, sizeof(swsusp_header
));
594 error
= bio_read_page(swsusp_resume_block
,
595 &swsusp_header
, NULL
);
599 if (!memcmp(SWSUSP_SIG
, swsusp_header
.sig
, 10)) {
600 memcpy(swsusp_header
.sig
, swsusp_header
.orig_sig
, 10);
601 /* Reset swap signature now */
602 error
= bio_write_page(swsusp_resume_block
,
603 &swsusp_header
, NULL
);
608 blkdev_put(resume_bdev
);
610 pr_debug("swsusp: Signature found, resuming\n");
612 error
= PTR_ERR(resume_bdev
);
616 pr_debug("swsusp: Error %d check for resume file\n", error
);
622 * swsusp_close - close swap device.
625 void swsusp_close(void)
627 if (IS_ERR(resume_bdev
)) {
628 pr_debug("swsusp: block device not initialised\n");
632 blkdev_put(resume_bdev
);
This page took 0.04511 seconds and 4 git commands to generate.