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