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