2 * linux/kernel/power/swap.c
4 * This file provides functions for reading the suspend image from
5 * and writing it to a swap partition.
7 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@ucw.cz>
8 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
9 * Copyright (C) 2010 Bojan Smojver <bojan@rexursive.com>
11 * This file is released under the GPLv2.
15 #include <linux/module.h>
16 #include <linux/file.h>
17 #include <linux/delay.h>
18 #include <linux/bitops.h>
19 #include <linux/genhd.h>
20 #include <linux/device.h>
21 #include <linux/buffer_head.h>
22 #include <linux/bio.h>
23 #include <linux/blkdev.h>
24 #include <linux/swap.h>
25 #include <linux/swapops.h>
27 #include <linux/slab.h>
28 #include <linux/lzo.h>
29 #include <linux/vmalloc.h>
33 #define HIBERNATE_SIG "S1SUSPEND"
36 * The swap map is a data structure used for keeping track of each page
37 * written to a swap partition. It consists of many swap_map_page
38 * structures that contain each an array of MAP_PAGE_ENTRIES swap entries.
39 * These structures are stored on the swap and linked together with the
40 * help of the .next_swap member.
42 * The swap map is created during suspend. The swap map pages are
43 * allocated and populated one at a time, so we only need one memory
44 * page to set up the entire structure.
46 * During resume we also only need to use one swap_map_page structure
50 #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
52 struct swap_map_page
{
53 sector_t entries
[MAP_PAGE_ENTRIES
];
58 * The swap_map_handle structure is used for handling swap in
62 struct swap_map_handle
{
63 struct swap_map_page
*cur
;
65 sector_t first_sector
;
69 struct swsusp_header
{
70 char reserved
[PAGE_SIZE
- 20 - sizeof(sector_t
) - sizeof(int)];
72 unsigned int flags
; /* Flags to pass to the "boot" kernel */
75 } __attribute__((packed
));
77 static struct swsusp_header
*swsusp_header
;
80 * The following functions are used for tracing the allocated
81 * swap pages, so that they can be freed in case of an error.
84 struct swsusp_extent
{
90 static struct rb_root swsusp_extents
= RB_ROOT
;
92 static int swsusp_extents_insert(unsigned long swap_offset
)
94 struct rb_node
**new = &(swsusp_extents
.rb_node
);
95 struct rb_node
*parent
= NULL
;
96 struct swsusp_extent
*ext
;
98 /* Figure out where to put the new node */
100 ext
= container_of(*new, struct swsusp_extent
, node
);
102 if (swap_offset
< ext
->start
) {
104 if (swap_offset
== ext
->start
- 1) {
108 new = &((*new)->rb_left
);
109 } else if (swap_offset
> ext
->end
) {
111 if (swap_offset
== ext
->end
+ 1) {
115 new = &((*new)->rb_right
);
117 /* It already is in the tree */
121 /* Add the new node and rebalance the tree. */
122 ext
= kzalloc(sizeof(struct swsusp_extent
), GFP_KERNEL
);
126 ext
->start
= swap_offset
;
127 ext
->end
= swap_offset
;
128 rb_link_node(&ext
->node
, parent
, new);
129 rb_insert_color(&ext
->node
, &swsusp_extents
);
134 * alloc_swapdev_block - allocate a swap page and register that it has
135 * been allocated, so that it can be freed in case of an error.
138 sector_t
alloc_swapdev_block(int swap
)
140 unsigned long offset
;
142 offset
= swp_offset(get_swap_page_of_type(swap
));
144 if (swsusp_extents_insert(offset
))
145 swap_free(swp_entry(swap
, offset
));
147 return swapdev_block(swap
, offset
);
153 * free_all_swap_pages - free swap pages allocated for saving image data.
154 * It also frees the extents used to register which swap entries had been
158 void free_all_swap_pages(int swap
)
160 struct rb_node
*node
;
162 while ((node
= swsusp_extents
.rb_node
)) {
163 struct swsusp_extent
*ext
;
164 unsigned long offset
;
166 ext
= container_of(node
, struct swsusp_extent
, node
);
167 rb_erase(node
, &swsusp_extents
);
168 for (offset
= ext
->start
; offset
<= ext
->end
; offset
++)
169 swap_free(swp_entry(swap
, offset
));
175 int swsusp_swap_in_use(void)
177 return (swsusp_extents
.rb_node
!= NULL
);
184 static unsigned short root_swap
= 0xffff;
185 struct block_device
*hib_resume_bdev
;
191 static int mark_swapfiles(struct swap_map_handle
*handle
, unsigned int flags
)
195 hib_bio_read_page(swsusp_resume_block
, swsusp_header
, NULL
);
196 if (!memcmp("SWAP-SPACE",swsusp_header
->sig
, 10) ||
197 !memcmp("SWAPSPACE2",swsusp_header
->sig
, 10)) {
198 memcpy(swsusp_header
->orig_sig
,swsusp_header
->sig
, 10);
199 memcpy(swsusp_header
->sig
, HIBERNATE_SIG
, 10);
200 swsusp_header
->image
= handle
->first_sector
;
201 swsusp_header
->flags
= flags
;
202 error
= hib_bio_write_page(swsusp_resume_block
,
203 swsusp_header
, NULL
);
205 printk(KERN_ERR
"PM: Swap header not found!\n");
212 * swsusp_swap_check - check if the resume device is a swap device
213 * and get its index (if so)
215 * This is called before saving image
217 static int swsusp_swap_check(void)
221 res
= swap_type_of(swsusp_resume_device
, swsusp_resume_block
,
227 res
= blkdev_get(hib_resume_bdev
, FMODE_WRITE
, NULL
);
231 res
= set_blocksize(hib_resume_bdev
, PAGE_SIZE
);
233 blkdev_put(hib_resume_bdev
, FMODE_WRITE
);
239 * write_page - Write one page to given swap location.
240 * @buf: Address we're writing.
241 * @offset: Offset of the swap page we're writing to.
242 * @bio_chain: Link the next write BIO here
245 static int write_page(void *buf
, sector_t offset
, struct bio
**bio_chain
)
253 src
= (void *)__get_free_page(__GFP_WAIT
| __GFP_HIGH
);
258 bio_chain
= NULL
; /* Go synchronous */
264 return hib_bio_write_page(offset
, src
, bio_chain
);
267 static void release_swap_writer(struct swap_map_handle
*handle
)
270 free_page((unsigned long)handle
->cur
);
274 static int get_swap_writer(struct swap_map_handle
*handle
)
278 ret
= swsusp_swap_check();
281 printk(KERN_ERR
"PM: Cannot find swap device, try "
285 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(GFP_KERNEL
);
290 handle
->cur_swap
= alloc_swapdev_block(root_swap
);
291 if (!handle
->cur_swap
) {
296 handle
->first_sector
= handle
->cur_swap
;
299 release_swap_writer(handle
);
301 swsusp_close(FMODE_WRITE
);
305 static int swap_write_page(struct swap_map_handle
*handle
, void *buf
,
306 struct bio
**bio_chain
)
313 offset
= alloc_swapdev_block(root_swap
);
314 error
= write_page(buf
, offset
, bio_chain
);
317 handle
->cur
->entries
[handle
->k
++] = offset
;
318 if (handle
->k
>= MAP_PAGE_ENTRIES
) {
319 error
= hib_wait_on_bio_chain(bio_chain
);
322 offset
= alloc_swapdev_block(root_swap
);
325 handle
->cur
->next_swap
= offset
;
326 error
= write_page(handle
->cur
, handle
->cur_swap
, NULL
);
329 clear_page(handle
->cur
);
330 handle
->cur_swap
= offset
;
337 static int flush_swap_writer(struct swap_map_handle
*handle
)
339 if (handle
->cur
&& handle
->cur_swap
)
340 return write_page(handle
->cur
, handle
->cur_swap
, NULL
);
345 static int swap_writer_finish(struct swap_map_handle
*handle
,
346 unsigned int flags
, int error
)
349 flush_swap_writer(handle
);
350 printk(KERN_INFO
"PM: S");
351 error
= mark_swapfiles(handle
, flags
);
356 free_all_swap_pages(root_swap
);
357 release_swap_writer(handle
);
358 swsusp_close(FMODE_WRITE
);
363 /* We need to remember how much compressed data we need to read. */
364 #define LZO_HEADER sizeof(size_t)
366 /* Number of pages/bytes we'll compress at one time. */
367 #define LZO_UNC_PAGES 32
368 #define LZO_UNC_SIZE (LZO_UNC_PAGES * PAGE_SIZE)
370 /* Number of pages/bytes we need for compressed data (worst case). */
371 #define LZO_CMP_PAGES DIV_ROUND_UP(lzo1x_worst_compress(LZO_UNC_SIZE) + \
372 LZO_HEADER, PAGE_SIZE)
373 #define LZO_CMP_SIZE (LZO_CMP_PAGES * PAGE_SIZE)
376 * save_image - save the suspend image data
379 static int save_image(struct swap_map_handle
*handle
,
380 struct snapshot_handle
*snapshot
,
381 unsigned int nr_to_write
)
388 struct timeval start
;
391 printk(KERN_INFO
"PM: Saving image data pages (%u pages) ... ",
393 m
= nr_to_write
/ 100;
398 do_gettimeofday(&start
);
400 ret
= snapshot_read_next(snapshot
);
403 ret
= swap_write_page(handle
, data_of(*snapshot
), &bio
);
407 printk(KERN_CONT
"\b\b\b\b%3d%%", nr_pages
/ m
);
410 err2
= hib_wait_on_bio_chain(&bio
);
411 do_gettimeofday(&stop
);
415 printk(KERN_CONT
"\b\b\b\bdone\n");
417 printk(KERN_CONT
"\n");
418 swsusp_show_speed(&start
, &stop
, nr_to_write
, "Wrote");
424 * save_image_lzo - Save the suspend image data compressed with LZO.
425 * @handle: Swap mam handle to use for saving the image.
426 * @snapshot: Image to read data from.
427 * @nr_to_write: Number of pages to save.
429 static int save_image_lzo(struct swap_map_handle
*handle
,
430 struct snapshot_handle
*snapshot
,
431 unsigned int nr_to_write
)
438 struct timeval start
;
440 size_t off
, unc_len
, cmp_len
;
441 unsigned char *unc
, *cmp
, *wrk
, *page
;
443 page
= (void *)__get_free_page(__GFP_WAIT
| __GFP_HIGH
);
445 printk(KERN_ERR
"PM: Failed to allocate LZO page\n");
449 wrk
= vmalloc(LZO1X_1_MEM_COMPRESS
);
451 printk(KERN_ERR
"PM: Failed to allocate LZO workspace\n");
452 free_page((unsigned long)page
);
456 unc
= vmalloc(LZO_UNC_SIZE
);
458 printk(KERN_ERR
"PM: Failed to allocate LZO uncompressed\n");
460 free_page((unsigned long)page
);
464 cmp
= vmalloc(LZO_CMP_SIZE
);
466 printk(KERN_ERR
"PM: Failed to allocate LZO compressed\n");
469 free_page((unsigned long)page
);
474 "PM: Compressing and saving image data (%u pages) ... ",
476 m
= nr_to_write
/ 100;
481 do_gettimeofday(&start
);
483 for (off
= 0; off
< LZO_UNC_SIZE
; off
+= PAGE_SIZE
) {
484 ret
= snapshot_read_next(snapshot
);
491 memcpy(unc
+ off
, data_of(*snapshot
), PAGE_SIZE
);
494 printk(KERN_CONT
"\b\b\b\b%3d%%", nr_pages
/ m
);
502 ret
= lzo1x_1_compress(unc
, unc_len
,
503 cmp
+ LZO_HEADER
, &cmp_len
, wrk
);
505 printk(KERN_ERR
"PM: LZO compression failed\n");
509 if (unlikely(!cmp_len
||
510 cmp_len
> lzo1x_worst_compress(unc_len
))) {
511 printk(KERN_ERR
"PM: Invalid LZO compressed length\n");
516 *(size_t *)cmp
= cmp_len
;
519 * Given we are writing one page at a time to disk, we copy
520 * that much from the buffer, although the last bit will likely
521 * be smaller than full page. This is OK - we saved the length
522 * of the compressed data, so any garbage at the end will be
523 * discarded when we read it.
525 for (off
= 0; off
< LZO_HEADER
+ cmp_len
; off
+= PAGE_SIZE
) {
526 memcpy(page
, cmp
+ off
, PAGE_SIZE
);
528 ret
= swap_write_page(handle
, page
, &bio
);
535 err2
= hib_wait_on_bio_chain(&bio
);
536 do_gettimeofday(&stop
);
540 printk(KERN_CONT
"\b\b\b\bdone\n");
542 printk(KERN_CONT
"\n");
543 swsusp_show_speed(&start
, &stop
, nr_to_write
, "Wrote");
548 free_page((unsigned long)page
);
554 * enough_swap - Make sure we have enough swap to save the image.
556 * Returns TRUE or FALSE after checking the total amount of swap
557 * space avaiable from the resume partition.
560 static int enough_swap(unsigned int nr_pages
, unsigned int flags
)
562 unsigned int free_swap
= count_swap_pages(root_swap
, 1);
563 unsigned int required
;
565 pr_debug("PM: Free swap pages: %u\n", free_swap
);
567 required
= PAGES_FOR_IO
+ ((flags
& SF_NOCOMPRESS_MODE
) ?
568 nr_pages
: (nr_pages
* LZO_CMP_PAGES
) / LZO_UNC_PAGES
+ 1);
569 return free_swap
> required
;
573 * swsusp_write - Write entire image and metadata.
574 * @flags: flags to pass to the "boot" kernel in the image header
576 * It is important _NOT_ to umount filesystems at this point. We want
577 * them synced (in case something goes wrong) but we DO not want to mark
578 * filesystem clean: it is not. (And it does not matter, if we resume
579 * correctly, we'll mark system clean, anyway.)
582 int swsusp_write(unsigned int flags
)
584 struct swap_map_handle handle
;
585 struct snapshot_handle snapshot
;
586 struct swsusp_info
*header
;
590 pages
= snapshot_get_image_size();
591 error
= get_swap_writer(&handle
);
593 printk(KERN_ERR
"PM: Cannot get swap writer\n");
596 if (!enough_swap(pages
, flags
)) {
597 printk(KERN_ERR
"PM: Not enough free swap\n");
601 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
602 error
= snapshot_read_next(&snapshot
);
603 if (error
< PAGE_SIZE
) {
609 header
= (struct swsusp_info
*)data_of(snapshot
);
610 error
= swap_write_page(&handle
, header
, NULL
);
612 error
= (flags
& SF_NOCOMPRESS_MODE
) ?
613 save_image(&handle
, &snapshot
, pages
- 1) :
614 save_image_lzo(&handle
, &snapshot
, pages
- 1);
617 error
= swap_writer_finish(&handle
, flags
, error
);
622 * The following functions allow us to read data using a swap map
623 * in a file-alike way
626 static void release_swap_reader(struct swap_map_handle
*handle
)
629 free_page((unsigned long)handle
->cur
);
633 static int get_swap_reader(struct swap_map_handle
*handle
,
634 unsigned int *flags_p
)
638 *flags_p
= swsusp_header
->flags
;
640 if (!swsusp_header
->image
) /* how can this happen? */
643 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(__GFP_WAIT
| __GFP_HIGH
);
647 error
= hib_bio_read_page(swsusp_header
->image
, handle
->cur
, NULL
);
649 release_swap_reader(handle
);
656 static int swap_read_page(struct swap_map_handle
*handle
, void *buf
,
657 struct bio
**bio_chain
)
664 offset
= handle
->cur
->entries
[handle
->k
];
667 error
= hib_bio_read_page(offset
, buf
, bio_chain
);
670 if (++handle
->k
>= MAP_PAGE_ENTRIES
) {
671 error
= hib_wait_on_bio_chain(bio_chain
);
673 offset
= handle
->cur
->next_swap
;
675 release_swap_reader(handle
);
677 error
= hib_bio_read_page(offset
, handle
->cur
, NULL
);
682 static int swap_reader_finish(struct swap_map_handle
*handle
)
684 release_swap_reader(handle
);
690 * load_image - load the image using the swap map handle
691 * @handle and the snapshot handle @snapshot
692 * (assume there are @nr_pages pages to load)
695 static int load_image(struct swap_map_handle
*handle
,
696 struct snapshot_handle
*snapshot
,
697 unsigned int nr_to_read
)
701 struct timeval start
;
707 printk(KERN_INFO
"PM: Loading image data pages (%u pages) ... ",
709 m
= nr_to_read
/ 100;
714 do_gettimeofday(&start
);
716 error
= snapshot_write_next(snapshot
);
719 error
= swap_read_page(handle
, data_of(*snapshot
), &bio
);
722 if (snapshot
->sync_read
)
723 error
= hib_wait_on_bio_chain(&bio
);
727 printk("\b\b\b\b%3d%%", nr_pages
/ m
);
730 err2
= hib_wait_on_bio_chain(&bio
);
731 do_gettimeofday(&stop
);
735 printk("\b\b\b\bdone\n");
736 snapshot_write_finalize(snapshot
);
737 if (!snapshot_image_loaded(snapshot
))
741 swsusp_show_speed(&start
, &stop
, nr_to_read
, "Read");
746 * load_image_lzo - Load compressed image data and decompress them with LZO.
747 * @handle: Swap map handle to use for loading data.
748 * @snapshot: Image to copy uncompressed data into.
749 * @nr_to_read: Number of pages to load.
751 static int load_image_lzo(struct swap_map_handle
*handle
,
752 struct snapshot_handle
*snapshot
,
753 unsigned int nr_to_read
)
758 struct timeval start
;
761 size_t i
, off
, unc_len
, cmp_len
;
762 unsigned char *unc
, *cmp
, *page
[LZO_CMP_PAGES
];
764 for (i
= 0; i
< LZO_CMP_PAGES
; i
++) {
765 page
[i
] = (void *)__get_free_page(__GFP_WAIT
| __GFP_HIGH
);
767 printk(KERN_ERR
"PM: Failed to allocate LZO page\n");
770 free_page((unsigned long)page
[--i
]);
776 unc
= vmalloc(LZO_UNC_SIZE
);
778 printk(KERN_ERR
"PM: Failed to allocate LZO uncompressed\n");
780 for (i
= 0; i
< LZO_CMP_PAGES
; i
++)
781 free_page((unsigned long)page
[i
]);
786 cmp
= vmalloc(LZO_CMP_SIZE
);
788 printk(KERN_ERR
"PM: Failed to allocate LZO compressed\n");
791 for (i
= 0; i
< LZO_CMP_PAGES
; i
++)
792 free_page((unsigned long)page
[i
]);
798 "PM: Loading and decompressing image data (%u pages) ... ",
800 m
= nr_to_read
/ 100;
805 do_gettimeofday(&start
);
807 error
= snapshot_write_next(snapshot
);
812 error
= swap_read_page(handle
, page
[0], NULL
); /* sync */
816 cmp_len
= *(size_t *)page
[0];
817 if (unlikely(!cmp_len
||
818 cmp_len
> lzo1x_worst_compress(LZO_UNC_SIZE
))) {
819 printk(KERN_ERR
"PM: Invalid LZO compressed length\n");
824 for (off
= PAGE_SIZE
, i
= 1;
825 off
< LZO_HEADER
+ cmp_len
; off
+= PAGE_SIZE
, i
++) {
826 error
= swap_read_page(handle
, page
[i
], &bio
);
831 error
= hib_wait_on_bio_chain(&bio
); /* need all data now */
836 off
< LZO_HEADER
+ cmp_len
; off
+= PAGE_SIZE
, i
++) {
837 memcpy(cmp
+ off
, page
[i
], PAGE_SIZE
);
840 unc_len
= LZO_UNC_SIZE
;
841 error
= lzo1x_decompress_safe(cmp
+ LZO_HEADER
, cmp_len
,
844 printk(KERN_ERR
"PM: LZO decompression failed\n");
848 if (unlikely(!unc_len
||
849 unc_len
> LZO_UNC_SIZE
||
850 unc_len
& (PAGE_SIZE
- 1))) {
851 printk(KERN_ERR
"PM: Invalid LZO uncompressed length\n");
856 for (off
= 0; off
< unc_len
; off
+= PAGE_SIZE
) {
857 memcpy(data_of(*snapshot
), unc
+ off
, PAGE_SIZE
);
860 printk("\b\b\b\b%3d%%", nr_pages
/ m
);
863 error
= snapshot_write_next(snapshot
);
870 do_gettimeofday(&stop
);
872 printk("\b\b\b\bdone\n");
873 snapshot_write_finalize(snapshot
);
874 if (!snapshot_image_loaded(snapshot
))
878 swsusp_show_speed(&start
, &stop
, nr_to_read
, "Read");
882 for (i
= 0; i
< LZO_CMP_PAGES
; i
++)
883 free_page((unsigned long)page
[i
]);
889 * swsusp_read - read the hibernation image.
890 * @flags_p: flags passed by the "frozen" kernel in the image header should
891 * be written into this memory location
894 int swsusp_read(unsigned int *flags_p
)
897 struct swap_map_handle handle
;
898 struct snapshot_handle snapshot
;
899 struct swsusp_info
*header
;
901 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
902 error
= snapshot_write_next(&snapshot
);
903 if (error
< PAGE_SIZE
)
904 return error
< 0 ? error
: -EFAULT
;
905 header
= (struct swsusp_info
*)data_of(snapshot
);
906 error
= get_swap_reader(&handle
, flags_p
);
910 error
= swap_read_page(&handle
, header
, NULL
);
912 error
= (*flags_p
& SF_NOCOMPRESS_MODE
) ?
913 load_image(&handle
, &snapshot
, header
->pages
- 1) :
914 load_image_lzo(&handle
, &snapshot
, header
->pages
- 1);
916 swap_reader_finish(&handle
);
919 pr_debug("PM: Image successfully loaded\n");
921 pr_debug("PM: Error %d resuming\n", error
);
926 * swsusp_check - Check for swsusp signature in the resume device
929 int swsusp_check(void)
933 hib_resume_bdev
= blkdev_get_by_dev(swsusp_resume_device
,
935 if (!IS_ERR(hib_resume_bdev
)) {
936 set_blocksize(hib_resume_bdev
, PAGE_SIZE
);
937 clear_page(swsusp_header
);
938 error
= hib_bio_read_page(swsusp_resume_block
,
939 swsusp_header
, NULL
);
943 if (!memcmp(HIBERNATE_SIG
, swsusp_header
->sig
, 10)) {
944 memcpy(swsusp_header
->sig
, swsusp_header
->orig_sig
, 10);
945 /* Reset swap signature now */
946 error
= hib_bio_write_page(swsusp_resume_block
,
947 swsusp_header
, NULL
);
954 blkdev_put(hib_resume_bdev
, FMODE_READ
);
956 pr_debug("PM: Image signature found, resuming\n");
958 error
= PTR_ERR(hib_resume_bdev
);
962 pr_debug("PM: Image not found (code %d)\n", error
);
968 * swsusp_close - close swap device.
971 void swsusp_close(fmode_t mode
)
973 if (IS_ERR(hib_resume_bdev
)) {
974 pr_debug("PM: Image device not initialised\n");
978 blkdev_put(hib_resume_bdev
, mode
);
981 static int swsusp_header_init(void)
983 swsusp_header
= (struct swsusp_header
*) __get_free_page(GFP_KERNEL
);
985 panic("Could not allocate memory for swsusp_header\n");
989 core_initcall(swsusp_header_init
);