blob: b18c155cbb6003f44a343f2d76a91283931bc110 [file] [log] [blame]
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001/*
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>
Andrew Morton546e0d22006-09-25 23:32:44 -070025#include <linux/blkdev.h>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080026#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
Vivek Goyal1b29c162007-05-02 19:27:07 +020036struct swsusp_header {
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -080037 char reserved[PAGE_SIZE - 20 - sizeof(sector_t)];
38 sector_t image;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080039 char orig_sig[10];
40 char sig[10];
Vivek Goyal1b29c162007-05-02 19:27:07 +020041} __attribute__((packed));
42
43static struct swsusp_header *swsusp_header;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080044
45/*
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -080046 * General things
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080047 */
48
49static unsigned short root_swap = 0xffff;
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -080050static struct block_device *resume_bdev;
51
52/**
53 * submit - submit BIO request.
54 * @rw: READ or WRITE.
55 * @off physical offset of page.
56 * @page: page we're reading or writing.
57 * @bio_chain: list of pending biod (for async reading)
58 *
59 * Straight from the textbook - allocate and initialize the bio.
60 * If we're reading, make sure the page is marked as dirty.
61 * Then submit it and, if @bio_chain == NULL, wait.
62 */
63static int submit(int rw, pgoff_t page_off, struct page *page,
64 struct bio **bio_chain)
65{
66 struct bio *bio;
67
Rafael J. Wysocki85949122006-12-06 20:34:19 -080068 bio = bio_alloc(__GFP_WAIT | __GFP_HIGH, 1);
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -080069 if (!bio)
70 return -ENOMEM;
71 bio->bi_sector = page_off * (PAGE_SIZE >> 9);
72 bio->bi_bdev = resume_bdev;
73 bio->bi_end_io = end_swap_bio_read;
74
75 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
76 printk("swsusp: ERROR: adding page to bio at %ld\n", page_off);
77 bio_put(bio);
78 return -EFAULT;
79 }
80
81 lock_page(page);
82 bio_get(bio);
83
84 if (bio_chain == NULL) {
85 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
86 wait_on_page_locked(page);
87 if (rw == READ)
88 bio_set_pages_dirty(bio);
89 bio_put(bio);
90 } else {
91 if (rw == READ)
92 get_page(page); /* These pages are freed later */
93 bio->bi_private = *bio_chain;
94 *bio_chain = bio;
95 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
96 }
97 return 0;
98}
99
100static int bio_read_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
101{
102 return submit(READ, page_off, virt_to_page(addr), bio_chain);
103}
104
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800105static int bio_write_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800106{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800107 return submit(WRITE, page_off, virt_to_page(addr), bio_chain);
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800108}
109
110static int wait_on_bio_chain(struct bio **bio_chain)
111{
112 struct bio *bio;
113 struct bio *next_bio;
114 int ret = 0;
115
116 if (bio_chain == NULL)
117 return 0;
118
119 bio = *bio_chain;
120 if (bio == NULL)
121 return 0;
122 while (bio) {
123 struct page *page;
124
125 next_bio = bio->bi_private;
126 page = bio->bi_io_vec[0].bv_page;
127 wait_on_page_locked(page);
128 if (!PageUptodate(page) || PageError(page))
129 ret = -EIO;
130 put_page(page);
131 bio_put(bio);
132 bio = next_bio;
133 }
134 *bio_chain = NULL;
135 return ret;
136}
137
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800138/*
139 * Saving part
140 */
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800141
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800142static int mark_swapfiles(sector_t start)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800143{
144 int error;
145
Vivek Goyal1b29c162007-05-02 19:27:07 +0200146 bio_read_page(swsusp_resume_block, swsusp_header, NULL);
147 if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) ||
148 !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) {
149 memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10);
150 memcpy(swsusp_header->sig,SWSUSP_SIG, 10);
151 swsusp_header->image = start;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800152 error = bio_write_page(swsusp_resume_block,
Vivek Goyal1b29c162007-05-02 19:27:07 +0200153 swsusp_header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800154 } else {
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800155 printk(KERN_ERR "swsusp: Swap header not found!\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800156 error = -ENODEV;
157 }
158 return error;
159}
160
161/**
162 * swsusp_swap_check - check if the resume device is a swap device
163 * and get its index (if so)
164 */
165
166static int swsusp_swap_check(void) /* This is called before saving image */
167{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800168 int res;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800169
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800170 res = swap_type_of(swsusp_resume_device, swsusp_resume_block,
171 &resume_bdev);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800172 if (res < 0)
173 return res;
174
175 root_swap = res;
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800176 res = blkdev_get(resume_bdev, FMODE_WRITE, O_RDWR);
177 if (res)
178 return res;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800179
180 res = set_blocksize(resume_bdev, PAGE_SIZE);
181 if (res < 0)
182 blkdev_put(resume_bdev);
183
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800184 return res;
185}
186
187/**
188 * write_page - Write one page to given swap location.
189 * @buf: Address we're writing.
190 * @offset: Offset of the swap page we're writing to.
Andrew Mortonab954162006-09-25 23:32:42 -0700191 * @bio_chain: Link the next write BIO here
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800192 */
193
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800194static int write_page(void *buf, sector_t offset, struct bio **bio_chain)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800195{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800196 void *src;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800197
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800198 if (!offset)
199 return -ENOSPC;
Andrew Mortonab954162006-09-25 23:32:42 -0700200
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800201 if (bio_chain) {
Rafael J. Wysocki85949122006-12-06 20:34:19 -0800202 src = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800203 if (src) {
204 memcpy(src, buf, PAGE_SIZE);
205 } else {
206 WARN_ON_ONCE(1);
207 bio_chain = NULL; /* Go synchronous */
208 src = buf;
Andrew Mortonab954162006-09-25 23:32:42 -0700209 }
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800210 } else {
211 src = buf;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800212 }
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800213 return bio_write_page(offset, src, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800214}
215
216/*
217 * The swap map is a data structure used for keeping track of each page
218 * written to a swap partition. It consists of many swap_map_page
219 * structures that contain each an array of MAP_PAGE_SIZE swap entries.
220 * These structures are stored on the swap and linked together with the
221 * help of the .next_swap member.
222 *
223 * The swap map is created during suspend. The swap map pages are
224 * allocated and populated one at a time, so we only need one memory
225 * page to set up the entire structure.
226 *
227 * During resume we also only need to use one swap_map_page structure
228 * at a time.
229 */
230
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800231#define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800232
233struct swap_map_page {
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800234 sector_t entries[MAP_PAGE_ENTRIES];
235 sector_t next_swap;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800236};
237
238/**
239 * The swap_map_handle structure is used for handling swap in
240 * a file-alike way
241 */
242
243struct swap_map_handle {
244 struct swap_map_page *cur;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800245 sector_t cur_swap;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800246 struct bitmap_page *bitmap;
247 unsigned int k;
248};
249
250static void release_swap_writer(struct swap_map_handle *handle)
251{
252 if (handle->cur)
253 free_page((unsigned long)handle->cur);
254 handle->cur = NULL;
255 if (handle->bitmap)
256 free_bitmap(handle->bitmap);
257 handle->bitmap = NULL;
258}
259
260static int get_swap_writer(struct swap_map_handle *handle)
261{
262 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
263 if (!handle->cur)
264 return -ENOMEM;
265 handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0));
266 if (!handle->bitmap) {
267 release_swap_writer(handle);
268 return -ENOMEM;
269 }
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800270 handle->cur_swap = alloc_swapdev_block(root_swap, handle->bitmap);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800271 if (!handle->cur_swap) {
272 release_swap_writer(handle);
273 return -ENOSPC;
274 }
275 handle->k = 0;
276 return 0;
277}
278
Andrew Mortonab954162006-09-25 23:32:42 -0700279static int swap_write_page(struct swap_map_handle *handle, void *buf,
280 struct bio **bio_chain)
281{
282 int error = 0;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800283 sector_t offset;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800284
285 if (!handle->cur)
286 return -EINVAL;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800287 offset = alloc_swapdev_block(root_swap, handle->bitmap);
Andrew Mortonab954162006-09-25 23:32:42 -0700288 error = write_page(buf, offset, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800289 if (error)
290 return error;
291 handle->cur->entries[handle->k++] = offset;
292 if (handle->k >= MAP_PAGE_ENTRIES) {
Andrew Mortonab954162006-09-25 23:32:42 -0700293 error = wait_on_bio_chain(bio_chain);
294 if (error)
295 goto out;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800296 offset = alloc_swapdev_block(root_swap, handle->bitmap);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800297 if (!offset)
298 return -ENOSPC;
299 handle->cur->next_swap = offset;
Andrew Mortonab954162006-09-25 23:32:42 -0700300 error = write_page(handle->cur, handle->cur_swap, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800301 if (error)
Andrew Mortonab954162006-09-25 23:32:42 -0700302 goto out;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800303 memset(handle->cur, 0, PAGE_SIZE);
304 handle->cur_swap = offset;
305 handle->k = 0;
306 }
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800307 out:
Andrew Mortonab954162006-09-25 23:32:42 -0700308 return error;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800309}
310
311static int flush_swap_writer(struct swap_map_handle *handle)
312{
313 if (handle->cur && handle->cur_swap)
Andrew Mortonab954162006-09-25 23:32:42 -0700314 return write_page(handle->cur, handle->cur_swap, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800315 else
316 return -EINVAL;
317}
318
319/**
320 * save_image - save the suspend image data
321 */
322
323static int save_image(struct swap_map_handle *handle,
324 struct snapshot_handle *snapshot,
Andrew Morton3a4f7572006-09-25 23:32:41 -0700325 unsigned int nr_to_write)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800326{
327 unsigned int m;
328 int ret;
329 int error = 0;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700330 int nr_pages;
Andrew Mortonab954162006-09-25 23:32:42 -0700331 int err2;
332 struct bio *bio;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700333 struct timeval start;
334 struct timeval stop;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800335
Andrew Morton3a4f7572006-09-25 23:32:41 -0700336 printk("Saving image data pages (%u pages) ... ", nr_to_write);
337 m = nr_to_write / 100;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800338 if (!m)
339 m = 1;
340 nr_pages = 0;
Andrew Mortonab954162006-09-25 23:32:42 -0700341 bio = NULL;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700342 do_gettimeofday(&start);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800343 do {
344 ret = snapshot_read_next(snapshot, PAGE_SIZE);
345 if (ret > 0) {
Andrew Mortonab954162006-09-25 23:32:42 -0700346 error = swap_write_page(handle, data_of(*snapshot),
347 &bio);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800348 if (error)
349 break;
350 if (!(nr_pages % m))
351 printk("\b\b\b\b%3d%%", nr_pages / m);
352 nr_pages++;
353 }
354 } while (ret > 0);
Andrew Mortonab954162006-09-25 23:32:42 -0700355 err2 = wait_on_bio_chain(&bio);
Andrew Morton3a4f7572006-09-25 23:32:41 -0700356 do_gettimeofday(&stop);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800357 if (!error)
Andrew Mortonab954162006-09-25 23:32:42 -0700358 error = err2;
359 if (!error)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800360 printk("\b\b\b\bdone\n");
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800361 swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800362 return error;
363}
364
365/**
366 * enough_swap - Make sure we have enough swap to save the image.
367 *
368 * Returns TRUE or FALSE after checking the total amount of swap
369 * space avaiable from the resume partition.
370 */
371
372static int enough_swap(unsigned int nr_pages)
373{
374 unsigned int free_swap = count_swap_pages(root_swap, 1);
375
376 pr_debug("swsusp: free swap pages: %u\n", free_swap);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -0700377 return free_swap > nr_pages + PAGES_FOR_IO;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800378}
379
380/**
381 * swsusp_write - Write entire image and metadata.
382 *
383 * It is important _NOT_ to umount filesystems at this point. We want
384 * them synced (in case something goes wrong) but we DO not want to mark
385 * filesystem clean: it is not. (And it does not matter, if we resume
386 * correctly, we'll mark system clean, anyway.)
387 */
388
389int swsusp_write(void)
390{
391 struct swap_map_handle handle;
392 struct snapshot_handle snapshot;
393 struct swsusp_info *header;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800394 int error;
395
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800396 error = swsusp_swap_check();
397 if (error) {
Andrew Morton546e0d22006-09-25 23:32:44 -0700398 printk(KERN_ERR "swsusp: Cannot find swap device, try "
399 "swapon -a.\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800400 return error;
401 }
402 memset(&snapshot, 0, sizeof(struct snapshot_handle));
403 error = snapshot_read_next(&snapshot, PAGE_SIZE);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800404 if (error < PAGE_SIZE) {
405 if (error >= 0)
406 error = -EFAULT;
407
408 goto out;
409 }
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800410 header = (struct swsusp_info *)data_of(snapshot);
411 if (!enough_swap(header->pages)) {
412 printk(KERN_ERR "swsusp: Not enough free swap\n");
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800413 error = -ENOSPC;
414 goto out;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800415 }
416 error = get_swap_writer(&handle);
417 if (!error) {
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800418 sector_t start = handle.cur_swap;
419
Andrew Mortonab954162006-09-25 23:32:42 -0700420 error = swap_write_page(&handle, header, NULL);
Andrew Morton712f4032006-07-10 04:45:00 -0700421 if (!error)
422 error = save_image(&handle, &snapshot,
423 header->pages - 1);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800424
Andrew Morton712f4032006-07-10 04:45:00 -0700425 if (!error) {
426 flush_swap_writer(&handle);
427 printk("S");
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800428 error = mark_swapfiles(start);
Andrew Morton712f4032006-07-10 04:45:00 -0700429 printk("|\n");
430 }
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800431 }
432 if (error)
433 free_all_swap_pages(root_swap, handle.bitmap);
434 release_swap_writer(&handle);
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800435 out:
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800436 swsusp_close();
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800437 return error;
438}
439
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800440/**
441 * The following functions allow us to read data using a swap map
442 * in a file-alike way
443 */
444
445static void release_swap_reader(struct swap_map_handle *handle)
446{
447 if (handle->cur)
448 free_page((unsigned long)handle->cur);
449 handle->cur = NULL;
450}
451
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800452static int get_swap_reader(struct swap_map_handle *handle, sector_t start)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800453{
454 int error;
455
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800456 if (!start)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800457 return -EINVAL;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800458
Rafael J. Wysocki85949122006-12-06 20:34:19 -0800459 handle->cur = (struct swap_map_page *)get_zeroed_page(__GFP_WAIT | __GFP_HIGH);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800460 if (!handle->cur)
461 return -ENOMEM;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800462
463 error = bio_read_page(start, handle->cur, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800464 if (error) {
465 release_swap_reader(handle);
466 return error;
467 }
468 handle->k = 0;
469 return 0;
470}
471
Andrew Morton546e0d22006-09-25 23:32:44 -0700472static int swap_read_page(struct swap_map_handle *handle, void *buf,
473 struct bio **bio_chain)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800474{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800475 sector_t offset;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800476 int error;
477
478 if (!handle->cur)
479 return -EINVAL;
480 offset = handle->cur->entries[handle->k];
481 if (!offset)
482 return -EFAULT;
Andrew Morton546e0d22006-09-25 23:32:44 -0700483 error = bio_read_page(offset, buf, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800484 if (error)
485 return error;
486 if (++handle->k >= MAP_PAGE_ENTRIES) {
Andrew Morton546e0d22006-09-25 23:32:44 -0700487 error = wait_on_bio_chain(bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800488 handle->k = 0;
489 offset = handle->cur->next_swap;
490 if (!offset)
491 release_swap_reader(handle);
Andrew Morton546e0d22006-09-25 23:32:44 -0700492 else if (!error)
493 error = bio_read_page(offset, handle->cur, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800494 }
495 return error;
496}
497
498/**
499 * load_image - load the image using the swap map handle
500 * @handle and the snapshot handle @snapshot
501 * (assume there are @nr_pages pages to load)
502 */
503
504static int load_image(struct swap_map_handle *handle,
505 struct snapshot_handle *snapshot,
Andrew Morton546e0d22006-09-25 23:32:44 -0700506 unsigned int nr_to_read)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800507{
508 unsigned int m;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800509 int error = 0;
Andrew Morton8c002492006-09-25 23:32:43 -0700510 struct timeval start;
511 struct timeval stop;
Andrew Morton546e0d22006-09-25 23:32:44 -0700512 struct bio *bio;
513 int err2;
514 unsigned nr_pages;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800515
Andrew Morton546e0d22006-09-25 23:32:44 -0700516 printk("Loading image data pages (%u pages) ... ", nr_to_read);
517 m = nr_to_read / 100;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800518 if (!m)
519 m = 1;
520 nr_pages = 0;
Andrew Morton546e0d22006-09-25 23:32:44 -0700521 bio = NULL;
Andrew Morton8c002492006-09-25 23:32:43 -0700522 do_gettimeofday(&start);
Andrew Morton546e0d22006-09-25 23:32:44 -0700523 for ( ; ; ) {
524 error = snapshot_write_next(snapshot, PAGE_SIZE);
525 if (error <= 0)
526 break;
527 error = swap_read_page(handle, data_of(*snapshot), &bio);
528 if (error)
529 break;
530 if (snapshot->sync_read)
531 error = wait_on_bio_chain(&bio);
532 if (error)
533 break;
534 if (!(nr_pages % m))
535 printk("\b\b\b\b%3d%%", nr_pages / m);
536 nr_pages++;
537 }
538 err2 = wait_on_bio_chain(&bio);
Andrew Morton8c002492006-09-25 23:32:43 -0700539 do_gettimeofday(&stop);
Andrew Morton546e0d22006-09-25 23:32:44 -0700540 if (!error)
541 error = err2;
Con Kolivase655a252006-03-26 01:37:11 -0800542 if (!error) {
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800543 printk("\b\b\b\bdone\n");
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800544 snapshot_write_finalize(snapshot);
Con Kolivase655a252006-03-26 01:37:11 -0800545 if (!snapshot_image_loaded(snapshot))
546 error = -ENODATA;
547 }
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800548 swsusp_show_speed(&start, &stop, nr_to_read, "Read");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800549 return error;
550}
551
552int swsusp_read(void)
553{
554 int error;
555 struct swap_map_handle handle;
556 struct snapshot_handle snapshot;
557 struct swsusp_info *header;
558
559 if (IS_ERR(resume_bdev)) {
560 pr_debug("swsusp: block device not initialised\n");
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);
Vivek Goyal1b29c162007-05-02 19:27:07 +0200569 error = get_swap_reader(&handle, swsusp_header->image);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800570 if (!error)
Andrew Morton546e0d22006-09-25 23:32:44 -0700571 error = swap_read_page(&handle, header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800572 if (!error)
573 error = load_image(&handle, &snapshot, header->pages - 1);
574 release_swap_reader(&handle);
575
576 blkdev_put(resume_bdev);
577
578 if (!error)
579 pr_debug("swsusp: Reading resume file was successful\n");
580 else
581 pr_debug("swsusp: Error %d resuming\n", error);
582 return error;
583}
584
585/**
586 * swsusp_check - Check for swsusp signature in the resume device
587 */
588
589int swsusp_check(void)
590{
591 int error;
592
593 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
594 if (!IS_ERR(resume_bdev)) {
595 set_blocksize(resume_bdev, PAGE_SIZE);
Vivek Goyal1b29c162007-05-02 19:27:07 +0200596 memset(swsusp_header, 0, sizeof(PAGE_SIZE));
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800597 error = bio_read_page(swsusp_resume_block,
Vivek Goyal1b29c162007-05-02 19:27:07 +0200598 swsusp_header, NULL);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800599 if (error)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800600 return error;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800601
Vivek Goyal1b29c162007-05-02 19:27:07 +0200602 if (!memcmp(SWSUSP_SIG, swsusp_header->sig, 10)) {
603 memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800604 /* Reset swap signature now */
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800605 error = bio_write_page(swsusp_resume_block,
Vivek Goyal1b29c162007-05-02 19:27:07 +0200606 swsusp_header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800607 } else {
608 return -EINVAL;
609 }
610 if (error)
611 blkdev_put(resume_bdev);
612 else
613 pr_debug("swsusp: Signature found, resuming\n");
614 } else {
615 error = PTR_ERR(resume_bdev);
616 }
617
618 if (error)
619 pr_debug("swsusp: Error %d check for resume file\n", error);
620
621 return error;
622}
623
624/**
625 * swsusp_close - close swap device.
626 */
627
628void swsusp_close(void)
629{
630 if (IS_ERR(resume_bdev)) {
631 pr_debug("swsusp: block device not initialised\n");
632 return;
633 }
634
635 blkdev_put(resume_bdev);
636}
Vivek Goyal1b29c162007-05-02 19:27:07 +0200637
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);