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