xref: /openbmc/linux/kernel/power/swap.c (revision 3aef83e0ef1ffb8ea3bea97be46821a45c952173)
1 /*
2  * linux/kernel/power/swap.c
3  *
4  * This file provides functions for reading the suspend image from
5  * and writing it to a swap partition.
6  *
7  * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
8  * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
9  *
10  * This file is released under the GPLv2.
11  *
12  */
13 
14 #include <linux/module.h>
15 #include <linux/smp_lock.h>
16 #include <linux/file.h>
17 #include <linux/utsname.h>
18 #include <linux/version.h>
19 #include <linux/delay.h>
20 #include <linux/bitops.h>
21 #include <linux/genhd.h>
22 #include <linux/device.h>
23 #include <linux/buffer_head.h>
24 #include <linux/bio.h>
25 #include <linux/blkdev.h>
26 #include <linux/swap.h>
27 #include <linux/swapops.h>
28 #include <linux/pm.h>
29 
30 #include "power.h"
31 
32 extern char resume_file[];
33 
34 #define SWSUSP_SIG	"S1SUSPEND"
35 
36 static struct swsusp_header {
37 	char reserved[PAGE_SIZE - 20 - sizeof(sector_t)];
38 	sector_t image;
39 	char	orig_sig[10];
40 	char	sig[10];
41 } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
42 
43 /*
44  * General things
45  */
46 
47 static unsigned short root_swap = 0xffff;
48 static struct block_device *resume_bdev;
49 
50 /**
51  *	submit - submit BIO request.
52  *	@rw:	READ or WRITE.
53  *	@off	physical offset of page.
54  *	@page:	page we're reading or writing.
55  *	@bio_chain: list of pending biod (for async reading)
56  *
57  *	Straight from the textbook - allocate and initialize the bio.
58  *	If we're reading, make sure the page is marked as dirty.
59  *	Then submit it and, if @bio_chain == NULL, wait.
60  */
61 static int submit(int rw, pgoff_t page_off, struct page *page,
62 			struct bio **bio_chain)
63 {
64 	struct bio *bio;
65 
66 	bio = bio_alloc(GFP_ATOMIC, 1);
67 	if (!bio)
68 		return -ENOMEM;
69 	bio->bi_sector = page_off * (PAGE_SIZE >> 9);
70 	bio->bi_bdev = resume_bdev;
71 	bio->bi_end_io = end_swap_bio_read;
72 
73 	if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
74 		printk("swsusp: ERROR: adding page to bio at %ld\n", page_off);
75 		bio_put(bio);
76 		return -EFAULT;
77 	}
78 
79 	lock_page(page);
80 	bio_get(bio);
81 
82 	if (bio_chain == NULL) {
83 		submit_bio(rw | (1 << BIO_RW_SYNC), bio);
84 		wait_on_page_locked(page);
85 		if (rw == READ)
86 			bio_set_pages_dirty(bio);
87 		bio_put(bio);
88 	} else {
89 		if (rw == READ)
90 			get_page(page);	/* These pages are freed later */
91 		bio->bi_private = *bio_chain;
92 		*bio_chain = bio;
93 		submit_bio(rw | (1 << BIO_RW_SYNC), bio);
94 	}
95 	return 0;
96 }
97 
98 static int bio_read_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
99 {
100 	return submit(READ, page_off, virt_to_page(addr), bio_chain);
101 }
102 
103 static int bio_write_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
104 {
105 	return submit(WRITE, page_off, virt_to_page(addr), bio_chain);
106 }
107 
108 static int wait_on_bio_chain(struct bio **bio_chain)
109 {
110 	struct bio *bio;
111 	struct bio *next_bio;
112 	int ret = 0;
113 
114 	if (bio_chain == NULL)
115 		return 0;
116 
117 	bio = *bio_chain;
118 	if (bio == NULL)
119 		return 0;
120 	while (bio) {
121 		struct page *page;
122 
123 		next_bio = bio->bi_private;
124 		page = bio->bi_io_vec[0].bv_page;
125 		wait_on_page_locked(page);
126 		if (!PageUptodate(page) || PageError(page))
127 			ret = -EIO;
128 		put_page(page);
129 		bio_put(bio);
130 		bio = next_bio;
131 	}
132 	*bio_chain = NULL;
133 	return ret;
134 }
135 
136 static void show_speed(struct timeval *start, struct timeval *stop,
137 			unsigned nr_pages, char *msg)
138 {
139 	s64 elapsed_centisecs64;
140 	int centisecs;
141 	int k;
142 	int kps;
143 
144 	elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
145 	do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
146 	centisecs = elapsed_centisecs64;
147 	if (centisecs == 0)
148 		centisecs = 1;	/* avoid div-by-zero */
149 	k = nr_pages * (PAGE_SIZE / 1024);
150 	kps = (k * 100) / centisecs;
151 	printk("%s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n", msg, k,
152 			centisecs / 100, centisecs % 100,
153 			kps / 1000, (kps % 1000) / 10);
154 }
155 
156 /*
157  * Saving part
158  */
159 
160 static int mark_swapfiles(sector_t start)
161 {
162 	int error;
163 
164 	bio_read_page(0, &swsusp_header, NULL);
165 	if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
166 	    !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
167 		memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
168 		memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
169 		swsusp_header.image = start;
170 		error = bio_write_page(0, &swsusp_header, NULL);
171 	} else {
172 		printk(KERN_ERR "swsusp: Swap header not found!\n");
173 		error = -ENODEV;
174 	}
175 	return error;
176 }
177 
178 /**
179  *	swsusp_swap_check - check if the resume device is a swap device
180  *	and get its index (if so)
181  */
182 
183 static int swsusp_swap_check(void) /* This is called before saving image */
184 {
185 	int res;
186 
187 	res = swap_type_of(swsusp_resume_device, 0);
188 	if (res < 0)
189 		return res;
190 
191 	root_swap = res;
192 	resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_WRITE);
193 	if (IS_ERR(resume_bdev))
194 		return PTR_ERR(resume_bdev);
195 
196 	res = set_blocksize(resume_bdev, PAGE_SIZE);
197 	if (res < 0)
198 		blkdev_put(resume_bdev);
199 
200 	return res;
201 }
202 
203 /**
204  *	write_page - Write one page to given swap location.
205  *	@buf:		Address we're writing.
206  *	@offset:	Offset of the swap page we're writing to.
207  *	@bio_chain:	Link the next write BIO here
208  */
209 
210 static int write_page(void *buf, sector_t offset, struct bio **bio_chain)
211 {
212 	void *src;
213 
214 	if (!offset)
215 		return -ENOSPC;
216 
217 	if (bio_chain) {
218 		src = (void *)__get_free_page(GFP_ATOMIC);
219 		if (src) {
220 			memcpy(src, buf, PAGE_SIZE);
221 		} else {
222 			WARN_ON_ONCE(1);
223 			bio_chain = NULL;	/* Go synchronous */
224 			src = buf;
225 		}
226 	} else {
227 		src = buf;
228 	}
229 	return bio_write_page(offset, src, bio_chain);
230 }
231 
232 /*
233  *	The swap map is a data structure used for keeping track of each page
234  *	written to a swap partition.  It consists of many swap_map_page
235  *	structures that contain each an array of MAP_PAGE_SIZE swap entries.
236  *	These structures are stored on the swap and linked together with the
237  *	help of the .next_swap member.
238  *
239  *	The swap map is created during suspend.  The swap map pages are
240  *	allocated and populated one at a time, so we only need one memory
241  *	page to set up the entire structure.
242  *
243  *	During resume we also only need to use one swap_map_page structure
244  *	at a time.
245  */
246 
247 #define MAP_PAGE_ENTRIES	(PAGE_SIZE / sizeof(sector_t) - 1)
248 
249 struct swap_map_page {
250 	sector_t entries[MAP_PAGE_ENTRIES];
251 	sector_t next_swap;
252 };
253 
254 /**
255  *	The swap_map_handle structure is used for handling swap in
256  *	a file-alike way
257  */
258 
259 struct swap_map_handle {
260 	struct swap_map_page *cur;
261 	sector_t cur_swap;
262 	struct bitmap_page *bitmap;
263 	unsigned int k;
264 };
265 
266 static void release_swap_writer(struct swap_map_handle *handle)
267 {
268 	if (handle->cur)
269 		free_page((unsigned long)handle->cur);
270 	handle->cur = NULL;
271 	if (handle->bitmap)
272 		free_bitmap(handle->bitmap);
273 	handle->bitmap = NULL;
274 }
275 
276 static int get_swap_writer(struct swap_map_handle *handle)
277 {
278 	handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
279 	if (!handle->cur)
280 		return -ENOMEM;
281 	handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0));
282 	if (!handle->bitmap) {
283 		release_swap_writer(handle);
284 		return -ENOMEM;
285 	}
286 	handle->cur_swap = alloc_swapdev_block(root_swap, handle->bitmap);
287 	if (!handle->cur_swap) {
288 		release_swap_writer(handle);
289 		return -ENOSPC;
290 	}
291 	handle->k = 0;
292 	return 0;
293 }
294 
295 static int swap_write_page(struct swap_map_handle *handle, void *buf,
296 				struct bio **bio_chain)
297 {
298 	int error = 0;
299 	sector_t offset;
300 
301 	if (!handle->cur)
302 		return -EINVAL;
303 	offset = alloc_swapdev_block(root_swap, handle->bitmap);
304 	error = write_page(buf, offset, bio_chain);
305 	if (error)
306 		return error;
307 	handle->cur->entries[handle->k++] = offset;
308 	if (handle->k >= MAP_PAGE_ENTRIES) {
309 		error = wait_on_bio_chain(bio_chain);
310 		if (error)
311 			goto out;
312 		offset = alloc_swapdev_block(root_swap, handle->bitmap);
313 		if (!offset)
314 			return -ENOSPC;
315 		handle->cur->next_swap = offset;
316 		error = write_page(handle->cur, handle->cur_swap, NULL);
317 		if (error)
318 			goto out;
319 		memset(handle->cur, 0, PAGE_SIZE);
320 		handle->cur_swap = offset;
321 		handle->k = 0;
322 	}
323 out:
324 	return error;
325 }
326 
327 static int flush_swap_writer(struct swap_map_handle *handle)
328 {
329 	if (handle->cur && handle->cur_swap)
330 		return write_page(handle->cur, handle->cur_swap, NULL);
331 	else
332 		return -EINVAL;
333 }
334 
335 /**
336  *	save_image - save the suspend image data
337  */
338 
339 static int save_image(struct swap_map_handle *handle,
340                       struct snapshot_handle *snapshot,
341                       unsigned int nr_to_write)
342 {
343 	unsigned int m;
344 	int ret;
345 	int error = 0;
346 	int nr_pages;
347 	int err2;
348 	struct bio *bio;
349 	struct timeval start;
350 	struct timeval stop;
351 
352 	printk("Saving image data pages (%u pages) ...     ", nr_to_write);
353 	m = nr_to_write / 100;
354 	if (!m)
355 		m = 1;
356 	nr_pages = 0;
357 	bio = NULL;
358 	do_gettimeofday(&start);
359 	do {
360 		ret = snapshot_read_next(snapshot, PAGE_SIZE);
361 		if (ret > 0) {
362 			error = swap_write_page(handle, data_of(*snapshot),
363 						&bio);
364 			if (error)
365 				break;
366 			if (!(nr_pages % m))
367 				printk("\b\b\b\b%3d%%", nr_pages / m);
368 			nr_pages++;
369 		}
370 	} while (ret > 0);
371 	err2 = wait_on_bio_chain(&bio);
372 	do_gettimeofday(&stop);
373 	if (!error)
374 		error = err2;
375 	if (!error)
376 		printk("\b\b\b\bdone\n");
377 	show_speed(&start, &stop, nr_to_write, "Wrote");
378 	return error;
379 }
380 
381 /**
382  *	enough_swap - Make sure we have enough swap to save the image.
383  *
384  *	Returns TRUE or FALSE after checking the total amount of swap
385  *	space avaiable from the resume partition.
386  */
387 
388 static int enough_swap(unsigned int nr_pages)
389 {
390 	unsigned int free_swap = count_swap_pages(root_swap, 1);
391 
392 	pr_debug("swsusp: free swap pages: %u\n", free_swap);
393 	return free_swap > nr_pages + PAGES_FOR_IO;
394 }
395 
396 /**
397  *	swsusp_write - Write entire image and metadata.
398  *
399  *	It is important _NOT_ to umount filesystems at this point. We want
400  *	them synced (in case something goes wrong) but we DO not want to mark
401  *	filesystem clean: it is not. (And it does not matter, if we resume
402  *	correctly, we'll mark system clean, anyway.)
403  */
404 
405 int swsusp_write(void)
406 {
407 	struct swap_map_handle handle;
408 	struct snapshot_handle snapshot;
409 	struct swsusp_info *header;
410 	int error;
411 
412 	error = swsusp_swap_check();
413 	if (error) {
414 		printk(KERN_ERR "swsusp: Cannot find swap device, try "
415 				"swapon -a.\n");
416 		return error;
417 	}
418 	memset(&snapshot, 0, sizeof(struct snapshot_handle));
419 	error = snapshot_read_next(&snapshot, PAGE_SIZE);
420 	if (error < PAGE_SIZE) {
421 		if (error >= 0)
422 			error = -EFAULT;
423 
424 		goto out;
425 	}
426 	header = (struct swsusp_info *)data_of(snapshot);
427 	if (!enough_swap(header->pages)) {
428 		printk(KERN_ERR "swsusp: Not enough free swap\n");
429 		error = -ENOSPC;
430 		goto out;
431 	}
432 	error = get_swap_writer(&handle);
433 	if (!error) {
434 		sector_t start = handle.cur_swap;
435 
436 		error = swap_write_page(&handle, header, NULL);
437 		if (!error)
438 			error = save_image(&handle, &snapshot,
439 					header->pages - 1);
440 
441 		if (!error) {
442 			flush_swap_writer(&handle);
443 			printk("S");
444 			error = mark_swapfiles(start);
445 			printk("|\n");
446 		}
447 	}
448 	if (error)
449 		free_all_swap_pages(root_swap, handle.bitmap);
450 	release_swap_writer(&handle);
451 out:
452 	swsusp_close();
453 	return error;
454 }
455 
456 /**
457  *	The following functions allow us to read data using a swap map
458  *	in a file-alike way
459  */
460 
461 static void release_swap_reader(struct swap_map_handle *handle)
462 {
463 	if (handle->cur)
464 		free_page((unsigned long)handle->cur);
465 	handle->cur = NULL;
466 }
467 
468 static int get_swap_reader(struct swap_map_handle *handle, sector_t start)
469 {
470 	int error;
471 
472 	if (!start)
473 		return -EINVAL;
474 
475 	handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
476 	if (!handle->cur)
477 		return -ENOMEM;
478 
479 	error = bio_read_page(start, handle->cur, NULL);
480 	if (error) {
481 		release_swap_reader(handle);
482 		return error;
483 	}
484 	handle->k = 0;
485 	return 0;
486 }
487 
488 static int swap_read_page(struct swap_map_handle *handle, void *buf,
489 				struct bio **bio_chain)
490 {
491 	sector_t offset;
492 	int error;
493 
494 	if (!handle->cur)
495 		return -EINVAL;
496 	offset = handle->cur->entries[handle->k];
497 	if (!offset)
498 		return -EFAULT;
499 	error = bio_read_page(offset, buf, bio_chain);
500 	if (error)
501 		return error;
502 	if (++handle->k >= MAP_PAGE_ENTRIES) {
503 		error = wait_on_bio_chain(bio_chain);
504 		handle->k = 0;
505 		offset = handle->cur->next_swap;
506 		if (!offset)
507 			release_swap_reader(handle);
508 		else if (!error)
509 			error = bio_read_page(offset, handle->cur, NULL);
510 	}
511 	return error;
512 }
513 
514 /**
515  *	load_image - load the image using the swap map handle
516  *	@handle and the snapshot handle @snapshot
517  *	(assume there are @nr_pages pages to load)
518  */
519 
520 static int load_image(struct swap_map_handle *handle,
521                       struct snapshot_handle *snapshot,
522                       unsigned int nr_to_read)
523 {
524 	unsigned int m;
525 	int error = 0;
526 	struct timeval start;
527 	struct timeval stop;
528 	struct bio *bio;
529 	int err2;
530 	unsigned nr_pages;
531 
532 	printk("Loading image data pages (%u pages) ...     ", nr_to_read);
533 	m = nr_to_read / 100;
534 	if (!m)
535 		m = 1;
536 	nr_pages = 0;
537 	bio = NULL;
538 	do_gettimeofday(&start);
539 	for ( ; ; ) {
540 		error = snapshot_write_next(snapshot, PAGE_SIZE);
541 		if (error <= 0)
542 			break;
543 		error = swap_read_page(handle, data_of(*snapshot), &bio);
544 		if (error)
545 			break;
546 		if (snapshot->sync_read)
547 			error = wait_on_bio_chain(&bio);
548 		if (error)
549 			break;
550 		if (!(nr_pages % m))
551 			printk("\b\b\b\b%3d%%", nr_pages / m);
552 		nr_pages++;
553 	}
554 	err2 = wait_on_bio_chain(&bio);
555 	do_gettimeofday(&stop);
556 	if (!error)
557 		error = err2;
558 	if (!error) {
559 		printk("\b\b\b\bdone\n");
560 		snapshot_free_unused_memory(snapshot);
561 		if (!snapshot_image_loaded(snapshot))
562 			error = -ENODATA;
563 	}
564 	show_speed(&start, &stop, nr_to_read, "Read");
565 	return error;
566 }
567 
568 int swsusp_read(void)
569 {
570 	int error;
571 	struct swap_map_handle handle;
572 	struct snapshot_handle snapshot;
573 	struct swsusp_info *header;
574 
575 	if (IS_ERR(resume_bdev)) {
576 		pr_debug("swsusp: block device not initialised\n");
577 		return PTR_ERR(resume_bdev);
578 	}
579 
580 	memset(&snapshot, 0, sizeof(struct snapshot_handle));
581 	error = snapshot_write_next(&snapshot, PAGE_SIZE);
582 	if (error < PAGE_SIZE)
583 		return error < 0 ? error : -EFAULT;
584 	header = (struct swsusp_info *)data_of(snapshot);
585 	error = get_swap_reader(&handle, swsusp_header.image);
586 	if (!error)
587 		error = swap_read_page(&handle, header, NULL);
588 	if (!error)
589 		error = load_image(&handle, &snapshot, header->pages - 1);
590 	release_swap_reader(&handle);
591 
592 	blkdev_put(resume_bdev);
593 
594 	if (!error)
595 		pr_debug("swsusp: Reading resume file was successful\n");
596 	else
597 		pr_debug("swsusp: Error %d resuming\n", error);
598 	return error;
599 }
600 
601 /**
602  *      swsusp_check - Check for swsusp signature in the resume device
603  */
604 
605 int swsusp_check(void)
606 {
607 	int error;
608 
609 	resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
610 	if (!IS_ERR(resume_bdev)) {
611 		set_blocksize(resume_bdev, PAGE_SIZE);
612 		memset(&swsusp_header, 0, sizeof(swsusp_header));
613 		if ((error = bio_read_page(0, &swsusp_header, NULL)))
614 			return error;
615 		if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
616 			memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
617 			/* Reset swap signature now */
618 			error = bio_write_page(0, &swsusp_header, NULL);
619 		} else {
620 			return -EINVAL;
621 		}
622 		if (error)
623 			blkdev_put(resume_bdev);
624 		else
625 			pr_debug("swsusp: Signature found, resuming\n");
626 	} else {
627 		error = PTR_ERR(resume_bdev);
628 	}
629 
630 	if (error)
631 		pr_debug("swsusp: Error %d check for resume file\n", error);
632 
633 	return error;
634 }
635 
636 /**
637  *	swsusp_close - close swap device.
638  */
639 
640 void swsusp_close(void)
641 {
642 	if (IS_ERR(resume_bdev)) {
643 		pr_debug("swsusp: block device not initialised\n");
644 		return;
645 	}
646 
647 	blkdev_put(resume_bdev);
648 }
649