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/version.h>
18 #include <linux/delay.h>
19 #include <linux/bitops.h>
20 #include <linux/genhd.h>
21 #include <linux/device.h>
22 #include <linux/buffer_head.h>
23 #include <linux/bio.h>
24 #include <linux/blkdev.h>
25 #include <linux/swap.h>
26 #include <linux/swapops.h>
31 extern char resume_file[];
33 #define SWSUSP_SIG "S1SUSPEND"
35 struct swsusp_header {
36 char reserved[PAGE_SIZE - 20 - sizeof(sector_t)];
40 } __attribute__((packed));
42 static struct swsusp_header *swsusp_header;
48 static unsigned short root_swap = 0xffff;
49 static struct block_device *resume_bdev;
52 * submit - submit BIO request.
54 * @off physical offset of page.
55 * @page: page we're reading or writing.
56 * @bio_chain: list of pending biod (for async reading)
58 * Straight from the textbook - allocate and initialize the bio.
59 * If we're reading, make sure the page is marked as dirty.
60 * Then submit it and, if @bio_chain == NULL, wait.
62 static int submit(int rw, pgoff_t page_off, struct page *page,
63 struct bio **bio_chain)
67 bio = bio_alloc(__GFP_WAIT | __GFP_HIGH, 1);
70 bio->bi_sector = page_off * (PAGE_SIZE >> 9);
71 bio->bi_bdev = resume_bdev;
72 bio->bi_end_io = end_swap_bio_read;
74 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
75 printk("swsusp: ERROR: adding page to bio at %ld\n", page_off);
83 if (bio_chain == NULL) {
84 submit_bio(rw | (1 << BIO_RW_SYNC), 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(rw | (1 << BIO_RW_SYNC), 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)
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 error = bio_write_page(swsusp_resume_block,
152 swsusp_header, NULL);
154 printk(KERN_ERR "swsusp: Swap header not found!\n");
161 * swsusp_swap_check - check if the resume device is a swap device
162 * and get its index (if so)
165 static int swsusp_swap_check(void) /* This is called before saving image */
169 res = swap_type_of(swsusp_resume_device, swsusp_resume_block,
175 res = blkdev_get(resume_bdev, FMODE_WRITE, O_RDWR);
179 res = set_blocksize(resume_bdev, PAGE_SIZE);
181 blkdev_put(resume_bdev);
187 * write_page - Write one page to given swap location.
188 * @buf: Address we're writing.
189 * @offset: Offset of the swap page we're writing to.
190 * @bio_chain: Link the next write BIO here
193 static int write_page(void *buf, sector_t offset, struct bio **bio_chain)
201 src = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
203 memcpy(src, buf, PAGE_SIZE);
206 bio_chain = NULL; /* Go synchronous */
212 return bio_write_page(offset, src, bio_chain);
216 * The swap map is a data structure used for keeping track of each page
217 * written to a swap partition. It consists of many swap_map_page
218 * structures that contain each an array of MAP_PAGE_SIZE swap entries.
219 * These structures are stored on the swap and linked together with the
220 * help of the .next_swap member.
222 * The swap map is created during suspend. The swap map pages are
223 * allocated and populated one at a time, so we only need one memory
224 * page to set up the entire structure.
226 * During resume we also only need to use one swap_map_page structure
230 #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
232 struct swap_map_page {
233 sector_t entries[MAP_PAGE_ENTRIES];
238 * The swap_map_handle structure is used for handling swap in
242 struct swap_map_handle {
243 struct swap_map_page *cur;
248 static void release_swap_writer(struct swap_map_handle *handle)
251 free_page((unsigned long)handle->cur);
255 static int get_swap_writer(struct swap_map_handle *handle)
257 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
260 handle->cur_swap = alloc_swapdev_block(root_swap);
261 if (!handle->cur_swap) {
262 release_swap_writer(handle);
269 static int swap_write_page(struct swap_map_handle *handle, void *buf,
270 struct bio **bio_chain)
277 offset = alloc_swapdev_block(root_swap);
278 error = write_page(buf, offset, bio_chain);
281 handle->cur->entries[handle->k++] = offset;
282 if (handle->k >= MAP_PAGE_ENTRIES) {
283 error = wait_on_bio_chain(bio_chain);
286 offset = alloc_swapdev_block(root_swap);
289 handle->cur->next_swap = offset;
290 error = write_page(handle->cur, handle->cur_swap, NULL);
293 memset(handle->cur, 0, PAGE_SIZE);
294 handle->cur_swap = offset;
301 static int flush_swap_writer(struct swap_map_handle *handle)
303 if (handle->cur && handle->cur_swap)
304 return write_page(handle->cur, handle->cur_swap, NULL);
310 * save_image - save the suspend image data
313 static int save_image(struct swap_map_handle *handle,
314 struct snapshot_handle *snapshot,
315 unsigned int nr_to_write)
323 struct timeval start;
326 printk("Saving image data pages (%u pages) ... ", nr_to_write);
327 m = nr_to_write / 100;
332 do_gettimeofday(&start);
334 ret = snapshot_read_next(snapshot, PAGE_SIZE);
336 error = swap_write_page(handle, data_of(*snapshot),
341 printk("\b\b\b\b%3d%%", nr_pages / m);
345 err2 = wait_on_bio_chain(&bio);
346 do_gettimeofday(&stop);
350 printk("\b\b\b\bdone\n");
351 swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
356 * enough_swap - Make sure we have enough swap to save the image.
358 * Returns TRUE or FALSE after checking the total amount of swap
359 * space avaiable from the resume partition.
362 static int enough_swap(unsigned int nr_pages)
364 unsigned int free_swap = count_swap_pages(root_swap, 1);
366 pr_debug("swsusp: free swap pages: %u\n", free_swap);
367 return free_swap > nr_pages + PAGES_FOR_IO;
371 * swsusp_write - Write entire image and metadata.
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(void)
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 "swsusp: 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 "swsusp: 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);
418 error = mark_swapfiles(start);
423 free_all_swap_pages(root_swap);
425 release_swap_writer(&handle);
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("Loading image data pages (%u pages) ... ", nr_to_read);
508 m = nr_to_read / 100;
513 do_gettimeofday(&start);
515 error = snapshot_write_next(snapshot, PAGE_SIZE);
518 error = swap_read_page(handle, data_of(*snapshot), &bio);
521 if (snapshot->sync_read)
522 error = wait_on_bio_chain(&bio);
526 printk("\b\b\b\b%3d%%", nr_pages / m);
529 err2 = wait_on_bio_chain(&bio);
530 do_gettimeofday(&stop);
534 printk("\b\b\b\bdone\n");
535 snapshot_write_finalize(snapshot);
536 if (!snapshot_image_loaded(snapshot))
539 swsusp_show_speed(&start, &stop, nr_to_read, "Read");
543 int swsusp_read(void)
546 struct swap_map_handle handle;
547 struct snapshot_handle snapshot;
548 struct swsusp_info *header;
550 if (IS_ERR(resume_bdev)) {
551 pr_debug("swsusp: block device not initialised\n");
552 return PTR_ERR(resume_bdev);
555 memset(&snapshot, 0, sizeof(struct snapshot_handle));
556 error = snapshot_write_next(&snapshot, PAGE_SIZE);
557 if (error < PAGE_SIZE)
558 return error < 0 ? error : -EFAULT;
559 header = (struct swsusp_info *)data_of(snapshot);
560 error = get_swap_reader(&handle, swsusp_header->image);
562 error = swap_read_page(&handle, header, NULL);
564 error = load_image(&handle, &snapshot, header->pages - 1);
565 release_swap_reader(&handle);
567 blkdev_put(resume_bdev);
570 pr_debug("swsusp: Reading resume file was successful\n");
572 pr_debug("swsusp: Error %d resuming\n", error);
577 * swsusp_check - Check for swsusp signature in the resume device
580 int swsusp_check(void)
584 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
585 if (!IS_ERR(resume_bdev)) {
586 set_blocksize(resume_bdev, PAGE_SIZE);
587 memset(swsusp_header, 0, PAGE_SIZE);
588 error = bio_read_page(swsusp_resume_block,
589 swsusp_header, NULL);
593 if (!memcmp(SWSUSP_SIG, swsusp_header->sig, 10)) {
594 memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
595 /* Reset swap signature now */
596 error = bio_write_page(swsusp_resume_block,
597 swsusp_header, NULL);
602 blkdev_put(resume_bdev);
604 pr_debug("swsusp: Signature found, resuming\n");
606 error = PTR_ERR(resume_bdev);
610 pr_debug("swsusp: Error %d check for resume file\n", error);
616 * swsusp_close - close swap device.
619 void swsusp_close(void)
621 if (IS_ERR(resume_bdev)) {
622 pr_debug("swsusp: block device not initialised\n");
626 blkdev_put(resume_bdev);
629 static int swsusp_header_init(void)
631 swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
633 panic("Could not allocate memory for swsusp_header\n");
637 core_initcall(swsusp_header_init);