Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * linux/kernel/power/swap.c |
| 3 | * |
| 4 | * This file provides functions for reading the suspend image from |
| 5 | * and writing it to a swap partition. |
| 6 | * |
| 7 | * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz> |
| 8 | * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl> |
| 9 | * |
| 10 | * This file is released under the GPLv2. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 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> |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 24 | #include <linux/blkdev.h> |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 25 | #include <linux/swap.h> |
| 26 | #include <linux/swapops.h> |
| 27 | #include <linux/pm.h> |
| 28 | |
| 29 | #include "power.h" |
| 30 | |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 31 | #define SWSUSP_SIG "S1SUSPEND" |
| 32 | |
Vivek Goyal | 1b29c16 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 33 | struct swsusp_header { |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 34 | char reserved[PAGE_SIZE - 20 - sizeof(sector_t) - sizeof(int)]; |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 35 | sector_t image; |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 36 | unsigned int flags; /* Flags to pass to the "boot" kernel */ |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 37 | char orig_sig[10]; |
| 38 | char sig[10]; |
Vivek Goyal | 1b29c16 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 39 | } __attribute__((packed)); |
| 40 | |
| 41 | static struct swsusp_header *swsusp_header; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 42 | |
| 43 | /* |
Rafael J. Wysocki | 3fc6b34 | 2006-12-06 20:34:09 -0800 | [diff] [blame] | 44 | * General things |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 45 | */ |
| 46 | |
| 47 | static unsigned short root_swap = 0xffff; |
Rafael J. Wysocki | 3fc6b34 | 2006-12-06 20:34:09 -0800 | [diff] [blame] | 48 | static struct block_device *resume_bdev; |
| 49 | |
| 50 | /** |
| 51 | * submit - submit BIO request. |
| 52 | * @rw: READ or WRITE. |
| 53 | * @off physical offset of page. |
| 54 | * @page: page we're reading or writing. |
| 55 | * @bio_chain: list of pending biod (for async reading) |
| 56 | * |
| 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. |
| 60 | */ |
| 61 | static int submit(int rw, pgoff_t page_off, struct page *page, |
| 62 | struct bio **bio_chain) |
| 63 | { |
| 64 | struct bio *bio; |
| 65 | |
Rafael J. Wysocki | 8594912 | 2006-12-06 20:34:19 -0800 | [diff] [blame] | 66 | bio = bio_alloc(__GFP_WAIT | __GFP_HIGH, 1); |
Rafael J. Wysocki | 3fc6b34 | 2006-12-06 20:34:09 -0800 | [diff] [blame] | 67 | if (!bio) |
| 68 | return -ENOMEM; |
| 69 | bio->bi_sector = page_off * (PAGE_SIZE >> 9); |
| 70 | bio->bi_bdev = resume_bdev; |
| 71 | bio->bi_end_io = end_swap_bio_read; |
| 72 | |
| 73 | if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) { |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 74 | printk(KERN_ERR "PM: Adding page to bio failed at %ld\n", |
| 75 | page_off); |
Rafael J. Wysocki | 3fc6b34 | 2006-12-06 20:34:09 -0800 | [diff] [blame] | 76 | bio_put(bio); |
| 77 | return -EFAULT; |
| 78 | } |
| 79 | |
| 80 | lock_page(page); |
| 81 | bio_get(bio); |
| 82 | |
| 83 | if (bio_chain == NULL) { |
| 84 | submit_bio(rw | (1 << BIO_RW_SYNC), bio); |
| 85 | wait_on_page_locked(page); |
| 86 | if (rw == READ) |
| 87 | bio_set_pages_dirty(bio); |
| 88 | bio_put(bio); |
| 89 | } else { |
| 90 | if (rw == READ) |
| 91 | get_page(page); /* These pages are freed later */ |
| 92 | bio->bi_private = *bio_chain; |
| 93 | *bio_chain = bio; |
| 94 | submit_bio(rw | (1 << BIO_RW_SYNC), bio); |
| 95 | } |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static int bio_read_page(pgoff_t page_off, void *addr, struct bio **bio_chain) |
| 100 | { |
| 101 | return submit(READ, page_off, virt_to_page(addr), bio_chain); |
| 102 | } |
| 103 | |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 104 | static int bio_write_page(pgoff_t page_off, void *addr, struct bio **bio_chain) |
Rafael J. Wysocki | 3fc6b34 | 2006-12-06 20:34:09 -0800 | [diff] [blame] | 105 | { |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 106 | return submit(WRITE, page_off, virt_to_page(addr), bio_chain); |
Rafael J. Wysocki | 3fc6b34 | 2006-12-06 20:34:09 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static int wait_on_bio_chain(struct bio **bio_chain) |
| 110 | { |
| 111 | struct bio *bio; |
| 112 | struct bio *next_bio; |
| 113 | int ret = 0; |
| 114 | |
| 115 | if (bio_chain == NULL) |
| 116 | return 0; |
| 117 | |
| 118 | bio = *bio_chain; |
| 119 | if (bio == NULL) |
| 120 | return 0; |
| 121 | while (bio) { |
| 122 | struct page *page; |
| 123 | |
| 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)) |
| 128 | ret = -EIO; |
| 129 | put_page(page); |
| 130 | bio_put(bio); |
| 131 | bio = next_bio; |
| 132 | } |
| 133 | *bio_chain = NULL; |
| 134 | return ret; |
| 135 | } |
| 136 | |
Rafael J. Wysocki | 3fc6b34 | 2006-12-06 20:34:09 -0800 | [diff] [blame] | 137 | /* |
| 138 | * Saving part |
| 139 | */ |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 140 | |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 141 | static int mark_swapfiles(sector_t start, unsigned int flags) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 142 | { |
| 143 | int error; |
| 144 | |
Vivek Goyal | 1b29c16 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 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; |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 151 | swsusp_header->flags = flags; |
Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 152 | error = bio_write_page(swsusp_resume_block, |
Vivek Goyal | 1b29c16 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 153 | swsusp_header, NULL); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 154 | } else { |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 155 | printk(KERN_ERR "PM: Swap header not found!\n"); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 156 | error = -ENODEV; |
| 157 | } |
| 158 | return error; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * swsusp_swap_check - check if the resume device is a swap device |
| 163 | * and get its index (if so) |
| 164 | */ |
| 165 | |
| 166 | static int swsusp_swap_check(void) /* This is called before saving image */ |
| 167 | { |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 168 | int res; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 169 | |
Rafael J. Wysocki | 7bf2368 | 2007-01-05 16:36:28 -0800 | [diff] [blame] | 170 | res = swap_type_of(swsusp_resume_device, swsusp_resume_block, |
| 171 | &resume_bdev); |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 172 | if (res < 0) |
| 173 | return res; |
| 174 | |
| 175 | root_swap = res; |
Rafael J. Wysocki | 7bf2368 | 2007-01-05 16:36:28 -0800 | [diff] [blame] | 176 | res = blkdev_get(resume_bdev, FMODE_WRITE, O_RDWR); |
| 177 | if (res) |
| 178 | return res; |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 179 | |
| 180 | res = set_blocksize(resume_bdev, PAGE_SIZE); |
| 181 | if (res < 0) |
| 182 | blkdev_put(resume_bdev); |
| 183 | |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 184 | return res; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * write_page - Write one page to given swap location. |
| 189 | * @buf: Address we're writing. |
| 190 | * @offset: Offset of the swap page we're writing to. |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 191 | * @bio_chain: Link the next write BIO here |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 192 | */ |
| 193 | |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 194 | static int write_page(void *buf, sector_t offset, struct bio **bio_chain) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 195 | { |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 196 | void *src; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 197 | |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 198 | if (!offset) |
| 199 | return -ENOSPC; |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 200 | |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 201 | if (bio_chain) { |
Rafael J. Wysocki | 8594912 | 2006-12-06 20:34:19 -0800 | [diff] [blame] | 202 | src = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH); |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 203 | if (src) { |
| 204 | memcpy(src, buf, PAGE_SIZE); |
| 205 | } else { |
| 206 | WARN_ON_ONCE(1); |
| 207 | bio_chain = NULL; /* Go synchronous */ |
| 208 | src = buf; |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 209 | } |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 210 | } else { |
| 211 | src = buf; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 212 | } |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 213 | return bio_write_page(offset, src, bio_chain); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* |
| 217 | * The swap map is a data structure used for keeping track of each page |
| 218 | * written to a swap partition. It consists of many swap_map_page |
| 219 | * structures that contain each an array of MAP_PAGE_SIZE swap entries. |
| 220 | * These structures are stored on the swap and linked together with the |
| 221 | * help of the .next_swap member. |
| 222 | * |
| 223 | * The swap map is created during suspend. The swap map pages are |
| 224 | * allocated and populated one at a time, so we only need one memory |
| 225 | * page to set up the entire structure. |
| 226 | * |
| 227 | * During resume we also only need to use one swap_map_page structure |
| 228 | * at a time. |
| 229 | */ |
| 230 | |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 231 | #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 232 | |
| 233 | struct swap_map_page { |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 234 | sector_t entries[MAP_PAGE_ENTRIES]; |
| 235 | sector_t next_swap; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | /** |
| 239 | * The swap_map_handle structure is used for handling swap in |
| 240 | * a file-alike way |
| 241 | */ |
| 242 | |
| 243 | struct swap_map_handle { |
| 244 | struct swap_map_page *cur; |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 245 | sector_t cur_swap; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 246 | unsigned int k; |
| 247 | }; |
| 248 | |
| 249 | static void release_swap_writer(struct swap_map_handle *handle) |
| 250 | { |
| 251 | if (handle->cur) |
| 252 | free_page((unsigned long)handle->cur); |
| 253 | handle->cur = NULL; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | static int get_swap_writer(struct swap_map_handle *handle) |
| 257 | { |
| 258 | handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL); |
| 259 | if (!handle->cur) |
| 260 | return -ENOMEM; |
Rafael J. Wysocki | d1d241c | 2007-05-06 14:50:47 -0700 | [diff] [blame] | 261 | handle->cur_swap = alloc_swapdev_block(root_swap); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 262 | if (!handle->cur_swap) { |
| 263 | release_swap_writer(handle); |
| 264 | return -ENOSPC; |
| 265 | } |
| 266 | handle->k = 0; |
| 267 | return 0; |
| 268 | } |
| 269 | |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 270 | static int swap_write_page(struct swap_map_handle *handle, void *buf, |
| 271 | struct bio **bio_chain) |
| 272 | { |
| 273 | int error = 0; |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 274 | sector_t offset; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 275 | |
| 276 | if (!handle->cur) |
| 277 | return -EINVAL; |
Rafael J. Wysocki | d1d241c | 2007-05-06 14:50:47 -0700 | [diff] [blame] | 278 | offset = alloc_swapdev_block(root_swap); |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 279 | error = write_page(buf, offset, bio_chain); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 280 | if (error) |
| 281 | return error; |
| 282 | handle->cur->entries[handle->k++] = offset; |
| 283 | if (handle->k >= MAP_PAGE_ENTRIES) { |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 284 | error = wait_on_bio_chain(bio_chain); |
| 285 | if (error) |
| 286 | goto out; |
Rafael J. Wysocki | d1d241c | 2007-05-06 14:50:47 -0700 | [diff] [blame] | 287 | offset = alloc_swapdev_block(root_swap); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 288 | if (!offset) |
| 289 | return -ENOSPC; |
| 290 | handle->cur->next_swap = offset; |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 291 | error = write_page(handle->cur, handle->cur_swap, NULL); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 292 | if (error) |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 293 | goto out; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 294 | memset(handle->cur, 0, PAGE_SIZE); |
| 295 | handle->cur_swap = offset; |
| 296 | handle->k = 0; |
| 297 | } |
Rafael J. Wysocki | 59a49335 | 2006-12-06 20:34:44 -0800 | [diff] [blame] | 298 | out: |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 299 | return error; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static int flush_swap_writer(struct swap_map_handle *handle) |
| 303 | { |
| 304 | if (handle->cur && handle->cur_swap) |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 305 | return write_page(handle->cur, handle->cur_swap, NULL); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 306 | else |
| 307 | return -EINVAL; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * save_image - save the suspend image data |
| 312 | */ |
| 313 | |
| 314 | static int save_image(struct swap_map_handle *handle, |
| 315 | struct snapshot_handle *snapshot, |
Andrew Morton | 3a4f757 | 2006-09-25 23:32:41 -0700 | [diff] [blame] | 316 | unsigned int nr_to_write) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 317 | { |
| 318 | unsigned int m; |
| 319 | int ret; |
| 320 | int error = 0; |
Andrew Morton | 3a4f757 | 2006-09-25 23:32:41 -0700 | [diff] [blame] | 321 | int nr_pages; |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 322 | int err2; |
| 323 | struct bio *bio; |
Andrew Morton | 3a4f757 | 2006-09-25 23:32:41 -0700 | [diff] [blame] | 324 | struct timeval start; |
| 325 | struct timeval stop; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 326 | |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 327 | printk(KERN_INFO "PM: Saving image data pages (%u pages) ... ", |
| 328 | nr_to_write); |
Andrew Morton | 3a4f757 | 2006-09-25 23:32:41 -0700 | [diff] [blame] | 329 | m = nr_to_write / 100; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 330 | if (!m) |
| 331 | m = 1; |
| 332 | nr_pages = 0; |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 333 | bio = NULL; |
Andrew Morton | 3a4f757 | 2006-09-25 23:32:41 -0700 | [diff] [blame] | 334 | do_gettimeofday(&start); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 335 | do { |
| 336 | ret = snapshot_read_next(snapshot, PAGE_SIZE); |
| 337 | if (ret > 0) { |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 338 | error = swap_write_page(handle, data_of(*snapshot), |
| 339 | &bio); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 340 | if (error) |
| 341 | break; |
| 342 | if (!(nr_pages % m)) |
| 343 | printk("\b\b\b\b%3d%%", nr_pages / m); |
| 344 | nr_pages++; |
| 345 | } |
| 346 | } while (ret > 0); |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 347 | err2 = wait_on_bio_chain(&bio); |
Andrew Morton | 3a4f757 | 2006-09-25 23:32:41 -0700 | [diff] [blame] | 348 | do_gettimeofday(&stop); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 349 | if (!error) |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 350 | error = err2; |
| 351 | if (!error) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 352 | printk("\b\b\b\bdone\n"); |
Rafael J. Wysocki | 0d3a9ab | 2006-12-06 20:34:32 -0800 | [diff] [blame] | 353 | swsusp_show_speed(&start, &stop, nr_to_write, "Wrote"); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 354 | return error; |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * enough_swap - Make sure we have enough swap to save the image. |
| 359 | * |
| 360 | * Returns TRUE or FALSE after checking the total amount of swap |
| 361 | * space avaiable from the resume partition. |
| 362 | */ |
| 363 | |
| 364 | static int enough_swap(unsigned int nr_pages) |
| 365 | { |
| 366 | unsigned int free_swap = count_swap_pages(root_swap, 1); |
| 367 | |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 368 | pr_debug("PM: Free swap pages: %u\n", free_swap); |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 369 | return free_swap > nr_pages + PAGES_FOR_IO; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /** |
| 373 | * swsusp_write - Write entire image and metadata. |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 374 | * @flags: flags to pass to the "boot" kernel in the image header |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 375 | * |
| 376 | * It is important _NOT_ to umount filesystems at this point. We want |
| 377 | * them synced (in case something goes wrong) but we DO not want to mark |
| 378 | * filesystem clean: it is not. (And it does not matter, if we resume |
| 379 | * correctly, we'll mark system clean, anyway.) |
| 380 | */ |
| 381 | |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 382 | int swsusp_write(unsigned int flags) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 383 | { |
| 384 | struct swap_map_handle handle; |
| 385 | struct snapshot_handle snapshot; |
| 386 | struct swsusp_info *header; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 387 | int error; |
| 388 | |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 389 | error = swsusp_swap_check(); |
| 390 | if (error) { |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 391 | printk(KERN_ERR "PM: Cannot find swap device, try " |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 392 | "swapon -a.\n"); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 393 | return error; |
| 394 | } |
| 395 | memset(&snapshot, 0, sizeof(struct snapshot_handle)); |
| 396 | error = snapshot_read_next(&snapshot, PAGE_SIZE); |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 397 | if (error < PAGE_SIZE) { |
| 398 | if (error >= 0) |
| 399 | error = -EFAULT; |
| 400 | |
| 401 | goto out; |
| 402 | } |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 403 | header = (struct swsusp_info *)data_of(snapshot); |
| 404 | if (!enough_swap(header->pages)) { |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 405 | printk(KERN_ERR "PM: Not enough free swap\n"); |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 406 | error = -ENOSPC; |
| 407 | goto out; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 408 | } |
| 409 | error = get_swap_writer(&handle); |
| 410 | if (!error) { |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 411 | sector_t start = handle.cur_swap; |
| 412 | |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 413 | error = swap_write_page(&handle, header, NULL); |
Andrew Morton | 712f403 | 2006-07-10 04:45:00 -0700 | [diff] [blame] | 414 | if (!error) |
| 415 | error = save_image(&handle, &snapshot, |
| 416 | header->pages - 1); |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 417 | |
Andrew Morton | 712f403 | 2006-07-10 04:45:00 -0700 | [diff] [blame] | 418 | if (!error) { |
| 419 | flush_swap_writer(&handle); |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 420 | printk(KERN_INFO "PM: S"); |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 421 | error = mark_swapfiles(start, flags); |
Andrew Morton | 712f403 | 2006-07-10 04:45:00 -0700 | [diff] [blame] | 422 | printk("|\n"); |
| 423 | } |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 424 | } |
| 425 | if (error) |
Rafael J. Wysocki | d1d241c | 2007-05-06 14:50:47 -0700 | [diff] [blame] | 426 | free_all_swap_pages(root_swap); |
| 427 | |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 428 | release_swap_writer(&handle); |
Rafael J. Wysocki | 59a49335 | 2006-12-06 20:34:44 -0800 | [diff] [blame] | 429 | out: |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 430 | swsusp_close(); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 431 | return error; |
| 432 | } |
| 433 | |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 434 | /** |
| 435 | * The following functions allow us to read data using a swap map |
| 436 | * in a file-alike way |
| 437 | */ |
| 438 | |
| 439 | static void release_swap_reader(struct swap_map_handle *handle) |
| 440 | { |
| 441 | if (handle->cur) |
| 442 | free_page((unsigned long)handle->cur); |
| 443 | handle->cur = NULL; |
| 444 | } |
| 445 | |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 446 | static int get_swap_reader(struct swap_map_handle *handle, sector_t start) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 447 | { |
| 448 | int error; |
| 449 | |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 450 | if (!start) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 451 | return -EINVAL; |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 452 | |
Rafael J. Wysocki | 8594912 | 2006-12-06 20:34:19 -0800 | [diff] [blame] | 453 | handle->cur = (struct swap_map_page *)get_zeroed_page(__GFP_WAIT | __GFP_HIGH); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 454 | if (!handle->cur) |
| 455 | return -ENOMEM; |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 456 | |
| 457 | error = bio_read_page(start, handle->cur, NULL); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 458 | if (error) { |
| 459 | release_swap_reader(handle); |
| 460 | return error; |
| 461 | } |
| 462 | handle->k = 0; |
| 463 | return 0; |
| 464 | } |
| 465 | |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 466 | static int swap_read_page(struct swap_map_handle *handle, void *buf, |
| 467 | struct bio **bio_chain) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 468 | { |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 469 | sector_t offset; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 470 | int error; |
| 471 | |
| 472 | if (!handle->cur) |
| 473 | return -EINVAL; |
| 474 | offset = handle->cur->entries[handle->k]; |
| 475 | if (!offset) |
| 476 | return -EFAULT; |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 477 | error = bio_read_page(offset, buf, bio_chain); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 478 | if (error) |
| 479 | return error; |
| 480 | if (++handle->k >= MAP_PAGE_ENTRIES) { |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 481 | error = wait_on_bio_chain(bio_chain); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 482 | handle->k = 0; |
| 483 | offset = handle->cur->next_swap; |
| 484 | if (!offset) |
| 485 | release_swap_reader(handle); |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 486 | else if (!error) |
| 487 | error = bio_read_page(offset, handle->cur, NULL); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 488 | } |
| 489 | return error; |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * load_image - load the image using the swap map handle |
| 494 | * @handle and the snapshot handle @snapshot |
| 495 | * (assume there are @nr_pages pages to load) |
| 496 | */ |
| 497 | |
| 498 | static int load_image(struct swap_map_handle *handle, |
| 499 | struct snapshot_handle *snapshot, |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 500 | unsigned int nr_to_read) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 501 | { |
| 502 | unsigned int m; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 503 | int error = 0; |
Andrew Morton | 8c00249 | 2006-09-25 23:32:43 -0700 | [diff] [blame] | 504 | struct timeval start; |
| 505 | struct timeval stop; |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 506 | struct bio *bio; |
| 507 | int err2; |
| 508 | unsigned nr_pages; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 509 | |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 510 | printk(KERN_INFO "PM: Loading image data pages (%u pages) ... ", |
| 511 | nr_to_read); |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 512 | m = nr_to_read / 100; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 513 | if (!m) |
| 514 | m = 1; |
| 515 | nr_pages = 0; |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 516 | bio = NULL; |
Andrew Morton | 8c00249 | 2006-09-25 23:32:43 -0700 | [diff] [blame] | 517 | do_gettimeofday(&start); |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 518 | for ( ; ; ) { |
| 519 | error = snapshot_write_next(snapshot, PAGE_SIZE); |
| 520 | if (error <= 0) |
| 521 | break; |
| 522 | error = swap_read_page(handle, data_of(*snapshot), &bio); |
| 523 | if (error) |
| 524 | break; |
| 525 | if (snapshot->sync_read) |
| 526 | error = wait_on_bio_chain(&bio); |
| 527 | if (error) |
| 528 | break; |
| 529 | if (!(nr_pages % m)) |
| 530 | printk("\b\b\b\b%3d%%", nr_pages / m); |
| 531 | nr_pages++; |
| 532 | } |
| 533 | err2 = wait_on_bio_chain(&bio); |
Andrew Morton | 8c00249 | 2006-09-25 23:32:43 -0700 | [diff] [blame] | 534 | do_gettimeofday(&stop); |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 535 | if (!error) |
| 536 | error = err2; |
Con Kolivas | e655a25 | 2006-03-26 01:37:11 -0800 | [diff] [blame] | 537 | if (!error) { |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 538 | printk("\b\b\b\bdone\n"); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 539 | snapshot_write_finalize(snapshot); |
Con Kolivas | e655a25 | 2006-03-26 01:37:11 -0800 | [diff] [blame] | 540 | if (!snapshot_image_loaded(snapshot)) |
| 541 | error = -ENODATA; |
| 542 | } |
Rafael J. Wysocki | 0d3a9ab | 2006-12-06 20:34:32 -0800 | [diff] [blame] | 543 | swsusp_show_speed(&start, &stop, nr_to_read, "Read"); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 544 | return error; |
| 545 | } |
| 546 | |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 547 | /** |
| 548 | * swsusp_read - read the hibernation image. |
| 549 | * @flags_p: flags passed by the "frozen" kernel in the image header should |
| 550 | * be written into this memeory location |
| 551 | */ |
| 552 | |
| 553 | int swsusp_read(unsigned int *flags_p) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 554 | { |
| 555 | int error; |
| 556 | struct swap_map_handle handle; |
| 557 | struct snapshot_handle snapshot; |
| 558 | struct swsusp_info *header; |
| 559 | |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 560 | *flags_p = swsusp_header->flags; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 561 | if (IS_ERR(resume_bdev)) { |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 562 | pr_debug("PM: Image device not initialised\n"); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 563 | return PTR_ERR(resume_bdev); |
| 564 | } |
| 565 | |
| 566 | memset(&snapshot, 0, sizeof(struct snapshot_handle)); |
| 567 | error = snapshot_write_next(&snapshot, PAGE_SIZE); |
| 568 | if (error < PAGE_SIZE) |
| 569 | return error < 0 ? error : -EFAULT; |
| 570 | header = (struct swsusp_info *)data_of(snapshot); |
Vivek Goyal | 1b29c16 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 571 | error = get_swap_reader(&handle, swsusp_header->image); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 572 | if (!error) |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 573 | error = swap_read_page(&handle, header, NULL); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 574 | if (!error) |
| 575 | error = load_image(&handle, &snapshot, header->pages - 1); |
| 576 | release_swap_reader(&handle); |
| 577 | |
| 578 | blkdev_put(resume_bdev); |
| 579 | |
| 580 | if (!error) |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 581 | pr_debug("PM: Image successfully loaded\n"); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 582 | else |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 583 | pr_debug("PM: Error %d resuming\n", error); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 584 | return error; |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * swsusp_check - Check for swsusp signature in the resume device |
| 589 | */ |
| 590 | |
| 591 | int swsusp_check(void) |
| 592 | { |
| 593 | int error; |
| 594 | |
| 595 | resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ); |
| 596 | if (!IS_ERR(resume_bdev)) { |
| 597 | set_blocksize(resume_bdev, PAGE_SIZE); |
OGAWA Hirofumi | 6373da1 | 2007-05-23 13:58:00 -0700 | [diff] [blame] | 598 | memset(swsusp_header, 0, PAGE_SIZE); |
Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 599 | error = bio_read_page(swsusp_resume_block, |
Vivek Goyal | 1b29c16 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 600 | swsusp_header, NULL); |
Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 601 | if (error) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 602 | return error; |
Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 603 | |
Vivek Goyal | 1b29c16 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 604 | if (!memcmp(SWSUSP_SIG, swsusp_header->sig, 10)) { |
| 605 | memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 606 | /* Reset swap signature now */ |
Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 607 | error = bio_write_page(swsusp_resume_block, |
Vivek Goyal | 1b29c16 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 608 | swsusp_header, NULL); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 609 | } else { |
| 610 | return -EINVAL; |
| 611 | } |
| 612 | if (error) |
| 613 | blkdev_put(resume_bdev); |
| 614 | else |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 615 | pr_debug("PM: Signature found, resuming\n"); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 616 | } else { |
| 617 | error = PTR_ERR(resume_bdev); |
| 618 | } |
| 619 | |
| 620 | if (error) |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 621 | pr_debug("PM: Error %d checking image file\n", error); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 622 | |
| 623 | return error; |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * swsusp_close - close swap device. |
| 628 | */ |
| 629 | |
| 630 | void swsusp_close(void) |
| 631 | { |
| 632 | if (IS_ERR(resume_bdev)) { |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 633 | pr_debug("PM: Image device not initialised\n"); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 634 | return; |
| 635 | } |
| 636 | |
| 637 | blkdev_put(resume_bdev); |
| 638 | } |
Vivek Goyal | 1b29c16 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 639 | |
| 640 | static int swsusp_header_init(void) |
| 641 | { |
| 642 | swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL); |
| 643 | if (!swsusp_header) |
| 644 | panic("Could not allocate memory for swsusp_header\n"); |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | core_initcall(swsusp_header_init); |