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