blob: 63e80628a326637ad2adab6eedefbcc2b4ba83ff [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080027
28#include "power.h"
29
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080030#define SWSUSP_SIG "S1SUSPEND"
31
Jiri Slaby51fb3522010-05-01 23:53:02 +020032/*
33 * The swap map is a data structure used for keeping track of each page
34 * written to a swap partition. It consists of many swap_map_page
35 * structures that contain each an array of MAP_PAGE_SIZE swap entries.
36 * These structures are stored on the swap and linked together with the
37 * help of the .next_swap member.
38 *
39 * The swap map is created during suspend. The swap map pages are
40 * allocated and populated one at a time, so we only need one memory
41 * page to set up the entire structure.
42 *
43 * During resume we also only need to use one swap_map_page structure
44 * at a time.
45 */
46
47#define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
48
49struct swap_map_page {
50 sector_t entries[MAP_PAGE_ENTRIES];
51 sector_t next_swap;
52};
53
54/**
55 * The swap_map_handle structure is used for handling swap in
56 * a file-alike way
57 */
58
59struct swap_map_handle {
60 struct swap_map_page *cur;
61 sector_t cur_swap;
62 sector_t first_sector;
63 unsigned int k;
64};
65
Vivek Goyal1b29c162007-05-02 19:27:07 +020066struct swsusp_header {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -070067 char reserved[PAGE_SIZE - 20 - sizeof(sector_t) - sizeof(int)];
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -080068 sector_t image;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -070069 unsigned int flags; /* Flags to pass to the "boot" kernel */
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080070 char orig_sig[10];
71 char sig[10];
Vivek Goyal1b29c162007-05-02 19:27:07 +020072} __attribute__((packed));
73
74static struct swsusp_header *swsusp_header;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080075
Nigel Cunningham0414f2e2009-12-06 16:15:53 +010076/**
77 * The following functions are used for tracing the allocated
78 * swap pages, so that they can be freed in case of an error.
79 */
80
81struct swsusp_extent {
82 struct rb_node node;
83 unsigned long start;
84 unsigned long end;
85};
86
87static struct rb_root swsusp_extents = RB_ROOT;
88
89static int swsusp_extents_insert(unsigned long swap_offset)
90{
91 struct rb_node **new = &(swsusp_extents.rb_node);
92 struct rb_node *parent = NULL;
93 struct swsusp_extent *ext;
94
95 /* Figure out where to put the new node */
96 while (*new) {
97 ext = container_of(*new, struct swsusp_extent, node);
98 parent = *new;
99 if (swap_offset < ext->start) {
100 /* Try to merge */
101 if (swap_offset == ext->start - 1) {
102 ext->start--;
103 return 0;
104 }
105 new = &((*new)->rb_left);
106 } else if (swap_offset > ext->end) {
107 /* Try to merge */
108 if (swap_offset == ext->end + 1) {
109 ext->end++;
110 return 0;
111 }
112 new = &((*new)->rb_right);
113 } else {
114 /* It already is in the tree */
115 return -EINVAL;
116 }
117 }
118 /* Add the new node and rebalance the tree. */
119 ext = kzalloc(sizeof(struct swsusp_extent), GFP_KERNEL);
120 if (!ext)
121 return -ENOMEM;
122
123 ext->start = swap_offset;
124 ext->end = swap_offset;
125 rb_link_node(&ext->node, parent, new);
126 rb_insert_color(&ext->node, &swsusp_extents);
127 return 0;
128}
129
130/**
131 * alloc_swapdev_block - allocate a swap page and register that it has
132 * been allocated, so that it can be freed in case of an error.
133 */
134
135sector_t alloc_swapdev_block(int swap)
136{
137 unsigned long offset;
138
139 offset = swp_offset(get_swap_page_of_type(swap));
140 if (offset) {
141 if (swsusp_extents_insert(offset))
142 swap_free(swp_entry(swap, offset));
143 else
144 return swapdev_block(swap, offset);
145 }
146 return 0;
147}
148
149/**
150 * free_all_swap_pages - free swap pages allocated for saving image data.
151 * It also frees the extents used to register which swap entres had been
152 * allocated.
153 */
154
155void free_all_swap_pages(int swap)
156{
157 struct rb_node *node;
158
159 while ((node = swsusp_extents.rb_node)) {
160 struct swsusp_extent *ext;
161 unsigned long offset;
162
163 ext = container_of(node, struct swsusp_extent, node);
164 rb_erase(node, &swsusp_extents);
165 for (offset = ext->start; offset <= ext->end; offset++)
166 swap_free(swp_entry(swap, offset));
167
168 kfree(ext);
169 }
170}
171
172int swsusp_swap_in_use(void)
173{
174 return (swsusp_extents.rb_node != NULL);
175}
176
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800177/*
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800178 * General things
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800179 */
180
181static unsigned short root_swap = 0xffff;
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200182struct block_device *hib_resume_bdev;
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800183
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800184/*
185 * Saving part
186 */
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800187
Jiri Slaby51fb3522010-05-01 23:53:02 +0200188static int mark_swapfiles(struct swap_map_handle *handle, unsigned int flags)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800189{
190 int error;
191
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200192 hib_bio_read_page(swsusp_resume_block, swsusp_header, NULL);
Vivek Goyal1b29c162007-05-02 19:27:07 +0200193 if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) ||
194 !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) {
195 memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10);
196 memcpy(swsusp_header->sig,SWSUSP_SIG, 10);
Jiri Slaby51fb3522010-05-01 23:53:02 +0200197 swsusp_header->image = handle->first_sector;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700198 swsusp_header->flags = flags;
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200199 error = hib_bio_write_page(swsusp_resume_block,
Vivek Goyal1b29c162007-05-02 19:27:07 +0200200 swsusp_header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800201 } else {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100202 printk(KERN_ERR "PM: Swap header not found!\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800203 error = -ENODEV;
204 }
205 return error;
206}
207
208/**
209 * swsusp_swap_check - check if the resume device is a swap device
210 * and get its index (if so)
211 */
212
213static int swsusp_swap_check(void) /* This is called before saving image */
214{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800215 int res;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800216
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800217 res = swap_type_of(swsusp_resume_device, swsusp_resume_block,
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200218 &hib_resume_bdev);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800219 if (res < 0)
220 return res;
221
222 root_swap = res;
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200223 res = blkdev_get(hib_resume_bdev, FMODE_WRITE);
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800224 if (res)
225 return res;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800226
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200227 res = set_blocksize(hib_resume_bdev, PAGE_SIZE);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800228 if (res < 0)
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200229 blkdev_put(hib_resume_bdev, FMODE_WRITE);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800230
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800231 return res;
232}
233
234/**
235 * write_page - Write one page to given swap location.
236 * @buf: Address we're writing.
237 * @offset: Offset of the swap page we're writing to.
Andrew Mortonab954162006-09-25 23:32:42 -0700238 * @bio_chain: Link the next write BIO here
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800239 */
240
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800241static int write_page(void *buf, sector_t offset, struct bio **bio_chain)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800242{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800243 void *src;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800244
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800245 if (!offset)
246 return -ENOSPC;
Andrew Mortonab954162006-09-25 23:32:42 -0700247
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800248 if (bio_chain) {
Rafael J. Wysocki85949122006-12-06 20:34:19 -0800249 src = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800250 if (src) {
251 memcpy(src, buf, PAGE_SIZE);
252 } else {
253 WARN_ON_ONCE(1);
254 bio_chain = NULL; /* Go synchronous */
255 src = buf;
Andrew Mortonab954162006-09-25 23:32:42 -0700256 }
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800257 } else {
258 src = buf;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800259 }
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200260 return hib_bio_write_page(offset, src, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800261}
262
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800263static void release_swap_writer(struct swap_map_handle *handle)
264{
265 if (handle->cur)
266 free_page((unsigned long)handle->cur);
267 handle->cur = NULL;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800268}
269
270static int get_swap_writer(struct swap_map_handle *handle)
271{
272 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
273 if (!handle->cur)
274 return -ENOMEM;
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700275 handle->cur_swap = alloc_swapdev_block(root_swap);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800276 if (!handle->cur_swap) {
277 release_swap_writer(handle);
278 return -ENOSPC;
279 }
280 handle->k = 0;
Jiri Slaby51fb3522010-05-01 23:53:02 +0200281 handle->first_sector = handle->cur_swap;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800282 return 0;
283}
284
Andrew Mortonab954162006-09-25 23:32:42 -0700285static int swap_write_page(struct swap_map_handle *handle, void *buf,
286 struct bio **bio_chain)
287{
288 int error = 0;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800289 sector_t offset;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800290
291 if (!handle->cur)
292 return -EINVAL;
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700293 offset = alloc_swapdev_block(root_swap);
Andrew Mortonab954162006-09-25 23:32:42 -0700294 error = write_page(buf, offset, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800295 if (error)
296 return error;
297 handle->cur->entries[handle->k++] = offset;
298 if (handle->k >= MAP_PAGE_ENTRIES) {
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200299 error = hib_wait_on_bio_chain(bio_chain);
Andrew Mortonab954162006-09-25 23:32:42 -0700300 if (error)
301 goto out;
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700302 offset = alloc_swapdev_block(root_swap);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800303 if (!offset)
304 return -ENOSPC;
305 handle->cur->next_swap = offset;
Andrew Mortonab954162006-09-25 23:32:42 -0700306 error = write_page(handle->cur, handle->cur_swap, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800307 if (error)
Andrew Mortonab954162006-09-25 23:32:42 -0700308 goto out;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800309 memset(handle->cur, 0, PAGE_SIZE);
310 handle->cur_swap = offset;
311 handle->k = 0;
312 }
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800313 out:
Andrew Mortonab954162006-09-25 23:32:42 -0700314 return error;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800315}
316
317static int flush_swap_writer(struct swap_map_handle *handle)
318{
319 if (handle->cur && handle->cur_swap)
Andrew Mortonab954162006-09-25 23:32:42 -0700320 return write_page(handle->cur, handle->cur_swap, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800321 else
322 return -EINVAL;
323}
324
325/**
326 * save_image - save the suspend image data
327 */
328
329static int save_image(struct swap_map_handle *handle,
330 struct snapshot_handle *snapshot,
Andrew Morton3a4f7572006-09-25 23:32:41 -0700331 unsigned int nr_to_write)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800332{
333 unsigned int m;
334 int ret;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700335 int nr_pages;
Andrew Mortonab954162006-09-25 23:32:42 -0700336 int err2;
337 struct bio *bio;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700338 struct timeval start;
339 struct timeval stop;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800340
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100341 printk(KERN_INFO "PM: Saving image data pages (%u pages) ... ",
342 nr_to_write);
Andrew Morton3a4f7572006-09-25 23:32:41 -0700343 m = nr_to_write / 100;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800344 if (!m)
345 m = 1;
346 nr_pages = 0;
Andrew Mortonab954162006-09-25 23:32:42 -0700347 bio = NULL;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700348 do_gettimeofday(&start);
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100349 while (1) {
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200350 ret = snapshot_read_next(snapshot);
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100351 if (ret <= 0)
352 break;
353 ret = swap_write_page(handle, data_of(*snapshot), &bio);
354 if (ret)
355 break;
356 if (!(nr_pages % m))
Jiri Slaby66d0ae42009-12-06 16:16:24 +0100357 printk(KERN_CONT "\b\b\b\b%3d%%", nr_pages / m);
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100358 nr_pages++;
359 }
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200360 err2 = hib_wait_on_bio_chain(&bio);
Andrew Morton3a4f7572006-09-25 23:32:41 -0700361 do_gettimeofday(&stop);
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100362 if (!ret)
363 ret = err2;
364 if (!ret)
Jiri Slaby66d0ae42009-12-06 16:16:24 +0100365 printk(KERN_CONT "\b\b\b\bdone\n");
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100366 else
Jiri Slaby66d0ae42009-12-06 16:16:24 +0100367 printk(KERN_CONT "\n");
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800368 swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100369 return ret;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800370}
371
372/**
373 * enough_swap - Make sure we have enough swap to save the image.
374 *
375 * Returns TRUE or FALSE after checking the total amount of swap
376 * space avaiable from the resume partition.
377 */
378
379static int enough_swap(unsigned int nr_pages)
380{
381 unsigned int free_swap = count_swap_pages(root_swap, 1);
382
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100383 pr_debug("PM: Free swap pages: %u\n", free_swap);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -0700384 return free_swap > nr_pages + PAGES_FOR_IO;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800385}
386
387/**
388 * swsusp_write - Write entire image and metadata.
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700389 * @flags: flags to pass to the "boot" kernel in the image header
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800390 *
391 * It is important _NOT_ to umount filesystems at this point. We want
392 * them synced (in case something goes wrong) but we DO not want to mark
393 * filesystem clean: it is not. (And it does not matter, if we resume
394 * correctly, we'll mark system clean, anyway.)
395 */
396
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700397int swsusp_write(unsigned int flags)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800398{
399 struct swap_map_handle handle;
400 struct snapshot_handle snapshot;
401 struct swsusp_info *header;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800402 int error;
403
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800404 error = swsusp_swap_check();
405 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100406 printk(KERN_ERR "PM: Cannot find swap device, try "
Andrew Morton546e0d22006-09-25 23:32:44 -0700407 "swapon -a.\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800408 return error;
409 }
410 memset(&snapshot, 0, sizeof(struct snapshot_handle));
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200411 error = snapshot_read_next(&snapshot);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800412 if (error < PAGE_SIZE) {
413 if (error >= 0)
414 error = -EFAULT;
415
416 goto out;
417 }
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800418 header = (struct swsusp_info *)data_of(snapshot);
419 if (!enough_swap(header->pages)) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100420 printk(KERN_ERR "PM: Not enough free swap\n");
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800421 error = -ENOSPC;
422 goto out;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800423 }
424 error = get_swap_writer(&handle);
425 if (!error) {
Andrew Mortonab954162006-09-25 23:32:42 -0700426 error = swap_write_page(&handle, header, NULL);
Andrew Morton712f4032006-07-10 04:45:00 -0700427 if (!error)
428 error = save_image(&handle, &snapshot,
429 header->pages - 1);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800430
Andrew Morton712f4032006-07-10 04:45:00 -0700431 if (!error) {
432 flush_swap_writer(&handle);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100433 printk(KERN_INFO "PM: S");
Jiri Slaby51fb3522010-05-01 23:53:02 +0200434 error = mark_swapfiles(&handle, flags);
Andrew Morton712f4032006-07-10 04:45:00 -0700435 printk("|\n");
436 }
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800437 }
438 if (error)
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700439 free_all_swap_pages(root_swap);
440
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800441 release_swap_writer(&handle);
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800442 out:
Al Viroc2dd0da2007-10-08 13:21:10 -0400443 swsusp_close(FMODE_WRITE);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800444 return error;
445}
446
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800447/**
448 * The following functions allow us to read data using a swap map
449 * in a file-alike way
450 */
451
452static void release_swap_reader(struct swap_map_handle *handle)
453{
454 if (handle->cur)
455 free_page((unsigned long)handle->cur);
456 handle->cur = NULL;
457}
458
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800459static int get_swap_reader(struct swap_map_handle *handle, sector_t start)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800460{
461 int error;
462
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800463 if (!start)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800464 return -EINVAL;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800465
Rafael J. Wysocki85949122006-12-06 20:34:19 -0800466 handle->cur = (struct swap_map_page *)get_zeroed_page(__GFP_WAIT | __GFP_HIGH);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800467 if (!handle->cur)
468 return -ENOMEM;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800469
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200470 error = hib_bio_read_page(start, handle->cur, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800471 if (error) {
472 release_swap_reader(handle);
473 return error;
474 }
475 handle->k = 0;
476 return 0;
477}
478
Andrew Morton546e0d22006-09-25 23:32:44 -0700479static int swap_read_page(struct swap_map_handle *handle, void *buf,
480 struct bio **bio_chain)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800481{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800482 sector_t offset;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800483 int error;
484
485 if (!handle->cur)
486 return -EINVAL;
487 offset = handle->cur->entries[handle->k];
488 if (!offset)
489 return -EFAULT;
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200490 error = hib_bio_read_page(offset, buf, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800491 if (error)
492 return error;
493 if (++handle->k >= MAP_PAGE_ENTRIES) {
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200494 error = hib_wait_on_bio_chain(bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800495 handle->k = 0;
496 offset = handle->cur->next_swap;
497 if (!offset)
498 release_swap_reader(handle);
Andrew Morton546e0d22006-09-25 23:32:44 -0700499 else if (!error)
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200500 error = hib_bio_read_page(offset, handle->cur, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800501 }
502 return error;
503}
504
505/**
506 * load_image - load the image using the swap map handle
507 * @handle and the snapshot handle @snapshot
508 * (assume there are @nr_pages pages to load)
509 */
510
511static int load_image(struct swap_map_handle *handle,
512 struct snapshot_handle *snapshot,
Andrew Morton546e0d22006-09-25 23:32:44 -0700513 unsigned int nr_to_read)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800514{
515 unsigned int m;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800516 int error = 0;
Andrew Morton8c002492006-09-25 23:32:43 -0700517 struct timeval start;
518 struct timeval stop;
Andrew Morton546e0d22006-09-25 23:32:44 -0700519 struct bio *bio;
520 int err2;
521 unsigned nr_pages;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800522
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100523 printk(KERN_INFO "PM: Loading image data pages (%u pages) ... ",
524 nr_to_read);
Andrew Morton546e0d22006-09-25 23:32:44 -0700525 m = nr_to_read / 100;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800526 if (!m)
527 m = 1;
528 nr_pages = 0;
Andrew Morton546e0d22006-09-25 23:32:44 -0700529 bio = NULL;
Andrew Morton8c002492006-09-25 23:32:43 -0700530 do_gettimeofday(&start);
Andrew Morton546e0d22006-09-25 23:32:44 -0700531 for ( ; ; ) {
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200532 error = snapshot_write_next(snapshot);
Andrew Morton546e0d22006-09-25 23:32:44 -0700533 if (error <= 0)
534 break;
535 error = swap_read_page(handle, data_of(*snapshot), &bio);
536 if (error)
537 break;
538 if (snapshot->sync_read)
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200539 error = hib_wait_on_bio_chain(&bio);
Andrew Morton546e0d22006-09-25 23:32:44 -0700540 if (error)
541 break;
542 if (!(nr_pages % m))
543 printk("\b\b\b\b%3d%%", nr_pages / m);
544 nr_pages++;
545 }
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200546 err2 = hib_wait_on_bio_chain(&bio);
Andrew Morton8c002492006-09-25 23:32:43 -0700547 do_gettimeofday(&stop);
Andrew Morton546e0d22006-09-25 23:32:44 -0700548 if (!error)
549 error = err2;
Con Kolivase655a252006-03-26 01:37:11 -0800550 if (!error) {
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800551 printk("\b\b\b\bdone\n");
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800552 snapshot_write_finalize(snapshot);
Con Kolivase655a252006-03-26 01:37:11 -0800553 if (!snapshot_image_loaded(snapshot))
554 error = -ENODATA;
Jiri Slabybf9fd672009-10-28 22:55:42 +0100555 } else
556 printk("\n");
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800557 swsusp_show_speed(&start, &stop, nr_to_read, "Read");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800558 return error;
559}
560
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700561/**
562 * swsusp_read - read the hibernation image.
563 * @flags_p: flags passed by the "frozen" kernel in the image header should
564 * be written into this memeory location
565 */
566
567int swsusp_read(unsigned int *flags_p)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800568{
569 int error;
570 struct swap_map_handle handle;
571 struct snapshot_handle snapshot;
572 struct swsusp_info *header;
573
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700574 *flags_p = swsusp_header->flags;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800575
576 memset(&snapshot, 0, sizeof(struct snapshot_handle));
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200577 error = snapshot_write_next(&snapshot);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800578 if (error < PAGE_SIZE)
579 return error < 0 ? error : -EFAULT;
580 header = (struct swsusp_info *)data_of(snapshot);
Vivek Goyal1b29c162007-05-02 19:27:07 +0200581 error = get_swap_reader(&handle, swsusp_header->image);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800582 if (!error)
Andrew Morton546e0d22006-09-25 23:32:44 -0700583 error = swap_read_page(&handle, header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800584 if (!error)
585 error = load_image(&handle, &snapshot, header->pages - 1);
586 release_swap_reader(&handle);
587
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800588 if (!error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100589 pr_debug("PM: Image successfully loaded\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800590 else
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100591 pr_debug("PM: Error %d resuming\n", error);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800592 return error;
593}
594
595/**
596 * swsusp_check - Check for swsusp signature in the resume device
597 */
598
599int swsusp_check(void)
600{
601 int error;
602
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200603 hib_resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
604 if (!IS_ERR(hib_resume_bdev)) {
605 set_blocksize(hib_resume_bdev, PAGE_SIZE);
OGAWA Hirofumi6373da12007-05-23 13:58:00 -0700606 memset(swsusp_header, 0, PAGE_SIZE);
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200607 error = hib_bio_read_page(swsusp_resume_block,
Vivek Goyal1b29c162007-05-02 19:27:07 +0200608 swsusp_header, NULL);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800609 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200610 goto put;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800611
Vivek Goyal1b29c162007-05-02 19:27:07 +0200612 if (!memcmp(SWSUSP_SIG, swsusp_header->sig, 10)) {
613 memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800614 /* Reset swap signature now */
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200615 error = hib_bio_write_page(swsusp_resume_block,
Vivek Goyal1b29c162007-05-02 19:27:07 +0200616 swsusp_header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800617 } else {
Jiri Slaby76b57e62009-10-07 22:37:35 +0200618 error = -EINVAL;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800619 }
Jiri Slaby76b57e62009-10-07 22:37:35 +0200620
621put:
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800622 if (error)
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200623 blkdev_put(hib_resume_bdev, FMODE_READ);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800624 else
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100625 pr_debug("PM: Signature found, resuming\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800626 } else {
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200627 error = PTR_ERR(hib_resume_bdev);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800628 }
629
630 if (error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100631 pr_debug("PM: Error %d checking image file\n", error);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800632
633 return error;
634}
635
636/**
637 * swsusp_close - close swap device.
638 */
639
Al Viroc2dd0da2007-10-08 13:21:10 -0400640void swsusp_close(fmode_t mode)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800641{
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200642 if (IS_ERR(hib_resume_bdev)) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100643 pr_debug("PM: Image device not initialised\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800644 return;
645 }
646
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200647 blkdev_put(hib_resume_bdev, mode);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800648}
Vivek Goyal1b29c162007-05-02 19:27:07 +0200649
650static int swsusp_header_init(void)
651{
652 swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
653 if (!swsusp_header)
654 panic("Could not allocate memory for swsusp_header\n");
655 return 0;
656}
657
658core_initcall(swsusp_header_init);