sky2: kfree_skb with IRQ with netconsole
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / power / swap.c
blob1a3b0dd2c3fcc18b2db25fc7472560cb67edfb7e
1 /*
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@suse.cz>
8 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
10 * This file is released under the GPLv2.
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>
30 #include "power.h"
32 extern char resume_file[];
34 #define SWSUSP_SIG "S1SUSPEND"
36 static struct swsusp_header {
37 char reserved[PAGE_SIZE - 20 - sizeof(swp_entry_t)];
38 swp_entry_t image;
39 char orig_sig[10];
40 char sig[10];
41 } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
44 * Saving part...
47 static unsigned short root_swap = 0xffff;
49 static int mark_swapfiles(swp_entry_t start)
51 int error;
53 rw_swap_page_sync(READ, swp_entry(root_swap, 0),
54 virt_to_page((unsigned long)&swsusp_header), NULL);
55 if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
56 !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
57 memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
58 memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
59 swsusp_header.image = start;
60 error = rw_swap_page_sync(WRITE, swp_entry(root_swap, 0),
61 virt_to_page((unsigned long)&swsusp_header),
62 NULL);
63 } else {
64 pr_debug("swsusp: Partition is not swap space.\n");
65 error = -ENODEV;
67 return error;
70 /**
71 * swsusp_swap_check - check if the resume device is a swap device
72 * and get its index (if so)
75 static int swsusp_swap_check(void) /* This is called before saving image */
77 int res = swap_type_of(swsusp_resume_device);
79 if (res >= 0) {
80 root_swap = res;
81 return 0;
83 return res;
86 /**
87 * write_page - Write one page to given swap location.
88 * @buf: Address we're writing.
89 * @offset: Offset of the swap page we're writing to.
90 * @bio_chain: Link the next write BIO here
93 static int write_page(void *buf, unsigned long offset, struct bio **bio_chain)
95 swp_entry_t entry;
96 int error = -ENOSPC;
98 if (offset) {
99 struct page *page = virt_to_page(buf);
101 if (bio_chain) {
103 * Whether or not we successfully allocated a copy page,
104 * we take a ref on the page here. It gets undone in
105 * wait_on_bio_chain().
107 struct page *page_copy;
108 page_copy = alloc_page(GFP_ATOMIC);
109 if (page_copy == NULL) {
110 WARN_ON_ONCE(1);
111 bio_chain = NULL; /* Go synchronous */
112 get_page(page);
113 } else {
114 memcpy(page_address(page_copy),
115 page_address(page), PAGE_SIZE);
116 page = page_copy;
119 entry = swp_entry(root_swap, offset);
120 error = rw_swap_page_sync(WRITE, entry, page, bio_chain);
122 return error;
126 * The swap map is a data structure used for keeping track of each page
127 * written to a swap partition. It consists of many swap_map_page
128 * structures that contain each an array of MAP_PAGE_SIZE swap entries.
129 * These structures are stored on the swap and linked together with the
130 * help of the .next_swap member.
132 * The swap map is created during suspend. The swap map pages are
133 * allocated and populated one at a time, so we only need one memory
134 * page to set up the entire structure.
136 * During resume we also only need to use one swap_map_page structure
137 * at a time.
140 #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(long) - 1)
142 struct swap_map_page {
143 unsigned long entries[MAP_PAGE_ENTRIES];
144 unsigned long next_swap;
148 * The swap_map_handle structure is used for handling swap in
149 * a file-alike way
152 struct swap_map_handle {
153 struct swap_map_page *cur;
154 unsigned long cur_swap;
155 struct bitmap_page *bitmap;
156 unsigned int k;
159 static void release_swap_writer(struct swap_map_handle *handle)
161 if (handle->cur)
162 free_page((unsigned long)handle->cur);
163 handle->cur = NULL;
164 if (handle->bitmap)
165 free_bitmap(handle->bitmap);
166 handle->bitmap = NULL;
169 static void show_speed(struct timeval *start, struct timeval *stop,
170 unsigned nr_pages, char *msg)
172 s64 elapsed_centisecs64;
173 int centisecs;
174 int k;
175 int kps;
177 elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
178 do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
179 centisecs = elapsed_centisecs64;
180 if (centisecs == 0)
181 centisecs = 1; /* avoid div-by-zero */
182 k = nr_pages * (PAGE_SIZE / 1024);
183 kps = (k * 100) / centisecs;
184 printk("%s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n", msg, k,
185 centisecs / 100, centisecs % 100,
186 kps / 1000, (kps % 1000) / 10);
189 static int get_swap_writer(struct swap_map_handle *handle)
191 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
192 if (!handle->cur)
193 return -ENOMEM;
194 handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0));
195 if (!handle->bitmap) {
196 release_swap_writer(handle);
197 return -ENOMEM;
199 handle->cur_swap = alloc_swap_page(root_swap, handle->bitmap);
200 if (!handle->cur_swap) {
201 release_swap_writer(handle);
202 return -ENOSPC;
204 handle->k = 0;
205 return 0;
208 static int wait_on_bio_chain(struct bio **bio_chain)
210 struct bio *bio;
211 struct bio *next_bio;
212 int ret = 0;
214 if (bio_chain == NULL)
215 return 0;
217 bio = *bio_chain;
218 if (bio == NULL)
219 return 0;
220 while (bio) {
221 struct page *page;
223 next_bio = bio->bi_private;
224 page = bio->bi_io_vec[0].bv_page;
225 wait_on_page_locked(page);
226 if (!PageUptodate(page) || PageError(page))
227 ret = -EIO;
228 put_page(page);
229 bio_put(bio);
230 bio = next_bio;
232 *bio_chain = NULL;
233 return ret;
236 static int swap_write_page(struct swap_map_handle *handle, void *buf,
237 struct bio **bio_chain)
239 int error = 0;
240 unsigned long offset;
242 if (!handle->cur)
243 return -EINVAL;
244 offset = alloc_swap_page(root_swap, handle->bitmap);
245 error = write_page(buf, offset, bio_chain);
246 if (error)
247 return error;
248 handle->cur->entries[handle->k++] = offset;
249 if (handle->k >= MAP_PAGE_ENTRIES) {
250 error = wait_on_bio_chain(bio_chain);
251 if (error)
252 goto out;
253 offset = alloc_swap_page(root_swap, handle->bitmap);
254 if (!offset)
255 return -ENOSPC;
256 handle->cur->next_swap = offset;
257 error = write_page(handle->cur, handle->cur_swap, NULL);
258 if (error)
259 goto out;
260 memset(handle->cur, 0, PAGE_SIZE);
261 handle->cur_swap = offset;
262 handle->k = 0;
264 out:
265 return error;
268 static int flush_swap_writer(struct swap_map_handle *handle)
270 if (handle->cur && handle->cur_swap)
271 return write_page(handle->cur, handle->cur_swap, NULL);
272 else
273 return -EINVAL;
277 * save_image - save the suspend image data
280 static int save_image(struct swap_map_handle *handle,
281 struct snapshot_handle *snapshot,
282 unsigned int nr_to_write)
284 unsigned int m;
285 int ret;
286 int error = 0;
287 int nr_pages;
288 int err2;
289 struct bio *bio;
290 struct timeval start;
291 struct timeval stop;
293 printk("Saving image data pages (%u pages) ... ", nr_to_write);
294 m = nr_to_write / 100;
295 if (!m)
296 m = 1;
297 nr_pages = 0;
298 bio = NULL;
299 do_gettimeofday(&start);
300 do {
301 ret = snapshot_read_next(snapshot, PAGE_SIZE);
302 if (ret > 0) {
303 error = swap_write_page(handle, data_of(*snapshot),
304 &bio);
305 if (error)
306 break;
307 if (!(nr_pages % m))
308 printk("\b\b\b\b%3d%%", nr_pages / m);
309 nr_pages++;
311 } while (ret > 0);
312 err2 = wait_on_bio_chain(&bio);
313 do_gettimeofday(&stop);
314 if (!error)
315 error = err2;
316 if (!error)
317 printk("\b\b\b\bdone\n");
318 show_speed(&start, &stop, nr_to_write, "Wrote");
319 return error;
323 * enough_swap - Make sure we have enough swap to save the image.
325 * Returns TRUE or FALSE after checking the total amount of swap
326 * space avaiable from the resume partition.
329 static int enough_swap(unsigned int nr_pages)
331 unsigned int free_swap = count_swap_pages(root_swap, 1);
333 pr_debug("swsusp: free swap pages: %u\n", free_swap);
334 return free_swap > nr_pages + PAGES_FOR_IO;
338 * swsusp_write - Write entire image and metadata.
340 * It is important _NOT_ to umount filesystems at this point. We want
341 * them synced (in case something goes wrong) but we DO not want to mark
342 * filesystem clean: it is not. (And it does not matter, if we resume
343 * correctly, we'll mark system clean, anyway.)
346 int swsusp_write(void)
348 struct swap_map_handle handle;
349 struct snapshot_handle snapshot;
350 struct swsusp_info *header;
351 int error;
353 if ((error = swsusp_swap_check())) {
354 printk(KERN_ERR "swsusp: Cannot find swap device, try "
355 "swapon -a.\n");
356 return error;
358 memset(&snapshot, 0, sizeof(struct snapshot_handle));
359 error = snapshot_read_next(&snapshot, PAGE_SIZE);
360 if (error < PAGE_SIZE)
361 return error < 0 ? error : -EFAULT;
362 header = (struct swsusp_info *)data_of(snapshot);
363 if (!enough_swap(header->pages)) {
364 printk(KERN_ERR "swsusp: Not enough free swap\n");
365 return -ENOSPC;
367 error = get_swap_writer(&handle);
368 if (!error) {
369 unsigned long start = handle.cur_swap;
370 error = swap_write_page(&handle, header, NULL);
371 if (!error)
372 error = save_image(&handle, &snapshot,
373 header->pages - 1);
374 if (!error) {
375 flush_swap_writer(&handle);
376 printk("S");
377 error = mark_swapfiles(swp_entry(root_swap, start));
378 printk("|\n");
381 if (error)
382 free_all_swap_pages(root_swap, handle.bitmap);
383 release_swap_writer(&handle);
384 return error;
387 static struct block_device *resume_bdev;
390 * submit - submit BIO request.
391 * @rw: READ or WRITE.
392 * @off physical offset of page.
393 * @page: page we're reading or writing.
394 * @bio_chain: list of pending biod (for async reading)
396 * Straight from the textbook - allocate and initialize the bio.
397 * If we're reading, make sure the page is marked as dirty.
398 * Then submit it and, if @bio_chain == NULL, wait.
400 static int submit(int rw, pgoff_t page_off, struct page *page,
401 struct bio **bio_chain)
403 struct bio *bio;
405 bio = bio_alloc(GFP_ATOMIC, 1);
406 if (!bio)
407 return -ENOMEM;
408 bio->bi_sector = page_off * (PAGE_SIZE >> 9);
409 bio->bi_bdev = resume_bdev;
410 bio->bi_end_io = end_swap_bio_read;
412 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
413 printk("swsusp: ERROR: adding page to bio at %ld\n", page_off);
414 bio_put(bio);
415 return -EFAULT;
418 lock_page(page);
419 bio_get(bio);
421 if (bio_chain == NULL) {
422 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
423 wait_on_page_locked(page);
424 if (rw == READ)
425 bio_set_pages_dirty(bio);
426 bio_put(bio);
427 } else {
428 if (rw == READ)
429 get_page(page); /* These pages are freed later */
430 bio->bi_private = *bio_chain;
431 *bio_chain = bio;
432 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
434 return 0;
437 static int bio_read_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
439 return submit(READ, page_off, virt_to_page(addr), bio_chain);
442 static int bio_write_page(pgoff_t page_off, void *addr)
444 return submit(WRITE, page_off, virt_to_page(addr), NULL);
448 * The following functions allow us to read data using a swap map
449 * in a file-alike way
452 static void release_swap_reader(struct swap_map_handle *handle)
454 if (handle->cur)
455 free_page((unsigned long)handle->cur);
456 handle->cur = NULL;
459 static int get_swap_reader(struct swap_map_handle *handle,
460 swp_entry_t start)
462 int error;
464 if (!swp_offset(start))
465 return -EINVAL;
466 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
467 if (!handle->cur)
468 return -ENOMEM;
469 error = bio_read_page(swp_offset(start), handle->cur, NULL);
470 if (error) {
471 release_swap_reader(handle);
472 return error;
474 handle->k = 0;
475 return 0;
478 static int swap_read_page(struct swap_map_handle *handle, void *buf,
479 struct bio **bio_chain)
481 unsigned long offset;
482 int error;
484 if (!handle->cur)
485 return -EINVAL;
486 offset = handle->cur->entries[handle->k];
487 if (!offset)
488 return -EFAULT;
489 error = bio_read_page(offset, buf, bio_chain);
490 if (error)
491 return error;
492 if (++handle->k >= MAP_PAGE_ENTRIES) {
493 error = wait_on_bio_chain(bio_chain);
494 handle->k = 0;
495 offset = handle->cur->next_swap;
496 if (!offset)
497 release_swap_reader(handle);
498 else if (!error)
499 error = bio_read_page(offset, handle->cur, NULL);
501 return error;
505 * load_image - load the image using the swap map handle
506 * @handle and the snapshot handle @snapshot
507 * (assume there are @nr_pages pages to load)
510 static int load_image(struct swap_map_handle *handle,
511 struct snapshot_handle *snapshot,
512 unsigned int nr_to_read)
514 unsigned int m;
515 int error = 0;
516 struct timeval start;
517 struct timeval stop;
518 struct bio *bio;
519 int err2;
520 unsigned nr_pages;
522 printk("Loading image data pages (%u pages) ... ", nr_to_read);
523 m = nr_to_read / 100;
524 if (!m)
525 m = 1;
526 nr_pages = 0;
527 bio = NULL;
528 do_gettimeofday(&start);
529 for ( ; ; ) {
530 error = snapshot_write_next(snapshot, PAGE_SIZE);
531 if (error <= 0)
532 break;
533 error = swap_read_page(handle, data_of(*snapshot), &bio);
534 if (error)
535 break;
536 if (snapshot->sync_read)
537 error = wait_on_bio_chain(&bio);
538 if (error)
539 break;
540 if (!(nr_pages % m))
541 printk("\b\b\b\b%3d%%", nr_pages / m);
542 nr_pages++;
544 err2 = wait_on_bio_chain(&bio);
545 do_gettimeofday(&stop);
546 if (!error)
547 error = err2;
548 if (!error) {
549 printk("\b\b\b\bdone\n");
550 snapshot_free_unused_memory(snapshot);
551 if (!snapshot_image_loaded(snapshot))
552 error = -ENODATA;
554 show_speed(&start, &stop, nr_to_read, "Read");
555 return error;
558 int swsusp_read(void)
560 int error;
561 struct swap_map_handle handle;
562 struct snapshot_handle snapshot;
563 struct swsusp_info *header;
565 if (IS_ERR(resume_bdev)) {
566 pr_debug("swsusp: block device not initialised\n");
567 return PTR_ERR(resume_bdev);
570 memset(&snapshot, 0, sizeof(struct snapshot_handle));
571 error = snapshot_write_next(&snapshot, PAGE_SIZE);
572 if (error < PAGE_SIZE)
573 return error < 0 ? error : -EFAULT;
574 header = (struct swsusp_info *)data_of(snapshot);
575 error = get_swap_reader(&handle, swsusp_header.image);
576 if (!error)
577 error = swap_read_page(&handle, header, NULL);
578 if (!error)
579 error = load_image(&handle, &snapshot, header->pages - 1);
580 release_swap_reader(&handle);
582 blkdev_put(resume_bdev);
584 if (!error)
585 pr_debug("swsusp: Reading resume file was successful\n");
586 else
587 pr_debug("swsusp: Error %d resuming\n", error);
588 return error;
592 * swsusp_check - Check for swsusp signature in the resume device
595 int swsusp_check(void)
597 int error;
599 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
600 if (!IS_ERR(resume_bdev)) {
601 set_blocksize(resume_bdev, PAGE_SIZE);
602 memset(&swsusp_header, 0, sizeof(swsusp_header));
603 if ((error = bio_read_page(0, &swsusp_header, NULL)))
604 return error;
605 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
606 memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
607 /* Reset swap signature now */
608 error = bio_write_page(0, &swsusp_header);
609 } else {
610 return -EINVAL;
612 if (error)
613 blkdev_put(resume_bdev);
614 else
615 pr_debug("swsusp: Signature found, resuming\n");
616 } else {
617 error = PTR_ERR(resume_bdev);
620 if (error)
621 pr_debug("swsusp: Error %d check for resume file\n", error);
623 return error;
627 * swsusp_close - close swap device.
630 void swsusp_close(void)
632 if (IS_ERR(resume_bdev)) {
633 pr_debug("swsusp: block device not initialised\n");
634 return;
637 blkdev_put(resume_bdev);