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