~ubuntu-branches/debian/stretch/uswsusp/stretch

« back to all changes in this revision

Viewing changes to suspend.c

  • Committer: Bazaar Package Importer
  • Author(s): Rodolfo García Peñas (kix), Rodolfo García Peñas (kix), Closes: #552484, #576803, #528483, Closes: #495111, #595125, #486352, #433872, #590233, Closes: #550725, #549118, Closes: #437094, #586674, #547158, #567604, Closes: #520705, Anibal Monsalve Salazar
  • Date: 2011-03-14 08:26:16 UTC
  • mfrom: (0.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110314082616-7mjcl6tfzsv22arm
Tags: 1.0-1
[ Rodolfo García Peñas (kix) ]
* New 1.0 version [Closes: #589743, #578496, #617534]
* A lot of new machines. 
  [Closes: #552484, #576803, #528483] 
  [Closes: #495111, #595125, #486352, #433872, #590233] 
* A new length for addressing 
  [Closes: #550725, #549118]
  (http://lkml.org/lkml/2009/11/3/377)
* Support for Kernel Mode Set (KMS) 
  [Closes: #437094, #586674, #547158, #567604]
* Switch to dpkg-source 3.0 (quilt) format
* Compiled without splash support. 
  libsplash is not included in stable, many bugs.
* Moved the manpage file "suspend.conf.8" to manual section 5.
  [Closes: #520705]

[ Anibal Monsalve Salazar ]
* Update uploaders list

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#include <errno.h>
33
33
#include <signal.h>
34
34
#include <termios.h>
 
35
#ifdef CONFIG_THREADS
 
36
#include <pthread.h>
 
37
#endif
35
38
#ifdef CONFIG_COMPRESS
36
39
#include <lzo/lzo1x.h>
37
40
#endif
38
41
 
39
42
#include "swsusp.h"
 
43
#include "memalloc.h"
40
44
#include "config_parser.h"
41
45
#include "md5.h"
42
46
#include "splash.h"
45
49
#ifdef CONFIG_BOTH
46
50
#include "s2ram.h"
47
51
#endif
 
52
static char test_file_name[MAX_STR_LEN] = "";
 
53
static loff_t test_image_size;
48
54
 
49
55
#define suspend_error(msg, args...) \
50
56
do { \
54
60
static char snapshot_dev_name[MAX_STR_LEN] = SNAPSHOT_DEVICE;
55
61
static char resume_dev_name[MAX_STR_LEN] = RESUME_DEVICE;
56
62
static loff_t resume_offset;
57
 
static unsigned long pref_image_size = IMAGE_SIZE;
 
63
static loff_t pref_image_size = IMAGE_SIZE;
58
64
static int suspend_loglevel = SUSPEND_LOGLEVEL;
59
65
static char compute_checksum;
60
66
#ifdef CONFIG_COMPRESS
61
67
static char do_compress;
62
 
static long long compr_diff;
63
68
#else
64
69
#define do_compress 0
65
 
#define compr_diff 0
66
70
#endif
67
71
#ifdef CONFIG_ENCRYPT
68
72
static char do_encrypt;
69
73
static char use_RSA;
70
 
static char key_name[MAX_STR_LEN] = SUSPEND_KEY_FILE;
71
 
static char password[PASS_SIZE];
 
74
static char key_name[MAX_STR_LEN] = SUSPEND_KEY_FILE_PATH;
 
75
static char password[PASSBUF_SIZE];
 
76
static unsigned long encrypt_buf_size;
72
77
#else
73
78
#define do_encrypt 0
74
79
#define key_name NULL
 
80
#define encrypt_buf_size 0
75
81
#endif
76
82
#ifdef CONFIG_BOTH
77
83
static char s2ram;
 
84
static char s2ram_kms;
78
85
#endif
79
86
static char early_writeout;
80
87
static char splash_param;
88
95
        SHUTDOWN_METHOD_PLATFORM,
89
96
        SHUTDOWN_METHOD_REBOOT
90
97
} shutdown_method = SHUTDOWN_METHOD_PLATFORM;
 
98
static int resume_pause;
 
99
static char verify_image;
 
100
#ifdef CONFIG_THREADS
 
101
static char use_threads;
 
102
#else
 
103
#define use_threads     0
 
104
#endif
91
105
 
92
106
static int suspend_swappiness = SUSPEND_SWAPPINESS;
93
 
static struct splash splash;
94
107
static struct vt_mode orig_vtm;
95
108
static int vfd;
96
 
static char *my_name;
97
109
 
98
110
static struct config_par parameters[] = {
99
111
        {
178
190
        },
179
191
#endif
180
192
        {
 
193
                .name = "resume pause",
 
194
                .fmt = "%d",
 
195
                .ptr = &resume_pause,
 
196
        },
 
197
        {
 
198
                .name = "debug test file",
 
199
                .fmt = "%s",
 
200
                .ptr = test_file_name,
 
201
                .len = MAX_STR_LEN
 
202
        },
 
203
        {
 
204
                .name = "debug verify image",
 
205
                .fmt = "%c",
 
206
                .ptr = &verify_image,
 
207
        },
 
208
#ifdef CONFIG_THREADS
 
209
        {
 
210
                .name = "threads",
 
211
                .fmt = "%c",
 
212
                .ptr = &use_threads,
 
213
        },
 
214
#endif
 
215
        {
181
216
                .name = NULL,
182
217
                .fmt = NULL,
183
218
                .ptr = NULL,
185
220
        }
186
221
};
187
222
 
188
 
static unsigned int page_size;
189
 
/* This MUST NOT be less than page_size */
190
 
static unsigned int max_block_size;
191
 
static unsigned int buffer_size;
192
 
static void *mem_pool;
193
 
#ifdef CONFIG_ENCRYPT
194
 
struct key_data *key_data;
195
 
gcry_cipher_hd_t cipher_handle;
196
 
#endif
197
 
 
198
 
static inline loff_t check_free_swap(int dev)
 
223
static loff_t check_free_swap(int dev)
199
224
{
200
225
        int error;
201
226
        loff_t free_swap;
202
227
 
203
 
        error = ioctl(dev, SNAPSHOT_AVAIL_SWAP, &free_swap);
 
228
        error = ioctl(dev, SNAPSHOT_AVAIL_SWAP_SIZE, &free_swap);
 
229
        if (error && errno == ENOTTY) {
 
230
                report_unsupported_ioctl("SNAPSHOT_AVAIL_SWAP_SIZE");
 
231
                error = ioctl(dev, SNAPSHOT_AVAIL_SWAP, &free_swap);
 
232
        }
204
233
        if (!error)
205
234
                return free_swap;
206
 
        else
207
 
                suspend_error("check_free_swap failed.");
208
 
        return 0;
209
 
}
210
 
 
211
 
static inline unsigned long get_swap_page(int dev)
 
235
 
 
236
        suspend_error("check_free_swap failed.");
 
237
        return 0;
 
238
}
 
239
 
 
240
static loff_t get_image_size(int dev)
 
241
{
 
242
        int error;
 
243
        loff_t image_size;
 
244
 
 
245
        error = ioctl(dev, SNAPSHOT_GET_IMAGE_SIZE, &image_size);
 
246
        if (!error)
 
247
                return image_size;
 
248
 
 
249
        if (errno == ENOTTY)
 
250
                report_unsupported_ioctl("SNAPSHOT_GET_IMAGE_SIZE");
 
251
        suspend_error("get_image_size failed.");
 
252
        return 0;
 
253
}
 
254
 
 
255
static loff_t alloc_swap_page(int dev, int verbose)
212
256
{
213
257
        int error;
214
258
        loff_t offset;
215
259
 
216
 
        error = ioctl(dev, SNAPSHOT_GET_SWAP_PAGE, &offset);
 
260
        error = ioctl(dev, SNAPSHOT_ALLOC_SWAP_PAGE, &offset);
 
261
        if (error && errno == ENOTTY) {
 
262
                if (verbose)
 
263
                        report_unsupported_ioctl("SNAPSHOT_ALLOC_SWAP_PAGE");
 
264
                error = ioctl(dev, SNAPSHOT_GET_SWAP_PAGE, &offset);
 
265
        }
217
266
        if (!error)
218
267
                return offset;
219
268
        return 0;
220
269
}
221
270
 
 
271
static inline loff_t get_swap_page(int dev)
 
272
{
 
273
        return alloc_swap_page(dev, 0);
 
274
}
 
275
 
222
276
static inline int free_swap_pages(int dev)
223
277
{
224
278
        return ioctl(dev, SNAPSHOT_FREE_SWAP_PAGES, 0);
225
279
}
226
280
 
227
 
static inline int set_swap_file(int dev, u_int32_t blkdev, loff_t offset)
 
281
static int set_swap_file(int dev, u_int32_t blkdev, loff_t offset)
228
282
{
229
283
        struct resume_swap_area swap;
230
284
        int error;
238
292
        return error;
239
293
}
240
294
 
241
 
/**
242
 
 *      write_area - Write data to given swap location.
243
 
 *      @fd:            File handle of the resume partition
 
295
static int atomic_snapshot(int dev, int *in_suspend)
 
296
{
 
297
        int error;
 
298
 
 
299
        error = ioctl(dev, SNAPSHOT_CREATE_IMAGE, in_suspend);
 
300
        if (error && errno == ENOTTY) {
 
301
                report_unsupported_ioctl("SNAPSHOT_CREATE_IMAGE");
 
302
                error = ioctl(dev, SNAPSHOT_ATOMIC_SNAPSHOT, in_suspend);
 
303
        }
 
304
        return error;
 
305
}
 
306
 
 
307
static inline int free_snapshot(int dev)
 
308
{
 
309
        return ioctl(dev, SNAPSHOT_FREE, 0);
 
310
}
 
311
 
 
312
static int set_image_size(int dev, loff_t size)
 
313
{
 
314
        int error;
 
315
 
 
316
        error = ioctl(dev, SNAPSHOT_PREF_IMAGE_SIZE, size);
 
317
        if (error && errno == ENOTTY) {
 
318
                report_unsupported_ioctl("SNAPSHOT_PREF_IMAGE_SIZE");
 
319
                error = ioctl(dev, SNAPSHOT_SET_IMAGE_SIZE, size);
 
320
        }
 
321
        return error;
 
322
}
 
323
 
 
324
static inline int suspend_to_ram(int dev)
 
325
{
 
326
        return ioctl(dev, SNAPSHOT_S2RAM, 0);
 
327
}
 
328
 
 
329
static int platform_enter(int dev)
 
330
{
 
331
        int error;
 
332
 
 
333
        error = ioctl(dev, SNAPSHOT_POWER_OFF, 0);
 
334
        if (error  && errno == ENOTTY) {
 
335
                report_unsupported_ioctl("SNAPSHOT_POWER_OFF");
 
336
                error = ioctl(dev, SNAPSHOT_PMOPS, PMOPS_ENTER);
 
337
        }
 
338
        return error;
 
339
}
 
340
 
 
341
/**
 
342
 *      alloc_swap - allocate a number of swap pages
 
343
 *      @dev:           Swap device to use for allocations.
 
344
 *      @extents:       Array of extents to track the allocations.
 
345
 *      @nr_extents:    Number of extents already in the array.
 
346
 *      @size_p:        Points to the number of bytes to allocate, used to
 
347
 *                      return the number of allocated bytes.
 
348
 *
 
349
 *      Allocate the number of swap pages sufficient for saving the number of
 
350
 *      bytes pointed to by @size_p.  Use the array @extents to track the
 
351
 *      allocations.  This array has to be page_size big and may already
 
352
 *      contain some initial elements (in that case @nr_extents must be the
 
353
 *      number of these elements).
 
354
 *      Each element of the array represents an area of allocated swap space.
 
355
 *      These areas may be extended when swap pages that can be added to them
 
356
 *      are found.  They also can be merged with one another.
 
357
 *      The function returns when the requested amount of swap space is
 
358
 *      allocated or if there is no room for more extents.  In the latter case
 
359
 *      the last extent created is put at the end of the array and may be passed
 
360
 *      to alloc_swap() as the initial extent when it is invoked next time.
 
361
 */
 
362
static int
 
363
alloc_swap(int dev, struct extent *extents, int nr_extents, loff_t *size_p)
 
364
{
 
365
        const int max_extents = page_size / sizeof(struct extent) - 1;
 
366
        loff_t size, total_size, offset;
 
367
 
 
368
        total_size = *size_p;
 
369
        if (nr_extents <= 0) {
 
370
                offset = get_swap_page(dev);
 
371
                if (!offset)
 
372
                        return -ENOSPC;
 
373
                extents->start = offset;
 
374
                extents->end = offset + page_size;
 
375
                nr_extents = 1;
 
376
                size = page_size;
 
377
        } else {
 
378
                size = 0;
 
379
        }
 
380
        while (size < total_size && nr_extents <= max_extents) {
 
381
                int i, j;
 
382
 
 
383
                offset = get_swap_page(dev);
 
384
                if (!offset)
 
385
                        return -ENOSPC;
 
386
                /* Check if we have a matching extent. */
 
387
                i = 0;
 
388
                j = nr_extents - 1;
 
389
                do {
 
390
                        struct extent *ext;
 
391
                        int k = (i + j) / 2;
 
392
 
 
393
 Repeat:
 
394
                        ext = extents + k;
 
395
                        if (offset == ext->start - page_size) {
 
396
                                ext->start = offset;
 
397
                                /* Check if we can merge extents */
 
398
                                if (k > 0 && extents[k-1].end == offset) {
 
399
                                        extents[k-1].end = ext->end;
 
400
                                        /* Pull the 'later' extents forward */
 
401
                                        memmove(ext, ext + 1,
 
402
                                                (nr_extents - k - 1) *
 
403
                                                        sizeof(*ext));
 
404
                                        nr_extents--;
 
405
                                }
 
406
                                offset = 0;
 
407
                                break;
 
408
                        } else if (offset == ext->end) {
 
409
                                ext->end += page_size;
 
410
                                /* Check if we can merge extents */
 
411
                                if (k + 1 < nr_extents
 
412
                                    && ext->end == extents[k+1].start) {
 
413
                                        ext->end = extents[k+1].end;
 
414
                                        /* Pull the 'later' extents forward */
 
415
                                        memmove(ext + 1, ext + 2,
 
416
                                                (nr_extents - k - 2) *
 
417
                                                        sizeof(*ext));
 
418
                                        nr_extents--;
 
419
                                }
 
420
                                offset = 0;
 
421
                                break;
 
422
                        } else if (offset > ext->end) {
 
423
                                if (i == k) {
 
424
                                        if (i < j) {
 
425
                                                /* This means i == j + 1 */
 
426
                                                k = j;
 
427
                                                i = j;
 
428
                                                goto Repeat;
 
429
                                        }
 
430
                                } else {
 
431
                                        i = k;
 
432
                                }
 
433
                        } else {
 
434
                                /* offset < ext->start - page_size */
 
435
                                j = k;
 
436
                        }
 
437
                } while (i < j);
 
438
                if (offset > 0) {
 
439
                        /* No match.  Create a new extent. */
 
440
                        struct extent *ext;
 
441
 
 
442
                        if (nr_extents < max_extents) {
 
443
                                ext = extents + i;
 
444
                                /*
 
445
                                 * We want to always replace the extent 'i' with
 
446
                                 * the new one.
 
447
                                 */
 
448
                                if (offset > ext->end) {
 
449
                                        i++;
 
450
                                        ext++;
 
451
                                }
 
452
                                /* Push the 'later' extents backwards. */
 
453
                                memmove(ext + 1, ext,
 
454
                                        (nr_extents - i) * sizeof(*ext));
 
455
                        } else {
 
456
                                ext = extents + nr_extents;
 
457
                        }
 
458
                        ext->start = offset;
 
459
                        ext->end = offset + page_size;
 
460
                        nr_extents++;
 
461
                }
 
462
                size += page_size;
 
463
        }
 
464
        *size_p = size;
 
465
        return nr_extents;
 
466
}
 
467
 
 
468
/**
 
469
 *      write_page - Write page_size data to given swap location.
 
470
 *      @fd:            File handle of the resume partition.
244
471
 *      @buf:           Pointer to the area we're writing.
245
 
 *      @offset:        Offset of the swap page we're writing to
246
 
 *      @size:          The number of bytes to save
 
472
 *      @offset:        Offset of the swap page we're writing to.
247
473
 */
248
 
 
249
 
static int write_area(int fd, void *buf, loff_t offset, unsigned int size)
 
474
static int write_page(int fd, void *buf, loff_t offset)
250
475
{
251
 
        int res = -EINVAL;
 
476
        int res = 0;
252
477
        ssize_t cnt = 0;
253
478
 
254
 
        if (offset) {
255
 
                if (lseek(fd, offset, SEEK_SET) == offset)
256
 
                        cnt = write(fd, buf, size);
257
 
                if (cnt == size)
258
 
                        res = 0;
259
 
                else
260
 
                        res = cnt < 0 ? cnt : -EIO;
261
 
        }
 
479
        if (!offset)
 
480
                return -EINVAL;
 
481
 
 
482
        if (lseek64(fd, offset, SEEK_SET) == offset)
 
483
                cnt = write(fd, buf, page_size);
 
484
        if (cnt != page_size)
 
485
                res = -EIO;
262
486
        return res;
263
487
}
264
488
 
265
489
/*
266
 
 *      The swap_map_handle structure is used for handling swap in
267
 
 *      a file-alike way
 
490
 * The swap_writer structure is used for handling swap in a file-alike way.
 
491
 *
 
492
 * @extents:    Array of extents used for trackig swap allocations.  It is
 
493
 *              page_size bytes large and holds at most
 
494
 *              (page_size / sizeof(struct extent) - 1) extents.  The last slot
 
495
 *              is used to hold the extent that will be used as an initial one
 
496
 *              for the next batch of allocations.
 
497
 *
 
498
 * @nr_extents:         Number of entries in @extents actually used.
 
499
 *
 
500
 * @cur_extent:         The extent currently used as the source of swap pages.
 
501
 *
 
502
 * @cur_extent_idx:     The index of @cur_extent.
 
503
 *
 
504
 * @cur_offset:         The offset of the swap page that will be used next.
 
505
 *
 
506
 * @swap_needed:        The amount of swap needed for saving the image.
 
507
 *
 
508
 * @written_data:       The amount of data actually saved.
 
509
 *
 
510
 * @extents_spc:        The swap page to which to save @extents.
 
511
 *
 
512
 * @buffer:             Buffer used for storing image data pages.
 
513
 *
 
514
 * @write_buffer:       If compression is used, the compressed contents of
 
515
 *                      @buffer are stored here.  Otherwise, it is equal to
 
516
 *                      @buffer.
 
517
 *
 
518
 * @page_ptr:           Address to write the next image page to.
 
519
 *
 
520
 * @dev:                Snapshot device handle used for reading image pages and
 
521
 *                      invoking ioctls.
 
522
 *
 
523
 * @fd:                 File handle associated with the swap.
 
524
 *
 
525
 * @ctx:                Used for checksum computing, if so configured.
 
526
 *
 
527
 * @lzo_work_buffer:    Work buffer used for compression.
 
528
 *
 
529
 * @encrypt_buffer:     Buffer for storing encrypted data (page_size bytes).
 
530
 *
 
531
 * @encrypt_ptr:        Address to store the next encrypted page at.
268
532
 */
269
 
 
270
 
struct swap_map_handle {
271
 
        char *page_buffer;
272
 
        char *write_buffer;
273
 
        struct swap_area *areas;
274
 
        unsigned short areas_per_page;
275
 
        loff_t *next_swap;
276
 
        struct swap_area cur_area;
277
 
        unsigned int cur_alloc;
278
 
        loff_t cur_swap;
279
 
        unsigned int k;
280
 
        int dev, fd;
 
533
struct swap_writer {
 
534
        struct extent *extents;
 
535
        int nr_extents;
 
536
        struct extent *cur_extent;
 
537
        int cur_extent_idx;
 
538
        loff_t cur_offset;
 
539
        loff_t swap_needed;
 
540
        loff_t written_data;
 
541
        loff_t extents_spc;
 
542
        void *buffer;
 
543
        void *write_buffer;
 
544
        void *page_ptr;
 
545
        int dev, fd, input;
281
546
        struct md5_ctx ctx;
282
 
#ifdef CONFIG_COMPRESS
283
547
        void *lzo_work_buffer;
284
 
#endif
285
 
#ifdef CONFIG_ENCRYPT
286
 
        unsigned char *encrypt_buffer;
287
 
#endif
 
548
        void *encrypt_buffer;
 
549
        void *encrypt_ptr;
288
550
};
289
551
 
290
 
static int
291
 
init_swap_writer(struct swap_map_handle *handle, int dev, int fd, void *buf)
292
 
{
293
 
        if (!buf)
294
 
                return -EINVAL;
295
 
 
296
 
        handle->areas = buf;
297
 
        buf += page_size;
298
 
 
299
 
        handle->page_buffer = buf;
300
 
        buf += page_size;
301
 
 
302
 
        handle->write_buffer = buf;
303
 
        buf += buffer_size;
304
 
 
305
 
#ifdef CONFIG_COMPRESS
306
 
        handle->lzo_work_buffer = buf;
307
 
        buf += LZO1X_1_MEM_COMPRESS;
308
 
#endif
309
 
 
310
 
#ifdef CONFIG_ENCRYPT
311
 
        handle->encrypt_buffer = buf;
312
 
#endif
313
 
 
314
 
        memset(handle->areas, 0, page_size);
315
 
        handle->areas_per_page = (page_size - sizeof(loff_t)) /
316
 
                        sizeof(struct swap_area);
317
 
        handle->next_swap = (loff_t *)(handle->areas + handle->areas_per_page);
318
 
 
319
 
        handle->cur_swap = get_swap_page(dev);
320
 
        if (!handle->cur_swap)
321
 
                return -ENOSPC;
322
 
        handle->cur_area.offset = get_swap_page(dev);
323
 
        if (!handle->cur_area.offset)
324
 
                return -ENOSPC;
325
 
        handle->cur_area.size = 0;
326
 
        handle->cur_alloc = page_size;
327
 
        handle->k = 0;
 
552
/**
 
553
 *      free_swap_writer - free memory allocated for saving the image
 
554
 *      @handle:        Structure containing pointers to memory buffers to free.
 
555
 */
 
556
static void free_swap_writer(struct swap_writer *handle)
 
557
{
 
558
        if (handle->write_buffer != handle->buffer)
 
559
                freemem(handle->write_buffer);
 
560
        if (do_compress)
 
561
                freemem(handle->lzo_work_buffer);
 
562
        if (handle->encrypt_buffer)
 
563
                freemem(handle->encrypt_buffer);
 
564
        freemem(handle->buffer);
 
565
        freemem(handle->extents);
 
566
}
 
567
 
 
568
/**
 
569
 *      init_swap_writer - initialize the structure used for saving the image
 
570
 *      @handle:        Structure to initialize.
 
571
 *      @dev:           Special device file to read image pages from.
 
572
 *      @fd:            File descriptor associated with the swap.
 
573
 *
 
574
 *      It doesn't preallocate swap, so preallocate_swap() has to be called on
 
575
 *      @handle after this.
 
576
 */
 
577
static int init_swap_writer(struct swap_writer *handle, int dev, int fd, int in)
 
578
{
 
579
        loff_t offset;
 
580
        unsigned int write_buf_size = 0;
 
581
 
 
582
        handle->extents = getmem(page_size);
 
583
 
 
584
        handle->buffer = getmem(buffer_size);
 
585
        handle->page_ptr = handle->buffer;
 
586
 
 
587
        if (do_encrypt) {
 
588
                handle->encrypt_buffer = getmem(encrypt_buf_size);
 
589
                handle->encrypt_ptr = handle->encrypt_buffer;
 
590
        } else {
 
591
                handle->encrypt_buffer = NULL;
 
592
        }
 
593
 
 
594
        if (do_compress) {
 
595
                handle->lzo_work_buffer = getmem(LZO1X_1_MEM_COMPRESS);
 
596
                write_buf_size = compress_buf_size;
 
597
                if (use_threads)
 
598
                        write_buf_size +=
 
599
                                (WRITE_BUFFERS - 1) * compress_buf_size;
 
600
        }
 
601
 
 
602
        if (write_buf_size > 0)
 
603
                handle->write_buffer = getmem(write_buf_size);
 
604
        else if (use_threads)
 
605
                handle->write_buffer = getmem(buffer_size * WRITE_BUFFERS);
 
606
        else
 
607
                handle->write_buffer = handle->buffer;
328
608
 
329
609
        handle->dev = dev;
330
610
        handle->fd = fd;
331
 
 
332
 
        if (compute_checksum)
 
611
        handle->input = (in >= 0) ? in : dev;
 
612
        handle->written_data = 0;
 
613
 
 
614
        memset(handle->extents, 0, page_size);
 
615
        handle->nr_extents = 0;
 
616
        offset = get_swap_page(dev);
 
617
        if (!offset) {
 
618
                free_swap_writer(handle);
 
619
                return -ENOSPC;
 
620
        }
 
621
        handle->extents_spc = offset;
 
622
 
 
623
        if (compute_checksum || verify_image)
333
624
                md5_init_ctx(&handle->ctx);
334
625
 
335
626
        return 0;
336
627
}
337
628
 
338
 
static int prepare(struct swap_map_handle *handle, int disp)
339
 
{
340
 
        struct buf_block *block;
341
 
        void *buf = handle->page_buffer;
342
 
 
343
 
        block = (struct buf_block *)(handle->write_buffer + disp);
 
629
/**
 
630
 *      preallocate_swap - use alloc_swap() to preallocate the number of pages
 
631
 *                      given by @handle->swap_needed
 
632
 *      @handle:        Pointer to the structure in which to store information
 
633
 *                      about the preallocated swap pool.
 
634
 *
 
635
 *      Returns the offset of the first swap page available from the
 
636
 *      preallocated pool.
 
637
 */
 
638
static loff_t preallocate_swap(struct swap_writer *handle)
 
639
{
 
640
        const int max = page_size / sizeof(struct extent) - 1;
 
641
        loff_t size;
 
642
        int nr_extents;
 
643
 
 
644
        if (handle->swap_needed < page_size)
 
645
                return 0;
 
646
        size = handle->swap_needed;
 
647
        if (do_compress && size > page_size)
 
648
                size /= 2;
 
649
        nr_extents = alloc_swap(handle->dev, handle->extents,
 
650
                                        handle->nr_extents, &size);
 
651
        if (nr_extents <= 0)
 
652
                return 0;
 
653
        handle->nr_extents = nr_extents < max ? nr_extents : max;
 
654
        handle->cur_extent = handle->extents;
 
655
        handle->cur_extent_idx = 0;
 
656
        handle->cur_offset = handle->cur_extent->start;
 
657
        return handle->cur_offset;
 
658
}
 
659
 
 
660
/**
 
661
 *      save_extents - save the array of extents
 
662
 *      handle: Structure holding the pointer to the array of extents etc.
 
663
 *      finish: If set, the last element of the extents array has to be filled
 
664
 *              with zeros.
 
665
 *
 
666
 *      Save the buffer (page) holding the array of extents to the swap
 
667
 *      location pointed to by @handle->extents_spc (this must be allocated
 
668
 *      earlier).  Before saving the last element of the array is used to store
 
669
 *      the swap offset of the next extents page (we allocate a swap page for
 
670
 *      this purpose).
 
671
 */
 
672
static int save_extents(struct swap_writer *handle, int finish)
 
673
{
 
674
        loff_t offset = 0;
 
675
        int error;
 
676
 
 
677
        if (!finish) {
 
678
                struct extent *last_extent;
 
679
 
 
680
                offset = get_swap_page(handle->dev);
 
681
                if (!offset)
 
682
                        return -ENOSPC;
 
683
                last_extent = handle->extents +
 
684
                                page_size / sizeof(struct extent) - 1;
 
685
                last_extent->start = offset;
 
686
        }
 
687
        error = write_page(handle->fd, handle->extents, handle->extents_spc);
 
688
        handle->extents_spc = offset;
 
689
        return error;
 
690
}
 
691
 
 
692
/**
 
693
 *      next_swap_page - take one swap page out of the pool allocated using
 
694
 *                      alloc_swap() before
 
695
 *      @handle:        Pointer to the structure containing information about
 
696
 *                      the preallocated swap pool.
 
697
 */
 
698
static loff_t next_swap_page(struct swap_writer *handle)
 
699
{
 
700
        struct extent ext;
 
701
 
 
702
        handle->cur_offset += page_size;
 
703
        if (handle->cur_offset < handle->cur_extent->end)
 
704
                return handle->cur_offset;
 
705
        /* We have exhausted the current extent.  Forward to the next one */
 
706
        handle->cur_extent++;
 
707
        handle->cur_extent_idx++;
 
708
        if (handle->cur_extent_idx < handle->nr_extents) {
 
709
                handle->cur_offset = handle->cur_extent->start;
 
710
                return handle->cur_offset;
 
711
        }
 
712
        /* No more extents.  Is there anything to pass to alloc_swap()? */
 
713
        if (handle->cur_extent->start < handle->cur_extent->end) {
 
714
                ext = *handle->cur_extent;
 
715
                memset(handle->cur_extent, 0, sizeof(struct extent));
 
716
                handle->nr_extents = 1;
 
717
        } else {
 
718
                memset(&ext, 0, sizeof(struct extent));
 
719
                handle->nr_extents = 0;
 
720
        }
 
721
        if (save_extents(handle, 0))
 
722
                return 0;
 
723
        memset(handle->extents, 0, page_size);
 
724
        *handle->extents = ext;
 
725
        return preallocate_swap(handle);
 
726
}
 
727
 
 
728
/**
 
729
 *      save_page - save one page of data to the swap
 
730
 *      @handle:        Pointer to the structure containing information about
 
731
 *                      the swap.
 
732
 *      @src:           Pointer to the data.
 
733
 */
 
734
static int save_page(struct swap_writer *handle, void *src)
 
735
{
 
736
        loff_t offset;
 
737
        int error;
 
738
 
 
739
        offset = next_swap_page(handle);
 
740
        if (!offset)
 
741
                return -ENOSPC;
 
742
        error = write_page(handle->fd, src, offset);
 
743
        if (error)
 
744
                return error;
 
745
        handle->swap_needed -= page_size;
 
746
        handle->written_data += page_size;
 
747
        return 0;
 
748
}
 
749
 
 
750
#ifdef CONFIG_THREADS
 
751
/*
 
752
 * If threads are used for saving the image with compression and encryption,
 
753
 * there are three of them.
 
754
 *
 
755
 * The main one reads image pages from the kernel and puts them into a work
 
756
 * buffer.  When the work buffer is full, it gets compressed, but that's not an
 
757
 * in-place compression, so the result has to be stored somewhere else.  There
 
758
 * are four so-called "write" buffers for that and the first empty "write"
 
759
 * buffer is used as the target.  If all of the "write" buffers are full, the
 
760
 * thread has to wait (see the rules below).  Otherwise, after placing the
 
761
 * (compressed) contents of the work buffer into a "write" buffer, the main
 
762
 * thread regards the work buffer as empty and starts to read more image pages
 
763
 * from the kernel.
 
764
 *
 
765
 * The second thread (call it the "move" thread) encrypts the contents of the
 
766
 * "write" buffers, one buffer at a time.  It really encrypts individual pages
 
767
 * and the encryption is not in-place, too.  The encrypted pages of data are
 
768
 * placed in yet another buffer (call it the "encrypt" buffer) until it's full,
 
769
 * in which case the "move" thread has to wait. Of course, it also has to wait
 
770
 * for data from the main thread if all of the "write" buffers are empty.
 
771
 * After encrypting an entire "write" buffer, the "move" thread progresses to
 
772
 * the next "write" buffer, in a round-robin manner.
 
773
 *
 
774
 * The synchronization between the main thread and the "move" thread is done
 
775
 * with the help of two index variables, move_start and move_end. �The rule
 
776
 * is that:
 
777
 * (1) the main thread can only put data into write_buffers[move_start],
 
778
 * (2) after putting data into write_buffers[move_start], the main thread
 
779
 *     increases move_start, modulo the number of "write" buffers, but
 
780
 *     move_start cannot be modified as long as the _next_ "write" buffer is
 
781
 *     write_buffers[move_end] (the thread has to wait if that happens),
 
782
 * (3) the "move" thread can only read data from write_buffers[move_end] and
 
783
 *     only if move_end != move_start (it has to wait if that's not the case),
 
784
 * (4) after reading data from write_buffers[move_end], the "move" thread
 
785
 *     increases move_end, modulo the number of "write" buffers.
 
786
 * This way, move_end always "follows" move_start and the threads don't access
 
787
 * the same buffer at any time.
 
788
 *
 
789
 * The third thread (call it the "save" thread) reads (encrypted) pages of data
 
790
 * from the "encrypt" buffer and writes them out to the swap.  This is done if
 
791
 * there are some pages to write in the "encrypt" buffer, otherwise the "save"
 
792
 * thread has to wait for the "move" thread to put more pages in there.
 
793
 *
 
794
 * The synchronization between the "move" thread and the "save" thread is done
 
795
 * with the help of two pointers, save_start and save_end, where save_start
 
796
 * points to the first empty page and save_end points to the last data page
 
797
 * that hasn't been written out yet.  Thus, the rule is:
 
798
 * (1) the "move" thread can only put data into the page pointed to by
 
799
 *     save_start,
 
800
 * (2) after putting data into the page pointed to by save_start, the "move"
 
801
 *     thread increases save_start, modulo the number of pages in the buffer,
 
802
 *     provided that the _next_ page is not the one pointed to by save_end (it
 
803
 *     has to wait if that happens),
 
804
 * (3) the "save" thread can only read from the page pointed to by save_end,
 
805
 *     as long as save_end != save_start (it has to wait if the two pointers
 
806
 *     are equal),
 
807
 * (4) after writing data from the page pointed to by save_end, the "save"
 
808
 *     thread increases save_end, modulo the number of pages in the buffer.
 
809
 * IOW, the "encrypt" buffer is handled as a typical circular buffer with one
 
810
 * producer (the "move" thread) and one consumer (the "save" thread).
 
811
 *
 
812
 * If encryption is not used, the "save" thread is not started and the "move"
 
813
 * thread writes data to the swap directly out of the "write" buffers.
 
814
 */
 
815
 
 
816
static int save_ret;
 
817
static pthread_mutex_t finish_mutex;
 
818
static pthread_cond_t finish_cond;
 
819
 
 
820
static char *encrypt_buf;
 
821
static char *save_start, *save_end;
 
822
static pthread_mutex_t save_mutex;
 
823
static pthread_cond_t save_cond;
 
824
static pthread_t save_th;
 
825
 
 
826
struct write_buffer {
 
827
        ssize_t size;
 
828
        void *start;
 
829
};
 
830
 
 
831
static struct write_buffer write_buffers[WRITE_BUFFERS];
 
832
static int move_start, move_end;
 
833
static pthread_mutex_t move_mutex;
 
834
static pthread_cond_t move_cond;
 
835
static pthread_t move_th;
 
836
 
 
837
#define FORCE_EXIT      1
 
838
 
 
839
static char *save_inc(char *ptr)
 
840
{
 
841
        return encrypt_buf +
 
842
                (((ptr - encrypt_buf) + page_size) % encrypt_buf_size);
 
843
}
 
844
 
 
845
static int move_inc(int index)
 
846
{
 
847
        return (index + 1) % WRITE_BUFFERS;
 
848
}
 
849
 
 
850
static int wait_for_finish(void)
 
851
{
 
852
        pthread_mutex_lock(&finish_mutex);
 
853
        while((save_end != save_start || move_start != move_end) && !save_ret)
 
854
                pthread_cond_wait(&finish_cond, &finish_mutex);
 
855
        pthread_mutex_unlock(&finish_mutex);
 
856
        return save_ret;
 
857
}
 
858
 
 
859
static void *save_thread(void *arg)
 
860
{
 
861
        struct swap_writer *handle = arg;
 
862
        int error = 0;
 
863
 
 
864
        for (;;) {
 
865
                /* Wait until there is a buffer ready for processing. */
 
866
                pthread_mutex_lock(&save_mutex);
 
867
                while(save_end == save_start && !save_ret)
 
868
                        pthread_cond_wait(&save_cond, &save_mutex);
 
869
                pthread_mutex_unlock(&save_mutex);
 
870
 
 
871
                if (save_ret)
 
872
                        return NULL;
 
873
 
 
874
                error = save_page(handle, save_end);
 
875
                if (error) {
 
876
                        pthread_mutex_lock(&finish_mutex);
 
877
                        if (!save_ret)
 
878
                                save_ret = error;
 
879
                        pthread_mutex_unlock(&finish_mutex);
 
880
                        pthread_cond_signal(&move_cond);
 
881
                        pthread_cond_signal(&save_cond);
 
882
                        pthread_cond_signal(&finish_cond);
 
883
                        return NULL;
 
884
                }
 
885
 
 
886
                /* Go to the next page */
 
887
                pthread_mutex_lock(&finish_mutex);
 
888
                pthread_mutex_lock(&save_mutex);
 
889
                save_end = save_inc(save_end);
 
890
                pthread_mutex_unlock(&save_mutex);
 
891
                pthread_mutex_unlock(&finish_mutex);
 
892
 
 
893
                pthread_cond_signal(&save_cond);
 
894
                pthread_cond_signal(&finish_cond);
 
895
        }
 
896
 
 
897
        return NULL;
 
898
}
 
899
 
 
900
#ifdef CONFIG_ENCRYPT
 
901
 
 
902
static void encrypt_and_save_buffer(void)
 
903
{
 
904
        char *src;
 
905
        ssize_t buf_size, moved_size;
 
906
 
 
907
        /*
 
908
         * The buffer to process is at write_buffers[move_end].start and the
 
909
         * size of it is write_buffers[move_end].size .
 
910
         */
 
911
        src = write_buffers[move_end].start;
 
912
        buf_size = write_buffers[move_end].size;
 
913
        moved_size = 0;
 
914
        do {
 
915
                int error;
 
916
                void *next_start;
 
917
 
 
918
                /* Encrypt page_size of data. */
 
919
                error = gcry_cipher_encrypt(cipher_handle,
 
920
                                                save_start, page_size,
 
921
                                                        src, page_size);
 
922
                if (error) {
 
923
                        pthread_mutex_lock(&finish_mutex);
 
924
                        if (!save_ret)
 
925
                                save_ret = error;
 
926
                        pthread_mutex_unlock(&finish_mutex);
 
927
                        pthread_cond_signal(&move_cond);
 
928
                        pthread_cond_signal(&save_cond);
 
929
                        pthread_cond_signal(&finish_cond);
 
930
                        break;
 
931
                }
 
932
 
 
933
                moved_size += page_size;
 
934
                src += page_size;
 
935
 
 
936
                pthread_mutex_lock(&save_mutex);
 
937
                next_start = save_inc(save_start);
 
938
                while (next_start == save_end && !save_ret)
 
939
                        pthread_cond_wait(&save_cond, &save_mutex);
 
940
                save_start = next_start;
 
941
                pthread_mutex_unlock(&save_mutex);
 
942
 
 
943
                pthread_cond_signal(&save_cond);
 
944
        } while (moved_size < buf_size && !save_ret);
 
945
}
 
946
 
 
947
#else /* !CONFIG_ENCRYPT */
 
948
 
 
949
static inline void encrypt_and_save_buffer(void) {}
 
950
 
 
951
#endif /* !CONFIG_ENCRYPT */
 
952
 
 
953
static void save_buffer(struct swap_writer *handle)
 
954
{
 
955
        void *src;
 
956
        ssize_t size;
 
957
 
 
958
        /*
 
959
         * The buffer to process is at write_buffers[move_end].start and the
 
960
         * size of it is write_buffers[move_end].size .
 
961
         */
 
962
        src = write_buffers[move_end].start;
 
963
        size = write_buffers[move_end].size;
 
964
        while (size > 0) {
 
965
                int error = save_page(handle, src);
 
966
                if (error) {
 
967
                        pthread_mutex_lock(&finish_mutex);
 
968
                        if (!save_ret)
 
969
                                save_ret = error;
 
970
                        pthread_mutex_unlock(&finish_mutex);
 
971
                        pthread_cond_signal(&move_cond);
 
972
                        pthread_cond_signal(&finish_cond);
 
973
                        break;
 
974
                }
 
975
                src += page_size;
 
976
                size -= page_size;
 
977
        }
 
978
}
 
979
 
 
980
static void *move_thread(void *arg)
 
981
{
 
982
        struct swap_writer *handle = arg;
 
983
 
 
984
        for (;;) {
 
985
                /* Wait until there is a buffer ready for processing. */
 
986
                pthread_mutex_lock(&move_mutex);
 
987
                while(move_end == move_start && !save_ret)
 
988
                        pthread_cond_wait(&move_cond, &move_mutex);
 
989
                pthread_mutex_unlock(&move_mutex);
 
990
 
 
991
                if (save_ret)
 
992
                        break;
 
993
 
 
994
                if (do_encrypt)
 
995
                        encrypt_and_save_buffer();
 
996
                else
 
997
                        save_buffer(handle);
 
998
 
 
999
                if (save_ret)
 
1000
                        break;
 
1001
 
 
1002
                /* Tell the reader thread that we have processed the buffer */
 
1003
                pthread_mutex_lock(&finish_mutex);
 
1004
                pthread_mutex_lock(&move_mutex);
 
1005
                move_end = move_inc(move_end);
 
1006
                pthread_mutex_unlock(&move_mutex);
 
1007
                pthread_mutex_unlock(&finish_mutex);
 
1008
 
 
1009
                pthread_cond_signal(&move_cond);
 
1010
                pthread_cond_signal(&finish_cond);
 
1011
        }
 
1012
 
 
1013
        return NULL;
 
1014
}
 
1015
 
 
1016
static inline void *current_write_buffer(void)
 
1017
{
 
1018
        return write_buffers[move_start].start;
 
1019
}
 
1020
 
 
1021
static int prepare_next_write_buffer(ssize_t size)
 
1022
{
 
1023
        int next_start;
 
1024
 
 
1025
        /* Move to the next buffer and signal that the current one is ready*/
 
1026
        write_buffers[move_start].size = size;
 
1027
 
 
1028
        pthread_mutex_lock(&move_mutex);
 
1029
        next_start = move_inc(move_start);
 
1030
        while (next_start == move_end && !save_ret)
 
1031
                pthread_cond_wait(&move_cond, &move_mutex);
 
1032
        move_start = next_start;
 
1033
        pthread_mutex_unlock(&move_mutex);
 
1034
 
 
1035
        pthread_cond_signal(&move_cond);
 
1036
 
 
1037
        return save_ret;
 
1038
}
 
1039
 
 
1040
static void start_threads(struct swap_writer *handle)
 
1041
{
 
1042
        int error;
 
1043
        unsigned int write_buf_size;
 
1044
        char *write_buf;
 
1045
        int j;
 
1046
 
 
1047
        encrypt_buf = handle->encrypt_buffer;
 
1048
        save_start = encrypt_buf;
 
1049
        save_end = save_start;
 
1050
 
 
1051
        write_buf_size = do_compress ? compress_buf_size : buffer_size;
 
1052
        write_buf = handle->write_buffer;
 
1053
        for (j = 0; j < WRITE_BUFFERS; j++) {
 
1054
                write_buffers[j].start = write_buf;
 
1055
                write_buf += write_buf_size;
 
1056
        }
 
1057
        move_start = 0;
 
1058
        move_end = move_start;
 
1059
 
 
1060
        if (do_encrypt) {
 
1061
                error = pthread_mutex_init(&save_mutex, NULL);
 
1062
                if (error) {
 
1063
                        perror("pthread_mutex_init() failed:");
 
1064
                        goto Error_exit;
 
1065
                }
 
1066
                error = pthread_cond_init(&save_cond, NULL);
 
1067
                if (error) {
 
1068
                        perror("pthread_cond_init() failed:");
 
1069
                        goto Destroy_save_mutex;
 
1070
                }
 
1071
        }
 
1072
 
 
1073
        error = pthread_mutex_init(&move_mutex, NULL);
 
1074
        if (error) {
 
1075
                perror("pthread_mutex_init() failed:");
 
1076
                goto Destroy_save_cond;
 
1077
        }
 
1078
        error = pthread_cond_init(&move_cond, NULL);
 
1079
        if (error) {
 
1080
                perror("pthread_cond_init() failed:");
 
1081
                goto Destroy_move_mutex;
 
1082
        }
 
1083
 
 
1084
        if (do_encrypt) {
 
1085
                error = pthread_create(&save_th, NULL, save_thread, handle);
 
1086
                if (error) {
 
1087
                        perror("pthread_create() failed:");
 
1088
                        goto Destroy_move_cond;
 
1089
                }
 
1090
        }
 
1091
 
 
1092
        error = pthread_create(&move_th, NULL, move_thread, handle);
 
1093
        if (error) {
 
1094
                perror("pthread_create() failed:");
 
1095
                goto Stop_save_thread;
 
1096
        }
 
1097
 
 
1098
        error = pthread_mutex_init(&finish_mutex, NULL);
 
1099
        if (error) {
 
1100
                perror("pthread_mutex_init() failed:");
 
1101
                goto Stop_move_thread;
 
1102
        }
 
1103
        error = pthread_cond_init(&finish_cond, NULL);
 
1104
        if (error) {
 
1105
                perror("pthread_cond_init() failed:");
 
1106
                goto Destroy_finish_mutex;
 
1107
        }
 
1108
 
 
1109
        return;
 
1110
 
 
1111
 Destroy_finish_mutex:
 
1112
        pthread_mutex_destroy(&finish_mutex);
 
1113
 
 
1114
 Stop_move_thread:
 
1115
        save_ret = FORCE_EXIT;
 
1116
        pthread_cond_signal(&move_cond);
 
1117
        pthread_join(move_th, NULL);
 
1118
 
 
1119
 Stop_save_thread:
 
1120
        if (do_encrypt) {
 
1121
                save_ret = FORCE_EXIT;
 
1122
                pthread_cond_signal(&save_cond);
 
1123
                pthread_join(save_th, NULL);
 
1124
        }
 
1125
 
 
1126
 Destroy_move_cond:
 
1127
        pthread_cond_destroy(&move_cond);
 
1128
 Destroy_move_mutex:
 
1129
        pthread_mutex_destroy(&move_mutex);
 
1130
 
 
1131
 Destroy_save_cond:
 
1132
        if (do_encrypt)
 
1133
                pthread_cond_destroy(&save_cond);
 
1134
 Destroy_save_mutex:
 
1135
        if (do_encrypt)
 
1136
                pthread_mutex_destroy(&save_mutex);
 
1137
 
 
1138
 Error_exit:
 
1139
        use_threads = 0;
 
1140
}
 
1141
 
 
1142
static void stop_threads(void)
 
1143
{
 
1144
        pthread_mutex_lock(&finish_mutex);
 
1145
        if (!save_ret)
 
1146
                save_ret = FORCE_EXIT;
 
1147
        pthread_mutex_unlock(&finish_mutex);
 
1148
 
 
1149
        pthread_cond_destroy(&finish_cond);
 
1150
        pthread_mutex_destroy(&finish_mutex);
 
1151
 
 
1152
        pthread_cond_signal(&move_cond);
 
1153
        pthread_join(move_th, NULL);
 
1154
        if (do_encrypt) {
 
1155
                pthread_cond_signal(&save_cond);
 
1156
                pthread_join(save_th, NULL);
 
1157
        }
 
1158
 
 
1159
        pthread_cond_destroy(&move_cond);
 
1160
        pthread_mutex_destroy(&move_mutex);
 
1161
 
 
1162
        if (do_encrypt) {
 
1163
                pthread_cond_destroy(&save_cond);
 
1164
                pthread_mutex_destroy(&save_mutex);
 
1165
        }
 
1166
}
 
1167
 
 
1168
#else /* !CONFIG_THREADS */
 
1169
 
 
1170
static inline int wait_for_finish(void) { return -ENOSYS; }
 
1171
static inline void *current_write_buffer(void) { return NULL; }
 
1172
static inline int prepare_next_write_buffer(ssize_t size)
 
1173
{
 
1174
        (void)size;
 
1175
        return -ENOSYS;
 
1176
}
 
1177
static inline void start_threads(struct swap_writer *handle) { (void)handle; }
 
1178
static inline void stop_threads(void) {}
 
1179
 
 
1180
#endif /* !CONFIG_THREADS */
 
1181
 
 
1182
/**
 
1183
 *      encrypt_and_save_page - encrypt a page of data and write it to the swap
 
1184
 */
 
1185
static int encrypt_and_save_page(struct swap_writer *handle, void *src)
 
1186
{
 
1187
#ifdef CONFIG_ENCRYPT
 
1188
        if (do_encrypt) {
 
1189
                int error = gcry_cipher_encrypt(cipher_handle,
 
1190
                        handle->encrypt_ptr, page_size, src, page_size);
 
1191
                if (error)
 
1192
                        return error;
 
1193
                src = handle->encrypt_ptr;
 
1194
                handle->encrypt_ptr += page_size;
 
1195
                if (handle->encrypt_ptr - handle->encrypt_buffer
 
1196
                    >= encrypt_buf_size)
 
1197
                        handle->encrypt_ptr = handle->encrypt_buffer;
 
1198
        }
 
1199
#endif
 
1200
        return save_page(handle, src);
 
1201
}
 
1202
 
 
1203
/**
 
1204
 *      flush_buffer - flush data stored in the buffer to the swap
 
1205
 */
 
1206
static int flush_buffer(struct swap_writer *handle)
 
1207
{
 
1208
        ssize_t size;
 
1209
        char *src;
 
1210
        int error = 0;
 
1211
 
 
1212
        /* Check if there is anything to do */
 
1213
        if (handle->page_ptr <= handle->buffer)
 
1214
                return 0;
 
1215
 
 
1216
        size = handle->page_ptr - handle->buffer;
 
1217
        if (compute_checksum || verify_image)
 
1218
                md5_process_block(handle->buffer, size, &handle->ctx);
 
1219
 
 
1220
        src = use_threads ? current_write_buffer() : handle->write_buffer;
 
1221
 
 
1222
        /* Compress the buffer, if necessary */
 
1223
        if (do_compress) {
344
1224
#ifdef CONFIG_COMPRESS
345
 
        if (do_compress) {
 
1225
                struct buf_block *block = (struct buf_block *)src;
346
1226
                lzo_uint cnt;
347
1227
 
348
 
                lzo1x_1_compress(buf, page_size, (lzo_bytep)block->data, &cnt,
349
 
                                        handle->lzo_work_buffer);
 
1228
                lzo1x_1_compress(handle->buffer, size,
 
1229
                                        (lzo_bytep)block->data, &cnt,
 
1230
                                                handle->lzo_work_buffer);
350
1231
                block->size = cnt;
351
 
                /* Sanity check, should not trigger */
352
 
                if (cnt + sizeof(short) > max_block_size)
353
 
                        printf("WARNING!"
354
 
                                " Data size too large after compression!\n");
355
 
                /* We have added a sizeof(short) field to the data */
356
 
                compr_diff += page_size - cnt - sizeof(short);
357
 
                return cnt + sizeof(short);
358
 
        }
359
 
#endif
360
 
        memcpy(block, buf, page_size);
361
 
        return page_size;
362
 
}
363
 
 
364
 
static int try_get_more_swap(struct swap_map_handle *handle)
365
 
{
366
 
        loff_t offset;
367
 
 
368
 
        while (handle->cur_area.size > handle->cur_alloc) {
369
 
                offset = get_swap_page(handle->dev);
370
 
                if (!offset)
371
 
                        return -ENOSPC;
372
 
                if (offset == handle->cur_area.offset + handle->cur_alloc) {
373
 
                        handle->cur_alloc += page_size;
374
 
                } else {
375
 
                        handle->cur_area.offset = offset;
376
 
                        handle->cur_alloc = page_size;
377
 
                }
378
 
        }
379
 
        return 0;
380
 
}
381
 
 
382
 
static int flush_buffer(struct swap_map_handle *handle)
383
 
{
384
 
        void *src = handle->write_buffer;
385
 
        int error =  0;
386
 
 
387
 
#ifdef CONFIG_ENCRYPT
388
 
        if (do_encrypt) {
389
 
                error = gcry_cipher_encrypt(cipher_handle,
390
 
                        handle->encrypt_buffer, handle->cur_area.size,
391
 
                        src, handle->cur_area.size);
392
 
                src = handle->encrypt_buffer;
393
 
        }
394
 
#endif
395
 
        if (!error)
396
 
                error = write_area(handle->fd, src, handle->cur_area.offset,
397
 
                                handle->cur_area.size);
398
 
        if (error)
399
 
                return error;
400
 
        handle->areas[handle->k].offset = handle->cur_area.offset;
401
 
        handle->areas[handle->k].size = handle->cur_area.size;
402
 
        return 0;
403
 
}
404
 
 
405
 
static int swap_write_page(struct swap_map_handle *handle)
406
 
{
407
 
        loff_t offset = 0;
408
 
        int error;
409
 
 
410
 
        if (compute_checksum)
411
 
                md5_process_block(handle->page_buffer, page_size, &handle->ctx);
412
 
 
413
 
        if (!handle->cur_area.size) {
414
 
                /* No data in the write buffer */
415
 
                handle->cur_area.size = prepare(handle, 0);
416
 
                return try_get_more_swap(handle);
417
 
        }
418
 
 
419
 
        if (handle->cur_alloc + max_block_size <= buffer_size) {
420
 
                if (handle->cur_area.size + max_block_size <= handle->cur_alloc) {
421
 
                        handle->cur_area.size += prepare(handle, handle->cur_area.size);
422
 
                        return 0;
423
 
                }
424
 
                offset = get_swap_page(handle->dev);
425
 
                if (!offset)
426
 
                        return -ENOSPC;
427
 
                if (offset == handle->cur_area.offset + handle->cur_alloc) {
428
 
                        handle->cur_alloc += page_size;
429
 
                        handle->cur_area.size += prepare(handle, handle->cur_area.size);
430
 
                        return try_get_more_swap(handle);
431
 
                }
432
 
        }
433
 
 
434
 
        /* The write buffer is full or the offset doesn't fit.  Flush the buffer */
435
 
        error = flush_buffer(handle);
436
 
        if (error)
437
 
                return error;
438
 
 
439
 
        /* The write buffer has been flushed.  Fill it from the start */
440
 
        if (!offset) {
441
 
                offset = get_swap_page(handle->dev);
442
 
                if (!offset)
443
 
                        return -ENOSPC;
444
 
        }
445
 
        handle->cur_area.offset = offset;
446
 
        handle->cur_alloc = page_size;
447
 
        handle->cur_area.size = prepare(handle, 0);
448
 
        error = try_get_more_swap(handle);
449
 
        if (error)
450
 
                return error;
451
 
        if (++handle->k >= handle->areas_per_page) {
452
 
                offset = get_swap_page(handle->dev);
453
 
                if (!offset)
454
 
                        return -ENOSPC;
455
 
                *handle->next_swap = offset;
456
 
                error = write_area(handle->fd, handle->areas, handle->cur_swap,
457
 
                                page_size);
 
1232
                size = cnt + sizeof(size_t);
 
1233
#endif
 
1234
        } else if (use_threads) {
 
1235
                memcpy(src, handle->buffer, size);
 
1236
        }
 
1237
 
 
1238
        if (use_threads)
 
1239
                return prepare_next_write_buffer(size);
 
1240
 
 
1241
        /*
 
1242
         * If there's no compression and threads are not used, handle->buffer is
 
1243
         * equal to handle->write_buffer.  In that case, the data are taken
 
1244
         * directly out of handle->buffer.
 
1245
         */
 
1246
        while (size > 0) {
 
1247
                error = encrypt_and_save_page(handle, src);
458
1248
                if (error)
459
 
                        return error;
460
 
                memset(handle->areas, 0, page_size);
461
 
                handle->cur_swap = offset;
462
 
                handle->k = 0;
 
1249
                        break;
 
1250
                src += page_size;
 
1251
                size -= page_size;
463
1252
        }
464
 
        return 0;
465
 
}
466
 
 
467
 
static inline int flush_swap_writer(struct swap_map_handle *handle)
468
 
{
469
 
        int error;
470
 
 
471
 
        if (!handle->cur_area.offset || !handle->cur_swap)
472
 
                return -EINVAL;
473
 
 
474
 
        /* Flush the write buffer */
475
 
        error = flush_buffer(handle);
476
 
        if (!error)
477
 
                /* Save the last swap map page */
478
 
                error = write_area(handle->fd, handle->areas, handle->cur_swap,
479
 
                                page_size);
480
1253
 
481
1254
        return error;
482
1255
}
483
1256
 
484
1257
/**
485
 
 *      save_image - save the suspend image data
 
1258
 *      save_image - save the hibernation image data
486
1259
 */
487
 
 
488
 
static int save_image(struct swap_map_handle *handle,
489
 
                      unsigned int nr_pages)
 
1260
static int save_image(struct swap_writer *handle, unsigned int nr_pages)
490
1261
{
491
1262
        unsigned int m, writeout_rate;
492
 
        int ret, abort_possible;
 
1263
        ssize_t ret;
493
1264
        struct termios newtrm, savedtrm;
494
 
        int error = 0;
 
1265
        int abort_possible, key, error = 0;
495
1266
        char message[SPLASH_GENERIC_MESSAGE_SIZE];
496
1267
 
497
1268
        /* Switch the state of the terminal so that we can read the keyboard
503
1274
 
504
1275
        sprintf(message, "Saving %u image data pages", nr_pages);
505
1276
        if (abort_possible)
506
 
                strcat(message, "(press " ABORT_KEY_NAME " to abort)");
 
1277
                strcat(message, " (press " ABORT_KEY_NAME " to abort) ");
507
1278
        strcat(message, "...");
508
1279
        printf("%s: %s     ", my_name, message);
509
1280
        splash.set_caption(message);
510
1281
 
 
1282
        if (use_threads)
 
1283
                start_threads(handle);
 
1284
 
511
1285
        m = nr_pages / 100;
512
1286
        if (!m)
513
1287
                m = 1;
515
1289
        if (early_writeout)
516
1290
                writeout_rate = m;
517
1291
        else
518
 
                writeout_rate = nr_pages;
 
1292
                writeout_rate = nr_pages + 1;
519
1293
 
 
1294
        /* The buffer may be partially filled at this point */
520
1295
        for (nr_pages = 0; ; nr_pages++) {
521
 
                ret = read(handle->dev, handle->page_buffer, page_size);
522
 
                if (ret <= 0)
 
1296
                ret = read(handle->input, handle->page_ptr, page_size);
 
1297
                if (ret < page_size) {
 
1298
                        if (ret < 0) {
 
1299
                                error = -EIO;
 
1300
                                perror("\nError reading an image page");
 
1301
                        } else if (ret > 0) {
 
1302
                                error = -EFAULT;
 
1303
                                perror("\nShort read from /dev/snapshot?");
 
1304
                        }
523
1305
                        break;
 
1306
                }
524
1307
 
525
 
                error = swap_write_page(handle);
526
 
                if (error)
527
 
                        break;
 
1308
                handle->page_ptr += page_size;
528
1309
 
529
1310
                if (!(nr_pages % m)) {
530
1311
                        printf("\b\b\b\b%3d%%", nr_pages / m);
531
1312
                        splash.progress(20 + (nr_pages / m) * 0.75);
532
1313
 
533
 
                        switch (splash.key_pressed()) {
 
1314
                        while ((key = splash.key_pressed()) > 0) {
 
1315
                                switch (key) {
534
1316
                                case ABORT_KEY_CODE:
535
1317
                                        if (abort_possible) {
536
1318
                                                printf(" aborted!\n");
545
1327
                                        shutdown_method =
546
1328
                                                        SHUTDOWN_METHOD_REBOOT;
547
1329
                                        break;
 
1330
                                }
548
1331
                        }
549
1332
                }
550
1333
 
551
 
                if (!(nr_pages % writeout_rate))
 
1334
                if (!((nr_pages + 1) % writeout_rate))
552
1335
                        start_writeout(handle->fd);
553
 
        }
554
 
 
555
 
        if (ret < 0)
556
 
                error = -errno;
557
 
 
558
 
        if (!error)
559
 
                printf(" done (%u pages)\n", nr_pages);
560
 
 
561
 
Exit:
 
1336
 
 
1337
                if (handle->page_ptr - handle->buffer >= buffer_size) {
 
1338
                        /* The buffer is full, flush it */
 
1339
                        error = flush_buffer(handle);
 
1340
                        if (error)
 
1341
                                break;
 
1342
                        handle->page_ptr = handle->buffer;
 
1343
                }
 
1344
        }
 
1345
 
 
1346
        if (!error) {
 
1347
                /* Flush whatever's left in the buffer and save the extents */
 
1348
                error = flush_buffer(handle);
 
1349
                if (use_threads)
 
1350
                        error = wait_for_finish();
 
1351
                if (!error)
 
1352
                        error = save_extents(handle, 1);
 
1353
                if (!error)
 
1354
                        printf(" done (%u pages)\n", nr_pages);
 
1355
        }
 
1356
 
 
1357
 Exit:
 
1358
        if (use_threads)
 
1359
                stop_threads();
 
1360
 
562
1361
        if (abort_possible)
563
1362
                splash.restore_abort(&savedtrm);
564
1363
 
571
1370
 *      Returns TRUE or FALSE after checking the total amount of swap
572
1371
 *      space avaiable from the resume partition.
573
1372
 */
574
 
 
575
 
static int enough_swap(int dev, unsigned long size)
 
1373
static int enough_swap(struct swap_writer *handle)
576
1374
{
577
 
        loff_t free_swap = check_free_swap(dev);
 
1375
        loff_t free_swap = check_free_swap(handle->dev);
 
1376
        loff_t size = do_compress ?
 
1377
                        handle->swap_needed / 2 : handle->swap_needed;
578
1378
 
579
 
        printf("%s: Free swap: %lu kilobytes\n", my_name,
580
 
                (unsigned long)free_swap / 1024);
 
1379
        printf("%s: Free swap: %llu kilobytes\n", my_name,
 
1380
                (unsigned long long)free_swap / 1024);
581
1381
        return free_swap > size;
582
1382
}
583
1383
 
587
1387
{
588
1388
        int error = 0;
589
1389
        unsigned int size = sizeof(struct swsusp_header);
590
 
        unsigned int shift = (resume_offset + 1) * page_size - size;
 
1390
        off64_t shift = ((off64_t)resume_offset + 1) * page_size - size;
591
1391
 
592
 
        if (lseek(fd, shift, SEEK_SET) != shift)
 
1392
        if (lseek64(fd, shift, SEEK_SET) != shift)
593
1393
                return -EIO;
594
1394
 
595
1395
        if (read(fd, &swsusp_header, size) < size)
600
1400
                memcpy(swsusp_header.orig_sig, swsusp_header.sig, 10);
601
1401
                memcpy(swsusp_header.sig, SWSUSP_SIG, 10);
602
1402
                swsusp_header.image = start;
603
 
                if (lseek(fd, shift, SEEK_SET) != shift)
 
1403
                if (lseek64(fd, shift, SEEK_SET) != shift)
604
1404
                        return -EIO;
605
1405
 
606
1406
                if (write(fd, &swsusp_header, size) < size)
613
1413
 
614
1414
/**
615
1415
 *      write_image - Write entire image and metadata.
 
1416
 *      @snapshot_fd: File handle of the snapshot device
 
1417
 *      @resume_fd: File handle of the swap device used for image saving
 
1418
 *      @test_fd: (Optional) File handle of a file to read the image from
 
1419
 *
 
1420
 *      If @test_fd is not negative, the function works in the test mode in
 
1421
 *      which the image is read from a regular file instead of the snapshot
 
1422
 *      device.
616
1423
 */
617
 
 
618
 
int write_image(int snapshot_fd, int resume_fd)
 
1424
static int write_image(int snapshot_fd, int resume_fd, int test_fd)
619
1425
{
620
 
        static struct swap_map_handle handle;
621
 
        struct swsusp_info *header = mem_pool;
 
1426
        static struct swap_writer handle;
 
1427
        struct image_header_info *header;
622
1428
        loff_t start;
623
 
        int error;
624
 
        struct timeval begin;
 
1429
        loff_t image_size;
 
1430
        double real_size;
 
1431
        unsigned long nr_pages = 0;
 
1432
        int error, test_mode = (test_fd >= 0);
 
1433
        struct timeval begin;
625
1434
 
626
1435
        printf("%s: System snapshot ready. Preparing to write\n", my_name);
627
 
        start = get_swap_page(snapshot_fd);
 
1436
        /* Allocate a swap page for the additional "userland" header */
 
1437
        start = alloc_swap_page(snapshot_fd, 1);
628
1438
        if (!start)
629
1439
                return -ENOSPC;
630
 
        error = read(snapshot_fd, header, page_size);
631
 
        if (error < (int)page_size)
632
 
                return error < 0 ? error : -EFAULT;
633
 
        printf("%s: Image size: %lu kilobytes\n", my_name, header->size / 1024);
634
 
        if (!enough_swap(snapshot_fd, header->size) && !do_compress) {
 
1440
 
 
1441
        header  = getmem(page_size);
 
1442
        memset(header, 0, page_size);
 
1443
 
 
1444
        error = init_swap_writer(&handle, snapshot_fd, resume_fd, test_fd);
 
1445
        if (error)
 
1446
                goto Exit;
 
1447
 
 
1448
        image_size = test_mode ? test_image_size : get_image_size(snapshot_fd);
 
1449
        if (image_size > 0) {
 
1450
                nr_pages = (unsigned long)((image_size + page_size - 1) /
 
1451
                                                page_size);
 
1452
        } else {
 
1453
                /*
 
1454
                 * The kernel doesn't allow us to get the image size via ioctl,
 
1455
                 * so we need to read it from the image header.
 
1456
                 */
 
1457
                struct swsusp_info *image_header;
 
1458
                ssize_t ret;
 
1459
 
 
1460
                /*
 
1461
                 * Do it in such a way that save_image() will believe it has
 
1462
                 * already read the header page.
 
1463
                 */
 
1464
                image_header = handle.page_ptr;
 
1465
                ret = read(snapshot_fd, image_header, page_size);
 
1466
                if (ret < page_size) {
 
1467
                        error = ret < 0 ? ret : -EFAULT;
 
1468
                        goto Free_writer;
 
1469
                }
 
1470
                handle.page_ptr += page_size;
 
1471
                image_size = image_header->size;
 
1472
                nr_pages = image_header->pages;
 
1473
                if (!nr_pages) {
 
1474
                        error = -ENODATA;
 
1475
                        goto Free_writer;
 
1476
                }
 
1477
                /* We have already read one page */
 
1478
                nr_pages--;
 
1479
        }
 
1480
        printf("%s: Image size: %lu kilobytes\n", my_name, (unsigned long) image_size / 1024);
 
1481
        real_size = image_size;
 
1482
 
 
1483
        handle.swap_needed = image_size;
 
1484
        if (do_compress) {
 
1485
                /* This is necessary in case the image is not compressible */
 
1486
                handle.swap_needed += round_up_page_size(
 
1487
                                        (handle.swap_needed >> 4) + 67);
 
1488
        }
 
1489
        if (!enough_swap(&handle)) {
635
1490
                fprintf(stderr, "%s: Not enough free swap\n", my_name);
636
 
                return -ENOSPC;
637
 
        }
638
 
        error = init_swap_writer(&handle, snapshot_fd, resume_fd,
639
 
                        (char *)mem_pool + page_size);
640
 
        if (error)
641
 
                goto Exit;
642
 
 
643
 
        header->map_start = handle.cur_swap;
644
 
        header->image_flags = 0;
645
 
        max_block_size = page_size;
 
1491
                error = -ENOSPC;
 
1492
                goto Free_writer;
 
1493
        }
 
1494
        if (!preallocate_swap(&handle)) {
 
1495
                fprintf(stderr, "%s: Failed to allocate swap\n", my_name);
 
1496
                error = -ENOSPC;
 
1497
                goto Free_writer;
 
1498
        }
 
1499
        /* Shift handle.cur_offset for the first call to next_swap_page() */
 
1500
        handle.cur_offset -= page_size;
 
1501
 
 
1502
        header->pages = nr_pages;
 
1503
        header->flags = 0;
 
1504
        header->map_start = handle.extents_spc;
 
1505
 
646
1506
        if (compute_checksum)
647
 
                header->image_flags |= IMAGE_CHECKSUM;
 
1507
                header->flags |= IMAGE_CHECKSUM;
648
1508
 
649
 
        if (do_compress) {
650
 
                header->image_flags |= IMAGE_COMPRESSED;
651
 
                /*
652
 
                 * The formula below follows from the worst-case expansion
653
 
                 * calculation for LZO1.  sizeof(short) is due to the size field
654
 
                 * in 'struct buf_block'.
655
 
                 */
656
 
                max_block_size += (page_size >> 4) + 67 + sizeof(short);
657
 
        }
 
1509
        if (do_compress)
 
1510
                header->flags |= IMAGE_COMPRESSED;
658
1511
 
659
1512
#ifdef CONFIG_ENCRYPT
660
1513
        if (!do_encrypt)
661
1514
                goto Save_image;
662
1515
 
663
1516
        if (use_RSA) {
664
 
                error = gcry_cipher_setkey(cipher_handle, key_data->key,
 
1517
                error = gcry_cipher_setkey(cipher_handle, key_data.key,
665
1518
                                                KEY_SIZE);
666
1519
                if (error)
667
1520
                        goto No_RSA;
668
1521
 
669
 
                error = gcry_cipher_setiv(cipher_handle, key_data->ivec,
 
1522
                error = gcry_cipher_setiv(cipher_handle, key_data.ivec,
670
1523
                                                CIPHER_BLOCK);
671
1524
                if (error)
672
1525
                        goto No_RSA;
673
1526
 
674
 
                header->image_flags |= IMAGE_ENCRYPTED | IMAGE_USE_RSA;
675
 
                memcpy(&header->rsa, &key_data->rsa, sizeof(struct RSA_data));
676
 
                memcpy(&header->key, &key_data->encrypted_key,
 
1527
                header->flags |= IMAGE_ENCRYPTED | IMAGE_USE_RSA;
 
1528
                memcpy(&header->rsa, &key_data.rsa, sizeof(struct RSA_data));
 
1529
                memcpy(&header->key, &key_data.encrypted_key,
677
1530
                                                sizeof(struct encrypted_key));
678
1531
        } else {
679
1532
                int j;
680
1533
 
681
1534
No_RSA:
682
 
                encrypt_init(key_data->key, key_data->ivec, password);
 
1535
                encrypt_init(key_data.key, key_data.ivec, password);
683
1536
                splash.progress(20);
684
1537
                get_random_salt(header->salt, CIPHER_BLOCK);
685
1538
                for (j = 0; j < CIPHER_BLOCK; j++)
686
 
                        key_data->ivec[j] ^= header->salt[j];
 
1539
                        key_data.ivec[j] ^= header->salt[j];
687
1540
 
688
 
                error = gcry_cipher_setkey(cipher_handle, key_data->key,
 
1541
                error = gcry_cipher_setkey(cipher_handle, key_data.key,
689
1542
                                                KEY_SIZE);
690
1543
                if (!error)
691
 
                        error = gcry_cipher_setiv(cipher_handle, key_data->ivec,
 
1544
                        error = gcry_cipher_setiv(cipher_handle, key_data.ivec,
692
1545
                                                CIPHER_BLOCK);
693
1546
                if (!error)
694
 
                        header->image_flags |= IMAGE_ENCRYPTED;
 
1547
                        header->flags |= IMAGE_ENCRYPTED;
695
1548
        }
696
1549
 
697
1550
        if (error) {
698
1551
                fprintf(stderr,"%s: libgcrypt error: %s\n", my_name,
699
1552
                        gcry_strerror(error));
700
 
                goto Exit;
 
1553
                goto Free_writer;
701
1554
        }
702
1555
 
703
1556
Save_image:
704
1557
#endif
705
1558
        gettimeofday(&begin, NULL);
706
1559
 
707
 
        error = save_image(&handle, header->pages - 1);
708
 
        if (error)
709
 
                goto Exit;
710
 
 
711
 
        /*
712
 
         * NOTICE: This needs to go after save_image(), because the user may
713
 
         * modify the behavior.
714
 
         */
715
 
        if (shutdown_method == SHUTDOWN_METHOD_PLATFORM)
716
 
                header->image_flags |= PLATFORM_SUSPEND;
717
 
 
718
 
        error = flush_swap_writer(&handle);
 
1560
        error = save_image(&handle, nr_pages);
719
1561
        if (!error) {
720
1562
                struct timeval end;
721
1563
 
722
1564
                fsync(resume_fd);
723
1565
 
724
 
                if (compute_checksum)
 
1566
                header->image_data_size = handle.written_data;
 
1567
                real_size = handle.written_data;
 
1568
 
 
1569
                /*
 
1570
                 * NOTICE: This needs to go after save_image(), because the
 
1571
                 * user may modify the behavior.
 
1572
                 */
 
1573
                if (shutdown_method == SHUTDOWN_METHOD_PLATFORM)
 
1574
                        header->flags |= PLATFORM_SUSPEND;
 
1575
 
 
1576
                if (compute_checksum || verify_image)
725
1577
                        md5_finish_ctx(&handle.ctx, header->checksum);
726
1578
 
727
1579
                gettimeofday(&end, NULL);
728
1580
                timersub(&end, &begin, &end);
729
1581
                header->writeout_time = end.tv_usec / 1000000.0 + end.tv_sec;
730
1582
 
731
 
                error = write_area(resume_fd, header, start, page_size);
 
1583
                header->resume_pause = resume_pause;
 
1584
 
 
1585
                error = write_page(resume_fd, header, start);
 
1586
                fsync(resume_fd);
 
1587
        }
 
1588
 
 
1589
 Free_writer:
 
1590
        free_swap_writer(&handle);
 
1591
 
 
1592
        if (!error && (verify_image || test_mode)) {
 
1593
                splash.progress(0);
 
1594
                if (verify_image)
 
1595
                        printf("%s: Image verification\n", my_name);
 
1596
                error = read_or_verify(snapshot_fd, resume_fd, header, start,
 
1597
                                        verify_image, test_mode);
 
1598
                if (verify_image)
 
1599
                        printf(error ? "%s: Image verification failed\n" :
 
1600
                                        "%s: Image verified successfully\n",
 
1601
                                        my_name);
 
1602
                splash.progress(100);
732
1603
        }
733
1604
 
734
1605
        if (!error) {
735
1606
                if (do_compress) {
736
 
                        double delta = header->size - compr_diff;
737
 
 
738
1607
                        printf("%s: Compression ratio %4.2lf\n", my_name,
739
 
                                delta / header->size);
 
1608
                                real_size / image_size);
740
1609
                }
741
 
                printf( "S" );
 
1610
                printf("S");
742
1611
                error = mark_swap(resume_fd, start);
 
1612
                if (!error) {
 
1613
                        fsync(resume_fd);
 
1614
                        printf( "|" );
 
1615
                }
 
1616
                printf("\n");
743
1617
        }
744
1618
 
745
 
        fsync(resume_fd);
746
 
 
747
 
        if (!error)
748
 
                printf( "|" );
749
 
 
750
 
Exit:
751
 
        printf("\n");
 
1619
 Exit:
 
1620
        freemem(header);
 
1621
 
752
1622
        return error;
753
1623
}
754
1624
 
755
 
#ifdef CONFIG_BOTH
756
1625
static int reset_signature(int fd)
757
1626
{
758
1627
        int ret, error = 0;
759
1628
        unsigned int size = sizeof(struct swsusp_header);
760
 
        unsigned int shift = (resume_offset + 1) * page_size - size;
 
1629
        off64_t shift = ((off64_t)resume_offset + 1) * page_size - size;
761
1630
 
762
 
        if (lseek(fd, shift, SEEK_SET) != shift)
 
1631
        if (lseek64(fd, shift, SEEK_SET) != shift)
763
1632
                return -EIO;
764
1633
 
765
1634
        memset(&swsusp_header, 0, size);
776
1645
        if (!error) {
777
1646
                /* Reset swap signature now */
778
1647
                memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
779
 
                if (lseek(fd, shift, SEEK_SET) == shift) {
 
1648
                if (lseek64(fd, shift, SEEK_SET) == shift) {
780
1649
                        ret = write(fd, &swsusp_header, size);
781
1650
                        if (ret != size)
782
1651
                                error = ret < 0 ? ret : -EIO;
786
1655
        }
787
1656
        fsync(fd);
788
1657
        if (error) {
789
 
                fprintf(stderr,
790
 
                        "reset_signature: Error %d resetting the image.\n"
791
 
                        "There should be valid image on disk. Powerdown and do normal resume.\n"
792
 
                        "Continuing with this booted system will lead to data corruption.\n", 
793
 
                        error);
794
 
                while(1);
 
1658
                fprintf(stderr, "%s: Error %d resetting the image.\n"
 
1659
                        "There should be valid image on disk. "
 
1660
                        "Powerdown and carry out normal resume.\n"
 
1661
                        "Continuing with this booted system "
 
1662
                        "will lead to data corruption.\n", my_name, error);
 
1663
                while(1)
 
1664
                        sleep(10);
795
1665
        }
796
1666
        return error;
797
1667
}
798
 
#endif
799
1668
 
800
1669
static void suspend_shutdown(int snapshot_fd)
801
1670
{
804
1673
        if (shutdown_method == SHUTDOWN_METHOD_REBOOT) {
805
1674
                reboot();
806
1675
        } else if (shutdown_method == SHUTDOWN_METHOD_PLATFORM) {
807
 
                int ret = platform_enter(snapshot_fd);
808
 
                if (ret < 0)
809
 
                        suspend_error("pm_ops->enter failed, calling power_off.");
 
1676
                if (platform_enter(snapshot_fd))
 
1677
                        suspend_error("Could not enter the hibernation state, "
 
1678
                                        "calling power_off.");
810
1679
        }
811
1680
        power_off();
812
1681
        /* Signature is on disk, it is very dangerous to continue now.
816
1685
                sleep (60);
817
1686
}
818
1687
 
819
 
int suspend_system(int snapshot_fd, int resume_fd)
 
1688
int suspend_system(int snapshot_fd, int resume_fd, int test_fd)
820
1689
{
821
1690
        loff_t avail_swap;
822
 
        unsigned long image_size;
 
1691
        loff_t image_size;
823
1692
        int attempts, in_suspend, error = 0;
824
1693
        char message[SPLASH_GENERIC_MESSAGE_SIZE];
825
1694
 
829
1698
        else
830
1699
                image_size = avail_swap;
831
1700
        if (!avail_swap) {
832
 
                fprintf(stderr, "%s: No swap space for suspend\n", my_name);
 
1701
                suspend_error("Not enough swap space for suspend");
833
1702
                return ENOSPC;
834
1703
        }
835
1704
 
841
1710
        splash.switch_to();
842
1711
        splash.progress(15);
843
1712
 
844
 
        if (error)
845
 
                goto Unfreeze;
 
1713
        if (error) {
 
1714
                suspend_error("Freeze failed.");
 
1715
                goto Unfreeze;
 
1716
        }
 
1717
 
 
1718
        if (test_fd >= 0) {
 
1719
                printf("%s: Running in test mode\n", my_name);
 
1720
                error = write_image(snapshot_fd, resume_fd, test_fd);
 
1721
                if (error)
 
1722
                        error = -error;
 
1723
                reset_signature(resume_fd);
 
1724
                free_swap_pages(snapshot_fd);
 
1725
                goto Unfreeze;
 
1726
        }
846
1727
 
847
1728
        if (shutdown_method == SHUTDOWN_METHOD_PLATFORM) {
848
 
                int ret = platform_prepare(snapshot_fd);
849
 
                if (ret < 0) {
850
 
                        suspend_error("pm_ops->prepare failed, using "
851
 
                                "'shutdown mode = shutdown'.");
 
1729
                if (platform_prepare(snapshot_fd)) {
 
1730
                        suspend_error("Unable to use platform hibernation "
 
1731
                                        "support, using shutdown mode.");
852
1732
                        shutdown_method = SHUTDOWN_METHOD_SHUTDOWN;
853
1733
                }
854
1734
        }
874
1754
                        break;
875
1755
                }
876
1756
 
877
 
                error = write_image(snapshot_fd, resume_fd);
 
1757
                error = write_image(snapshot_fd, resume_fd, -1);
878
1758
                if (error) {
879
1759
                        free_swap_pages(snapshot_fd);
880
1760
                        free_snapshot(snapshot_fd);
885
1765
                } else {
886
1766
                        splash.progress(100);
887
1767
#ifdef CONFIG_BOTH
888
 
                        if (s2ram) {
 
1768
                        if (s2ram_kms || s2ram) {
889
1769
                                /* If we die (and allow system to continue)
890
1770
                                 * between now and reset_signature(), very bad
891
1771
                                 * things will happen. */
895
1775
                                reset_signature(resume_fd);
896
1776
                                free_swap_pages(snapshot_fd);
897
1777
                                free_snapshot(snapshot_fd);
898
 
                                s2ram_resume();
 
1778
                                if (!s2ram_kms)
 
1779
                                        s2ram_resume();
899
1780
                                goto Unfreeze;
900
1781
                        }
901
1782
Shutdown:
902
1783
#endif
 
1784
                        close(resume_fd);
903
1785
                        suspend_shutdown(snapshot_fd);
904
1786
                }
905
1787
        } while (--attempts);
919
1801
 *      console_fd - get file descriptor for given file name and verify
920
1802
 *      if that's a console descriptor (based on the code of openvt)
921
1803
 */
922
 
 
923
1804
static inline int console_fd(const char *fname)
924
1805
{
925
1806
        int fd;
928
1809
        fd = open(fname, O_RDONLY);
929
1810
        if (fd < 0 && errno == EACCES)
930
1811
                fd = open(fname, O_WRONLY);
931
 
        if (fd >= 0 && (ioctl(fd, KDGKBTYPE, &arg) || (arg != KB_101 && arg != KB_84))) {
 
1812
        if (fd >= 0 && (ioctl(fd, KDGKBTYPE, &arg)
 
1813
            || (arg != KB_101 && arg != KB_84))) {
932
1814
                close(fd);
933
1815
                return -ENOTTY;
934
1816
        }
946
1828
 *      the standard streams to it.  The number of the currently active
947
1829
 *      virtual terminal is saved via @orig_vc
948
1830
 */
949
 
 
950
1831
static int prepare_console(int *orig_vc, int *new_vc)
951
1832
{
952
1833
        int fd, error, vt = -1;
953
 
        char *vt_name = mem_pool;
 
1834
        char vt_name[GENERIC_NAME_SIZE];
954
1835
        struct vt_stat vtstat;
955
1836
        char clear_vt, tiocl[2];
956
1837
 
1026
1907
 
1027
1908
/**
1028
1909
 *      restore_console - switch to the virtual console that was active before
1029
 
 *      suspend
 
1910
 *                        suspend
1030
1911
 */
1031
 
 
1032
1912
static void restore_console(int fd, int orig_vc)
1033
1913
{
1034
1914
        int error;
1095
1975
        gcry_ac_data_t rsa_data_set, key_set;
1096
1976
        gcry_ac_key_t rsa_pub;
1097
1977
        gcry_mpi_t mpi;
 
1978
        size_t size;
1098
1979
        int ret, fd, rnd_fd;
1099
1980
        struct RSA_data *rsa;
1100
1981
        unsigned char *buf;
1101
 
        unsigned short size;
1102
1982
        int j;
1103
1983
 
1104
 
        if (!key_data)
1105
 
                return;
1106
 
 
1107
1984
        fd = open(key_name, O_RDONLY);
1108
1985
        if (fd < 0)
1109
1986
                return;
1110
1987
 
1111
 
        rsa = &key_data->rsa;
 
1988
        rsa = &key_data.rsa;
1112
1989
        if (read(fd, rsa, sizeof(struct RSA_data)) <= 0)
1113
1990
                goto Close;
1114
1991
 
1149
2026
                goto Destroy_key_set;
1150
2027
 
1151
2028
        size = KEY_SIZE + CIPHER_BLOCK;
1152
 
        if (read(rnd_fd, key_data->key, size) != size)
1153
 
                goto Close_urandom;
1154
 
 
1155
 
        gcry_mpi_scan(&mpi, GCRYMPI_FMT_USG, key_data->key, size, NULL);
 
2029
        for (;;) {
 
2030
                unsigned char *res;
 
2031
                size_t test;
 
2032
                int cmp;
 
2033
 
 
2034
                if (read(rnd_fd, key_data.key, size) != size)
 
2035
                        goto Close_urandom;
 
2036
 
 
2037
                gcry_mpi_scan(&mpi, GCRYMPI_FMT_USG, key_data.key, size, NULL);
 
2038
                gcry_mpi_aprint(GCRYMPI_FMT_USG, &res, &test, mpi);
 
2039
                cmp = memcmp(key_data.key, res, size);
 
2040
                gcry_free(res);
 
2041
                if (test == size && !cmp)
 
2042
                        break;
 
2043
                gcry_mpi_release(mpi);
 
2044
        }
1156
2045
        ret = gcry_ac_data_encrypt(rsa_hd, 0, rsa_pub, mpi, &key_set);
1157
2046
        gcry_mpi_release(mpi);
1158
2047
        if (!ret) {
1159
 
                struct encrypted_key *key = &key_data->encrypted_key;
 
2048
                struct encrypted_key *key = &key_data.encrypted_key;
1160
2049
                char *str;
1161
2050
                size_t s;
1162
2051
 
1203
2092
        struct sigaction sa;
1204
2093
        struct vt_mode vtm;
1205
2094
        struct vt_stat vtstat;
1206
 
        char *vt_name = mem_pool;
 
2095
        char vt_name[GENERIC_NAME_SIZE];
1207
2096
        int fd, error;
1208
2097
 
1209
2098
        fd = console_fd("/dev/console");
1356
2245
                strncpy(resume_dev_name, argv[optind], MAX_STR_LEN - 1);
1357
2246
 
1358
2247
#ifdef CONFIG_BOTH
 
2248
        s2ram_kms = !s2ram_check_kms();
 
2249
        if (s2ram_kms)
 
2250
                return 0;
 
2251
 
1359
2252
        s2ram = s2ram_is_supported();
1360
2253
        /* s2ram_is_supported returns EINVAL if there was something wrong
1361
2254
         * with the options that where added with s2ram_add_flag.
1375
2268
        unsigned int mem_size;
1376
2269
        struct stat stat_buf;
1377
2270
        int resume_fd, snapshot_fd, vt_fd, orig_vc = -1, suspend_vc = -1;
 
2271
        int test_fd = -1;
1378
2272
        dev_t resume_dev;
1379
2273
        int orig_loglevel, orig_swappiness, ret;
1380
2274
        struct rlimit rlim;
1388
2282
        do {
1389
2283
                ret = open("/dev/null", O_RDWR);
1390
2284
                if (ret < 0) {
1391
 
                        ret = errno;
1392
 
                        suspend_error("Could not open /dev/null.");
 
2285
                        perror(argv[0]);
1393
2286
                        return ret;
1394
2287
                }
1395
2288
        } while (ret < 3);
1424
2317
 
1425
2318
        if (!strcmp (shutdown_method_value, "shutdown")) {
1426
2319
                shutdown_method = SHUTDOWN_METHOD_SHUTDOWN;
1427
 
        }
1428
 
        else if (!strcmp (shutdown_method_value, "platform")) {
 
2320
        } else if (!strcmp (shutdown_method_value, "platform")) {
1429
2321
                shutdown_method = SHUTDOWN_METHOD_PLATFORM;
1430
 
        }
1431
 
        else if (!strcmp (shutdown_method_value, "reboot")) {
 
2322
        } else if (!strcmp (shutdown_method_value, "reboot")) {
1432
2323
                shutdown_method = SHUTDOWN_METHOD_REBOOT;
1433
2324
        }
1434
2325
 
1435
 
        page_size = getpagesize();
1436
 
        buffer_size = BUFFER_PAGES * page_size;
1437
 
 
1438
 
        mem_size = 3 * page_size + buffer_size;
 
2326
        if (resume_pause > RESUME_PAUSE_MAX)
 
2327
                resume_pause = RESUME_PAUSE_MAX;
 
2328
 
 
2329
        if (verify_image != 'y' && verify_image != 'Y')
 
2330
                verify_image = 0;
 
2331
 
 
2332
#ifdef CONFIG_THREADS
 
2333
        if (use_threads != 'y' && use_threads != 'Y')
 
2334
                use_threads = 0;
 
2335
#endif
 
2336
 
 
2337
        get_page_and_buffer_sizes();
 
2338
 
 
2339
        mem_size = 2 * page_size + buffer_size;
1439
2340
#ifdef CONFIG_COMPRESS
1440
 
        if (do_compress)
1441
 
                mem_size += LZO1X_1_MEM_COMPRESS;
 
2341
        if (do_compress) {
 
2342
                /*
 
2343
                 * The formula below follows from the worst-case expansion
 
2344
                 * calculation for LZO1 (size / 16 + 67) and the fact that the
 
2345
                 * size of the compressed data must be stored in the buffer
 
2346
                 * (sizeof(size_t)).
 
2347
                 */
 
2348
                compress_buf_size = buffer_size +
 
2349
                        round_up_page_size((buffer_size >> 4) + 67 +
 
2350
                                                sizeof(size_t));
 
2351
                mem_size += compress_buf_size +
 
2352
                                round_up_page_size(LZO1X_1_MEM_COMPRESS);
 
2353
        }
1442
2354
#endif
1443
2355
#ifdef CONFIG_ENCRYPT
1444
2356
        if (do_encrypt) {
1448
2360
                ret = gcry_cipher_open(&cipher_handle, IMAGE_CIPHER,
1449
2361
                                GCRY_CIPHER_MODE_CFB, GCRY_CIPHER_SECURE);
1450
2362
                if (ret) {
1451
 
                        fprintf(stderr, "%s: libgcrypt error %s\n", my_name, 
1452
 
                                gcry_strerror(ret));
 
2363
                        suspend_error("libgcrypt error %s", gcry_strerror(ret));
1453
2364
                        do_encrypt = 0;
1454
2365
                } else {
1455
 
                        mem_size += buffer_size;
 
2366
                        encrypt_buf_size = ENCRYPT_BUF_PAGES * page_size;
 
2367
                        mem_size += encrypt_buf_size;
1456
2368
                }
1457
2369
        }
1458
2370
#endif
1459
 
        mem_pool = malloc(mem_size);
1460
 
        if (!mem_pool) {
1461
 
                ret = errno;
 
2371
        if (use_threads) {
 
2372
                mem_size += (compress_buf_size > 0) ?
 
2373
                                (WRITE_BUFFERS - 1) * compress_buf_size :
 
2374
                                WRITE_BUFFERS * buffer_size;
 
2375
                if (!do_encrypt)
 
2376
                        mem_size += WRITE_BUFFERS * buffer_size;
 
2377
        }
 
2378
 
 
2379
        ret = init_memalloc(page_size, mem_size);
 
2380
        if (ret) {
1462
2381
                suspend_error("Could not allocate memory.");
1463
2382
                return ret;
1464
2383
        }
 
2384
 
1465
2385
#ifdef CONFIG_ENCRYPT
1466
 
        if (do_encrypt) {
1467
 
                mem_size -= buffer_size;
1468
 
                key_data = (struct key_data *)((char *)mem_pool + mem_size);
 
2386
        if (do_encrypt)
1469
2387
                generate_key();
1470
 
        }
1471
2388
#endif
1472
2389
 
1473
2390
        setvbuf(stdout, NULL, _IONBF, 0);
1479
2396
                return ret;
1480
2397
        }
1481
2398
 
 
2399
        if (strlen(test_file_name) > 0) {
 
2400
                if (stat(test_file_name, &stat_buf)) {
 
2401
                        ret = errno;
 
2402
                        suspend_error("Unable to stat test image file %s",
 
2403
                                        test_file_name);
 
2404
                        return ret;
 
2405
                }
 
2406
                test_image_size = round_down_page_size(stat_buf.st_size);
 
2407
                if (test_image_size < MIN_TEST_IMAGE_PAGES * page_size) {
 
2408
                        suspend_error("Test image file %s is too small",
 
2409
                                        test_file_name);
 
2410
                        return ENODATA;
 
2411
                }
 
2412
                test_fd = open(test_file_name, O_RDONLY);
 
2413
                if (test_fd < 0) {
 
2414
                        ret = errno;
 
2415
                        suspend_error("Unable to open test image file %s",
 
2416
                                        test_file_name);
 
2417
                        return ret;
 
2418
                }
 
2419
        }
 
2420
 
1482
2421
        snprintf(chroot_path, MAX_STR_LEN, "/proc/%d", getpid());
1483
2422
        if (mount("none", chroot_path, "tmpfs", 0, NULL)) {
1484
2423
                ret = errno;
1493
2432
                goto Umount;
1494
2433
        }
1495
2434
        if (!S_ISBLK(stat_buf.st_mode)) {
1496
 
                fprintf(stderr, "%s: Invalid resume device\n", my_name);
 
2435
                suspend_error("Invalid resume device.");
1497
2436
                ret = EINVAL;
1498
2437
                goto Umount;
1499
2438
        }
1523
2462
        }
1524
2463
 
1525
2464
        if (!S_ISCHR(stat_buf.st_mode)) {
1526
 
                fprintf(stderr, "%s: Invalid snapshot device\n", my_name);
 
2465
                suspend_error("Invalid snapshot device.");
1527
2466
                ret = EINVAL;
1528
2467
                goto Close_resume_fd;
1529
2468
        }
1568
2507
                s2ram = !s2ram_hacks();
1569
2508
#endif
1570
2509
#ifdef CONFIG_ENCRYPT
1571
 
        if (do_encrypt && ! use_RSA) {
1572
 
                splash.read_password((char *)mem_pool,1);
1573
 
                strncpy(password,(char *)mem_pool,PASS_SIZE);
1574
 
        }
 
2510
        if (do_encrypt && ! use_RSA)
 
2511
                splash.read_password(password, 1);
1575
2512
#endif
1576
2513
 
1577
2514
        open_printk();
1592
2529
        setrlimit(RLIMIT_NPROC, &rlim);
1593
2530
        setrlimit(RLIMIT_CORE, &rlim);
1594
2531
 
1595
 
        ret = suspend_system(snapshot_fd, resume_fd);
 
2532
        ret = suspend_system(snapshot_fd, resume_fd, test_fd);
1596
2533
 
1597
2534
        if (orig_loglevel >= 0)
1598
2535
                set_kernel_console_loglevel(orig_loglevel);
1618
2555
        } else {
1619
2556
                umount(chroot_path);
1620
2557
        }
 
2558
 
 
2559
        if (test_fd >= 0)
 
2560
                close(test_fd);
 
2561
 
1621
2562
#ifdef CONFIG_ENCRYPT
1622
2563
        if (do_encrypt)
1623
2564
                gcry_cipher_close(cipher_handle);
1624
2565
#endif
1625
 
        free(mem_pool);
 
2566
        free_memalloc();
1626
2567
 
1627
2568
        return ret;
1628
2569
}