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