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> |
| 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> |
| 27 | #include <linux/pm.h> |
| 28 | |
| 29 | #include "power.h" |
| 30 | |
| 31 | extern char resume_file[]; |
| 32 | |
| 33 | #define SWSUSP_SIG "S1SUSPEND" |
| 34 | |
| 35 | static struct swsusp_header { |
| 36 | char reserved[PAGE_SIZE - 20 - sizeof(swp_entry_t)]; |
| 37 | swp_entry_t image; |
| 38 | char orig_sig[10]; |
| 39 | char sig[10]; |
| 40 | } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header; |
| 41 | |
| 42 | /* |
| 43 | * Saving part... |
| 44 | */ |
| 45 | |
| 46 | static unsigned short root_swap = 0xffff; |
| 47 | |
| 48 | static int mark_swapfiles(swp_entry_t start) |
| 49 | { |
| 50 | int error; |
| 51 | |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame^] | 52 | rw_swap_page_sync(READ, swp_entry(root_swap, 0), |
| 53 | virt_to_page((unsigned long)&swsusp_header), NULL); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 54 | if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) || |
| 55 | !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) { |
| 56 | memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10); |
| 57 | memcpy(swsusp_header.sig,SWSUSP_SIG, 10); |
| 58 | swsusp_header.image = start; |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame^] | 59 | error = rw_swap_page_sync(WRITE, swp_entry(root_swap, 0), |
| 60 | virt_to_page((unsigned long)&swsusp_header), |
| 61 | NULL); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 62 | } else { |
| 63 | pr_debug("swsusp: Partition is not swap space.\n"); |
| 64 | error = -ENODEV; |
| 65 | } |
| 66 | return error; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * swsusp_swap_check - check if the resume device is a swap device |
| 71 | * and get its index (if so) |
| 72 | */ |
| 73 | |
| 74 | static int swsusp_swap_check(void) /* This is called before saving image */ |
| 75 | { |
| 76 | int res = swap_type_of(swsusp_resume_device); |
| 77 | |
| 78 | if (res >= 0) { |
| 79 | root_swap = res; |
| 80 | return 0; |
| 81 | } |
| 82 | return res; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * write_page - Write one page to given swap location. |
| 87 | * @buf: Address we're writing. |
| 88 | * @offset: Offset of the swap page we're writing to. |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame^] | 89 | * @bio_chain: Link the next write BIO here |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 90 | */ |
| 91 | |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame^] | 92 | static int write_page(void *buf, unsigned long offset, struct bio **bio_chain) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 93 | { |
| 94 | swp_entry_t entry; |
| 95 | int error = -ENOSPC; |
| 96 | |
| 97 | if (offset) { |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame^] | 98 | struct page *page = virt_to_page(buf); |
| 99 | |
| 100 | if (bio_chain) { |
| 101 | /* |
| 102 | * Whether or not we successfully allocated a copy page, |
| 103 | * we take a ref on the page here. It gets undone in |
| 104 | * wait_on_bio_chain(). |
| 105 | */ |
| 106 | struct page *page_copy; |
| 107 | page_copy = alloc_page(GFP_ATOMIC); |
| 108 | if (page_copy == NULL) { |
| 109 | WARN_ON_ONCE(1); |
| 110 | bio_chain = NULL; /* Go synchronous */ |
| 111 | get_page(page); |
| 112 | } else { |
| 113 | memcpy(page_address(page_copy), |
| 114 | page_address(page), PAGE_SIZE); |
| 115 | page = page_copy; |
| 116 | } |
| 117 | } |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 118 | entry = swp_entry(root_swap, offset); |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame^] | 119 | error = rw_swap_page_sync(WRITE, entry, page, bio_chain); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 120 | } |
| 121 | return error; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * The swap map is a data structure used for keeping track of each page |
| 126 | * written to a swap partition. It consists of many swap_map_page |
| 127 | * structures that contain each an array of MAP_PAGE_SIZE swap entries. |
| 128 | * These structures are stored on the swap and linked together with the |
| 129 | * help of the .next_swap member. |
| 130 | * |
| 131 | * The swap map is created during suspend. The swap map pages are |
| 132 | * allocated and populated one at a time, so we only need one memory |
| 133 | * page to set up the entire structure. |
| 134 | * |
| 135 | * During resume we also only need to use one swap_map_page structure |
| 136 | * at a time. |
| 137 | */ |
| 138 | |
| 139 | #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(long) - 1) |
| 140 | |
| 141 | struct swap_map_page { |
| 142 | unsigned long entries[MAP_PAGE_ENTRIES]; |
| 143 | unsigned long next_swap; |
| 144 | }; |
| 145 | |
| 146 | /** |
| 147 | * The swap_map_handle structure is used for handling swap in |
| 148 | * a file-alike way |
| 149 | */ |
| 150 | |
| 151 | struct swap_map_handle { |
| 152 | struct swap_map_page *cur; |
| 153 | unsigned long cur_swap; |
| 154 | struct bitmap_page *bitmap; |
| 155 | unsigned int k; |
| 156 | }; |
| 157 | |
| 158 | static void release_swap_writer(struct swap_map_handle *handle) |
| 159 | { |
| 160 | if (handle->cur) |
| 161 | free_page((unsigned long)handle->cur); |
| 162 | handle->cur = NULL; |
| 163 | if (handle->bitmap) |
| 164 | free_bitmap(handle->bitmap); |
| 165 | handle->bitmap = NULL; |
| 166 | } |
| 167 | |
Andrew Morton | 3a4f757 | 2006-09-25 23:32:41 -0700 | [diff] [blame] | 168 | static void show_speed(struct timeval *start, struct timeval *stop, |
| 169 | unsigned nr_pages, char *msg) |
| 170 | { |
| 171 | s64 elapsed_centisecs64; |
| 172 | int centisecs; |
| 173 | int k; |
| 174 | int kps; |
| 175 | |
| 176 | elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start); |
| 177 | do_div(elapsed_centisecs64, NSEC_PER_SEC / 100); |
| 178 | centisecs = elapsed_centisecs64; |
| 179 | if (centisecs == 0) |
| 180 | centisecs = 1; /* avoid div-by-zero */ |
| 181 | k = nr_pages * (PAGE_SIZE / 1024); |
| 182 | kps = (k * 100) / centisecs; |
| 183 | printk("%s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n", msg, k, |
| 184 | centisecs / 100, centisecs % 100, |
| 185 | kps / 1000, (kps % 1000) / 10); |
| 186 | } |
| 187 | |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 188 | static int get_swap_writer(struct swap_map_handle *handle) |
| 189 | { |
| 190 | handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL); |
| 191 | if (!handle->cur) |
| 192 | return -ENOMEM; |
| 193 | handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0)); |
| 194 | if (!handle->bitmap) { |
| 195 | release_swap_writer(handle); |
| 196 | return -ENOMEM; |
| 197 | } |
| 198 | handle->cur_swap = alloc_swap_page(root_swap, handle->bitmap); |
| 199 | if (!handle->cur_swap) { |
| 200 | release_swap_writer(handle); |
| 201 | return -ENOSPC; |
| 202 | } |
| 203 | handle->k = 0; |
| 204 | return 0; |
| 205 | } |
| 206 | |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame^] | 207 | static int wait_on_bio_chain(struct bio **bio_chain) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 208 | { |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame^] | 209 | struct bio *bio; |
| 210 | struct bio *next_bio; |
| 211 | int ret = 0; |
| 212 | |
| 213 | if (bio_chain == NULL) |
| 214 | return 0; |
| 215 | |
| 216 | bio = *bio_chain; |
| 217 | while (bio) { |
| 218 | struct page *page; |
| 219 | |
| 220 | next_bio = bio->bi_private; |
| 221 | page = bio->bi_io_vec[0].bv_page; |
| 222 | wait_on_page_locked(page); |
| 223 | if (!PageUptodate(page) || PageError(page)) |
| 224 | ret = -EIO; |
| 225 | put_page(page); |
| 226 | bio_put(bio); |
| 227 | bio = next_bio; |
| 228 | } |
| 229 | *bio_chain = NULL; |
| 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | static int swap_write_page(struct swap_map_handle *handle, void *buf, |
| 234 | struct bio **bio_chain) |
| 235 | { |
| 236 | int error = 0; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 237 | unsigned long offset; |
| 238 | |
| 239 | if (!handle->cur) |
| 240 | return -EINVAL; |
| 241 | offset = alloc_swap_page(root_swap, handle->bitmap); |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame^] | 242 | error = write_page(buf, offset, bio_chain); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 243 | if (error) |
| 244 | return error; |
| 245 | handle->cur->entries[handle->k++] = offset; |
| 246 | if (handle->k >= MAP_PAGE_ENTRIES) { |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame^] | 247 | error = wait_on_bio_chain(bio_chain); |
| 248 | if (error) |
| 249 | goto out; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 250 | offset = alloc_swap_page(root_swap, handle->bitmap); |
| 251 | if (!offset) |
| 252 | return -ENOSPC; |
| 253 | handle->cur->next_swap = offset; |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame^] | 254 | error = write_page(handle->cur, handle->cur_swap, NULL); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 255 | if (error) |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame^] | 256 | goto out; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 257 | memset(handle->cur, 0, PAGE_SIZE); |
| 258 | handle->cur_swap = offset; |
| 259 | handle->k = 0; |
| 260 | } |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame^] | 261 | out: |
| 262 | return error; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | static int flush_swap_writer(struct swap_map_handle *handle) |
| 266 | { |
| 267 | if (handle->cur && handle->cur_swap) |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame^] | 268 | return write_page(handle->cur, handle->cur_swap, NULL); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 269 | else |
| 270 | return -EINVAL; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * save_image - save the suspend image data |
| 275 | */ |
| 276 | |
| 277 | static int save_image(struct swap_map_handle *handle, |
| 278 | struct snapshot_handle *snapshot, |
Andrew Morton | 3a4f757 | 2006-09-25 23:32:41 -0700 | [diff] [blame] | 279 | unsigned int nr_to_write) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 280 | { |
| 281 | unsigned int m; |
| 282 | int ret; |
| 283 | int error = 0; |
Andrew Morton | 3a4f757 | 2006-09-25 23:32:41 -0700 | [diff] [blame] | 284 | int nr_pages; |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame^] | 285 | int err2; |
| 286 | struct bio *bio; |
Andrew Morton | 3a4f757 | 2006-09-25 23:32:41 -0700 | [diff] [blame] | 287 | struct timeval start; |
| 288 | struct timeval stop; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 289 | |
Andrew Morton | 3a4f757 | 2006-09-25 23:32:41 -0700 | [diff] [blame] | 290 | printk("Saving image data pages (%u pages) ... ", nr_to_write); |
| 291 | m = nr_to_write / 100; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 292 | if (!m) |
| 293 | m = 1; |
| 294 | nr_pages = 0; |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame^] | 295 | bio = NULL; |
Andrew Morton | 3a4f757 | 2006-09-25 23:32:41 -0700 | [diff] [blame] | 296 | do_gettimeofday(&start); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 297 | do { |
| 298 | ret = snapshot_read_next(snapshot, PAGE_SIZE); |
| 299 | if (ret > 0) { |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame^] | 300 | error = swap_write_page(handle, data_of(*snapshot), |
| 301 | &bio); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 302 | if (error) |
| 303 | break; |
| 304 | if (!(nr_pages % m)) |
| 305 | printk("\b\b\b\b%3d%%", nr_pages / m); |
| 306 | nr_pages++; |
| 307 | } |
| 308 | } while (ret > 0); |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame^] | 309 | err2 = wait_on_bio_chain(&bio); |
Andrew Morton | 3a4f757 | 2006-09-25 23:32:41 -0700 | [diff] [blame] | 310 | do_gettimeofday(&stop); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 311 | if (!error) |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame^] | 312 | error = err2; |
| 313 | if (!error) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 314 | printk("\b\b\b\bdone\n"); |
Andrew Morton | 3a4f757 | 2006-09-25 23:32:41 -0700 | [diff] [blame] | 315 | show_speed(&start, &stop, nr_to_write, "Wrote"); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 316 | return error; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * enough_swap - Make sure we have enough swap to save the image. |
| 321 | * |
| 322 | * Returns TRUE or FALSE after checking the total amount of swap |
| 323 | * space avaiable from the resume partition. |
| 324 | */ |
| 325 | |
| 326 | static int enough_swap(unsigned int nr_pages) |
| 327 | { |
| 328 | unsigned int free_swap = count_swap_pages(root_swap, 1); |
| 329 | |
| 330 | pr_debug("swsusp: free swap pages: %u\n", free_swap); |
| 331 | return free_swap > (nr_pages + PAGES_FOR_IO + |
| 332 | (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * swsusp_write - Write entire image and metadata. |
| 337 | * |
| 338 | * It is important _NOT_ to umount filesystems at this point. We want |
| 339 | * them synced (in case something goes wrong) but we DO not want to mark |
| 340 | * filesystem clean: it is not. (And it does not matter, if we resume |
| 341 | * correctly, we'll mark system clean, anyway.) |
| 342 | */ |
| 343 | |
| 344 | int swsusp_write(void) |
| 345 | { |
| 346 | struct swap_map_handle handle; |
| 347 | struct snapshot_handle snapshot; |
| 348 | struct swsusp_info *header; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 349 | int error; |
| 350 | |
| 351 | if ((error = swsusp_swap_check())) { |
| 352 | printk(KERN_ERR "swsusp: Cannot find swap device, try swapon -a.\n"); |
| 353 | return error; |
| 354 | } |
| 355 | memset(&snapshot, 0, sizeof(struct snapshot_handle)); |
| 356 | error = snapshot_read_next(&snapshot, PAGE_SIZE); |
| 357 | if (error < PAGE_SIZE) |
| 358 | return error < 0 ? error : -EFAULT; |
| 359 | header = (struct swsusp_info *)data_of(snapshot); |
| 360 | if (!enough_swap(header->pages)) { |
| 361 | printk(KERN_ERR "swsusp: Not enough free swap\n"); |
| 362 | return -ENOSPC; |
| 363 | } |
| 364 | error = get_swap_writer(&handle); |
| 365 | if (!error) { |
Andrew Morton | 712f403 | 2006-07-10 04:45:00 -0700 | [diff] [blame] | 366 | unsigned long start = handle.cur_swap; |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame^] | 367 | error = swap_write_page(&handle, header, NULL); |
Andrew Morton | 712f403 | 2006-07-10 04:45:00 -0700 | [diff] [blame] | 368 | if (!error) |
| 369 | error = save_image(&handle, &snapshot, |
| 370 | header->pages - 1); |
| 371 | if (!error) { |
| 372 | flush_swap_writer(&handle); |
| 373 | printk("S"); |
| 374 | error = mark_swapfiles(swp_entry(root_swap, start)); |
| 375 | printk("|\n"); |
| 376 | } |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 377 | } |
| 378 | if (error) |
| 379 | free_all_swap_pages(root_swap, handle.bitmap); |
| 380 | release_swap_writer(&handle); |
| 381 | return error; |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * Using bio to read from swap. |
| 386 | * This code requires a bit more work than just using buffer heads |
| 387 | * but, it is the recommended way for 2.5/2.6. |
| 388 | * The following are to signal the beginning and end of I/O. Bios |
| 389 | * finish asynchronously, while we want them to happen synchronously. |
| 390 | * A simple atomic_t, and a wait loop take care of this problem. |
| 391 | */ |
| 392 | |
| 393 | static atomic_t io_done = ATOMIC_INIT(0); |
| 394 | |
| 395 | static int end_io(struct bio *bio, unsigned int num, int err) |
| 396 | { |
Linus Torvalds | aeceb15 | 2006-07-10 04:45:01 -0700 | [diff] [blame] | 397 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 398 | printk(KERN_ERR "I/O error reading swsusp image.\n"); |
| 399 | return -EIO; |
| 400 | } |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 401 | atomic_set(&io_done, 0); |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | static struct block_device *resume_bdev; |
| 406 | |
| 407 | /** |
| 408 | * submit - submit BIO request. |
| 409 | * @rw: READ or WRITE. |
| 410 | * @off physical offset of page. |
| 411 | * @page: page we're reading or writing. |
| 412 | * |
| 413 | * Straight from the textbook - allocate and initialize the bio. |
| 414 | * If we're writing, make sure the page is marked as dirty. |
| 415 | * Then submit it and wait. |
| 416 | */ |
| 417 | |
| 418 | static int submit(int rw, pgoff_t page_off, void *page) |
| 419 | { |
| 420 | int error = 0; |
| 421 | struct bio *bio; |
| 422 | |
| 423 | bio = bio_alloc(GFP_ATOMIC, 1); |
| 424 | if (!bio) |
| 425 | return -ENOMEM; |
| 426 | bio->bi_sector = page_off * (PAGE_SIZE >> 9); |
| 427 | bio->bi_bdev = resume_bdev; |
| 428 | bio->bi_end_io = end_io; |
| 429 | |
| 430 | if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) { |
| 431 | printk("swsusp: ERROR: adding page to bio at %ld\n",page_off); |
| 432 | error = -EFAULT; |
| 433 | goto Done; |
| 434 | } |
| 435 | |
| 436 | atomic_set(&io_done, 1); |
| 437 | submit_bio(rw | (1 << BIO_RW_SYNC), bio); |
| 438 | while (atomic_read(&io_done)) |
| 439 | yield(); |
| 440 | if (rw == READ) |
| 441 | bio_set_pages_dirty(bio); |
| 442 | Done: |
| 443 | bio_put(bio); |
| 444 | return error; |
| 445 | } |
| 446 | |
| 447 | static int bio_read_page(pgoff_t page_off, void *page) |
| 448 | { |
| 449 | return submit(READ, page_off, page); |
| 450 | } |
| 451 | |
| 452 | static int bio_write_page(pgoff_t page_off, void *page) |
| 453 | { |
| 454 | return submit(WRITE, page_off, page); |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * The following functions allow us to read data using a swap map |
| 459 | * in a file-alike way |
| 460 | */ |
| 461 | |
| 462 | static void release_swap_reader(struct swap_map_handle *handle) |
| 463 | { |
| 464 | if (handle->cur) |
| 465 | free_page((unsigned long)handle->cur); |
| 466 | handle->cur = NULL; |
| 467 | } |
| 468 | |
| 469 | static int get_swap_reader(struct swap_map_handle *handle, |
| 470 | swp_entry_t start) |
| 471 | { |
| 472 | int error; |
| 473 | |
| 474 | if (!swp_offset(start)) |
| 475 | return -EINVAL; |
| 476 | handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC); |
| 477 | if (!handle->cur) |
| 478 | return -ENOMEM; |
| 479 | error = bio_read_page(swp_offset(start), handle->cur); |
| 480 | if (error) { |
| 481 | release_swap_reader(handle); |
| 482 | return error; |
| 483 | } |
| 484 | handle->k = 0; |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | static int swap_read_page(struct swap_map_handle *handle, void *buf) |
| 489 | { |
| 490 | unsigned long offset; |
| 491 | int error; |
| 492 | |
| 493 | if (!handle->cur) |
| 494 | return -EINVAL; |
| 495 | offset = handle->cur->entries[handle->k]; |
| 496 | if (!offset) |
| 497 | return -EFAULT; |
| 498 | error = bio_read_page(offset, buf); |
| 499 | if (error) |
| 500 | return error; |
| 501 | if (++handle->k >= MAP_PAGE_ENTRIES) { |
| 502 | handle->k = 0; |
| 503 | offset = handle->cur->next_swap; |
| 504 | if (!offset) |
| 505 | release_swap_reader(handle); |
| 506 | else |
| 507 | error = bio_read_page(offset, handle->cur); |
| 508 | } |
| 509 | return error; |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * load_image - load the image using the swap map handle |
| 514 | * @handle and the snapshot handle @snapshot |
| 515 | * (assume there are @nr_pages pages to load) |
| 516 | */ |
| 517 | |
| 518 | static int load_image(struct swap_map_handle *handle, |
| 519 | struct snapshot_handle *snapshot, |
| 520 | unsigned int nr_pages) |
| 521 | { |
| 522 | unsigned int m; |
| 523 | int ret; |
| 524 | int error = 0; |
| 525 | |
| 526 | printk("Loading image data pages (%u pages) ... ", nr_pages); |
| 527 | m = nr_pages / 100; |
| 528 | if (!m) |
| 529 | m = 1; |
| 530 | nr_pages = 0; |
| 531 | do { |
| 532 | ret = snapshot_write_next(snapshot, PAGE_SIZE); |
| 533 | if (ret > 0) { |
| 534 | error = swap_read_page(handle, data_of(*snapshot)); |
| 535 | if (error) |
| 536 | break; |
| 537 | if (!(nr_pages % m)) |
| 538 | printk("\b\b\b\b%3d%%", nr_pages / m); |
| 539 | nr_pages++; |
| 540 | } |
| 541 | } while (ret > 0); |
Con Kolivas | e655a25 | 2006-03-26 01:37:11 -0800 | [diff] [blame] | 542 | if (!error) { |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 543 | printk("\b\b\b\bdone\n"); |
Con Kolivas | e655a25 | 2006-03-26 01:37:11 -0800 | [diff] [blame] | 544 | if (!snapshot_image_loaded(snapshot)) |
| 545 | error = -ENODATA; |
| 546 | } |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 547 | return error; |
| 548 | } |
| 549 | |
| 550 | int swsusp_read(void) |
| 551 | { |
| 552 | int error; |
| 553 | struct swap_map_handle handle; |
| 554 | struct snapshot_handle snapshot; |
| 555 | struct swsusp_info *header; |
| 556 | |
| 557 | if (IS_ERR(resume_bdev)) { |
| 558 | pr_debug("swsusp: block device not initialised\n"); |
| 559 | return PTR_ERR(resume_bdev); |
| 560 | } |
| 561 | |
| 562 | memset(&snapshot, 0, sizeof(struct snapshot_handle)); |
| 563 | error = snapshot_write_next(&snapshot, PAGE_SIZE); |
| 564 | if (error < PAGE_SIZE) |
| 565 | return error < 0 ? error : -EFAULT; |
| 566 | header = (struct swsusp_info *)data_of(snapshot); |
| 567 | error = get_swap_reader(&handle, swsusp_header.image); |
| 568 | if (!error) |
| 569 | error = swap_read_page(&handle, header); |
| 570 | if (!error) |
| 571 | error = load_image(&handle, &snapshot, header->pages - 1); |
| 572 | release_swap_reader(&handle); |
| 573 | |
| 574 | blkdev_put(resume_bdev); |
| 575 | |
| 576 | if (!error) |
| 577 | pr_debug("swsusp: Reading resume file was successful\n"); |
| 578 | else |
| 579 | pr_debug("swsusp: Error %d resuming\n", error); |
| 580 | return error; |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * swsusp_check - Check for swsusp signature in the resume device |
| 585 | */ |
| 586 | |
| 587 | int swsusp_check(void) |
| 588 | { |
| 589 | int error; |
| 590 | |
| 591 | resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ); |
| 592 | if (!IS_ERR(resume_bdev)) { |
| 593 | set_blocksize(resume_bdev, PAGE_SIZE); |
| 594 | memset(&swsusp_header, 0, sizeof(swsusp_header)); |
| 595 | if ((error = bio_read_page(0, &swsusp_header))) |
| 596 | return error; |
| 597 | if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) { |
| 598 | memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10); |
| 599 | /* Reset swap signature now */ |
| 600 | error = bio_write_page(0, &swsusp_header); |
| 601 | } else { |
| 602 | return -EINVAL; |
| 603 | } |
| 604 | if (error) |
| 605 | blkdev_put(resume_bdev); |
| 606 | else |
| 607 | pr_debug("swsusp: Signature found, resuming\n"); |
| 608 | } else { |
| 609 | error = PTR_ERR(resume_bdev); |
| 610 | } |
| 611 | |
| 612 | if (error) |
| 613 | pr_debug("swsusp: Error %d check for resume file\n", error); |
| 614 | |
| 615 | return error; |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * swsusp_close - close swap device. |
| 620 | */ |
| 621 | |
| 622 | void swsusp_close(void) |
| 623 | { |
| 624 | if (IS_ERR(resume_bdev)) { |
| 625 | pr_debug("swsusp: block device not initialised\n"); |
| 626 | return; |
| 627 | } |
| 628 | |
| 629 | blkdev_put(resume_bdev); |
| 630 | } |