15 #include <linux/module.h>
18 #include <linux/bitops.h>
20 #include <linux/device.h>
26 #include <linux/slab.h>
36 #define HIBERNATE_SIG "S1SUSPEND"
52 #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
57 static inline unsigned long low_free_pages(
void)
59 return nr_free_pages() - nr_free_highpages();
66 static inline unsigned long reqd_free_pages(
void)
68 return low_free_pages() / 2;
121 static int swsusp_extents_insert(
unsigned long swap_offset)
131 if (swap_offset < ext->
start) {
133 if (swap_offset == ext->
start - 1) {
137 new = &((*new)->rb_left);
138 }
else if (swap_offset > ext->
end) {
140 if (swap_offset == ext->
end + 1) {
144 new = &((*new)->rb_right);
155 ext->
start = swap_offset;
156 ext->
end = swap_offset;
157 rb_link_node(&ext->
node, parent,
new);
173 if (swsusp_extents_insert(offset))
176 return swapdev_block(swap, offset);
191 while ((node = swsusp_extents.
rb_node)) {
213 static unsigned short root_swap = 0xffff;
225 if (!
memcmp(
"SWAP-SPACE",swsusp_header->
sig, 10) ||
226 !
memcmp(
"SWAPSPACE2",swsusp_header->
sig, 10)) {
234 swsusp_header,
NULL);
248 static int swsusp_swap_check(
void)
321 ret = swsusp_swap_check();
343 release_swap_writer(handle);
350 struct bio **bio_chain)
358 error = write_page(buf, offset, bio_chain);
367 error = write_page(handle->
cur, handle->
cur_swap, bio_chain);
398 unsigned int flags,
int error)
401 flush_swap_writer(handle);
403 error = mark_swapfiles(handle, flags);
409 release_swap_writer(handle);
416 #define LZO_HEADER sizeof(size_t)
419 #define LZO_UNC_PAGES 32
420 #define LZO_UNC_SIZE (LZO_UNC_PAGES * PAGE_SIZE)
423 #define LZO_CMP_PAGES DIV_ROUND_UP(lzo1x_worst_compress(LZO_UNC_SIZE) + \
424 LZO_HEADER, PAGE_SIZE)
425 #define LZO_CMP_SIZE (LZO_CMP_PAGES * PAGE_SIZE)
428 #define LZO_THREADS 3
431 #define LZO_MIN_RD_PAGES 1024
432 #define LZO_MAX_RD_PAGES 8192
441 unsigned int nr_to_write)
453 m = nr_to_write / 10;
463 ret = swap_write_page(handle,
data_of(*snapshot), &bio);
499 static int crc32_threadfn(
void *
data)
543 static int lzo_compress_threadfn(
void *
data)
576 unsigned int nr_to_write)
605 data =
vmalloc(
sizeof(*data) * nr_threads);
631 "image_compress/%u", thr);
632 if (IS_ERR(data[thr].thr)) {
635 "PM: Cannot start compression threads\n");
655 if (IS_ERR(crc->
thr)) {
669 "PM: Using %u thread(s) for compression.\n"
670 "PM: Compressing and saving image data (%u pages)...\n",
671 nr_threads, nr_to_write);
672 m = nr_to_write / 10;
693 "PM: Image saving progress: "
714 for (run_threads = thr, thr = 0; thr <
run_threads; thr++) {
730 "PM: Invalid LZO compressed length\n");
735 *(
size_t *)data[thr].cmp = data[thr].cmp_len;
750 ret = swap_write_page(handle, page, &bio);
780 if (page)
free_page((
unsigned long)page);
792 static int enough_swap(
unsigned int nr_pages,
unsigned int flags)
794 unsigned int free_swap = count_swap_pages(root_swap, 1);
795 unsigned int required;
797 pr_debug(
"PM: Free swap pages: %u\n", free_swap);
799 required = PAGES_FOR_IO + nr_pages;
800 return free_swap > required;
822 error = get_swap_writer(&handle);
828 if (!enough_swap(pages, flags)) {
843 error = swap_write_page(&handle, header,
NULL);
846 save_image(&handle, &snapshot, pages - 1) :
847 save_image_lzo(&handle, &snapshot, pages - 1);
850 error = swap_writer_finish(&handle, flags, error);
863 while (handle->
maps) {
864 if (handle->
maps->map)
874 unsigned int *flags_p)
880 *flags_p = swsusp_header->
flags;
882 if (!swsusp_header->
image)
887 offset = swsusp_header->
image;
891 release_swap_reader(handle);
894 memset(tmp, 0,
sizeof(*tmp));
904 release_swap_reader(handle);
910 release_swap_reader(handle);
913 offset = tmp->
map->next_swap;
916 handle->
cur = handle->
maps->map;
921 struct bio **bio_chain)
929 offset = handle->
cur->entries[handle->
k];
942 release_swap_reader(handle);
944 handle->
cur = handle->
maps->map;
951 release_swap_reader(handle);
964 unsigned int nr_to_read)
986 ret = swap_read_page(handle,
data_of(*snapshot), &bio);
1031 static int lzo_decompress_threadfn(
void *data)
1064 unsigned int nr_to_read)
1076 have = 0, want, need, asked = 0;
1077 unsigned long read_pages = 0;
1078 unsigned char **page =
NULL;
1096 data =
vmalloc(
sizeof(*data) * nr_threads);
1122 "image_decompress/%u", thr);
1123 if (IS_ERR(data[thr].thr)) {
1126 "PM: Cannot start decompression threads\n");
1146 if (IS_ERR(crc->
thr)) {
1164 for (i = 0; i < read_pages; i++) {
1174 "PM: Failed to allocate LZO pages\n");
1185 "PM: Using %u thread(s) for decompression.\n"
1186 "PM: Loading and decompressing image data (%u pages)...\n",
1187 nr_threads, nr_to_read);
1188 m = nr_to_read / 10;
1200 for (i = 0; !eof && i < want; i++) {
1201 ret = swap_read_page(handle, page[ring], &bio);
1208 handle->
cur->entries[handle->
k]) {
1249 "PM: Invalid LZO compressed length\n");
1267 memcpy(data[thr].cmp + off,
1292 for (run_threads = thr, thr = 0; thr <
run_threads; thr++) {
1301 "PM: LZO decompression failed\n");
1306 data[thr].unc_len > LZO_UNC_SIZE ||
1309 "PM: Invalid LZO uncompressed length\n");
1319 if (!(nr_pages % m))
1321 "PM: Image loading progress: "
1356 "PM: Invalid image CRC32!\n");
1377 if (page)
vfree(page);
1398 return error < 0 ? error : -
EFAULT;
1400 error = get_swap_reader(&handle, flags_p);
1404 error = swap_read_page(&handle, header,
NULL);
1407 load_image(&handle, &snapshot, header->
pages - 1) :
1408 load_image_lzo(&handle, &snapshot, header->
pages - 1);
1410 swap_reader_finish(&handle);
1413 pr_debug(
"PM: Image successfully loaded\n");
1415 pr_debug(
"PM: Error %d resuming\n", error);
1429 if (!IS_ERR(hib_resume_bdev)) {
1433 swsusp_header,
NULL);
1441 swsusp_header,
NULL);
1450 pr_debug(
"PM: Image signature found, resuming\n");
1452 error = PTR_ERR(hib_resume_bdev);
1456 pr_debug(
"PM: Image not found (code %d)\n", error);
1467 if (IS_ERR(hib_resume_bdev)) {
1468 pr_debug(
"PM: Image device not initialised\n");
1479 #ifdef CONFIG_SUSPEND
1480 int swsusp_unmark(
void)
1488 swsusp_header,
NULL);
1503 static int swsusp_header_init(
void)
1507 panic(
"Could not allocate memory for swsusp_header\n");