summaryrefslogtreecommitdiff
path: root/kernel/power/tuxonice_bio_signature.c
blob: f5418f092b29307c2d8136e973ef65db06f4a48b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 * kernel/power/tuxonice_bio_signature.c
 *
 * Copyright (C) 2004-2015 Nigel Cunningham (nigel at nigelcunningham com au)
 *
 * 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) {
                        printk(KERN_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 %lx,"
                        " sector %d.",
                        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;

        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);

        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 %d.", 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) {
                printk("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;
        const char *normal_sigs[] = {"SWAP-SPACE", "SWAPSPACE2" };
        const char *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 %d.",
                                        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)
                        printk(KERN_INFO "Not even trying to read header "
                                "because resume_dev_t is not set.\n");
                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";

        printk(KERN_INFO "%s", msg);

        return result;
}

int toi_bio_scan_for_image(int quiet)
{
        struct block_device *bdev;
        char default_name[255] = "";

        if (!quiet)
                printk(KERN_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)
                        printk(KERN_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 (!default_name[0])
                        strcpy(default_name, name);

                if (result == 1) {
                        /* Got one! */
                        strcpy(resume_file, name);
                        next_bdev_of_type(bdev, NULL);
                        if (!quiet)
                                printk(KERN_DEBUG " ==> Image found on %s.\n",
                                                resume_file);
                        return 1;
                }
                forget_signature_page();
        }

        if (!quiet)
                printk(KERN_DEBUG "TuxOnIce scan: No image found.\n");
        strcpy(resume_file, default_name);
        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;

}