Pull platform-drivers into test branch
[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(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_WAIT | __GFP_HIGH, 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 /*
137  * Saving part
138  */
139
140 static int mark_swapfiles(sector_t start)
141 {
142         int error;
143
144         bio_read_page(swsusp_resume_block, &swsusp_header, NULL);
145         if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
146             !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
147                 memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
148                 memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
149                 swsusp_header.image = start;
150                 error = bio_write_page(swsusp_resume_block,
151                                         &swsusp_header, NULL);
152         } else {
153                 printk(KERN_ERR "swsusp: Swap header not found!\n");
154                 error = -ENODEV;
155         }
156         return error;
157 }
158
159 /**
160  *      swsusp_swap_check - check if the resume device is a swap device
161  *      and get its index (if so)
162  */
163
164 static int swsusp_swap_check(void) /* This is called before saving image */
165 {
166         int res;
167
168         res = swap_type_of(swsusp_resume_device, swsusp_resume_block);
169         if (res < 0)
170                 return res;
171
172         root_swap = res;
173         resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_WRITE);
174         if (IS_ERR(resume_bdev))
175                 return PTR_ERR(resume_bdev);
176
177         res = set_blocksize(resume_bdev, PAGE_SIZE);
178         if (res < 0)
179                 blkdev_put(resume_bdev);
180
181         return res;
182 }
183
184 /**
185  *      write_page - Write one page to given swap location.
186  *      @buf:           Address we're writing.
187  *      @offset:        Offset of the swap page we're writing to.
188  *      @bio_chain:     Link the next write BIO here
189  */
190
191 static int write_page(void *buf, sector_t offset, struct bio **bio_chain)
192 {
193         void *src;
194
195         if (!offset)
196                 return -ENOSPC;
197
198         if (bio_chain) {
199                 src = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
200                 if (src) {
201                         memcpy(src, buf, PAGE_SIZE);
202                 } else {
203                         WARN_ON_ONCE(1);
204                         bio_chain = NULL;       /* Go synchronous */
205                         src = buf;
206                 }
207         } else {
208                 src = buf;
209         }
210         return bio_write_page(offset, src, bio_chain);
211 }
212
213 /*
214  *      The swap map is a data structure used for keeping track of each page
215  *      written to a swap partition.  It consists of many swap_map_page
216  *      structures that contain each an array of MAP_PAGE_SIZE swap entries.
217  *      These structures are stored on the swap and linked together with the
218  *      help of the .next_swap member.
219  *
220  *      The swap map is created during suspend.  The swap map pages are
221  *      allocated and populated one at a time, so we only need one memory
222  *      page to set up the entire structure.
223  *
224  *      During resume we also only need to use one swap_map_page structure
225  *      at a time.
226  */
227
228 #define MAP_PAGE_ENTRIES        (PAGE_SIZE / sizeof(sector_t) - 1)
229
230 struct swap_map_page {
231         sector_t entries[MAP_PAGE_ENTRIES];
232         sector_t next_swap;
233 };
234
235 /**
236  *      The swap_map_handle structure is used for handling swap in
237  *      a file-alike way
238  */
239
240 struct swap_map_handle {
241         struct swap_map_page *cur;
242         sector_t cur_swap;
243         struct bitmap_page *bitmap;
244         unsigned int k;
245 };
246
247 static void release_swap_writer(struct swap_map_handle *handle)
248 {
249         if (handle->cur)
250                 free_page((unsigned long)handle->cur);
251         handle->cur = NULL;
252         if (handle->bitmap)
253                 free_bitmap(handle->bitmap);
254         handle->bitmap = NULL;
255 }
256
257 static int get_swap_writer(struct swap_map_handle *handle)
258 {
259         handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
260         if (!handle->cur)
261                 return -ENOMEM;
262         handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0));
263         if (!handle->bitmap) {
264                 release_swap_writer(handle);
265                 return -ENOMEM;
266         }
267         handle->cur_swap = alloc_swapdev_block(root_swap, handle->bitmap);
268         if (!handle->cur_swap) {
269                 release_swap_writer(handle);
270                 return -ENOSPC;
271         }
272         handle->k = 0;
273         return 0;
274 }
275
276 static int swap_write_page(struct swap_map_handle *handle, void *buf,
277                                 struct bio **bio_chain)
278 {
279         int error = 0;
280         sector_t offset;
281
282         if (!handle->cur)
283                 return -EINVAL;
284         offset = alloc_swapdev_block(root_swap, handle->bitmap);
285         error = write_page(buf, offset, bio_chain);
286         if (error)
287                 return error;
288         handle->cur->entries[handle->k++] = offset;
289         if (handle->k >= MAP_PAGE_ENTRIES) {
290                 error = wait_on_bio_chain(bio_chain);
291                 if (error)
292                         goto out;
293                 offset = alloc_swapdev_block(root_swap, handle->bitmap);
294                 if (!offset)
295                         return -ENOSPC;
296                 handle->cur->next_swap = offset;
297                 error = write_page(handle->cur, handle->cur_swap, NULL);
298                 if (error)
299                         goto out;
300                 memset(handle->cur, 0, PAGE_SIZE);
301                 handle->cur_swap = offset;
302                 handle->k = 0;
303         }
304  out:
305         return error;
306 }
307
308 static int flush_swap_writer(struct swap_map_handle *handle)
309 {
310         if (handle->cur && handle->cur_swap)
311                 return write_page(handle->cur, handle->cur_swap, NULL);
312         else
313                 return -EINVAL;
314 }
315
316 /**
317  *      save_image - save the suspend image data
318  */
319
320 static int save_image(struct swap_map_handle *handle,
321                       struct snapshot_handle *snapshot,
322                       unsigned int nr_to_write)
323 {
324         unsigned int m;
325         int ret;
326         int error = 0;
327         int nr_pages;
328         int err2;
329         struct bio *bio;
330         struct timeval start;
331         struct timeval stop;
332
333         printk("Saving image data pages (%u pages) ...     ", nr_to_write);
334         m = nr_to_write / 100;
335         if (!m)
336                 m = 1;
337         nr_pages = 0;
338         bio = NULL;
339         do_gettimeofday(&start);
340         do {
341                 ret = snapshot_read_next(snapshot, PAGE_SIZE);
342                 if (ret > 0) {
343                         error = swap_write_page(handle, data_of(*snapshot),
344                                                 &bio);
345                         if (error)
346                                 break;
347                         if (!(nr_pages % m))
348                                 printk("\b\b\b\b%3d%%", nr_pages / m);
349                         nr_pages++;
350                 }
351         } while (ret > 0);
352         err2 = wait_on_bio_chain(&bio);
353         do_gettimeofday(&stop);
354         if (!error)
355                 error = err2;
356         if (!error)
357                 printk("\b\b\b\bdone\n");
358         swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
359         return error;
360 }
361
362 /**
363  *      enough_swap - Make sure we have enough swap to save the image.
364  *
365  *      Returns TRUE or FALSE after checking the total amount of swap
366  *      space avaiable from the resume partition.
367  */
368
369 static int enough_swap(unsigned int nr_pages)
370 {
371         unsigned int free_swap = count_swap_pages(root_swap, 1);
372
373         pr_debug("swsusp: free swap pages: %u\n", free_swap);
374         return free_swap > nr_pages + PAGES_FOR_IO;
375 }
376
377 /**
378  *      swsusp_write - Write entire image and metadata.
379  *
380  *      It is important _NOT_ to umount filesystems at this point. We want
381  *      them synced (in case something goes wrong) but we DO not want to mark
382  *      filesystem clean: it is not. (And it does not matter, if we resume
383  *      correctly, we'll mark system clean, anyway.)
384  */
385
386 int swsusp_write(void)
387 {
388         struct swap_map_handle handle;
389         struct snapshot_handle snapshot;
390         struct swsusp_info *header;
391         int error;
392
393         error = swsusp_swap_check();
394         if (error) {
395                 printk(KERN_ERR "swsusp: Cannot find swap device, try "
396                                 "swapon -a.\n");
397                 return error;
398         }
399         memset(&snapshot, 0, sizeof(struct snapshot_handle));
400         error = snapshot_read_next(&snapshot, PAGE_SIZE);
401         if (error < PAGE_SIZE) {
402                 if (error >= 0)
403                         error = -EFAULT;
404
405                 goto out;
406         }
407         header = (struct swsusp_info *)data_of(snapshot);
408         if (!enough_swap(header->pages)) {
409                 printk(KERN_ERR "swsusp: Not enough free swap\n");
410                 error = -ENOSPC;
411                 goto out;
412         }
413         error = get_swap_writer(&handle);
414         if (!error) {
415                 sector_t start = handle.cur_swap;
416
417                 error = swap_write_page(&handle, header, NULL);
418                 if (!error)
419                         error = save_image(&handle, &snapshot,
420                                         header->pages - 1);
421
422                 if (!error) {
423                         flush_swap_writer(&handle);
424                         printk("S");
425                         error = mark_swapfiles(start);
426                         printk("|\n");
427                 }
428         }
429         if (error)
430                 free_all_swap_pages(root_swap, handle.bitmap);
431         release_swap_writer(&handle);
432  out:
433         swsusp_close();
434         return error;
435 }
436
437 /**
438  *      The following functions allow us to read data using a swap map
439  *      in a file-alike way
440  */
441
442 static void release_swap_reader(struct swap_map_handle *handle)
443 {
444         if (handle->cur)
445                 free_page((unsigned long)handle->cur);
446         handle->cur = NULL;
447 }
448
449 static int get_swap_reader(struct swap_map_handle *handle, sector_t start)
450 {
451         int error;
452
453         if (!start)
454                 return -EINVAL;
455
456         handle->cur = (struct swap_map_page *)get_zeroed_page(__GFP_WAIT | __GFP_HIGH);
457         if (!handle->cur)
458                 return -ENOMEM;
459
460         error = bio_read_page(start, handle->cur, NULL);
461         if (error) {
462                 release_swap_reader(handle);
463                 return error;
464         }
465         handle->k = 0;
466         return 0;
467 }
468
469 static int swap_read_page(struct swap_map_handle *handle, void *buf,
470                                 struct bio **bio_chain)
471 {
472         sector_t offset;
473         int error;
474
475         if (!handle->cur)
476                 return -EINVAL;
477         offset = handle->cur->entries[handle->k];
478         if (!offset)
479                 return -EFAULT;
480         error = bio_read_page(offset, buf, bio_chain);
481         if (error)
482                 return error;
483         if (++handle->k >= MAP_PAGE_ENTRIES) {
484                 error = wait_on_bio_chain(bio_chain);
485                 handle->k = 0;
486                 offset = handle->cur->next_swap;
487                 if (!offset)
488                         release_swap_reader(handle);
489                 else if (!error)
490                         error = bio_read_page(offset, handle->cur, NULL);
491         }
492         return error;
493 }
494
495 /**
496  *      load_image - load the image using the swap map handle
497  *      @handle and the snapshot handle @snapshot
498  *      (assume there are @nr_pages pages to load)
499  */
500
501 static int load_image(struct swap_map_handle *handle,
502                       struct snapshot_handle *snapshot,
503                       unsigned int nr_to_read)
504 {
505         unsigned int m;
506         int error = 0;
507         struct timeval start;
508         struct timeval stop;
509         struct bio *bio;
510         int err2;
511         unsigned nr_pages;
512
513         printk("Loading image data pages (%u pages) ...     ", nr_to_read);
514         m = nr_to_read / 100;
515         if (!m)
516                 m = 1;
517         nr_pages = 0;
518         bio = NULL;
519         do_gettimeofday(&start);
520         for ( ; ; ) {
521                 error = snapshot_write_next(snapshot, PAGE_SIZE);
522                 if (error <= 0)
523                         break;
524                 error = swap_read_page(handle, data_of(*snapshot), &bio);
525                 if (error)
526                         break;
527                 if (snapshot->sync_read)
528                         error = wait_on_bio_chain(&bio);
529                 if (error)
530                         break;
531                 if (!(nr_pages % m))
532                         printk("\b\b\b\b%3d%%", nr_pages / m);
533                 nr_pages++;
534         }
535         err2 = wait_on_bio_chain(&bio);
536         do_gettimeofday(&stop);
537         if (!error)
538                 error = err2;
539         if (!error) {
540                 printk("\b\b\b\bdone\n");
541                 snapshot_write_finalize(snapshot);
542                 if (!snapshot_image_loaded(snapshot))
543                         error = -ENODATA;
544         }
545         swsusp_show_speed(&start, &stop, nr_to_read, "Read");
546         return error;
547 }
548
549 int swsusp_read(void)
550 {
551         int error;
552         struct swap_map_handle handle;
553         struct snapshot_handle snapshot;
554         struct swsusp_info *header;
555
556         if (IS_ERR(resume_bdev)) {
557                 pr_debug("swsusp: block device not initialised\n");
558                 return PTR_ERR(resume_bdev);
559         }
560
561         memset(&snapshot, 0, sizeof(struct snapshot_handle));
562         error = snapshot_write_next(&snapshot, PAGE_SIZE);
563         if (error < PAGE_SIZE)
564                 return error < 0 ? error : -EFAULT;
565         header = (struct swsusp_info *)data_of(snapshot);
566         error = get_swap_reader(&handle, swsusp_header.image);
567         if (!error)
568                 error = swap_read_page(&handle, header, NULL);
569         if (!error)
570                 error = load_image(&handle, &snapshot, header->pages - 1);
571         release_swap_reader(&handle);
572
573         blkdev_put(resume_bdev);
574
575         if (!error)
576                 pr_debug("swsusp: Reading resume file was successful\n");
577         else
578                 pr_debug("swsusp: Error %d resuming\n", error);
579         return error;
580 }
581
582 /**
583  *      swsusp_check - Check for swsusp signature in the resume device
584  */
585
586 int swsusp_check(void)
587 {
588         int error;
589
590         resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
591         if (!IS_ERR(resume_bdev)) {
592                 set_blocksize(resume_bdev, PAGE_SIZE);
593                 memset(&swsusp_header, 0, sizeof(swsusp_header));
594                 error = bio_read_page(swsusp_resume_block,
595                                         &swsusp_header, NULL);
596                 if (error)
597                         return error;
598
599                 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
600                         memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
601                         /* Reset swap signature now */
602                         error = bio_write_page(swsusp_resume_block,
603                                                 &swsusp_header, NULL);
604                 } else {
605                         return -EINVAL;
606                 }
607                 if (error)
608                         blkdev_put(resume_bdev);
609                 else
610                         pr_debug("swsusp: Signature found, resuming\n");
611         } else {
612                 error = PTR_ERR(resume_bdev);
613         }
614
615         if (error)
616                 pr_debug("swsusp: Error %d check for resume file\n", error);
617
618         return error;
619 }
620
621 /**
622  *      swsusp_close - close swap device.
623  */
624
625 void swsusp_close(void)
626 {
627         if (IS_ERR(resume_bdev)) {
628                 pr_debug("swsusp: block device not initialised\n");
629                 return;
630         }
631
632         blkdev_put(resume_bdev);
633 }