blob: 3581f8f86acdc430d4fdd8edb09dfc8d31d2b5c0 [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. Wysocki7bf23682007-01-05 16:36:28 -0800168 res = swap_type_of(swsusp_resume_device, swsusp_resume_block,
169 &resume_bdev);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800170 if (res < 0)
171 return res;
172
173 root_swap = res;
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800174 res = blkdev_get(resume_bdev, FMODE_WRITE, O_RDWR);
175 if (res)
176 return res;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800177
178 res = set_blocksize(resume_bdev, PAGE_SIZE);
179 if (res < 0)
180 blkdev_put(resume_bdev);
181
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800182 return res;
183}
184
185/**
186 * write_page - Write one page to given swap location.
187 * @buf: Address we're writing.
188 * @offset: Offset of the swap page we're writing to.
Andrew Mortonab954162006-09-25 23:32:42 -0700189 * @bio_chain: Link the next write BIO here
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800190 */
191
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800192static int write_page(void *buf, sector_t offset, struct bio **bio_chain)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800193{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800194 void *src;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800195
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800196 if (!offset)
197 return -ENOSPC;
Andrew Mortonab954162006-09-25 23:32:42 -0700198
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800199 if (bio_chain) {
Rafael J. Wysocki85949122006-12-06 20:34:19 -0800200 src = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800201 if (src) {
202 memcpy(src, buf, PAGE_SIZE);
203 } else {
204 WARN_ON_ONCE(1);
205 bio_chain = NULL; /* Go synchronous */
206 src = buf;
Andrew Mortonab954162006-09-25 23:32:42 -0700207 }
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800208 } else {
209 src = buf;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800210 }
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800211 return bio_write_page(offset, src, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800212}
213
214/*
215 * The swap map is a data structure used for keeping track of each page
216 * written to a swap partition. It consists of many swap_map_page
217 * structures that contain each an array of MAP_PAGE_SIZE swap entries.
218 * These structures are stored on the swap and linked together with the
219 * help of the .next_swap member.
220 *
221 * The swap map is created during suspend. The swap map pages are
222 * allocated and populated one at a time, so we only need one memory
223 * page to set up the entire structure.
224 *
225 * During resume we also only need to use one swap_map_page structure
226 * at a time.
227 */
228
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800229#define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800230
231struct swap_map_page {
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800232 sector_t entries[MAP_PAGE_ENTRIES];
233 sector_t next_swap;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800234};
235
236/**
237 * The swap_map_handle structure is used for handling swap in
238 * a file-alike way
239 */
240
241struct swap_map_handle {
242 struct swap_map_page *cur;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800243 sector_t cur_swap;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800244 struct bitmap_page *bitmap;
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;
253 if (handle->bitmap)
254 free_bitmap(handle->bitmap);
255 handle->bitmap = NULL;
256}
257
258static int get_swap_writer(struct swap_map_handle *handle)
259{
260 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
261 if (!handle->cur)
262 return -ENOMEM;
263 handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0));
264 if (!handle->bitmap) {
265 release_swap_writer(handle);
266 return -ENOMEM;
267 }
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800268 handle->cur_swap = alloc_swapdev_block(root_swap, handle->bitmap);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800269 if (!handle->cur_swap) {
270 release_swap_writer(handle);
271 return -ENOSPC;
272 }
273 handle->k = 0;
274 return 0;
275}
276
Andrew Mortonab954162006-09-25 23:32:42 -0700277static int swap_write_page(struct swap_map_handle *handle, void *buf,
278 struct bio **bio_chain)
279{
280 int error = 0;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800281 sector_t offset;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800282
283 if (!handle->cur)
284 return -EINVAL;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800285 offset = alloc_swapdev_block(root_swap, handle->bitmap);
Andrew Mortonab954162006-09-25 23:32:42 -0700286 error = write_page(buf, offset, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800287 if (error)
288 return error;
289 handle->cur->entries[handle->k++] = offset;
290 if (handle->k >= MAP_PAGE_ENTRIES) {
Andrew Mortonab954162006-09-25 23:32:42 -0700291 error = wait_on_bio_chain(bio_chain);
292 if (error)
293 goto out;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800294 offset = alloc_swapdev_block(root_swap, handle->bitmap);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800295 if (!offset)
296 return -ENOSPC;
297 handle->cur->next_swap = offset;
Andrew Mortonab954162006-09-25 23:32:42 -0700298 error = write_page(handle->cur, handle->cur_swap, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800299 if (error)
Andrew Mortonab954162006-09-25 23:32:42 -0700300 goto out;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800301 memset(handle->cur, 0, PAGE_SIZE);
302 handle->cur_swap = offset;
303 handle->k = 0;
304 }
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800305 out:
Andrew Mortonab954162006-09-25 23:32:42 -0700306 return error;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800307}
308
309static int flush_swap_writer(struct swap_map_handle *handle)
310{
311 if (handle->cur && handle->cur_swap)
Andrew Mortonab954162006-09-25 23:32:42 -0700312 return write_page(handle->cur, handle->cur_swap, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800313 else
314 return -EINVAL;
315}
316
317/**
318 * save_image - save the suspend image data
319 */
320
321static int save_image(struct swap_map_handle *handle,
322 struct snapshot_handle *snapshot,
Andrew Morton3a4f7572006-09-25 23:32:41 -0700323 unsigned int nr_to_write)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800324{
325 unsigned int m;
326 int ret;
327 int error = 0;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700328 int nr_pages;
Andrew Mortonab954162006-09-25 23:32:42 -0700329 int err2;
330 struct bio *bio;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700331 struct timeval start;
332 struct timeval stop;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800333
Andrew Morton3a4f7572006-09-25 23:32:41 -0700334 printk("Saving image data pages (%u pages) ... ", nr_to_write);
335 m = nr_to_write / 100;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800336 if (!m)
337 m = 1;
338 nr_pages = 0;
Andrew Mortonab954162006-09-25 23:32:42 -0700339 bio = NULL;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700340 do_gettimeofday(&start);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800341 do {
342 ret = snapshot_read_next(snapshot, PAGE_SIZE);
343 if (ret > 0) {
Andrew Mortonab954162006-09-25 23:32:42 -0700344 error = swap_write_page(handle, data_of(*snapshot),
345 &bio);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800346 if (error)
347 break;
348 if (!(nr_pages % m))
349 printk("\b\b\b\b%3d%%", nr_pages / m);
350 nr_pages++;
351 }
352 } while (ret > 0);
Andrew Mortonab954162006-09-25 23:32:42 -0700353 err2 = wait_on_bio_chain(&bio);
Andrew Morton3a4f7572006-09-25 23:32:41 -0700354 do_gettimeofday(&stop);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800355 if (!error)
Andrew Mortonab954162006-09-25 23:32:42 -0700356 error = err2;
357 if (!error)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800358 printk("\b\b\b\bdone\n");
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800359 swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800360 return error;
361}
362
363/**
364 * enough_swap - Make sure we have enough swap to save the image.
365 *
366 * Returns TRUE or FALSE after checking the total amount of swap
367 * space avaiable from the resume partition.
368 */
369
370static int enough_swap(unsigned int nr_pages)
371{
372 unsigned int free_swap = count_swap_pages(root_swap, 1);
373
374 pr_debug("swsusp: free swap pages: %u\n", free_swap);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -0700375 return free_swap > nr_pages + PAGES_FOR_IO;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800376}
377
378/**
379 * swsusp_write - Write entire image and metadata.
380 *
381 * It is important _NOT_ to umount filesystems at this point. We want
382 * them synced (in case something goes wrong) but we DO not want to mark
383 * filesystem clean: it is not. (And it does not matter, if we resume
384 * correctly, we'll mark system clean, anyway.)
385 */
386
387int swsusp_write(void)
388{
389 struct swap_map_handle handle;
390 struct snapshot_handle snapshot;
391 struct swsusp_info *header;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800392 int error;
393
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800394 error = swsusp_swap_check();
395 if (error) {
Andrew Morton546e0d22006-09-25 23:32:44 -0700396 printk(KERN_ERR "swsusp: Cannot find swap device, try "
397 "swapon -a.\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800398 return error;
399 }
400 memset(&snapshot, 0, sizeof(struct snapshot_handle));
401 error = snapshot_read_next(&snapshot, PAGE_SIZE);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800402 if (error < PAGE_SIZE) {
403 if (error >= 0)
404 error = -EFAULT;
405
406 goto out;
407 }
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800408 header = (struct swsusp_info *)data_of(snapshot);
409 if (!enough_swap(header->pages)) {
410 printk(KERN_ERR "swsusp: Not enough free swap\n");
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800411 error = -ENOSPC;
412 goto out;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800413 }
414 error = get_swap_writer(&handle);
415 if (!error) {
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800416 sector_t start = handle.cur_swap;
417
Andrew Mortonab954162006-09-25 23:32:42 -0700418 error = swap_write_page(&handle, header, NULL);
Andrew Morton712f4032006-07-10 04:45:00 -0700419 if (!error)
420 error = save_image(&handle, &snapshot,
421 header->pages - 1);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800422
Andrew Morton712f4032006-07-10 04:45:00 -0700423 if (!error) {
424 flush_swap_writer(&handle);
425 printk("S");
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800426 error = mark_swapfiles(start);
Andrew Morton712f4032006-07-10 04:45:00 -0700427 printk("|\n");
428 }
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800429 }
430 if (error)
431 free_all_swap_pages(root_swap, handle.bitmap);
432 release_swap_writer(&handle);
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800433 out:
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800434 swsusp_close();
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800435 return error;
436}
437
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800438/**
439 * The following functions allow us to read data using a swap map
440 * in a file-alike way
441 */
442
443static void release_swap_reader(struct swap_map_handle *handle)
444{
445 if (handle->cur)
446 free_page((unsigned long)handle->cur);
447 handle->cur = NULL;
448}
449
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800450static int get_swap_reader(struct swap_map_handle *handle, sector_t start)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800451{
452 int error;
453
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800454 if (!start)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800455 return -EINVAL;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800456
Rafael J. Wysocki85949122006-12-06 20:34:19 -0800457 handle->cur = (struct swap_map_page *)get_zeroed_page(__GFP_WAIT | __GFP_HIGH);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800458 if (!handle->cur)
459 return -ENOMEM;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800460
461 error = bio_read_page(start, handle->cur, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800462 if (error) {
463 release_swap_reader(handle);
464 return error;
465 }
466 handle->k = 0;
467 return 0;
468}
469
Andrew Morton546e0d22006-09-25 23:32:44 -0700470static int swap_read_page(struct swap_map_handle *handle, void *buf,
471 struct bio **bio_chain)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800472{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800473 sector_t offset;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800474 int error;
475
476 if (!handle->cur)
477 return -EINVAL;
478 offset = handle->cur->entries[handle->k];
479 if (!offset)
480 return -EFAULT;
Andrew Morton546e0d22006-09-25 23:32:44 -0700481 error = bio_read_page(offset, buf, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800482 if (error)
483 return error;
484 if (++handle->k >= MAP_PAGE_ENTRIES) {
Andrew Morton546e0d22006-09-25 23:32:44 -0700485 error = wait_on_bio_chain(bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800486 handle->k = 0;
487 offset = handle->cur->next_swap;
488 if (!offset)
489 release_swap_reader(handle);
Andrew Morton546e0d22006-09-25 23:32:44 -0700490 else if (!error)
491 error = bio_read_page(offset, handle->cur, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800492 }
493 return error;
494}
495
496/**
497 * load_image - load the image using the swap map handle
498 * @handle and the snapshot handle @snapshot
499 * (assume there are @nr_pages pages to load)
500 */
501
502static int load_image(struct swap_map_handle *handle,
503 struct snapshot_handle *snapshot,
Andrew Morton546e0d22006-09-25 23:32:44 -0700504 unsigned int nr_to_read)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800505{
506 unsigned int m;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800507 int error = 0;
Andrew Morton8c002492006-09-25 23:32:43 -0700508 struct timeval start;
509 struct timeval stop;
Andrew Morton546e0d22006-09-25 23:32:44 -0700510 struct bio *bio;
511 int err2;
512 unsigned nr_pages;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800513
Andrew Morton546e0d22006-09-25 23:32:44 -0700514 printk("Loading image data pages (%u pages) ... ", nr_to_read);
515 m = nr_to_read / 100;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800516 if (!m)
517 m = 1;
518 nr_pages = 0;
Andrew Morton546e0d22006-09-25 23:32:44 -0700519 bio = NULL;
Andrew Morton8c002492006-09-25 23:32:43 -0700520 do_gettimeofday(&start);
Andrew Morton546e0d22006-09-25 23:32:44 -0700521 for ( ; ; ) {
522 error = snapshot_write_next(snapshot, PAGE_SIZE);
523 if (error <= 0)
524 break;
525 error = swap_read_page(handle, data_of(*snapshot), &bio);
526 if (error)
527 break;
528 if (snapshot->sync_read)
529 error = wait_on_bio_chain(&bio);
530 if (error)
531 break;
532 if (!(nr_pages % m))
533 printk("\b\b\b\b%3d%%", nr_pages / m);
534 nr_pages++;
535 }
536 err2 = wait_on_bio_chain(&bio);
Andrew Morton8c002492006-09-25 23:32:43 -0700537 do_gettimeofday(&stop);
Andrew Morton546e0d22006-09-25 23:32:44 -0700538 if (!error)
539 error = err2;
Con Kolivase655a252006-03-26 01:37:11 -0800540 if (!error) {
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800541 printk("\b\b\b\bdone\n");
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800542 snapshot_write_finalize(snapshot);
Con Kolivase655a252006-03-26 01:37:11 -0800543 if (!snapshot_image_loaded(snapshot))
544 error = -ENODATA;
545 }
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800546 swsusp_show_speed(&start, &stop, nr_to_read, "Read");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800547 return error;
548}
549
550int swsusp_read(void)
551{
552 int error;
553 struct swap_map_handle handle;
554 struct snapshot_handle snapshot;
555 struct swsusp_info *header;
556
557 if (IS_ERR(resume_bdev)) {
558 pr_debug("swsusp: block device not initialised\n");
559 return PTR_ERR(resume_bdev);
560 }
561
562 memset(&snapshot, 0, sizeof(struct snapshot_handle));
563 error = snapshot_write_next(&snapshot, PAGE_SIZE);
564 if (error < PAGE_SIZE)
565 return error < 0 ? error : -EFAULT;
566 header = (struct swsusp_info *)data_of(snapshot);
567 error = get_swap_reader(&handle, swsusp_header.image);
568 if (!error)
Andrew Morton546e0d22006-09-25 23:32:44 -0700569 error = swap_read_page(&handle, header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800570 if (!error)
571 error = load_image(&handle, &snapshot, header->pages - 1);
572 release_swap_reader(&handle);
573
574 blkdev_put(resume_bdev);
575
576 if (!error)
577 pr_debug("swsusp: Reading resume file was successful\n");
578 else
579 pr_debug("swsusp: Error %d resuming\n", error);
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);
594 memset(&swsusp_header, 0, sizeof(swsusp_header));
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800595 error = bio_read_page(swsusp_resume_block,
596 &swsusp_header, NULL);
597 if (error)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800598 return error;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800599
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800600 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
601 memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
602 /* Reset swap signature now */
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800603 error = bio_write_page(swsusp_resume_block,
604 &swsusp_header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800605 } else {
606 return -EINVAL;
607 }
608 if (error)
609 blkdev_put(resume_bdev);
610 else
611 pr_debug("swsusp: Signature found, resuming\n");
612 } else {
613 error = PTR_ERR(resume_bdev);
614 }
615
616 if (error)
617 pr_debug("swsusp: Error %d check for resume file\n", error);
618
619 return error;
620}
621
622/**
623 * swsusp_close - close swap device.
624 */
625
626void swsusp_close(void)
627{
628 if (IS_ERR(resume_bdev)) {
629 pr_debug("swsusp: block device not initialised\n");
630 return;
631 }
632
633 blkdev_put(resume_bdev);
634}