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/swap.h>
26 #include <linux/swapops.h>
31 extern char resume_file[];
33 #define SWSUSP_SIG "S1SUSPEND"
35 static struct swsusp_header {
36 char reserved[PAGE_SIZE - 20 - sizeof(swp_entry_t)];
40 } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
46 static unsigned short root_swap = 0xffff;
48 static int mark_swapfiles(swp_entry_t start)
52 rw_swap_page_sync(READ,
53 swp_entry(root_swap, 0),
54 virt_to_page((unsigned long)&swsusp_header));
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,
61 swp_entry(root_swap, 0),
62 virt_to_page((unsigned long)
65 pr_debug("swsusp: Partition is not swap space.\n");
72 * swsusp_swap_check - check if the resume device is a swap device
73 * and get its index (if so)
76 static int swsusp_swap_check(void) /* This is called before saving image */
78 int res = swap_type_of(swsusp_resume_device);
88 * write_page - Write one page to given swap location.
89 * @buf: Address we're writing.
90 * @offset: Offset of the swap page we're writing to.
93 static int write_page(void *buf, unsigned long offset)
99 entry = swp_entry(root_swap, offset);
100 error = rw_swap_page_sync(WRITE, entry, virt_to_page(buf));
106 * The swap map is a data structure used for keeping track of each page
107 * written to a swap partition. It consists of many swap_map_page
108 * structures that contain each an array of MAP_PAGE_SIZE swap entries.
109 * These structures are stored on the swap and linked together with the
110 * help of the .next_swap member.
112 * The swap map is created during suspend. The swap map pages are
113 * allocated and populated one at a time, so we only need one memory
114 * page to set up the entire structure.
116 * During resume we also only need to use one swap_map_page structure
120 #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(long) - 1)
122 struct swap_map_page {
123 unsigned long entries[MAP_PAGE_ENTRIES];
124 unsigned long next_swap;
128 * The swap_map_handle structure is used for handling swap in
132 struct swap_map_handle {
133 struct swap_map_page *cur;
134 unsigned long cur_swap;
135 struct bitmap_page *bitmap;
139 static void release_swap_writer(struct swap_map_handle *handle)
142 free_page((unsigned long)handle->cur);
145 free_bitmap(handle->bitmap);
146 handle->bitmap = NULL;
149 static int get_swap_writer(struct swap_map_handle *handle)
151 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
154 handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0));
155 if (!handle->bitmap) {
156 release_swap_writer(handle);
159 handle->cur_swap = alloc_swap_page(root_swap, handle->bitmap);
160 if (!handle->cur_swap) {
161 release_swap_writer(handle);
168 static int swap_write_page(struct swap_map_handle *handle, void *buf)
171 unsigned long offset;
175 offset = alloc_swap_page(root_swap, handle->bitmap);
176 error = write_page(buf, offset);
179 handle->cur->entries[handle->k++] = offset;
180 if (handle->k >= MAP_PAGE_ENTRIES) {
181 offset = alloc_swap_page(root_swap, handle->bitmap);
184 handle->cur->next_swap = offset;
185 error = write_page(handle->cur, handle->cur_swap);
188 memset(handle->cur, 0, PAGE_SIZE);
189 handle->cur_swap = offset;
195 static int flush_swap_writer(struct swap_map_handle *handle)
197 if (handle->cur && handle->cur_swap)
198 return write_page(handle->cur, handle->cur_swap);
204 * save_image - save the suspend image data
207 static int save_image(struct swap_map_handle *handle,
208 struct snapshot_handle *snapshot,
209 unsigned int nr_pages)
215 printk("Saving image data pages (%u pages) ... ", nr_pages);
221 ret = snapshot_read_next(snapshot, PAGE_SIZE);
223 error = swap_write_page(handle, data_of(*snapshot));
227 printk("\b\b\b\b%3d%%", nr_pages / m);
232 printk("\b\b\b\bdone\n");
237 * enough_swap - Make sure we have enough swap to save the image.
239 * Returns TRUE or FALSE after checking the total amount of swap
240 * space avaiable from the resume partition.
243 static int enough_swap(unsigned int nr_pages)
245 unsigned int free_swap = count_swap_pages(root_swap, 1);
247 pr_debug("swsusp: free swap pages: %u\n", free_swap);
248 return free_swap > (nr_pages + PAGES_FOR_IO +
249 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
253 * swsusp_write - Write entire image and metadata.
255 * It is important _NOT_ to umount filesystems at this point. We want
256 * them synced (in case something goes wrong) but we DO not want to mark
257 * filesystem clean: it is not. (And it does not matter, if we resume
258 * correctly, we'll mark system clean, anyway.)
261 int swsusp_write(void)
263 struct swap_map_handle handle;
264 struct snapshot_handle snapshot;
265 struct swsusp_info *header;
269 if ((error = swsusp_swap_check())) {
270 printk(KERN_ERR "swsusp: Cannot find swap device, try swapon -a.\n");
273 memset(&snapshot, 0, sizeof(struct snapshot_handle));
274 error = snapshot_read_next(&snapshot, PAGE_SIZE);
275 if (error < PAGE_SIZE)
276 return error < 0 ? error : -EFAULT;
277 header = (struct swsusp_info *)data_of(snapshot);
278 if (!enough_swap(header->pages)) {
279 printk(KERN_ERR "swsusp: Not enough free swap\n");
282 error = get_swap_writer(&handle);
284 start = handle.cur_swap;
285 error = swap_write_page(&handle, header);
288 error = save_image(&handle, &snapshot, header->pages - 1);
290 flush_swap_writer(&handle);
292 error = mark_swapfiles(swp_entry(root_swap, start));
296 free_all_swap_pages(root_swap, handle.bitmap);
297 release_swap_writer(&handle);
302 * Using bio to read from swap.
303 * This code requires a bit more work than just using buffer heads
304 * but, it is the recommended way for 2.5/2.6.
305 * The following are to signal the beginning and end of I/O. Bios
306 * finish asynchronously, while we want them to happen synchronously.
307 * A simple atomic_t, and a wait loop take care of this problem.
310 static atomic_t io_done = ATOMIC_INIT(0);
312 static int end_io(struct bio *bio, unsigned int num, int err)
314 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
315 panic("I/O error reading memory image");
316 atomic_set(&io_done, 0);
320 static struct block_device *resume_bdev;
323 * submit - submit BIO request.
324 * @rw: READ or WRITE.
325 * @off physical offset of page.
326 * @page: page we're reading or writing.
328 * Straight from the textbook - allocate and initialize the bio.
329 * If we're writing, make sure the page is marked as dirty.
330 * Then submit it and wait.
333 static int submit(int rw, pgoff_t page_off, void *page)
338 bio = bio_alloc(GFP_ATOMIC, 1);
341 bio->bi_sector = page_off * (PAGE_SIZE >> 9);
342 bio->bi_bdev = resume_bdev;
343 bio->bi_end_io = end_io;
345 if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
346 printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
351 atomic_set(&io_done, 1);
352 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
353 while (atomic_read(&io_done))
356 bio_set_pages_dirty(bio);
362 static int bio_read_page(pgoff_t page_off, void *page)
364 return submit(READ, page_off, page);
367 static int bio_write_page(pgoff_t page_off, void *page)
369 return submit(WRITE, page_off, page);
373 * The following functions allow us to read data using a swap map
374 * in a file-alike way
377 static void release_swap_reader(struct swap_map_handle *handle)
380 free_page((unsigned long)handle->cur);
384 static int get_swap_reader(struct swap_map_handle *handle,
389 if (!swp_offset(start))
391 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
394 error = bio_read_page(swp_offset(start), handle->cur);
396 release_swap_reader(handle);
403 static int swap_read_page(struct swap_map_handle *handle, void *buf)
405 unsigned long offset;
410 offset = handle->cur->entries[handle->k];
413 error = bio_read_page(offset, buf);
416 if (++handle->k >= MAP_PAGE_ENTRIES) {
418 offset = handle->cur->next_swap;
420 release_swap_reader(handle);
422 error = bio_read_page(offset, handle->cur);
428 * load_image - load the image using the swap map handle
429 * @handle and the snapshot handle @snapshot
430 * (assume there are @nr_pages pages to load)
433 static int load_image(struct swap_map_handle *handle,
434 struct snapshot_handle *snapshot,
435 unsigned int nr_pages)
441 printk("Loading image data pages (%u pages) ... ", nr_pages);
447 ret = snapshot_write_next(snapshot, PAGE_SIZE);
449 error = swap_read_page(handle, data_of(*snapshot));
453 printk("\b\b\b\b%3d%%", nr_pages / m);
458 printk("\b\b\b\bdone\n");
459 if (!snapshot_image_loaded(snapshot))
465 int swsusp_read(void)
468 struct swap_map_handle handle;
469 struct snapshot_handle snapshot;
470 struct swsusp_info *header;
472 if (IS_ERR(resume_bdev)) {
473 pr_debug("swsusp: block device not initialised\n");
474 return PTR_ERR(resume_bdev);
477 memset(&snapshot, 0, sizeof(struct snapshot_handle));
478 error = snapshot_write_next(&snapshot, PAGE_SIZE);
479 if (error < PAGE_SIZE)
480 return error < 0 ? error : -EFAULT;
481 header = (struct swsusp_info *)data_of(snapshot);
482 error = get_swap_reader(&handle, swsusp_header.image);
484 error = swap_read_page(&handle, header);
486 error = load_image(&handle, &snapshot, header->pages - 1);
487 release_swap_reader(&handle);
489 blkdev_put(resume_bdev);
492 pr_debug("swsusp: Reading resume file was successful\n");
494 pr_debug("swsusp: Error %d resuming\n", error);
499 * swsusp_check - Check for swsusp signature in the resume device
502 int swsusp_check(void)
506 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
507 if (!IS_ERR(resume_bdev)) {
508 set_blocksize(resume_bdev, PAGE_SIZE);
509 memset(&swsusp_header, 0, sizeof(swsusp_header));
510 if ((error = bio_read_page(0, &swsusp_header)))
512 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
513 memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
514 /* Reset swap signature now */
515 error = bio_write_page(0, &swsusp_header);
520 blkdev_put(resume_bdev);
522 pr_debug("swsusp: Signature found, resuming\n");
524 error = PTR_ERR(resume_bdev);
528 pr_debug("swsusp: Error %d check for resume file\n", error);
534 * swsusp_close - close swap device.
537 void swsusp_close(void)
539 if (IS_ERR(resume_bdev)) {
540 pr_debug("swsusp: block device not initialised\n");
544 blkdev_put(resume_bdev);