blob: 66824d71983a6f9cbf56c3e5c4ac77dc981070e8 [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
Vivek Goyal1b29c162007-05-02 19:27:07 +020032struct swsusp_header {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -070033 char reserved[PAGE_SIZE - 20 - sizeof(sector_t) - sizeof(int)];
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -080034 sector_t image;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -070035 unsigned int flags; /* Flags to pass to the "boot" kernel */
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080036 char orig_sig[10];
37 char sig[10];
Vivek Goyal1b29c162007-05-02 19:27:07 +020038} __attribute__((packed));
39
40static struct swsusp_header *swsusp_header;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080041
Nigel Cunningham0414f2e2009-12-06 16:15:53 +010042/**
43 * The following functions are used for tracing the allocated
44 * swap pages, so that they can be freed in case of an error.
45 */
46
47struct swsusp_extent {
48 struct rb_node node;
49 unsigned long start;
50 unsigned long end;
51};
52
53static struct rb_root swsusp_extents = RB_ROOT;
54
55static int swsusp_extents_insert(unsigned long swap_offset)
56{
57 struct rb_node **new = &(swsusp_extents.rb_node);
58 struct rb_node *parent = NULL;
59 struct swsusp_extent *ext;
60
61 /* Figure out where to put the new node */
62 while (*new) {
63 ext = container_of(*new, struct swsusp_extent, node);
64 parent = *new;
65 if (swap_offset < ext->start) {
66 /* Try to merge */
67 if (swap_offset == ext->start - 1) {
68 ext->start--;
69 return 0;
70 }
71 new = &((*new)->rb_left);
72 } else if (swap_offset > ext->end) {
73 /* Try to merge */
74 if (swap_offset == ext->end + 1) {
75 ext->end++;
76 return 0;
77 }
78 new = &((*new)->rb_right);
79 } else {
80 /* It already is in the tree */
81 return -EINVAL;
82 }
83 }
84 /* Add the new node and rebalance the tree. */
85 ext = kzalloc(sizeof(struct swsusp_extent), GFP_KERNEL);
86 if (!ext)
87 return -ENOMEM;
88
89 ext->start = swap_offset;
90 ext->end = swap_offset;
91 rb_link_node(&ext->node, parent, new);
92 rb_insert_color(&ext->node, &swsusp_extents);
93 return 0;
94}
95
96/**
97 * alloc_swapdev_block - allocate a swap page and register that it has
98 * been allocated, so that it can be freed in case of an error.
99 */
100
101sector_t alloc_swapdev_block(int swap)
102{
103 unsigned long offset;
104
105 offset = swp_offset(get_swap_page_of_type(swap));
106 if (offset) {
107 if (swsusp_extents_insert(offset))
108 swap_free(swp_entry(swap, offset));
109 else
110 return swapdev_block(swap, offset);
111 }
112 return 0;
113}
114
115/**
116 * free_all_swap_pages - free swap pages allocated for saving image data.
117 * It also frees the extents used to register which swap entres had been
118 * allocated.
119 */
120
121void free_all_swap_pages(int swap)
122{
123 struct rb_node *node;
124
125 while ((node = swsusp_extents.rb_node)) {
126 struct swsusp_extent *ext;
127 unsigned long offset;
128
129 ext = container_of(node, struct swsusp_extent, node);
130 rb_erase(node, &swsusp_extents);
131 for (offset = ext->start; offset <= ext->end; offset++)
132 swap_free(swp_entry(swap, offset));
133
134 kfree(ext);
135 }
136}
137
138int swsusp_swap_in_use(void)
139{
140 return (swsusp_extents.rb_node != NULL);
141}
142
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800143/*
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800144 * General things
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800145 */
146
147static unsigned short root_swap = 0xffff;
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800148static struct block_device *resume_bdev;
149
150/**
151 * submit - submit BIO request.
152 * @rw: READ or WRITE.
153 * @off physical offset of page.
154 * @page: page we're reading or writing.
155 * @bio_chain: list of pending biod (for async reading)
156 *
157 * Straight from the textbook - allocate and initialize the bio.
158 * If we're reading, make sure the page is marked as dirty.
159 * Then submit it and, if @bio_chain == NULL, wait.
160 */
161static int submit(int rw, pgoff_t page_off, struct page *page,
162 struct bio **bio_chain)
163{
Jens Axboe93dbb392009-02-16 10:25:40 +0100164 const int bio_rw = rw | (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800165 struct bio *bio;
166
Rafael J. Wysocki85949122006-12-06 20:34:19 -0800167 bio = bio_alloc(__GFP_WAIT | __GFP_HIGH, 1);
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800168 bio->bi_sector = page_off * (PAGE_SIZE >> 9);
169 bio->bi_bdev = resume_bdev;
170 bio->bi_end_io = end_swap_bio_read;
171
172 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100173 printk(KERN_ERR "PM: Adding page to bio failed at %ld\n",
174 page_off);
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800175 bio_put(bio);
176 return -EFAULT;
177 }
178
179 lock_page(page);
180 bio_get(bio);
181
182 if (bio_chain == NULL) {
Jens Axboe93dbb392009-02-16 10:25:40 +0100183 submit_bio(bio_rw, bio);
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800184 wait_on_page_locked(page);
185 if (rw == READ)
186 bio_set_pages_dirty(bio);
187 bio_put(bio);
188 } else {
189 if (rw == READ)
190 get_page(page); /* These pages are freed later */
191 bio->bi_private = *bio_chain;
192 *bio_chain = bio;
Jens Axboe93dbb392009-02-16 10:25:40 +0100193 submit_bio(bio_rw, bio);
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800194 }
195 return 0;
196}
197
198static int bio_read_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
199{
200 return submit(READ, page_off, virt_to_page(addr), bio_chain);
201}
202
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800203static int bio_write_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800204{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800205 return submit(WRITE, page_off, virt_to_page(addr), bio_chain);
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800206}
207
208static int wait_on_bio_chain(struct bio **bio_chain)
209{
210 struct bio *bio;
211 struct bio *next_bio;
212 int ret = 0;
213
214 if (bio_chain == NULL)
215 return 0;
216
217 bio = *bio_chain;
218 if (bio == NULL)
219 return 0;
220 while (bio) {
221 struct page *page;
222
223 next_bio = bio->bi_private;
224 page = bio->bi_io_vec[0].bv_page;
225 wait_on_page_locked(page);
226 if (!PageUptodate(page) || PageError(page))
227 ret = -EIO;
228 put_page(page);
229 bio_put(bio);
230 bio = next_bio;
231 }
232 *bio_chain = NULL;
233 return ret;
234}
235
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800236/*
237 * Saving part
238 */
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800239
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700240static int mark_swapfiles(sector_t start, unsigned int flags)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800241{
242 int error;
243
Vivek Goyal1b29c162007-05-02 19:27:07 +0200244 bio_read_page(swsusp_resume_block, swsusp_header, NULL);
245 if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) ||
246 !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) {
247 memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10);
248 memcpy(swsusp_header->sig,SWSUSP_SIG, 10);
249 swsusp_header->image = start;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700250 swsusp_header->flags = flags;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800251 error = bio_write_page(swsusp_resume_block,
Vivek Goyal1b29c162007-05-02 19:27:07 +0200252 swsusp_header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800253 } else {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100254 printk(KERN_ERR "PM: Swap header not found!\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800255 error = -ENODEV;
256 }
257 return error;
258}
259
260/**
261 * swsusp_swap_check - check if the resume device is a swap device
262 * and get its index (if so)
263 */
264
265static int swsusp_swap_check(void) /* This is called before saving image */
266{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800267 int res;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800268
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800269 res = swap_type_of(swsusp_resume_device, swsusp_resume_block,
270 &resume_bdev);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800271 if (res < 0)
272 return res;
273
274 root_swap = res;
Al Viro572c4892007-10-08 13:24:05 -0400275 res = blkdev_get(resume_bdev, FMODE_WRITE);
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800276 if (res)
277 return res;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800278
279 res = set_blocksize(resume_bdev, PAGE_SIZE);
280 if (res < 0)
Al Viro9a1c3542008-02-22 20:40:24 -0500281 blkdev_put(resume_bdev, FMODE_WRITE);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800282
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800283 return res;
284}
285
286/**
287 * write_page - Write one page to given swap location.
288 * @buf: Address we're writing.
289 * @offset: Offset of the swap page we're writing to.
Andrew Mortonab954162006-09-25 23:32:42 -0700290 * @bio_chain: Link the next write BIO here
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800291 */
292
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800293static int write_page(void *buf, sector_t offset, struct bio **bio_chain)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800294{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800295 void *src;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800296
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800297 if (!offset)
298 return -ENOSPC;
Andrew Mortonab954162006-09-25 23:32:42 -0700299
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800300 if (bio_chain) {
Rafael J. Wysocki85949122006-12-06 20:34:19 -0800301 src = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800302 if (src) {
303 memcpy(src, buf, PAGE_SIZE);
304 } else {
305 WARN_ON_ONCE(1);
306 bio_chain = NULL; /* Go synchronous */
307 src = buf;
Andrew Mortonab954162006-09-25 23:32:42 -0700308 }
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800309 } else {
310 src = buf;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800311 }
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800312 return bio_write_page(offset, src, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800313}
314
315/*
316 * The swap map is a data structure used for keeping track of each page
317 * written to a swap partition. It consists of many swap_map_page
318 * structures that contain each an array of MAP_PAGE_SIZE swap entries.
319 * These structures are stored on the swap and linked together with the
320 * help of the .next_swap member.
321 *
322 * The swap map is created during suspend. The swap map pages are
323 * allocated and populated one at a time, so we only need one memory
324 * page to set up the entire structure.
325 *
326 * During resume we also only need to use one swap_map_page structure
327 * at a time.
328 */
329
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800330#define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800331
332struct swap_map_page {
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800333 sector_t entries[MAP_PAGE_ENTRIES];
334 sector_t next_swap;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800335};
336
337/**
338 * The swap_map_handle structure is used for handling swap in
339 * a file-alike way
340 */
341
342struct swap_map_handle {
343 struct swap_map_page *cur;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800344 sector_t cur_swap;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800345 unsigned int k;
346};
347
348static void release_swap_writer(struct swap_map_handle *handle)
349{
350 if (handle->cur)
351 free_page((unsigned long)handle->cur);
352 handle->cur = NULL;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800353}
354
355static int get_swap_writer(struct swap_map_handle *handle)
356{
357 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
358 if (!handle->cur)
359 return -ENOMEM;
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700360 handle->cur_swap = alloc_swapdev_block(root_swap);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800361 if (!handle->cur_swap) {
362 release_swap_writer(handle);
363 return -ENOSPC;
364 }
365 handle->k = 0;
366 return 0;
367}
368
Andrew Mortonab954162006-09-25 23:32:42 -0700369static int swap_write_page(struct swap_map_handle *handle, void *buf,
370 struct bio **bio_chain)
371{
372 int error = 0;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800373 sector_t offset;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800374
375 if (!handle->cur)
376 return -EINVAL;
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700377 offset = alloc_swapdev_block(root_swap);
Andrew Mortonab954162006-09-25 23:32:42 -0700378 error = write_page(buf, offset, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800379 if (error)
380 return error;
381 handle->cur->entries[handle->k++] = offset;
382 if (handle->k >= MAP_PAGE_ENTRIES) {
Andrew Mortonab954162006-09-25 23:32:42 -0700383 error = wait_on_bio_chain(bio_chain);
384 if (error)
385 goto out;
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700386 offset = alloc_swapdev_block(root_swap);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800387 if (!offset)
388 return -ENOSPC;
389 handle->cur->next_swap = offset;
Andrew Mortonab954162006-09-25 23:32:42 -0700390 error = write_page(handle->cur, handle->cur_swap, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800391 if (error)
Andrew Mortonab954162006-09-25 23:32:42 -0700392 goto out;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800393 memset(handle->cur, 0, PAGE_SIZE);
394 handle->cur_swap = offset;
395 handle->k = 0;
396 }
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800397 out:
Andrew Mortonab954162006-09-25 23:32:42 -0700398 return error;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800399}
400
401static int flush_swap_writer(struct swap_map_handle *handle)
402{
403 if (handle->cur && handle->cur_swap)
Andrew Mortonab954162006-09-25 23:32:42 -0700404 return write_page(handle->cur, handle->cur_swap, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800405 else
406 return -EINVAL;
407}
408
409/**
410 * save_image - save the suspend image data
411 */
412
413static int save_image(struct swap_map_handle *handle,
414 struct snapshot_handle *snapshot,
Andrew Morton3a4f7572006-09-25 23:32:41 -0700415 unsigned int nr_to_write)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800416{
417 unsigned int m;
418 int ret;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700419 int nr_pages;
Andrew Mortonab954162006-09-25 23:32:42 -0700420 int err2;
421 struct bio *bio;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700422 struct timeval start;
423 struct timeval stop;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800424
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100425 printk(KERN_INFO "PM: Saving image data pages (%u pages) ... ",
426 nr_to_write);
Andrew Morton3a4f7572006-09-25 23:32:41 -0700427 m = nr_to_write / 100;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800428 if (!m)
429 m = 1;
430 nr_pages = 0;
Andrew Mortonab954162006-09-25 23:32:42 -0700431 bio = NULL;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700432 do_gettimeofday(&start);
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100433 while (1) {
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800434 ret = snapshot_read_next(snapshot, PAGE_SIZE);
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100435 if (ret <= 0)
436 break;
437 ret = swap_write_page(handle, data_of(*snapshot), &bio);
438 if (ret)
439 break;
440 if (!(nr_pages % m))
Jiri Slaby66d0ae42009-12-06 16:16:24 +0100441 printk(KERN_CONT "\b\b\b\b%3d%%", nr_pages / m);
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100442 nr_pages++;
443 }
Andrew Mortonab954162006-09-25 23:32:42 -0700444 err2 = wait_on_bio_chain(&bio);
Andrew Morton3a4f7572006-09-25 23:32:41 -0700445 do_gettimeofday(&stop);
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100446 if (!ret)
447 ret = err2;
448 if (!ret)
Jiri Slaby66d0ae42009-12-06 16:16:24 +0100449 printk(KERN_CONT "\b\b\b\bdone\n");
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100450 else
Jiri Slaby66d0ae42009-12-06 16:16:24 +0100451 printk(KERN_CONT "\n");
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800452 swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100453 return ret;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800454}
455
456/**
457 * enough_swap - Make sure we have enough swap to save the image.
458 *
459 * Returns TRUE or FALSE after checking the total amount of swap
460 * space avaiable from the resume partition.
461 */
462
463static int enough_swap(unsigned int nr_pages)
464{
465 unsigned int free_swap = count_swap_pages(root_swap, 1);
466
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100467 pr_debug("PM: Free swap pages: %u\n", free_swap);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -0700468 return free_swap > nr_pages + PAGES_FOR_IO;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800469}
470
471/**
472 * swsusp_write - Write entire image and metadata.
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700473 * @flags: flags to pass to the "boot" kernel in the image header
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800474 *
475 * It is important _NOT_ to umount filesystems at this point. We want
476 * them synced (in case something goes wrong) but we DO not want to mark
477 * filesystem clean: it is not. (And it does not matter, if we resume
478 * correctly, we'll mark system clean, anyway.)
479 */
480
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700481int swsusp_write(unsigned int flags)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800482{
483 struct swap_map_handle handle;
484 struct snapshot_handle snapshot;
485 struct swsusp_info *header;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800486 int error;
487
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800488 error = swsusp_swap_check();
489 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100490 printk(KERN_ERR "PM: Cannot find swap device, try "
Andrew Morton546e0d22006-09-25 23:32:44 -0700491 "swapon -a.\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800492 return error;
493 }
494 memset(&snapshot, 0, sizeof(struct snapshot_handle));
495 error = snapshot_read_next(&snapshot, PAGE_SIZE);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800496 if (error < PAGE_SIZE) {
497 if (error >= 0)
498 error = -EFAULT;
499
500 goto out;
501 }
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800502 header = (struct swsusp_info *)data_of(snapshot);
503 if (!enough_swap(header->pages)) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100504 printk(KERN_ERR "PM: Not enough free swap\n");
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800505 error = -ENOSPC;
506 goto out;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800507 }
508 error = get_swap_writer(&handle);
509 if (!error) {
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800510 sector_t start = handle.cur_swap;
511
Andrew Mortonab954162006-09-25 23:32:42 -0700512 error = swap_write_page(&handle, header, NULL);
Andrew Morton712f4032006-07-10 04:45:00 -0700513 if (!error)
514 error = save_image(&handle, &snapshot,
515 header->pages - 1);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800516
Andrew Morton712f4032006-07-10 04:45:00 -0700517 if (!error) {
518 flush_swap_writer(&handle);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100519 printk(KERN_INFO "PM: S");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700520 error = mark_swapfiles(start, flags);
Andrew Morton712f4032006-07-10 04:45:00 -0700521 printk("|\n");
522 }
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800523 }
524 if (error)
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700525 free_all_swap_pages(root_swap);
526
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800527 release_swap_writer(&handle);
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800528 out:
Al Viroc2dd0da2007-10-08 13:21:10 -0400529 swsusp_close(FMODE_WRITE);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800530 return error;
531}
532
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800533/**
534 * The following functions allow us to read data using a swap map
535 * in a file-alike way
536 */
537
538static void release_swap_reader(struct swap_map_handle *handle)
539{
540 if (handle->cur)
541 free_page((unsigned long)handle->cur);
542 handle->cur = NULL;
543}
544
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800545static int get_swap_reader(struct swap_map_handle *handle, sector_t start)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800546{
547 int error;
548
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800549 if (!start)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800550 return -EINVAL;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800551
Rafael J. Wysocki85949122006-12-06 20:34:19 -0800552 handle->cur = (struct swap_map_page *)get_zeroed_page(__GFP_WAIT | __GFP_HIGH);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800553 if (!handle->cur)
554 return -ENOMEM;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800555
556 error = bio_read_page(start, handle->cur, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800557 if (error) {
558 release_swap_reader(handle);
559 return error;
560 }
561 handle->k = 0;
562 return 0;
563}
564
Andrew Morton546e0d22006-09-25 23:32:44 -0700565static int swap_read_page(struct swap_map_handle *handle, void *buf,
566 struct bio **bio_chain)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800567{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800568 sector_t offset;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800569 int error;
570
571 if (!handle->cur)
572 return -EINVAL;
573 offset = handle->cur->entries[handle->k];
574 if (!offset)
575 return -EFAULT;
Andrew Morton546e0d22006-09-25 23:32:44 -0700576 error = bio_read_page(offset, buf, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800577 if (error)
578 return error;
579 if (++handle->k >= MAP_PAGE_ENTRIES) {
Andrew Morton546e0d22006-09-25 23:32:44 -0700580 error = wait_on_bio_chain(bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800581 handle->k = 0;
582 offset = handle->cur->next_swap;
583 if (!offset)
584 release_swap_reader(handle);
Andrew Morton546e0d22006-09-25 23:32:44 -0700585 else if (!error)
586 error = bio_read_page(offset, handle->cur, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800587 }
588 return error;
589}
590
591/**
592 * load_image - load the image using the swap map handle
593 * @handle and the snapshot handle @snapshot
594 * (assume there are @nr_pages pages to load)
595 */
596
597static int load_image(struct swap_map_handle *handle,
598 struct snapshot_handle *snapshot,
Andrew Morton546e0d22006-09-25 23:32:44 -0700599 unsigned int nr_to_read)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800600{
601 unsigned int m;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800602 int error = 0;
Andrew Morton8c002492006-09-25 23:32:43 -0700603 struct timeval start;
604 struct timeval stop;
Andrew Morton546e0d22006-09-25 23:32:44 -0700605 struct bio *bio;
606 int err2;
607 unsigned nr_pages;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800608
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100609 printk(KERN_INFO "PM: Loading image data pages (%u pages) ... ",
610 nr_to_read);
Andrew Morton546e0d22006-09-25 23:32:44 -0700611 m = nr_to_read / 100;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800612 if (!m)
613 m = 1;
614 nr_pages = 0;
Andrew Morton546e0d22006-09-25 23:32:44 -0700615 bio = NULL;
Andrew Morton8c002492006-09-25 23:32:43 -0700616 do_gettimeofday(&start);
Andrew Morton546e0d22006-09-25 23:32:44 -0700617 for ( ; ; ) {
618 error = snapshot_write_next(snapshot, PAGE_SIZE);
619 if (error <= 0)
620 break;
621 error = swap_read_page(handle, data_of(*snapshot), &bio);
622 if (error)
623 break;
624 if (snapshot->sync_read)
625 error = wait_on_bio_chain(&bio);
626 if (error)
627 break;
628 if (!(nr_pages % m))
629 printk("\b\b\b\b%3d%%", nr_pages / m);
630 nr_pages++;
631 }
632 err2 = wait_on_bio_chain(&bio);
Andrew Morton8c002492006-09-25 23:32:43 -0700633 do_gettimeofday(&stop);
Andrew Morton546e0d22006-09-25 23:32:44 -0700634 if (!error)
635 error = err2;
Con Kolivase655a252006-03-26 01:37:11 -0800636 if (!error) {
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800637 printk("\b\b\b\bdone\n");
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800638 snapshot_write_finalize(snapshot);
Con Kolivase655a252006-03-26 01:37:11 -0800639 if (!snapshot_image_loaded(snapshot))
640 error = -ENODATA;
Jiri Slabybf9fd672009-10-28 22:55:42 +0100641 } else
642 printk("\n");
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800643 swsusp_show_speed(&start, &stop, nr_to_read, "Read");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800644 return error;
645}
646
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700647/**
648 * swsusp_read - read the hibernation image.
649 * @flags_p: flags passed by the "frozen" kernel in the image header should
650 * be written into this memeory location
651 */
652
653int swsusp_read(unsigned int *flags_p)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800654{
655 int error;
656 struct swap_map_handle handle;
657 struct snapshot_handle snapshot;
658 struct swsusp_info *header;
659
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700660 *flags_p = swsusp_header->flags;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800661
662 memset(&snapshot, 0, sizeof(struct snapshot_handle));
663 error = snapshot_write_next(&snapshot, PAGE_SIZE);
664 if (error < PAGE_SIZE)
665 return error < 0 ? error : -EFAULT;
666 header = (struct swsusp_info *)data_of(snapshot);
Vivek Goyal1b29c162007-05-02 19:27:07 +0200667 error = get_swap_reader(&handle, swsusp_header->image);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800668 if (!error)
Andrew Morton546e0d22006-09-25 23:32:44 -0700669 error = swap_read_page(&handle, header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800670 if (!error)
671 error = load_image(&handle, &snapshot, header->pages - 1);
672 release_swap_reader(&handle);
673
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800674 if (!error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100675 pr_debug("PM: Image successfully loaded\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800676 else
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100677 pr_debug("PM: Error %d resuming\n", error);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800678 return error;
679}
680
681/**
682 * swsusp_check - Check for swsusp signature in the resume device
683 */
684
685int swsusp_check(void)
686{
687 int error;
688
689 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
690 if (!IS_ERR(resume_bdev)) {
691 set_blocksize(resume_bdev, PAGE_SIZE);
OGAWA Hirofumi6373da12007-05-23 13:58:00 -0700692 memset(swsusp_header, 0, PAGE_SIZE);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800693 error = bio_read_page(swsusp_resume_block,
Vivek Goyal1b29c162007-05-02 19:27:07 +0200694 swsusp_header, NULL);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800695 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200696 goto put;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800697
Vivek Goyal1b29c162007-05-02 19:27:07 +0200698 if (!memcmp(SWSUSP_SIG, swsusp_header->sig, 10)) {
699 memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800700 /* Reset swap signature now */
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800701 error = bio_write_page(swsusp_resume_block,
Vivek Goyal1b29c162007-05-02 19:27:07 +0200702 swsusp_header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800703 } else {
Jiri Slaby76b57e62009-10-07 22:37:35 +0200704 error = -EINVAL;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800705 }
Jiri Slaby76b57e62009-10-07 22:37:35 +0200706
707put:
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800708 if (error)
Al Viro9a1c3542008-02-22 20:40:24 -0500709 blkdev_put(resume_bdev, FMODE_READ);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800710 else
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100711 pr_debug("PM: Signature found, resuming\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800712 } else {
713 error = PTR_ERR(resume_bdev);
714 }
715
716 if (error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100717 pr_debug("PM: Error %d checking image file\n", error);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800718
719 return error;
720}
721
722/**
723 * swsusp_close - close swap device.
724 */
725
Al Viroc2dd0da2007-10-08 13:21:10 -0400726void swsusp_close(fmode_t mode)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800727{
728 if (IS_ERR(resume_bdev)) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100729 pr_debug("PM: Image device not initialised\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800730 return;
731 }
732
Al Viro50c396d2008-11-30 01:47:12 -0500733 blkdev_put(resume_bdev, mode);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800734}
Vivek Goyal1b29c162007-05-02 19:27:07 +0200735
736static int swsusp_header_init(void)
737{
738 swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
739 if (!swsusp_header)
740 panic("Could not allocate memory for swsusp_header\n");
741 return 0;
742}
743
744core_initcall(swsusp_header_init);