]> err.no Git - linux-2.6/blob - kernel/power/swap.c
[PATCH] swsusp: add write-speed instrumentation
[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 void show_speed(struct timeval *start, struct timeval *stop,
150                         unsigned nr_pages, char *msg)
151 {
152         s64 elapsed_centisecs64;
153         int centisecs;
154         int k;
155         int kps;
156
157         elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
158         do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
159         centisecs = elapsed_centisecs64;
160         if (centisecs == 0)
161                 centisecs = 1;  /* avoid div-by-zero */
162         k = nr_pages * (PAGE_SIZE / 1024);
163         kps = (k * 100) / centisecs;
164         printk("%s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n", msg, k,
165                         centisecs / 100, centisecs % 100,
166                         kps / 1000, (kps % 1000) / 10);
167 }
168
169 static int get_swap_writer(struct swap_map_handle *handle)
170 {
171         handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
172         if (!handle->cur)
173                 return -ENOMEM;
174         handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0));
175         if (!handle->bitmap) {
176                 release_swap_writer(handle);
177                 return -ENOMEM;
178         }
179         handle->cur_swap = alloc_swap_page(root_swap, handle->bitmap);
180         if (!handle->cur_swap) {
181                 release_swap_writer(handle);
182                 return -ENOSPC;
183         }
184         handle->k = 0;
185         return 0;
186 }
187
188 static int swap_write_page(struct swap_map_handle *handle, void *buf)
189 {
190         int error;
191         unsigned long offset;
192
193         if (!handle->cur)
194                 return -EINVAL;
195         offset = alloc_swap_page(root_swap, handle->bitmap);
196         error = write_page(buf, offset);
197         if (error)
198                 return error;
199         handle->cur->entries[handle->k++] = offset;
200         if (handle->k >= MAP_PAGE_ENTRIES) {
201                 offset = alloc_swap_page(root_swap, handle->bitmap);
202                 if (!offset)
203                         return -ENOSPC;
204                 handle->cur->next_swap = offset;
205                 error = write_page(handle->cur, handle->cur_swap);
206                 if (error)
207                         return error;
208                 memset(handle->cur, 0, PAGE_SIZE);
209                 handle->cur_swap = offset;
210                 handle->k = 0;
211         }
212         return 0;
213 }
214
215 static int flush_swap_writer(struct swap_map_handle *handle)
216 {
217         if (handle->cur && handle->cur_swap)
218                 return write_page(handle->cur, handle->cur_swap);
219         else
220                 return -EINVAL;
221 }
222
223 /**
224  *      save_image - save the suspend image data
225  */
226
227 static int save_image(struct swap_map_handle *handle,
228                       struct snapshot_handle *snapshot,
229                       unsigned int nr_to_write)
230 {
231         unsigned int m;
232         int ret;
233         int error = 0;
234         int nr_pages;
235         struct timeval start;
236         struct timeval stop;
237
238         printk("Saving image data pages (%u pages) ...     ", nr_to_write);
239         m = nr_to_write / 100;
240         if (!m)
241                 m = 1;
242         nr_pages = 0;
243         do_gettimeofday(&start);
244         do {
245                 ret = snapshot_read_next(snapshot, PAGE_SIZE);
246                 if (ret > 0) {
247                         error = swap_write_page(handle, data_of(*snapshot));
248                         if (error)
249                                 break;
250                         if (!(nr_pages % m))
251                                 printk("\b\b\b\b%3d%%", nr_pages / m);
252                         nr_pages++;
253                 }
254         } while (ret > 0);
255         do_gettimeofday(&stop);
256         if (!error)
257                 printk("\b\b\b\bdone\n");
258         show_speed(&start, &stop, nr_to_write, "Wrote");
259         return error;
260 }
261
262 /**
263  *      enough_swap - Make sure we have enough swap to save the image.
264  *
265  *      Returns TRUE or FALSE after checking the total amount of swap
266  *      space avaiable from the resume partition.
267  */
268
269 static int enough_swap(unsigned int nr_pages)
270 {
271         unsigned int free_swap = count_swap_pages(root_swap, 1);
272
273         pr_debug("swsusp: free swap pages: %u\n", free_swap);
274         return free_swap > (nr_pages + PAGES_FOR_IO +
275                 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
276 }
277
278 /**
279  *      swsusp_write - Write entire image and metadata.
280  *
281  *      It is important _NOT_ to umount filesystems at this point. We want
282  *      them synced (in case something goes wrong) but we DO not want to mark
283  *      filesystem clean: it is not. (And it does not matter, if we resume
284  *      correctly, we'll mark system clean, anyway.)
285  */
286
287 int swsusp_write(void)
288 {
289         struct swap_map_handle handle;
290         struct snapshot_handle snapshot;
291         struct swsusp_info *header;
292         int error;
293
294         if ((error = swsusp_swap_check())) {
295                 printk(KERN_ERR "swsusp: Cannot find swap device, try swapon -a.\n");
296                 return error;
297         }
298         memset(&snapshot, 0, sizeof(struct snapshot_handle));
299         error = snapshot_read_next(&snapshot, PAGE_SIZE);
300         if (error < PAGE_SIZE)
301                 return error < 0 ? error : -EFAULT;
302         header = (struct swsusp_info *)data_of(snapshot);
303         if (!enough_swap(header->pages)) {
304                 printk(KERN_ERR "swsusp: Not enough free swap\n");
305                 return -ENOSPC;
306         }
307         error = get_swap_writer(&handle);
308         if (!error) {
309                 unsigned long start = handle.cur_swap;
310                 error = swap_write_page(&handle, header);
311                 if (!error)
312                         error = save_image(&handle, &snapshot,
313                                         header->pages - 1);
314                 if (!error) {
315                         flush_swap_writer(&handle);
316                         printk("S");
317                         error = mark_swapfiles(swp_entry(root_swap, start));
318                         printk("|\n");
319                 }
320         }
321         if (error)
322                 free_all_swap_pages(root_swap, handle.bitmap);
323         release_swap_writer(&handle);
324         return error;
325 }
326
327 /*
328  *      Using bio to read from swap.
329  *      This code requires a bit more work than just using buffer heads
330  *      but, it is the recommended way for 2.5/2.6.
331  *      The following are to signal the beginning and end of I/O. Bios
332  *      finish asynchronously, while we want them to happen synchronously.
333  *      A simple atomic_t, and a wait loop take care of this problem.
334  */
335
336 static atomic_t io_done = ATOMIC_INIT(0);
337
338 static int end_io(struct bio *bio, unsigned int num, int err)
339 {
340         if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
341                 printk(KERN_ERR "I/O error reading swsusp image.\n");
342                 return -EIO;
343         }
344         atomic_set(&io_done, 0);
345         return 0;
346 }
347
348 static struct block_device *resume_bdev;
349
350 /**
351  *      submit - submit BIO request.
352  *      @rw:    READ or WRITE.
353  *      @off    physical offset of page.
354  *      @page:  page we're reading or writing.
355  *
356  *      Straight from the textbook - allocate and initialize the bio.
357  *      If we're writing, make sure the page is marked as dirty.
358  *      Then submit it and wait.
359  */
360
361 static int submit(int rw, pgoff_t page_off, void *page)
362 {
363         int error = 0;
364         struct bio *bio;
365
366         bio = bio_alloc(GFP_ATOMIC, 1);
367         if (!bio)
368                 return -ENOMEM;
369         bio->bi_sector = page_off * (PAGE_SIZE >> 9);
370         bio->bi_bdev = resume_bdev;
371         bio->bi_end_io = end_io;
372
373         if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
374                 printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
375                 error = -EFAULT;
376                 goto Done;
377         }
378
379         atomic_set(&io_done, 1);
380         submit_bio(rw | (1 << BIO_RW_SYNC), bio);
381         while (atomic_read(&io_done))
382                 yield();
383         if (rw == READ)
384                 bio_set_pages_dirty(bio);
385  Done:
386         bio_put(bio);
387         return error;
388 }
389
390 static int bio_read_page(pgoff_t page_off, void *page)
391 {
392         return submit(READ, page_off, page);
393 }
394
395 static int bio_write_page(pgoff_t page_off, void *page)
396 {
397         return submit(WRITE, page_off, page);
398 }
399
400 /**
401  *      The following functions allow us to read data using a swap map
402  *      in a file-alike way
403  */
404
405 static void release_swap_reader(struct swap_map_handle *handle)
406 {
407         if (handle->cur)
408                 free_page((unsigned long)handle->cur);
409         handle->cur = NULL;
410 }
411
412 static int get_swap_reader(struct swap_map_handle *handle,
413                                       swp_entry_t start)
414 {
415         int error;
416
417         if (!swp_offset(start))
418                 return -EINVAL;
419         handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
420         if (!handle->cur)
421                 return -ENOMEM;
422         error = bio_read_page(swp_offset(start), handle->cur);
423         if (error) {
424                 release_swap_reader(handle);
425                 return error;
426         }
427         handle->k = 0;
428         return 0;
429 }
430
431 static int swap_read_page(struct swap_map_handle *handle, void *buf)
432 {
433         unsigned long offset;
434         int error;
435
436         if (!handle->cur)
437                 return -EINVAL;
438         offset = handle->cur->entries[handle->k];
439         if (!offset)
440                 return -EFAULT;
441         error = bio_read_page(offset, buf);
442         if (error)
443                 return error;
444         if (++handle->k >= MAP_PAGE_ENTRIES) {
445                 handle->k = 0;
446                 offset = handle->cur->next_swap;
447                 if (!offset)
448                         release_swap_reader(handle);
449                 else
450                         error = bio_read_page(offset, handle->cur);
451         }
452         return error;
453 }
454
455 /**
456  *      load_image - load the image using the swap map handle
457  *      @handle and the snapshot handle @snapshot
458  *      (assume there are @nr_pages pages to load)
459  */
460
461 static int load_image(struct swap_map_handle *handle,
462                       struct snapshot_handle *snapshot,
463                       unsigned int nr_pages)
464 {
465         unsigned int m;
466         int ret;
467         int error = 0;
468
469         printk("Loading image data pages (%u pages) ...     ", nr_pages);
470         m = nr_pages / 100;
471         if (!m)
472                 m = 1;
473         nr_pages = 0;
474         do {
475                 ret = snapshot_write_next(snapshot, PAGE_SIZE);
476                 if (ret > 0) {
477                         error = swap_read_page(handle, data_of(*snapshot));
478                         if (error)
479                                 break;
480                         if (!(nr_pages % m))
481                                 printk("\b\b\b\b%3d%%", nr_pages / m);
482                         nr_pages++;
483                 }
484         } while (ret > 0);
485         if (!error) {
486                 printk("\b\b\b\bdone\n");
487                 if (!snapshot_image_loaded(snapshot))
488                         error = -ENODATA;
489         }
490         return error;
491 }
492
493 int swsusp_read(void)
494 {
495         int error;
496         struct swap_map_handle handle;
497         struct snapshot_handle snapshot;
498         struct swsusp_info *header;
499
500         if (IS_ERR(resume_bdev)) {
501                 pr_debug("swsusp: block device not initialised\n");
502                 return PTR_ERR(resume_bdev);
503         }
504
505         memset(&snapshot, 0, sizeof(struct snapshot_handle));
506         error = snapshot_write_next(&snapshot, PAGE_SIZE);
507         if (error < PAGE_SIZE)
508                 return error < 0 ? error : -EFAULT;
509         header = (struct swsusp_info *)data_of(snapshot);
510         error = get_swap_reader(&handle, swsusp_header.image);
511         if (!error)
512                 error = swap_read_page(&handle, header);
513         if (!error)
514                 error = load_image(&handle, &snapshot, header->pages - 1);
515         release_swap_reader(&handle);
516
517         blkdev_put(resume_bdev);
518
519         if (!error)
520                 pr_debug("swsusp: Reading resume file was successful\n");
521         else
522                 pr_debug("swsusp: Error %d resuming\n", error);
523         return error;
524 }
525
526 /**
527  *      swsusp_check - Check for swsusp signature in the resume device
528  */
529
530 int swsusp_check(void)
531 {
532         int error;
533
534         resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
535         if (!IS_ERR(resume_bdev)) {
536                 set_blocksize(resume_bdev, PAGE_SIZE);
537                 memset(&swsusp_header, 0, sizeof(swsusp_header));
538                 if ((error = bio_read_page(0, &swsusp_header)))
539                         return error;
540                 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
541                         memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
542                         /* Reset swap signature now */
543                         error = bio_write_page(0, &swsusp_header);
544                 } else {
545                         return -EINVAL;
546                 }
547                 if (error)
548                         blkdev_put(resume_bdev);
549                 else
550                         pr_debug("swsusp: Signature found, resuming\n");
551         } else {
552                 error = PTR_ERR(resume_bdev);
553         }
554
555         if (error)
556                 pr_debug("swsusp: Error %d check for resume file\n", error);
557
558         return error;
559 }
560
561 /**
562  *      swsusp_close - close swap device.
563  */
564
565 void swsusp_close(void)
566 {
567         if (IS_ERR(resume_bdev)) {
568                 pr_debug("swsusp: block device not initialised\n");
569                 return;
570         }
571
572         blkdev_put(resume_bdev);
573 }