Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 1 | /* |
| 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. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 15 | #include <linux/file.h> |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 16 | #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 Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 22 | #include <linux/blkdev.h> |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 23 | #include <linux/swap.h> |
| 24 | #include <linux/swapops.h> |
| 25 | #include <linux/pm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 27 | |
| 28 | #include "power.h" |
| 29 | |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 30 | #define SWSUSP_SIG "S1SUSPEND" |
| 31 | |
Jiri Slaby | 51fb352 | 2010-05-01 23:53:02 +0200 | [diff] [blame] | 32 | /* |
| 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 | |
| 49 | struct 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 | |
| 59 | struct 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 Goyal | 1b29c16 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 66 | struct swsusp_header { |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 67 | char reserved[PAGE_SIZE - 20 - sizeof(sector_t) - sizeof(int)]; |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 68 | sector_t image; |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 69 | unsigned int flags; /* Flags to pass to the "boot" kernel */ |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 70 | char orig_sig[10]; |
| 71 | char sig[10]; |
Vivek Goyal | 1b29c16 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 72 | } __attribute__((packed)); |
| 73 | |
| 74 | static struct swsusp_header *swsusp_header; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 75 | |
Nigel Cunningham | 0414f2e | 2009-12-06 16:15:53 +0100 | [diff] [blame] | 76 | /** |
| 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 | |
| 81 | struct swsusp_extent { |
| 82 | struct rb_node node; |
| 83 | unsigned long start; |
| 84 | unsigned long end; |
| 85 | }; |
| 86 | |
| 87 | static struct rb_root swsusp_extents = RB_ROOT; |
| 88 | |
| 89 | static 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 | |
| 135 | sector_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 | |
| 155 | void 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 | |
| 172 | int swsusp_swap_in_use(void) |
| 173 | { |
| 174 | return (swsusp_extents.rb_node != NULL); |
| 175 | } |
| 176 | |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 177 | /* |
Rafael J. Wysocki | 3fc6b34 | 2006-12-06 20:34:09 -0800 | [diff] [blame] | 178 | * General things |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 179 | */ |
| 180 | |
| 181 | static unsigned short root_swap = 0xffff; |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 182 | struct block_device *hib_resume_bdev; |
Rafael J. Wysocki | 3fc6b34 | 2006-12-06 20:34:09 -0800 | [diff] [blame] | 183 | |
Rafael J. Wysocki | 3fc6b34 | 2006-12-06 20:34:09 -0800 | [diff] [blame] | 184 | /* |
| 185 | * Saving part |
| 186 | */ |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 187 | |
Jiri Slaby | 51fb352 | 2010-05-01 23:53:02 +0200 | [diff] [blame] | 188 | static int mark_swapfiles(struct swap_map_handle *handle, unsigned int flags) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 189 | { |
| 190 | int error; |
| 191 | |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 192 | hib_bio_read_page(swsusp_resume_block, swsusp_header, NULL); |
Vivek Goyal | 1b29c16 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 193 | 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 Slaby | 51fb352 | 2010-05-01 23:53:02 +0200 | [diff] [blame] | 197 | swsusp_header->image = handle->first_sector; |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 198 | swsusp_header->flags = flags; |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 199 | error = hib_bio_write_page(swsusp_resume_block, |
Vivek Goyal | 1b29c16 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 200 | swsusp_header, NULL); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 201 | } else { |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 202 | printk(KERN_ERR "PM: Swap header not found!\n"); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 203 | 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) |
Jiri Slaby | 6f612af | 2010-05-01 23:54:02 +0200 | [diff] [blame] | 211 | * |
| 212 | * This is called before saving image |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 213 | */ |
Jiri Slaby | 6f612af | 2010-05-01 23:54:02 +0200 | [diff] [blame] | 214 | static int swsusp_swap_check(void) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 215 | { |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 216 | int res; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 217 | |
Rafael J. Wysocki | 7bf2368 | 2007-01-05 16:36:28 -0800 | [diff] [blame] | 218 | res = swap_type_of(swsusp_resume_device, swsusp_resume_block, |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 219 | &hib_resume_bdev); |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 220 | if (res < 0) |
| 221 | return res; |
| 222 | |
| 223 | root_swap = res; |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 224 | res = blkdev_get(hib_resume_bdev, FMODE_WRITE); |
Rafael J. Wysocki | 7bf2368 | 2007-01-05 16:36:28 -0800 | [diff] [blame] | 225 | if (res) |
| 226 | return res; |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 227 | |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 228 | res = set_blocksize(hib_resume_bdev, PAGE_SIZE); |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 229 | if (res < 0) |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 230 | blkdev_put(hib_resume_bdev, FMODE_WRITE); |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 231 | |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 232 | return res; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * write_page - Write one page to given swap location. |
| 237 | * @buf: Address we're writing. |
| 238 | * @offset: Offset of the swap page we're writing to. |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 239 | * @bio_chain: Link the next write BIO here |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 240 | */ |
| 241 | |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 242 | static int write_page(void *buf, sector_t offset, struct bio **bio_chain) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 243 | { |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 244 | void *src; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 245 | |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 246 | if (!offset) |
| 247 | return -ENOSPC; |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 248 | |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 249 | if (bio_chain) { |
Rafael J. Wysocki | 8594912 | 2006-12-06 20:34:19 -0800 | [diff] [blame] | 250 | src = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH); |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 251 | if (src) { |
| 252 | memcpy(src, buf, PAGE_SIZE); |
| 253 | } else { |
| 254 | WARN_ON_ONCE(1); |
| 255 | bio_chain = NULL; /* Go synchronous */ |
| 256 | src = buf; |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 257 | } |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 258 | } else { |
| 259 | src = buf; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 260 | } |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 261 | return hib_bio_write_page(offset, src, bio_chain); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 264 | static void release_swap_writer(struct swap_map_handle *handle) |
| 265 | { |
| 266 | if (handle->cur) |
| 267 | free_page((unsigned long)handle->cur); |
| 268 | handle->cur = NULL; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static int get_swap_writer(struct swap_map_handle *handle) |
| 272 | { |
Jiri Slaby | 6f612af | 2010-05-01 23:54:02 +0200 | [diff] [blame] | 273 | int ret; |
| 274 | |
| 275 | ret = swsusp_swap_check(); |
| 276 | if (ret) { |
| 277 | if (ret != -ENOSPC) |
| 278 | printk(KERN_ERR "PM: Cannot find swap device, try " |
| 279 | "swapon -a.\n"); |
| 280 | return ret; |
| 281 | } |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 282 | handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL); |
Jiri Slaby | 6f612af | 2010-05-01 23:54:02 +0200 | [diff] [blame] | 283 | if (!handle->cur) { |
| 284 | ret = -ENOMEM; |
| 285 | goto err_close; |
| 286 | } |
Rafael J. Wysocki | d1d241c | 2007-05-06 14:50:47 -0700 | [diff] [blame] | 287 | handle->cur_swap = alloc_swapdev_block(root_swap); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 288 | if (!handle->cur_swap) { |
Jiri Slaby | 6f612af | 2010-05-01 23:54:02 +0200 | [diff] [blame] | 289 | ret = -ENOSPC; |
| 290 | goto err_rel; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 291 | } |
| 292 | handle->k = 0; |
Jiri Slaby | 51fb352 | 2010-05-01 23:53:02 +0200 | [diff] [blame] | 293 | handle->first_sector = handle->cur_swap; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 294 | return 0; |
Jiri Slaby | 6f612af | 2010-05-01 23:54:02 +0200 | [diff] [blame] | 295 | err_rel: |
| 296 | release_swap_writer(handle); |
| 297 | err_close: |
| 298 | swsusp_close(FMODE_WRITE); |
| 299 | return ret; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 302 | static int swap_write_page(struct swap_map_handle *handle, void *buf, |
| 303 | struct bio **bio_chain) |
| 304 | { |
| 305 | int error = 0; |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 306 | sector_t offset; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 307 | |
| 308 | if (!handle->cur) |
| 309 | return -EINVAL; |
Rafael J. Wysocki | d1d241c | 2007-05-06 14:50:47 -0700 | [diff] [blame] | 310 | offset = alloc_swapdev_block(root_swap); |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 311 | error = write_page(buf, offset, bio_chain); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 312 | if (error) |
| 313 | return error; |
| 314 | handle->cur->entries[handle->k++] = offset; |
| 315 | if (handle->k >= MAP_PAGE_ENTRIES) { |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 316 | error = hib_wait_on_bio_chain(bio_chain); |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 317 | if (error) |
| 318 | goto out; |
Rafael J. Wysocki | d1d241c | 2007-05-06 14:50:47 -0700 | [diff] [blame] | 319 | offset = alloc_swapdev_block(root_swap); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 320 | if (!offset) |
| 321 | return -ENOSPC; |
| 322 | handle->cur->next_swap = offset; |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 323 | error = write_page(handle->cur, handle->cur_swap, NULL); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 324 | if (error) |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 325 | goto out; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 326 | memset(handle->cur, 0, PAGE_SIZE); |
| 327 | handle->cur_swap = offset; |
| 328 | handle->k = 0; |
| 329 | } |
Rafael J. Wysocki | 59a4933 | 2006-12-06 20:34:44 -0800 | [diff] [blame] | 330 | out: |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 331 | return error; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | static int flush_swap_writer(struct swap_map_handle *handle) |
| 335 | { |
| 336 | if (handle->cur && handle->cur_swap) |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 337 | return write_page(handle->cur, handle->cur_swap, NULL); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 338 | else |
| 339 | return -EINVAL; |
| 340 | } |
| 341 | |
Jiri Slaby | 6f612af | 2010-05-01 23:54:02 +0200 | [diff] [blame] | 342 | static int swap_writer_finish(struct swap_map_handle *handle, |
| 343 | unsigned int flags, int error) |
| 344 | { |
| 345 | if (!error) { |
| 346 | flush_swap_writer(handle); |
| 347 | printk(KERN_INFO "PM: S"); |
| 348 | error = mark_swapfiles(handle, flags); |
| 349 | printk("|\n"); |
| 350 | } |
| 351 | |
| 352 | if (error) |
| 353 | free_all_swap_pages(root_swap); |
| 354 | release_swap_writer(handle); |
| 355 | swsusp_close(FMODE_WRITE); |
| 356 | |
| 357 | return error; |
| 358 | } |
| 359 | |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 360 | /** |
| 361 | * save_image - save the suspend image data |
| 362 | */ |
| 363 | |
| 364 | static int save_image(struct swap_map_handle *handle, |
| 365 | struct snapshot_handle *snapshot, |
Andrew Morton | 3a4f757 | 2006-09-25 23:32:41 -0700 | [diff] [blame] | 366 | unsigned int nr_to_write) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 367 | { |
| 368 | unsigned int m; |
| 369 | int ret; |
Andrew Morton | 3a4f757 | 2006-09-25 23:32:41 -0700 | [diff] [blame] | 370 | int nr_pages; |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 371 | int err2; |
| 372 | struct bio *bio; |
Andrew Morton | 3a4f757 | 2006-09-25 23:32:41 -0700 | [diff] [blame] | 373 | struct timeval start; |
| 374 | struct timeval stop; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 375 | |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 376 | printk(KERN_INFO "PM: Saving image data pages (%u pages) ... ", |
| 377 | nr_to_write); |
Andrew Morton | 3a4f757 | 2006-09-25 23:32:41 -0700 | [diff] [blame] | 378 | m = nr_to_write / 100; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 379 | if (!m) |
| 380 | m = 1; |
| 381 | nr_pages = 0; |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 382 | bio = NULL; |
Andrew Morton | 3a4f757 | 2006-09-25 23:32:41 -0700 | [diff] [blame] | 383 | do_gettimeofday(&start); |
Jiri Slaby | 4ff277f | 2009-10-28 22:55:33 +0100 | [diff] [blame] | 384 | while (1) { |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 385 | ret = snapshot_read_next(snapshot); |
Jiri Slaby | 4ff277f | 2009-10-28 22:55:33 +0100 | [diff] [blame] | 386 | if (ret <= 0) |
| 387 | break; |
| 388 | ret = swap_write_page(handle, data_of(*snapshot), &bio); |
| 389 | if (ret) |
| 390 | break; |
| 391 | if (!(nr_pages % m)) |
Jiri Slaby | 66d0ae4 | 2009-12-06 16:16:24 +0100 | [diff] [blame] | 392 | printk(KERN_CONT "\b\b\b\b%3d%%", nr_pages / m); |
Jiri Slaby | 4ff277f | 2009-10-28 22:55:33 +0100 | [diff] [blame] | 393 | nr_pages++; |
| 394 | } |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 395 | err2 = hib_wait_on_bio_chain(&bio); |
Andrew Morton | 3a4f757 | 2006-09-25 23:32:41 -0700 | [diff] [blame] | 396 | do_gettimeofday(&stop); |
Jiri Slaby | 4ff277f | 2009-10-28 22:55:33 +0100 | [diff] [blame] | 397 | if (!ret) |
| 398 | ret = err2; |
| 399 | if (!ret) |
Jiri Slaby | 66d0ae4 | 2009-12-06 16:16:24 +0100 | [diff] [blame] | 400 | printk(KERN_CONT "\b\b\b\bdone\n"); |
Jiri Slaby | 4ff277f | 2009-10-28 22:55:33 +0100 | [diff] [blame] | 401 | else |
Jiri Slaby | 66d0ae4 | 2009-12-06 16:16:24 +0100 | [diff] [blame] | 402 | printk(KERN_CONT "\n"); |
Rafael J. Wysocki | 0d3a9ab | 2006-12-06 20:34:32 -0800 | [diff] [blame] | 403 | swsusp_show_speed(&start, &stop, nr_to_write, "Wrote"); |
Jiri Slaby | 4ff277f | 2009-10-28 22:55:33 +0100 | [diff] [blame] | 404 | return ret; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /** |
| 408 | * enough_swap - Make sure we have enough swap to save the image. |
| 409 | * |
| 410 | * Returns TRUE or FALSE after checking the total amount of swap |
| 411 | * space avaiable from the resume partition. |
| 412 | */ |
| 413 | |
| 414 | static int enough_swap(unsigned int nr_pages) |
| 415 | { |
| 416 | unsigned int free_swap = count_swap_pages(root_swap, 1); |
| 417 | |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 418 | pr_debug("PM: Free swap pages: %u\n", free_swap); |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 419 | return free_swap > nr_pages + PAGES_FOR_IO; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /** |
| 423 | * swsusp_write - Write entire image and metadata. |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 424 | * @flags: flags to pass to the "boot" kernel in the image header |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 425 | * |
| 426 | * It is important _NOT_ to umount filesystems at this point. We want |
| 427 | * them synced (in case something goes wrong) but we DO not want to mark |
| 428 | * filesystem clean: it is not. (And it does not matter, if we resume |
| 429 | * correctly, we'll mark system clean, anyway.) |
| 430 | */ |
| 431 | |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 432 | int swsusp_write(unsigned int flags) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 433 | { |
| 434 | struct swap_map_handle handle; |
| 435 | struct snapshot_handle snapshot; |
| 436 | struct swsusp_info *header; |
Jiri Slaby | 6f612af | 2010-05-01 23:54:02 +0200 | [diff] [blame] | 437 | unsigned long pages; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 438 | int error; |
| 439 | |
Jiri Slaby | 6f612af | 2010-05-01 23:54:02 +0200 | [diff] [blame] | 440 | pages = snapshot_get_image_size(); |
| 441 | error = get_swap_writer(&handle); |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 442 | if (error) { |
Jiri Slaby | 6f612af | 2010-05-01 23:54:02 +0200 | [diff] [blame] | 443 | printk(KERN_ERR "PM: Cannot get swap writer\n"); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 444 | return error; |
| 445 | } |
Jiri Slaby | 6f612af | 2010-05-01 23:54:02 +0200 | [diff] [blame] | 446 | if (!enough_swap(pages)) { |
| 447 | printk(KERN_ERR "PM: Not enough free swap\n"); |
| 448 | error = -ENOSPC; |
| 449 | goto out_finish; |
| 450 | } |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 451 | memset(&snapshot, 0, sizeof(struct snapshot_handle)); |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 452 | error = snapshot_read_next(&snapshot); |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 453 | if (error < PAGE_SIZE) { |
| 454 | if (error >= 0) |
| 455 | error = -EFAULT; |
| 456 | |
Jiri Slaby | 6f612af | 2010-05-01 23:54:02 +0200 | [diff] [blame] | 457 | goto out_finish; |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 458 | } |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 459 | header = (struct swsusp_info *)data_of(snapshot); |
Jiri Slaby | 6f612af | 2010-05-01 23:54:02 +0200 | [diff] [blame] | 460 | error = swap_write_page(&handle, header, NULL); |
| 461 | if (!error) |
| 462 | error = save_image(&handle, &snapshot, pages - 1); |
| 463 | out_finish: |
| 464 | error = swap_writer_finish(&handle, flags, error); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 465 | return error; |
| 466 | } |
| 467 | |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 468 | /** |
| 469 | * The following functions allow us to read data using a swap map |
| 470 | * in a file-alike way |
| 471 | */ |
| 472 | |
| 473 | static void release_swap_reader(struct swap_map_handle *handle) |
| 474 | { |
| 475 | if (handle->cur) |
| 476 | free_page((unsigned long)handle->cur); |
| 477 | handle->cur = NULL; |
| 478 | } |
| 479 | |
Jiri Slaby | 6f612af | 2010-05-01 23:54:02 +0200 | [diff] [blame] | 480 | static int get_swap_reader(struct swap_map_handle *handle, |
| 481 | unsigned int *flags_p) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 482 | { |
| 483 | int error; |
| 484 | |
Jiri Slaby | 6f612af | 2010-05-01 23:54:02 +0200 | [diff] [blame] | 485 | *flags_p = swsusp_header->flags; |
| 486 | |
| 487 | if (!swsusp_header->image) /* how can this happen? */ |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 488 | return -EINVAL; |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 489 | |
Rafael J. Wysocki | 8594912 | 2006-12-06 20:34:19 -0800 | [diff] [blame] | 490 | handle->cur = (struct swap_map_page *)get_zeroed_page(__GFP_WAIT | __GFP_HIGH); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 491 | if (!handle->cur) |
| 492 | return -ENOMEM; |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 493 | |
Jiri Slaby | 6f612af | 2010-05-01 23:54:02 +0200 | [diff] [blame] | 494 | error = hib_bio_read_page(swsusp_header->image, handle->cur, NULL); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 495 | if (error) { |
| 496 | release_swap_reader(handle); |
| 497 | return error; |
| 498 | } |
| 499 | handle->k = 0; |
| 500 | return 0; |
| 501 | } |
| 502 | |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 503 | static int swap_read_page(struct swap_map_handle *handle, void *buf, |
| 504 | struct bio **bio_chain) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 505 | { |
Rafael J. Wysocki | 3aef83e | 2006-12-06 20:34:10 -0800 | [diff] [blame] | 506 | sector_t offset; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 507 | int error; |
| 508 | |
| 509 | if (!handle->cur) |
| 510 | return -EINVAL; |
| 511 | offset = handle->cur->entries[handle->k]; |
| 512 | if (!offset) |
| 513 | return -EFAULT; |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 514 | error = hib_bio_read_page(offset, buf, bio_chain); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 515 | if (error) |
| 516 | return error; |
| 517 | if (++handle->k >= MAP_PAGE_ENTRIES) { |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 518 | error = hib_wait_on_bio_chain(bio_chain); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 519 | handle->k = 0; |
| 520 | offset = handle->cur->next_swap; |
| 521 | if (!offset) |
| 522 | release_swap_reader(handle); |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 523 | else if (!error) |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 524 | error = hib_bio_read_page(offset, handle->cur, NULL); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 525 | } |
| 526 | return error; |
| 527 | } |
| 528 | |
Jiri Slaby | 6f612af | 2010-05-01 23:54:02 +0200 | [diff] [blame] | 529 | static int swap_reader_finish(struct swap_map_handle *handle) |
| 530 | { |
| 531 | release_swap_reader(handle); |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 536 | /** |
| 537 | * load_image - load the image using the swap map handle |
| 538 | * @handle and the snapshot handle @snapshot |
| 539 | * (assume there are @nr_pages pages to load) |
| 540 | */ |
| 541 | |
| 542 | static int load_image(struct swap_map_handle *handle, |
| 543 | struct snapshot_handle *snapshot, |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 544 | unsigned int nr_to_read) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 545 | { |
| 546 | unsigned int m; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 547 | int error = 0; |
Andrew Morton | 8c00249 | 2006-09-25 23:32:43 -0700 | [diff] [blame] | 548 | struct timeval start; |
| 549 | struct timeval stop; |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 550 | struct bio *bio; |
| 551 | int err2; |
| 552 | unsigned nr_pages; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 553 | |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 554 | printk(KERN_INFO "PM: Loading image data pages (%u pages) ... ", |
| 555 | nr_to_read); |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 556 | m = nr_to_read / 100; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 557 | if (!m) |
| 558 | m = 1; |
| 559 | nr_pages = 0; |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 560 | bio = NULL; |
Andrew Morton | 8c00249 | 2006-09-25 23:32:43 -0700 | [diff] [blame] | 561 | do_gettimeofday(&start); |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 562 | for ( ; ; ) { |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 563 | error = snapshot_write_next(snapshot); |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 564 | if (error <= 0) |
| 565 | break; |
| 566 | error = swap_read_page(handle, data_of(*snapshot), &bio); |
| 567 | if (error) |
| 568 | break; |
| 569 | if (snapshot->sync_read) |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 570 | error = hib_wait_on_bio_chain(&bio); |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 571 | if (error) |
| 572 | break; |
| 573 | if (!(nr_pages % m)) |
| 574 | printk("\b\b\b\b%3d%%", nr_pages / m); |
| 575 | nr_pages++; |
| 576 | } |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 577 | err2 = hib_wait_on_bio_chain(&bio); |
Andrew Morton | 8c00249 | 2006-09-25 23:32:43 -0700 | [diff] [blame] | 578 | do_gettimeofday(&stop); |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 579 | if (!error) |
| 580 | error = err2; |
Con Kolivas | e655a25 | 2006-03-26 01:37:11 -0800 | [diff] [blame] | 581 | if (!error) { |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 582 | printk("\b\b\b\bdone\n"); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 583 | snapshot_write_finalize(snapshot); |
Con Kolivas | e655a25 | 2006-03-26 01:37:11 -0800 | [diff] [blame] | 584 | if (!snapshot_image_loaded(snapshot)) |
| 585 | error = -ENODATA; |
Jiri Slaby | bf9fd67 | 2009-10-28 22:55:42 +0100 | [diff] [blame] | 586 | } else |
| 587 | printk("\n"); |
Rafael J. Wysocki | 0d3a9ab | 2006-12-06 20:34:32 -0800 | [diff] [blame] | 588 | swsusp_show_speed(&start, &stop, nr_to_read, "Read"); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 589 | return error; |
| 590 | } |
| 591 | |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 592 | /** |
| 593 | * swsusp_read - read the hibernation image. |
| 594 | * @flags_p: flags passed by the "frozen" kernel in the image header should |
| 595 | * be written into this memeory location |
| 596 | */ |
| 597 | |
| 598 | int swsusp_read(unsigned int *flags_p) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 599 | { |
| 600 | int error; |
| 601 | struct swap_map_handle handle; |
| 602 | struct snapshot_handle snapshot; |
| 603 | struct swsusp_info *header; |
| 604 | |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 605 | memset(&snapshot, 0, sizeof(struct snapshot_handle)); |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 606 | error = snapshot_write_next(&snapshot); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 607 | if (error < PAGE_SIZE) |
| 608 | return error < 0 ? error : -EFAULT; |
| 609 | header = (struct swsusp_info *)data_of(snapshot); |
Jiri Slaby | 6f612af | 2010-05-01 23:54:02 +0200 | [diff] [blame] | 610 | error = get_swap_reader(&handle, flags_p); |
| 611 | if (error) |
| 612 | goto end; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 613 | if (!error) |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 614 | error = swap_read_page(&handle, header, NULL); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 615 | if (!error) |
| 616 | error = load_image(&handle, &snapshot, header->pages - 1); |
Jiri Slaby | 6f612af | 2010-05-01 23:54:02 +0200 | [diff] [blame] | 617 | swap_reader_finish(&handle); |
| 618 | end: |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 619 | if (!error) |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 620 | pr_debug("PM: Image successfully loaded\n"); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 621 | else |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 622 | pr_debug("PM: Error %d resuming\n", error); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 623 | return error; |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * swsusp_check - Check for swsusp signature in the resume device |
| 628 | */ |
| 629 | |
| 630 | int swsusp_check(void) |
| 631 | { |
| 632 | int error; |
| 633 | |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 634 | hib_resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ); |
| 635 | if (!IS_ERR(hib_resume_bdev)) { |
| 636 | set_blocksize(hib_resume_bdev, PAGE_SIZE); |
OGAWA Hirofumi | 6373da1 | 2007-05-23 13:58:00 -0700 | [diff] [blame] | 637 | memset(swsusp_header, 0, PAGE_SIZE); |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 638 | error = hib_bio_read_page(swsusp_resume_block, |
Vivek Goyal | 1b29c16 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 639 | swsusp_header, NULL); |
Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 640 | if (error) |
Jiri Slaby | 76b57e6 | 2009-10-07 22:37:35 +0200 | [diff] [blame] | 641 | goto put; |
Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 642 | |
Vivek Goyal | 1b29c16 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 643 | if (!memcmp(SWSUSP_SIG, swsusp_header->sig, 10)) { |
| 644 | memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 645 | /* Reset swap signature now */ |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 646 | error = hib_bio_write_page(swsusp_resume_block, |
Vivek Goyal | 1b29c16 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 647 | swsusp_header, NULL); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 648 | } else { |
Jiri Slaby | 76b57e6 | 2009-10-07 22:37:35 +0200 | [diff] [blame] | 649 | error = -EINVAL; |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 650 | } |
Jiri Slaby | 76b57e6 | 2009-10-07 22:37:35 +0200 | [diff] [blame] | 651 | |
| 652 | put: |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 653 | if (error) |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 654 | blkdev_put(hib_resume_bdev, FMODE_READ); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 655 | else |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 656 | pr_debug("PM: Signature found, resuming\n"); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 657 | } else { |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 658 | error = PTR_ERR(hib_resume_bdev); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | if (error) |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 662 | pr_debug("PM: Error %d checking image file\n", error); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 663 | |
| 664 | return error; |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * swsusp_close - close swap device. |
| 669 | */ |
| 670 | |
Al Viro | c2dd0da | 2007-10-08 13:21:10 -0400 | [diff] [blame] | 671 | void swsusp_close(fmode_t mode) |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 672 | { |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 673 | if (IS_ERR(hib_resume_bdev)) { |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 674 | pr_debug("PM: Image device not initialised\n"); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 675 | return; |
| 676 | } |
| 677 | |
Jiri Slaby | 8a0d613 | 2010-05-01 23:52:34 +0200 | [diff] [blame] | 678 | blkdev_put(hib_resume_bdev, mode); |
Rafael J. Wysocki | 61159a3 | 2006-03-23 03:00:00 -0800 | [diff] [blame] | 679 | } |
Vivek Goyal | 1b29c16 | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 680 | |
| 681 | static int swsusp_header_init(void) |
| 682 | { |
| 683 | swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL); |
| 684 | if (!swsusp_header) |
| 685 | panic("Could not allocate memory for swsusp_header\n"); |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | core_initcall(swsusp_header_init); |