Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[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/swap.h>
26 #include <linux/swapops.h>
27 #include <linux/pm.h>
28
29 #include "power.h"
30
31 extern char resume_file[];
32
33 #define SWSUSP_SIG      "S1SUSPEND"
34
35 static struct swsusp_header {
36         char reserved[PAGE_SIZE - 20 - sizeof(swp_entry_t)];
37         swp_entry_t image;
38         char    orig_sig[10];
39         char    sig[10];
40 } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
41
42 /*
43  * Saving part...
44  */
45
46 static unsigned short root_swap = 0xffff;
47
48 static int mark_swapfiles(swp_entry_t start)
49 {
50         int error;
51
52         rw_swap_page_sync(READ,
53                           swp_entry(root_swap, 0),
54                           virt_to_page((unsigned long)&swsusp_header));
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,
61                                           swp_entry(root_swap, 0),
62                                           virt_to_page((unsigned long)
63                                                        &swsusp_header));
64         } else {
65                 pr_debug("swsusp: Partition is not swap space.\n");
66                 error = -ENODEV;
67         }
68         return error;
69 }
70
71 /**
72  *      swsusp_swap_check - check if the resume device is a swap device
73  *      and get its index (if so)
74  */
75
76 static int swsusp_swap_check(void) /* This is called before saving image */
77 {
78         int res = swap_type_of(swsusp_resume_device);
79
80         if (res >= 0) {
81                 root_swap = res;
82                 return 0;
83         }
84         return res;
85 }
86
87 /**
88  *      write_page - Write one page to given swap location.
89  *      @buf:           Address we're writing.
90  *      @offset:        Offset of the swap page we're writing to.
91  */
92
93 static int write_page(void *buf, unsigned long offset)
94 {
95         swp_entry_t entry;
96         int error = -ENOSPC;
97
98         if (offset) {
99                 entry = swp_entry(root_swap, offset);
100                 error = rw_swap_page_sync(WRITE, entry, virt_to_page(buf));
101         }
102         return error;
103 }
104
105 /*
106  *      The swap map is a data structure used for keeping track of each page
107  *      written to a swap partition.  It consists of many swap_map_page
108  *      structures that contain each an array of MAP_PAGE_SIZE swap entries.
109  *      These structures are stored on the swap and linked together with the
110  *      help of the .next_swap member.
111  *
112  *      The swap map is created during suspend.  The swap map pages are
113  *      allocated and populated one at a time, so we only need one memory
114  *      page to set up the entire structure.
115  *
116  *      During resume we also only need to use one swap_map_page structure
117  *      at a time.
118  */
119
120 #define MAP_PAGE_ENTRIES        (PAGE_SIZE / sizeof(long) - 1)
121
122 struct swap_map_page {
123         unsigned long           entries[MAP_PAGE_ENTRIES];
124         unsigned long           next_swap;
125 };
126
127 /**
128  *      The swap_map_handle structure is used for handling swap in
129  *      a file-alike way
130  */
131
132 struct swap_map_handle {
133         struct swap_map_page *cur;
134         unsigned long cur_swap;
135         struct bitmap_page *bitmap;
136         unsigned int k;
137 };
138
139 static void release_swap_writer(struct swap_map_handle *handle)
140 {
141         if (handle->cur)
142                 free_page((unsigned long)handle->cur);
143         handle->cur = NULL;
144         if (handle->bitmap)
145                 free_bitmap(handle->bitmap);
146         handle->bitmap = NULL;
147 }
148
149 static int get_swap_writer(struct swap_map_handle *handle)
150 {
151         handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
152         if (!handle->cur)
153                 return -ENOMEM;
154         handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0));
155         if (!handle->bitmap) {
156                 release_swap_writer(handle);
157                 return -ENOMEM;
158         }
159         handle->cur_swap = alloc_swap_page(root_swap, handle->bitmap);
160         if (!handle->cur_swap) {
161                 release_swap_writer(handle);
162                 return -ENOSPC;
163         }
164         handle->k = 0;
165         return 0;
166 }
167
168 static int swap_write_page(struct swap_map_handle *handle, void *buf)
169 {
170         int error;
171         unsigned long offset;
172
173         if (!handle->cur)
174                 return -EINVAL;
175         offset = alloc_swap_page(root_swap, handle->bitmap);
176         error = write_page(buf, offset);
177         if (error)
178                 return error;
179         handle->cur->entries[handle->k++] = offset;
180         if (handle->k >= MAP_PAGE_ENTRIES) {
181                 offset = alloc_swap_page(root_swap, handle->bitmap);
182                 if (!offset)
183                         return -ENOSPC;
184                 handle->cur->next_swap = offset;
185                 error = write_page(handle->cur, handle->cur_swap);
186                 if (error)
187                         return error;
188                 memset(handle->cur, 0, PAGE_SIZE);
189                 handle->cur_swap = offset;
190                 handle->k = 0;
191         }
192         return 0;
193 }
194
195 static int flush_swap_writer(struct swap_map_handle *handle)
196 {
197         if (handle->cur && handle->cur_swap)
198                 return write_page(handle->cur, handle->cur_swap);
199         else
200                 return -EINVAL;
201 }
202
203 /**
204  *      save_image - save the suspend image data
205  */
206
207 static int save_image(struct swap_map_handle *handle,
208                       struct snapshot_handle *snapshot,
209                       unsigned int nr_pages)
210 {
211         unsigned int m;
212         int ret;
213         int error = 0;
214
215         printk("Saving image data pages (%u pages) ...     ", nr_pages);
216         m = nr_pages / 100;
217         if (!m)
218                 m = 1;
219         nr_pages = 0;
220         do {
221                 ret = snapshot_read_next(snapshot, PAGE_SIZE);
222                 if (ret > 0) {
223                         error = swap_write_page(handle, data_of(*snapshot));
224                         if (error)
225                                 break;
226                         if (!(nr_pages % m))
227                                 printk("\b\b\b\b%3d%%", nr_pages / m);
228                         nr_pages++;
229                 }
230         } while (ret > 0);
231         if (!error)
232                 printk("\b\b\b\bdone\n");
233         return error;
234 }
235
236 /**
237  *      enough_swap - Make sure we have enough swap to save the image.
238  *
239  *      Returns TRUE or FALSE after checking the total amount of swap
240  *      space avaiable from the resume partition.
241  */
242
243 static int enough_swap(unsigned int nr_pages)
244 {
245         unsigned int free_swap = count_swap_pages(root_swap, 1);
246
247         pr_debug("swsusp: free swap pages: %u\n", free_swap);
248         return free_swap > (nr_pages + PAGES_FOR_IO +
249                 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
250 }
251
252 /**
253  *      swsusp_write - Write entire image and metadata.
254  *
255  *      It is important _NOT_ to umount filesystems at this point. We want
256  *      them synced (in case something goes wrong) but we DO not want to mark
257  *      filesystem clean: it is not. (And it does not matter, if we resume
258  *      correctly, we'll mark system clean, anyway.)
259  */
260
261 int swsusp_write(void)
262 {
263         struct swap_map_handle handle;
264         struct snapshot_handle snapshot;
265         struct swsusp_info *header;
266         int error;
267
268         if ((error = swsusp_swap_check())) {
269                 printk(KERN_ERR "swsusp: Cannot find swap device, try swapon -a.\n");
270                 return error;
271         }
272         memset(&snapshot, 0, sizeof(struct snapshot_handle));
273         error = snapshot_read_next(&snapshot, PAGE_SIZE);
274         if (error < PAGE_SIZE)
275                 return error < 0 ? error : -EFAULT;
276         header = (struct swsusp_info *)data_of(snapshot);
277         if (!enough_swap(header->pages)) {
278                 printk(KERN_ERR "swsusp: Not enough free swap\n");
279                 return -ENOSPC;
280         }
281         error = get_swap_writer(&handle);
282         if (!error) {
283                 unsigned long start = handle.cur_swap;
284                 error = swap_write_page(&handle, header);
285                 if (!error)
286                         error = save_image(&handle, &snapshot,
287                                         header->pages - 1);
288                 if (!error) {
289                         flush_swap_writer(&handle);
290                         printk("S");
291                         error = mark_swapfiles(swp_entry(root_swap, start));
292                         printk("|\n");
293                 }
294         }
295         if (error)
296                 free_all_swap_pages(root_swap, handle.bitmap);
297         release_swap_writer(&handle);
298         return error;
299 }
300
301 /*
302  *      Using bio to read from swap.
303  *      This code requires a bit more work than just using buffer heads
304  *      but, it is the recommended way for 2.5/2.6.
305  *      The following are to signal the beginning and end of I/O. Bios
306  *      finish asynchronously, while we want them to happen synchronously.
307  *      A simple atomic_t, and a wait loop take care of this problem.
308  */
309
310 static atomic_t io_done = ATOMIC_INIT(0);
311
312 static int end_io(struct bio *bio, unsigned int num, int err)
313 {
314         if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
315                 printk(KERN_ERR "I/O error reading swsusp image.\n");
316                 return -EIO;
317         }
318         atomic_set(&io_done, 0);
319         return 0;
320 }
321
322 static struct block_device *resume_bdev;
323
324 /**
325  *      submit - submit BIO request.
326  *      @rw:    READ or WRITE.
327  *      @off    physical offset of page.
328  *      @page:  page we're reading or writing.
329  *
330  *      Straight from the textbook - allocate and initialize the bio.
331  *      If we're writing, make sure the page is marked as dirty.
332  *      Then submit it and wait.
333  */
334
335 static int submit(int rw, pgoff_t page_off, void *page)
336 {
337         int error = 0;
338         struct bio *bio;
339
340         bio = bio_alloc(GFP_ATOMIC, 1);
341         if (!bio)
342                 return -ENOMEM;
343         bio->bi_sector = page_off * (PAGE_SIZE >> 9);
344         bio->bi_bdev = resume_bdev;
345         bio->bi_end_io = end_io;
346
347         if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
348                 printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
349                 error = -EFAULT;
350                 goto Done;
351         }
352
353         atomic_set(&io_done, 1);
354         submit_bio(rw | (1 << BIO_RW_SYNC), bio);
355         while (atomic_read(&io_done))
356                 yield();
357         if (rw == READ)
358                 bio_set_pages_dirty(bio);
359  Done:
360         bio_put(bio);
361         return error;
362 }
363
364 static int bio_read_page(pgoff_t page_off, void *page)
365 {
366         return submit(READ, page_off, page);
367 }
368
369 static int bio_write_page(pgoff_t page_off, void *page)
370 {
371         return submit(WRITE, page_off, page);
372 }
373
374 /**
375  *      The following functions allow us to read data using a swap map
376  *      in a file-alike way
377  */
378
379 static void release_swap_reader(struct swap_map_handle *handle)
380 {
381         if (handle->cur)
382                 free_page((unsigned long)handle->cur);
383         handle->cur = NULL;
384 }
385
386 static int get_swap_reader(struct swap_map_handle *handle,
387                                       swp_entry_t start)
388 {
389         int error;
390
391         if (!swp_offset(start))
392                 return -EINVAL;
393         handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
394         if (!handle->cur)
395                 return -ENOMEM;
396         error = bio_read_page(swp_offset(start), handle->cur);
397         if (error) {
398                 release_swap_reader(handle);
399                 return error;
400         }
401         handle->k = 0;
402         return 0;
403 }
404
405 static int swap_read_page(struct swap_map_handle *handle, void *buf)
406 {
407         unsigned long offset;
408         int error;
409
410         if (!handle->cur)
411                 return -EINVAL;
412         offset = handle->cur->entries[handle->k];
413         if (!offset)
414                 return -EFAULT;
415         error = bio_read_page(offset, buf);
416         if (error)
417                 return error;
418         if (++handle->k >= MAP_PAGE_ENTRIES) {
419                 handle->k = 0;
420                 offset = handle->cur->next_swap;
421                 if (!offset)
422                         release_swap_reader(handle);
423                 else
424                         error = bio_read_page(offset, handle->cur);
425         }
426         return error;
427 }
428
429 /**
430  *      load_image - load the image using the swap map handle
431  *      @handle and the snapshot handle @snapshot
432  *      (assume there are @nr_pages pages to load)
433  */
434
435 static int load_image(struct swap_map_handle *handle,
436                       struct snapshot_handle *snapshot,
437                       unsigned int nr_pages)
438 {
439         unsigned int m;
440         int ret;
441         int error = 0;
442
443         printk("Loading image data pages (%u pages) ...     ", nr_pages);
444         m = nr_pages / 100;
445         if (!m)
446                 m = 1;
447         nr_pages = 0;
448         do {
449                 ret = snapshot_write_next(snapshot, PAGE_SIZE);
450                 if (ret > 0) {
451                         error = swap_read_page(handle, data_of(*snapshot));
452                         if (error)
453                                 break;
454                         if (!(nr_pages % m))
455                                 printk("\b\b\b\b%3d%%", nr_pages / m);
456                         nr_pages++;
457                 }
458         } while (ret > 0);
459         if (!error) {
460                 printk("\b\b\b\bdone\n");
461                 if (!snapshot_image_loaded(snapshot))
462                         error = -ENODATA;
463         }
464         return error;
465 }
466
467 int swsusp_read(void)
468 {
469         int error;
470         struct swap_map_handle handle;
471         struct snapshot_handle snapshot;
472         struct swsusp_info *header;
473
474         if (IS_ERR(resume_bdev)) {
475                 pr_debug("swsusp: block device not initialised\n");
476                 return PTR_ERR(resume_bdev);
477         }
478
479         memset(&snapshot, 0, sizeof(struct snapshot_handle));
480         error = snapshot_write_next(&snapshot, PAGE_SIZE);
481         if (error < PAGE_SIZE)
482                 return error < 0 ? error : -EFAULT;
483         header = (struct swsusp_info *)data_of(snapshot);
484         error = get_swap_reader(&handle, swsusp_header.image);
485         if (!error)
486                 error = swap_read_page(&handle, header);
487         if (!error)
488                 error = load_image(&handle, &snapshot, header->pages - 1);
489         release_swap_reader(&handle);
490
491         blkdev_put(resume_bdev);
492
493         if (!error)
494                 pr_debug("swsusp: Reading resume file was successful\n");
495         else
496                 pr_debug("swsusp: Error %d resuming\n", error);
497         return error;
498 }
499
500 /**
501  *      swsusp_check - Check for swsusp signature in the resume device
502  */
503
504 int swsusp_check(void)
505 {
506         int error;
507
508         resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
509         if (!IS_ERR(resume_bdev)) {
510                 set_blocksize(resume_bdev, PAGE_SIZE);
511                 memset(&swsusp_header, 0, sizeof(swsusp_header));
512                 if ((error = bio_read_page(0, &swsusp_header)))
513                         return error;
514                 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
515                         memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
516                         /* Reset swap signature now */
517                         error = bio_write_page(0, &swsusp_header);
518                 } else {
519                         return -EINVAL;
520                 }
521                 if (error)
522                         blkdev_put(resume_bdev);
523                 else
524                         pr_debug("swsusp: Signature found, resuming\n");
525         } else {
526                 error = PTR_ERR(resume_bdev);
527         }
528
529         if (error)
530                 pr_debug("swsusp: Error %d check for resume file\n", error);
531
532         return error;
533 }
534
535 /**
536  *      swsusp_close - close swap device.
537  */
538
539 void swsusp_close(void)
540 {
541         if (IS_ERR(resume_bdev)) {
542                 pr_debug("swsusp: block device not initialised\n");
543                 return;
544         }
545
546         blkdev_put(resume_bdev);
547 }