| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- /*
- * kernel/power/tuxonice_bio_signature.c
- *
- * Copyright (C) 2004-2014 Nigel Cunningham (nigel at tuxonice net)
- *
- * Distributed under GPLv2.
- *
- */
- #include <linux/fs_uuid.h>
- #include "tuxonice.h"
- #include "tuxonice_sysfs.h"
- #include "tuxonice_modules.h"
- #include "tuxonice_prepare_image.h"
- #include "tuxonice_bio.h"
- #include "tuxonice_ui.h"
- #include "tuxonice_alloc.h"
- #include "tuxonice_io.h"
- #include "tuxonice_builtin.h"
- #include "tuxonice_bio_internal.h"
- struct sig_data *toi_sig_data;
- /* Struct of swap header pages */
- struct old_sig_data {
- dev_t device;
- unsigned long sector;
- int resume_attempted;
- int orig_sig_type;
- };
- union diskpage {
- union swap_header swh; /* swh.magic is the only member used */
- struct sig_data sig_data;
- struct old_sig_data old_sig_data;
- };
- union p_diskpage {
- union diskpage *pointer;
- char *ptr;
- unsigned long address;
- };
- char *toi_cur_sig_page;
- char *toi_orig_sig_page;
- int have_image;
- int have_old_image;
- int get_signature_page(void)
- {
- if (!toi_cur_sig_page) {
- toi_message(TOI_IO, TOI_VERBOSE, 0, "Allocating current signature page.");
- toi_cur_sig_page = (char *)toi_get_zeroed_page(38, TOI_ATOMIC_GFP);
- if (!toi_cur_sig_page) {
- pr_err("Failed to allocate memory for the current image signature.\n");
- return -ENOMEM;
- }
- toi_sig_data = (struct sig_data *)toi_cur_sig_page;
- }
- toi_message(TOI_IO, TOI_VERBOSE, 0, "Reading signature from dev %x, sector %lu.",
- (unsigned int) resume_block_device->bd_dev, resume_firstblock);
- return toi_bio_ops.bdev_page_io(READ, resume_block_device,
- resume_firstblock, virt_to_page(toi_cur_sig_page));
- }
- void forget_signature_page(void)
- {
- if (toi_cur_sig_page) {
- toi_sig_data = NULL;
- toi_message(TOI_IO, TOI_VERBOSE, 0, "Freeing toi_cur_sig_page (%p).",
- toi_cur_sig_page);
- toi_free_page(38, (unsigned long)toi_cur_sig_page);
- toi_cur_sig_page = NULL;
- }
- if (toi_orig_sig_page) {
- toi_message(TOI_IO, TOI_VERBOSE, 0, "Freeing toi_orig_sig_page (%p).",
- toi_orig_sig_page);
- toi_free_page(38, (unsigned long)toi_orig_sig_page);
- toi_orig_sig_page = NULL;
- }
- }
- /*
- * We need to ensure we use the signature page that's currently on disk,
- * so as to not remove the image header. Post-atomic-restore, the orig sig
- * page will be empty, so we can use that as our method of knowing that we
- * need to load the on-disk signature and not use the non-image sig in
- * memory. (We're going to powerdown after writing the change, so it's safe.
- */
- int toi_bio_mark_resume_attempted(int flag)
- {
- toi_message(TOI_IO, TOI_VERBOSE, 0, "Make resume attempted = %d.", flag);
- if (!toi_orig_sig_page) {
- forget_signature_page();
- get_signature_page();
- }
- toi_sig_data->resumed_before = flag;
- return toi_bio_ops.bdev_page_io(WRITE, resume_block_device,
- resume_firstblock, virt_to_page(toi_cur_sig_page));
- }
- int toi_bio_mark_have_image(void)
- {
- int result = 0;
- char buf[32];
- struct fs_info *fs_info;
- toi_message(TOI_IO, TOI_VERBOSE, 0, "Recording that an image exists.");
- memcpy(toi_sig_data->sig, tuxonice_signature, sizeof(tuxonice_signature));
- toi_sig_data->have_image = 1;
- toi_sig_data->resumed_before = 0;
- toi_sig_data->header_dev_t = get_header_dev_t();
- toi_sig_data->have_uuid = 0;
- #if 1 /* FIXME: check this uuid is mismatch, such that it will failed on toi_bio_read_header_init() !!! */
- fs_info = fs_info_from_block_dev(get_header_bdev());
- if (fs_info && !IS_ERR(fs_info)) {
- memcpy(toi_sig_data->header_uuid, &fs_info->uuid, 16);
- free_fs_info(fs_info);
- } else
- result = (int)PTR_ERR(fs_info);
- #endif
- if (!result) {
- toi_message(TOI_IO, TOI_VERBOSE, 0, "Got uuid for dev_t %s.",
- format_dev_t(buf, get_header_dev_t()));
- toi_sig_data->have_uuid = 1;
- } else
- toi_message(TOI_IO, TOI_VERBOSE, 0, "Could not get uuid for dev_t %s.",
- format_dev_t(buf, get_header_dev_t()));
- toi_sig_data->first_header_block = get_headerblock();
- have_image = 1;
- toi_message(TOI_IO, TOI_VERBOSE, 0, "header dev_t is %x. First block is %lu.",
- (unsigned int) toi_sig_data->header_dev_t, toi_sig_data->first_header_block);
- memcpy(toi_sig_data->sig2, tuxonice_signature, sizeof(tuxonice_signature));
- toi_sig_data->header_version = TOI_HEADER_VERSION;
- return toi_bio_ops.bdev_page_io(WRITE, resume_block_device,
- resume_firstblock, virt_to_page(toi_cur_sig_page));
- }
- int remove_old_signature(void)
- {
- union p_diskpage swap_header_page = (union p_diskpage)toi_cur_sig_page;
- char *orig_sig;
- char *header_start = (char *)toi_get_zeroed_page(38, TOI_ATOMIC_GFP);
- int result;
- struct block_device *header_bdev;
- struct old_sig_data *old_sig_data = &swap_header_page.pointer->old_sig_data;
- header_bdev = toi_open_bdev(NULL, old_sig_data->device, 1);
- result = toi_bio_ops.bdev_page_io(READ, header_bdev,
- old_sig_data->sector, virt_to_page(header_start));
- if (result)
- goto out;
- /*
- * TODO: Get the original contents of the first bytes of the swap
- * header page.
- */
- if (!old_sig_data->orig_sig_type)
- orig_sig = "SWAP-SPACE";
- else
- orig_sig = "SWAPSPACE2";
- memcpy(swap_header_page.pointer->swh.magic.magic, orig_sig, 10);
- memcpy(swap_header_page.ptr, header_start, 10);
- result = toi_bio_ops.bdev_page_io(WRITE, resume_block_device,
- resume_firstblock, virt_to_page(swap_header_page.ptr));
- out:
- toi_close_bdev(header_bdev);
- have_old_image = 0;
- toi_free_page(38, (unsigned long)header_start);
- return result;
- }
- /*
- * toi_bio_restore_original_signature - restore the original signature
- *
- * At boot time (aborting pre atomic-restore), toi_orig_sig_page gets used.
- * It will have the original signature page contents, stored in the image
- * header. Post atomic-restore, we use :toi_cur_sig_page, which will contain
- * the contents that were loaded when we started the cycle.
- */
- int toi_bio_restore_original_signature(void)
- {
- char *use = toi_orig_sig_page ? toi_orig_sig_page : toi_cur_sig_page;
- if (have_old_image)
- return remove_old_signature();
- if (!use) {
- pr_warn("toi_bio_restore_original_signature: No signature " "page loaded.\n");
- return 0;
- }
- toi_message(TOI_IO, TOI_VERBOSE, 0, "Recording that no image exists.");
- have_image = 0;
- toi_sig_data->have_image = 0;
- return toi_bio_ops.bdev_page_io(WRITE, resume_block_device,
- resume_firstblock, virt_to_page(use));
- }
- /*
- * check_for_signature - See whether we have an image.
- *
- * Returns 0 if no image, 1 if there is one, -1 if indeterminate.
- */
- int toi_check_for_signature(void)
- {
- union p_diskpage swap_header_page;
- int type;
- static const char * const normal_sigs[] = { "SWAP-SPACE", "SWAPSPACE2" };
- static const char * const swsusp_sigs[] = { "S1SUSP", "S2SUSP", "S1SUSPEND" };
- char *swap_header;
- if (!toi_cur_sig_page) {
- int result = get_signature_page();
- if (result)
- return result;
- }
- /*
- * Start by looking for the binary header.
- */
- if (!memcmp(tuxonice_signature, toi_cur_sig_page, sizeof(tuxonice_signature))) {
- have_image = toi_sig_data->have_image;
- toi_message(TOI_IO, TOI_VERBOSE, 0, "Have binary signature. Have image is %d.",
- have_image);
- if (have_image)
- toi_message(TOI_IO, TOI_VERBOSE, 0, "header dev_t is %x. First block is %lu.",
- (unsigned int) toi_sig_data->header_dev_t, toi_sig_data->first_header_block);
- return toi_sig_data->have_image;
- }
- /*
- * Failing that, try old file allocator headers.
- */
- if (!memcmp(HaveImage, toi_cur_sig_page, strlen(HaveImage))) {
- have_image = 1;
- return 1;
- }
- have_image = 0;
- if (!memcmp(NoImage, toi_cur_sig_page, strlen(NoImage)))
- return 0;
- /*
- * Nope? How about swap?
- */
- swap_header_page = (union p_diskpage)toi_cur_sig_page;
- swap_header = swap_header_page.pointer->swh.magic.magic;
- /* Normal swapspace? */
- for (type = 0; type < 2; type++)
- if (!memcmp(normal_sigs[type], swap_header, strlen(normal_sigs[type])))
- return 0;
- /* Swsusp or uswsusp? */
- for (type = 0; type < 3; type++)
- if (!memcmp(swsusp_sigs[type], swap_header, strlen(swsusp_sigs[type])))
- return 2;
- /* Old TuxOnIce version? */
- if (!memcmp(tuxonice_signature, swap_header, sizeof(tuxonice_signature) - 1)) {
- toi_message(TOI_IO, TOI_VERBOSE, 0, "Found old TuxOnIce " "signature.");
- have_old_image = 1;
- return 3;
- }
- return -1;
- }
- /*
- * Image_exists
- *
- * Returns -1 if don't know, otherwise 0 (no) or 1 (yes).
- */
- int toi_bio_image_exists(int quiet)
- {
- int result;
- char *msg = NULL;
- toi_message(TOI_IO, TOI_VERBOSE, 0, "toi_bio_image_exists.");
- if (!resume_dev_t) {
- if (!quiet)
- pr_warn("Not even trying to read header because resume_dev_t is not set.");
- return -1;
- }
- if (open_resume_dev_t(0, quiet))
- return -1;
- result = toi_check_for_signature();
- clear_toi_state(TOI_RESUMED_BEFORE);
- if (toi_sig_data->resumed_before)
- set_toi_state(TOI_RESUMED_BEFORE);
- if (quiet || result == -ENOMEM)
- return result;
- if (result == -1)
- msg = "TuxOnIce: Unable to find a signature. Could you have moved a swap file?\n";
- else if (!result)
- msg = "TuxOnIce: No image found.\n";
- else if (result == 1)
- msg = "TuxOnIce: Image found.\n";
- else if (result == 2)
- msg = "TuxOnIce: uswsusp or swsusp image found.\n";
- else if (result == 3)
- msg = "TuxOnIce: Old implementation's signature found.\n";
- pr_warn("%s", msg);
- return result;
- }
- int toi_bio_scan_for_image(int quiet)
- {
- struct block_device *bdev;
- int first = 1;
- if (!quiet)
- pr_debug("Scanning swap devices for TuxOnIce " "signature...\n");
- for (bdev = next_bdev_of_type(NULL, "swap"); bdev; bdev = next_bdev_of_type(bdev, "swap")) {
- int result;
- char name[255] = "";
- sprintf(name, "%u:%u", MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev));
- if (!quiet)
- pr_debug("- Trying %s.\n", name);
- resume_block_device = bdev;
- resume_dev_t = bdev->bd_dev;
- result = toi_check_for_signature();
- resume_block_device = NULL;
- resume_dev_t = MKDEV(0, 0);
- if (result == 1) {
- /* Got one! */
- strcpy(resume_file, name);
- next_bdev_of_type(bdev, NULL);
- if (!quiet)
- pr_debug(" ==> Image found on %s.\n", resume_file);
- return 1;
- }
- if (first) {
- strcpy(resume_file, name);
- first = 0;
- }
- forget_signature_page();
- }
- if (!quiet)
- pr_debug("TuxOnIce scan: No image found.\n");
- return 0;
- }
- int toi_bio_get_header_version(void)
- {
- return (memcmp(toi_sig_data->sig2, tuxonice_signature,
- sizeof(tuxonice_signature))) ? 0 : toi_sig_data->header_version;
- }
|