]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/power/swap.c
[PATCH] swsusp: update userland interface documentation
[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
9a154d9d 164 bio_read_page(swsusp_resume_block, &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;
9a154d9d
RW
170 error = bio_write_page(swsusp_resume_block,
171 &swsusp_header, NULL);
61159a31 172 } else {
3aef83e0 173 printk(KERN_ERR "swsusp: Swap header not found!\n");
61159a31
RW
174 error = -ENODEV;
175 }
176 return error;
177}
178
179/**
180 * swsusp_swap_check - check if the resume device is a swap device
181 * and get its index (if so)
182 */
183
184static int swsusp_swap_check(void) /* This is called before saving image */
185{
3aef83e0
RW
186 int res;
187
9a154d9d 188 res = swap_type_of(swsusp_resume_device, swsusp_resume_block);
3aef83e0
RW
189 if (res < 0)
190 return res;
191
192 root_swap = res;
193 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_WRITE);
194 if (IS_ERR(resume_bdev))
195 return PTR_ERR(resume_bdev);
196
197 res = set_blocksize(resume_bdev, PAGE_SIZE);
198 if (res < 0)
199 blkdev_put(resume_bdev);
61159a31 200
61159a31
RW
201 return res;
202}
203
204/**
205 * write_page - Write one page to given swap location.
206 * @buf: Address we're writing.
207 * @offset: Offset of the swap page we're writing to.
ab954160 208 * @bio_chain: Link the next write BIO here
61159a31
RW
209 */
210
3aef83e0 211static int write_page(void *buf, sector_t offset, struct bio **bio_chain)
61159a31 212{
3aef83e0
RW
213 void *src;
214
215 if (!offset)
216 return -ENOSPC;
217
218 if (bio_chain) {
219 src = (void *)__get_free_page(GFP_ATOMIC);
220 if (src) {
221 memcpy(src, buf, PAGE_SIZE);
222 } else {
223 WARN_ON_ONCE(1);
224 bio_chain = NULL; /* Go synchronous */
225 src = buf;
ab954160 226 }
3aef83e0
RW
227 } else {
228 src = buf;
61159a31 229 }
3aef83e0 230 return bio_write_page(offset, src, bio_chain);
61159a31
RW
231}
232
233/*
234 * The swap map is a data structure used for keeping track of each page
235 * written to a swap partition. It consists of many swap_map_page
236 * structures that contain each an array of MAP_PAGE_SIZE swap entries.
237 * These structures are stored on the swap and linked together with the
238 * help of the .next_swap member.
239 *
240 * The swap map is created during suspend. The swap map pages are
241 * allocated and populated one at a time, so we only need one memory
242 * page to set up the entire structure.
243 *
244 * During resume we also only need to use one swap_map_page structure
245 * at a time.
246 */
247
3aef83e0 248#define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
61159a31
RW
249
250struct swap_map_page {
3aef83e0
RW
251 sector_t entries[MAP_PAGE_ENTRIES];
252 sector_t next_swap;
61159a31
RW
253};
254
255/**
256 * The swap_map_handle structure is used for handling swap in
257 * a file-alike way
258 */
259
260struct swap_map_handle {
261 struct swap_map_page *cur;
3aef83e0 262 sector_t cur_swap;
61159a31
RW
263 struct bitmap_page *bitmap;
264 unsigned int k;
265};
266
267static void release_swap_writer(struct swap_map_handle *handle)
268{
269 if (handle->cur)
270 free_page((unsigned long)handle->cur);
271 handle->cur = NULL;
272 if (handle->bitmap)
273 free_bitmap(handle->bitmap);
274 handle->bitmap = NULL;
275}
276
277static int get_swap_writer(struct swap_map_handle *handle)
278{
279 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
280 if (!handle->cur)
281 return -ENOMEM;
282 handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0));
283 if (!handle->bitmap) {
284 release_swap_writer(handle);
285 return -ENOMEM;
286 }
3aef83e0 287 handle->cur_swap = alloc_swapdev_block(root_swap, handle->bitmap);
61159a31
RW
288 if (!handle->cur_swap) {
289 release_swap_writer(handle);
290 return -ENOSPC;
291 }
292 handle->k = 0;
293 return 0;
294}
295
ab954160
AM
296static int swap_write_page(struct swap_map_handle *handle, void *buf,
297 struct bio **bio_chain)
298{
299 int error = 0;
3aef83e0 300 sector_t offset;
61159a31
RW
301
302 if (!handle->cur)
303 return -EINVAL;
3aef83e0 304 offset = alloc_swapdev_block(root_swap, handle->bitmap);
ab954160 305 error = write_page(buf, offset, bio_chain);
61159a31
RW
306 if (error)
307 return error;
308 handle->cur->entries[handle->k++] = offset;
309 if (handle->k >= MAP_PAGE_ENTRIES) {
ab954160
AM
310 error = wait_on_bio_chain(bio_chain);
311 if (error)
312 goto out;
3aef83e0 313 offset = alloc_swapdev_block(root_swap, handle->bitmap);
61159a31
RW
314 if (!offset)
315 return -ENOSPC;
316 handle->cur->next_swap = offset;
ab954160 317 error = write_page(handle->cur, handle->cur_swap, NULL);
61159a31 318 if (error)
ab954160 319 goto out;
61159a31
RW
320 memset(handle->cur, 0, PAGE_SIZE);
321 handle->cur_swap = offset;
322 handle->k = 0;
323 }
ab954160
AM
324out:
325 return error;
61159a31
RW
326}
327
328static int flush_swap_writer(struct swap_map_handle *handle)
329{
330 if (handle->cur && handle->cur_swap)
ab954160 331 return write_page(handle->cur, handle->cur_swap, NULL);
61159a31
RW
332 else
333 return -EINVAL;
334}
335
336/**
337 * save_image - save the suspend image data
338 */
339
340static int save_image(struct swap_map_handle *handle,
341 struct snapshot_handle *snapshot,
3a4f7577 342 unsigned int nr_to_write)
61159a31
RW
343{
344 unsigned int m;
345 int ret;
346 int error = 0;
3a4f7577 347 int nr_pages;
ab954160
AM
348 int err2;
349 struct bio *bio;
3a4f7577
AM
350 struct timeval start;
351 struct timeval stop;
61159a31 352
3a4f7577
AM
353 printk("Saving image data pages (%u pages) ... ", nr_to_write);
354 m = nr_to_write / 100;
61159a31
RW
355 if (!m)
356 m = 1;
357 nr_pages = 0;
ab954160 358 bio = NULL;
3a4f7577 359 do_gettimeofday(&start);
61159a31
RW
360 do {
361 ret = snapshot_read_next(snapshot, PAGE_SIZE);
362 if (ret > 0) {
ab954160
AM
363 error = swap_write_page(handle, data_of(*snapshot),
364 &bio);
61159a31
RW
365 if (error)
366 break;
367 if (!(nr_pages % m))
368 printk("\b\b\b\b%3d%%", nr_pages / m);
369 nr_pages++;
370 }
371 } while (ret > 0);
ab954160 372 err2 = wait_on_bio_chain(&bio);
3a4f7577 373 do_gettimeofday(&stop);
ab954160
AM
374 if (!error)
375 error = err2;
61159a31
RW
376 if (!error)
377 printk("\b\b\b\bdone\n");
3a4f7577 378 show_speed(&start, &stop, nr_to_write, "Wrote");
61159a31
RW
379 return error;
380}
381
382/**
383 * enough_swap - Make sure we have enough swap to save the image.
384 *
385 * Returns TRUE or FALSE after checking the total amount of swap
386 * space avaiable from the resume partition.
387 */
388
389static int enough_swap(unsigned int nr_pages)
390{
391 unsigned int free_swap = count_swap_pages(root_swap, 1);
392
393 pr_debug("swsusp: free swap pages: %u\n", free_swap);
940864dd 394 return free_swap > nr_pages + PAGES_FOR_IO;
61159a31
RW
395}
396
397/**
398 * swsusp_write - Write entire image and metadata.
399 *
400 * It is important _NOT_ to umount filesystems at this point. We want
401 * them synced (in case something goes wrong) but we DO not want to mark
402 * filesystem clean: it is not. (And it does not matter, if we resume
403 * correctly, we'll mark system clean, anyway.)
404 */
405
406int swsusp_write(void)
407{
408 struct swap_map_handle handle;
409 struct snapshot_handle snapshot;
410 struct swsusp_info *header;
61159a31
RW
411 int error;
412
3aef83e0
RW
413 error = swsusp_swap_check();
414 if (error) {
546e0d27
AM
415 printk(KERN_ERR "swsusp: Cannot find swap device, try "
416 "swapon -a.\n");
61159a31
RW
417 return error;
418 }
419 memset(&snapshot, 0, sizeof(struct snapshot_handle));
420 error = snapshot_read_next(&snapshot, PAGE_SIZE);
3aef83e0
RW
421 if (error < PAGE_SIZE) {
422 if (error >= 0)
423 error = -EFAULT;
424
425 goto out;
426 }
61159a31
RW
427 header = (struct swsusp_info *)data_of(snapshot);
428 if (!enough_swap(header->pages)) {
429 printk(KERN_ERR "swsusp: Not enough free swap\n");
3aef83e0
RW
430 error = -ENOSPC;
431 goto out;
61159a31
RW
432 }
433 error = get_swap_writer(&handle);
434 if (!error) {
3aef83e0
RW
435 sector_t start = handle.cur_swap;
436
ab954160 437 error = swap_write_page(&handle, header, NULL);
712f403a
AM
438 if (!error)
439 error = save_image(&handle, &snapshot,
440 header->pages - 1);
3aef83e0 441
712f403a
AM
442 if (!error) {
443 flush_swap_writer(&handle);
444 printk("S");
3aef83e0 445 error = mark_swapfiles(start);
712f403a
AM
446 printk("|\n");
447 }
61159a31
RW
448 }
449 if (error)
450 free_all_swap_pages(root_swap, handle.bitmap);
451 release_swap_writer(&handle);
3aef83e0
RW
452out:
453 swsusp_close();
61159a31
RW
454 return error;
455}
456
61159a31
RW
457/**
458 * The following functions allow us to read data using a swap map
459 * in a file-alike way
460 */
461
462static 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
3aef83e0 469static int get_swap_reader(struct swap_map_handle *handle, sector_t start)
61159a31
RW
470{
471 int error;
472
3aef83e0 473 if (!start)
61159a31 474 return -EINVAL;
3aef83e0 475
61159a31
RW
476 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
477 if (!handle->cur)
478 return -ENOMEM;
3aef83e0
RW
479
480 error = bio_read_page(start, handle->cur, NULL);
61159a31
RW
481 if (error) {
482 release_swap_reader(handle);
483 return error;
484 }
485 handle->k = 0;
486 return 0;
487}
488
546e0d27
AM
489static int swap_read_page(struct swap_map_handle *handle, void *buf,
490 struct bio **bio_chain)
61159a31 491{
3aef83e0 492 sector_t offset;
61159a31
RW
493 int error;
494
495 if (!handle->cur)
496 return -EINVAL;
497 offset = handle->cur->entries[handle->k];
498 if (!offset)
499 return -EFAULT;
546e0d27 500 error = bio_read_page(offset, buf, bio_chain);
61159a31
RW
501 if (error)
502 return error;
503 if (++handle->k >= MAP_PAGE_ENTRIES) {
546e0d27 504 error = wait_on_bio_chain(bio_chain);
61159a31
RW
505 handle->k = 0;
506 offset = handle->cur->next_swap;
507 if (!offset)
508 release_swap_reader(handle);
546e0d27
AM
509 else if (!error)
510 error = bio_read_page(offset, handle->cur, NULL);
61159a31
RW
511 }
512 return error;
513}
514
515/**
516 * load_image - load the image using the swap map handle
517 * @handle and the snapshot handle @snapshot
518 * (assume there are @nr_pages pages to load)
519 */
520
521static int load_image(struct swap_map_handle *handle,
522 struct snapshot_handle *snapshot,
546e0d27 523 unsigned int nr_to_read)
61159a31
RW
524{
525 unsigned int m;
61159a31 526 int error = 0;
8c002494
AM
527 struct timeval start;
528 struct timeval stop;
546e0d27
AM
529 struct bio *bio;
530 int err2;
531 unsigned nr_pages;
61159a31 532
546e0d27
AM
533 printk("Loading image data pages (%u pages) ... ", nr_to_read);
534 m = nr_to_read / 100;
61159a31
RW
535 if (!m)
536 m = 1;
537 nr_pages = 0;
546e0d27 538 bio = NULL;
8c002494 539 do_gettimeofday(&start);
546e0d27
AM
540 for ( ; ; ) {
541 error = snapshot_write_next(snapshot, PAGE_SIZE);
542 if (error <= 0)
543 break;
544 error = swap_read_page(handle, data_of(*snapshot), &bio);
545 if (error)
546 break;
547 if (snapshot->sync_read)
548 error = wait_on_bio_chain(&bio);
549 if (error)
550 break;
551 if (!(nr_pages % m))
552 printk("\b\b\b\b%3d%%", nr_pages / m);
553 nr_pages++;
554 }
555 err2 = wait_on_bio_chain(&bio);
8c002494 556 do_gettimeofday(&stop);
546e0d27
AM
557 if (!error)
558 error = err2;
e655a250 559 if (!error) {
61159a31 560 printk("\b\b\b\bdone\n");
940864dd 561 snapshot_free_unused_memory(snapshot);
e655a250
CK
562 if (!snapshot_image_loaded(snapshot))
563 error = -ENODATA;
564 }
546e0d27 565 show_speed(&start, &stop, nr_to_read, "Read");
61159a31
RW
566 return error;
567}
568
569int swsusp_read(void)
570{
571 int error;
572 struct swap_map_handle handle;
573 struct snapshot_handle snapshot;
574 struct swsusp_info *header;
575
576 if (IS_ERR(resume_bdev)) {
577 pr_debug("swsusp: block device not initialised\n");
578 return PTR_ERR(resume_bdev);
579 }
580
581 memset(&snapshot, 0, sizeof(struct snapshot_handle));
582 error = snapshot_write_next(&snapshot, PAGE_SIZE);
583 if (error < PAGE_SIZE)
584 return error < 0 ? error : -EFAULT;
585 header = (struct swsusp_info *)data_of(snapshot);
586 error = get_swap_reader(&handle, swsusp_header.image);
587 if (!error)
546e0d27 588 error = swap_read_page(&handle, header, NULL);
61159a31
RW
589 if (!error)
590 error = load_image(&handle, &snapshot, header->pages - 1);
591 release_swap_reader(&handle);
592
593 blkdev_put(resume_bdev);
594
595 if (!error)
596 pr_debug("swsusp: Reading resume file was successful\n");
597 else
598 pr_debug("swsusp: Error %d resuming\n", error);
599 return error;
600}
601
602/**
603 * swsusp_check - Check for swsusp signature in the resume device
604 */
605
606int swsusp_check(void)
607{
608 int error;
609
610 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
611 if (!IS_ERR(resume_bdev)) {
612 set_blocksize(resume_bdev, PAGE_SIZE);
613 memset(&swsusp_header, 0, sizeof(swsusp_header));
9a154d9d
RW
614 error = bio_read_page(swsusp_resume_block,
615 &swsusp_header, NULL);
616 if (error)
61159a31 617 return error;
9a154d9d 618
61159a31
RW
619 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
620 memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
621 /* Reset swap signature now */
9a154d9d
RW
622 error = bio_write_page(swsusp_resume_block,
623 &swsusp_header, NULL);
61159a31
RW
624 } else {
625 return -EINVAL;
626 }
627 if (error)
628 blkdev_put(resume_bdev);
629 else
630 pr_debug("swsusp: Signature found, resuming\n");
631 } else {
632 error = PTR_ERR(resume_bdev);
633 }
634
635 if (error)
636 pr_debug("swsusp: Error %d check for resume file\n", error);
637
638 return error;
639}
640
641/**
642 * swsusp_close - close swap device.
643 */
644
645void swsusp_close(void)
646{
647 if (IS_ERR(resume_bdev)) {
648 pr_debug("swsusp: block device not initialised\n");
649 return;
650 }
651
652 blkdev_put(resume_bdev);
653}