blob: dedf8797b723f670b9a96a333226675747661b62 [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
36static struct 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];
41} __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
42
43/*
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -080044 * General things
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080045 */
46
47static unsigned short root_swap = 0xffff;
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -080048static 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
Rafael J. Wysocki85949122006-12-06 20:34:19 -080066 bio = bio_alloc(__GFP_WAIT | __GFP_HIGH, 1);
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -080067 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
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800103static int bio_write_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800104{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800105 return submit(WRITE, page_off, virt_to_page(addr), bio_chain);
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800106}
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
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800136/*
137 * Saving part
138 */
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800139
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800140static int mark_swapfiles(sector_t start)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800141{
142 int error;
143
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800144 bio_read_page(swsusp_resume_block, &swsusp_header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800145 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;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800150 error = bio_write_page(swsusp_resume_block,
151 &swsusp_header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800152 } else {
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800153 printk(KERN_ERR "swsusp: Swap header not found!\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800154 error = -ENODEV;
155 }
156 return error;
157}
158
159/**
160 * swsusp_swap_check - check if the resume device is a swap device
161 * and get its index (if so)
162 */
163
164static int swsusp_swap_check(void) /* This is called before saving image */
165{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800166 int res;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800167
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800168 res = swap_type_of(swsusp_resume_device, swsusp_resume_block);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800169 if (res < 0)
170 return res;
171
172 root_swap = res;
173 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_WRITE);
174 if (IS_ERR(resume_bdev))
175 return PTR_ERR(resume_bdev);
176
177 res = set_blocksize(resume_bdev, PAGE_SIZE);
178 if (res < 0)
179 blkdev_put(resume_bdev);
180
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800181 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.
Andrew Mortonab954162006-09-25 23:32:42 -0700188 * @bio_chain: Link the next write BIO here
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800189 */
190
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800191static int write_page(void *buf, sector_t offset, struct bio **bio_chain)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800192{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800193 void *src;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800194
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800195 if (!offset)
196 return -ENOSPC;
Andrew Mortonab954162006-09-25 23:32:42 -0700197
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800198 if (bio_chain) {
Rafael J. Wysocki85949122006-12-06 20:34:19 -0800199 src = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800200 if (src) {
201 memcpy(src, buf, PAGE_SIZE);
202 } else {
203 WARN_ON_ONCE(1);
204 bio_chain = NULL; /* Go synchronous */
205 src = buf;
Andrew Mortonab954162006-09-25 23:32:42 -0700206 }
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800207 } else {
208 src = buf;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800209 }
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800210 return bio_write_page(offset, src, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800211}
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
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800228#define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800229
230struct swap_map_page {
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800231 sector_t entries[MAP_PAGE_ENTRIES];
232 sector_t next_swap;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800233};
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;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800242 sector_t cur_swap;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800243 struct bitmap_page *bitmap;
244 unsigned int k;
245};
246
247static void release_swap_writer(struct swap_map_handle *handle)
248{
249 if (handle->cur)
250 free_page((unsigned long)handle->cur);
251 handle->cur = NULL;
252 if (handle->bitmap)
253 free_bitmap(handle->bitmap);
254 handle->bitmap = NULL;
255}
256
257static int get_swap_writer(struct swap_map_handle *handle)
258{
259 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
260 if (!handle->cur)
261 return -ENOMEM;
262 handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0));
263 if (!handle->bitmap) {
264 release_swap_writer(handle);
265 return -ENOMEM;
266 }
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800267 handle->cur_swap = alloc_swapdev_block(root_swap, handle->bitmap);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800268 if (!handle->cur_swap) {
269 release_swap_writer(handle);
270 return -ENOSPC;
271 }
272 handle->k = 0;
273 return 0;
274}
275
Andrew Mortonab954162006-09-25 23:32:42 -0700276static int swap_write_page(struct swap_map_handle *handle, void *buf,
277 struct bio **bio_chain)
278{
279 int error = 0;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800280 sector_t offset;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800281
282 if (!handle->cur)
283 return -EINVAL;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800284 offset = alloc_swapdev_block(root_swap, handle->bitmap);
Andrew Mortonab954162006-09-25 23:32:42 -0700285 error = write_page(buf, offset, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800286 if (error)
287 return error;
288 handle->cur->entries[handle->k++] = offset;
289 if (handle->k >= MAP_PAGE_ENTRIES) {
Andrew Mortonab954162006-09-25 23:32:42 -0700290 error = wait_on_bio_chain(bio_chain);
291 if (error)
292 goto out;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800293 offset = alloc_swapdev_block(root_swap, handle->bitmap);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800294 if (!offset)
295 return -ENOSPC;
296 handle->cur->next_swap = offset;
Andrew Mortonab954162006-09-25 23:32:42 -0700297 error = write_page(handle->cur, handle->cur_swap, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800298 if (error)
Andrew Mortonab954162006-09-25 23:32:42 -0700299 goto out;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800300 memset(handle->cur, 0, PAGE_SIZE);
301 handle->cur_swap = offset;
302 handle->k = 0;
303 }
Andrew Mortonab954162006-09-25 23:32:42 -0700304out:
305 return error;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800306}
307
308static int flush_swap_writer(struct swap_map_handle *handle)
309{
310 if (handle->cur && handle->cur_swap)
Andrew Mortonab954162006-09-25 23:32:42 -0700311 return write_page(handle->cur, handle->cur_swap, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800312 else
313 return -EINVAL;
314}
315
316/**
317 * save_image - save the suspend image data
318 */
319
320static int save_image(struct swap_map_handle *handle,
321 struct snapshot_handle *snapshot,
Andrew Morton3a4f7572006-09-25 23:32:41 -0700322 unsigned int nr_to_write)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800323{
324 unsigned int m;
325 int ret;
326 int error = 0;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700327 int nr_pages;
Andrew Mortonab954162006-09-25 23:32:42 -0700328 int err2;
329 struct bio *bio;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700330 struct timeval start;
331 struct timeval stop;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800332
Andrew Morton3a4f7572006-09-25 23:32:41 -0700333 printk("Saving image data pages (%u pages) ... ", nr_to_write);
334 m = nr_to_write / 100;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800335 if (!m)
336 m = 1;
337 nr_pages = 0;
Andrew Mortonab954162006-09-25 23:32:42 -0700338 bio = NULL;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700339 do_gettimeofday(&start);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800340 do {
341 ret = snapshot_read_next(snapshot, PAGE_SIZE);
342 if (ret > 0) {
Andrew Mortonab954162006-09-25 23:32:42 -0700343 error = swap_write_page(handle, data_of(*snapshot),
344 &bio);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800345 if (error)
346 break;
347 if (!(nr_pages % m))
348 printk("\b\b\b\b%3d%%", nr_pages / m);
349 nr_pages++;
350 }
351 } while (ret > 0);
Andrew Mortonab954162006-09-25 23:32:42 -0700352 err2 = wait_on_bio_chain(&bio);
Andrew Morton3a4f7572006-09-25 23:32:41 -0700353 do_gettimeofday(&stop);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800354 if (!error)
Andrew Mortonab954162006-09-25 23:32:42 -0700355 error = err2;
356 if (!error)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800357 printk("\b\b\b\bdone\n");
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800358 swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800359 return error;
360}
361
362/**
363 * enough_swap - Make sure we have enough swap to save the image.
364 *
365 * Returns TRUE or FALSE after checking the total amount of swap
366 * space avaiable from the resume partition.
367 */
368
369static int enough_swap(unsigned int nr_pages)
370{
371 unsigned int free_swap = count_swap_pages(root_swap, 1);
372
373 pr_debug("swsusp: free swap pages: %u\n", free_swap);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -0700374 return free_swap > nr_pages + PAGES_FOR_IO;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800375}
376
377/**
378 * swsusp_write - Write entire image and metadata.
379 *
380 * It is important _NOT_ to umount filesystems at this point. We want
381 * them synced (in case something goes wrong) but we DO not want to mark
382 * filesystem clean: it is not. (And it does not matter, if we resume
383 * correctly, we'll mark system clean, anyway.)
384 */
385
386int swsusp_write(void)
387{
388 struct swap_map_handle handle;
389 struct snapshot_handle snapshot;
390 struct swsusp_info *header;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800391 int error;
392
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800393 error = swsusp_swap_check();
394 if (error) {
Andrew Morton546e0d22006-09-25 23:32:44 -0700395 printk(KERN_ERR "swsusp: Cannot find swap device, try "
396 "swapon -a.\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800397 return error;
398 }
399 memset(&snapshot, 0, sizeof(struct snapshot_handle));
400 error = snapshot_read_next(&snapshot, PAGE_SIZE);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800401 if (error < PAGE_SIZE) {
402 if (error >= 0)
403 error = -EFAULT;
404
405 goto out;
406 }
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800407 header = (struct swsusp_info *)data_of(snapshot);
408 if (!enough_swap(header->pages)) {
409 printk(KERN_ERR "swsusp: Not enough free swap\n");
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800410 error = -ENOSPC;
411 goto out;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800412 }
413 error = get_swap_writer(&handle);
414 if (!error) {
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800415 sector_t start = handle.cur_swap;
416
Andrew Mortonab954162006-09-25 23:32:42 -0700417 error = swap_write_page(&handle, header, NULL);
Andrew Morton712f4032006-07-10 04:45:00 -0700418 if (!error)
419 error = save_image(&handle, &snapshot,
420 header->pages - 1);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800421
Andrew Morton712f4032006-07-10 04:45:00 -0700422 if (!error) {
423 flush_swap_writer(&handle);
424 printk("S");
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800425 error = mark_swapfiles(start);
Andrew Morton712f4032006-07-10 04:45:00 -0700426 printk("|\n");
427 }
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800428 }
429 if (error)
430 free_all_swap_pages(root_swap, handle.bitmap);
431 release_swap_writer(&handle);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800432out:
433 swsusp_close();
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800434 return error;
435}
436
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800437/**
438 * The following functions allow us to read data using a swap map
439 * in a file-alike way
440 */
441
442static void release_swap_reader(struct swap_map_handle *handle)
443{
444 if (handle->cur)
445 free_page((unsigned long)handle->cur);
446 handle->cur = NULL;
447}
448
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800449static int get_swap_reader(struct swap_map_handle *handle, sector_t start)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800450{
451 int error;
452
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800453 if (!start)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800454 return -EINVAL;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800455
Rafael J. Wysocki85949122006-12-06 20:34:19 -0800456 handle->cur = (struct swap_map_page *)get_zeroed_page(__GFP_WAIT | __GFP_HIGH);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800457 if (!handle->cur)
458 return -ENOMEM;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800459
460 error = bio_read_page(start, handle->cur, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800461 if (error) {
462 release_swap_reader(handle);
463 return error;
464 }
465 handle->k = 0;
466 return 0;
467}
468
Andrew Morton546e0d22006-09-25 23:32:44 -0700469static int swap_read_page(struct swap_map_handle *handle, void *buf,
470 struct bio **bio_chain)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800471{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800472 sector_t offset;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800473 int error;
474
475 if (!handle->cur)
476 return -EINVAL;
477 offset = handle->cur->entries[handle->k];
478 if (!offset)
479 return -EFAULT;
Andrew Morton546e0d22006-09-25 23:32:44 -0700480 error = bio_read_page(offset, buf, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800481 if (error)
482 return error;
483 if (++handle->k >= MAP_PAGE_ENTRIES) {
Andrew Morton546e0d22006-09-25 23:32:44 -0700484 error = wait_on_bio_chain(bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800485 handle->k = 0;
486 offset = handle->cur->next_swap;
487 if (!offset)
488 release_swap_reader(handle);
Andrew Morton546e0d22006-09-25 23:32:44 -0700489 else if (!error)
490 error = bio_read_page(offset, handle->cur, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800491 }
492 return error;
493}
494
495/**
496 * load_image - load the image using the swap map handle
497 * @handle and the snapshot handle @snapshot
498 * (assume there are @nr_pages pages to load)
499 */
500
501static int load_image(struct swap_map_handle *handle,
502 struct snapshot_handle *snapshot,
Andrew Morton546e0d22006-09-25 23:32:44 -0700503 unsigned int nr_to_read)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800504{
505 unsigned int m;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800506 int error = 0;
Andrew Morton8c002492006-09-25 23:32:43 -0700507 struct timeval start;
508 struct timeval stop;
Andrew Morton546e0d22006-09-25 23:32:44 -0700509 struct bio *bio;
510 int err2;
511 unsigned nr_pages;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800512
Andrew Morton546e0d22006-09-25 23:32:44 -0700513 printk("Loading image data pages (%u pages) ... ", nr_to_read);
514 m = nr_to_read / 100;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800515 if (!m)
516 m = 1;
517 nr_pages = 0;
Andrew Morton546e0d22006-09-25 23:32:44 -0700518 bio = NULL;
Andrew Morton8c002492006-09-25 23:32:43 -0700519 do_gettimeofday(&start);
Andrew Morton546e0d22006-09-25 23:32:44 -0700520 for ( ; ; ) {
521 error = snapshot_write_next(snapshot, PAGE_SIZE);
522 if (error <= 0)
523 break;
524 error = swap_read_page(handle, data_of(*snapshot), &bio);
525 if (error)
526 break;
527 if (snapshot->sync_read)
528 error = wait_on_bio_chain(&bio);
529 if (error)
530 break;
531 if (!(nr_pages % m))
532 printk("\b\b\b\b%3d%%", nr_pages / m);
533 nr_pages++;
534 }
535 err2 = wait_on_bio_chain(&bio);
Andrew Morton8c002492006-09-25 23:32:43 -0700536 do_gettimeofday(&stop);
Andrew Morton546e0d22006-09-25 23:32:44 -0700537 if (!error)
538 error = err2;
Con Kolivase655a252006-03-26 01:37:11 -0800539 if (!error) {
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800540 printk("\b\b\b\bdone\n");
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800541 snapshot_write_finalize(snapshot);
Con Kolivase655a252006-03-26 01:37:11 -0800542 if (!snapshot_image_loaded(snapshot))
543 error = -ENODATA;
544 }
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800545 swsusp_show_speed(&start, &stop, nr_to_read, "Read");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800546 return error;
547}
548
549int swsusp_read(void)
550{
551 int error;
552 struct swap_map_handle handle;
553 struct snapshot_handle snapshot;
554 struct swsusp_info *header;
555
556 if (IS_ERR(resume_bdev)) {
557 pr_debug("swsusp: block device not initialised\n");
558 return PTR_ERR(resume_bdev);
559 }
560
561 memset(&snapshot, 0, sizeof(struct snapshot_handle));
562 error = snapshot_write_next(&snapshot, PAGE_SIZE);
563 if (error < PAGE_SIZE)
564 return error < 0 ? error : -EFAULT;
565 header = (struct swsusp_info *)data_of(snapshot);
566 error = get_swap_reader(&handle, swsusp_header.image);
567 if (!error)
Andrew Morton546e0d22006-09-25 23:32:44 -0700568 error = swap_read_page(&handle, header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800569 if (!error)
570 error = load_image(&handle, &snapshot, header->pages - 1);
571 release_swap_reader(&handle);
572
573 blkdev_put(resume_bdev);
574
575 if (!error)
576 pr_debug("swsusp: Reading resume file was successful\n");
577 else
578 pr_debug("swsusp: Error %d resuming\n", error);
579 return error;
580}
581
582/**
583 * swsusp_check - Check for swsusp signature in the resume device
584 */
585
586int swsusp_check(void)
587{
588 int error;
589
590 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
591 if (!IS_ERR(resume_bdev)) {
592 set_blocksize(resume_bdev, PAGE_SIZE);
593 memset(&swsusp_header, 0, sizeof(swsusp_header));
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800594 error = bio_read_page(swsusp_resume_block,
595 &swsusp_header, NULL);
596 if (error)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800597 return error;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800598
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800599 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
600 memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
601 /* Reset swap signature now */
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800602 error = bio_write_page(swsusp_resume_block,
603 &swsusp_header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800604 } else {
605 return -EINVAL;
606 }
607 if (error)
608 blkdev_put(resume_bdev);
609 else
610 pr_debug("swsusp: Signature found, resuming\n");
611 } else {
612 error = PTR_ERR(resume_bdev);
613 }
614
615 if (error)
616 pr_debug("swsusp: Error %d check for resume file\n", error);
617
618 return error;
619}
620
621/**
622 * swsusp_close - close swap device.
623 */
624
625void swsusp_close(void)
626{
627 if (IS_ERR(resume_bdev)) {
628 pr_debug("swsusp: block device not initialised\n");
629 return;
630 }
631
632 blkdev_put(resume_bdev);
633}