blob: 8511c7f3386a0e655120adb5d507b0d7f6049555 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/power/swsusp.c
3 *
Pavel Machek96bc7ae2005-10-30 14:59:58 -08004 * This file provides code to write suspend image to swap and read it back.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08007 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This file is released under the GPLv2.
10 *
11 * I'd like to thank the following people for their work:
Pavel Machek2e4d5822005-06-25 14:55:12 -070012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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 Machek2e4d5822005-06-25 14:55:12 -070017 * Steve Doddi <dirk@loth.demon.co.uk>:
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * 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 Steinmetzc2ff18f2005-09-03 15:56:59 -070033 * Andreas Steinmetz <ast@domdv.de>:
34 * Added encrypted suspend option
35 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * 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 Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#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 Torvalds1da177e2005-04-16 15:20:36 -070061#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 Steinmetzc2ff18f2005-09-03 15:56:59 -070070#include <linux/random.h>
71#include <linux/crypto.h>
72#include <asm/scatterlist.h>
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include "power.h"
75
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -070076#define CIPHER "aes"
77#define MAXKEY 32
78#define MAXIV 32
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080extern char resume_file[];
81
82/* Local variables that should not be affected by save */
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080083unsigned int nr_copy_pages __nosavedata = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/* Suspend pagedir is allocated before final copy, therefore it
Pavel Machek2e4d5822005-06-25 14:55:12 -070086 must be freed after resume
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 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 */
92suspend_pagedir_t *pagedir_nosave __nosavedata = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94#define SWSUSP_SIG "S1SUSPEND"
95
96static struct swsusp_header {
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -070097 char reserved[PAGE_SIZE - 20 - MAXKEY - MAXIV - sizeof(swp_entry_t)];
98 u8 key_iv[MAXKEY+MAXIV];
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 swp_entry_t swsusp_info;
100 char orig_sig[10];
101 char sig[10];
102} __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
103
104static struct swsusp_info swsusp_info;
105
106/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * 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
115static unsigned short swapfile_used[MAX_SWAPFILES];
116static unsigned short root_swap;
117
Pavel Machekdc19d502005-11-07 00:58:40 -0800118static int write_page(unsigned long addr, swp_entry_t *loc);
119static int bio_read_page(pgoff_t page_off, void *page);
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700120
121static u8 key_iv[MAXKEY+MAXIV];
122
123#ifdef CONFIG_SWSUSP_ENCRYPT
124
125static 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
174fail: crypto_free_tfm(tfm);
175out: return error;
176}
177
178static __inline__ void crypto_exit(void *mem)
179{
180 crypto_free_tfm((struct crypto_tfm *)mem);
181}
182
183static __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
204static __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
223static __inline__ int crypto_init(int mode, void *mem)
224{
225 return 0;
226}
227
228static __inline__ void crypto_exit(void *mem)
229{
230}
231
232static __inline__ int crypto_write(struct pbe *p, void *mem)
233{
234 return write_page(p->address, &(p->swap_address));
235}
236
237static __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 Torvalds1da177e2005-04-16 15:20:36 -0700243static int mark_swapfiles(swp_entry_t prev)
244{
245 int error;
246
Pavel Machek2e4d5822005-06-25 14:55:12 -0700247 rw_swap_page_sync(READ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 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 Steinmetzc2ff18f2005-09-03 15:56:59 -0700254 memcpy(swsusp_header.key_iv, key_iv, MAXKEY+MAXIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 swsusp_header.swsusp_info = prev;
Pavel Machek2e4d5822005-06-25 14:55:12 -0700256 error = rw_swap_page_sync(WRITE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 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 */
278static 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
287static int swsusp_swap_check(void) /* This is called before saving image */
288{
289 int i, len;
Pavel Machek2e4d5822005-06-25 14:55:12 -0700290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 len=strlen(resume_file);
292 root_swap = 0xFFFF;
Pavel Machek2e4d5822005-06-25 14:55:12 -0700293
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700294 spin_lock(&swap_lock);
Pavel Machek2e4d5822005-06-25 14:55:12 -0700295 for (i=0; i<MAX_SWAPFILES; i++) {
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700296 if (!(swap_info[i].flags & SWP_WRITEOK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 swapfile_used[i]=SWAPFILE_UNUSED;
298 } else {
Pavel Machek2e4d5822005-06-25 14:55:12 -0700299 if (!len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 printk(KERN_WARNING "resume= option should be used to set suspend device" );
Pavel Machek2e4d5822005-06-25 14:55:12 -0700301 if (root_swap == 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 swapfile_used[i] = SWAPFILE_SUSPEND;
303 root_swap = i;
304 } else
Pavel Machek2e4d5822005-06-25 14:55:12 -0700305 swapfile_used[i] = SWAPFILE_IGNORED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 } 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 Dickinsdae06ac2005-09-03 15:54:42 -0700317 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 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 Machek2e4d5822005-06-25 14:55:12 -0700325 * lock_swapdevices can unlock the devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
327static void lock_swapdevices(void)
328{
329 int i;
330
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700331 spin_lock(&swap_lock);
Pavel Machek2e4d5822005-06-25 14:55:12 -0700332 for (i = 0; i< MAX_SWAPFILES; i++)
333 if (swapfile_used[i] == SWAPFILE_IGNORED) {
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700334 swap_info[i].flags ^= SWP_WRITEOK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700336 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339/**
Pavel Machek8686bcd2005-09-22 21:44:11 -0700340 * write_page - Write one page to a fresh swap location.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 * @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 Machek2e4d5822005-06-25 14:55:12 -0700345 * errors. That is an artifact left over from swsusp. It did not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 * 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 Machekdc19d502005-11-07 00:58:40 -0800351static int write_page(unsigned long addr, swp_entry_t *loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 swp_entry_t entry;
354 int error = 0;
355
356 entry = get_swap_page();
Pavel Machek2e4d5822005-06-25 14:55:12 -0700357 if (swp_offset(entry) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 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 Machek2e4d5822005-06-25 14:55:12 -0700373 * Walk the list of used swap entries and free each one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * This is only used for cleanup when suspend fails.
375 */
376static void data_free(void)
377{
378 swp_entry_t entry;
Pavel Machekdc19d502005-11-07 00:58:40 -0800379 struct pbe *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Pavel Machek47b90ff2005-11-07 00:58:41 -0800381 for_each_pbe (p, pagedir_nosave) {
Rafael J. Wysocki254b5472005-09-27 21:45:31 -0700382 entry = p->swap_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (entry.val)
384 swap_free(entry);
385 else
386 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
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 */
395static int data_write(void)
396{
397 int error = 0, i = 0;
398 unsigned int mod = nr_copy_pages / 100;
399 struct pbe *p;
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700400 void *tfm;
401
402 if ((error = crypto_init(1, &tfm)))
403 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if (!mod)
406 mod = 1;
407
408 printk( "Writing data to swap (%d pages)... ", nr_copy_pages );
Pavel Machek2e4d5822005-06-25 14:55:12 -0700409 for_each_pbe (p, pagedir_nosave) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (!(i%mod))
411 printk( "\b\b\b\b%3d%%", i / mod );
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700412 if ((error = crypto_write(p, tfm))) {
413 crypto_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return error;
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 i++;
417 }
418 printk("\b\b\b\bdone\n");
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700419 crypto_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return error;
421}
422
423static 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
438static 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
450static 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 Machek2e4d5822005-06-25 14:55:12 -0700457 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 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
471static 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
485static int write_pagedir(void)
486{
487 int error = 0;
Pavel Machekdc19d502005-11-07 00:58:40 -0800488 unsigned int n = 0;
489 struct pbe *pbe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 printk( "Writing pagedir...");
Pavel Machek2e4d5822005-06-25 14:55:12 -0700492 for_each_pb_page (pbe, pagedir_nosave) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 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 Torvalds1da177e2005-04-16 15:20:36 -0700506static 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 Steinmetzc2ff18f2005-09-03 15:56:59 -0700520 memset(key_iv, 0, MAXKEY+MAXIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return error;
522 FreePagedir:
523 free_pagedir_entries();
524 FreeData:
525 data_free();
526 goto Done;
527}
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529/**
530 * enough_swap - Make sure we have enough swap to save the image.
531 *
Pavel Machek2e4d5822005-06-25 14:55:12 -0700532 * Returns TRUE or FALSE after checking the total amount of swap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 * space avaiable.
534 *
535 * FIXME: si_swapinfo(&i) returns all swap devices information.
Pavel Machek2e4d5822005-06-25 14:55:12 -0700536 * We should only consider resume_device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 */
538
Pavel Machekdc19d502005-11-07 00:58:40 -0800539int enough_swap(unsigned int nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 struct sysinfo i;
542
543 si_swapinfo(&i);
Rafael J. Wysocki351619b2005-10-30 14:59:55 -0800544 pr_debug("swsusp: available swap: %lu pages\n", i.freeswap);
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800545 return i.freeswap > (nr_pages + PAGES_FOR_IO +
546 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
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 */
555int swsusp_write(void)
556{
557 int error;
Rafael J. Wysocki0245b3e2005-10-30 15:00:01 -0800558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 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 Torvalds1da177e2005-04-16 15:20:36 -0700568
569int 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 Machek99dc7d62005-09-03 15:57:05 -0700582 printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return error;
585 }
Pavel Machek47b724f2005-07-07 17:56:44 -0700586
587 if ((error = swsusp_swap_check())) {
Pavel Machek99dc7d62005-09-03 15:57:05 -0700588 printk(KERN_ERR "swsusp: cannot find swap device, try swapon -a.\n");
589 device_power_up();
Pavel Machek47b724f2005-07-07 17:56:44 -0700590 local_irq_enable();
591 return error;
592 }
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 save_processor_state();
595 if ((error = swsusp_arch_suspend()))
Pavel Machek99dc7d62005-09-03 15:57:05 -0700596 printk(KERN_ERR "Error %d suspending\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 /* Restore control flow magically appears here */
598 restore_processor_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 restore_highmem();
600 device_power_up();
601 local_irq_enable();
602 return error;
603}
604
605int 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. Wysocki2c1b4a52005-10-30 14:59:58 -0800618 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700623 restore_processor_state();
624 restore_highmem();
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700625 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 device_power_up();
627 local_irq_enable();
628 return error;
629}
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631/**
Rafael J. Wysockied14b522005-11-08 21:34:40 -0800632 * mark_unsafe_pages - mark the pages that cannot be used for storing
633 * the image during resume, because they conflict with the pages that
634 * had been used before suspend
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 */
636
Rafael J. Wysockied14b522005-11-08 21:34:40 -0800637static void mark_unsafe_pages(struct pbe *pblist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 struct zone *zone;
640 unsigned long zone_pfn;
Rafael J. Wysockied14b522005-11-08 21:34:40 -0800641 struct pbe *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 if (!pblist) /* a sanity check */
Rafael J. Wysockied14b522005-11-08 21:34:40 -0800644 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800646 /* Clear page flags */
Pavel Machek2e4d5822005-06-25 14:55:12 -0700647 for_each_zone (zone) {
Rafael J. Wysockied14b522005-11-08 21:34:40 -0800648 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
649 if (pfn_valid(zone_pfn + zone->zone_start_pfn))
650 ClearPageNosaveFree(pfn_to_page(zone_pfn +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 zone->zone_start_pfn));
652 }
653
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800654 /* Mark orig addresses */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 for_each_pbe (p, pblist)
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800656 SetPageNosaveFree(virt_to_page(p->orig_address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Rafael J. Wysockied14b522005-11-08 21:34:40 -0800658}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Rafael J. Wysockied14b522005-11-08 21:34:40 -0800660static void copy_page_backup_list(struct pbe *dst, struct pbe *src)
661{
662 /* We assume both lists contain the same number of elements */
663 while (src) {
664 dst->orig_address = src->orig_address;
665 dst->swap_address = src->swap_address;
666 dst = dst->next;
667 src = src->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700671/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 * Using bio to read from swap.
673 * This code requires a bit more work than just using buffer heads
674 * but, it is the recommended way for 2.5/2.6.
675 * The following are to signal the beginning and end of I/O. Bios
676 * finish asynchronously, while we want them to happen synchronously.
677 * A simple atomic_t, and a wait loop take care of this problem.
678 */
679
680static atomic_t io_done = ATOMIC_INIT(0);
681
Pavel Machekdc19d502005-11-07 00:58:40 -0800682static int end_io(struct bio *bio, unsigned int num, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
685 panic("I/O error reading memory image");
686 atomic_set(&io_done, 0);
687 return 0;
688}
689
Pavel Machekdc19d502005-11-07 00:58:40 -0800690static struct block_device *resume_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692/**
693 * submit - submit BIO request.
694 * @rw: READ or WRITE.
695 * @off physical offset of page.
696 * @page: page we're reading or writing.
697 *
698 * Straight from the textbook - allocate and initialize the bio.
699 * If we're writing, make sure the page is marked as dirty.
700 * Then submit it and wait.
701 */
702
Pavel Machekdc19d502005-11-07 00:58:40 -0800703static int submit(int rw, pgoff_t page_off, void *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 int error = 0;
Pavel Machekdc19d502005-11-07 00:58:40 -0800706 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 bio = bio_alloc(GFP_ATOMIC, 1);
709 if (!bio)
710 return -ENOMEM;
711 bio->bi_sector = page_off * (PAGE_SIZE >> 9);
712 bio_get(bio);
713 bio->bi_bdev = resume_bdev;
714 bio->bi_end_io = end_io;
715
716 if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
717 printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
718 error = -EFAULT;
719 goto Done;
720 }
721
722 if (rw == WRITE)
723 bio_set_pages_dirty(bio);
724
725 atomic_set(&io_done, 1);
726 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
727 while (atomic_read(&io_done))
728 yield();
729
730 Done:
731 bio_put(bio);
732 return error;
733}
734
Pavel Machekdc19d502005-11-07 00:58:40 -0800735static int bio_read_page(pgoff_t page_off, void *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 return submit(READ, page_off, page);
738}
739
Pavel Machekdc19d502005-11-07 00:58:40 -0800740static int bio_write_page(pgoff_t page_off, void *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 return submit(WRITE, page_off, page);
743}
744
745/*
746 * Sanity check if this image makes sense with this kernel/swap context
747 * I really don't think that it's foolproof but more than nothing..
748 */
749
Pavel Machekdc19d502005-11-07 00:58:40 -0800750static const char *sanity_check(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 dump_info();
Pavel Machek47b724f2005-07-07 17:56:44 -0700753 if (swsusp_info.version_code != LINUX_VERSION_CODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return "kernel version";
Pavel Machek47b724f2005-07-07 17:56:44 -0700755 if (swsusp_info.num_physpages != num_physpages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return "memory size";
757 if (strcmp(swsusp_info.uts.sysname,system_utsname.sysname))
758 return "system type";
759 if (strcmp(swsusp_info.uts.release,system_utsname.release))
760 return "kernel release";
761 if (strcmp(swsusp_info.uts.version,system_utsname.version))
762 return "version";
763 if (strcmp(swsusp_info.uts.machine,system_utsname.machine))
764 return "machine";
Li Shaohua5a72e042005-06-25 14:55:06 -0700765#if 0
Pavel Machek99dc7d62005-09-03 15:57:05 -0700766 /* We can't use number of online CPUs when we use hotplug to remove them ;-))) */
767 if (swsusp_info.cpus != num_possible_cpus())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return "number of cpus";
Li Shaohua5a72e042005-06-25 14:55:06 -0700769#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return NULL;
771}
772
773
774static int check_header(void)
775{
Pavel Machekdc19d502005-11-07 00:58:40 -0800776 const char *reason = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int error;
778
779 if ((error = bio_read_page(swp_offset(swsusp_header.swsusp_info), &swsusp_info)))
780 return error;
781
782 /* Is this same machine? */
783 if ((reason = sanity_check())) {
784 printk(KERN_ERR "swsusp: Resume mismatch: %s\n",reason);
785 return -EPERM;
786 }
787 nr_copy_pages = swsusp_info.image_pages;
788 return error;
789}
790
791static int check_sig(void)
792{
793 int error;
794
795 memset(&swsusp_header, 0, sizeof(swsusp_header));
796 if ((error = bio_read_page(0, &swsusp_header)))
797 return error;
798 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
799 memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700800 memcpy(key_iv, swsusp_header.key_iv, MAXKEY+MAXIV);
801 memset(swsusp_header.key_iv, 0, MAXKEY+MAXIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 /*
804 * Reset swap signature now.
805 */
806 error = bio_write_page(0, &swsusp_header);
807 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return -EINVAL;
809 }
810 if (!error)
811 pr_debug("swsusp: Signature found, resuming\n");
812 return error;
813}
814
815/**
816 * data_read - Read image pages from swap.
817 *
818 * You do not need to check for overlaps, check_pagedir()
819 * already did that.
820 */
821
822static int data_read(struct pbe *pblist)
823{
Pavel Machekdc19d502005-11-07 00:58:40 -0800824 struct pbe *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 int error = 0;
826 int i = 0;
827 int mod = swsusp_info.image_pages / 100;
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700828 void *tfm;
829
830 if ((error = crypto_init(0, &tfm)))
831 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 if (!mod)
834 mod = 1;
835
836 printk("swsusp: Reading image data (%lu pages): ",
837 swsusp_info.image_pages);
838
839 for_each_pbe (p, pblist) {
840 if (!(i % mod))
841 printk("\b\b\b\b%3d%%", i / mod);
842
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700843 if ((error = crypto_read(p, tfm))) {
844 crypto_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return error;
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 i++;
849 }
850 printk("\b\b\b\bdone\n");
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700851 crypto_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return error;
853}
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855/**
856 * read_pagedir - Read page backup list pages from swap
857 */
858
859static int read_pagedir(struct pbe *pblist)
860{
861 struct pbe *pbpage, *p;
Pavel Machekdc19d502005-11-07 00:58:40 -0800862 unsigned int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 int error;
864
865 if (!pblist)
866 return -EFAULT;
867
868 printk("swsusp: Reading pagedir (%lu pages)\n",
869 swsusp_info.pagedir_pages);
870
871 for_each_pb_page (pbpage, pblist) {
872 unsigned long offset = swp_offset(swsusp_info.pagedir[i++]);
873
874 error = -EFAULT;
875 if (offset) {
876 p = (pbpage + PB_PAGE_SKIP)->next;
877 error = bio_read_page(offset, (void *)pbpage);
878 (pbpage + PB_PAGE_SKIP)->next = p;
879 }
880 if (error)
881 break;
882 }
883
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800884 if (!error)
Rafael J. Wysockif2d61372005-09-27 21:45:32 -0700885 BUG_ON(i != swsusp_info.pagedir_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 return error;
888}
889
890
891static int check_suspend_image(void)
892{
893 int error = 0;
894
895 if ((error = check_sig()))
896 return error;
897
898 if ((error = check_header()))
899 return error;
900
901 return 0;
902}
903
904static int read_suspend_image(void)
905{
906 int error = 0;
907 struct pbe *p;
908
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800909 if (!(p = alloc_pagedir(nr_copy_pages, GFP_ATOMIC, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return -ENOMEM;
911
912 if ((error = read_pagedir(p)))
913 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 create_pbe_list(p, nr_copy_pages);
Rafael J. Wysockied14b522005-11-08 21:34:40 -0800915 mark_unsafe_pages(p);
916 pagedir_nosave = alloc_pagedir(nr_copy_pages, GFP_ATOMIC, 1);
917 if (pagedir_nosave) {
918 create_pbe_list(pagedir_nosave, nr_copy_pages);
919 copy_page_backup_list(pagedir_nosave, p);
920 }
921 free_pagedir(p);
922 if (!pagedir_nosave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return -ENOMEM;
924
925 /* Allocate memory for the image and read the data from swap */
926
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800927 error = alloc_data_pages(pagedir_nosave, GFP_ATOMIC, 1);
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (!error)
930 error = data_read(pagedir_nosave);
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return error;
933}
934
935/**
936 * swsusp_check - Check for saved image in swap
937 */
938
939int swsusp_check(void)
940{
941 int error;
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
944 if (!IS_ERR(resume_bdev)) {
945 set_blocksize(resume_bdev, PAGE_SIZE);
946 error = check_suspend_image();
947 if (error)
948 blkdev_put(resume_bdev);
949 } else
950 error = PTR_ERR(resume_bdev);
951
952 if (!error)
953 pr_debug("swsusp: resume file found\n");
954 else
955 pr_debug("swsusp: Error %d check for resume file\n", error);
956 return error;
957}
958
959/**
960 * swsusp_read - Read saved image from swap.
961 */
962
963int swsusp_read(void)
964{
965 int error;
966
967 if (IS_ERR(resume_bdev)) {
968 pr_debug("swsusp: block device not initialised\n");
969 return PTR_ERR(resume_bdev);
970 }
971
972 error = read_suspend_image();
973 blkdev_put(resume_bdev);
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700974 memset(key_iv, 0, MAXKEY+MAXIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 if (!error)
977 pr_debug("swsusp: Reading resume file was successful\n");
978 else
979 pr_debug("swsusp: Error %d resuming\n", error);
980 return error;
981}
982
983/**
984 * swsusp_close - close swap device.
985 */
986
987void swsusp_close(void)
988{
989 if (IS_ERR(resume_bdev)) {
990 pr_debug("swsusp: block device not initialised\n");
991 return;
992 }
993
994 blkdev_put(resume_bdev);
995}