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