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(swp_entry_t)];
41 } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
47 static unsigned short root_swap = 0xffff;
49 static int mark_swapfiles(swp_entry_t start)
53 rw_swap_page_sync(READ, swp_entry(root_swap, 0),
54 virt_to_page((unsigned long)&swsusp_header), NULL);
55 if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
56 !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
57 memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
58 memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
59 swsusp_header.image = start;
60 error = rw_swap_page_sync(WRITE, swp_entry(root_swap, 0),
61 virt_to_page((unsigned long)&swsusp_header),
64 pr_debug("swsusp: Partition is not swap space.\n");
71 * swsusp_swap_check - check if the resume device is a swap device
72 * and get its index (if so)
75 static int swsusp_swap_check(void) /* This is called before saving image */
77 int res = swap_type_of(swsusp_resume_device);
87 * write_page - Write one page to given swap location.
88 * @buf: Address we're writing.
89 * @offset: Offset of the swap page we're writing to.
90 * @bio_chain: Link the next write BIO here
93 static int write_page(void *buf, unsigned long offset, struct bio **bio_chain)
99 struct page *page = virt_to_page(buf);
103 * Whether or not we successfully allocated a copy page,
104 * we take a ref on the page here. It gets undone in
105 * wait_on_bio_chain().
107 struct page *page_copy;
108 page_copy = alloc_page(GFP_ATOMIC);
109 if (page_copy == NULL) {
111 bio_chain = NULL; /* Go synchronous */
114 memcpy(page_address(page_copy),
115 page_address(page), PAGE_SIZE);
119 entry = swp_entry(root_swap, offset);
120 error = rw_swap_page_sync(WRITE, entry, page, bio_chain);
126 * The swap map is a data structure used for keeping track of each page
127 * written to a swap partition. It consists of many swap_map_page
128 * structures that contain each an array of MAP_PAGE_SIZE swap entries.
129 * These structures are stored on the swap and linked together with the
130 * help of the .next_swap member.
132 * The swap map is created during suspend. The swap map pages are
133 * allocated and populated one at a time, so we only need one memory
134 * page to set up the entire structure.
136 * During resume we also only need to use one swap_map_page structure
140 #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(long) - 1)
142 struct swap_map_page {
143 unsigned long entries[MAP_PAGE_ENTRIES];
144 unsigned long next_swap;
148 * The swap_map_handle structure is used for handling swap in
152 struct swap_map_handle {
153 struct swap_map_page *cur;
154 unsigned long cur_swap;
155 struct bitmap_page *bitmap;
159 static void release_swap_writer(struct swap_map_handle *handle)
162 free_page((unsigned long)handle->cur);
165 free_bitmap(handle->bitmap);
166 handle->bitmap = NULL;
169 static void show_speed(struct timeval *start, struct timeval *stop,
170 unsigned nr_pages, char *msg)
172 s64 elapsed_centisecs64;
177 elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
178 do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
179 centisecs = elapsed_centisecs64;
181 centisecs = 1; /* avoid div-by-zero */
182 k = nr_pages * (PAGE_SIZE / 1024);
183 kps = (k * 100) / centisecs;
184 printk("%s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n", msg, k,
185 centisecs / 100, centisecs % 100,
186 kps / 1000, (kps % 1000) / 10);
189 static int get_swap_writer(struct swap_map_handle *handle)
191 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
194 handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0));
195 if (!handle->bitmap) {
196 release_swap_writer(handle);
199 handle->cur_swap = alloc_swap_page(root_swap, handle->bitmap);
200 if (!handle->cur_swap) {
201 release_swap_writer(handle);
208 static int wait_on_bio_chain(struct bio **bio_chain)
211 struct bio *next_bio;
214 if (bio_chain == NULL)
223 next_bio = bio->bi_private;
224 page = bio->bi_io_vec[0].bv_page;
225 wait_on_page_locked(page);
226 if (!PageUptodate(page) || PageError(page))
236 static int swap_write_page(struct swap_map_handle *handle, void *buf,
237 struct bio **bio_chain)
240 unsigned long offset;
244 offset = alloc_swap_page(root_swap, handle->bitmap);
245 error = write_page(buf, offset, bio_chain);
248 handle->cur->entries[handle->k++] = offset;
249 if (handle->k >= MAP_PAGE_ENTRIES) {
250 error = wait_on_bio_chain(bio_chain);
253 offset = alloc_swap_page(root_swap, handle->bitmap);
256 handle->cur->next_swap = offset;
257 error = write_page(handle->cur, handle->cur_swap, NULL);
260 memset(handle->cur, 0, PAGE_SIZE);
261 handle->cur_swap = offset;
268 static int flush_swap_writer(struct swap_map_handle *handle)
270 if (handle->cur && handle->cur_swap)
271 return write_page(handle->cur, handle->cur_swap, NULL);
277 * save_image - save the suspend image data
280 static int save_image(struct swap_map_handle *handle,
281 struct snapshot_handle *snapshot,
282 unsigned int nr_to_write)
290 struct timeval start;
293 printk("Saving image data pages (%u pages) ... ", nr_to_write);
294 m = nr_to_write / 100;
299 do_gettimeofday(&start);
301 ret = snapshot_read_next(snapshot, PAGE_SIZE);
303 error = swap_write_page(handle, data_of(*snapshot),
308 printk("\b\b\b\b%3d%%", nr_pages / m);
312 err2 = wait_on_bio_chain(&bio);
313 do_gettimeofday(&stop);
317 printk("\b\b\b\bdone\n");
318 show_speed(&start, &stop, nr_to_write, "Wrote");
323 * enough_swap - Make sure we have enough swap to save the image.
325 * Returns TRUE or FALSE after checking the total amount of swap
326 * space avaiable from the resume partition.
329 static int enough_swap(unsigned int nr_pages)
331 unsigned int free_swap = count_swap_pages(root_swap, 1);
333 pr_debug("swsusp: free swap pages: %u\n", free_swap);
334 return free_swap > nr_pages + PAGES_FOR_IO;
338 * swsusp_write - Write entire image and metadata.
340 * It is important _NOT_ to umount filesystems at this point. We want
341 * them synced (in case something goes wrong) but we DO not want to mark
342 * filesystem clean: it is not. (And it does not matter, if we resume
343 * correctly, we'll mark system clean, anyway.)
346 int swsusp_write(void)
348 struct swap_map_handle handle;
349 struct snapshot_handle snapshot;
350 struct swsusp_info *header;
353 if ((error = swsusp_swap_check())) {
354 printk(KERN_ERR "swsusp: Cannot find swap device, try "
358 memset(&snapshot, 0, sizeof(struct snapshot_handle));
359 error = snapshot_read_next(&snapshot, PAGE_SIZE);
360 if (error < PAGE_SIZE)
361 return error < 0 ? error : -EFAULT;
362 header = (struct swsusp_info *)data_of(snapshot);
363 if (!enough_swap(header->pages)) {
364 printk(KERN_ERR "swsusp: Not enough free swap\n");
367 error = get_swap_writer(&handle);
369 unsigned long start = handle.cur_swap;
370 error = swap_write_page(&handle, header, NULL);
372 error = save_image(&handle, &snapshot,
375 flush_swap_writer(&handle);
377 error = mark_swapfiles(swp_entry(root_swap, start));
382 free_all_swap_pages(root_swap, handle.bitmap);
383 release_swap_writer(&handle);
387 static struct block_device *resume_bdev;
390 * submit - submit BIO request.
391 * @rw: READ or WRITE.
392 * @off physical offset of page.
393 * @page: page we're reading or writing.
394 * @bio_chain: list of pending biod (for async reading)
396 * Straight from the textbook - allocate and initialize the bio.
397 * If we're reading, make sure the page is marked as dirty.
398 * Then submit it and, if @bio_chain == NULL, wait.
400 static int submit(int rw, pgoff_t page_off, struct page *page,
401 struct bio **bio_chain)
405 bio = bio_alloc(GFP_ATOMIC, 1);
408 bio->bi_sector = page_off * (PAGE_SIZE >> 9);
409 bio->bi_bdev = resume_bdev;
410 bio->bi_end_io = end_swap_bio_read;
412 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
413 printk("swsusp: ERROR: adding page to bio at %ld\n", page_off);
421 if (bio_chain == NULL) {
422 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
423 wait_on_page_locked(page);
425 bio_set_pages_dirty(bio);
429 bio->bi_private = *bio_chain;
431 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
436 static int bio_read_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
438 return submit(READ, page_off, virt_to_page(addr), bio_chain);
441 static int bio_write_page(pgoff_t page_off, void *addr)
443 return submit(WRITE, page_off, virt_to_page(addr), NULL);
447 * The following functions allow us to read data using a swap map
448 * in a file-alike way
451 static void release_swap_reader(struct swap_map_handle *handle)
454 free_page((unsigned long)handle->cur);
458 static int get_swap_reader(struct swap_map_handle *handle,
463 if (!swp_offset(start))
465 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
468 error = bio_read_page(swp_offset(start), handle->cur, NULL);
470 release_swap_reader(handle);
477 static int swap_read_page(struct swap_map_handle *handle, void *buf,
478 struct bio **bio_chain)
480 unsigned long offset;
485 offset = handle->cur->entries[handle->k];
488 error = bio_read_page(offset, buf, bio_chain);
491 if (++handle->k >= MAP_PAGE_ENTRIES) {
492 error = wait_on_bio_chain(bio_chain);
494 offset = handle->cur->next_swap;
496 release_swap_reader(handle);
498 error = bio_read_page(offset, handle->cur, NULL);
504 * load_image - load the image using the swap map handle
505 * @handle and the snapshot handle @snapshot
506 * (assume there are @nr_pages pages to load)
509 static int load_image(struct swap_map_handle *handle,
510 struct snapshot_handle *snapshot,
511 unsigned int nr_to_read)
515 struct timeval start;
521 printk("Loading image data pages (%u pages) ... ", nr_to_read);
522 m = nr_to_read / 100;
527 do_gettimeofday(&start);
529 error = snapshot_write_next(snapshot, PAGE_SIZE);
532 error = swap_read_page(handle, data_of(*snapshot), &bio);
535 if (snapshot->sync_read)
536 error = wait_on_bio_chain(&bio);
540 printk("\b\b\b\b%3d%%", nr_pages / m);
543 err2 = wait_on_bio_chain(&bio);
544 do_gettimeofday(&stop);
548 printk("\b\b\b\bdone\n");
549 snapshot_free_unused_memory(snapshot);
550 if (!snapshot_image_loaded(snapshot))
553 show_speed(&start, &stop, nr_to_read, "Read");
557 int swsusp_read(void)
560 struct swap_map_handle handle;
561 struct snapshot_handle snapshot;
562 struct swsusp_info *header;
564 if (IS_ERR(resume_bdev)) {
565 pr_debug("swsusp: block device not initialised\n");
566 return PTR_ERR(resume_bdev);
569 memset(&snapshot, 0, sizeof(struct snapshot_handle));
570 error = snapshot_write_next(&snapshot, PAGE_SIZE);
571 if (error < PAGE_SIZE)
572 return error < 0 ? error : -EFAULT;
573 header = (struct swsusp_info *)data_of(snapshot);
574 error = get_swap_reader(&handle, swsusp_header.image);
576 error = swap_read_page(&handle, header, NULL);
578 error = load_image(&handle, &snapshot, header->pages - 1);
579 release_swap_reader(&handle);
581 blkdev_put(resume_bdev);
584 pr_debug("swsusp: Reading resume file was successful\n");
586 pr_debug("swsusp: Error %d resuming\n", error);
591 * swsusp_check - Check for swsusp signature in the resume device
594 int swsusp_check(void)
598 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
599 if (!IS_ERR(resume_bdev)) {
600 set_blocksize(resume_bdev, PAGE_SIZE);
601 memset(&swsusp_header, 0, sizeof(swsusp_header));
602 if ((error = bio_read_page(0, &swsusp_header, NULL)))
604 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
605 memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
606 /* Reset swap signature now */
607 error = bio_write_page(0, &swsusp_header);
612 blkdev_put(resume_bdev);
614 pr_debug("swsusp: Signature found, resuming\n");
616 error = PTR_ERR(resume_bdev);
620 pr_debug("swsusp: Error %d check for resume file\n", error);
626 * swsusp_close - close swap device.
629 void swsusp_close(void)
631 if (IS_ERR(resume_bdev)) {
632 pr_debug("swsusp: block device not initialised\n");
636 blkdev_put(resume_bdev);