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
,
174 res
= blkdev_get(resume_bdev
, FMODE_WRITE
, O_RDWR
);
178 res
= set_blocksize(resume_bdev
, PAGE_SIZE
);
180 blkdev_put(resume_bdev
);
186 * write_page - Write one page to given swap location.
187 * @buf: Address we're writing.
188 * @offset: Offset of the swap page we're writing to.
189 * @bio_chain: Link the next write BIO here
192 static int write_page(void *buf
, sector_t offset
, struct bio
**bio_chain
)
200 src
= (void *)__get_free_page(__GFP_WAIT
| __GFP_HIGH
);
202 memcpy(src
, buf
, PAGE_SIZE
);
205 bio_chain
= NULL
; /* Go synchronous */
211 return bio_write_page(offset
, src
, bio_chain
);
215 * The swap map is a data structure used for keeping track of each page
216 * written to a swap partition. It consists of many swap_map_page
217 * structures that contain each an array of MAP_PAGE_SIZE swap entries.
218 * These structures are stored on the swap and linked together with the
219 * help of the .next_swap member.
221 * The swap map is created during suspend. The swap map pages are
222 * allocated and populated one at a time, so we only need one memory
223 * page to set up the entire structure.
225 * During resume we also only need to use one swap_map_page structure
229 #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
231 struct swap_map_page
{
232 sector_t entries
[MAP_PAGE_ENTRIES
];
237 * The swap_map_handle structure is used for handling swap in
241 struct swap_map_handle
{
242 struct swap_map_page
*cur
;
244 struct bitmap_page
*bitmap
;
248 static void release_swap_writer(struct swap_map_handle
*handle
)
251 free_page((unsigned long)handle
->cur
);
254 free_bitmap(handle
->bitmap
);
255 handle
->bitmap
= NULL
;
258 static int get_swap_writer(struct swap_map_handle
*handle
)
260 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(GFP_KERNEL
);
263 handle
->bitmap
= alloc_bitmap(count_swap_pages(root_swap
, 0));
264 if (!handle
->bitmap
) {
265 release_swap_writer(handle
);
268 handle
->cur_swap
= alloc_swapdev_block(root_swap
, handle
->bitmap
);
269 if (!handle
->cur_swap
) {
270 release_swap_writer(handle
);
277 static int swap_write_page(struct swap_map_handle
*handle
, void *buf
,
278 struct bio
**bio_chain
)
285 offset
= alloc_swapdev_block(root_swap
, handle
->bitmap
);
286 error
= write_page(buf
, offset
, bio_chain
);
289 handle
->cur
->entries
[handle
->k
++] = offset
;
290 if (handle
->k
>= MAP_PAGE_ENTRIES
) {
291 error
= wait_on_bio_chain(bio_chain
);
294 offset
= alloc_swapdev_block(root_swap
, handle
->bitmap
);
297 handle
->cur
->next_swap
= offset
;
298 error
= write_page(handle
->cur
, handle
->cur_swap
, NULL
);
301 memset(handle
->cur
, 0, PAGE_SIZE
);
302 handle
->cur_swap
= offset
;
309 static int flush_swap_writer(struct swap_map_handle
*handle
)
311 if (handle
->cur
&& handle
->cur_swap
)
312 return write_page(handle
->cur
, handle
->cur_swap
, NULL
);
318 * save_image - save the suspend image data
321 static int save_image(struct swap_map_handle
*handle
,
322 struct snapshot_handle
*snapshot
,
323 unsigned int nr_to_write
)
331 struct timeval start
;
334 printk("Saving image data pages (%u pages) ... ", nr_to_write
);
335 m
= nr_to_write
/ 100;
340 do_gettimeofday(&start
);
342 ret
= snapshot_read_next(snapshot
, PAGE_SIZE
);
344 error
= swap_write_page(handle
, data_of(*snapshot
),
349 printk("\b\b\b\b%3d%%", nr_pages
/ m
);
353 err2
= wait_on_bio_chain(&bio
);
354 do_gettimeofday(&stop
);
358 printk("\b\b\b\bdone\n");
359 swsusp_show_speed(&start
, &stop
, nr_to_write
, "Wrote");
364 * enough_swap - Make sure we have enough swap to save the image.
366 * Returns TRUE or FALSE after checking the total amount of swap
367 * space avaiable from the resume partition.
370 static int enough_swap(unsigned int nr_pages
)
372 unsigned int free_swap
= count_swap_pages(root_swap
, 1);
374 pr_debug("swsusp: free swap pages: %u\n", free_swap
);
375 return free_swap
> nr_pages
+ PAGES_FOR_IO
;
379 * swsusp_write - Write entire image and metadata.
381 * It is important _NOT_ to umount filesystems at this point. We want
382 * them synced (in case something goes wrong) but we DO not want to mark
383 * filesystem clean: it is not. (And it does not matter, if we resume
384 * correctly, we'll mark system clean, anyway.)
387 int swsusp_write(void)
389 struct swap_map_handle handle
;
390 struct snapshot_handle snapshot
;
391 struct swsusp_info
*header
;
394 error
= swsusp_swap_check();
396 printk(KERN_ERR
"swsusp: Cannot find swap device, try "
400 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
401 error
= snapshot_read_next(&snapshot
, PAGE_SIZE
);
402 if (error
< PAGE_SIZE
) {
408 header
= (struct swsusp_info
*)data_of(snapshot
);
409 if (!enough_swap(header
->pages
)) {
410 printk(KERN_ERR
"swsusp: Not enough free swap\n");
414 error
= get_swap_writer(&handle
);
416 sector_t start
= handle
.cur_swap
;
418 error
= swap_write_page(&handle
, header
, NULL
);
420 error
= save_image(&handle
, &snapshot
,
424 flush_swap_writer(&handle
);
426 error
= mark_swapfiles(start
);
431 free_all_swap_pages(root_swap
, handle
.bitmap
);
432 release_swap_writer(&handle
);
439 * The following functions allow us to read data using a swap map
440 * in a file-alike way
443 static void release_swap_reader(struct swap_map_handle
*handle
)
446 free_page((unsigned long)handle
->cur
);
450 static int get_swap_reader(struct swap_map_handle
*handle
, sector_t start
)
457 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(__GFP_WAIT
| __GFP_HIGH
);
461 error
= bio_read_page(start
, handle
->cur
, NULL
);
463 release_swap_reader(handle
);
470 static int swap_read_page(struct swap_map_handle
*handle
, void *buf
,
471 struct bio
**bio_chain
)
478 offset
= handle
->cur
->entries
[handle
->k
];
481 error
= bio_read_page(offset
, buf
, bio_chain
);
484 if (++handle
->k
>= MAP_PAGE_ENTRIES
) {
485 error
= wait_on_bio_chain(bio_chain
);
487 offset
= handle
->cur
->next_swap
;
489 release_swap_reader(handle
);
491 error
= bio_read_page(offset
, handle
->cur
, NULL
);
497 * load_image - load the image using the swap map handle
498 * @handle and the snapshot handle @snapshot
499 * (assume there are @nr_pages pages to load)
502 static int load_image(struct swap_map_handle
*handle
,
503 struct snapshot_handle
*snapshot
,
504 unsigned int nr_to_read
)
508 struct timeval start
;
514 printk("Loading image data pages (%u pages) ... ", nr_to_read
);
515 m
= nr_to_read
/ 100;
520 do_gettimeofday(&start
);
522 error
= snapshot_write_next(snapshot
, PAGE_SIZE
);
525 error
= swap_read_page(handle
, data_of(*snapshot
), &bio
);
528 if (snapshot
->sync_read
)
529 error
= wait_on_bio_chain(&bio
);
533 printk("\b\b\b\b%3d%%", nr_pages
/ m
);
536 err2
= wait_on_bio_chain(&bio
);
537 do_gettimeofday(&stop
);
541 printk("\b\b\b\bdone\n");
542 snapshot_write_finalize(snapshot
);
543 if (!snapshot_image_loaded(snapshot
))
546 swsusp_show_speed(&start
, &stop
, nr_to_read
, "Read");
550 int swsusp_read(void)
553 struct swap_map_handle handle
;
554 struct snapshot_handle snapshot
;
555 struct swsusp_info
*header
;
557 if (IS_ERR(resume_bdev
)) {
558 pr_debug("swsusp: block device not initialised\n");
559 return PTR_ERR(resume_bdev
);
562 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
563 error
= snapshot_write_next(&snapshot
, PAGE_SIZE
);
564 if (error
< PAGE_SIZE
)
565 return error
< 0 ? error
: -EFAULT
;
566 header
= (struct swsusp_info
*)data_of(snapshot
);
567 error
= get_swap_reader(&handle
, swsusp_header
.image
);
569 error
= swap_read_page(&handle
, header
, NULL
);
571 error
= load_image(&handle
, &snapshot
, header
->pages
- 1);
572 release_swap_reader(&handle
);
574 blkdev_put(resume_bdev
);
577 pr_debug("swsusp: Reading resume file was successful\n");
579 pr_debug("swsusp: Error %d resuming\n", error
);
584 * swsusp_check - Check for swsusp signature in the resume device
587 int swsusp_check(void)
591 resume_bdev
= open_by_devnum(swsusp_resume_device
, FMODE_READ
);
592 if (!IS_ERR(resume_bdev
)) {
593 set_blocksize(resume_bdev
, PAGE_SIZE
);
594 memset(&swsusp_header
, 0, sizeof(swsusp_header
));
595 error
= bio_read_page(swsusp_resume_block
,
596 &swsusp_header
, NULL
);
600 if (!memcmp(SWSUSP_SIG
, swsusp_header
.sig
, 10)) {
601 memcpy(swsusp_header
.sig
, swsusp_header
.orig_sig
, 10);
602 /* Reset swap signature now */
603 error
= bio_write_page(swsusp_resume_block
,
604 &swsusp_header
, NULL
);
609 blkdev_put(resume_bdev
);
611 pr_debug("swsusp: Signature found, resuming\n");
613 error
= PTR_ERR(resume_bdev
);
617 pr_debug("swsusp: Error %d check for resume file\n", error
);
623 * swsusp_close - close swap device.
626 void swsusp_close(void)
628 if (IS_ERR(resume_bdev
)) {
629 pr_debug("swsusp: block device not initialised\n");
633 blkdev_put(resume_bdev
);