blob: afa052b61161407b5691901e846e27df33bfd0e1 [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>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080016#include <linux/delay.h>
17#include <linux/bitops.h>
18#include <linux/genhd.h>
19#include <linux/device.h>
20#include <linux/buffer_head.h>
21#include <linux/bio.h>
Andrew Morton546e0d22006-09-25 23:32:44 -070022#include <linux/blkdev.h>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080023#include <linux/swap.h>
24#include <linux/swapops.h>
25#include <linux/pm.h>
26
27#include "power.h"
28
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080029#define SWSUSP_SIG "S1SUSPEND"
30
Vivek Goyal1b29c162007-05-02 19:27:07 +020031struct swsusp_header {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -070032 char reserved[PAGE_SIZE - 20 - sizeof(sector_t) - sizeof(int)];
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -080033 sector_t image;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -070034 unsigned int flags; /* Flags to pass to the "boot" kernel */
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080035 char orig_sig[10];
36 char sig[10];
Vivek Goyal1b29c162007-05-02 19:27:07 +020037} __attribute__((packed));
38
39static struct swsusp_header *swsusp_header;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080040
41/*
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -080042 * General things
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080043 */
44
45static unsigned short root_swap = 0xffff;
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -080046static struct block_device *resume_bdev;
47
48/**
49 * submit - submit BIO request.
50 * @rw: READ or WRITE.
51 * @off physical offset of page.
52 * @page: page we're reading or writing.
53 * @bio_chain: list of pending biod (for async reading)
54 *
55 * Straight from the textbook - allocate and initialize the bio.
56 * If we're reading, make sure the page is marked as dirty.
57 * Then submit it and, if @bio_chain == NULL, wait.
58 */
59static int submit(int rw, pgoff_t page_off, struct page *page,
60 struct bio **bio_chain)
61{
Jens Axboe93dbb392009-02-16 10:25:40 +010062 const int bio_rw = rw | (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -080063 struct bio *bio;
64
Rafael J. Wysocki85949122006-12-06 20:34:19 -080065 bio = bio_alloc(__GFP_WAIT | __GFP_HIGH, 1);
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -080066 bio->bi_sector = page_off * (PAGE_SIZE >> 9);
67 bio->bi_bdev = resume_bdev;
68 bio->bi_end_io = end_swap_bio_read;
69
70 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +010071 printk(KERN_ERR "PM: Adding page to bio failed at %ld\n",
72 page_off);
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -080073 bio_put(bio);
74 return -EFAULT;
75 }
76
77 lock_page(page);
78 bio_get(bio);
79
80 if (bio_chain == NULL) {
Jens Axboe93dbb392009-02-16 10:25:40 +010081 submit_bio(bio_rw, bio);
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -080082 wait_on_page_locked(page);
83 if (rw == READ)
84 bio_set_pages_dirty(bio);
85 bio_put(bio);
86 } else {
87 if (rw == READ)
88 get_page(page); /* These pages are freed later */
89 bio->bi_private = *bio_chain;
90 *bio_chain = bio;
Jens Axboe93dbb392009-02-16 10:25:40 +010091 submit_bio(bio_rw, bio);
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -080092 }
93 return 0;
94}
95
96static int bio_read_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
97{
98 return submit(READ, page_off, virt_to_page(addr), bio_chain);
99}
100
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800101static int bio_write_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800102{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800103 return submit(WRITE, page_off, virt_to_page(addr), bio_chain);
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800104}
105
106static int wait_on_bio_chain(struct bio **bio_chain)
107{
108 struct bio *bio;
109 struct bio *next_bio;
110 int ret = 0;
111
112 if (bio_chain == NULL)
113 return 0;
114
115 bio = *bio_chain;
116 if (bio == NULL)
117 return 0;
118 while (bio) {
119 struct page *page;
120
121 next_bio = bio->bi_private;
122 page = bio->bi_io_vec[0].bv_page;
123 wait_on_page_locked(page);
124 if (!PageUptodate(page) || PageError(page))
125 ret = -EIO;
126 put_page(page);
127 bio_put(bio);
128 bio = next_bio;
129 }
130 *bio_chain = NULL;
131 return ret;
132}
133
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800134/*
135 * Saving part
136 */
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800137
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700138static int mark_swapfiles(sector_t start, unsigned int flags)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800139{
140 int error;
141
Vivek Goyal1b29c162007-05-02 19:27:07 +0200142 bio_read_page(swsusp_resume_block, swsusp_header, NULL);
143 if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) ||
144 !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) {
145 memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10);
146 memcpy(swsusp_header->sig,SWSUSP_SIG, 10);
147 swsusp_header->image = start;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700148 swsusp_header->flags = flags;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800149 error = bio_write_page(swsusp_resume_block,
Vivek Goyal1b29c162007-05-02 19:27:07 +0200150 swsusp_header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800151 } else {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100152 printk(KERN_ERR "PM: Swap header not found!\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800153 error = -ENODEV;
154 }
155 return error;
156}
157
158/**
159 * swsusp_swap_check - check if the resume device is a swap device
160 * and get its index (if so)
161 */
162
163static int swsusp_swap_check(void) /* This is called before saving image */
164{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800165 int res;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800166
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800167 res = swap_type_of(swsusp_resume_device, swsusp_resume_block,
168 &resume_bdev);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800169 if (res < 0)
170 return res;
171
172 root_swap = res;
Al Viro572c4892007-10-08 13:24:05 -0400173 res = blkdev_get(resume_bdev, FMODE_WRITE);
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800174 if (res)
175 return res;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800176
177 res = set_blocksize(resume_bdev, PAGE_SIZE);
178 if (res < 0)
Al Viro9a1c3542008-02-22 20:40:24 -0500179 blkdev_put(resume_bdev, FMODE_WRITE);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800180
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 unsigned int k;
244};
245
246static void release_swap_writer(struct swap_map_handle *handle)
247{
248 if (handle->cur)
249 free_page((unsigned long)handle->cur);
250 handle->cur = NULL;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800251}
252
253static int get_swap_writer(struct swap_map_handle *handle)
254{
255 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
256 if (!handle->cur)
257 return -ENOMEM;
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700258 handle->cur_swap = alloc_swapdev_block(root_swap);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800259 if (!handle->cur_swap) {
260 release_swap_writer(handle);
261 return -ENOSPC;
262 }
263 handle->k = 0;
264 return 0;
265}
266
Andrew Mortonab954162006-09-25 23:32:42 -0700267static int swap_write_page(struct swap_map_handle *handle, void *buf,
268 struct bio **bio_chain)
269{
270 int error = 0;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800271 sector_t offset;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800272
273 if (!handle->cur)
274 return -EINVAL;
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700275 offset = alloc_swapdev_block(root_swap);
Andrew Mortonab954162006-09-25 23:32:42 -0700276 error = write_page(buf, offset, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800277 if (error)
278 return error;
279 handle->cur->entries[handle->k++] = offset;
280 if (handle->k >= MAP_PAGE_ENTRIES) {
Andrew Mortonab954162006-09-25 23:32:42 -0700281 error = wait_on_bio_chain(bio_chain);
282 if (error)
283 goto out;
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700284 offset = alloc_swapdev_block(root_swap);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800285 if (!offset)
286 return -ENOSPC;
287 handle->cur->next_swap = offset;
Andrew Mortonab954162006-09-25 23:32:42 -0700288 error = write_page(handle->cur, handle->cur_swap, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800289 if (error)
Andrew Mortonab954162006-09-25 23:32:42 -0700290 goto out;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800291 memset(handle->cur, 0, PAGE_SIZE);
292 handle->cur_swap = offset;
293 handle->k = 0;
294 }
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800295 out:
Andrew Mortonab954162006-09-25 23:32:42 -0700296 return error;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800297}
298
299static int flush_swap_writer(struct swap_map_handle *handle)
300{
301 if (handle->cur && handle->cur_swap)
Andrew Mortonab954162006-09-25 23:32:42 -0700302 return write_page(handle->cur, handle->cur_swap, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800303 else
304 return -EINVAL;
305}
306
307/**
308 * save_image - save the suspend image data
309 */
310
311static int save_image(struct swap_map_handle *handle,
312 struct snapshot_handle *snapshot,
Andrew Morton3a4f7572006-09-25 23:32:41 -0700313 unsigned int nr_to_write)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800314{
315 unsigned int m;
316 int ret;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700317 int nr_pages;
Andrew Mortonab954162006-09-25 23:32:42 -0700318 int err2;
319 struct bio *bio;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700320 struct timeval start;
321 struct timeval stop;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800322
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100323 printk(KERN_INFO "PM: Saving image data pages (%u pages) ... ",
324 nr_to_write);
Andrew Morton3a4f7572006-09-25 23:32:41 -0700325 m = nr_to_write / 100;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800326 if (!m)
327 m = 1;
328 nr_pages = 0;
Andrew Mortonab954162006-09-25 23:32:42 -0700329 bio = NULL;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700330 do_gettimeofday(&start);
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100331 while (1) {
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800332 ret = snapshot_read_next(snapshot, PAGE_SIZE);
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100333 if (ret <= 0)
334 break;
335 ret = swap_write_page(handle, data_of(*snapshot), &bio);
336 if (ret)
337 break;
338 if (!(nr_pages % m))
339 printk("\b\b\b\b%3d%%", nr_pages / m);
340 nr_pages++;
341 }
Andrew Mortonab954162006-09-25 23:32:42 -0700342 err2 = wait_on_bio_chain(&bio);
Andrew Morton3a4f7572006-09-25 23:32:41 -0700343 do_gettimeofday(&stop);
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100344 if (!ret)
345 ret = err2;
346 if (!ret)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800347 printk("\b\b\b\bdone\n");
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100348 else
349 printk("\n");
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800350 swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100351 return ret;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800352}
353
354/**
355 * enough_swap - Make sure we have enough swap to save the image.
356 *
357 * Returns TRUE or FALSE after checking the total amount of swap
358 * space avaiable from the resume partition.
359 */
360
361static int enough_swap(unsigned int nr_pages)
362{
363 unsigned int free_swap = count_swap_pages(root_swap, 1);
364
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100365 pr_debug("PM: Free swap pages: %u\n", free_swap);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -0700366 return free_swap > nr_pages + PAGES_FOR_IO;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800367}
368
369/**
370 * swsusp_write - Write entire image and metadata.
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700371 * @flags: flags to pass to the "boot" kernel in the image header
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800372 *
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
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700379int swsusp_write(unsigned int flags)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800380{
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) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100388 printk(KERN_ERR "PM: Cannot find swap device, try "
Andrew Morton546e0d22006-09-25 23:32:44 -0700389 "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)) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100402 printk(KERN_ERR "PM: 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);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100417 printk(KERN_INFO "PM: S");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700418 error = mark_swapfiles(start, flags);
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:
Al Viroc2dd0da2007-10-08 13:21:10 -0400427 swsusp_close(FMODE_WRITE);
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
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100507 printk(KERN_INFO "PM: Loading image data pages (%u pages) ... ",
508 nr_to_read);
Andrew Morton546e0d22006-09-25 23:32:44 -0700509 m = nr_to_read / 100;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800510 if (!m)
511 m = 1;
512 nr_pages = 0;
Andrew Morton546e0d22006-09-25 23:32:44 -0700513 bio = NULL;
Andrew Morton8c002492006-09-25 23:32:43 -0700514 do_gettimeofday(&start);
Andrew Morton546e0d22006-09-25 23:32:44 -0700515 for ( ; ; ) {
516 error = snapshot_write_next(snapshot, PAGE_SIZE);
517 if (error <= 0)
518 break;
519 error = swap_read_page(handle, data_of(*snapshot), &bio);
520 if (error)
521 break;
522 if (snapshot->sync_read)
523 error = wait_on_bio_chain(&bio);
524 if (error)
525 break;
526 if (!(nr_pages % m))
527 printk("\b\b\b\b%3d%%", nr_pages / m);
528 nr_pages++;
529 }
530 err2 = wait_on_bio_chain(&bio);
Andrew Morton8c002492006-09-25 23:32:43 -0700531 do_gettimeofday(&stop);
Andrew Morton546e0d22006-09-25 23:32:44 -0700532 if (!error)
533 error = err2;
Con Kolivase655a252006-03-26 01:37:11 -0800534 if (!error) {
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800535 printk("\b\b\b\bdone\n");
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800536 snapshot_write_finalize(snapshot);
Con Kolivase655a252006-03-26 01:37:11 -0800537 if (!snapshot_image_loaded(snapshot))
538 error = -ENODATA;
539 }
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800540 swsusp_show_speed(&start, &stop, nr_to_read, "Read");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800541 return error;
542}
543
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700544/**
545 * swsusp_read - read the hibernation image.
546 * @flags_p: flags passed by the "frozen" kernel in the image header should
547 * be written into this memeory location
548 */
549
550int swsusp_read(unsigned int *flags_p)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800551{
552 int error;
553 struct swap_map_handle handle;
554 struct snapshot_handle snapshot;
555 struct swsusp_info *header;
556
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700557 *flags_p = swsusp_header->flags;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800558 if (IS_ERR(resume_bdev)) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100559 pr_debug("PM: Image device not initialised\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800560 return PTR_ERR(resume_bdev);
561 }
562
563 memset(&snapshot, 0, sizeof(struct snapshot_handle));
564 error = snapshot_write_next(&snapshot, PAGE_SIZE);
565 if (error < PAGE_SIZE)
566 return error < 0 ? error : -EFAULT;
567 header = (struct swsusp_info *)data_of(snapshot);
Vivek Goyal1b29c162007-05-02 19:27:07 +0200568 error = get_swap_reader(&handle, swsusp_header->image);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800569 if (!error)
Andrew Morton546e0d22006-09-25 23:32:44 -0700570 error = swap_read_page(&handle, header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800571 if (!error)
572 error = load_image(&handle, &snapshot, header->pages - 1);
573 release_swap_reader(&handle);
574
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800575 if (!error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100576 pr_debug("PM: Image successfully loaded\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800577 else
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100578 pr_debug("PM: Error %d resuming\n", error);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800579 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);
OGAWA Hirofumi6373da12007-05-23 13:58:00 -0700593 memset(swsusp_header, 0, PAGE_SIZE);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800594 error = bio_read_page(swsusp_resume_block,
Vivek Goyal1b29c162007-05-02 19:27:07 +0200595 swsusp_header, NULL);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800596 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200597 goto put;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800598
Vivek Goyal1b29c162007-05-02 19:27:07 +0200599 if (!memcmp(SWSUSP_SIG, swsusp_header->sig, 10)) {
600 memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800601 /* Reset swap signature now */
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800602 error = bio_write_page(swsusp_resume_block,
Vivek Goyal1b29c162007-05-02 19:27:07 +0200603 swsusp_header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800604 } else {
Jiri Slaby76b57e62009-10-07 22:37:35 +0200605 error = -EINVAL;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800606 }
Jiri Slaby76b57e62009-10-07 22:37:35 +0200607
608put:
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800609 if (error)
Al Viro9a1c3542008-02-22 20:40:24 -0500610 blkdev_put(resume_bdev, FMODE_READ);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800611 else
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100612 pr_debug("PM: Signature found, resuming\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800613 } else {
614 error = PTR_ERR(resume_bdev);
615 }
616
617 if (error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100618 pr_debug("PM: Error %d checking image file\n", error);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800619
620 return error;
621}
622
623/**
624 * swsusp_close - close swap device.
625 */
626
Al Viroc2dd0da2007-10-08 13:21:10 -0400627void swsusp_close(fmode_t mode)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800628{
629 if (IS_ERR(resume_bdev)) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100630 pr_debug("PM: Image device not initialised\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800631 return;
632 }
633
Al Viro50c396d2008-11-30 01:47:12 -0500634 blkdev_put(resume_bdev, mode);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800635}
Vivek Goyal1b29c162007-05-02 19:27:07 +0200636
637static int swsusp_header_init(void)
638{
639 swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
640 if (!swsusp_header)
641 panic("Could not allocate memory for swsusp_header\n");
642 return 0;
643}
644
645core_initcall(swsusp_header_init);