Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/kernel/power/swsusp.c |
| 3 | * |
Pavel Machek | 96bc7ae | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 4 | * This file provides code to write suspend image to swap and read it back. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 7 | * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * This file is released under the GPLv2. |
| 10 | * |
| 11 | * I'd like to thank the following people for their work: |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 12 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * Pavel Machek <pavel@ucw.cz>: |
| 14 | * Modifications, defectiveness pointing, being with me at the very beginning, |
| 15 | * suspend to swap space, stop all tasks. Port to 2.4.18-ac and 2.5.17. |
| 16 | * |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 17 | * Steve Doddi <dirk@loth.demon.co.uk>: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * Support the possibility of hardware state restoring. |
| 19 | * |
| 20 | * Raph <grey.havens@earthling.net>: |
| 21 | * Support for preserving states of network devices and virtual console |
| 22 | * (including X and svgatextmode) |
| 23 | * |
| 24 | * Kurt Garloff <garloff@suse.de>: |
| 25 | * Straightened the critical function in order to prevent compilers from |
| 26 | * playing tricks with local variables. |
| 27 | * |
| 28 | * Andreas Mohr <a.mohr@mailto.de> |
| 29 | * |
| 30 | * Alex Badea <vampire@go.ro>: |
| 31 | * Fixed runaway init |
| 32 | * |
Andreas Steinmetz | c2ff18f | 2005-09-03 15:56:59 -0700 | [diff] [blame] | 33 | * Andreas Steinmetz <ast@domdv.de>: |
| 34 | * Added encrypted suspend option |
| 35 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | * More state savers are welcome. Especially for the scsi layer... |
| 37 | * |
| 38 | * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt |
| 39 | */ |
| 40 | |
| 41 | #include <linux/module.h> |
| 42 | #include <linux/mm.h> |
| 43 | #include <linux/suspend.h> |
| 44 | #include <linux/smp_lock.h> |
| 45 | #include <linux/file.h> |
| 46 | #include <linux/utsname.h> |
| 47 | #include <linux/version.h> |
| 48 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <linux/bitops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <linux/spinlock.h> |
| 51 | #include <linux/genhd.h> |
| 52 | #include <linux/kernel.h> |
| 53 | #include <linux/major.h> |
| 54 | #include <linux/swap.h> |
| 55 | #include <linux/pm.h> |
| 56 | #include <linux/device.h> |
| 57 | #include <linux/buffer_head.h> |
| 58 | #include <linux/swapops.h> |
| 59 | #include <linux/bootmem.h> |
| 60 | #include <linux/syscalls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #include <linux/highmem.h> |
| 62 | #include <linux/bio.h> |
| 63 | |
| 64 | #include <asm/uaccess.h> |
| 65 | #include <asm/mmu_context.h> |
| 66 | #include <asm/pgtable.h> |
| 67 | #include <asm/tlbflush.h> |
| 68 | #include <asm/io.h> |
| 69 | |
Andreas Steinmetz | c2ff18f | 2005-09-03 15:56:59 -0700 | [diff] [blame] | 70 | #include <linux/random.h> |
| 71 | #include <linux/crypto.h> |
| 72 | #include <asm/scatterlist.h> |
| 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | #include "power.h" |
| 75 | |
Andreas Steinmetz | c2ff18f | 2005-09-03 15:56:59 -0700 | [diff] [blame] | 76 | #define CIPHER "aes" |
| 77 | #define MAXKEY 32 |
| 78 | #define MAXIV 32 |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | extern char resume_file[]; |
| 81 | |
| 82 | /* Local variables that should not be affected by save */ |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 83 | unsigned int nr_copy_pages __nosavedata = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | /* Suspend pagedir is allocated before final copy, therefore it |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 86 | must be freed after resume |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | Warning: this is even more evil than it seems. Pagedirs this file |
| 89 | talks about are completely different from page directories used by |
| 90 | MMU hardware. |
| 91 | */ |
| 92 | suspend_pagedir_t *pagedir_nosave __nosavedata = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | #define SWSUSP_SIG "S1SUSPEND" |
| 95 | |
| 96 | static struct swsusp_header { |
Andreas Steinmetz | c2ff18f | 2005-09-03 15:56:59 -0700 | [diff] [blame] | 97 | char reserved[PAGE_SIZE - 20 - MAXKEY - MAXIV - sizeof(swp_entry_t)]; |
| 98 | u8 key_iv[MAXKEY+MAXIV]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | swp_entry_t swsusp_info; |
| 100 | char orig_sig[10]; |
| 101 | char sig[10]; |
| 102 | } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header; |
| 103 | |
| 104 | static struct swsusp_info swsusp_info; |
| 105 | |
| 106 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | * Saving part... |
| 108 | */ |
| 109 | |
| 110 | /* We memorize in swapfile_used what swap devices are used for suspension */ |
| 111 | #define SWAPFILE_UNUSED 0 |
| 112 | #define SWAPFILE_SUSPEND 1 /* This is the suspending device */ |
| 113 | #define SWAPFILE_IGNORED 2 /* Those are other swap devices ignored for suspension */ |
| 114 | |
| 115 | static unsigned short swapfile_used[MAX_SWAPFILES]; |
| 116 | static unsigned short root_swap; |
| 117 | |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 118 | static int write_page(unsigned long addr, swp_entry_t *loc); |
| 119 | static int bio_read_page(pgoff_t page_off, void *page); |
Andreas Steinmetz | c2ff18f | 2005-09-03 15:56:59 -0700 | [diff] [blame] | 120 | |
| 121 | static u8 key_iv[MAXKEY+MAXIV]; |
| 122 | |
| 123 | #ifdef CONFIG_SWSUSP_ENCRYPT |
| 124 | |
| 125 | static int crypto_init(int mode, void **mem) |
| 126 | { |
| 127 | int error = 0; |
| 128 | int len; |
| 129 | char *modemsg; |
| 130 | struct crypto_tfm *tfm; |
| 131 | |
| 132 | modemsg = mode ? "suspend not possible" : "resume not possible"; |
| 133 | |
| 134 | tfm = crypto_alloc_tfm(CIPHER, CRYPTO_TFM_MODE_CBC); |
| 135 | if(!tfm) { |
| 136 | printk(KERN_ERR "swsusp: no tfm, %s\n", modemsg); |
| 137 | error = -EINVAL; |
| 138 | goto out; |
| 139 | } |
| 140 | |
| 141 | if(MAXKEY < crypto_tfm_alg_min_keysize(tfm)) { |
| 142 | printk(KERN_ERR "swsusp: key buffer too small, %s\n", modemsg); |
| 143 | error = -ENOKEY; |
| 144 | goto fail; |
| 145 | } |
| 146 | |
| 147 | if (mode) |
| 148 | get_random_bytes(key_iv, MAXKEY+MAXIV); |
| 149 | |
| 150 | len = crypto_tfm_alg_max_keysize(tfm); |
| 151 | if (len > MAXKEY) |
| 152 | len = MAXKEY; |
| 153 | |
| 154 | if (crypto_cipher_setkey(tfm, key_iv, len)) { |
| 155 | printk(KERN_ERR "swsusp: key setup failure, %s\n", modemsg); |
| 156 | error = -EKEYREJECTED; |
| 157 | goto fail; |
| 158 | } |
| 159 | |
| 160 | len = crypto_tfm_alg_ivsize(tfm); |
| 161 | |
| 162 | if (MAXIV < len) { |
| 163 | printk(KERN_ERR "swsusp: iv buffer too small, %s\n", modemsg); |
| 164 | error = -EOVERFLOW; |
| 165 | goto fail; |
| 166 | } |
| 167 | |
| 168 | crypto_cipher_set_iv(tfm, key_iv+MAXKEY, len); |
| 169 | |
| 170 | *mem=(void *)tfm; |
| 171 | |
| 172 | goto out; |
| 173 | |
| 174 | fail: crypto_free_tfm(tfm); |
| 175 | out: return error; |
| 176 | } |
| 177 | |
| 178 | static __inline__ void crypto_exit(void *mem) |
| 179 | { |
| 180 | crypto_free_tfm((struct crypto_tfm *)mem); |
| 181 | } |
| 182 | |
| 183 | static __inline__ int crypto_write(struct pbe *p, void *mem) |
| 184 | { |
| 185 | int error = 0; |
| 186 | struct scatterlist src, dst; |
| 187 | |
| 188 | src.page = virt_to_page(p->address); |
| 189 | src.offset = 0; |
| 190 | src.length = PAGE_SIZE; |
| 191 | dst.page = virt_to_page((void *)&swsusp_header); |
| 192 | dst.offset = 0; |
| 193 | dst.length = PAGE_SIZE; |
| 194 | |
| 195 | error = crypto_cipher_encrypt((struct crypto_tfm *)mem, &dst, &src, |
| 196 | PAGE_SIZE); |
| 197 | |
| 198 | if (!error) |
| 199 | error = write_page((unsigned long)&swsusp_header, |
| 200 | &(p->swap_address)); |
| 201 | return error; |
| 202 | } |
| 203 | |
| 204 | static __inline__ int crypto_read(struct pbe *p, void *mem) |
| 205 | { |
| 206 | int error = 0; |
| 207 | struct scatterlist src, dst; |
| 208 | |
| 209 | error = bio_read_page(swp_offset(p->swap_address), (void *)p->address); |
| 210 | if (!error) { |
| 211 | src.offset = 0; |
| 212 | src.length = PAGE_SIZE; |
| 213 | dst.offset = 0; |
| 214 | dst.length = PAGE_SIZE; |
| 215 | src.page = dst.page = virt_to_page((void *)p->address); |
| 216 | |
| 217 | error = crypto_cipher_decrypt((struct crypto_tfm *)mem, &dst, |
| 218 | &src, PAGE_SIZE); |
| 219 | } |
| 220 | return error; |
| 221 | } |
| 222 | #else |
| 223 | static __inline__ int crypto_init(int mode, void *mem) |
| 224 | { |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static __inline__ void crypto_exit(void *mem) |
| 229 | { |
| 230 | } |
| 231 | |
| 232 | static __inline__ int crypto_write(struct pbe *p, void *mem) |
| 233 | { |
| 234 | return write_page(p->address, &(p->swap_address)); |
| 235 | } |
| 236 | |
| 237 | static __inline__ int crypto_read(struct pbe *p, void *mem) |
| 238 | { |
| 239 | return bio_read_page(swp_offset(p->swap_address), (void *)p->address); |
| 240 | } |
| 241 | #endif |
| 242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | static int mark_swapfiles(swp_entry_t prev) |
| 244 | { |
| 245 | int error; |
| 246 | |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 247 | rw_swap_page_sync(READ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | swp_entry(root_swap, 0), |
| 249 | virt_to_page((unsigned long)&swsusp_header)); |
| 250 | if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) || |
| 251 | !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) { |
| 252 | memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10); |
| 253 | memcpy(swsusp_header.sig,SWSUSP_SIG, 10); |
Andreas Steinmetz | c2ff18f | 2005-09-03 15:56:59 -0700 | [diff] [blame] | 254 | memcpy(swsusp_header.key_iv, key_iv, MAXKEY+MAXIV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | swsusp_header.swsusp_info = prev; |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 256 | error = rw_swap_page_sync(WRITE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | swp_entry(root_swap, 0), |
| 258 | virt_to_page((unsigned long) |
| 259 | &swsusp_header)); |
| 260 | } else { |
| 261 | pr_debug("swsusp: Partition is not swap space.\n"); |
| 262 | error = -ENODEV; |
| 263 | } |
| 264 | return error; |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * Check whether the swap device is the specified resume |
| 269 | * device, irrespective of whether they are specified by |
| 270 | * identical names. |
| 271 | * |
| 272 | * (Thus, device inode aliasing is allowed. You can say /dev/hda4 |
| 273 | * instead of /dev/ide/host0/bus0/target0/lun0/part4 [if using devfs] |
| 274 | * and they'll be considered the same device. This is *necessary* for |
| 275 | * devfs, since the resume code can only recognize the form /dev/hda4, |
| 276 | * but the suspend code would see the long name.) |
| 277 | */ |
| 278 | static int is_resume_device(const struct swap_info_struct *swap_info) |
| 279 | { |
| 280 | struct file *file = swap_info->swap_file; |
| 281 | struct inode *inode = file->f_dentry->d_inode; |
| 282 | |
| 283 | return S_ISBLK(inode->i_mode) && |
| 284 | swsusp_resume_device == MKDEV(imajor(inode), iminor(inode)); |
| 285 | } |
| 286 | |
| 287 | static int swsusp_swap_check(void) /* This is called before saving image */ |
| 288 | { |
| 289 | int i, len; |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | len=strlen(resume_file); |
| 292 | root_swap = 0xFFFF; |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 293 | |
Hugh Dickins | dae06ac | 2005-09-03 15:54:42 -0700 | [diff] [blame] | 294 | spin_lock(&swap_lock); |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 295 | for (i=0; i<MAX_SWAPFILES; i++) { |
Hugh Dickins | dae06ac | 2005-09-03 15:54:42 -0700 | [diff] [blame] | 296 | if (!(swap_info[i].flags & SWP_WRITEOK)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | swapfile_used[i]=SWAPFILE_UNUSED; |
| 298 | } else { |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 299 | if (!len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | printk(KERN_WARNING "resume= option should be used to set suspend device" ); |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 301 | if (root_swap == 0xFFFF) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | swapfile_used[i] = SWAPFILE_SUSPEND; |
| 303 | root_swap = i; |
| 304 | } else |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 305 | swapfile_used[i] = SWAPFILE_IGNORED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } else { |
| 307 | /* we ignore all swap devices that are not the resume_file */ |
| 308 | if (is_resume_device(&swap_info[i])) { |
| 309 | swapfile_used[i] = SWAPFILE_SUSPEND; |
| 310 | root_swap = i; |
| 311 | } else { |
| 312 | swapfile_used[i] = SWAPFILE_IGNORED; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | } |
Hugh Dickins | dae06ac | 2005-09-03 15:54:42 -0700 | [diff] [blame] | 317 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | return (root_swap != 0xffff) ? 0 : -ENODEV; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * This is called after saving image so modification |
| 323 | * will be lost after resume... and that's what we want. |
| 324 | * we make the device unusable. A new call to |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 325 | * lock_swapdevices can unlock the devices. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | */ |
| 327 | static void lock_swapdevices(void) |
| 328 | { |
| 329 | int i; |
| 330 | |
Hugh Dickins | dae06ac | 2005-09-03 15:54:42 -0700 | [diff] [blame] | 331 | spin_lock(&swap_lock); |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 332 | for (i = 0; i< MAX_SWAPFILES; i++) |
| 333 | if (swapfile_used[i] == SWAPFILE_IGNORED) { |
Hugh Dickins | dae06ac | 2005-09-03 15:54:42 -0700 | [diff] [blame] | 334 | swap_info[i].flags ^= SWP_WRITEOK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } |
Hugh Dickins | dae06ac | 2005-09-03 15:54:42 -0700 | [diff] [blame] | 336 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /** |
Pavel Machek | 8686bcd | 2005-09-22 21:44:11 -0700 | [diff] [blame] | 340 | * write_page - Write one page to a fresh swap location. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | * @addr: Address we're writing. |
| 342 | * @loc: Place to store the entry we used. |
| 343 | * |
| 344 | * Allocate a new swap entry and 'sync' it. Note we discard -EIO |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 345 | * errors. That is an artifact left over from swsusp. It did not |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | * check the return of rw_swap_page_sync() at all, since most pages |
| 347 | * written back to swap would return -EIO. |
| 348 | * This is a partial improvement, since we will at least return other |
| 349 | * errors, though we need to eventually fix the damn code. |
| 350 | */ |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 351 | static int write_page(unsigned long addr, swp_entry_t *loc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | { |
| 353 | swp_entry_t entry; |
| 354 | int error = 0; |
| 355 | |
| 356 | entry = get_swap_page(); |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 357 | if (swp_offset(entry) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | swapfile_used[swp_type(entry)] == SWAPFILE_SUSPEND) { |
| 359 | error = rw_swap_page_sync(WRITE, entry, |
| 360 | virt_to_page(addr)); |
| 361 | if (error == -EIO) |
| 362 | error = 0; |
| 363 | if (!error) |
| 364 | *loc = entry; |
| 365 | } else |
| 366 | error = -ENOSPC; |
| 367 | return error; |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * data_free - Free the swap entries used by the saved image. |
| 372 | * |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 373 | * Walk the list of used swap entries and free each one. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | * This is only used for cleanup when suspend fails. |
| 375 | */ |
| 376 | static void data_free(void) |
| 377 | { |
| 378 | swp_entry_t entry; |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 379 | struct pbe *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
Pavel Machek | 47b90ff | 2005-11-07 00:58:41 -0800 | [diff] [blame^] | 381 | for_each_pbe (p, pagedir_nosave) { |
Rafael J. Wysocki | 254b547 | 2005-09-27 21:45:31 -0700 | [diff] [blame] | 382 | entry = p->swap_address; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | if (entry.val) |
| 384 | swap_free(entry); |
| 385 | else |
| 386 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * data_write - Write saved image to swap. |
| 392 | * |
| 393 | * Walk the list of pages in the image and sync each one to swap. |
| 394 | */ |
| 395 | static int data_write(void) |
| 396 | { |
| 397 | int error = 0, i = 0; |
| 398 | unsigned int mod = nr_copy_pages / 100; |
| 399 | struct pbe *p; |
Andreas Steinmetz | c2ff18f | 2005-09-03 15:56:59 -0700 | [diff] [blame] | 400 | void *tfm; |
| 401 | |
| 402 | if ((error = crypto_init(1, &tfm))) |
| 403 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
| 405 | if (!mod) |
| 406 | mod = 1; |
| 407 | |
| 408 | printk( "Writing data to swap (%d pages)... ", nr_copy_pages ); |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 409 | for_each_pbe (p, pagedir_nosave) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | if (!(i%mod)) |
| 411 | printk( "\b\b\b\b%3d%%", i / mod ); |
Andreas Steinmetz | c2ff18f | 2005-09-03 15:56:59 -0700 | [diff] [blame] | 412 | if ((error = crypto_write(p, tfm))) { |
| 413 | crypto_exit(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | return error; |
Andreas Steinmetz | c2ff18f | 2005-09-03 15:56:59 -0700 | [diff] [blame] | 415 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | i++; |
| 417 | } |
| 418 | printk("\b\b\b\bdone\n"); |
Andreas Steinmetz | c2ff18f | 2005-09-03 15:56:59 -0700 | [diff] [blame] | 419 | crypto_exit(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | return error; |
| 421 | } |
| 422 | |
| 423 | static void dump_info(void) |
| 424 | { |
| 425 | pr_debug(" swsusp: Version: %u\n",swsusp_info.version_code); |
| 426 | pr_debug(" swsusp: Num Pages: %ld\n",swsusp_info.num_physpages); |
| 427 | pr_debug(" swsusp: UTS Sys: %s\n",swsusp_info.uts.sysname); |
| 428 | pr_debug(" swsusp: UTS Node: %s\n",swsusp_info.uts.nodename); |
| 429 | pr_debug(" swsusp: UTS Release: %s\n",swsusp_info.uts.release); |
| 430 | pr_debug(" swsusp: UTS Version: %s\n",swsusp_info.uts.version); |
| 431 | pr_debug(" swsusp: UTS Machine: %s\n",swsusp_info.uts.machine); |
| 432 | pr_debug(" swsusp: UTS Domain: %s\n",swsusp_info.uts.domainname); |
| 433 | pr_debug(" swsusp: CPUs: %d\n",swsusp_info.cpus); |
| 434 | pr_debug(" swsusp: Image: %ld Pages\n",swsusp_info.image_pages); |
| 435 | pr_debug(" swsusp: Pagedir: %ld Pages\n",swsusp_info.pagedir_pages); |
| 436 | } |
| 437 | |
| 438 | static void init_header(void) |
| 439 | { |
| 440 | memset(&swsusp_info, 0, sizeof(swsusp_info)); |
| 441 | swsusp_info.version_code = LINUX_VERSION_CODE; |
| 442 | swsusp_info.num_physpages = num_physpages; |
| 443 | memcpy(&swsusp_info.uts, &system_utsname, sizeof(system_utsname)); |
| 444 | |
| 445 | swsusp_info.suspend_pagedir = pagedir_nosave; |
| 446 | swsusp_info.cpus = num_online_cpus(); |
| 447 | swsusp_info.image_pages = nr_copy_pages; |
| 448 | } |
| 449 | |
| 450 | static int close_swap(void) |
| 451 | { |
| 452 | swp_entry_t entry; |
| 453 | int error; |
| 454 | |
| 455 | dump_info(); |
| 456 | error = write_page((unsigned long)&swsusp_info, &entry); |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 457 | if (!error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | printk( "S" ); |
| 459 | error = mark_swapfiles(entry); |
| 460 | printk( "|\n" ); |
| 461 | } |
| 462 | return error; |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * free_pagedir_entries - Free pages used by the page directory. |
| 467 | * |
| 468 | * This is used during suspend for error recovery. |
| 469 | */ |
| 470 | |
| 471 | static void free_pagedir_entries(void) |
| 472 | { |
| 473 | int i; |
| 474 | |
| 475 | for (i = 0; i < swsusp_info.pagedir_pages; i++) |
| 476 | swap_free(swsusp_info.pagedir[i]); |
| 477 | } |
| 478 | |
| 479 | |
| 480 | /** |
| 481 | * write_pagedir - Write the array of pages holding the page directory. |
| 482 | * @last: Last swap entry we write (needed for header). |
| 483 | */ |
| 484 | |
| 485 | static int write_pagedir(void) |
| 486 | { |
| 487 | int error = 0; |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 488 | unsigned int n = 0; |
| 489 | struct pbe *pbe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
| 491 | printk( "Writing pagedir..."); |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 492 | for_each_pb_page (pbe, pagedir_nosave) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | if ((error = write_page((unsigned long)pbe, &swsusp_info.pagedir[n++]))) |
| 494 | return error; |
| 495 | } |
| 496 | |
| 497 | swsusp_info.pagedir_pages = n; |
| 498 | printk("done (%u pages)\n", n); |
| 499 | return error; |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * write_suspend_image - Write entire image and metadata. |
| 504 | * |
| 505 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | static int write_suspend_image(void) |
| 507 | { |
| 508 | int error; |
| 509 | |
| 510 | init_header(); |
| 511 | if ((error = data_write())) |
| 512 | goto FreeData; |
| 513 | |
| 514 | if ((error = write_pagedir())) |
| 515 | goto FreePagedir; |
| 516 | |
| 517 | if ((error = close_swap())) |
| 518 | goto FreePagedir; |
| 519 | Done: |
Andreas Steinmetz | c2ff18f | 2005-09-03 15:56:59 -0700 | [diff] [blame] | 520 | memset(key_iv, 0, MAXKEY+MAXIV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | return error; |
| 522 | FreePagedir: |
| 523 | free_pagedir_entries(); |
| 524 | FreeData: |
| 525 | data_free(); |
| 526 | goto Done; |
| 527 | } |
| 528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | /** |
| 530 | * enough_swap - Make sure we have enough swap to save the image. |
| 531 | * |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 532 | * Returns TRUE or FALSE after checking the total amount of swap |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | * space avaiable. |
| 534 | * |
| 535 | * FIXME: si_swapinfo(&i) returns all swap devices information. |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 536 | * We should only consider resume_device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | */ |
| 538 | |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 539 | int enough_swap(unsigned int nr_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | { |
| 541 | struct sysinfo i; |
| 542 | |
| 543 | si_swapinfo(&i); |
Rafael J. Wysocki | 351619b | 2005-10-30 14:59:55 -0800 | [diff] [blame] | 544 | pr_debug("swsusp: available swap: %lu pages\n", i.freeswap); |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 545 | return i.freeswap > (nr_pages + PAGES_FOR_IO + |
| 546 | (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
| 550 | /* It is important _NOT_ to umount filesystems at this point. We want |
| 551 | * them synced (in case something goes wrong) but we DO not want to mark |
| 552 | * filesystem clean: it is not. (And it does not matter, if we resume |
| 553 | * correctly, we'll mark system clean, anyway.) |
| 554 | */ |
| 555 | int swsusp_write(void) |
| 556 | { |
| 557 | int error; |
Rafael J. Wysocki | 0245b3e | 2005-10-30 15:00:01 -0800 | [diff] [blame] | 558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | lock_swapdevices(); |
| 560 | error = write_suspend_image(); |
| 561 | /* This will unlock ignored swap devices since writing is finished */ |
| 562 | lock_swapdevices(); |
| 563 | return error; |
| 564 | |
| 565 | } |
| 566 | |
| 567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | |
| 569 | int swsusp_suspend(void) |
| 570 | { |
| 571 | int error; |
| 572 | if ((error = arch_prepare_suspend())) |
| 573 | return error; |
| 574 | local_irq_disable(); |
| 575 | /* At this point, device_suspend() has been called, but *not* |
| 576 | * device_power_down(). We *must* device_power_down() now. |
| 577 | * Otherwise, drivers for some devices (e.g. interrupt controllers) |
| 578 | * become desynchronized with the actual state of the hardware |
| 579 | * at resume time, and evil weirdness ensues. |
| 580 | */ |
| 581 | if ((error = device_power_down(PMSG_FREEZE))) { |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 582 | printk(KERN_ERR "Some devices failed to power down, aborting suspend\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | local_irq_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | return error; |
| 585 | } |
Pavel Machek | 47b724f | 2005-07-07 17:56:44 -0700 | [diff] [blame] | 586 | |
| 587 | if ((error = swsusp_swap_check())) { |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 588 | printk(KERN_ERR "swsusp: cannot find swap device, try swapon -a.\n"); |
| 589 | device_power_up(); |
Pavel Machek | 47b724f | 2005-07-07 17:56:44 -0700 | [diff] [blame] | 590 | local_irq_enable(); |
| 591 | return error; |
| 592 | } |
| 593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | save_processor_state(); |
| 595 | if ((error = swsusp_arch_suspend())) |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 596 | printk(KERN_ERR "Error %d suspending\n", error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | /* Restore control flow magically appears here */ |
| 598 | restore_processor_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | restore_highmem(); |
| 600 | device_power_up(); |
| 601 | local_irq_enable(); |
| 602 | return error; |
| 603 | } |
| 604 | |
| 605 | int swsusp_resume(void) |
| 606 | { |
| 607 | int error; |
| 608 | local_irq_disable(); |
| 609 | if (device_power_down(PMSG_FREEZE)) |
| 610 | printk(KERN_ERR "Some devices failed to power down, very bad\n"); |
| 611 | /* We'll ignore saved state, but this gets preempt count (etc) right */ |
| 612 | save_processor_state(); |
| 613 | error = swsusp_arch_resume(); |
| 614 | /* Code below is only ever reached in case of failure. Otherwise |
| 615 | * execution continues at place where swsusp_arch_suspend was called |
| 616 | */ |
| 617 | BUG_ON(!error); |
Rafael J. Wysocki | 2c1b4a5 | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 618 | /* The only reason why swsusp_arch_resume() can fail is memory being |
| 619 | * very tight, so we have to free it as soon as we can to avoid |
| 620 | * subsequent failures |
| 621 | */ |
| 622 | swsusp_free(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | restore_processor_state(); |
| 624 | restore_highmem(); |
Ingo Molnar | 8446f1d | 2005-09-06 15:16:27 -0700 | [diff] [blame] | 625 | touch_softlockup_watchdog(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | device_power_up(); |
| 627 | local_irq_enable(); |
| 628 | return error; |
| 629 | } |
| 630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | /** |
| 632 | * On resume, for storing the PBE list and the image, |
| 633 | * we can only use memory pages that do not conflict with the pages |
| 634 | * which had been used before suspend. |
| 635 | * |
| 636 | * We don't know which pages are usable until we allocate them. |
| 637 | * |
Rafael J. Wysocki | 2c1b4a5 | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 638 | * Allocated but unusable (ie eaten) memory pages are marked so that |
| 639 | * swsusp_free() can release them |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | |
Rafael J. Wysocki | 2c1b4a5 | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 642 | unsigned long get_safe_page(gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | { |
| 644 | unsigned long m; |
| 645 | |
Rafael J. Wysocki | 2c1b4a5 | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 646 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | m = get_zeroed_page(gfp_mask); |
Rafael J. Wysocki | 2c1b4a5 | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 648 | if (m && PageNosaveFree(virt_to_page(m))) |
| 649 | /* This is for swsusp_free() */ |
| 650 | SetPageNosave(virt_to_page(m)); |
| 651 | } while (m && PageNosaveFree(virt_to_page(m))); |
| 652 | if (m) { |
| 653 | /* This is for swsusp_free() */ |
| 654 | SetPageNosave(virt_to_page(m)); |
| 655 | SetPageNosaveFree(virt_to_page(m)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | } |
| 657 | return m; |
| 658 | } |
| 659 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | /** |
| 661 | * check_pagedir - We ensure here that pages that the PBEs point to |
| 662 | * won't collide with pages where we're going to restore from the loaded |
| 663 | * pages later |
| 664 | */ |
| 665 | |
| 666 | static int check_pagedir(struct pbe *pblist) |
| 667 | { |
| 668 | struct pbe *p; |
| 669 | |
| 670 | /* This is necessary, so that we can free allocated pages |
| 671 | * in case of failure |
| 672 | */ |
| 673 | for_each_pbe (p, pblist) |
| 674 | p->address = 0UL; |
| 675 | |
| 676 | for_each_pbe (p, pblist) { |
Rafael J. Wysocki | 2c1b4a5 | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 677 | p->address = get_safe_page(GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | if (!p->address) |
| 679 | return -ENOMEM; |
| 680 | } |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * swsusp_pagedir_relocate - It is possible, that some memory pages |
| 686 | * occupied by the list of PBEs collide with pages where we're going to |
| 687 | * restore from the loaded pages later. We relocate them here. |
| 688 | */ |
| 689 | |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 690 | static struct pbe *swsusp_pagedir_relocate(struct pbe *pblist) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | { |
| 692 | struct zone *zone; |
| 693 | unsigned long zone_pfn; |
| 694 | struct pbe *pbpage, *tail, *p; |
| 695 | void *m; |
Rafael J. Wysocki | 2c1b4a5 | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 696 | int rel = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
| 698 | if (!pblist) /* a sanity check */ |
| 699 | return NULL; |
| 700 | |
| 701 | pr_debug("swsusp: Relocating pagedir (%lu pages to check)\n", |
| 702 | swsusp_info.pagedir_pages); |
| 703 | |
Rafael J. Wysocki | 2c1b4a5 | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 704 | /* Clear page flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | |
Pavel Machek | 2e4d582 | 2005-06-25 14:55:12 -0700 | [diff] [blame] | 706 | for_each_zone (zone) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) |
Rafael J. Wysocki | 2c1b4a5 | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 708 | if (pfn_valid(zone_pfn + zone->zone_start_pfn)) |
| 709 | ClearPageNosaveFree(pfn_to_page(zone_pfn + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | zone->zone_start_pfn)); |
| 711 | } |
| 712 | |
Rafael J. Wysocki | 2c1b4a5 | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 713 | /* Mark orig addresses */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
| 715 | for_each_pbe (p, pblist) |
Rafael J. Wysocki | 2c1b4a5 | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 716 | SetPageNosaveFree(virt_to_page(p->orig_address)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
| 718 | tail = pblist + PB_PAGE_SKIP; |
| 719 | |
| 720 | /* Relocate colliding pages */ |
| 721 | |
| 722 | for_each_pb_page (pbpage, pblist) { |
Rafael J. Wysocki | 2c1b4a5 | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 723 | if (PageNosaveFree(virt_to_page((unsigned long)pbpage))) { |
| 724 | m = (void *)get_safe_page(GFP_ATOMIC | __GFP_COLD); |
| 725 | if (!m) |
| 726 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | memcpy(m, (void *)pbpage, PAGE_SIZE); |
| 728 | if (pbpage == pblist) |
| 729 | pblist = (struct pbe *)m; |
| 730 | else |
| 731 | tail->next = (struct pbe *)m; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | pbpage = (struct pbe *)m; |
| 733 | |
| 734 | /* We have to link the PBEs again */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | for (p = pbpage; p < pbpage + PB_PAGE_SKIP; p++) |
| 736 | if (p->next) /* needed to save the end */ |
| 737 | p->next = p + 1; |
| 738 | |
| 739 | rel++; |
| 740 | } |
| 741 | tail = pbpage + PB_PAGE_SKIP; |
| 742 | } |
| 743 | |
Rafael J. Wysocki | 2c1b4a5 | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 744 | /* This is for swsusp_free() */ |
| 745 | for_each_pb_page (pbpage, pblist) { |
| 746 | SetPageNosave(virt_to_page(pbpage)); |
| 747 | SetPageNosaveFree(virt_to_page(pbpage)); |
| 748 | } |
| 749 | |
| 750 | printk("swsusp: Relocated %d pages\n", rel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | |
| 752 | return pblist; |
| 753 | } |
| 754 | |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 755 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | * Using bio to read from swap. |
| 757 | * This code requires a bit more work than just using buffer heads |
| 758 | * but, it is the recommended way for 2.5/2.6. |
| 759 | * The following are to signal the beginning and end of I/O. Bios |
| 760 | * finish asynchronously, while we want them to happen synchronously. |
| 761 | * A simple atomic_t, and a wait loop take care of this problem. |
| 762 | */ |
| 763 | |
| 764 | static atomic_t io_done = ATOMIC_INIT(0); |
| 765 | |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 766 | static int end_io(struct bio *bio, unsigned int num, int err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | { |
| 768 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) |
| 769 | panic("I/O error reading memory image"); |
| 770 | atomic_set(&io_done, 0); |
| 771 | return 0; |
| 772 | } |
| 773 | |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 774 | static struct block_device *resume_bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | |
| 776 | /** |
| 777 | * submit - submit BIO request. |
| 778 | * @rw: READ or WRITE. |
| 779 | * @off physical offset of page. |
| 780 | * @page: page we're reading or writing. |
| 781 | * |
| 782 | * Straight from the textbook - allocate and initialize the bio. |
| 783 | * If we're writing, make sure the page is marked as dirty. |
| 784 | * Then submit it and wait. |
| 785 | */ |
| 786 | |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 787 | static int submit(int rw, pgoff_t page_off, void *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | { |
| 789 | int error = 0; |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 790 | struct bio *bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | |
| 792 | bio = bio_alloc(GFP_ATOMIC, 1); |
| 793 | if (!bio) |
| 794 | return -ENOMEM; |
| 795 | bio->bi_sector = page_off * (PAGE_SIZE >> 9); |
| 796 | bio_get(bio); |
| 797 | bio->bi_bdev = resume_bdev; |
| 798 | bio->bi_end_io = end_io; |
| 799 | |
| 800 | if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) { |
| 801 | printk("swsusp: ERROR: adding page to bio at %ld\n",page_off); |
| 802 | error = -EFAULT; |
| 803 | goto Done; |
| 804 | } |
| 805 | |
| 806 | if (rw == WRITE) |
| 807 | bio_set_pages_dirty(bio); |
| 808 | |
| 809 | atomic_set(&io_done, 1); |
| 810 | submit_bio(rw | (1 << BIO_RW_SYNC), bio); |
| 811 | while (atomic_read(&io_done)) |
| 812 | yield(); |
| 813 | |
| 814 | Done: |
| 815 | bio_put(bio); |
| 816 | return error; |
| 817 | } |
| 818 | |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 819 | static int bio_read_page(pgoff_t page_off, void *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | { |
| 821 | return submit(READ, page_off, page); |
| 822 | } |
| 823 | |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 824 | static int bio_write_page(pgoff_t page_off, void *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | { |
| 826 | return submit(WRITE, page_off, page); |
| 827 | } |
| 828 | |
| 829 | /* |
| 830 | * Sanity check if this image makes sense with this kernel/swap context |
| 831 | * I really don't think that it's foolproof but more than nothing.. |
| 832 | */ |
| 833 | |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 834 | static const char *sanity_check(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | { |
| 836 | dump_info(); |
Pavel Machek | 47b724f | 2005-07-07 17:56:44 -0700 | [diff] [blame] | 837 | if (swsusp_info.version_code != LINUX_VERSION_CODE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | return "kernel version"; |
Pavel Machek | 47b724f | 2005-07-07 17:56:44 -0700 | [diff] [blame] | 839 | if (swsusp_info.num_physpages != num_physpages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | return "memory size"; |
| 841 | if (strcmp(swsusp_info.uts.sysname,system_utsname.sysname)) |
| 842 | return "system type"; |
| 843 | if (strcmp(swsusp_info.uts.release,system_utsname.release)) |
| 844 | return "kernel release"; |
| 845 | if (strcmp(swsusp_info.uts.version,system_utsname.version)) |
| 846 | return "version"; |
| 847 | if (strcmp(swsusp_info.uts.machine,system_utsname.machine)) |
| 848 | return "machine"; |
Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 849 | #if 0 |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 850 | /* We can't use number of online CPUs when we use hotplug to remove them ;-))) */ |
| 851 | if (swsusp_info.cpus != num_possible_cpus()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | return "number of cpus"; |
Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 853 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | return NULL; |
| 855 | } |
| 856 | |
| 857 | |
| 858 | static int check_header(void) |
| 859 | { |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 860 | const char *reason = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | int error; |
| 862 | |
| 863 | if ((error = bio_read_page(swp_offset(swsusp_header.swsusp_info), &swsusp_info))) |
| 864 | return error; |
| 865 | |
| 866 | /* Is this same machine? */ |
| 867 | if ((reason = sanity_check())) { |
| 868 | printk(KERN_ERR "swsusp: Resume mismatch: %s\n",reason); |
| 869 | return -EPERM; |
| 870 | } |
| 871 | nr_copy_pages = swsusp_info.image_pages; |
| 872 | return error; |
| 873 | } |
| 874 | |
| 875 | static int check_sig(void) |
| 876 | { |
| 877 | int error; |
| 878 | |
| 879 | memset(&swsusp_header, 0, sizeof(swsusp_header)); |
| 880 | if ((error = bio_read_page(0, &swsusp_header))) |
| 881 | return error; |
| 882 | if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) { |
| 883 | memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10); |
Andreas Steinmetz | c2ff18f | 2005-09-03 15:56:59 -0700 | [diff] [blame] | 884 | memcpy(key_iv, swsusp_header.key_iv, MAXKEY+MAXIV); |
| 885 | memset(swsusp_header.key_iv, 0, MAXKEY+MAXIV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
| 887 | /* |
| 888 | * Reset swap signature now. |
| 889 | */ |
| 890 | error = bio_write_page(0, &swsusp_header); |
| 891 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | return -EINVAL; |
| 893 | } |
| 894 | if (!error) |
| 895 | pr_debug("swsusp: Signature found, resuming\n"); |
| 896 | return error; |
| 897 | } |
| 898 | |
| 899 | /** |
| 900 | * data_read - Read image pages from swap. |
| 901 | * |
| 902 | * You do not need to check for overlaps, check_pagedir() |
| 903 | * already did that. |
| 904 | */ |
| 905 | |
| 906 | static int data_read(struct pbe *pblist) |
| 907 | { |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 908 | struct pbe *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | int error = 0; |
| 910 | int i = 0; |
| 911 | int mod = swsusp_info.image_pages / 100; |
Andreas Steinmetz | c2ff18f | 2005-09-03 15:56:59 -0700 | [diff] [blame] | 912 | void *tfm; |
| 913 | |
| 914 | if ((error = crypto_init(0, &tfm))) |
| 915 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | |
| 917 | if (!mod) |
| 918 | mod = 1; |
| 919 | |
| 920 | printk("swsusp: Reading image data (%lu pages): ", |
| 921 | swsusp_info.image_pages); |
| 922 | |
| 923 | for_each_pbe (p, pblist) { |
| 924 | if (!(i % mod)) |
| 925 | printk("\b\b\b\b%3d%%", i / mod); |
| 926 | |
Andreas Steinmetz | c2ff18f | 2005-09-03 15:56:59 -0700 | [diff] [blame] | 927 | if ((error = crypto_read(p, tfm))) { |
| 928 | crypto_exit(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | return error; |
Andreas Steinmetz | c2ff18f | 2005-09-03 15:56:59 -0700 | [diff] [blame] | 930 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | |
| 932 | i++; |
| 933 | } |
| 934 | printk("\b\b\b\bdone\n"); |
Andreas Steinmetz | c2ff18f | 2005-09-03 15:56:59 -0700 | [diff] [blame] | 935 | crypto_exit(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | return error; |
| 937 | } |
| 938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | /** |
| 940 | * read_pagedir - Read page backup list pages from swap |
| 941 | */ |
| 942 | |
| 943 | static int read_pagedir(struct pbe *pblist) |
| 944 | { |
| 945 | struct pbe *pbpage, *p; |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 946 | unsigned int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | int error; |
| 948 | |
| 949 | if (!pblist) |
| 950 | return -EFAULT; |
| 951 | |
| 952 | printk("swsusp: Reading pagedir (%lu pages)\n", |
| 953 | swsusp_info.pagedir_pages); |
| 954 | |
| 955 | for_each_pb_page (pbpage, pblist) { |
| 956 | unsigned long offset = swp_offset(swsusp_info.pagedir[i++]); |
| 957 | |
| 958 | error = -EFAULT; |
| 959 | if (offset) { |
| 960 | p = (pbpage + PB_PAGE_SKIP)->next; |
| 961 | error = bio_read_page(offset, (void *)pbpage); |
| 962 | (pbpage + PB_PAGE_SKIP)->next = p; |
| 963 | } |
| 964 | if (error) |
| 965 | break; |
| 966 | } |
| 967 | |
Rafael J. Wysocki | 2c1b4a5 | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 968 | if (!error) |
Rafael J. Wysocki | f2d6137 | 2005-09-27 21:45:32 -0700 | [diff] [blame] | 969 | BUG_ON(i != swsusp_info.pagedir_pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | |
| 971 | return error; |
| 972 | } |
| 973 | |
| 974 | |
| 975 | static int check_suspend_image(void) |
| 976 | { |
| 977 | int error = 0; |
| 978 | |
| 979 | if ((error = check_sig())) |
| 980 | return error; |
| 981 | |
| 982 | if ((error = check_header())) |
| 983 | return error; |
| 984 | |
| 985 | return 0; |
| 986 | } |
| 987 | |
| 988 | static int read_suspend_image(void) |
| 989 | { |
| 990 | int error = 0; |
| 991 | struct pbe *p; |
| 992 | |
| 993 | if (!(p = alloc_pagedir(nr_copy_pages))) |
| 994 | return -ENOMEM; |
| 995 | |
| 996 | if ((error = read_pagedir(p))) |
| 997 | return error; |
| 998 | |
| 999 | create_pbe_list(p, nr_copy_pages); |
| 1000 | |
| 1001 | if (!(pagedir_nosave = swsusp_pagedir_relocate(p))) |
| 1002 | return -ENOMEM; |
| 1003 | |
| 1004 | /* Allocate memory for the image and read the data from swap */ |
| 1005 | |
| 1006 | error = check_pagedir(pagedir_nosave); |
Rafael J. Wysocki | 3dd0832 | 2005-10-09 21:19:40 +0200 | [diff] [blame] | 1007 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | if (!error) |
| 1009 | error = data_read(pagedir_nosave); |
| 1010 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | return error; |
| 1012 | } |
| 1013 | |
| 1014 | /** |
| 1015 | * swsusp_check - Check for saved image in swap |
| 1016 | */ |
| 1017 | |
| 1018 | int swsusp_check(void) |
| 1019 | { |
| 1020 | int error; |
| 1021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ); |
| 1023 | if (!IS_ERR(resume_bdev)) { |
| 1024 | set_blocksize(resume_bdev, PAGE_SIZE); |
| 1025 | error = check_suspend_image(); |
| 1026 | if (error) |
| 1027 | blkdev_put(resume_bdev); |
| 1028 | } else |
| 1029 | error = PTR_ERR(resume_bdev); |
| 1030 | |
| 1031 | if (!error) |
| 1032 | pr_debug("swsusp: resume file found\n"); |
| 1033 | else |
| 1034 | pr_debug("swsusp: Error %d check for resume file\n", error); |
| 1035 | return error; |
| 1036 | } |
| 1037 | |
| 1038 | /** |
| 1039 | * swsusp_read - Read saved image from swap. |
| 1040 | */ |
| 1041 | |
| 1042 | int swsusp_read(void) |
| 1043 | { |
| 1044 | int error; |
| 1045 | |
| 1046 | if (IS_ERR(resume_bdev)) { |
| 1047 | pr_debug("swsusp: block device not initialised\n"); |
| 1048 | return PTR_ERR(resume_bdev); |
| 1049 | } |
| 1050 | |
| 1051 | error = read_suspend_image(); |
| 1052 | blkdev_put(resume_bdev); |
Andreas Steinmetz | c2ff18f | 2005-09-03 15:56:59 -0700 | [diff] [blame] | 1053 | memset(key_iv, 0, MAXKEY+MAXIV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | |
| 1055 | if (!error) |
| 1056 | pr_debug("swsusp: Reading resume file was successful\n"); |
| 1057 | else |
| 1058 | pr_debug("swsusp: Error %d resuming\n", error); |
| 1059 | return error; |
| 1060 | } |
| 1061 | |
| 1062 | /** |
| 1063 | * swsusp_close - close swap device. |
| 1064 | */ |
| 1065 | |
| 1066 | void swsusp_close(void) |
| 1067 | { |
| 1068 | if (IS_ERR(resume_bdev)) { |
| 1069 | pr_debug("swsusp: block device not initialised\n"); |
| 1070 | return; |
| 1071 | } |
| 1072 | |
| 1073 | blkdev_put(resume_bdev); |
| 1074 | } |