Merge branch 'upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/vitb/linux...
[linux-2.6] / kernel / power / swap.c
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(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;
42
43 /*
44  * Saving part...
45  */
46
47 static unsigned short root_swap = 0xffff;
48
49 static int mark_swapfiles(swp_entry_t start)
50 {
51         int error;
52
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;
66         }
67         return error;
68 }
69
70 /**
71  *      swsusp_swap_check - check if the resume device is a swap device
72  *      and get its index (if so)
73  */
74
75 static int swsusp_swap_check(void) /* This is called before saving image */
76 {
77         int res = swap_type_of(swsusp_resume_device);
78
79         if (res >= 0) {
80                 root_swap = res;
81                 return 0;
82         }
83         return res;
84 }
85
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
91  */
92
93 static int write_page(void *buf, unsigned long offset, struct bio **bio_chain)
94 {
95         swp_entry_t entry;
96         int error = -ENOSPC;
97
98         if (offset) {
99                 struct page *page = virt_to_page(buf);
100
101                 if (bio_chain) {
102                         /*
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().
106                          */
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;
117                         }
118                 }
119                 entry = swp_entry(root_swap, offset);
120                 error = rw_swap_page_sync(WRITE, entry, page, bio_chain);
121         }
122         return error;
123 }
124
125 /*
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.
131  *
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.
135  *
136  *      During resume we also only need to use one swap_map_page structure
137  *      at a time.
138  */
139
140 #define MAP_PAGE_ENTRIES        (PAGE_SIZE / sizeof(long) - 1)
141
142 struct swap_map_page {
143         unsigned long           entries[MAP_PAGE_ENTRIES];
144         unsigned long           next_swap;
145 };
146
147 /**
148  *      The swap_map_handle structure is used for handling swap in
149  *      a file-alike way
150  */
151
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;
157 };
158
159 static void release_swap_writer(struct swap_map_handle *handle)
160 {
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;
167 }
168
169 static void show_speed(struct timeval *start, struct timeval *stop,
170                         unsigned nr_pages, char *msg)
171 {
172         s64 elapsed_centisecs64;
173         int centisecs;
174         int k;
175         int kps;
176
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);
187 }
188
189 static int get_swap_writer(struct swap_map_handle *handle)
190 {
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;
198         }
199         handle->cur_swap = alloc_swap_page(root_swap, handle->bitmap);
200         if (!handle->cur_swap) {
201                 release_swap_writer(handle);
202                 return -ENOSPC;
203         }
204         handle->k = 0;
205         return 0;
206 }
207
208 static int wait_on_bio_chain(struct bio **bio_chain)
209 {
210         struct bio *bio;
211         struct bio *next_bio;
212         int ret = 0;
213
214         if (bio_chain == NULL)
215                 return 0;
216
217         bio = *bio_chain;
218         if (bio == NULL)
219                 return 0;
220         while (bio) {
221                 struct page *page;
222
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;
231         }
232         *bio_chain = NULL;
233         return ret;
234 }
235
236 static int swap_write_page(struct swap_map_handle *handle, void *buf,
237                                 struct bio **bio_chain)
238 {
239         int error = 0;
240         unsigned long offset;
241
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;
263         }
264 out:
265         return error;
266 }
267
268 static int flush_swap_writer(struct swap_map_handle *handle)
269 {
270         if (handle->cur && handle->cur_swap)
271                 return write_page(handle->cur, handle->cur_swap, NULL);
272         else
273                 return -EINVAL;
274 }
275
276 /**
277  *      save_image - save the suspend image data
278  */
279
280 static int save_image(struct swap_map_handle *handle,
281                       struct snapshot_handle *snapshot,
282                       unsigned int nr_to_write)
283 {
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;
292
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++;
310                 }
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;
320 }
321
322 /**
323  *      enough_swap - Make sure we have enough swap to save the image.
324  *
325  *      Returns TRUE or FALSE after checking the total amount of swap
326  *      space avaiable from the resume partition.
327  */
328
329 static int enough_swap(unsigned int nr_pages)
330 {
331         unsigned int free_swap = count_swap_pages(root_swap, 1);
332
333         pr_debug("swsusp: free swap pages: %u\n", free_swap);
334         return free_swap > nr_pages + PAGES_FOR_IO;
335 }
336
337 /**
338  *      swsusp_write - Write entire image and metadata.
339  *
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.)
344  */
345
346 int swsusp_write(void)
347 {
348         struct swap_map_handle handle;
349         struct snapshot_handle snapshot;
350         struct swsusp_info *header;
351         int error;
352
353         if ((error = swsusp_swap_check())) {
354                 printk(KERN_ERR "swsusp: Cannot find swap device, try "
355                                 "swapon -a.\n");
356                 return error;
357         }
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;
366         }
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");
379                 }
380         }
381         if (error)
382                 free_all_swap_pages(root_swap, handle.bitmap);
383         release_swap_writer(&handle);
384         return error;
385 }
386
387 static struct block_device *resume_bdev;
388
389 /**
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)
395  *
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.
399  */
400 static int submit(int rw, pgoff_t page_off, struct page *page,
401                         struct bio **bio_chain)
402 {
403         struct bio *bio;
404
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;
411
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;
416         }
417
418         lock_page(page);
419         bio_get(bio);
420
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                 get_page(page);
429                 bio->bi_private = *bio_chain;
430                 *bio_chain = bio;
431                 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
432         }
433         return 0;
434 }
435
436 static int bio_read_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
437 {
438         return submit(READ, page_off, virt_to_page(addr), bio_chain);
439 }
440
441 static int bio_write_page(pgoff_t page_off, void *addr)
442 {
443         return submit(WRITE, page_off, virt_to_page(addr), NULL);
444 }
445
446 /**
447  *      The following functions allow us to read data using a swap map
448  *      in a file-alike way
449  */
450
451 static void release_swap_reader(struct swap_map_handle *handle)
452 {
453         if (handle->cur)
454                 free_page((unsigned long)handle->cur);
455         handle->cur = NULL;
456 }
457
458 static int get_swap_reader(struct swap_map_handle *handle,
459                                       swp_entry_t start)
460 {
461         int error;
462
463         if (!swp_offset(start))
464                 return -EINVAL;
465         handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
466         if (!handle->cur)
467                 return -ENOMEM;
468         error = bio_read_page(swp_offset(start), handle->cur, NULL);
469         if (error) {
470                 release_swap_reader(handle);
471                 return error;
472         }
473         handle->k = 0;
474         return 0;
475 }
476
477 static int swap_read_page(struct swap_map_handle *handle, void *buf,
478                                 struct bio **bio_chain)
479 {
480         unsigned long offset;
481         int error;
482
483         if (!handle->cur)
484                 return -EINVAL;
485         offset = handle->cur->entries[handle->k];
486         if (!offset)
487                 return -EFAULT;
488         error = bio_read_page(offset, buf, bio_chain);
489         if (error)
490                 return error;
491         if (++handle->k >= MAP_PAGE_ENTRIES) {
492                 error = wait_on_bio_chain(bio_chain);
493                 handle->k = 0;
494                 offset = handle->cur->next_swap;
495                 if (!offset)
496                         release_swap_reader(handle);
497                 else if (!error)
498                         error = bio_read_page(offset, handle->cur, NULL);
499         }
500         return error;
501 }
502
503 /**
504  *      load_image - load the image using the swap map handle
505  *      @handle and the snapshot handle @snapshot
506  *      (assume there are @nr_pages pages to load)
507  */
508
509 static int load_image(struct swap_map_handle *handle,
510                       struct snapshot_handle *snapshot,
511                       unsigned int nr_to_read)
512 {
513         unsigned int m;
514         int error = 0;
515         struct timeval start;
516         struct timeval stop;
517         struct bio *bio;
518         int err2;
519         unsigned nr_pages;
520
521         printk("Loading image data pages (%u pages) ...     ", nr_to_read);
522         m = nr_to_read / 100;
523         if (!m)
524                 m = 1;
525         nr_pages = 0;
526         bio = NULL;
527         do_gettimeofday(&start);
528         for ( ; ; ) {
529                 error = snapshot_write_next(snapshot, PAGE_SIZE);
530                 if (error <= 0)
531                         break;
532                 error = swap_read_page(handle, data_of(*snapshot), &bio);
533                 if (error)
534                         break;
535                 if (snapshot->sync_read)
536                         error = wait_on_bio_chain(&bio);
537                 if (error)
538                         break;
539                 if (!(nr_pages % m))
540                         printk("\b\b\b\b%3d%%", nr_pages / m);
541                 nr_pages++;
542         }
543         err2 = wait_on_bio_chain(&bio);
544         do_gettimeofday(&stop);
545         if (!error)
546                 error = err2;
547         if (!error) {
548                 printk("\b\b\b\bdone\n");
549                 snapshot_free_unused_memory(snapshot);
550                 if (!snapshot_image_loaded(snapshot))
551                         error = -ENODATA;
552         }
553         show_speed(&start, &stop, nr_to_read, "Read");
554         return error;
555 }
556
557 int swsusp_read(void)
558 {
559         int error;
560         struct swap_map_handle handle;
561         struct snapshot_handle snapshot;
562         struct swsusp_info *header;
563
564         if (IS_ERR(resume_bdev)) {
565                 pr_debug("swsusp: block device not initialised\n");
566                 return PTR_ERR(resume_bdev);
567         }
568
569         memset(&snapshot, 0, sizeof(struct snapshot_handle));
570         error = snapshot_write_next(&snapshot, PAGE_SIZE);
571         if (error < PAGE_SIZE)
572                 return error < 0 ? error : -EFAULT;
573         header = (struct swsusp_info *)data_of(snapshot);
574         error = get_swap_reader(&handle, swsusp_header.image);
575         if (!error)
576                 error = swap_read_page(&handle, header, NULL);
577         if (!error)
578                 error = load_image(&handle, &snapshot, header->pages - 1);
579         release_swap_reader(&handle);
580
581         blkdev_put(resume_bdev);
582
583         if (!error)
584                 pr_debug("swsusp: Reading resume file was successful\n");
585         else
586                 pr_debug("swsusp: Error %d resuming\n", error);
587         return error;
588 }
589
590 /**
591  *      swsusp_check - Check for swsusp signature in the resume device
592  */
593
594 int swsusp_check(void)
595 {
596         int error;
597
598         resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
599         if (!IS_ERR(resume_bdev)) {
600                 set_blocksize(resume_bdev, PAGE_SIZE);
601                 memset(&swsusp_header, 0, sizeof(swsusp_header));
602                 if ((error = bio_read_page(0, &swsusp_header, NULL)))
603                         return error;
604                 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
605                         memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
606                         /* Reset swap signature now */
607                         error = bio_write_page(0, &swsusp_header);
608                 } else {
609                         return -EINVAL;
610                 }
611                 if (error)
612                         blkdev_put(resume_bdev);
613                 else
614                         pr_debug("swsusp: Signature found, resuming\n");
615         } else {
616                 error = PTR_ERR(resume_bdev);
617         }
618
619         if (error)
620                 pr_debug("swsusp: Error %d check for resume file\n", error);
621
622         return error;
623 }
624
625 /**
626  *      swsusp_close - close swap device.
627  */
628
629 void swsusp_close(void)
630 {
631         if (IS_ERR(resume_bdev)) {
632                 pr_debug("swsusp: block device not initialised\n");
633                 return;
634         }
635
636         blkdev_put(resume_bdev);
637 }