blob: 7c97c3a0eee393ea1e5e879d84bc8c78a6ecabf2 [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 *
Pavel Macheka2531292010-07-18 14:27:13 +02007 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@ucw.cz>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08008 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
Bojan Smojver9f339ca2010-11-25 23:41:39 +01009 * Copyright (C) 2010 Bojan Smojver <bojan@rexursive.com>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080010 *
11 * This file is released under the GPLv2.
12 *
13 */
14
15#include <linux/module.h>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080016#include <linux/file.h>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080017#include <linux/delay.h>
18#include <linux/bitops.h>
19#include <linux/genhd.h>
20#include <linux/device.h>
21#include <linux/buffer_head.h>
22#include <linux/bio.h>
Andrew Morton546e0d22006-09-25 23:32:44 -070023#include <linux/blkdev.h>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080024#include <linux/swap.h>
25#include <linux/swapops.h>
26#include <linux/pm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Bojan Smojverf996fc92010-09-09 23:06:23 +020028#include <linux/lzo.h>
29#include <linux/vmalloc.h>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080030
31#include "power.h"
32
Rafael J. Wysockibe8cd642010-12-11 21:46:44 +010033#define HIBERNATE_SIG "S1SUSPEND"
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080034
Jiri Slaby51fb3522010-05-01 23:53:02 +020035/*
36 * The swap map is a data structure used for keeping track of each page
37 * written to a swap partition. It consists of many swap_map_page
Cesar Eduardo Barros90133672010-06-07 22:23:12 +020038 * structures that contain each an array of MAP_PAGE_ENTRIES swap entries.
Jiri Slaby51fb3522010-05-01 23:53:02 +020039 * These structures are stored on the swap and linked together with the
40 * help of the .next_swap member.
41 *
42 * The swap map is created during suspend. The swap map pages are
43 * allocated and populated one at a time, so we only need one memory
44 * page to set up the entire structure.
45 *
46 * During resume we also only need to use one swap_map_page structure
47 * at a time.
48 */
49
50#define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
51
52struct swap_map_page {
53 sector_t entries[MAP_PAGE_ENTRIES];
54 sector_t next_swap;
55};
56
57/**
58 * The swap_map_handle structure is used for handling swap in
59 * a file-alike way
60 */
61
62struct swap_map_handle {
63 struct swap_map_page *cur;
64 sector_t cur_swap;
65 sector_t first_sector;
66 unsigned int k;
67};
68
Vivek Goyal1b29c162007-05-02 19:27:07 +020069struct swsusp_header {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -070070 char reserved[PAGE_SIZE - 20 - sizeof(sector_t) - sizeof(int)];
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -080071 sector_t image;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -070072 unsigned int flags; /* Flags to pass to the "boot" kernel */
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080073 char orig_sig[10];
74 char sig[10];
Vivek Goyal1b29c162007-05-02 19:27:07 +020075} __attribute__((packed));
76
77static struct swsusp_header *swsusp_header;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080078
Nigel Cunningham0414f2e2009-12-06 16:15:53 +010079/**
80 * The following functions are used for tracing the allocated
81 * swap pages, so that they can be freed in case of an error.
82 */
83
84struct swsusp_extent {
85 struct rb_node node;
86 unsigned long start;
87 unsigned long end;
88};
89
90static struct rb_root swsusp_extents = RB_ROOT;
91
92static int swsusp_extents_insert(unsigned long swap_offset)
93{
94 struct rb_node **new = &(swsusp_extents.rb_node);
95 struct rb_node *parent = NULL;
96 struct swsusp_extent *ext;
97
98 /* Figure out where to put the new node */
99 while (*new) {
100 ext = container_of(*new, struct swsusp_extent, node);
101 parent = *new;
102 if (swap_offset < ext->start) {
103 /* Try to merge */
104 if (swap_offset == ext->start - 1) {
105 ext->start--;
106 return 0;
107 }
108 new = &((*new)->rb_left);
109 } else if (swap_offset > ext->end) {
110 /* Try to merge */
111 if (swap_offset == ext->end + 1) {
112 ext->end++;
113 return 0;
114 }
115 new = &((*new)->rb_right);
116 } else {
117 /* It already is in the tree */
118 return -EINVAL;
119 }
120 }
121 /* Add the new node and rebalance the tree. */
122 ext = kzalloc(sizeof(struct swsusp_extent), GFP_KERNEL);
123 if (!ext)
124 return -ENOMEM;
125
126 ext->start = swap_offset;
127 ext->end = swap_offset;
128 rb_link_node(&ext->node, parent, new);
129 rb_insert_color(&ext->node, &swsusp_extents);
130 return 0;
131}
132
133/**
134 * alloc_swapdev_block - allocate a swap page and register that it has
135 * been allocated, so that it can be freed in case of an error.
136 */
137
138sector_t alloc_swapdev_block(int swap)
139{
140 unsigned long offset;
141
Hugh Dickins910321e2010-09-09 16:38:07 -0700142 offset = swp_offset(get_swap_page_of_type(swap));
Nigel Cunningham0414f2e2009-12-06 16:15:53 +0100143 if (offset) {
144 if (swsusp_extents_insert(offset))
Hugh Dickins910321e2010-09-09 16:38:07 -0700145 swap_free(swp_entry(swap, offset));
Nigel Cunningham0414f2e2009-12-06 16:15:53 +0100146 else
147 return swapdev_block(swap, offset);
148 }
149 return 0;
150}
151
152/**
153 * free_all_swap_pages - free swap pages allocated for saving image data.
Cesar Eduardo Barros90133672010-06-07 22:23:12 +0200154 * It also frees the extents used to register which swap entries had been
Nigel Cunningham0414f2e2009-12-06 16:15:53 +0100155 * allocated.
156 */
157
158void free_all_swap_pages(int swap)
159{
160 struct rb_node *node;
161
162 while ((node = swsusp_extents.rb_node)) {
163 struct swsusp_extent *ext;
164 unsigned long offset;
165
166 ext = container_of(node, struct swsusp_extent, node);
167 rb_erase(node, &swsusp_extents);
168 for (offset = ext->start; offset <= ext->end; offset++)
Hugh Dickins910321e2010-09-09 16:38:07 -0700169 swap_free(swp_entry(swap, offset));
Nigel Cunningham0414f2e2009-12-06 16:15:53 +0100170
171 kfree(ext);
172 }
173}
174
175int swsusp_swap_in_use(void)
176{
177 return (swsusp_extents.rb_node != NULL);
178}
179
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800180/*
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800181 * General things
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800182 */
183
184static unsigned short root_swap = 0xffff;
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200185struct block_device *hib_resume_bdev;
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800186
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800187/*
188 * Saving part
189 */
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800190
Jiri Slaby51fb3522010-05-01 23:53:02 +0200191static int mark_swapfiles(struct swap_map_handle *handle, unsigned int flags)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800192{
193 int error;
194
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200195 hib_bio_read_page(swsusp_resume_block, swsusp_header, NULL);
Vivek Goyal1b29c162007-05-02 19:27:07 +0200196 if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) ||
197 !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) {
198 memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10);
Rafael J. Wysocki3624eb02010-10-04 22:08:12 +0200199 memcpy(swsusp_header->sig, HIBERNATE_SIG, 10);
Jiri Slaby51fb3522010-05-01 23:53:02 +0200200 swsusp_header->image = handle->first_sector;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700201 swsusp_header->flags = flags;
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200202 error = hib_bio_write_page(swsusp_resume_block,
Vivek Goyal1b29c162007-05-02 19:27:07 +0200203 swsusp_header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800204 } else {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100205 printk(KERN_ERR "PM: Swap header not found!\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800206 error = -ENODEV;
207 }
208 return error;
209}
210
211/**
212 * swsusp_swap_check - check if the resume device is a swap device
213 * and get its index (if so)
Jiri Slaby6f612af2010-05-01 23:54:02 +0200214 *
215 * This is called before saving image
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800216 */
Jiri Slaby6f612af2010-05-01 23:54:02 +0200217static int swsusp_swap_check(void)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800218{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800219 int res;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800220
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800221 res = swap_type_of(swsusp_resume_device, swsusp_resume_block,
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200222 &hib_resume_bdev);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800223 if (res < 0)
224 return res;
225
226 root_swap = res;
Tejun Heoe525fd82010-11-13 11:55:17 +0100227 res = blkdev_get(hib_resume_bdev, FMODE_WRITE, NULL);
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800228 if (res)
229 return res;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800230
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200231 res = set_blocksize(hib_resume_bdev, PAGE_SIZE);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800232 if (res < 0)
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200233 blkdev_put(hib_resume_bdev, FMODE_WRITE);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800234
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800235 return res;
236}
237
238/**
239 * write_page - Write one page to given swap location.
240 * @buf: Address we're writing.
241 * @offset: Offset of the swap page we're writing to.
Andrew Mortonab954162006-09-25 23:32:42 -0700242 * @bio_chain: Link the next write BIO here
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800243 */
244
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800245static int write_page(void *buf, sector_t offset, struct bio **bio_chain)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800246{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800247 void *src;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800248
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800249 if (!offset)
250 return -ENOSPC;
Andrew Mortonab954162006-09-25 23:32:42 -0700251
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800252 if (bio_chain) {
Rafael J. Wysocki85949122006-12-06 20:34:19 -0800253 src = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800254 if (src) {
Jan Beulich3ecb01d2010-10-26 14:22:27 -0700255 copy_page(src, buf);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800256 } else {
257 WARN_ON_ONCE(1);
258 bio_chain = NULL; /* Go synchronous */
259 src = buf;
Andrew Mortonab954162006-09-25 23:32:42 -0700260 }
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800261 } else {
262 src = buf;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800263 }
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200264 return hib_bio_write_page(offset, src, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800265}
266
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800267static void release_swap_writer(struct swap_map_handle *handle)
268{
269 if (handle->cur)
270 free_page((unsigned long)handle->cur);
271 handle->cur = NULL;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800272}
273
274static int get_swap_writer(struct swap_map_handle *handle)
275{
Jiri Slaby6f612af2010-05-01 23:54:02 +0200276 int ret;
277
278 ret = swsusp_swap_check();
279 if (ret) {
280 if (ret != -ENOSPC)
281 printk(KERN_ERR "PM: Cannot find swap device, try "
282 "swapon -a.\n");
283 return ret;
284 }
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800285 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
Jiri Slaby6f612af2010-05-01 23:54:02 +0200286 if (!handle->cur) {
287 ret = -ENOMEM;
288 goto err_close;
289 }
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700290 handle->cur_swap = alloc_swapdev_block(root_swap);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800291 if (!handle->cur_swap) {
Jiri Slaby6f612af2010-05-01 23:54:02 +0200292 ret = -ENOSPC;
293 goto err_rel;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800294 }
295 handle->k = 0;
Jiri Slaby51fb3522010-05-01 23:53:02 +0200296 handle->first_sector = handle->cur_swap;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800297 return 0;
Jiri Slaby6f612af2010-05-01 23:54:02 +0200298err_rel:
299 release_swap_writer(handle);
300err_close:
301 swsusp_close(FMODE_WRITE);
302 return ret;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800303}
304
Andrew Mortonab954162006-09-25 23:32:42 -0700305static int swap_write_page(struct swap_map_handle *handle, void *buf,
306 struct bio **bio_chain)
307{
308 int error = 0;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800309 sector_t offset;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800310
311 if (!handle->cur)
312 return -EINVAL;
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700313 offset = alloc_swapdev_block(root_swap);
Andrew Mortonab954162006-09-25 23:32:42 -0700314 error = write_page(buf, offset, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800315 if (error)
316 return error;
317 handle->cur->entries[handle->k++] = offset;
318 if (handle->k >= MAP_PAGE_ENTRIES) {
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200319 error = hib_wait_on_bio_chain(bio_chain);
Andrew Mortonab954162006-09-25 23:32:42 -0700320 if (error)
321 goto out;
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700322 offset = alloc_swapdev_block(root_swap);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800323 if (!offset)
324 return -ENOSPC;
325 handle->cur->next_swap = offset;
Andrew Mortonab954162006-09-25 23:32:42 -0700326 error = write_page(handle->cur, handle->cur_swap, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800327 if (error)
Andrew Mortonab954162006-09-25 23:32:42 -0700328 goto out;
Jan Beulich3ecb01d2010-10-26 14:22:27 -0700329 clear_page(handle->cur);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800330 handle->cur_swap = offset;
331 handle->k = 0;
332 }
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800333 out:
Andrew Mortonab954162006-09-25 23:32:42 -0700334 return error;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800335}
336
337static int flush_swap_writer(struct swap_map_handle *handle)
338{
339 if (handle->cur && handle->cur_swap)
Andrew Mortonab954162006-09-25 23:32:42 -0700340 return write_page(handle->cur, handle->cur_swap, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800341 else
342 return -EINVAL;
343}
344
Jiri Slaby6f612af2010-05-01 23:54:02 +0200345static int swap_writer_finish(struct swap_map_handle *handle,
346 unsigned int flags, int error)
347{
348 if (!error) {
349 flush_swap_writer(handle);
350 printk(KERN_INFO "PM: S");
351 error = mark_swapfiles(handle, flags);
352 printk("|\n");
353 }
354
355 if (error)
356 free_all_swap_pages(root_swap);
357 release_swap_writer(handle);
358 swsusp_close(FMODE_WRITE);
359
360 return error;
361}
362
Bojan Smojverf996fc92010-09-09 23:06:23 +0200363/* We need to remember how much compressed data we need to read. */
364#define LZO_HEADER sizeof(size_t)
365
366/* Number of pages/bytes we'll compress at one time. */
367#define LZO_UNC_PAGES 32
368#define LZO_UNC_SIZE (LZO_UNC_PAGES * PAGE_SIZE)
369
370/* Number of pages/bytes we need for compressed data (worst case). */
371#define LZO_CMP_PAGES DIV_ROUND_UP(lzo1x_worst_compress(LZO_UNC_SIZE) + \
372 LZO_HEADER, PAGE_SIZE)
373#define LZO_CMP_SIZE (LZO_CMP_PAGES * PAGE_SIZE)
374
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800375/**
376 * save_image - save the suspend image data
377 */
378
379static int save_image(struct swap_map_handle *handle,
380 struct snapshot_handle *snapshot,
Andrew Morton3a4f7572006-09-25 23:32:41 -0700381 unsigned int nr_to_write)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800382{
383 unsigned int m;
384 int ret;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700385 int nr_pages;
Andrew Mortonab954162006-09-25 23:32:42 -0700386 int err2;
387 struct bio *bio;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700388 struct timeval start;
389 struct timeval stop;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800390
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100391 printk(KERN_INFO "PM: Saving image data pages (%u pages) ... ",
392 nr_to_write);
Andrew Morton3a4f7572006-09-25 23:32:41 -0700393 m = nr_to_write / 100;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800394 if (!m)
395 m = 1;
396 nr_pages = 0;
Andrew Mortonab954162006-09-25 23:32:42 -0700397 bio = NULL;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700398 do_gettimeofday(&start);
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100399 while (1) {
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200400 ret = snapshot_read_next(snapshot);
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100401 if (ret <= 0)
402 break;
403 ret = swap_write_page(handle, data_of(*snapshot), &bio);
404 if (ret)
405 break;
406 if (!(nr_pages % m))
Jiri Slaby66d0ae42009-12-06 16:16:24 +0100407 printk(KERN_CONT "\b\b\b\b%3d%%", nr_pages / m);
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100408 nr_pages++;
409 }
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200410 err2 = hib_wait_on_bio_chain(&bio);
Andrew Morton3a4f7572006-09-25 23:32:41 -0700411 do_gettimeofday(&stop);
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100412 if (!ret)
413 ret = err2;
414 if (!ret)
Jiri Slaby66d0ae42009-12-06 16:16:24 +0100415 printk(KERN_CONT "\b\b\b\bdone\n");
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100416 else
Jiri Slaby66d0ae42009-12-06 16:16:24 +0100417 printk(KERN_CONT "\n");
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800418 swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100419 return ret;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800420}
421
Bojan Smojverf996fc92010-09-09 23:06:23 +0200422
423/**
424 * save_image_lzo - Save the suspend image data compressed with LZO.
425 * @handle: Swap mam handle to use for saving the image.
426 * @snapshot: Image to read data from.
427 * @nr_to_write: Number of pages to save.
428 */
429static int save_image_lzo(struct swap_map_handle *handle,
430 struct snapshot_handle *snapshot,
431 unsigned int nr_to_write)
432{
433 unsigned int m;
434 int ret = 0;
435 int nr_pages;
436 int err2;
437 struct bio *bio;
438 struct timeval start;
439 struct timeval stop;
440 size_t off, unc_len, cmp_len;
441 unsigned char *unc, *cmp, *wrk, *page;
442
443 page = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
444 if (!page) {
445 printk(KERN_ERR "PM: Failed to allocate LZO page\n");
446 return -ENOMEM;
447 }
448
449 wrk = vmalloc(LZO1X_1_MEM_COMPRESS);
450 if (!wrk) {
451 printk(KERN_ERR "PM: Failed to allocate LZO workspace\n");
452 free_page((unsigned long)page);
453 return -ENOMEM;
454 }
455
456 unc = vmalloc(LZO_UNC_SIZE);
457 if (!unc) {
458 printk(KERN_ERR "PM: Failed to allocate LZO uncompressed\n");
459 vfree(wrk);
460 free_page((unsigned long)page);
461 return -ENOMEM;
462 }
463
464 cmp = vmalloc(LZO_CMP_SIZE);
465 if (!cmp) {
466 printk(KERN_ERR "PM: Failed to allocate LZO compressed\n");
467 vfree(unc);
468 vfree(wrk);
469 free_page((unsigned long)page);
470 return -ENOMEM;
471 }
472
473 printk(KERN_INFO
474 "PM: Compressing and saving image data (%u pages) ... ",
475 nr_to_write);
476 m = nr_to_write / 100;
477 if (!m)
478 m = 1;
479 nr_pages = 0;
480 bio = NULL;
481 do_gettimeofday(&start);
482 for (;;) {
483 for (off = 0; off < LZO_UNC_SIZE; off += PAGE_SIZE) {
484 ret = snapshot_read_next(snapshot);
485 if (ret < 0)
486 goto out_finish;
487
488 if (!ret)
489 break;
490
491 memcpy(unc + off, data_of(*snapshot), PAGE_SIZE);
492
493 if (!(nr_pages % m))
494 printk(KERN_CONT "\b\b\b\b%3d%%", nr_pages / m);
495 nr_pages++;
496 }
497
498 if (!off)
499 break;
500
501 unc_len = off;
502 ret = lzo1x_1_compress(unc, unc_len,
503 cmp + LZO_HEADER, &cmp_len, wrk);
504 if (ret < 0) {
505 printk(KERN_ERR "PM: LZO compression failed\n");
506 break;
507 }
508
509 if (unlikely(!cmp_len ||
510 cmp_len > lzo1x_worst_compress(unc_len))) {
511 printk(KERN_ERR "PM: Invalid LZO compressed length\n");
512 ret = -1;
513 break;
514 }
515
516 *(size_t *)cmp = cmp_len;
517
518 /*
519 * Given we are writing one page at a time to disk, we copy
520 * that much from the buffer, although the last bit will likely
521 * be smaller than full page. This is OK - we saved the length
522 * of the compressed data, so any garbage at the end will be
523 * discarded when we read it.
524 */
525 for (off = 0; off < LZO_HEADER + cmp_len; off += PAGE_SIZE) {
526 memcpy(page, cmp + off, PAGE_SIZE);
527
528 ret = swap_write_page(handle, page, &bio);
529 if (ret)
530 goto out_finish;
531 }
532 }
533
534out_finish:
535 err2 = hib_wait_on_bio_chain(&bio);
536 do_gettimeofday(&stop);
537 if (!ret)
538 ret = err2;
539 if (!ret)
540 printk(KERN_CONT "\b\b\b\bdone\n");
541 else
542 printk(KERN_CONT "\n");
543 swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
544
545 vfree(cmp);
546 vfree(unc);
547 vfree(wrk);
548 free_page((unsigned long)page);
549
550 return ret;
551}
552
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800553/**
554 * enough_swap - Make sure we have enough swap to save the image.
555 *
556 * Returns TRUE or FALSE after checking the total amount of swap
557 * space avaiable from the resume partition.
558 */
559
Bojan Smojverf996fc92010-09-09 23:06:23 +0200560static int enough_swap(unsigned int nr_pages, unsigned int flags)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800561{
562 unsigned int free_swap = count_swap_pages(root_swap, 1);
Bojan Smojverf996fc92010-09-09 23:06:23 +0200563 unsigned int required;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800564
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100565 pr_debug("PM: Free swap pages: %u\n", free_swap);
Bojan Smojverf996fc92010-09-09 23:06:23 +0200566
567 required = PAGES_FOR_IO + ((flags & SF_NOCOMPRESS_MODE) ?
568 nr_pages : (nr_pages * LZO_CMP_PAGES) / LZO_UNC_PAGES + 1);
569 return free_swap > required;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800570}
571
572/**
573 * swsusp_write - Write entire image and metadata.
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700574 * @flags: flags to pass to the "boot" kernel in the image header
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800575 *
576 * It is important _NOT_ to umount filesystems at this point. We want
577 * them synced (in case something goes wrong) but we DO not want to mark
578 * filesystem clean: it is not. (And it does not matter, if we resume
579 * correctly, we'll mark system clean, anyway.)
580 */
581
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700582int swsusp_write(unsigned int flags)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800583{
584 struct swap_map_handle handle;
585 struct snapshot_handle snapshot;
586 struct swsusp_info *header;
Jiri Slaby6f612af2010-05-01 23:54:02 +0200587 unsigned long pages;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800588 int error;
589
Jiri Slaby6f612af2010-05-01 23:54:02 +0200590 pages = snapshot_get_image_size();
591 error = get_swap_writer(&handle);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800592 if (error) {
Jiri Slaby6f612af2010-05-01 23:54:02 +0200593 printk(KERN_ERR "PM: Cannot get swap writer\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800594 return error;
595 }
Bojan Smojverf996fc92010-09-09 23:06:23 +0200596 if (!enough_swap(pages, flags)) {
Jiri Slaby6f612af2010-05-01 23:54:02 +0200597 printk(KERN_ERR "PM: Not enough free swap\n");
598 error = -ENOSPC;
599 goto out_finish;
600 }
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800601 memset(&snapshot, 0, sizeof(struct snapshot_handle));
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200602 error = snapshot_read_next(&snapshot);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800603 if (error < PAGE_SIZE) {
604 if (error >= 0)
605 error = -EFAULT;
606
Jiri Slaby6f612af2010-05-01 23:54:02 +0200607 goto out_finish;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800608 }
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800609 header = (struct swsusp_info *)data_of(snapshot);
Jiri Slaby6f612af2010-05-01 23:54:02 +0200610 error = swap_write_page(&handle, header, NULL);
Bojan Smojverf996fc92010-09-09 23:06:23 +0200611 if (!error) {
612 error = (flags & SF_NOCOMPRESS_MODE) ?
613 save_image(&handle, &snapshot, pages - 1) :
614 save_image_lzo(&handle, &snapshot, pages - 1);
615 }
Jiri Slaby6f612af2010-05-01 23:54:02 +0200616out_finish:
617 error = swap_writer_finish(&handle, flags, error);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800618 return error;
619}
620
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800621/**
622 * The following functions allow us to read data using a swap map
623 * in a file-alike way
624 */
625
626static void release_swap_reader(struct swap_map_handle *handle)
627{
628 if (handle->cur)
629 free_page((unsigned long)handle->cur);
630 handle->cur = NULL;
631}
632
Jiri Slaby6f612af2010-05-01 23:54:02 +0200633static int get_swap_reader(struct swap_map_handle *handle,
634 unsigned int *flags_p)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800635{
636 int error;
637
Jiri Slaby6f612af2010-05-01 23:54:02 +0200638 *flags_p = swsusp_header->flags;
639
640 if (!swsusp_header->image) /* how can this happen? */
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800641 return -EINVAL;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800642
Rafael J. Wysocki85949122006-12-06 20:34:19 -0800643 handle->cur = (struct swap_map_page *)get_zeroed_page(__GFP_WAIT | __GFP_HIGH);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800644 if (!handle->cur)
645 return -ENOMEM;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800646
Jiri Slaby6f612af2010-05-01 23:54:02 +0200647 error = hib_bio_read_page(swsusp_header->image, handle->cur, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800648 if (error) {
649 release_swap_reader(handle);
650 return error;
651 }
652 handle->k = 0;
653 return 0;
654}
655
Andrew Morton546e0d22006-09-25 23:32:44 -0700656static int swap_read_page(struct swap_map_handle *handle, void *buf,
657 struct bio **bio_chain)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800658{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800659 sector_t offset;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800660 int error;
661
662 if (!handle->cur)
663 return -EINVAL;
664 offset = handle->cur->entries[handle->k];
665 if (!offset)
666 return -EFAULT;
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200667 error = hib_bio_read_page(offset, buf, bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800668 if (error)
669 return error;
670 if (++handle->k >= MAP_PAGE_ENTRIES) {
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200671 error = hib_wait_on_bio_chain(bio_chain);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800672 handle->k = 0;
673 offset = handle->cur->next_swap;
674 if (!offset)
675 release_swap_reader(handle);
Andrew Morton546e0d22006-09-25 23:32:44 -0700676 else if (!error)
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200677 error = hib_bio_read_page(offset, handle->cur, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800678 }
679 return error;
680}
681
Jiri Slaby6f612af2010-05-01 23:54:02 +0200682static int swap_reader_finish(struct swap_map_handle *handle)
683{
684 release_swap_reader(handle);
685
686 return 0;
687}
688
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800689/**
690 * load_image - load the image using the swap map handle
691 * @handle and the snapshot handle @snapshot
692 * (assume there are @nr_pages pages to load)
693 */
694
695static int load_image(struct swap_map_handle *handle,
696 struct snapshot_handle *snapshot,
Andrew Morton546e0d22006-09-25 23:32:44 -0700697 unsigned int nr_to_read)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800698{
699 unsigned int m;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800700 int error = 0;
Andrew Morton8c002492006-09-25 23:32:43 -0700701 struct timeval start;
702 struct timeval stop;
Andrew Morton546e0d22006-09-25 23:32:44 -0700703 struct bio *bio;
704 int err2;
705 unsigned nr_pages;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800706
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100707 printk(KERN_INFO "PM: Loading image data pages (%u pages) ... ",
708 nr_to_read);
Andrew Morton546e0d22006-09-25 23:32:44 -0700709 m = nr_to_read / 100;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800710 if (!m)
711 m = 1;
712 nr_pages = 0;
Andrew Morton546e0d22006-09-25 23:32:44 -0700713 bio = NULL;
Andrew Morton8c002492006-09-25 23:32:43 -0700714 do_gettimeofday(&start);
Andrew Morton546e0d22006-09-25 23:32:44 -0700715 for ( ; ; ) {
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200716 error = snapshot_write_next(snapshot);
Andrew Morton546e0d22006-09-25 23:32:44 -0700717 if (error <= 0)
718 break;
719 error = swap_read_page(handle, data_of(*snapshot), &bio);
720 if (error)
721 break;
722 if (snapshot->sync_read)
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200723 error = hib_wait_on_bio_chain(&bio);
Andrew Morton546e0d22006-09-25 23:32:44 -0700724 if (error)
725 break;
726 if (!(nr_pages % m))
727 printk("\b\b\b\b%3d%%", nr_pages / m);
728 nr_pages++;
729 }
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200730 err2 = hib_wait_on_bio_chain(&bio);
Andrew Morton8c002492006-09-25 23:32:43 -0700731 do_gettimeofday(&stop);
Andrew Morton546e0d22006-09-25 23:32:44 -0700732 if (!error)
733 error = err2;
Con Kolivase655a252006-03-26 01:37:11 -0800734 if (!error) {
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800735 printk("\b\b\b\bdone\n");
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800736 snapshot_write_finalize(snapshot);
Con Kolivase655a252006-03-26 01:37:11 -0800737 if (!snapshot_image_loaded(snapshot))
738 error = -ENODATA;
Jiri Slabybf9fd672009-10-28 22:55:42 +0100739 } else
740 printk("\n");
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800741 swsusp_show_speed(&start, &stop, nr_to_read, "Read");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800742 return error;
743}
744
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700745/**
Bojan Smojverf996fc92010-09-09 23:06:23 +0200746 * load_image_lzo - Load compressed image data and decompress them with LZO.
747 * @handle: Swap map handle to use for loading data.
748 * @snapshot: Image to copy uncompressed data into.
749 * @nr_to_read: Number of pages to load.
750 */
751static int load_image_lzo(struct swap_map_handle *handle,
752 struct snapshot_handle *snapshot,
753 unsigned int nr_to_read)
754{
755 unsigned int m;
756 int error = 0;
Bojan Smojver9f339ca2010-11-25 23:41:39 +0100757 struct bio *bio;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200758 struct timeval start;
759 struct timeval stop;
760 unsigned nr_pages;
Bojan Smojver9f339ca2010-11-25 23:41:39 +0100761 size_t i, off, unc_len, cmp_len;
762 unsigned char *unc, *cmp, *page[LZO_CMP_PAGES];
Bojan Smojverf996fc92010-09-09 23:06:23 +0200763
Bojan Smojver9f339ca2010-11-25 23:41:39 +0100764 for (i = 0; i < LZO_CMP_PAGES; i++) {
765 page[i] = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
766 if (!page[i]) {
767 printk(KERN_ERR "PM: Failed to allocate LZO page\n");
768
769 while (i)
770 free_page((unsigned long)page[--i]);
771
772 return -ENOMEM;
773 }
Bojan Smojverf996fc92010-09-09 23:06:23 +0200774 }
775
776 unc = vmalloc(LZO_UNC_SIZE);
777 if (!unc) {
778 printk(KERN_ERR "PM: Failed to allocate LZO uncompressed\n");
Bojan Smojver9f339ca2010-11-25 23:41:39 +0100779
780 for (i = 0; i < LZO_CMP_PAGES; i++)
781 free_page((unsigned long)page[i]);
782
Bojan Smojverf996fc92010-09-09 23:06:23 +0200783 return -ENOMEM;
784 }
785
786 cmp = vmalloc(LZO_CMP_SIZE);
787 if (!cmp) {
788 printk(KERN_ERR "PM: Failed to allocate LZO compressed\n");
Bojan Smojver9f339ca2010-11-25 23:41:39 +0100789
Bojan Smojverf996fc92010-09-09 23:06:23 +0200790 vfree(unc);
Bojan Smojver9f339ca2010-11-25 23:41:39 +0100791 for (i = 0; i < LZO_CMP_PAGES; i++)
792 free_page((unsigned long)page[i]);
793
Bojan Smojverf996fc92010-09-09 23:06:23 +0200794 return -ENOMEM;
795 }
796
797 printk(KERN_INFO
798 "PM: Loading and decompressing image data (%u pages) ... ",
799 nr_to_read);
800 m = nr_to_read / 100;
801 if (!m)
802 m = 1;
803 nr_pages = 0;
Bojan Smojver9f339ca2010-11-25 23:41:39 +0100804 bio = NULL;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200805 do_gettimeofday(&start);
806
807 error = snapshot_write_next(snapshot);
808 if (error <= 0)
809 goto out_finish;
810
811 for (;;) {
Bojan Smojver9f339ca2010-11-25 23:41:39 +0100812 error = swap_read_page(handle, page[0], NULL); /* sync */
Bojan Smojverf996fc92010-09-09 23:06:23 +0200813 if (error)
814 break;
815
Bojan Smojver9f339ca2010-11-25 23:41:39 +0100816 cmp_len = *(size_t *)page[0];
Bojan Smojverf996fc92010-09-09 23:06:23 +0200817 if (unlikely(!cmp_len ||
818 cmp_len > lzo1x_worst_compress(LZO_UNC_SIZE))) {
819 printk(KERN_ERR "PM: Invalid LZO compressed length\n");
820 error = -1;
821 break;
822 }
823
Bojan Smojver9f339ca2010-11-25 23:41:39 +0100824 for (off = PAGE_SIZE, i = 1;
825 off < LZO_HEADER + cmp_len; off += PAGE_SIZE, i++) {
826 error = swap_read_page(handle, page[i], &bio);
Bojan Smojverf996fc92010-09-09 23:06:23 +0200827 if (error)
828 goto out_finish;
Bojan Smojver9f339ca2010-11-25 23:41:39 +0100829 }
Bojan Smojverf996fc92010-09-09 23:06:23 +0200830
Bojan Smojver9f339ca2010-11-25 23:41:39 +0100831 error = hib_wait_on_bio_chain(&bio); /* need all data now */
832 if (error)
833 goto out_finish;
834
835 for (off = 0, i = 0;
836 off < LZO_HEADER + cmp_len; off += PAGE_SIZE, i++) {
837 memcpy(cmp + off, page[i], PAGE_SIZE);
Bojan Smojverf996fc92010-09-09 23:06:23 +0200838 }
839
840 unc_len = LZO_UNC_SIZE;
841 error = lzo1x_decompress_safe(cmp + LZO_HEADER, cmp_len,
842 unc, &unc_len);
843 if (error < 0) {
844 printk(KERN_ERR "PM: LZO decompression failed\n");
845 break;
846 }
847
848 if (unlikely(!unc_len ||
849 unc_len > LZO_UNC_SIZE ||
850 unc_len & (PAGE_SIZE - 1))) {
851 printk(KERN_ERR "PM: Invalid LZO uncompressed length\n");
852 error = -1;
853 break;
854 }
855
856 for (off = 0; off < unc_len; off += PAGE_SIZE) {
857 memcpy(data_of(*snapshot), unc + off, PAGE_SIZE);
858
859 if (!(nr_pages % m))
860 printk("\b\b\b\b%3d%%", nr_pages / m);
861 nr_pages++;
862
863 error = snapshot_write_next(snapshot);
864 if (error <= 0)
865 goto out_finish;
866 }
867 }
868
869out_finish:
870 do_gettimeofday(&stop);
871 if (!error) {
872 printk("\b\b\b\bdone\n");
873 snapshot_write_finalize(snapshot);
874 if (!snapshot_image_loaded(snapshot))
875 error = -ENODATA;
876 } else
877 printk("\n");
878 swsusp_show_speed(&start, &stop, nr_to_read, "Read");
879
880 vfree(cmp);
881 vfree(unc);
Bojan Smojver9f339ca2010-11-25 23:41:39 +0100882 for (i = 0; i < LZO_CMP_PAGES; i++)
883 free_page((unsigned long)page[i]);
Bojan Smojverf996fc92010-09-09 23:06:23 +0200884
885 return error;
886}
887
888/**
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700889 * swsusp_read - read the hibernation image.
890 * @flags_p: flags passed by the "frozen" kernel in the image header should
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400891 * be written into this memory location
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700892 */
893
894int swsusp_read(unsigned int *flags_p)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800895{
896 int error;
897 struct swap_map_handle handle;
898 struct snapshot_handle snapshot;
899 struct swsusp_info *header;
900
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800901 memset(&snapshot, 0, sizeof(struct snapshot_handle));
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200902 error = snapshot_write_next(&snapshot);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800903 if (error < PAGE_SIZE)
904 return error < 0 ? error : -EFAULT;
905 header = (struct swsusp_info *)data_of(snapshot);
Jiri Slaby6f612af2010-05-01 23:54:02 +0200906 error = get_swap_reader(&handle, flags_p);
907 if (error)
908 goto end;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800909 if (!error)
Andrew Morton546e0d22006-09-25 23:32:44 -0700910 error = swap_read_page(&handle, header, NULL);
Bojan Smojverf996fc92010-09-09 23:06:23 +0200911 if (!error) {
912 error = (*flags_p & SF_NOCOMPRESS_MODE) ?
913 load_image(&handle, &snapshot, header->pages - 1) :
914 load_image_lzo(&handle, &snapshot, header->pages - 1);
915 }
Jiri Slaby6f612af2010-05-01 23:54:02 +0200916 swap_reader_finish(&handle);
917end:
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800918 if (!error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100919 pr_debug("PM: Image successfully loaded\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800920 else
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100921 pr_debug("PM: Error %d resuming\n", error);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800922 return error;
923}
924
925/**
926 * swsusp_check - Check for swsusp signature in the resume device
927 */
928
929int swsusp_check(void)
930{
931 int error;
932
Tejun Heod4d77622010-11-13 11:55:18 +0100933 hib_resume_bdev = blkdev_get_by_dev(swsusp_resume_device,
934 FMODE_READ, NULL);
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200935 if (!IS_ERR(hib_resume_bdev)) {
936 set_blocksize(hib_resume_bdev, PAGE_SIZE);
Jan Beulich3ecb01d2010-10-26 14:22:27 -0700937 clear_page(swsusp_header);
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200938 error = hib_bio_read_page(swsusp_resume_block,
Vivek Goyal1b29c162007-05-02 19:27:07 +0200939 swsusp_header, NULL);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800940 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200941 goto put;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800942
Rafael J. Wysocki3624eb02010-10-04 22:08:12 +0200943 if (!memcmp(HIBERNATE_SIG, swsusp_header->sig, 10)) {
Vivek Goyal1b29c162007-05-02 19:27:07 +0200944 memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800945 /* Reset swap signature now */
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200946 error = hib_bio_write_page(swsusp_resume_block,
Vivek Goyal1b29c162007-05-02 19:27:07 +0200947 swsusp_header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800948 } else {
Jiri Slaby76b57e62009-10-07 22:37:35 +0200949 error = -EINVAL;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800950 }
Jiri Slaby76b57e62009-10-07 22:37:35 +0200951
952put:
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800953 if (error)
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200954 blkdev_put(hib_resume_bdev, FMODE_READ);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800955 else
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200956 pr_debug("PM: Image signature found, resuming\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800957 } else {
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200958 error = PTR_ERR(hib_resume_bdev);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800959 }
960
961 if (error)
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200962 pr_debug("PM: Image not found (code %d)\n", error);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800963
964 return error;
965}
966
967/**
968 * swsusp_close - close swap device.
969 */
970
Al Viroc2dd0da2007-10-08 13:21:10 -0400971void swsusp_close(fmode_t mode)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800972{
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200973 if (IS_ERR(hib_resume_bdev)) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100974 pr_debug("PM: Image device not initialised\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800975 return;
976 }
977
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200978 blkdev_put(hib_resume_bdev, mode);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800979}
Vivek Goyal1b29c162007-05-02 19:27:07 +0200980
981static int swsusp_header_init(void)
982{
983 swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
984 if (!swsusp_header)
985 panic("Could not allocate memory for swsusp_header\n");
986 return 0;
987}
988
989core_initcall(swsusp_header_init);