blob: b8b235cc19d1591077f7cda71175ec2c152ff808 [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>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080015#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>
Andrew Morton546e0d22006-09-25 23:32:44 -070024#include <linux/blkdev.h>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080025#include <linux/swap.h>
26#include <linux/swapops.h>
27#include <linux/pm.h>
28
29#include "power.h"
30
31extern char resume_file[];
32
33#define SWSUSP_SIG "S1SUSPEND"
34
Vivek Goyal1b29c162007-05-02 19:27:07 +020035struct swsusp_header {
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -080036 char reserved[PAGE_SIZE - 20 - sizeof(sector_t)];
37 sector_t image;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080038 char orig_sig[10];
39 char sig[10];
Vivek Goyal1b29c162007-05-02 19:27:07 +020040} __attribute__((packed));
41
42static struct swsusp_header *swsusp_header;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080043
44/*
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -080045 * General things
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080046 */
47
48static unsigned short root_swap = 0xffff;
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -080049static struct block_device *resume_bdev;
50
51/**
52 * submit - submit BIO request.
53 * @rw: READ or WRITE.
54 * @off physical offset of page.
55 * @page: page we're reading or writing.
56 * @bio_chain: list of pending biod (for async reading)
57 *
58 * Straight from the textbook - allocate and initialize the bio.
59 * If we're reading, make sure the page is marked as dirty.
60 * Then submit it and, if @bio_chain == NULL, wait.
61 */
62static int submit(int rw, pgoff_t page_off, struct page *page,
63 struct bio **bio_chain)
64{
65 struct bio *bio;
66
Rafael J. Wysocki85949122006-12-06 20:34:19 -080067 bio = bio_alloc(__GFP_WAIT | __GFP_HIGH, 1);
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -080068 if (!bio)
69 return -ENOMEM;
70 bio->bi_sector = page_off * (PAGE_SIZE >> 9);
71 bio->bi_bdev = resume_bdev;
72 bio->bi_end_io = end_swap_bio_read;
73
74 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
75 printk("swsusp: ERROR: adding page to bio at %ld\n", page_off);
76 bio_put(bio);
77 return -EFAULT;
78 }
79
80 lock_page(page);
81 bio_get(bio);
82
83 if (bio_chain == NULL) {
84 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
85 wait_on_page_locked(page);
86 if (rw == READ)
87 bio_set_pages_dirty(bio);
88 bio_put(bio);
89 } else {
90 if (rw == READ)
91 get_page(page); /* These pages are freed later */
92 bio->bi_private = *bio_chain;
93 *bio_chain = bio;
94 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
95 }
96 return 0;
97}
98
99static int bio_read_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
100{
101 return submit(READ, page_off, virt_to_page(addr), bio_chain);
102}
103
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800104static int bio_write_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800105{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800106 return submit(WRITE, page_off, virt_to_page(addr), bio_chain);
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800107}
108
109static int wait_on_bio_chain(struct bio **bio_chain)
110{
111 struct bio *bio;
112 struct bio *next_bio;
113 int ret = 0;
114
115 if (bio_chain == NULL)
116 return 0;
117
118 bio = *bio_chain;
119 if (bio == NULL)
120 return 0;
121 while (bio) {
122 struct page *page;
123
124 next_bio = bio->bi_private;
125 page = bio->bi_io_vec[0].bv_page;
126 wait_on_page_locked(page);
127 if (!PageUptodate(page) || PageError(page))
128 ret = -EIO;
129 put_page(page);
130 bio_put(bio);
131 bio = next_bio;
132 }
133 *bio_chain = NULL;
134 return ret;
135}
136
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800137/*
138 * Saving part
139 */
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800140
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800141static int mark_swapfiles(sector_t start)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800142{
143 int error;
144
Vivek Goyal1b29c162007-05-02 19:27:07 +0200145 bio_read_page(swsusp_resume_block, swsusp_header, NULL);
146 if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) ||
147 !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) {
148 memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10);
149 memcpy(swsusp_header->sig,SWSUSP_SIG, 10);
150 swsusp_header->image = start;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800151 error = bio_write_page(swsusp_resume_block,
Vivek Goyal1b29c162007-05-02 19:27:07 +0200152 swsusp_header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800153 } else {
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800154 printk(KERN_ERR "swsusp: Swap header not found!\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800155 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{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800167 int res;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800168
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800169 res = swap_type_of(swsusp_resume_device, swsusp_resume_block,
170 &resume_bdev);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800171 if (res < 0)
172 return res;
173
174 root_swap = res;
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800175 res = blkdev_get(resume_bdev, FMODE_WRITE, O_RDWR);
176 if (res)
177 return res;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800178
179 res = set_blocksize(resume_bdev, PAGE_SIZE);
180 if (res < 0)
181 blkdev_put(resume_bdev);
182
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800183 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.
Andrew Mortonab954162006-09-25 23:32:42 -0700190 * @bio_chain: Link the next write BIO here
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800191 */
192
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800193static int write_page(void *buf, sector_t offset, struct bio **bio_chain)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800194{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800195 void *src;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800196
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800197 if (!offset)
198 return -ENOSPC;
Andrew Mortonab954162006-09-25 23:32:42 -0700199
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800200 if (bio_chain) {
Rafael J. Wysocki85949122006-12-06 20:34:19 -0800201 src = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800202 if (src) {
203 memcpy(src, buf, PAGE_SIZE);
204 } else {
205 WARN_ON_ONCE(1);
206 bio_chain = NULL; /* Go synchronous */
207 src = buf;
Andrew Mortonab954162006-09-25 23:32:42 -0700208 }
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800209 } else {
210 src = buf;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800211 }
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800212 return bio_write_page(offset, src, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800213}
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
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800230#define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800231
232struct swap_map_page {
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800233 sector_t entries[MAP_PAGE_ENTRIES];
234 sector_t next_swap;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800235};
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;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800244 sector_t cur_swap;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800245 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;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800253}
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;
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700260 handle->cur_swap = alloc_swapdev_block(root_swap);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800261 if (!handle->cur_swap) {
262 release_swap_writer(handle);
263 return -ENOSPC;
264 }
265 handle->k = 0;
266 return 0;
267}
268
Andrew Mortonab954162006-09-25 23:32:42 -0700269static int swap_write_page(struct swap_map_handle *handle, void *buf,
270 struct bio **bio_chain)
271{
272 int error = 0;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800273 sector_t offset;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800274
275 if (!handle->cur)
276 return -EINVAL;
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700277 offset = alloc_swapdev_block(root_swap);
Andrew Mortonab954162006-09-25 23:32:42 -0700278 error = write_page(buf, offset, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800279 if (error)
280 return error;
281 handle->cur->entries[handle->k++] = offset;
282 if (handle->k >= MAP_PAGE_ENTRIES) {
Andrew Mortonab954162006-09-25 23:32:42 -0700283 error = wait_on_bio_chain(bio_chain);
284 if (error)
285 goto out;
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700286 offset = alloc_swapdev_block(root_swap);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800287 if (!offset)
288 return -ENOSPC;
289 handle->cur->next_swap = offset;
Andrew Mortonab954162006-09-25 23:32:42 -0700290 error = write_page(handle->cur, handle->cur_swap, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800291 if (error)
Andrew Mortonab954162006-09-25 23:32:42 -0700292 goto out;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800293 memset(handle->cur, 0, PAGE_SIZE);
294 handle->cur_swap = offset;
295 handle->k = 0;
296 }
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800297 out:
Andrew Mortonab954162006-09-25 23:32:42 -0700298 return error;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800299}
300
301static int flush_swap_writer(struct swap_map_handle *handle)
302{
303 if (handle->cur && handle->cur_swap)
Andrew Mortonab954162006-09-25 23:32:42 -0700304 return write_page(handle->cur, handle->cur_swap, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800305 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,
Andrew Morton3a4f7572006-09-25 23:32:41 -0700315 unsigned int nr_to_write)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800316{
317 unsigned int m;
318 int ret;
319 int error = 0;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700320 int nr_pages;
Andrew Mortonab954162006-09-25 23:32:42 -0700321 int err2;
322 struct bio *bio;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700323 struct timeval start;
324 struct timeval stop;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800325
Andrew Morton3a4f7572006-09-25 23:32:41 -0700326 printk("Saving image data pages (%u pages) ... ", nr_to_write);
327 m = nr_to_write / 100;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800328 if (!m)
329 m = 1;
330 nr_pages = 0;
Andrew Mortonab954162006-09-25 23:32:42 -0700331 bio = NULL;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700332 do_gettimeofday(&start);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800333 do {
334 ret = snapshot_read_next(snapshot, PAGE_SIZE);
335 if (ret > 0) {
Andrew Mortonab954162006-09-25 23:32:42 -0700336 error = swap_write_page(handle, data_of(*snapshot),
337 &bio);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800338 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);
Andrew Mortonab954162006-09-25 23:32:42 -0700345 err2 = wait_on_bio_chain(&bio);
Andrew Morton3a4f7572006-09-25 23:32:41 -0700346 do_gettimeofday(&stop);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800347 if (!error)
Andrew Mortonab954162006-09-25 23:32:42 -0700348 error = err2;
349 if (!error)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800350 printk("\b\b\b\bdone\n");
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800351 swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800352 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);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -0700367 return free_swap > nr_pages + PAGES_FOR_IO;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800368}
369
370/**
371 * swsusp_write - Write entire image and metadata.
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
379int swsusp_write(void)
380{
381 struct swap_map_handle handle;
382 struct snapshot_handle snapshot;
383 struct swsusp_info *header;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800384 int error;
385
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800386 error = swsusp_swap_check();
387 if (error) {
Andrew Morton546e0d22006-09-25 23:32:44 -0700388 printk(KERN_ERR "swsusp: Cannot find swap device, try "
389 "swapon -a.\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800390 return error;
391 }
392 memset(&snapshot, 0, sizeof(struct snapshot_handle));
393 error = snapshot_read_next(&snapshot, PAGE_SIZE);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800394 if (error < PAGE_SIZE) {
395 if (error >= 0)
396 error = -EFAULT;
397
398 goto out;
399 }
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800400 header = (struct swsusp_info *)data_of(snapshot);
401 if (!enough_swap(header->pages)) {
402 printk(KERN_ERR "swsusp: Not enough free swap\n");
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800403 error = -ENOSPC;
404 goto out;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800405 }
406 error = get_swap_writer(&handle);
407 if (!error) {
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800408 sector_t start = handle.cur_swap;
409
Andrew Mortonab954162006-09-25 23:32:42 -0700410 error = swap_write_page(&handle, header, NULL);
Andrew Morton712f4032006-07-10 04:45:00 -0700411 if (!error)
412 error = save_image(&handle, &snapshot,
413 header->pages - 1);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800414
Andrew Morton712f4032006-07-10 04:45:00 -0700415 if (!error) {
416 flush_swap_writer(&handle);
417 printk("S");
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800418 error = mark_swapfiles(start);
Andrew Morton712f4032006-07-10 04:45:00 -0700419 printk("|\n");
420 }
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800421 }
422 if (error)
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700423 free_all_swap_pages(root_swap);
424
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800425 release_swap_writer(&handle);
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800426 out:
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800427 swsusp_close();
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800428 return error;
429}
430
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800431/**
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
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800443static int get_swap_reader(struct swap_map_handle *handle, sector_t start)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800444{
445 int error;
446
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800447 if (!start)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800448 return -EINVAL;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800449
Rafael J. Wysocki85949122006-12-06 20:34:19 -0800450 handle->cur = (struct swap_map_page *)get_zeroed_page(__GFP_WAIT | __GFP_HIGH);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800451 if (!handle->cur)
452 return -ENOMEM;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800453
454 error = bio_read_page(start, handle->cur, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800455 if (error) {
456 release_swap_reader(handle);
457 return error;
458 }
459 handle->k = 0;
460 return 0;
461}
462
Andrew Morton546e0d22006-09-25 23:32:44 -0700463static int swap_read_page(struct swap_map_handle *handle, void *buf,
464 struct bio **bio_chain)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800465{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800466 sector_t offset;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800467 int error;
468
469 if (!handle->cur)
470 return -EINVAL;
471 offset = handle->cur->entries[handle->k];
472 if (!offset)
473 return -EFAULT;
Andrew Morton546e0d22006-09-25 23:32:44 -0700474 error = bio_read_page(offset, buf, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800475 if (error)
476 return error;
477 if (++handle->k >= MAP_PAGE_ENTRIES) {
Andrew Morton546e0d22006-09-25 23:32:44 -0700478 error = wait_on_bio_chain(bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800479 handle->k = 0;
480 offset = handle->cur->next_swap;
481 if (!offset)
482 release_swap_reader(handle);
Andrew Morton546e0d22006-09-25 23:32:44 -0700483 else if (!error)
484 error = bio_read_page(offset, handle->cur, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800485 }
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,
Andrew Morton546e0d22006-09-25 23:32:44 -0700497 unsigned int nr_to_read)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800498{
499 unsigned int m;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800500 int error = 0;
Andrew Morton8c002492006-09-25 23:32:43 -0700501 struct timeval start;
502 struct timeval stop;
Andrew Morton546e0d22006-09-25 23:32:44 -0700503 struct bio *bio;
504 int err2;
505 unsigned nr_pages;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800506
Andrew Morton546e0d22006-09-25 23:32:44 -0700507 printk("Loading image data pages (%u pages) ... ", nr_to_read);
508 m = nr_to_read / 100;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800509 if (!m)
510 m = 1;
511 nr_pages = 0;
Andrew Morton546e0d22006-09-25 23:32:44 -0700512 bio = NULL;
Andrew Morton8c002492006-09-25 23:32:43 -0700513 do_gettimeofday(&start);
Andrew Morton546e0d22006-09-25 23:32:44 -0700514 for ( ; ; ) {
515 error = snapshot_write_next(snapshot, PAGE_SIZE);
516 if (error <= 0)
517 break;
518 error = swap_read_page(handle, data_of(*snapshot), &bio);
519 if (error)
520 break;
521 if (snapshot->sync_read)
522 error = wait_on_bio_chain(&bio);
523 if (error)
524 break;
525 if (!(nr_pages % m))
526 printk("\b\b\b\b%3d%%", nr_pages / m);
527 nr_pages++;
528 }
529 err2 = wait_on_bio_chain(&bio);
Andrew Morton8c002492006-09-25 23:32:43 -0700530 do_gettimeofday(&stop);
Andrew Morton546e0d22006-09-25 23:32:44 -0700531 if (!error)
532 error = err2;
Con Kolivase655a252006-03-26 01:37:11 -0800533 if (!error) {
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800534 printk("\b\b\b\bdone\n");
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800535 snapshot_write_finalize(snapshot);
Con Kolivase655a252006-03-26 01:37:11 -0800536 if (!snapshot_image_loaded(snapshot))
537 error = -ENODATA;
538 }
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800539 swsusp_show_speed(&start, &stop, nr_to_read, "Read");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800540 return error;
541}
542
543int swsusp_read(void)
544{
545 int error;
546 struct swap_map_handle handle;
547 struct snapshot_handle snapshot;
548 struct swsusp_info *header;
549
550 if (IS_ERR(resume_bdev)) {
551 pr_debug("swsusp: block device not initialised\n");
552 return PTR_ERR(resume_bdev);
553 }
554
555 memset(&snapshot, 0, sizeof(struct snapshot_handle));
556 error = snapshot_write_next(&snapshot, PAGE_SIZE);
557 if (error < PAGE_SIZE)
558 return error < 0 ? error : -EFAULT;
559 header = (struct swsusp_info *)data_of(snapshot);
Vivek Goyal1b29c162007-05-02 19:27:07 +0200560 error = get_swap_reader(&handle, swsusp_header->image);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800561 if (!error)
Andrew Morton546e0d22006-09-25 23:32:44 -0700562 error = swap_read_page(&handle, header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800563 if (!error)
564 error = load_image(&handle, &snapshot, header->pages - 1);
565 release_swap_reader(&handle);
566
567 blkdev_put(resume_bdev);
568
569 if (!error)
570 pr_debug("swsusp: Reading resume file was successful\n");
571 else
572 pr_debug("swsusp: Error %d resuming\n", error);
573 return error;
574}
575
576/**
577 * swsusp_check - Check for swsusp signature in the resume device
578 */
579
580int swsusp_check(void)
581{
582 int error;
583
584 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
585 if (!IS_ERR(resume_bdev)) {
586 set_blocksize(resume_bdev, PAGE_SIZE);
Vivek Goyal1b29c162007-05-02 19:27:07 +0200587 memset(swsusp_header, 0, sizeof(PAGE_SIZE));
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800588 error = bio_read_page(swsusp_resume_block,
Vivek Goyal1b29c162007-05-02 19:27:07 +0200589 swsusp_header, NULL);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800590 if (error)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800591 return error;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800592
Vivek Goyal1b29c162007-05-02 19:27:07 +0200593 if (!memcmp(SWSUSP_SIG, swsusp_header->sig, 10)) {
594 memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800595 /* Reset swap signature now */
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800596 error = bio_write_page(swsusp_resume_block,
Vivek Goyal1b29c162007-05-02 19:27:07 +0200597 swsusp_header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800598 } else {
599 return -EINVAL;
600 }
601 if (error)
602 blkdev_put(resume_bdev);
603 else
604 pr_debug("swsusp: Signature found, resuming\n");
605 } else {
606 error = PTR_ERR(resume_bdev);
607 }
608
609 if (error)
610 pr_debug("swsusp: Error %d check for resume file\n", error);
611
612 return error;
613}
614
615/**
616 * swsusp_close - close swap device.
617 */
618
619void swsusp_close(void)
620{
621 if (IS_ERR(resume_bdev)) {
622 pr_debug("swsusp: block device not initialised\n");
623 return;
624 }
625
626 blkdev_put(resume_bdev);
627}
Vivek Goyal1b29c162007-05-02 19:27:07 +0200628
629static int swsusp_header_init(void)
630{
631 swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
632 if (!swsusp_header)
633 panic("Could not allocate memory for swsusp_header\n");
634 return 0;
635}
636
637core_initcall(swsusp_header_init);