]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/power/swap.c
[PATCH] swsusp: use block device offsets to identify swap locations
[net-next-2.6.git] / kernel / power / swap.c
CommitLineData
61159a31
RW
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>
546e0d27 25#include <linux/blkdev.h>
61159a31
RW
26#include <linux/swap.h>
27#include <linux/swapops.h>
28#include <linux/pm.h>
29
30#include "power.h"
31
32extern char resume_file[];
33
34#define SWSUSP_SIG "S1SUSPEND"
35
36static struct swsusp_header {
3aef83e0
RW
37 char reserved[PAGE_SIZE - 20 - sizeof(sector_t)];
38 sector_t image;
61159a31
RW
39 char orig_sig[10];
40 char sig[10];
41} __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
42
43/*
3fc6b34f 44 * General things
61159a31
RW
45 */
46
47static unsigned short root_swap = 0xffff;
3fc6b34f
RW
48static 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 */
61static int submit(int rw, pgoff_t page_off, struct page *page,
62 struct bio **bio_chain)
63{
64 struct bio *bio;
65
66 bio = bio_alloc(GFP_ATOMIC, 1);
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) {
74 printk("swsusp: ERROR: adding page to bio at %ld\n", page_off);
75 bio_put(bio);
76 return -EFAULT;
77 }
78
79 lock_page(page);
80 bio_get(bio);
81
82 if (bio_chain == NULL) {
83 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
84 wait_on_page_locked(page);
85 if (rw == READ)
86 bio_set_pages_dirty(bio);
87 bio_put(bio);
88 } else {
89 if (rw == READ)
90 get_page(page); /* These pages are freed later */
91 bio->bi_private = *bio_chain;
92 *bio_chain = bio;
93 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
94 }
95 return 0;
96}
97
98static int bio_read_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
99{
100 return submit(READ, page_off, virt_to_page(addr), bio_chain);
101}
102
3aef83e0 103static int bio_write_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
3fc6b34f 104{
3aef83e0 105 return submit(WRITE, page_off, virt_to_page(addr), bio_chain);
3fc6b34f
RW
106}
107
108static int wait_on_bio_chain(struct bio **bio_chain)
109{
110 struct bio *bio;
111 struct bio *next_bio;
112 int ret = 0;
113
114 if (bio_chain == NULL)
115 return 0;
116
117 bio = *bio_chain;
118 if (bio == NULL)
119 return 0;
120 while (bio) {
121 struct page *page;
122
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))
127 ret = -EIO;
128 put_page(page);
129 bio_put(bio);
130 bio = next_bio;
131 }
132 *bio_chain = NULL;
133 return ret;
134}
135
136static void show_speed(struct timeval *start, struct timeval *stop,
137 unsigned nr_pages, char *msg)
138{
139 s64 elapsed_centisecs64;
140 int centisecs;
141 int k;
142 int kps;
143
144 elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
145 do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
146 centisecs = elapsed_centisecs64;
147 if (centisecs == 0)
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);
154}
155
156/*
157 * Saving part
158 */
61159a31 159
3aef83e0 160static int mark_swapfiles(sector_t start)
61159a31
RW
161{
162 int error;
163
3aef83e0 164 bio_read_page(0, &swsusp_header, NULL);
61159a31
RW
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;
3aef83e0 170 error = bio_write_page(0, &swsusp_header, NULL);
61159a31 171 } else {
3aef83e0 172 printk(KERN_ERR "swsusp: Swap header not found!\n");
61159a31
RW
173 error = -ENODEV;
174 }
175 return error;
176}
177
178/**
179 * swsusp_swap_check - check if the resume device is a swap device
180 * and get its index (if so)
181 */
182
183static int swsusp_swap_check(void) /* This is called before saving image */
184{
3aef83e0
RW
185 int res;
186
187 res = swap_type_of(swsusp_resume_device, 0);
188 if (res < 0)
189 return res;
190
191 root_swap = res;
192 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_WRITE);
193 if (IS_ERR(resume_bdev))
194 return PTR_ERR(resume_bdev);
195
196 res = set_blocksize(resume_bdev, PAGE_SIZE);
197 if (res < 0)
198 blkdev_put(resume_bdev);
61159a31 199
61159a31
RW
200 return res;
201}
202
203/**
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.
ab954160 207 * @bio_chain: Link the next write BIO here
61159a31
RW
208 */
209
3aef83e0 210static int write_page(void *buf, sector_t offset, struct bio **bio_chain)
61159a31 211{
3aef83e0
RW
212 void *src;
213
214 if (!offset)
215 return -ENOSPC;
216
217 if (bio_chain) {
218 src = (void *)__get_free_page(GFP_ATOMIC);
219 if (src) {
220 memcpy(src, buf, PAGE_SIZE);
221 } else {
222 WARN_ON_ONCE(1);
223 bio_chain = NULL; /* Go synchronous */
224 src = buf;
ab954160 225 }
3aef83e0
RW
226 } else {
227 src = buf;
61159a31 228 }
3aef83e0 229 return bio_write_page(offset, src, bio_chain);
61159a31
RW
230}
231
232/*
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.
238 *
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.
242 *
243 * During resume we also only need to use one swap_map_page structure
244 * at a time.
245 */
246
3aef83e0 247#define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
61159a31
RW
248
249struct swap_map_page {
3aef83e0
RW
250 sector_t entries[MAP_PAGE_ENTRIES];
251 sector_t next_swap;
61159a31
RW
252};
253
254/**
255 * The swap_map_handle structure is used for handling swap in
256 * a file-alike way
257 */
258
259struct swap_map_handle {
260 struct swap_map_page *cur;
3aef83e0 261 sector_t cur_swap;
61159a31
RW
262 struct bitmap_page *bitmap;
263 unsigned int k;
264};
265
266static void release_swap_writer(struct swap_map_handle *handle)
267{
268 if (handle->cur)
269 free_page((unsigned long)handle->cur);
270 handle->cur = NULL;
271 if (handle->bitmap)
272 free_bitmap(handle->bitmap);
273 handle->bitmap = NULL;
274}
275
276static int get_swap_writer(struct swap_map_handle *handle)
277{
278 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
279 if (!handle->cur)
280 return -ENOMEM;
281 handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0));
282 if (!handle->bitmap) {
283 release_swap_writer(handle);
284 return -ENOMEM;
285 }
3aef83e0 286 handle->cur_swap = alloc_swapdev_block(root_swap, handle->bitmap);
61159a31
RW
287 if (!handle->cur_swap) {
288 release_swap_writer(handle);
289 return -ENOSPC;
290 }
291 handle->k = 0;
292 return 0;
293}
294
ab954160
AM
295static int swap_write_page(struct swap_map_handle *handle, void *buf,
296 struct bio **bio_chain)
297{
298 int error = 0;
3aef83e0 299 sector_t offset;
61159a31
RW
300
301 if (!handle->cur)
302 return -EINVAL;
3aef83e0 303 offset = alloc_swapdev_block(root_swap, handle->bitmap);
ab954160 304 error = write_page(buf, offset, bio_chain);
61159a31
RW
305 if (error)
306 return error;
307 handle->cur->entries[handle->k++] = offset;
308 if (handle->k >= MAP_PAGE_ENTRIES) {
ab954160
AM
309 error = wait_on_bio_chain(bio_chain);
310 if (error)
311 goto out;
3aef83e0 312 offset = alloc_swapdev_block(root_swap, handle->bitmap);
61159a31
RW
313 if (!offset)
314 return -ENOSPC;
315 handle->cur->next_swap = offset;
ab954160 316 error = write_page(handle->cur, handle->cur_swap, NULL);
61159a31 317 if (error)
ab954160 318 goto out;
61159a31
RW
319 memset(handle->cur, 0, PAGE_SIZE);
320 handle->cur_swap = offset;
321 handle->k = 0;
322 }
ab954160
AM
323out:
324 return error;
61159a31
RW
325}
326
327static int flush_swap_writer(struct swap_map_handle *handle)
328{
329 if (handle->cur && handle->cur_swap)
ab954160 330 return write_page(handle->cur, handle->cur_swap, NULL);
61159a31
RW
331 else
332 return -EINVAL;
333}
334
335/**
336 * save_image - save the suspend image data
337 */
338
339static int save_image(struct swap_map_handle *handle,
340 struct snapshot_handle *snapshot,
3a4f7577 341 unsigned int nr_to_write)
61159a31
RW
342{
343 unsigned int m;
344 int ret;
345 int error = 0;
3a4f7577 346 int nr_pages;
ab954160
AM
347 int err2;
348 struct bio *bio;
3a4f7577
AM
349 struct timeval start;
350 struct timeval stop;
61159a31 351
3a4f7577
AM
352 printk("Saving image data pages (%u pages) ... ", nr_to_write);
353 m = nr_to_write / 100;
61159a31
RW
354 if (!m)
355 m = 1;
356 nr_pages = 0;
ab954160 357 bio = NULL;
3a4f7577 358 do_gettimeofday(&start);
61159a31
RW
359 do {
360 ret = snapshot_read_next(snapshot, PAGE_SIZE);
361 if (ret > 0) {
ab954160
AM
362 error = swap_write_page(handle, data_of(*snapshot),
363 &bio);
61159a31
RW
364 if (error)
365 break;
366 if (!(nr_pages % m))
367 printk("\b\b\b\b%3d%%", nr_pages / m);
368 nr_pages++;
369 }
370 } while (ret > 0);
ab954160 371 err2 = wait_on_bio_chain(&bio);
3a4f7577 372 do_gettimeofday(&stop);
ab954160
AM
373 if (!error)
374 error = err2;
61159a31
RW
375 if (!error)
376 printk("\b\b\b\bdone\n");
3a4f7577 377 show_speed(&start, &stop, nr_to_write, "Wrote");
61159a31
RW
378 return error;
379}
380
381/**
382 * enough_swap - Make sure we have enough swap to save the image.
383 *
384 * Returns TRUE or FALSE after checking the total amount of swap
385 * space avaiable from the resume partition.
386 */
387
388static int enough_swap(unsigned int nr_pages)
389{
390 unsigned int free_swap = count_swap_pages(root_swap, 1);
391
392 pr_debug("swsusp: free swap pages: %u\n", free_swap);
940864dd 393 return free_swap > nr_pages + PAGES_FOR_IO;
61159a31
RW
394}
395
396/**
397 * swsusp_write - Write entire image and metadata.
398 *
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.)
403 */
404
405int swsusp_write(void)
406{
407 struct swap_map_handle handle;
408 struct snapshot_handle snapshot;
409 struct swsusp_info *header;
61159a31
RW
410 int error;
411
3aef83e0
RW
412 error = swsusp_swap_check();
413 if (error) {
546e0d27
AM
414 printk(KERN_ERR "swsusp: Cannot find swap device, try "
415 "swapon -a.\n");
61159a31
RW
416 return error;
417 }
418 memset(&snapshot, 0, sizeof(struct snapshot_handle));
419 error = snapshot_read_next(&snapshot, PAGE_SIZE);
3aef83e0
RW
420 if (error < PAGE_SIZE) {
421 if (error >= 0)
422 error = -EFAULT;
423
424 goto out;
425 }
61159a31
RW
426 header = (struct swsusp_info *)data_of(snapshot);
427 if (!enough_swap(header->pages)) {
428 printk(KERN_ERR "swsusp: Not enough free swap\n");
3aef83e0
RW
429 error = -ENOSPC;
430 goto out;
61159a31
RW
431 }
432 error = get_swap_writer(&handle);
433 if (!error) {
3aef83e0
RW
434 sector_t start = handle.cur_swap;
435
ab954160 436 error = swap_write_page(&handle, header, NULL);
712f403a
AM
437 if (!error)
438 error = save_image(&handle, &snapshot,
439 header->pages - 1);
3aef83e0 440
712f403a
AM
441 if (!error) {
442 flush_swap_writer(&handle);
443 printk("S");
3aef83e0 444 error = mark_swapfiles(start);
712f403a
AM
445 printk("|\n");
446 }
61159a31
RW
447 }
448 if (error)
449 free_all_swap_pages(root_swap, handle.bitmap);
450 release_swap_writer(&handle);
3aef83e0
RW
451out:
452 swsusp_close();
61159a31
RW
453 return error;
454}
455
61159a31
RW
456/**
457 * The following functions allow us to read data using a swap map
458 * in a file-alike way
459 */
460
461static void release_swap_reader(struct swap_map_handle *handle)
462{
463 if (handle->cur)
464 free_page((unsigned long)handle->cur);
465 handle->cur = NULL;
466}
467
3aef83e0 468static int get_swap_reader(struct swap_map_handle *handle, sector_t start)
61159a31
RW
469{
470 int error;
471
3aef83e0 472 if (!start)
61159a31 473 return -EINVAL;
3aef83e0 474
61159a31
RW
475 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
476 if (!handle->cur)
477 return -ENOMEM;
3aef83e0
RW
478
479 error = bio_read_page(start, handle->cur, NULL);
61159a31
RW
480 if (error) {
481 release_swap_reader(handle);
482 return error;
483 }
484 handle->k = 0;
485 return 0;
486}
487
546e0d27
AM
488static int swap_read_page(struct swap_map_handle *handle, void *buf,
489 struct bio **bio_chain)
61159a31 490{
3aef83e0 491 sector_t offset;
61159a31
RW
492 int error;
493
494 if (!handle->cur)
495 return -EINVAL;
496 offset = handle->cur->entries[handle->k];
497 if (!offset)
498 return -EFAULT;
546e0d27 499 error = bio_read_page(offset, buf, bio_chain);
61159a31
RW
500 if (error)
501 return error;
502 if (++handle->k >= MAP_PAGE_ENTRIES) {
546e0d27 503 error = wait_on_bio_chain(bio_chain);
61159a31
RW
504 handle->k = 0;
505 offset = handle->cur->next_swap;
506 if (!offset)
507 release_swap_reader(handle);
546e0d27
AM
508 else if (!error)
509 error = bio_read_page(offset, handle->cur, NULL);
61159a31
RW
510 }
511 return error;
512}
513
514/**
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)
518 */
519
520static int load_image(struct swap_map_handle *handle,
521 struct snapshot_handle *snapshot,
546e0d27 522 unsigned int nr_to_read)
61159a31
RW
523{
524 unsigned int m;
61159a31 525 int error = 0;
8c002494
AM
526 struct timeval start;
527 struct timeval stop;
546e0d27
AM
528 struct bio *bio;
529 int err2;
530 unsigned nr_pages;
61159a31 531
546e0d27
AM
532 printk("Loading image data pages (%u pages) ... ", nr_to_read);
533 m = nr_to_read / 100;
61159a31
RW
534 if (!m)
535 m = 1;
536 nr_pages = 0;
546e0d27 537 bio = NULL;
8c002494 538 do_gettimeofday(&start);
546e0d27
AM
539 for ( ; ; ) {
540 error = snapshot_write_next(snapshot, PAGE_SIZE);
541 if (error <= 0)
542 break;
543 error = swap_read_page(handle, data_of(*snapshot), &bio);
544 if (error)
545 break;
546 if (snapshot->sync_read)
547 error = wait_on_bio_chain(&bio);
548 if (error)
549 break;
550 if (!(nr_pages % m))
551 printk("\b\b\b\b%3d%%", nr_pages / m);
552 nr_pages++;
553 }
554 err2 = wait_on_bio_chain(&bio);
8c002494 555 do_gettimeofday(&stop);
546e0d27
AM
556 if (!error)
557 error = err2;
e655a250 558 if (!error) {
61159a31 559 printk("\b\b\b\bdone\n");
940864dd 560 snapshot_free_unused_memory(snapshot);
e655a250
CK
561 if (!snapshot_image_loaded(snapshot))
562 error = -ENODATA;
563 }
546e0d27 564 show_speed(&start, &stop, nr_to_read, "Read");
61159a31
RW
565 return error;
566}
567
568int swsusp_read(void)
569{
570 int error;
571 struct swap_map_handle handle;
572 struct snapshot_handle snapshot;
573 struct swsusp_info *header;
574
575 if (IS_ERR(resume_bdev)) {
576 pr_debug("swsusp: block device not initialised\n");
577 return PTR_ERR(resume_bdev);
578 }
579
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);
586 if (!error)
546e0d27 587 error = swap_read_page(&handle, header, NULL);
61159a31
RW
588 if (!error)
589 error = load_image(&handle, &snapshot, header->pages - 1);
590 release_swap_reader(&handle);
591
592 blkdev_put(resume_bdev);
593
594 if (!error)
595 pr_debug("swsusp: Reading resume file was successful\n");
596 else
597 pr_debug("swsusp: Error %d resuming\n", error);
598 return error;
599}
600
601/**
602 * swsusp_check - Check for swsusp signature in the resume device
603 */
604
605int swsusp_check(void)
606{
607 int error;
608
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));
546e0d27 613 if ((error = bio_read_page(0, &swsusp_header, NULL)))
61159a31
RW
614 return error;
615 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
616 memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
617 /* Reset swap signature now */
3aef83e0 618 error = bio_write_page(0, &swsusp_header, NULL);
61159a31
RW
619 } else {
620 return -EINVAL;
621 }
622 if (error)
623 blkdev_put(resume_bdev);
624 else
625 pr_debug("swsusp: Signature found, resuming\n");
626 } else {
627 error = PTR_ERR(resume_bdev);
628 }
629
630 if (error)
631 pr_debug("swsusp: Error %d check for resume file\n", error);
632
633 return error;
634}
635
636/**
637 * swsusp_close - close swap device.
638 */
639
640void swsusp_close(void)
641{
642 if (IS_ERR(resume_bdev)) {
643 pr_debug("swsusp: block device not initialised\n");
644 return;
645 }
646
647 blkdev_put(resume_bdev);
648}