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_ATOMIC, 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))
136 static void show_speed(struct timeval *start, struct timeval *stop,
137 unsigned nr_pages, char *msg)
139 s64 elapsed_centisecs64;
144 elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
145 do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
146 centisecs = elapsed_centisecs64;
148 centisecs = 1; /* avoid div-by-zero */
149 k = nr_pages * (PAGE_SIZE / 1024);
150 kps = (k * 100) / centisecs;
151 printk("%s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n", msg, k,
152 centisecs / 100, centisecs % 100,
153 kps / 1000, (kps % 1000) / 10);
160 static int mark_swapfiles(sector_t start)
164 bio_read_page(0, &swsusp_header, NULL);
165 if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
166 !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
167 memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
168 memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
169 swsusp_header.image = start;
170 error = bio_write_page(0, &swsusp_header, NULL);
172 printk(KERN_ERR "swsusp: Swap header not found!\n");
179 * swsusp_swap_check - check if the resume device is a swap device
180 * and get its index (if so)
183 static int swsusp_swap_check(void) /* This is called before saving image */
187 res = swap_type_of(swsusp_resume_device, 0);
192 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_WRITE);
193 if (IS_ERR(resume_bdev))
194 return PTR_ERR(resume_bdev);
196 res = set_blocksize(resume_bdev, PAGE_SIZE);
198 blkdev_put(resume_bdev);
204 * write_page - Write one page to given swap location.
205 * @buf: Address we're writing.
206 * @offset: Offset of the swap page we're writing to.
207 * @bio_chain: Link the next write BIO here
210 static int write_page(void *buf, sector_t offset, struct bio **bio_chain)
218 src = (void *)__get_free_page(GFP_ATOMIC);
220 memcpy(src, buf, PAGE_SIZE);
223 bio_chain = NULL; /* Go synchronous */
229 return bio_write_page(offset, src, bio_chain);
233 * The swap map is a data structure used for keeping track of each page
234 * written to a swap partition. It consists of many swap_map_page
235 * structures that contain each an array of MAP_PAGE_SIZE swap entries.
236 * These structures are stored on the swap and linked together with the
237 * help of the .next_swap member.
239 * The swap map is created during suspend. The swap map pages are
240 * allocated and populated one at a time, so we only need one memory
241 * page to set up the entire structure.
243 * During resume we also only need to use one swap_map_page structure
247 #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
249 struct swap_map_page {
250 sector_t entries[MAP_PAGE_ENTRIES];
255 * The swap_map_handle structure is used for handling swap in
259 struct swap_map_handle {
260 struct swap_map_page *cur;
262 struct bitmap_page *bitmap;
266 static void release_swap_writer(struct swap_map_handle *handle)
269 free_page((unsigned long)handle->cur);
272 free_bitmap(handle->bitmap);
273 handle->bitmap = NULL;
276 static int get_swap_writer(struct swap_map_handle *handle)
278 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
281 handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0));
282 if (!handle->bitmap) {
283 release_swap_writer(handle);
286 handle->cur_swap = alloc_swapdev_block(root_swap, handle->bitmap);
287 if (!handle->cur_swap) {
288 release_swap_writer(handle);
295 static int swap_write_page(struct swap_map_handle *handle, void *buf,
296 struct bio **bio_chain)
303 offset = alloc_swapdev_block(root_swap, handle->bitmap);
304 error = write_page(buf, offset, bio_chain);
307 handle->cur->entries[handle->k++] = offset;
308 if (handle->k >= MAP_PAGE_ENTRIES) {
309 error = wait_on_bio_chain(bio_chain);
312 offset = alloc_swapdev_block(root_swap, handle->bitmap);
315 handle->cur->next_swap = offset;
316 error = write_page(handle->cur, handle->cur_swap, NULL);
319 memset(handle->cur, 0, PAGE_SIZE);
320 handle->cur_swap = offset;
327 static int flush_swap_writer(struct swap_map_handle *handle)
329 if (handle->cur && handle->cur_swap)
330 return write_page(handle->cur, handle->cur_swap, NULL);
336 * save_image - save the suspend image data
339 static int save_image(struct swap_map_handle *handle,
340 struct snapshot_handle *snapshot,
341 unsigned int nr_to_write)
349 struct timeval start;
352 printk("Saving image data pages (%u pages) ... ", nr_to_write);
353 m = nr_to_write / 100;
358 do_gettimeofday(&start);
360 ret = snapshot_read_next(snapshot, PAGE_SIZE);
362 error = swap_write_page(handle, data_of(*snapshot),
367 printk("\b\b\b\b%3d%%", nr_pages / m);
371 err2 = wait_on_bio_chain(&bio);
372 do_gettimeofday(&stop);
376 printk("\b\b\b\bdone\n");
377 show_speed(&start, &stop, nr_to_write, "Wrote");
382 * enough_swap - Make sure we have enough swap to save the image.
384 * Returns TRUE or FALSE after checking the total amount of swap
385 * space avaiable from the resume partition.
388 static int enough_swap(unsigned int nr_pages)
390 unsigned int free_swap = count_swap_pages(root_swap, 1);
392 pr_debug("swsusp: free swap pages: %u\n", free_swap);
393 return free_swap > nr_pages + PAGES_FOR_IO;
397 * swsusp_write - Write entire image and metadata.
399 * It is important _NOT_ to umount filesystems at this point. We want
400 * them synced (in case something goes wrong) but we DO not want to mark
401 * filesystem clean: it is not. (And it does not matter, if we resume
402 * correctly, we'll mark system clean, anyway.)
405 int swsusp_write(void)
407 struct swap_map_handle handle;
408 struct snapshot_handle snapshot;
409 struct swsusp_info *header;
412 error = swsusp_swap_check();
414 printk(KERN_ERR "swsusp: Cannot find swap device, try "
418 memset(&snapshot, 0, sizeof(struct snapshot_handle));
419 error = snapshot_read_next(&snapshot, PAGE_SIZE);
420 if (error < PAGE_SIZE) {
426 header = (struct swsusp_info *)data_of(snapshot);
427 if (!enough_swap(header->pages)) {
428 printk(KERN_ERR "swsusp: Not enough free swap\n");
432 error = get_swap_writer(&handle);
434 sector_t start = handle.cur_swap;
436 error = swap_write_page(&handle, header, NULL);
438 error = save_image(&handle, &snapshot,
442 flush_swap_writer(&handle);
444 error = mark_swapfiles(start);
449 free_all_swap_pages(root_swap, handle.bitmap);
450 release_swap_writer(&handle);
457 * The following functions allow us to read data using a swap map
458 * in a file-alike way
461 static void release_swap_reader(struct swap_map_handle *handle)
464 free_page((unsigned long)handle->cur);
468 static int get_swap_reader(struct swap_map_handle *handle, sector_t start)
475 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
479 error = bio_read_page(start, handle->cur, NULL);
481 release_swap_reader(handle);
488 static int swap_read_page(struct swap_map_handle *handle, void *buf,
489 struct bio **bio_chain)
496 offset = handle->cur->entries[handle->k];
499 error = bio_read_page(offset, buf, bio_chain);
502 if (++handle->k >= MAP_PAGE_ENTRIES) {
503 error = wait_on_bio_chain(bio_chain);
505 offset = handle->cur->next_swap;
507 release_swap_reader(handle);
509 error = bio_read_page(offset, handle->cur, NULL);
515 * load_image - load the image using the swap map handle
516 * @handle and the snapshot handle @snapshot
517 * (assume there are @nr_pages pages to load)
520 static int load_image(struct swap_map_handle *handle,
521 struct snapshot_handle *snapshot,
522 unsigned int nr_to_read)
526 struct timeval start;
532 printk("Loading image data pages (%u pages) ... ", nr_to_read);
533 m = nr_to_read / 100;
538 do_gettimeofday(&start);
540 error = snapshot_write_next(snapshot, PAGE_SIZE);
543 error = swap_read_page(handle, data_of(*snapshot), &bio);
546 if (snapshot->sync_read)
547 error = wait_on_bio_chain(&bio);
551 printk("\b\b\b\b%3d%%", nr_pages / m);
554 err2 = wait_on_bio_chain(&bio);
555 do_gettimeofday(&stop);
559 printk("\b\b\b\bdone\n");
560 snapshot_free_unused_memory(snapshot);
561 if (!snapshot_image_loaded(snapshot))
564 show_speed(&start, &stop, nr_to_read, "Read");
568 int swsusp_read(void)
571 struct swap_map_handle handle;
572 struct snapshot_handle snapshot;
573 struct swsusp_info *header;
575 if (IS_ERR(resume_bdev)) {
576 pr_debug("swsusp: block device not initialised\n");
577 return PTR_ERR(resume_bdev);
580 memset(&snapshot, 0, sizeof(struct snapshot_handle));
581 error = snapshot_write_next(&snapshot, PAGE_SIZE);
582 if (error < PAGE_SIZE)
583 return error < 0 ? error : -EFAULT;
584 header = (struct swsusp_info *)data_of(snapshot);
585 error = get_swap_reader(&handle, swsusp_header.image);
587 error = swap_read_page(&handle, header, NULL);
589 error = load_image(&handle, &snapshot, header->pages - 1);
590 release_swap_reader(&handle);
592 blkdev_put(resume_bdev);
595 pr_debug("swsusp: Reading resume file was successful\n");
597 pr_debug("swsusp: Error %d resuming\n", error);
602 * swsusp_check - Check for swsusp signature in the resume device
605 int swsusp_check(void)
609 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
610 if (!IS_ERR(resume_bdev)) {
611 set_blocksize(resume_bdev, PAGE_SIZE);
612 memset(&swsusp_header, 0, sizeof(swsusp_header));
613 if ((error = bio_read_page(0, &swsusp_header, NULL)))
615 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
616 memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
617 /* Reset swap signature now */
618 error = bio_write_page(0, &swsusp_header, NULL);
623 blkdev_put(resume_bdev);
625 pr_debug("swsusp: Signature found, resuming\n");
627 error = PTR_ERR(resume_bdev);
631 pr_debug("swsusp: Error %d check for resume file\n", error);
637 * swsusp_close - close swap device.
640 void swsusp_close(void)
642 if (IS_ERR(resume_bdev)) {
643 pr_debug("swsusp: block device not initialised\n");
647 blkdev_put(resume_bdev);