Signed-off-by: Pekka Enberg --- include/linux/netlink.h | 2 include/linux/suspend.h | 66 + include/linux/swap.h | 6 kernel/power/Kconfig | 4 kernel/power/Makefile | 2 kernel/power/extent.h | 77 ++ kernel/power/io.c | 1399 +++++++++++++++++++++++++++++++++++++++++ kernel/power/io.h | 49 + kernel/power/modules.h | 164 ++++ kernel/power/netlink.h | 58 + kernel/power/pagedir.h | 53 + kernel/power/pageflags.h | 49 + kernel/power/power.h | 26 kernel/power/prepare_image.h | 30 kernel/power/snapshot.c | 20 kernel/power/storage.c | 288 ++++++++ kernel/power/storage.h | 53 + kernel/power/suspend.h | 180 +++++ kernel/power/suspend_builtin.h | 30 kernel/power/sysfs.h | 132 +++ kernel/power/ui.h | 106 +++ 21 files changed, 2781 insertions(+), 13 deletions(-) Index: 2.6/include/linux/netlink.h =================================================================== --- 2.6.orig/include/linux/netlink.h 2007-04-27 14:42:13.000000000 +0300 +++ 2.6/include/linux/netlink.h 2007-04-27 14:43:58.000000000 +0300 @@ -24,6 +24,8 @@ #define NETLINK_FIB_LOOKUP 10 /* leave room for NETLINK_DM (DM Events) */ #define NETLINK_SCSITRANSPORT 18 /* SCSI Transports */ #define NETLINK_ECRYPTFS 19 +#define NETLINK_SUSPEND2_USERUI 20 /* For suspend2's userui */ +#define NETLINK_SUSPEND2_USM 21 /* For suspend2's userspace storage manager */ #define MAX_LINKS 32 Index: 2.6/include/linux/suspend.h =================================================================== --- 2.6.orig/include/linux/suspend.h 2007-04-27 14:43:56.000000000 +0300 +++ 2.6/include/linux/suspend.h 2007-04-27 14:43:58.000000000 +0300 @@ -27,14 +27,9 @@ extern void mark_free_pages(struct zone /* kernel/power/swsusp.c */ extern int software_suspend(void); -#if defined(CONFIG_VT) && defined(CONFIG_VT_CONSOLE) extern int pm_prepare_console(void); extern void pm_restore_console(void); #else -static inline int pm_prepare_console(void) { return 0; } -static inline void pm_restore_console(void) {} -#endif /* defined(CONFIG_VT) && defined(CONFIG_VT_CONSOLE) */ -#else static inline int software_suspend(void) { printk("Warning: fake suspend called\n"); @@ -45,8 +40,6 @@ static inline int software_suspend(void) void save_processor_state(void); void restore_processor_state(void); struct saved_context; -void __save_processor_state(struct saved_context *ctxt); -void __restore_processor_state(struct saved_context *ctxt); unsigned long get_safe_page(gfp_t gfp_mask); /* @@ -63,4 +56,63 @@ #define PAGES_FOR_IO 1024 extern int software_resume(void); +enum { + SUSPEND_CAN_SUSPEND, + SUSPEND_CAN_RESUME, + SUSPEND_RUNNING, + SUSPEND_RESUME_DEVICE_OK, + SUSPEND_NORESUME_SPECIFIED, + SUSPEND_SANITY_CHECK_PROMPT, + SUSPEND_PAGESET2_NOT_LOADED, + SUSPEND_CONTINUE_REQ, + SUSPEND_RESUMED_BEFORE, + SUSPEND_RESUME_NOT_DONE, + SUSPEND_BOOT_TIME, + SUSPEND_NOW_RESUMING, + SUSPEND_IGNORE_LOGLEVEL, + SUSPEND_TRYING_TO_RESUME, + SUSPEND_TRY_RESUME_RD, + SUSPEND_LOADING_ALT_IMAGE, + SUSPEND_STOP_RESUME, + SUSPEND_IO_STOPPED, +}; + +#ifdef CONFIG_SUSPEND2 + +/* Used in init dir files */ +extern unsigned long suspend_state; + +#define set_suspend_state(bit) (set_bit(bit, &suspend_state)) +#define clear_suspend_state(bit) (clear_bit(bit, &suspend_state)) +#define test_suspend_state(bit) (test_bit(bit, &suspend_state)) + +extern void suspend2_try_resume(void); +extern int suspend2_running; +#else + +#define suspend_state (0) +#define set_suspend_state(bit) do { } while(0) +#define clear_suspend_state(bit) do { } while (0) +#define test_suspend_state(bit) (0) + +#define suspend2_running (0) + +#define suspend2_try_resume() do { } while(0) +#endif /* CONFIG_SUSPEND2 */ + +#ifdef CONFIG_SOFTWARE_SUSPEND +extern int software_resume(void); +#else +#ifdef CONFIG_SUSPEND2 +extern void suspend2_try_resume(void); +static inline int software_resume(void) +{ + suspend2_try_resume(); + return 0; +} +#else +#define software_resume() do { } while(0) +#endif +#endif + #endif /* _LINUX_SWSUSP_H */ Index: 2.6/include/linux/swap.h =================================================================== --- 2.6.orig/include/linux/swap.h 2007-04-27 14:42:13.000000000 +0300 +++ 2.6/include/linux/swap.h 2007-04-27 14:43:58.000000000 +0300 @@ -190,6 +190,7 @@ extern void swap_setup(void); /* linux/mm/vmscan.c */ extern unsigned long try_to_free_pages(struct zone **, gfp_t); extern unsigned long shrink_all_memory(unsigned long nr_pages); +extern void shrink_one_zone(struct zone *zone, int desired_size); extern int vm_swappiness; extern int remove_mapping(struct address_space *mapping, struct page *page); extern long vm_total_pages; @@ -368,5 +369,10 @@ #define has_swap_token(x) 0 #define disable_swap_token() do { } while(0) #endif /* CONFIG_SWAP */ + +/* For Suspend2 - unlink LRU pages while saving separately */ +void unlink_lru_lists(void); +void relink_lru_lists(void); + #endif /* __KERNEL__*/ #endif /* _LINUX_SWAP_H */ Index: 2.6/kernel/power/Kconfig =================================================================== --- 2.6.orig/kernel/power/Kconfig 2007-04-27 14:43:13.000000000 +0300 +++ 2.6/kernel/power/Kconfig 2007-04-27 14:43:58.000000000 +0300 @@ -121,6 +121,10 @@ config SOFTWARE_SUSPEND For more information take a look at . +config SUSPEND2 + bool + default y + config PM_STD_PARTITION string "Default resume partition" depends on SOFTWARE_SUSPEND Index: 2.6/kernel/power/Makefile =================================================================== --- 2.6.orig/kernel/power/Makefile 2007-04-27 14:42:13.000000000 +0300 +++ 2.6/kernel/power/Makefile 2007-04-27 14:43:58.000000000 +0300 @@ -5,6 +5,6 @@ endif obj-y := main.o process.o console.o obj-$(CONFIG_PM_LEGACY) += pm.o -obj-$(CONFIG_SOFTWARE_SUSPEND) += swsusp.o disk.o snapshot.o swap.o user.o +obj-$(CONFIG_SOFTWARE_SUSPEND) += swsusp.o disk.o snapshot.o swap.o user.o io.o storage.o obj-$(CONFIG_MAGIC_SYSRQ) += poweroff.o Index: 2.6/kernel/power/extent.h =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ 2.6/kernel/power/extent.h 2007-04-27 14:43:58.000000000 +0300 @@ -0,0 +1,77 @@ +/* + * kernel/power/extent.h + * + * Copyright (C) 2003-2007 Nigel Cunningham (nigel at suspend2 net) + * + * This file is released under the GPLv2. + * + * It contains declarations related to extents. Extents are + * suspend's method of storing some of the metadata for the image. + * See extent.c for more info. + * + */ + +#include "modules.h" + +#ifndef EXTENT_H +#define EXTENT_H + +struct extent { + unsigned long minimum, maximum; + struct extent *next; +}; + +struct extent_chain { + int size; /* size of the chain ie sum (max-min+1) */ + int num_extents; + struct extent *first, *last_touched; +}; + +struct extent_iterate_state { + struct extent_chain *chains; + int num_chains; + int current_chain; + struct extent *current_extent; + unsigned long current_offset; +}; + +struct extent_iterate_saved_state { + int chain_num; + int extent_num; + unsigned long offset; +}; + +#define suspend_extent_state_eof(state) ((state)->num_chains == (state)->current_chain) + +/* Simplify iterating through all the values in an extent chain */ +#define suspend_extent_for_each(extent_chain, extentpointer, value) \ +if ((extent_chain)->first) \ + for ((extentpointer) = (extent_chain)->first, (value) = \ + (extentpointer)->minimum; \ + ((extentpointer) && ((extentpointer)->next || (value) <= \ + (extentpointer)->maximum)); \ + (((value) == (extentpointer)->maximum) ? \ + ((extentpointer) = (extentpointer)->next, (value) = \ + ((extentpointer) ? (extentpointer)->minimum : 0)) : \ + (value)++)) + +void suspend_put_extent_chain(struct extent_chain *chain); +int suspend_add_to_extent_chain(struct extent_chain *chain, + unsigned long minimum, unsigned long maximum); +int suspend_serialise_extent_chain(struct suspend_module_ops *owner, + struct extent_chain *chain); +int suspend_load_extent_chain(struct extent_chain *chain); + +/* swap_entry_to_extent_val & extent_val_to_swap_entry: + * We are putting offset in the low bits so consecutive swap entries + * make consecutive extent values */ +#define swap_entry_to_extent_val(swp_entry) (swp_entry.val) +#define extent_val_to_swap_entry(val) (swp_entry_t) { (val) } + +void suspend_extent_state_save(struct extent_iterate_state *state, + struct extent_iterate_saved_state *saved_state); +void suspend_extent_state_restore(struct extent_iterate_state *state, + struct extent_iterate_saved_state *saved_state); +void suspend_extent_state_goto_start(struct extent_iterate_state *state); +unsigned long suspend_extent_state_next(struct extent_iterate_state *state); +#endif Index: 2.6/kernel/power/io.c =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ 2.6/kernel/power/io.c 2007-04-27 14:43:58.000000000 +0300 @@ -0,0 +1,1399 @@ +/* + * kernel/power/io.c + * + * Copyright (C) 1998-2001 Gabor Kuti + * Copyright (C) 1998,2001,2002 Pavel Machek + * Copyright (C) 2002-2003 Florent Chabaud + * Copyright (C) 2002-2007 Nigel Cunningham (nigel at suspend2 net) + * + * This file is released under the GPLv2. + * + * It contains high level IO routines for suspending. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "suspend.h" +#include "modules.h" +#include "pageflags.h" +#include "io.h" +#include "ui.h" +#include "storage.h" +#include "prepare_image.h" +#include "extent.h" +#include "sysfs.h" +#include "suspend_builtin.h" + +char poweroff_resume2[256]; + +/* Variables shared between threads and updated under the mutex */ +static int io_write, io_finish_at, io_base, io_barmax, io_pageset, io_result; +static int io_index, io_nextupdate, io_pc, io_pc_step; +static unsigned long pfn, other_pfn; +static DEFINE_MUTEX(io_mutex); +static DEFINE_PER_CPU(struct page *, last_sought); +static DEFINE_PER_CPU(struct page *, last_high_page); +static DEFINE_PER_CPU(struct pbe *, last_low_page); +static atomic_t worker_thread_count; +static atomic_t io_count; + +/* suspend_attempt_to_parse_resume_device + * + * Can we suspend, using the current resume2= parameter? + */ +int suspend_attempt_to_parse_resume_device(int quiet) +{ + struct list_head *Allocator; + struct suspend_module_ops *thisAllocator; + int result, returning = 0; + + if (suspend_activate_storage(0)) + return 0; + + suspendActiveAllocator = NULL; + clear_suspend_state(SUSPEND_RESUME_DEVICE_OK); + clear_suspend_state(SUSPEND_CAN_RESUME); + clear_result_state(SUSPEND_ABORTED); + + if (!suspendNumAllocators) { + if (!quiet) + printk("Suspend2: No storage allocators have been " + "registered. Suspending will be disabled.\n"); + goto cleanup; + } + + if (!resume2_file[0]) { + if (!quiet) + printk("Suspend2: Resume2 parameter is empty." + " Suspending will be disabled.\n"); + goto cleanup; + } + + list_for_each(Allocator, &suspendAllocators) { + thisAllocator = list_entry(Allocator, struct suspend_module_ops, + type_list); + + /* + * Not sure why you'd want to disable an allocator, but + * we should honour the flag if we're providing it + */ + if (!thisAllocator->enabled) + continue; + + result = thisAllocator->parse_sig_location( + resume2_file, (suspendNumAllocators == 1), + quiet); + + switch (result) { + case -EINVAL: + /* For this allocator, but not a valid + * configuration. Error already printed. */ + goto cleanup; + + case 0: + /* For this allocator and valid. */ + suspendActiveAllocator = thisAllocator; + + set_suspend_state(SUSPEND_RESUME_DEVICE_OK); + set_suspend_state(SUSPEND_CAN_RESUME); + if (!quiet) + printk("Suspend2: Resuming enabled.\n"); + + returning = 1; + goto cleanup; + } + } + if (!quiet) + printk("Suspend2: No matching enabled allocator found. " + "Resuming disabled.\n"); +cleanup: + suspend_deactivate_storage(0); + return returning; +} + +void attempt_to_parse_resume_device2(void) +{ + suspend_prepare_usm(); + suspend_attempt_to_parse_resume_device(0); + suspend_cleanup_usm(); +} + +void replace_restore_resume2(int replace, int quiet) +{ + static char resume2_save[255]; + static unsigned long suspend_state_save; + + if (replace) { + suspend_state_save = suspend_state; + strcpy(resume2_save, resume2_file); + strcpy(resume2_file, poweroff_resume2); + } else { + strcpy(resume2_file, resume2_save); + suspend_state = suspend_state_save; + } + suspend_attempt_to_parse_resume_device(quiet); +} + +void attempt_to_parse_po_resume_device2(void) +{ + int ok = 0; + + /* Temporarily set resume2 to the poweroff value */ + if (!strlen(poweroff_resume2)) + return; + + printk("=== Trying Poweroff Resume2 ===\n"); + replace_restore_resume2(1, 0); + if (test_suspend_state(SUSPEND_CAN_RESUME)) + ok = 1; + + printk("=== Done ===\n"); + replace_restore_resume2(0, 1); + + /* If not ok, clear the string */ + if (ok) + return; + + printk("Can't resume from that location; clearing poweroff_resume2.\n"); + poweroff_resume2[0] = '\0'; +} + +/* noresume_reset_modules + * + * Description: When we read the start of an image, modules (and especially the + * active allocator) might need to reset data structures if we + * decide to invalidate the image rather than resuming from it. + */ + +static void noresume_reset_modules(void) +{ + struct suspend_module_ops *this_filter; + + list_for_each_entry(this_filter, &suspend_filters, type_list) + if (this_filter->noresume_reset) + this_filter->noresume_reset(); + + if (suspendActiveAllocator && suspendActiveAllocator->noresume_reset) + suspendActiveAllocator->noresume_reset(); +} + +/* fill_suspend_header() + * + * Description: Fill the suspend header structure. + * Arguments: struct suspend_header: Header data structure to be filled. + */ + +static void fill_suspend_header(struct suspend_header *sh) +{ + int i; + + memset((char *)sh, 0, sizeof(*sh)); + + sh->version_code = LINUX_VERSION_CODE; + sh->num_physpages = num_physpages; + memcpy(&sh->uts, init_utsname(), sizeof(struct new_utsname)); + sh->page_size = PAGE_SIZE; + sh->pagedir = pagedir1; + sh->pageset_2_size = pagedir2.size; + sh->param0 = suspend_result; + sh->param1 = suspend_action; + sh->param2 = suspend_debug_state; + sh->param3 = console_loglevel; + sh->root_fs = current->fs->rootmnt->mnt_sb->s_dev; + for (i = 0; i < 4; i++) + sh->io_time[i/2][i%2] = suspend_io_time[i/2][i%2]; +} + +/* + * rw_init_modules + * + * Iterate over modules, preparing the ones that will be used to read or write + * data. + */ +static int rw_init_modules(int rw, int which) +{ + struct suspend_module_ops *this_module; + /* Initialise page transformers */ + list_for_each_entry(this_module, &suspend_filters, type_list) { + if (!this_module->enabled) + continue; + if (this_module->rw_init && this_module->rw_init(rw, which)) { + abort_suspend(SUSPEND_FAILED_MODULE_INIT, + "Failed to initialise the %s filter.", + this_module->name); + return 1; + } + } + + /* Initialise allocator */ + if (suspendActiveAllocator->rw_init(rw, which)) { + abort_suspend(SUSPEND_FAILED_MODULE_INIT, + "Failed to initialise the allocator."); + if (!rw) + suspendActiveAllocator->invalidate_image(); + return 1; + } + + /* Initialise other modules */ + list_for_each_entry(this_module, &suspend_modules, module_list) { + if (!this_module->enabled || + this_module->type == FILTER_MODULE || + this_module->type == WRITER_MODULE) + continue; + if (this_module->rw_init && this_module->rw_init(rw, which)) { + set_result_state(SUSPEND_ABORTED); + printk("Setting aborted flag due to module init failure.\n"); + return 1; + } + } + + return 0; +} + +/* + * rw_cleanup_modules + * + * Cleanup components after reading or writing a set of pages. + * Only the allocator may fail. + */ +static int rw_cleanup_modules(int rw) +{ + struct suspend_module_ops *this_module; + int result = 0; + + /* Cleanup other modules */ + list_for_each_entry(this_module, &suspend_modules, module_list) { + if (!this_module->enabled || + this_module->type == FILTER_MODULE || + this_module->type == WRITER_MODULE) + continue; + if (this_module->rw_cleanup) + result |= this_module->rw_cleanup(rw); + } + + /* Flush data and cleanup */ + list_for_each_entry(this_module, &suspend_filters, type_list) { + if (!this_module->enabled) + continue; + if (this_module->rw_cleanup) + result |= this_module->rw_cleanup(rw); + } + + result |= suspendActiveAllocator->rw_cleanup(rw); + + return result; +} + +static struct page *copy_page_from_orig_page(struct page *orig_page) +{ + int is_high = PageHighMem(orig_page), index, min, max; + struct page *high_page = NULL, + **my_last_high_page = &__get_cpu_var(last_high_page), + **my_last_sought = &__get_cpu_var(last_sought); + struct pbe *this, **my_last_low_page = &__get_cpu_var(last_low_page); + void *compare; + + if (is_high) { + if (*my_last_sought && *my_last_high_page && *my_last_sought < orig_page) + high_page = *my_last_high_page; + else + high_page = (struct page *) restore_highmem_pblist; + this = (struct pbe *) kmap(high_page); + compare = orig_page; + } else { + if (*my_last_sought && *my_last_low_page && *my_last_sought < orig_page) + this = *my_last_low_page; + else + this = restore_pblist; + compare = page_address(orig_page); + } + + *my_last_sought = orig_page; + + /* Locate page containing pbe */ + while ( this[PBES_PER_PAGE - 1].next && + this[PBES_PER_PAGE - 1].orig_address < compare) { + if (is_high) { + struct page *next_high_page = (struct page *) + this[PBES_PER_PAGE - 1].next; + kunmap(high_page); + this = kmap(next_high_page); + high_page = next_high_page; + } else + this = this[PBES_PER_PAGE - 1].next; + } + + /* Do a binary search within the page */ + min = 0; + max = PBES_PER_PAGE; + index = PBES_PER_PAGE / 2; + while (max - min) { + if (!this[index].orig_address || + this[index].orig_address > compare) + max = index; + else if (this[index].orig_address == compare) { + if (is_high) { + struct page *page = this[index].address; + *my_last_high_page = high_page; + kunmap(high_page); + return page; + } else { + *my_last_low_page = this; + return virt_to_page(this[index].address); + } + } else + min = index; + index = ((max + min) / 2); + }; + + if (is_high) + kunmap(high_page); + + abort_suspend(SUSPEND_FAILED_IO, "Failed to get destination page for" + " orig page %p. This[min].orig_address=%p.\n", orig_page, + this[index].orig_address); + return NULL; +} + +/* + * do_rw_loop + * + * The main I/O loop for reading or writing pages. + */ +static int worker_rw_loop(void *data) +{ + unsigned long orig_pfn, write_pfn; + int result, my_io_index = 0; + struct suspend_module_ops *first_filter = suspend_get_next_filter(NULL); + struct page *buffer = alloc_page(GFP_ATOMIC); + + atomic_inc(&worker_thread_count); + + mutex_lock(&io_mutex); + + do { + int buf_size; + + /* + * What page to use? If reading, don't know yet which page's + * data will be read, so always use the buffer. If writing, + * use the copy (Pageset1) or original page (Pageset2), but + * always write the pfn of the original page. + */ + if (io_write) { + struct page *page; + + pfn = get_next_bit_on(io_map, pfn); + + /* Another thread could have beaten us to it. */ + if (pfn == max_pfn + 1) { + BUG_ON(atomic_read(&io_count)); + break; + } + + atomic_dec(&io_count); + + orig_pfn = pfn; + write_pfn = pfn; + + /* + * Other_pfn is updated by all threads, so we're not + * writing the same page multiple times. + */ + clear_dynpageflag(&io_map, pfn_to_page(pfn)); + if (io_pageset == 1) { + other_pfn = get_next_bit_on(pageset1_map, other_pfn); + write_pfn = other_pfn; + } + page = pfn_to_page(pfn); + + my_io_index = io_finish_at - atomic_read(&io_count); + + mutex_unlock(&io_mutex); + + result = first_filter->write_chunk(write_pfn, page, + PAGE_SIZE); + } else { + atomic_dec(&io_count); + mutex_unlock(&io_mutex); + + /* + * Are we aborting? If so, don't submit any more I/O as + * resetting the resume_attempted flag (from ui.c) will + * clear the bdev flags, making this thread oops. + */ + if (unlikely(test_suspend_state(SUSPEND_STOP_RESUME))) { + atomic_dec(&worker_thread_count); + if (!atomic_read(&worker_thread_count)) + set_suspend_state(SUSPEND_IO_STOPPED); + while (1) + schedule(); + } + + result = first_filter->read_chunk(&write_pfn, buffer, + &buf_size, SUSPEND_ASYNC); + if (buf_size != PAGE_SIZE) { + abort_suspend(SUSPEND_FAILED_IO, + "I/O pipeline returned %d bytes instead " + "of %d.\n", buf_size, PAGE_SIZE); + mutex_lock(&io_mutex); + break; + } + } + + if (result) { + io_result = result; + if (io_write) { + printk("Write chunk returned %d.\n", result); + abort_suspend(SUSPEND_FAILED_IO, + "Failed to write a chunk of the " + "image."); + mutex_lock(&io_mutex); + break; + } else + panic("Read chunk returned (%d)", result); + } + + /* + * Discard reads of resaved pages while reading ps2 + * and unwanted pages while rereading ps2 when aborting. + */ + if (!io_write && !PageResave(pfn_to_page(write_pfn))) { + struct page *final_page = pfn_to_page(write_pfn), + *copy_page = final_page; + char *virt, *buffer_virt; + + if (io_pageset == 1 && !load_direct(final_page)) { + copy_page = copy_page_from_orig_page(final_page); + BUG_ON(!copy_page); + } + + if (test_dynpageflag(&io_map, final_page)) { + virt = kmap(copy_page); + buffer_virt = kmap(buffer); + memcpy(virt, buffer_virt, PAGE_SIZE); + kunmap(copy_page); + kunmap(buffer); + clear_dynpageflag(&io_map, final_page); + mutex_lock(&io_mutex); + my_io_index = io_finish_at - atomic_read(&io_count); + mutex_unlock(&io_mutex); + } else { + mutex_lock(&io_mutex); + atomic_inc(&io_count); + mutex_unlock(&io_mutex); + } + } + + /* Strictly speaking, this is racy - another thread could + * output the next the next percentage before we've done + * ours. 1/5th of the pageset would have to be done first, + * though, so I'm not worried. In addition, the only impact + * would be messed up output, not image corruption. Doing + * this under the mutex seems an unnecessary slowdown. + */ + if ((my_io_index + io_base) >= io_nextupdate) + io_nextupdate = suspend_update_status(my_io_index + + io_base, io_barmax, " %d/%d MB ", + MB(io_base+my_io_index+1), MB(io_barmax)); + + if ((my_io_index + 1) == io_pc) { + printk("%d%%...", 20 * io_pc_step); + io_pc_step++; + io_pc = io_finish_at * io_pc_step / 5; + } + + suspend_cond_pause(0, NULL); + + /* + * Subtle: If there's less I/O still to be done than threads + * running, quit. This stops us doing I/O beyond the end of + * the image when reading. + * + * Possible race condition. Two threads could do the test at + * the same time; one should exit and one should continue. + * Therefore we take the mutex before comparing and exiting. + */ + + mutex_lock(&io_mutex); + + } while(atomic_read(&io_count) >= atomic_read(&worker_thread_count) && + !(io_write && test_result_state(SUSPEND_ABORTED))); + + atomic_dec(&worker_thread_count); + mutex_unlock(&io_mutex); + + __free_pages(buffer, 0); + + return 0; +} + +void start_other_threads(void) +{ + int cpu; + struct task_struct *p; + + for_each_online_cpu(cpu) { + if (cpu == smp_processor_id()) + continue; + + p = kthread_create(worker_rw_loop, NULL, "ks2io/%d", cpu); + if (IS_ERR(p)) { + printk("ks2io for %i failed\n", cpu); + continue; + } + kthread_bind(p, cpu); + wake_up_process(p); + } +} + +/* + * do_rw_loop + * + * The main I/O loop for reading or writing pages. + */ +static int do_rw_loop(int write, int finish_at, dyn_pageflags_t *pageflags, + int base, int barmax, int pageset) +{ + int index = 0, cpu; + + if (!finish_at) + return 0; + + io_write = write; + io_finish_at = finish_at; + io_base = base; + io_barmax = barmax; + io_pageset = pageset; + io_index = 0; + io_pc = io_finish_at / 5; + io_pc_step = 1; + io_result = 0; + io_nextupdate = 0; + + for_each_online_cpu(cpu) { + per_cpu(last_sought, cpu) = NULL; + per_cpu(last_low_page, cpu) = NULL; + per_cpu(last_high_page, cpu) = NULL; + } + + /* Ensure all bits clear */ + pfn = get_next_bit_on(io_map, max_pfn + 1); + + while (pfn < max_pfn + 1) { + clear_dynpageflag(&io_map, pfn_to_page(pfn)); + pfn = get_next_bit_on(io_map, pfn); + } + + /* Set the bits for the pages to write */ + pfn = get_next_bit_on(*pageflags, max_pfn + 1); + + while (pfn < max_pfn + 1 && index < finish_at) { + set_dynpageflag(&io_map, pfn_to_page(pfn)); + pfn = get_next_bit_on(*pageflags, pfn); + index++; + } + + atomic_set(&io_count, finish_at); + + pfn = max_pfn + 1; + other_pfn = pfn; + + clear_suspend_state(SUSPEND_IO_STOPPED); + + if (!test_action_state(SUSPEND_NO_MULTITHREADED_IO)) + start_other_threads(); + worker_rw_loop(NULL); + + while (atomic_read(&worker_thread_count)) + schedule(); + + set_suspend_state(SUSPEND_IO_STOPPED); + if (unlikely(test_suspend_state(SUSPEND_STOP_RESUME))) { + while (1) + schedule(); + } + + if (!io_result) { + printk("done.\n"); + + suspend_update_status(io_base + io_finish_at, io_barmax, " %d/%d MB ", + MB(io_base + io_finish_at), MB(io_barmax)); + } + + if (io_write && test_result_state(SUSPEND_ABORTED)) + io_result = 1; + else /* All I/O done? */ + BUG_ON(get_next_bit_on(io_map, max_pfn + 1) != max_pfn + 1); + + return io_result; +} + +/* write_pageset() + * + * Description: Write a pageset to disk. + * Arguments: pagedir: Which pagedir to write.. + * Returns: Zero on success or -1 on failure. + */ + +int write_pageset(struct pagedir *pagedir) +{ + int finish_at, base = 0, start_time, end_time; + int barmax = pagedir1.size + pagedir2.size; + long error = 0; + dyn_pageflags_t *pageflags; + + /* + * Even if there is nothing to read or write, the allocator + * may need the init/cleanup for it's housekeeping. (eg: + * Pageset1 may start where pageset2 ends when writing). + */ + finish_at = pagedir->size; + + if (pagedir->id == 1) { + suspend_prepare_status(DONT_CLEAR_BAR, + "Writing kernel & process data..."); + base = pagedir2.size; + if (test_action_state(SUSPEND_TEST_FILTER_SPEED) || + test_action_state(SUSPEND_TEST_BIO)) + pageflags = &pageset1_map; + else + pageflags = &pageset1_copy_map; + } else { + suspend_prepare_status(CLEAR_BAR, "Writing caches..."); + pageflags = &pageset2_map; + } + + start_time = jiffies; + + if (rw_init_modules(1, pagedir->id)) { + abort_suspend(SUSPEND_FAILED_MODULE_INIT, + "Failed to initialise modules for writing."); + error = 1; + } + + if (!error) + error = do_rw_loop(1, finish_at, pageflags, base, barmax, + pagedir->id); + + if (rw_cleanup_modules(WRITE) && !error) { + abort_suspend(SUSPEND_FAILED_MODULE_CLEANUP, + "Failed to cleanup after writing."); + error = 1; + } + + end_time = jiffies; + + if ((end_time - start_time) && (!test_result_state(SUSPEND_ABORTED))) { + suspend_io_time[0][0] += finish_at, + suspend_io_time[0][1] += (end_time - start_time); + } + + return error; +} + +/* read_pageset() + * + * Description: Read a pageset from disk. + * Arguments: whichtowrite: Controls what debugging output is printed. + * overwrittenpagesonly: Whether to read the whole pageset or + * only part. + * Returns: Zero on success or -1 on failure. + */ + +static int read_pageset(struct pagedir *pagedir, int overwrittenpagesonly) +{ + int result = 0, base = 0, start_time, end_time; + int finish_at = pagedir->size; + int barmax = pagedir1.size + pagedir2.size; + dyn_pageflags_t *pageflags; + + if (pagedir->id == 1) { + suspend_prepare_status(CLEAR_BAR, + "Reading kernel & process data..."); + pageflags = &pageset1_map; + } else { + suspend_prepare_status(DONT_CLEAR_BAR, "Reading caches..."); + if (overwrittenpagesonly) + barmax = finish_at = min(pagedir1.size, + pagedir2.size); + else { + base = pagedir1.size; + } + pageflags = &pageset2_map; + } + + start_time = jiffies; + + if (rw_init_modules(0, pagedir->id)) { + suspendActiveAllocator->invalidate_image(); + result = 1; + } else + result = do_rw_loop(0, finish_at, pageflags, base, barmax, + pagedir->id); + + if (rw_cleanup_modules(READ) && !result) { + abort_suspend(SUSPEND_FAILED_MODULE_CLEANUP, + "Failed to cleanup after reading."); + result = 1; + } + + /* Statistics */ + end_time=jiffies; + + if ((end_time - start_time) && (!test_result_state(SUSPEND_ABORTED))) { + suspend_io_time[1][0] += finish_at, + suspend_io_time[1][1] += (end_time - start_time); + } + + return result; +} + +/* write_module_configs() + * + * Description: Store the configuration for each module in the image header. + * Returns: Int: Zero on success, Error value otherwise. + */ +static int write_module_configs(void) +{ + struct suspend_module_ops *this_module; + char *buffer = (char *) get_zeroed_page(GFP_ATOMIC); + int len, index = 1; + struct suspend_module_header suspend_module_header; + + if (!buffer) { + printk("Failed to allocate a buffer for saving " + "module configuration info.\n"); + return -ENOMEM; + } + + /* + * We have to know which data goes with which module, so we at + * least write a length of zero for a module. Note that we are + * also assuming every module's config data takes <= PAGE_SIZE. + */ + + /* For each module (in registration order) */ + list_for_each_entry(this_module, &suspend_modules, module_list) { + if (!this_module->enabled || !this_module->storage_needed || + (this_module->type == WRITER_MODULE && + suspendActiveAllocator != this_module)) + continue; + + /* Get the data from the module */ + len = 0; + if (this_module->save_config_info) + len = this_module->save_config_info(buffer); + + /* Save the details of the module */ + suspend_module_header.enabled = this_module->enabled; + suspend_module_header.type = this_module->type; + suspend_module_header.index = index++; + strncpy(suspend_module_header.name, this_module->name, + sizeof(suspend_module_header.name)); + suspendActiveAllocator->rw_header_chunk(WRITE, + this_module, + (char *) &suspend_module_header, + sizeof(suspend_module_header)); + + /* Save the size of the data and any data returned */ + suspendActiveAllocator->rw_header_chunk(WRITE, + this_module, + (char *) &len, sizeof(int)); + if (len) + suspendActiveAllocator->rw_header_chunk( + WRITE, this_module, buffer, len); + } + + /* Write a blank header to terminate the list */ + suspend_module_header.name[0] = '\0'; + suspendActiveAllocator->rw_header_chunk(WRITE, + NULL, + (char *) &suspend_module_header, + sizeof(suspend_module_header)); + + free_page((unsigned long) buffer); + return 0; +} + +/* read_module_configs() + * + * Description: Reload module configurations from the image header. + * Returns: Int. Zero on success, error value otherwise. + */ + +static int read_module_configs(void) +{ + struct suspend_module_ops *this_module; + char *buffer = (char *) get_zeroed_page(GFP_ATOMIC); + int len, result = 0; + struct suspend_module_header suspend_module_header; + + if (!buffer) { + printk("Failed to allocate a buffer for reloading module " + "configuration info.\n"); + return -ENOMEM; + } + + /* All modules are initially disabled. That way, if we have a module + * loaded now that wasn't loaded when we suspended, it won't be used + * in trying to read the data. + */ + list_for_each_entry(this_module, &suspend_modules, module_list) + this_module->enabled = 0; + + /* Get the first module header */ + result = suspendActiveAllocator->rw_header_chunk(READ, NULL, + (char *) &suspend_module_header, + sizeof(suspend_module_header)); + if (result) { + printk("Failed to read the next module header.\n"); + free_page((unsigned long) buffer); + return -EINVAL; + } + + /* For each module (in registration order) */ + while (suspend_module_header.name[0]) { + + /* Find the module */ + this_module = suspend_find_module_given_name(suspend_module_header.name); + + if (!this_module) { + /* + * Is it used? Only need to worry about filters. The active + * allocator must be loaded! + */ + if (suspend_module_header.enabled) { + suspend_early_boot_message(1, SUSPEND_CONTINUE_REQ, + "It looks like we need module %s for " + "reading the image but it hasn't been " + "registered.\n", + suspend_module_header.name); + if (!(test_suspend_state(SUSPEND_CONTINUE_REQ))) { + suspendActiveAllocator->invalidate_image(); + free_page((unsigned long) buffer); + return -EINVAL; + } + } else + printk("Module %s configuration data found, but" + " the module hasn't registered. Looks " + "like it was disabled, so we're " + "ignoring it's data.", + suspend_module_header.name); + } + + /* Get the length of the data (if any) */ + result = suspendActiveAllocator->rw_header_chunk(READ, NULL, + (char *) &len, sizeof(int)); + if (result) { + printk("Failed to read the length of the module %s's" + " configuration data.\n", + suspend_module_header.name); + free_page((unsigned long) buffer); + return -EINVAL; + } + + /* Read any data and pass to the module (if we found one) */ + if (len) { + suspendActiveAllocator->rw_header_chunk(READ, NULL, + buffer, len); + if (this_module) { + if (!this_module->save_config_info) { + printk("Huh? Module %s appears to have " + "a save_config_info, but not a " + "load_config_info function!\n", + this_module->name); + } else + this_module->load_config_info(buffer, len); + } + } + + if (this_module) { + /* Now move this module to the tail of its lists. This + * will put it in order. Any new modules will end up at + * the top of the lists. They should have been set to + * disabled when loaded (people will normally not edit + * an initrd to load a new module and then suspend + * without using it!). + */ + + suspend_move_module_tail(this_module); + + /* + * We apply the disabled state; modules don't need to + * save whether they were disabled and if they do, we + * override them anyway. + */ + this_module->enabled = suspend_module_header.enabled; + } + + /* Get the next module header */ + result = suspendActiveAllocator->rw_header_chunk(READ, NULL, + (char *) &suspend_module_header, + sizeof(suspend_module_header)); + + if (result) { + printk("Failed to read the next module header.\n"); + free_page((unsigned long) buffer); + return -EINVAL; + } + + } + + free_page((unsigned long) buffer); + return 0; +} + +/* write_image_header() + * + * Description: Write the image header after write the image proper. + * Returns: Int. Zero on success or -1 on failure. + */ + +int write_image_header(void) +{ + int ret; + int total = pagedir1.size + pagedir2.size+2; + char *header_buffer = NULL; + + /* Now prepare to write the header */ + if ((ret = suspendActiveAllocator->write_header_init())) { + abort_suspend(SUSPEND_FAILED_MODULE_INIT, + "Active allocator's write_header_init" + " function failed."); + goto write_image_header_abort; + } + + /* Get a buffer */ + header_buffer = (char *) get_zeroed_page(GFP_ATOMIC); + if (!header_buffer) { + abort_suspend(SUSPEND_OUT_OF_MEMORY, + "Out of memory when trying to get page for header!"); + goto write_image_header_abort; + } + + /* Write suspend header */ + fill_suspend_header((struct suspend_header *) header_buffer); + suspendActiveAllocator->rw_header_chunk(WRITE, NULL, + header_buffer, sizeof(struct suspend_header)); + + free_page((unsigned long) header_buffer); + + /* Write module configurations */ + if ((ret = write_module_configs())) { + abort_suspend(SUSPEND_FAILED_IO, + "Failed to write module configs."); + goto write_image_header_abort; + } + + save_dyn_pageflags(pageset1_map); + + /* Flush data and let allocator cleanup */ + if (suspendActiveAllocator->write_header_cleanup()) { + abort_suspend(SUSPEND_FAILED_IO, + "Failed to cleanup writing header."); + goto write_image_header_abort_no_cleanup; + } + + if (test_result_state(SUSPEND_ABORTED)) + goto write_image_header_abort_no_cleanup; + + suspend_message(SUSPEND_IO, SUSPEND_VERBOSE, 1, "|\n"); + suspend_update_status(total, total, NULL); + + return 0; + +write_image_header_abort: + suspendActiveAllocator->write_header_cleanup(); +write_image_header_abort_no_cleanup: + return -1; +} + +/* sanity_check() + * + * Description: Perform a few checks, seeking to ensure that the kernel being + * booted matches the one suspended. They need to match so we can + * be _sure_ things will work. It is not absolutely impossible for + * resuming from a different kernel to work, just not assured. + * Arguments: Struct suspend_header. The header which was saved at suspend + * time. + */ +static char *sanity_check(struct suspend_header *sh) +{ + if (sh->version_code != LINUX_VERSION_CODE) + return "Incorrect kernel version."; + + if (sh->num_physpages != num_physpages) + return "Incorrect memory size."; + + if (strncmp(sh->uts.sysname, init_utsname()->sysname, 65)) + return "Incorrect system type."; + + if (strncmp(sh->uts.release, init_utsname()->release, 65)) + return "Incorrect release."; + + if (strncmp(sh->uts.version, init_utsname()->version, 65)) + return "Right kernel version but wrong build number."; + + if (strncmp(sh->uts.machine, init_utsname()->machine, 65)) + return "Incorrect machine type."; + + if (sh->page_size != PAGE_SIZE) + return "Incorrect PAGE_SIZE."; + + if (!test_action_state(SUSPEND_IGNORE_ROOTFS)) { + const struct super_block *sb; + list_for_each_entry(sb, &super_blocks, s_list) { + if ((!(sb->s_flags & MS_RDONLY)) && + (sb->s_type->fs_flags & FS_REQUIRES_DEV)) + return "Device backed fs has been mounted " + "rw prior to resume or initrd/ramfs " + "is mounted rw."; + } + } + + return 0; +} + +/* __read_pageset1 + * + * Description: Test for the existence of an image and attempt to load it. + * Returns: Int. Zero if image found and pageset1 successfully loaded. + * Error if no image found or loaded. + */ +static int __read_pageset1(void) +{ + int i, result = 0; + char *header_buffer = (char *) get_zeroed_page(GFP_ATOMIC), + *sanity_error = NULL; + struct suspend_header *suspend_header; + + if (!header_buffer) { + printk("Unable to allocate a page for reading the signature.\n"); + return -ENOMEM; + } + + /* Check for an image */ + if (!(result = suspendActiveAllocator->image_exists())) { + result = -ENODATA; + noresume_reset_modules(); + printk("Suspend2: No image found.\n"); + goto out; + } + + /* Check for noresume command line option */ + if (test_suspend_state(SUSPEND_NORESUME_SPECIFIED)) { + printk("Suspend2: Noresume: Invalidated image.\n"); + goto out_invalidate_image; + } + + /* Check whether we've resumed before */ + if (test_suspend_state(SUSPEND_RESUMED_BEFORE)) { + int resumed_before_default = 0; + if (test_suspend_state(SUSPEND_RETRY_RESUME)) + resumed_before_default = SUSPEND_CONTINUE_REQ; + suspend_early_boot_message(1, resumed_before_default, NULL); + clear_suspend_state(SUSPEND_RETRY_RESUME); + if (!(test_suspend_state(SUSPEND_CONTINUE_REQ))) { + printk("Suspend2: Tried to resume before: " + "Invalidated image.\n"); + goto out_invalidate_image; + } + } + + clear_suspend_state(SUSPEND_CONTINUE_REQ); + + /* + * Prepare the active allocator for reading the image header. The + * activate allocator might read its own configuration. + * + * NB: This call may never return because there might be a signature + * for a different image such that we warn the user and they choose + * to reboot. (If the device ids look erroneous (2.4 vs 2.6) or the + * location of the image might be unavailable if it was stored on a + * network connection. + */ + + if ((result = suspendActiveAllocator->read_header_init())) { + printk("Suspend2: Failed to initialise, reading the image " + "header.\n"); + goto out_invalidate_image; + } + + /* Read suspend header */ + if ((result = suspendActiveAllocator->rw_header_chunk(READ, NULL, + header_buffer, sizeof(struct suspend_header))) < 0) { + printk("Suspend2: Failed to read the image signature.\n"); + goto out_invalidate_image; + } + + suspend_header = (struct suspend_header *) header_buffer; + + /* + * NB: This call may also result in a reboot rather than returning. + */ + + if ((sanity_error = sanity_check(suspend_header)) && + suspend_early_boot_message(1, SUSPEND_CONTINUE_REQ, sanity_error)) { + printk("Suspend2: Sanity check failed.\n"); + goto out_invalidate_image; + } + + /* + * We have an image and it looks like it will load okay. + * + * Get metadata from header. Don't override commandline parameters. + * + * We don't need to save the image size limit because it's not used + * during resume and will be restored with the image anyway. + */ + + memcpy((char *) &pagedir1, + (char *) &suspend_header->pagedir, sizeof(pagedir1)); + suspend_result = suspend_header->param0; + suspend_action = suspend_header->param1; + suspend_debug_state = suspend_header->param2; + console_loglevel = suspend_header->param3; + clear_suspend_state(SUSPEND_IGNORE_LOGLEVEL); + pagedir2.size = suspend_header->pageset_2_size; + for (i = 0; i < 4; i++) + suspend_io_time[i/2][i%2] = + suspend_header->io_time[i/2][i%2]; + + /* Read module configurations */ + if ((result = read_module_configs())) { + pagedir1.size = pagedir2.size = 0; + printk("Suspend2: Failed to read Suspend module " + "configurations.\n"); + clear_action_state(SUSPEND_KEEP_IMAGE); + goto out_invalidate_image; + } + + suspend_prepare_console(); + + set_suspend_state(SUSPEND_NOW_RESUMING); + + suspend_cond_pause(1, "About to read original pageset1 locations."); + + /* + * Read original pageset1 locations. These are the addresses we can't + * use for the data to be restored. + */ + + if (allocate_dyn_pageflags(&pageset1_map)) + goto out_invalidate_image; + if (allocate_dyn_pageflags(&pageset1_copy_map)) + goto out_invalidate_image; + if (allocate_dyn_pageflags(&io_map)) + goto out_invalidate_image; + if (load_dyn_pageflags(pageset1_map)) + goto out_reset_console; + + /* Clean up after reading the header */ + if ((result = suspendActiveAllocator->read_header_cleanup())) { + printk("Suspend2: Failed to cleanup after reading the image " + "header.\n"); + goto out_reset_console; + } + + suspend_cond_pause(1, "About to read pagedir."); + + /* + * Get the addresses of pages into which we will load the kernel to + * be copied back + */ + if (suspend_get_pageset1_load_addresses()) { + printk("Suspend2: Failed to get load addresses for pageset1.\n"); + goto out_reset_console; + } + + /* Read the original kernel back */ + suspend_cond_pause(1, "About to read pageset 1."); + + if (read_pageset(&pagedir1, 0)) { + suspend_prepare_status(CLEAR_BAR, "Failed to read pageset 1."); + result = -EIO; + printk("Suspend2: Failed to get load pageset1.\n"); + goto out_reset_console; + } + + suspend_cond_pause(1, "About to restore original kernel."); + result = 0; + + if (!test_action_state(SUSPEND_KEEP_IMAGE) && + suspendActiveAllocator->mark_resume_attempted) + suspendActiveAllocator->mark_resume_attempted(1); + +out: + free_page((unsigned long) header_buffer); + return result; + +out_reset_console: + suspend_cleanup_console(); + +out_invalidate_image: + free_dyn_pageflags(&pageset1_map); + free_dyn_pageflags(&pageset1_copy_map); + free_dyn_pageflags(&io_map); + result = -EINVAL; + if (!test_action_state(SUSPEND_KEEP_IMAGE)) + suspendActiveAllocator->invalidate_image(); + suspendActiveAllocator->read_header_cleanup(); + noresume_reset_modules(); + goto out; +} + +/* read_pageset1() + * + * Description: Attempt to read the header and pageset1 of a suspend image. + * Handle the outcome, complaining where appropriate. + */ + +int read_pageset1(void) +{ + int error; + + error = __read_pageset1(); + + switch (error) { + case 0: + case -ENODATA: + case -EINVAL: /* non fatal error */ + break; + default: + if (test_result_state(SUSPEND_ABORTED)) + break; + + abort_suspend(SUSPEND_IMAGE_ERROR, + "Suspend2: Error %d resuming\n", + error); + } + return error; +} + +/* + * get_have_image_data() + */ +static char *get_have_image_data(void) +{ + char *output_buffer = (char *) get_zeroed_page(GFP_ATOMIC); + struct suspend_header *suspend_header; + + if (!output_buffer) { + printk("Output buffer null.\n"); + return NULL; + } + + /* Check for an image */ + if (!suspendActiveAllocator->image_exists() || + suspendActiveAllocator->read_header_init() || + suspendActiveAllocator->rw_header_chunk(READ, NULL, + output_buffer, sizeof(struct suspend_header))) { + sprintf(output_buffer, "0\n"); + goto out; + } + + suspend_header = (struct suspend_header *) output_buffer; + + sprintf(output_buffer, "1\n%s\n%s\n", + suspend_header->uts.machine, + suspend_header->uts.version); + + /* Check whether we've resumed before */ + if (test_suspend_state(SUSPEND_RESUMED_BEFORE)) + strcat(output_buffer, "Resumed before.\n"); + +out: + noresume_reset_modules(); + return output_buffer; +} + +/* read_pageset2() + * + * Description: Read in part or all of pageset2 of an image, depending upon + * whether we are suspending and have only overwritten a portion + * with pageset1 pages, or are resuming and need to read them + * all. + * Arguments: Int. Boolean. Read only pages which would have been + * overwritten by pageset1? + * Returns: Int. Zero if no error, otherwise the error value. + */ +int read_pageset2(int overwrittenpagesonly) +{ + int result = 0; + + if (!pagedir2.size) + return 0; + + result = read_pageset(&pagedir2, overwrittenpagesonly); + + suspend_update_status(100, 100, NULL); + suspend_cond_pause(1, "Pagedir 2 read."); + + return result; +} + +/* image_exists_read + * + * Return 0 or 1, depending on whether an image is found. + * Incoming buffer is PAGE_SIZE and result is guaranteed + * to be far less than that, so we don't worry about + * overflow. + */ +int image_exists_read(const char *page, int count) +{ + int len = 0; + char *result; + + if (suspend_activate_storage(0)) + return count; + + if (!test_suspend_state(SUSPEND_RESUME_DEVICE_OK)) + suspend_attempt_to_parse_resume_device(0); + + if (!suspendActiveAllocator) { + len = sprintf((char *) page, "-1\n"); + } else { + result = get_have_image_data(); + if (result) { + len = sprintf((char *) page, "%s", result); + free_page((unsigned long) result); + } + } + + suspend_deactivate_storage(0); + + return len; +} + +/* image_exists_write + * + * Invalidate an image if one exists. + */ +int image_exists_write(const char *buffer, int count) +{ + if (suspend_activate_storage(0)) + return count; + + if (suspendActiveAllocator && suspendActiveAllocator->image_exists()) + suspendActiveAllocator->invalidate_image(); + + suspend_deactivate_storage(0); + + clear_result_state(SUSPEND_KEPT_IMAGE); + + return count; +} + +#ifdef CONFIG_SUSPEND2_EXPORTS +EXPORT_SYMBOL(suspend_attempt_to_parse_resume_device); +EXPORT_SYMBOL(attempt_to_parse_resume_device2); +#endif Index: 2.6/kernel/power/io.h =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ 2.6/kernel/power/io.h 2007-04-27 14:43:58.000000000 +0300 @@ -0,0 +1,49 @@ +/* + * kernel/power/io.h + * + * Copyright (C) 2005-2007 Nigel Cunningham (nigel at suspend2 net) + * + * This file is released under the GPLv2. + * + * It contains high level IO routines for suspending. + * + */ + +#include +#include "pagedir.h" + +/* Non-module data saved in our image header */ +struct suspend_header { + u32 version_code; + unsigned long num_physpages; + unsigned long orig_mem_free; + struct new_utsname uts; + int num_cpus; + int page_size; + int pageset_2_size; + int param0; + int param1; + int param2; + int param3; + int progress0; + int progress1; + int progress2; + int progress3; + int io_time[2][2]; + struct pagedir pagedir; + dev_t root_fs; +}; + +extern int write_pageset(struct pagedir *pagedir); +extern int write_image_header(void); +extern int read_pageset1(void); +extern int read_pageset2(int overwrittenpagesonly); + +extern int suspend_attempt_to_parse_resume_device(int quiet); +extern void attempt_to_parse_resume_device2(void); +extern void attempt_to_parse_po_resume_device2(void); +int image_exists_read(const char *page, int count); +int image_exists_write(const char *buffer, int count); +extern void replace_restore_resume2(int replace, int quiet); + +extern dev_t name_to_dev_t(char *line); Index: 2.6/kernel/power/modules.h =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ 2.6/kernel/power/modules.h 2007-04-27 14:43:58.000000000 +0300 @@ -0,0 +1,164 @@ +/* + * kernel/power/modules.h + * + * Copyright (C) 2004-2007 Nigel Cunningham (nigel at suspend2 net) + * + * This file is released under the GPLv2. + * + * It contains declarations for modules. Modules are additions to + * suspend2 that provide facilities such as image compression or + * encryption, backends for storage of the image and user interfaces. + * + */ + +#ifndef SUSPEND_MODULES_H +#define SUSPEND_MODULES_H + +/* This is the maximum size we store in the image header for a module name */ +#define SUSPEND_MAX_MODULE_NAME_LENGTH 30 + +/* Per-module metadata */ +struct suspend_module_header { + char name[SUSPEND_MAX_MODULE_NAME_LENGTH]; + int enabled; + int type; + int index; + int data_length; + unsigned long signature; +}; + +enum { + FILTER_MODULE, + WRITER_MODULE, + MISC_MODULE /* Block writer, eg. */ +}; + +enum { + SUSPEND_ASYNC, + SUSPEND_SYNC +}; + +struct suspend_module_ops { + /* Functions common to all modules */ + int type; + char *name; + char *directory; + char *shared_directory; + struct kobject *dir_kobj; + struct module *module; + int enabled; + struct list_head module_list; + + /* List of filters or allocators */ + struct list_head list, type_list; + + /* + * Requirements for memory and storage in + * the image header.. + */ + int (*memory_needed) (void); + int (*storage_needed) (void); + + int header_requested, header_used; + + int (*expected_compression) (void); + + /* + * Debug info + */ + int (*print_debug_info) (char *buffer, int size); + int (*save_config_info) (char *buffer); + void (*load_config_info) (char *buffer, int len); + + /* + * Initialise & cleanup - general routines called + * at the start and end of a cycle. + */ + int (*initialise) (int starting_cycle); + void (*cleanup) (int finishing_cycle); + + /* + * Calls for allocating storage (allocators only). + * + * Header space is allocated separately. Note that allocation + * of space for the header might result in allocated space + * being stolen from the main pool if there is no unallocated + * space. We have to be able to allocate enough space for + * the header. We can eat memory to ensure there is enough + * for the main pool. + */ + + int (*storage_available) (void); + int (*allocate_header_space) (int space_requested); + int (*allocate_storage) (int space_requested); + int (*storage_allocated) (void); + int (*release_storage) (void); + + /* + * Routines used in image I/O. + */ + int (*rw_init) (int rw, int stream_number); + int (*rw_cleanup) (int rw); + int (*write_chunk) (unsigned long index, struct page *buffer_page, + unsigned int buf_size); + int (*read_chunk) (unsigned long *index, struct page *buffer_page, + unsigned int *buf_size, int sync); + + /* Reset module if image exists but reading aborted */ + void (*noresume_reset) (void); + + /* Read and write the metadata */ + int (*write_header_init) (void); + int (*write_header_cleanup) (void); + + int (*read_header_init) (void); + int (*read_header_cleanup) (void); + + int (*rw_header_chunk) (int rw, struct suspend_module_ops *owner, + char *buffer_start, int buffer_size); + + /* Attempt to parse an image location */ + int (*parse_sig_location) (char *buffer, int only_writer, int quiet); + + /* Determine whether image exists that we can restore */ + int (*image_exists) (void); + + /* Mark the image as having tried to resume */ + void (*mark_resume_attempted) (int); + + /* Destroy image if one exists */ + int (*invalidate_image) (void); + + /* Sysfs Data */ + struct suspend_sysfs_data *sysfs_data; + int num_sysfs_entries; +}; + +extern int suspend_num_modules, suspendNumAllocators; + +extern struct suspend_module_ops *suspendActiveAllocator; +extern struct list_head suspend_filters, suspendAllocators, suspend_modules; + +extern void suspend_prepare_console_modules(void); +extern void suspend_cleanup_console_modules(void); + +extern struct suspend_module_ops *suspend_find_module_given_name(char *name); +extern struct suspend_module_ops *suspend_get_next_filter(struct suspend_module_ops *); + +extern int suspend_register_module(struct suspend_module_ops *module); +extern void suspend_move_module_tail(struct suspend_module_ops *module); + +extern int suspend_header_storage_for_modules(void); +extern int suspend_memory_for_modules(void); +extern int suspend_expected_compression_ratio(void); + +extern int suspend_print_module_debug_info(char *buffer, int buffer_size); +extern int suspend_register_module(struct suspend_module_ops *module); +extern void suspend_unregister_module(struct suspend_module_ops *module); + +extern int suspend_initialise_modules(int starting_cycle); +extern void suspend_cleanup_modules(int finishing_cycle); + +int suspend_get_modules(void); +void suspend_put_modules(void); +#endif Index: 2.6/kernel/power/netlink.h =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ 2.6/kernel/power/netlink.h 2007-04-27 14:43:58.000000000 +0300 @@ -0,0 +1,58 @@ +/* + * kernel/power/netlink.h + * + * Copyright (C) 2004-2007 Nigel Cunningham (nigel at suspend2 net) + * + * This file is released under the GPLv2. + * + * Declarations for functions for communicating with a userspace helper + * via netlink. + */ + +#include +#include + +#define NETLINK_MSG_BASE 0x10 + +#define NETLINK_MSG_READY 0x10 +#define NETLINK_MSG_NOFREEZE_ME 0x16 +#define NETLINK_MSG_GET_DEBUGGING 0x19 +#define NETLINK_MSG_CLEANUP 0x24 +#define NETLINK_MSG_NOFREEZE_ACK 0x27 +#define NETLINK_MSG_IS_DEBUGGING 0x28 + +struct user_helper_data { + int (*rcv_msg) (struct sk_buff *skb, struct nlmsghdr *nlh); + void (* not_ready) (void); + struct sock *nl; + u32 sock_seq; + pid_t pid; + char *comm; + char program[256]; + int pool_level; + int pool_limit; + struct sk_buff *emerg_skbs; + int skb_size; + int netlink_id; + char *name; + struct user_helper_data *next; + struct completion wait_for_process; + int interface_version; + int must_init; +}; + +#ifdef CONFIG_NET +int suspend_netlink_setup(struct user_helper_data *uhd); +void suspend_netlink_close(struct user_helper_data *uhd); +void suspend_send_netlink_message(struct user_helper_data *uhd, + int type, void* params, size_t len); +#else +static inline int suspend_netlink_setup(struct user_helper_data *uhd) +{ + return 0; +} + +static inline void suspend_netlink_close(struct user_helper_data *uhd) { }; +static inline void suspend_send_netlink_message(struct user_helper_data *uhd, + int type, void* params, size_t len) { }; +#endif Index: 2.6/kernel/power/pagedir.h =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ 2.6/kernel/power/pagedir.h 2007-04-27 14:43:58.000000000 +0300 @@ -0,0 +1,53 @@ +/* + * kernel/power/pagedir.h + * + * Copyright (C) 2006-2007 Nigel Cunningham (nigel at suspend2 net) + * + * This file is released under the GPLv2. + * + * Declarations for routines for handling pagesets. + */ + +#ifndef KERNEL_POWER_PAGEDIR_H +#define KERNEL_POWER_PAGEDIR_H + +/* Pagedir + * + * Contains the metadata for a set of pages saved in the image. + */ + +struct pagedir { + int id; + int size; +#ifdef CONFIG_HIGHMEM + int size_high; +#endif +}; + +#ifdef CONFIG_HIGHMEM +#define get_highmem_size(pagedir) (pagedir.size_high) +#define set_highmem_size(pagedir, sz) do { pagedir.size_high = sz; } while(0) +#define inc_highmem_size(pagedir) do { pagedir.size_high++; } while(0) +#define get_lowmem_size(pagedir) (pagedir.size - pagedir.size_high) +#else +#define get_highmem_size(pagedir) (0) +#define set_highmem_size(pagedir, sz) do { } while(0) +#define inc_highmem_size(pagedir) do { } while(0) +#define get_lowmem_size(pagedir) (pagedir.size) +#endif + +extern struct pagedir pagedir1, pagedir2; + +extern void suspend_copy_pageset1(void); + +extern void suspend_mark_pages_for_pageset2(void); + +extern int suspend_get_pageset1_load_addresses(void); + +extern unsigned long __suspend_get_nonconflicting_page(void); +struct page * ___suspend_get_nonconflicting_page(int can_be_highmem); + +extern void suspend_reset_alt_image_pageset2_pfn(void); + +extern struct page *suspend2_follow_page(struct mm_struct *mm, unsigned long address); +#endif Index: 2.6/kernel/power/pageflags.h =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ 2.6/kernel/power/pageflags.h 2007-04-27 14:43:58.000000000 +0300 @@ -0,0 +1,49 @@ +/* + * kernel/power/pageflags.h + * + * Copyright (C) 2004-2007 Nigel Cunningham (nigel at suspend2 net) + * + * This file is released under the GPLv2. + * + * Suspend2 needs a few pageflags while working that aren't otherwise + * used. To save the struct page pageflags, we dynamically allocate + * a bitmap and use that. These are the only non order-0 allocations + * we do. + * + * NOTE!!! + * We assume that PAGE_SIZE - sizeof(void *) is a multiple of + * sizeof(unsigned long). Is this ever false? + */ + +#include +#include + +extern dyn_pageflags_t pageset1_map; +extern dyn_pageflags_t pageset1_copy_map; +extern dyn_pageflags_t pageset2_map; +extern dyn_pageflags_t page_resave_map; +extern dyn_pageflags_t io_map; + +#define PagePageset1(page) (test_dynpageflag(&pageset1_map, page)) +#define SetPagePageset1(page) (set_dynpageflag(&pageset1_map, page)) +#define ClearPagePageset1(page) (clear_dynpageflag(&pageset1_map, page)) + +#define PagePageset1Copy(page) (test_dynpageflag(&pageset1_copy_map, page)) +#define SetPagePageset1Copy(page) (set_dynpageflag(&pageset1_copy_map, page)) +#define ClearPagePageset1Copy(page) (clear_dynpageflag(&pageset1_copy_map, page)) + +#define PagePageset2(page) (test_dynpageflag(&pageset2_map, page)) +#define SetPagePageset2(page) (set_dynpageflag(&pageset2_map, page)) +#define ClearPagePageset2(page) (clear_dynpageflag(&pageset2_map, page)) + +#define PageWasRW(page) (test_dynpageflag(&pageset2_map, page)) +#define SetPageWasRW(page) (set_dynpageflag(&pageset2_map, page)) +#define ClearPageWasRW(page) (clear_dynpageflag(&pageset2_map, page)) + +#define PageResave(page) (page_resave_map ? test_dynpageflag(&page_resave_map, page) : 0) +#define SetPageResave(page) (set_dynpageflag(&page_resave_map, page)) +#define ClearPageResave(page) (clear_dynpageflag(&page_resave_map, page)) + +extern void save_dyn_pageflags(dyn_pageflags_t pagemap); +extern int load_dyn_pageflags(dyn_pageflags_t pagemap); +extern int suspend_pageflags_space_needed(void); Index: 2.6/kernel/power/power.h =================================================================== --- 2.6.orig/kernel/power/power.h 2007-04-27 14:42:13.000000000 +0300 +++ 2.6/kernel/power/power.h 2007-04-27 14:43:58.000000000 +0300 @@ -1,5 +1,10 @@ +/* + * Copyright (C) 2004-2007 Nigel Cunningham (nigel at suspend2 net) + */ + #include #include +#include "suspend.h" struct swsusp_info { struct new_utsname uts; @@ -15,11 +20,15 @@ struct swsusp_info { #ifdef CONFIG_SOFTWARE_SUSPEND extern int pm_suspend_disk(void); - +extern char resume_file[256]; #else static inline int pm_suspend_disk(void) { - return -EPERM; +#ifdef CONFIG_SUSPEND2 + return suspend2_try_suspend(1); +#else + return -ENODEV; +#endif } #endif @@ -40,6 +49,8 @@ extern struct subsystem power_subsys; /* References to section boundaries */ extern const void __nosave_begin, __nosave_end; +extern struct pbe *restore_pblist; + /* Preferred image size in bytes (default 500 MB) */ extern unsigned long image_size; extern int in_suspend; @@ -177,3 +188,14 @@ extern int suspend_enter(suspend_state_t struct timeval; extern void swsusp_show_speed(struct timeval *, struct timeval *, unsigned int, char *); +extern struct page *saveable_page(unsigned long pfn); +#ifdef CONFIG_HIGHMEM +extern struct page *saveable_highmem_page(unsigned long pfn); +#else +static inline void *saveable_highmem_page(unsigned long pfn) { return NULL; } +#endif +extern unsigned long suspend_get_nonconflicting_page(void); +extern int suspend_post_context_save(void); +extern int suspend2_try_suspend(int have_pmsem); + +#define PBES_PER_PAGE (PAGE_SIZE / sizeof(struct pbe)) Index: 2.6/kernel/power/prepare_image.h =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ 2.6/kernel/power/prepare_image.h 2007-04-27 14:43:58.000000000 +0300 @@ -0,0 +1,30 @@ +/* + * kernel/power/prepare_image.h + * + * Copyright (C) 2003-2007 Nigel Cunningham (nigel at suspend2 net) + * + * This file is released under the GPLv2. + * + */ + +#include + +extern int suspend_prepare_image(void); +extern void suspend_recalculate_image_contents(int storage_available); +extern int real_nr_free_pages(unsigned long zone_idx_mask); +extern int image_size_limit; +extern void suspend_free_extra_pagedir_memory(void); +extern int extra_pd1_pages_allowance; + +#define MIN_FREE_RAM 100 +#define MIN_EXTRA_PAGES_ALLOWANCE 500 + +#define all_zones_mask ((unsigned long) ((1 << MAX_NR_ZONES) - 1)) +#ifdef CONFIG_HIGHMEM +#define real_nr_free_high_pages() (real_nr_free_pages(1 << ZONE_HIGHMEM)) +#define real_nr_free_low_pages() (real_nr_free_pages(all_zones_mask - \ + (1 << ZONE_HIGHMEM))) +#else +#define real_nr_free_high_pages() (0) +#define real_nr_free_low_pages() (real_nr_free_pages(all_zones_mask)) +#endif Index: 2.6/kernel/power/snapshot.c =================================================================== --- 2.6.orig/kernel/power/snapshot.c 2007-04-27 14:42:13.000000000 +0300 +++ 2.6/kernel/power/snapshot.c 2007-04-27 14:43:58.000000000 +0300 @@ -41,6 +41,11 @@ * Copyright (C) 2006 Rafael J. Wysocki */ struct pbe *restore_pblist; +#ifdef CONFIG_SUSPEND2 +#include "pagedir.h" +int suspend_post_context_save(void); +#endif + /* Pointer to an auxiliary buffer (1 page) */ static void *buffer; @@ -82,6 +87,11 @@ static void *get_image_page(gfp_t gfp_ma unsigned long get_safe_page(gfp_t gfp_mask) { +#ifdef CONFIG_SUSPEND2 + if (suspend2_running) + return suspend_get_nonconflicting_page(); +#endif + return (unsigned long)get_image_page(gfp_mask, PG_SAFE); } @@ -604,7 +614,7 @@ unsigned int cnt = 0; * and it isn't a part of a free chunk of pages. */ -static struct page *saveable_highmem_page(unsigned long pfn) +struct page *saveable_highmem_page(unsigned long pfn) { struct page *page; @@ -646,7 +656,6 @@ unsigned int n = 0; return n; } #else -static inline void *saveable_highmem_page(unsigned long pfn) { return NULL; } static inline unsigned int count_highmem_pages(void) { return 0; } #endif /* CONFIG_HIGHMEM */ @@ -670,7 +679,7 @@ static inline int pfn_is_nosave(unsigned * a free chunk of pages. */ -static struct page *saveable_page(unsigned long pfn) +struct page *saveable_page(unsigned long pfn) { struct page *page; @@ -986,6 +995,11 @@ asmlinkage int swsusp_save(void) { unsigned int nr_pages, nr_highmem; +#ifdef CONFIG_SUSPEND2 + if (suspend2_running) + return suspend_post_context_save(); +#endif + printk("swsusp: critical section: \n"); drain_local_pages(); Index: 2.6/kernel/power/storage.c =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ 2.6/kernel/power/storage.c 2007-04-27 14:43:58.000000000 +0300 @@ -0,0 +1,288 @@ +/* + * kernel/power/storage.c + * + * Copyright (C) 2005-2007 Nigel Cunningham (nigel at suspend2 net) + * + * This file is released under the GPLv2. + * + * Routines for talking to a userspace program that manages storage. + * + * The kernel side: + * - starts the userspace program; + * - sends messages telling it when to open and close the connection; + * - tells it when to quit; + * + * The user space side: + * - passes messages regarding status; + * + */ + +#include +#include + +#include "sysfs.h" +#include "modules.h" +#include "netlink.h" +#include "storage.h" +#include "ui.h" + +static struct user_helper_data usm_helper_data; +static struct suspend_module_ops usm_ops; +static int message_received = 0; +static int usm_prepare_count = 0; +static int storage_manager_last_action = 0; +static int storage_manager_action = 0; + +static int usm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) +{ + int type; + int *data; + + type = nlh->nlmsg_type; + + /* A control message: ignore them */ + if (type < NETLINK_MSG_BASE) + return 0; + + /* Unknown message: reply with EINVAL */ + if (type >= USM_MSG_MAX) + return -EINVAL; + + /* All operations require privileges, even GET */ + if (security_netlink_recv(skb, CAP_NET_ADMIN)) + return -EPERM; + + /* Only allow one task to receive NOFREEZE privileges */ + if (type == NETLINK_MSG_NOFREEZE_ME && usm_helper_data.pid != -1) + return -EBUSY; + + data = (int*)NLMSG_DATA(nlh); + + switch (type) { + case USM_MSG_SUCCESS: + case USM_MSG_FAILED: + message_received = type; + complete(&usm_helper_data.wait_for_process); + break; + default: + printk("Storage manager doesn't recognise message %d.\n", type); + } + + return 1; +} + +#ifdef CONFIG_NET +static int activations = 0; + +int suspend_activate_storage(int force) +{ + int tries = 1; + + if (usm_helper_data.pid == -1 || !usm_ops.enabled) + return 0; + + message_received = 0; + activations++; + + if (activations > 1 && !force) + return 0; + + while ((!message_received || message_received == USM_MSG_FAILED) && tries < 2) { + suspend_prepare_status(DONT_CLEAR_BAR, "Activate storage attempt %d.\n", tries); + + init_completion(&usm_helper_data.wait_for_process); + + suspend_send_netlink_message(&usm_helper_data, + USM_MSG_CONNECT, + NULL, 0); + + /* Wait 2 seconds for the userspace process to make contact */ + wait_for_completion_timeout(&usm_helper_data.wait_for_process, 2*HZ); + + tries++; + } + + return 0; +} + +int suspend_deactivate_storage(int force) +{ + if (usm_helper_data.pid == -1 || !usm_ops.enabled) + return 0; + + message_received = 0; + activations--; + + if (activations && !force) + return 0; + + init_completion(&usm_helper_data.wait_for_process); + + suspend_send_netlink_message(&usm_helper_data, + USM_MSG_DISCONNECT, + NULL, 0); + + wait_for_completion_timeout(&usm_helper_data.wait_for_process, 2*HZ); + + if (!message_received || message_received == USM_MSG_FAILED) { + printk("Returning failure disconnecting storage.\n"); + return 1; + } + + return 0; +} +#endif + +static void storage_manager_simulate(void) +{ + printk("--- Storage manager simulate ---\n"); + suspend_prepare_usm(); + schedule(); + printk("--- Activate storage 1 ---\n"); + suspend_activate_storage(1); + schedule(); + printk("--- Deactivate storage 1 ---\n"); + suspend_deactivate_storage(1); + schedule(); + printk("--- Cleanup usm ---\n"); + suspend_cleanup_usm(); + schedule(); + printk("--- Storage manager simulate ends ---\n"); +} + +static int usm_storage_needed(void) +{ + return strlen(usm_helper_data.program); +} + +static int usm_save_config_info(char *buf) +{ + int len = strlen(usm_helper_data.program); + memcpy(buf, usm_helper_data.program, len); + return len; +} + +static void usm_load_config_info(char *buf, int size) +{ + /* Don't load the saved path if one has already been set */ + if (usm_helper_data.program[0]) + return; + + memcpy(usm_helper_data.program, buf, size); +} + +static int usm_memory_needed(void) +{ + /* ball park figure of 32 pages */ + return (32 * PAGE_SIZE); +} + +/* suspend_prepare_usm + */ +int suspend_prepare_usm(void) +{ + usm_prepare_count++; + + if (usm_prepare_count > 1 || !usm_ops.enabled) + return 0; + + usm_helper_data.pid = -1; + + if (!*usm_helper_data.program) + return 0; + + suspend_netlink_setup(&usm_helper_data); + + if (usm_helper_data.pid == -1) + printk("Suspend2 Storage Manager wanted, but couldn't start it.\n"); + + suspend_activate_storage(0); + + return (usm_helper_data.pid != -1); +} + +void suspend_cleanup_usm(void) +{ + usm_prepare_count--; + + if (usm_helper_data.pid > -1 && !usm_prepare_count) { + suspend_deactivate_storage(0); + suspend_netlink_close(&usm_helper_data); + } +} + +static void storage_manager_activate(void) +{ + if (storage_manager_action == storage_manager_last_action) + return; + + if (storage_manager_action) + suspend_prepare_usm(); + else + suspend_cleanup_usm(); + + storage_manager_last_action = storage_manager_action; +} + +/* + * User interface specific /sys/power/suspend2 entries. + */ + +static struct suspend_sysfs_data sysfs_params[] = { + { SUSPEND2_ATTR("simulate_atomic_copy", SYSFS_RW), + .type = SUSPEND_SYSFS_DATA_NONE, + .write_side_effect = storage_manager_simulate, + }, + + { SUSPEND2_ATTR("enabled", SYSFS_RW), + SYSFS_INT(&usm_ops.enabled, 0, 1, 0) + }, + + { SUSPEND2_ATTR("program", SYSFS_RW), + SYSFS_STRING(usm_helper_data.program, 254, 0) + }, + + { SUSPEND2_ATTR("activate_storage", SYSFS_RW), + SYSFS_INT(&storage_manager_action, 0, 1, 0), + .write_side_effect = storage_manager_activate, + } +}; + +static struct suspend_module_ops usm_ops = { + .type = MISC_MODULE, + .name = "Userspace Storage Manager", + .directory = "storage_manager", + .module = THIS_MODULE, + .storage_needed = usm_storage_needed, + .save_config_info = usm_save_config_info, + .load_config_info = usm_load_config_info, + .memory_needed = usm_memory_needed, + + .sysfs_data = sysfs_params, + .num_sysfs_entries = sizeof(sysfs_params) / sizeof(struct suspend_sysfs_data), +}; + +/* suspend_usm_sysfs_init + * Description: Boot time initialisation for user interface. + */ +int s2_usm_init(void) +{ + usm_helper_data.nl = NULL; + usm_helper_data.program[0] = '\0'; + usm_helper_data.pid = -1; + usm_helper_data.skb_size = 0; + usm_helper_data.pool_limit = 6; + usm_helper_data.netlink_id = NETLINK_SUSPEND2_USM; + usm_helper_data.name = "userspace storage manager"; + usm_helper_data.rcv_msg = usm_user_rcv_msg; + usm_helper_data.interface_version = 1; + usm_helper_data.must_init = 0; + init_completion(&usm_helper_data.wait_for_process); + + return suspend_register_module(&usm_ops); +} + +void s2_usm_exit(void) +{ + suspend_unregister_module(&usm_ops); +} Index: 2.6/kernel/power/storage.h =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ 2.6/kernel/power/storage.h 2007-04-27 14:43:58.000000000 +0300 @@ -0,0 +1,53 @@ +/* + * kernel/power/storage.h + * + * Copyright (C) 2005-2007 Nigel Cunningham (nigel at suspend2 net) + * + * This file is released under the GPLv2. + */ + +#ifdef CONFIG_NET +int suspend_prepare_usm(void); +void suspend_cleanup_usm(void); + +int suspend_activate_storage(int force); +int suspend_deactivate_storage(int force); +extern int s2_usm_init(void); +extern void s2_usm_exit(void); +#else +static inline int s2_usm_init(void) { return 0; } +static inline void s2_usm_exit(void) { } + +static inline int suspend_activate_storage(int force) +{ + return 0; +} + +static inline int suspend_deactivate_storage(int force) +{ + return 0; +} + +static inline int suspend_prepare_usm(void) { return 0; } +static inline void suspend_cleanup_usm(void) { } +#endif + +enum { + USM_MSG_BASE = 0x10, + + /* Kernel -> Userspace */ + USM_MSG_CONNECT = 0x30, + USM_MSG_DISCONNECT = 0x31, + USM_MSG_SUCCESS = 0x40, + USM_MSG_FAILED = 0x41, + + USM_MSG_MAX, +}; + +#ifdef CONFIG_NET +extern __init int suspend_usm_init(void); +extern __exit void suspend_usm_cleanup(void); +#else +#define suspend_usm_init() do { } while(0) +#define suspend_usb_cleanup() do { } while(0) +#endif Index: 2.6/kernel/power/suspend.h =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ 2.6/kernel/power/suspend.h 2007-04-27 14:43:58.000000000 +0300 @@ -0,0 +1,180 @@ +/* + * kernel/power/suspend.h + * + * Copyright (C) 2004-2007 Nigel Cunningham (nigel at suspend2 net) + * + * This file is released under the GPLv2. + * + * It contains declarations used throughout swsusp. + * + */ + +#ifndef KERNEL_POWER_SUSPEND_H +#define KERNEL_POWER_SUSPEND_H + +#include +#include +#include +#include +#include +#include "pageflags.h" + +#define SUSPEND_CORE_VERSION "2.2.9.13" + +/* == Action states == */ + +enum { + SUSPEND_REBOOT, + SUSPEND_PAUSE, + SUSPEND_SLOW, + SUSPEND_LOGALL, + SUSPEND_CAN_CANCEL, + SUSPEND_KEEP_IMAGE, + SUSPEND_FREEZER_TEST, + SUSPEND_SINGLESTEP, + SUSPEND_PAUSE_NEAR_PAGESET_END, + SUSPEND_TEST_FILTER_SPEED, + SUSPEND_TEST_BIO, + SUSPEND_NO_PAGESET2, + SUSPEND_PM_PREPARE_CONSOLE, + SUSPEND_IGNORE_ROOTFS, + SUSPEND_REPLACE_SWSUSP, + SUSPEND_RETRY_RESUME, + SUSPEND_PAGESET2_FULL, + SUSPEND_ABORT_ON_RESAVE_NEEDED, + SUSPEND_NO_MULTITHREADED_IO, + SUSPEND_NO_DIRECT_LOAD, + SUSPEND_LATE_CPU_HOTPLUG, +}; + +extern unsigned long suspend_action; + +#define clear_action_state(bit) (test_and_clear_bit(bit, &suspend_action)) +#define test_action_state(bit) (test_bit(bit, &suspend_action)) + +/* == Result states == */ + +enum { + SUSPEND_ABORTED, + SUSPEND_ABORT_REQUESTED, + SUSPEND_NOSTORAGE_AVAILABLE, + SUSPEND_INSUFFICIENT_STORAGE, + SUSPEND_FREEZING_FAILED, + SUSPEND_UNEXPECTED_ALLOC, + SUSPEND_KEPT_IMAGE, + SUSPEND_WOULD_EAT_MEMORY, + SUSPEND_UNABLE_TO_FREE_ENOUGH_MEMORY, + SUSPEND_ENCRYPTION_SETUP_FAILED, + SUSPEND_PM_SEM, + SUSPEND_DEVICE_REFUSED, + SUSPEND_EXTRA_PAGES_ALLOW_TOO_SMALL, + SUSPEND_UNABLE_TO_PREPARE_IMAGE, + SUSPEND_FAILED_MODULE_INIT, + SUSPEND_FAILED_MODULE_CLEANUP, + SUSPEND_FAILED_IO, + SUSPEND_OUT_OF_MEMORY, + SUSPEND_IMAGE_ERROR, +}; + +extern unsigned long suspend_result; + +#define set_result_state(bit) (test_and_set_bit(bit, &suspend_result)) +#define clear_result_state(bit) (test_and_clear_bit(bit, &suspend_result)) +#define test_result_state(bit) (test_bit(bit, &suspend_result)) + +/* == Debug sections and levels == */ + +/* debugging levels. */ +enum { + SUSPEND_STATUS = 0, + SUSPEND_ERROR = 2, + SUSPEND_LOW, + SUSPEND_MEDIUM, + SUSPEND_HIGH, + SUSPEND_VERBOSE, +}; + +enum { + SUSPEND_ANY_SECTION, + SUSPEND_EAT_MEMORY, + SUSPEND_IO, + SUSPEND_HEADER, + SUSPEND_WRITER, + SUSPEND_MEMORY, +}; + +extern unsigned long suspend_debug_state; + +#define set_debug_state(bit) (test_and_set_bit(bit, &suspend_debug_state)) +#define clear_debug_state(bit) (test_and_clear_bit(bit, &suspend_debug_state)) +#define test_debug_state(bit) (test_bit(bit, &suspend_debug_state)) + +/* == Steps in suspending == */ + +enum { + STEP_SUSPEND_PREPARE_IMAGE, + STEP_SUSPEND_SAVE_IMAGE, + STEP_SUSPEND_POWERDOWN, + STEP_RESUME_CAN_RESUME, + STEP_RESUME_LOAD_PS1, + STEP_RESUME_DO_RESTORE, + STEP_RESUME_READ_PS2, + STEP_RESUME_GO, + STEP_RESUME_ALT_IMAGE, +}; + +/* == Suspend states == + (see also include/linux/suspend.h) */ + +#define get_suspend_state() (suspend_state) +#define restore_suspend_state(saved_state) \ + do { suspend_state = saved_state; } while(0) + +/* == Module support == */ + +struct suspend2_core_fns { + int (*post_context_save)(void); + unsigned long (*get_nonconflicting_page)(void); + int (*try_suspend)(int have_pmsem); + void (*try_resume)(void); +}; + +extern struct suspend2_core_fns *s2_core_fns; + +/* == All else == */ +#define KB(x) ((x) << (PAGE_SHIFT - 10)) +#define MB(x) ((x) >> (20 - PAGE_SHIFT)) + +extern int suspend_start_anything(int suspend_or_resume); +extern void suspend_finish_anything(int suspend_or_resume); + +extern int save_image_part1(void); +extern int suspend_atomic_restore(void); + +extern int __suspend2_try_suspend(int had_pmsem); +extern int suspend2_try_suspend(int have_pmsem); +extern void __suspend_try_resume(void); + +extern int __suspend_post_context_save(void); + +extern unsigned int nr_suspends; +extern char resume2_file[256]; +extern char poweroff_resume2[256]; + +extern void copyback_post(void); +extern int suspend2_suspend(void); +extern int extra_pd1_pages_used; + +extern int suspend_io_time[2][2]; + +#define SECTOR_SIZE 512 + +extern int suspend_early_boot_message + (int can_erase_image, int default_answer, char *warning_reason, ...); + +static inline int load_direct(struct page *page) +{ + return test_action_state(SUSPEND_NO_DIRECT_LOAD) ? 0 : PagePageset1Copy(page); +} + +#endif Index: 2.6/kernel/power/suspend_builtin.h =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ 2.6/kernel/power/suspend_builtin.h 2007-04-27 14:43:58.000000000 +0300 @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2004-2007 Nigel Cunningham (nigel at suspend2 net) + * + * This file is released under the GPLv2. + */ +#include +#include + +extern struct suspend2_core_fns *s2_core_fns; +extern unsigned long suspend_compress_bytes_in, suspend_compress_bytes_out; +extern unsigned int nr_suspends; +extern char resume2_file[256]; +extern int suspend2_in_suspend; + +extern unsigned long suspend2_nosave_state1 __nosavedata; +extern unsigned long suspend2_nosave_state2 __nosavedata; +extern int suspend2_nosave_state3 __nosavedata; +extern int suspend2_nosave_io_speed[2][2] __nosavedata; +extern __nosavedata char suspend2_nosave_commandline[COMMAND_LINE_SIZE]; +extern __nosavedata struct pbe *restore_highmem_pblist; + +int suspend2_lowlevel_builtin(void); + +extern dyn_pageflags_t __nosavedata suspend2_nosave_origmap; +extern dyn_pageflags_t __nosavedata suspend2_nosave_copymap; + +#ifdef CONFIG_HIGHMEM +extern __nosavedata struct zone_data *suspend2_nosave_zone_list; +extern __nosavedata unsigned long suspend2_nosave_max_pfn; +#endif Index: 2.6/kernel/power/sysfs.h =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ 2.6/kernel/power/sysfs.h 2007-04-27 14:43:58.000000000 +0300 @@ -0,0 +1,132 @@ +/* + * kernel/power/sysfs.h + * + * Copyright (C) 2004-2007 Nigel Cunningham (nigel at suspend2 net) + * + * This file is released under the GPLv2. + * + * It provides declarations for suspend to use in managing + * /sysfs/suspend2. When we switch to kobjects, + * this will become redundant. + * + */ + +#include +#include "power.h" + +struct suspend_sysfs_data { + struct attribute attr; + int type; + int flags; + union { + struct { + unsigned long *bit_vector; + int bit; + } bit; + struct { + int *variable; + int minimum; + int maximum; + } integer; + struct { + long *variable; + long minimum; + long maximum; + } a_long; + struct { + unsigned long *variable; + unsigned long minimum; + unsigned long maximum; + } ul; + struct { + char *variable; + int max_length; + } string; + struct { + int (*read_sysfs) (const char *buffer, int count); + int (*write_sysfs) (const char *buffer, int count); + void *data; + } special; + } data; + + /* Side effects routines. Used, eg, for reparsing the + * resume2 entry when it changes */ + void (*read_side_effect) (void); + void (*write_side_effect) (void); + struct list_head sysfs_data_list; +}; + +enum { + SUSPEND_SYSFS_DATA_NONE = 1, + SUSPEND_SYSFS_DATA_CUSTOM, + SUSPEND_SYSFS_DATA_BIT, + SUSPEND_SYSFS_DATA_INTEGER, + SUSPEND_SYSFS_DATA_UL, + SUSPEND_SYSFS_DATA_LONG, + SUSPEND_SYSFS_DATA_STRING +}; + +#define SUSPEND2_ATTR(_name, _mode) \ + .attr = {.name = _name , .mode = _mode } + +#define SYSFS_BIT(_ul, _bit, _flags) \ + .type = SUSPEND_SYSFS_DATA_BIT, \ + .flags = _flags, \ + .data = { .bit = { .bit_vector = _ul, .bit = _bit } } + +#define SYSFS_INT(_int, _min, _max, _flags) \ + .type = SUSPEND_SYSFS_DATA_INTEGER, \ + .flags = _flags, \ + .data = { .integer = { .variable = _int, .minimum = _min, \ + .maximum = _max } } + +#define SYSFS_UL(_ul, _min, _max, _flags) \ + .type = SUSPEND_SYSFS_DATA_UL, \ + .flags = _flags, \ + .data = { .ul = { .variable = _ul, .minimum = _min, \ + .maximum = _max } } + +#define SYSFS_LONG(_long, _min, _max, _flags) \ + .type = SUSPEND_SYSFS_DATA_LONG, \ + .flags = _flags, \ + .data = { .a_long = { .variable = _long, .minimum = _min, \ + .maximum = _max } } + +#define SYSFS_STRING(_string, _max_len, _flags) \ + .type = SUSPEND_SYSFS_DATA_STRING, \ + .flags = _flags, \ + .data = { .string = { .variable = _string, .max_length = _max_len } } + +#define SYSFS_CUSTOM(_read, _write, _flags) \ + .type = SUSPEND_SYSFS_DATA_CUSTOM, \ + .flags = _flags, \ + .data = { .special = { .read_sysfs = _read, .write_sysfs = _write } } + +#define SYSFS_WRITEONLY 0200 +#define SYSFS_READONLY 0444 +#define SYSFS_RW 0644 + +/* Flags */ +#define SYSFS_NEEDS_SM_FOR_READ 1 +#define SYSFS_NEEDS_SM_FOR_WRITE 2 +#define SYSFS_SUSPEND 4 +#define SYSFS_RESUME 8 +#define SYSFS_SUSPEND_OR_RESUME (SYSFS_SUSPEND | SYSFS_RESUME) +#define SYSFS_SUSPENDING (SYSFS_SUSPEND | SYSFS_NEEDS_SM_FOR_WRITE) +#define SYSFS_RESUMING (SYSFS_RESUME | SYSFS_NEEDS_SM_FOR_WRITE) +#define SYSFS_NEEDS_SM_FOR_BOTH \ + (SYSFS_NEEDS_SM_FOR_READ | SYSFS_NEEDS_SM_FOR_WRITE) + +int suspend_register_sysfs_file(struct kobject *kobj, + struct suspend_sysfs_data *suspend_sysfs_data); +void suspend_unregister_sysfs_file(struct kobject *kobj, + struct suspend_sysfs_data *suspend_sysfs_data); + +extern struct subsystem suspend2_subsys; + +struct kobject *make_suspend2_sysdir(char *name); +void remove_suspend2_sysdir(struct kobject *obj); +extern void suspend_cleanup_sysfs(void); + +extern int s2_sysfs_init(void); +extern void s2_sysfs_exit(void); Index: 2.6/kernel/power/ui.h =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ 2.6/kernel/power/ui.h 2007-04-27 14:43:58.000000000 +0300 @@ -0,0 +1,106 @@ +/* + * kernel/power/ui.h + * + * Copyright (C) 2004-2007 Nigel Cunningham (nigel at suspend2 net) + */ + +enum { + DONT_CLEAR_BAR, + CLEAR_BAR +}; + +enum { + /* Userspace -> Kernel */ + USERUI_MSG_ABORT = 0x11, + USERUI_MSG_SET_STATE = 0x12, + USERUI_MSG_GET_STATE = 0x13, + USERUI_MSG_GET_DEBUG_STATE = 0x14, + USERUI_MSG_SET_DEBUG_STATE = 0x15, + USERUI_MSG_SPACE = 0x18, + USERUI_MSG_GET_POWERDOWN_METHOD = 0x1A, + USERUI_MSG_SET_POWERDOWN_METHOD = 0x1B, + + /* Kernel -> Userspace */ + USERUI_MSG_MESSAGE = 0x21, + USERUI_MSG_PROGRESS = 0x22, + USERUI_MSG_REDRAW = 0x25, + + USERUI_MSG_MAX, +}; + +struct userui_msg_params { + unsigned long a, b, c, d; + char text[255]; +}; + +struct ui_ops { + char (*wait_for_key) (int timeout); + unsigned long (*update_status) (unsigned long value, + unsigned long maximum, const char *fmt, ...); + void (*prepare_status) (int clearbar, const char *fmt, ...); + void (*cond_pause) (int pause, char *message); + void (*abort)(int result_code, const char *fmt, ...); + void (*prepare)(void); + void (*cleanup)(void); + void (*redraw)(void); + void (*message)(unsigned long section, unsigned long level, + int normally_logged, const char *fmt, ...); +}; + +extern struct ui_ops *s2_current_ui; + +#define suspend_update_status(val, max, fmt, args...) \ + (s2_current_ui ? (s2_current_ui->update_status) (val, max, fmt, ##args) : max) + +#define suspend_wait_for_keypress(timeout) \ + (s2_current_ui ? (s2_current_ui->wait_for_key) (timeout) : 0) + +#define suspend_ui_redraw(void) \ + do { if (s2_current_ui) \ + (s2_current_ui->redraw)(); \ + } while(0) + +#define suspend_prepare_console(void) \ + do { if (s2_current_ui) \ + (s2_current_ui->prepare)(); \ + } while(0) + +#define suspend_cleanup_console(void) \ + do { if (s2_current_ui) \ + (s2_current_ui->cleanup)(); \ + } while(0) + +#define abort_suspend(result, fmt, args...) \ + do { if (s2_current_ui) \ + (s2_current_ui->abort)(result, fmt, ##args); \ + else { \ + set_result_state(SUSPEND_ABORTED); \ + set_result_state(result); \ + } \ + } while(0) + +#define suspend_cond_pause(pause, message) \ + do { if (s2_current_ui) \ + (s2_current_ui->cond_pause)(pause, message); \ + } while(0) + +#define suspend_prepare_status(clear, fmt, args...) \ + do { if (s2_current_ui) \ + (s2_current_ui->prepare_status)(clear, fmt, ##args); \ + else \ + printk(fmt, ##args); \ + } while(0) + +extern int suspend_default_console_level; + +#define suspend_message(sn, lev, log, fmt, a...) \ +do { \ + if (s2_current_ui && (!sn || test_debug_state(sn))) \ + s2_current_ui->message(sn, lev, log, fmt, ##a); \ +} while(0) + +__exit void suspend_ui_cleanup(void); +extern int s2_ui_init(void); +extern void s2_ui_exit(void); +extern int s2_register_ui_ops(struct ui_ops *this_ui); +extern void s2_remove_ui_ops(struct ui_ops *this_ui);