8309d20b2563d2783395b91f45cad020f97329f0
[sfrench/cifs-2.6.git] / 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                 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
336 }
337
338 /**
339  *      swsusp_write - Write entire image and metadata.
340  *
341  *      It is important _NOT_ to umount filesystems at this point. We want
342  *      them synced (in case something goes wrong) but we DO not want to mark
343  *      filesystem clean: it is not. (And it does not matter, if we resume
344  *      correctly, we'll mark system clean, anyway.)
345  */
346
347 int swsusp_write(void)
348 {
349         struct swap_map_handle handle;
350         struct snapshot_handle snapshot;
351         struct swsusp_info *header;
352         int error;
353
354         if ((error = swsusp_swap_check())) {
355                 printk(KERN_ERR "swsusp: Cannot find swap device, try "
356                                 "swapon -a.\n");
357                 return error;
358         }
359         memset(&snapshot, 0, sizeof(struct snapshot_handle));
360         error = snapshot_read_next(&snapshot, PAGE_SIZE);
361         if (error < PAGE_SIZE)
362                 return error < 0 ? error : -EFAULT;
363         header = (struct swsusp_info *)data_of(snapshot);
364         if (!enough_swap(header->pages)) {
365                 printk(KERN_ERR "swsusp: Not enough free swap\n");
366                 return -ENOSPC;
367         }
368         error = get_swap_writer(&handle);
369         if (!error) {
370                 unsigned long start = handle.cur_swap;
371                 error = swap_write_page(&handle, header, NULL);
372                 if (!error)
373                         error = save_image(&handle, &snapshot,
374                                         header->pages - 1);
375                 if (!error) {
376                         flush_swap_writer(&handle);
377                         printk("S");
378                         error = mark_swapfiles(swp_entry(root_swap, start));
379                         printk("|\n");
380                 }
381         }
382         if (error)
383                 free_all_swap_pages(root_swap, handle.bitmap);
384         release_swap_writer(&handle);
385         return error;
386 }
387
388 static struct block_device *resume_bdev;
389
390 /**
391  *      submit - submit BIO request.
392  *      @rw:    READ or WRITE.
393  *      @off    physical offset of page.
394  *      @page:  page we're reading or writing.
395  *      @bio_chain: list of pending biod (for async reading)
396  *
397  *      Straight from the textbook - allocate and initialize the bio.
398  *      If we're reading, make sure the page is marked as dirty.
399  *      Then submit it and, if @bio_chain == NULL, wait.
400  */
401 static int submit(int rw, pgoff_t page_off, struct page *page,
402                         struct bio **bio_chain)
403 {
404         struct bio *bio;
405
406         bio = bio_alloc(GFP_ATOMIC, 1);
407         if (!bio)
408                 return -ENOMEM;
409         bio->bi_sector = page_off * (PAGE_SIZE >> 9);
410         bio->bi_bdev = resume_bdev;
411         bio->bi_end_io = end_swap_bio_read;
412
413         if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
414                 printk("swsusp: ERROR: adding page to bio at %ld\n", page_off);
415                 bio_put(bio);
416                 return -EFAULT;
417         }
418
419         lock_page(page);
420         bio_get(bio);
421
422         if (bio_chain == NULL) {
423                 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
424                 wait_on_page_locked(page);
425                 if (rw == READ)
426                         bio_set_pages_dirty(bio);
427                 bio_put(bio);
428         } else {
429                 get_page(page);
430                 bio->bi_private = *bio_chain;
431                 *bio_chain = bio;
432                 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
433         }
434         return 0;
435 }
436
437 static int bio_read_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
438 {
439         return submit(READ, page_off, virt_to_page(addr), bio_chain);
440 }
441
442 static int bio_write_page(pgoff_t page_off, void *addr)
443 {
444         return submit(WRITE, page_off, virt_to_page(addr), NULL);
445 }
446
447 /**
448  *      The following functions allow us to read data using a swap map
449  *      in a file-alike way
450  */
451
452 static void release_swap_reader(struct swap_map_handle *handle)
453 {
454         if (handle->cur)
455                 free_page((unsigned long)handle->cur);
456         handle->cur = NULL;
457 }
458
459 static int get_swap_reader(struct swap_map_handle *handle,
460                                       swp_entry_t start)
461 {
462         int error;
463
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;
473         }
474         handle->k = 0;
475         return 0;
476 }
477
478 static int swap_read_page(struct swap_map_handle *handle, void *buf,
479                                 struct bio **bio_chain)
480 {
481         unsigned long offset;
482         int error;
483
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);
500         }
501         return error;
502 }
503
504 /**
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)
508  */
509
510 static int load_image(struct swap_map_handle *handle,
511                       struct snapshot_handle *snapshot,
512                       unsigned int nr_to_read)
513 {
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;
521
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++;
543         }
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                 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 }