Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / kernel / power / snapshot.c
1 /*
2  * linux/kernel/power/snapshot.c
3  *
4  * This file provide system snapshot/restore functionality.
5  *
6  * Copyright (C) 1998-2005 Pavel Machek <pavel@suse.cz>
7  *
8  * This file is released under the GPLv2, and is based on swsusp.c.
9  *
10  */
11
12
13 #include <linux/version.h>
14 #include <linux/module.h>
15 #include <linux/mm.h>
16 #include <linux/suspend.h>
17 #include <linux/smp_lock.h>
18 #include <linux/delay.h>
19 #include <linux/bitops.h>
20 #include <linux/spinlock.h>
21 #include <linux/kernel.h>
22 #include <linux/pm.h>
23 #include <linux/device.h>
24 #include <linux/bootmem.h>
25 #include <linux/syscalls.h>
26 #include <linux/console.h>
27 #include <linux/highmem.h>
28
29 #include <asm/uaccess.h>
30 #include <asm/mmu_context.h>
31 #include <asm/pgtable.h>
32 #include <asm/tlbflush.h>
33 #include <asm/io.h>
34
35 #include "power.h"
36
37 struct pbe *pagedir_nosave;
38 static unsigned int nr_copy_pages;
39 static unsigned int nr_meta_pages;
40 static unsigned long *buffer;
41
42 #ifdef CONFIG_HIGHMEM
43 unsigned int count_highmem_pages(void)
44 {
45         struct zone *zone;
46         unsigned long zone_pfn;
47         unsigned int n = 0;
48
49         for_each_zone (zone)
50                 if (is_highmem(zone)) {
51                         mark_free_pages(zone);
52                         for (zone_pfn = 0; zone_pfn < zone->spanned_pages; zone_pfn++) {
53                                 struct page *page;
54                                 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
55                                 if (!pfn_valid(pfn))
56                                         continue;
57                                 page = pfn_to_page(pfn);
58                                 if (PageReserved(page))
59                                         continue;
60                                 if (PageNosaveFree(page))
61                                         continue;
62                                 n++;
63                         }
64                 }
65         return n;
66 }
67
68 struct highmem_page {
69         char *data;
70         struct page *page;
71         struct highmem_page *next;
72 };
73
74 static struct highmem_page *highmem_copy;
75
76 static int save_highmem_zone(struct zone *zone)
77 {
78         unsigned long zone_pfn;
79         mark_free_pages(zone);
80         for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
81                 struct page *page;
82                 struct highmem_page *save;
83                 void *kaddr;
84                 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
85
86                 if (!(pfn%10000))
87                         printk(".");
88                 if (!pfn_valid(pfn))
89                         continue;
90                 page = pfn_to_page(pfn);
91                 /*
92                  * This condition results from rvmalloc() sans vmalloc_32()
93                  * and architectural memory reservations. This should be
94                  * corrected eventually when the cases giving rise to this
95                  * are better understood.
96                  */
97                 if (PageReserved(page))
98                         continue;
99                 BUG_ON(PageNosave(page));
100                 if (PageNosaveFree(page))
101                         continue;
102                 save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
103                 if (!save)
104                         return -ENOMEM;
105                 save->next = highmem_copy;
106                 save->page = page;
107                 save->data = (void *) get_zeroed_page(GFP_ATOMIC);
108                 if (!save->data) {
109                         kfree(save);
110                         return -ENOMEM;
111                 }
112                 kaddr = kmap_atomic(page, KM_USER0);
113                 memcpy(save->data, kaddr, PAGE_SIZE);
114                 kunmap_atomic(kaddr, KM_USER0);
115                 highmem_copy = save;
116         }
117         return 0;
118 }
119
120 int save_highmem(void)
121 {
122         struct zone *zone;
123         int res = 0;
124
125         pr_debug("swsusp: Saving Highmem");
126         drain_local_pages();
127         for_each_zone (zone) {
128                 if (is_highmem(zone))
129                         res = save_highmem_zone(zone);
130                 if (res)
131                         return res;
132         }
133         printk("\n");
134         return 0;
135 }
136
137 int restore_highmem(void)
138 {
139         printk("swsusp: Restoring Highmem\n");
140         while (highmem_copy) {
141                 struct highmem_page *save = highmem_copy;
142                 void *kaddr;
143                 highmem_copy = save->next;
144
145                 kaddr = kmap_atomic(save->page, KM_USER0);
146                 memcpy(kaddr, save->data, PAGE_SIZE);
147                 kunmap_atomic(kaddr, KM_USER0);
148                 free_page((long) save->data);
149                 kfree(save);
150         }
151         return 0;
152 }
153 #else
154 static inline unsigned int count_highmem_pages(void) {return 0;}
155 static inline int save_highmem(void) {return 0;}
156 static inline int restore_highmem(void) {return 0;}
157 #endif
158
159 static inline int pfn_is_nosave(unsigned long pfn)
160 {
161         unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
162         unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
163         return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
164 }
165
166 /**
167  *      saveable - Determine whether a page should be cloned or not.
168  *      @pfn:   The page
169  *
170  *      We save a page if it isn't Nosave, and is not in the range of pages
171  *      statically defined as 'unsaveable', and it
172  *      isn't a part of a free chunk of pages.
173  */
174
175 static struct page *saveable_page(unsigned long pfn)
176 {
177         struct page *page;
178
179         if (!pfn_valid(pfn))
180                 return NULL;
181
182         page = pfn_to_page(pfn);
183
184         if (PageNosave(page))
185                 return NULL;
186         if (PageReserved(page) && pfn_is_nosave(pfn))
187                 return NULL;
188         if (PageNosaveFree(page))
189                 return NULL;
190
191         return page;
192 }
193
194 unsigned int count_data_pages(void)
195 {
196         struct zone *zone;
197         unsigned long pfn, max_zone_pfn;
198         unsigned int n = 0;
199
200         for_each_zone (zone) {
201                 if (is_highmem(zone))
202                         continue;
203                 mark_free_pages(zone);
204                 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
205                 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
206                         n += !!saveable_page(pfn);
207         }
208         return n;
209 }
210
211 static void copy_data_pages(struct pbe *pblist)
212 {
213         struct zone *zone;
214         unsigned long pfn, max_zone_pfn;
215         struct pbe *pbe, *p;
216
217         pbe = pblist;
218         for_each_zone (zone) {
219                 if (is_highmem(zone))
220                         continue;
221                 mark_free_pages(zone);
222                 /* This is necessary for swsusp_free() */
223                 for_each_pb_page (p, pblist)
224                         SetPageNosaveFree(virt_to_page(p));
225                 for_each_pbe (p, pblist)
226                         SetPageNosaveFree(virt_to_page(p->address));
227                 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
228                 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
229                         struct page *page = saveable_page(pfn);
230
231                         if (page) {
232                                 long *src, *dst;
233                                 int n;
234
235                                 BUG_ON(!pbe);
236                                 pbe->orig_address = (unsigned long)page_address(page);
237                                 /* copy_page and memcpy are not usable for copying task structs. */
238                                 dst = (long *)pbe->address;
239                                 src = (long *)pbe->orig_address;
240                                 for (n = PAGE_SIZE / sizeof(long); n; n--)
241                                         *dst++ = *src++;
242                                 pbe = pbe->next;
243                         }
244                 }
245         }
246         BUG_ON(pbe);
247 }
248
249
250 /**
251  *      free_pagedir - free pages allocated with alloc_pagedir()
252  */
253
254 static void free_pagedir(struct pbe *pblist, int clear_nosave_free)
255 {
256         struct pbe *pbe;
257
258         while (pblist) {
259                 pbe = (pblist + PB_PAGE_SKIP)->next;
260                 ClearPageNosave(virt_to_page(pblist));
261                 if (clear_nosave_free)
262                         ClearPageNosaveFree(virt_to_page(pblist));
263                 free_page((unsigned long)pblist);
264                 pblist = pbe;
265         }
266 }
267
268 /**
269  *      fill_pb_page - Create a list of PBEs on a given memory page
270  */
271
272 static inline void fill_pb_page(struct pbe *pbpage)
273 {
274         struct pbe *p;
275
276         p = pbpage;
277         pbpage += PB_PAGE_SKIP;
278         do
279                 p->next = p + 1;
280         while (++p < pbpage);
281 }
282
283 /**
284  *      create_pbe_list - Create a list of PBEs on top of a given chain
285  *      of memory pages allocated with alloc_pagedir()
286  */
287
288 static inline void create_pbe_list(struct pbe *pblist, unsigned int nr_pages)
289 {
290         struct pbe *pbpage, *p;
291         unsigned int num = PBES_PER_PAGE;
292
293         for_each_pb_page (pbpage, pblist) {
294                 if (num >= nr_pages)
295                         break;
296
297                 fill_pb_page(pbpage);
298                 num += PBES_PER_PAGE;
299         }
300         if (pbpage) {
301                 for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
302                         p->next = p + 1;
303                 p->next = NULL;
304         }
305 }
306
307 static unsigned int unsafe_pages;
308
309 /**
310  *      @safe_needed - on resume, for storing the PBE list and the image,
311  *      we can only use memory pages that do not conflict with the pages
312  *      used before suspend.
313  *
314  *      The unsafe pages are marked with the PG_nosave_free flag
315  *      and we count them using unsafe_pages
316  */
317
318 static void *alloc_image_page(gfp_t gfp_mask, int safe_needed)
319 {
320         void *res;
321
322         res = (void *)get_zeroed_page(gfp_mask);
323         if (safe_needed)
324                 while (res && PageNosaveFree(virt_to_page(res))) {
325                         /* The page is unsafe, mark it for swsusp_free() */
326                         SetPageNosave(virt_to_page(res));
327                         unsafe_pages++;
328                         res = (void *)get_zeroed_page(gfp_mask);
329                 }
330         if (res) {
331                 SetPageNosave(virt_to_page(res));
332                 SetPageNosaveFree(virt_to_page(res));
333         }
334         return res;
335 }
336
337 unsigned long get_safe_page(gfp_t gfp_mask)
338 {
339         return (unsigned long)alloc_image_page(gfp_mask, 1);
340 }
341
342 /**
343  *      alloc_pagedir - Allocate the page directory.
344  *
345  *      First, determine exactly how many pages we need and
346  *      allocate them.
347  *
348  *      We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
349  *      struct pbe elements (pbes) and the last element in the page points
350  *      to the next page.
351  *
352  *      On each page we set up a list of struct_pbe elements.
353  */
354
355 static struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask,
356                                  int safe_needed)
357 {
358         unsigned int num;
359         struct pbe *pblist, *pbe;
360
361         if (!nr_pages)
362                 return NULL;
363
364         pblist = alloc_image_page(gfp_mask, safe_needed);
365         /* FIXME: rewrite this ugly loop */
366         for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
367                         pbe = pbe->next, num += PBES_PER_PAGE) {
368                 pbe += PB_PAGE_SKIP;
369                 pbe->next = alloc_image_page(gfp_mask, safe_needed);
370         }
371         if (!pbe) { /* get_zeroed_page() failed */
372                 free_pagedir(pblist, 1);
373                 pblist = NULL;
374         } else
375                 create_pbe_list(pblist, nr_pages);
376         return pblist;
377 }
378
379 /**
380  * Free pages we allocated for suspend. Suspend pages are alocated
381  * before atomic copy, so we need to free them after resume.
382  */
383
384 void swsusp_free(void)
385 {
386         struct zone *zone;
387         unsigned long pfn, max_zone_pfn;
388
389         for_each_zone(zone) {
390                 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
391                 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
392                         if (pfn_valid(pfn)) {
393                                 struct page *page = pfn_to_page(pfn);
394
395                                 if (PageNosave(page) && PageNosaveFree(page)) {
396                                         ClearPageNosave(page);
397                                         ClearPageNosaveFree(page);
398                                         free_page((long) page_address(page));
399                                 }
400                         }
401         }
402         nr_copy_pages = 0;
403         nr_meta_pages = 0;
404         pagedir_nosave = NULL;
405         buffer = NULL;
406 }
407
408
409 /**
410  *      enough_free_mem - Make sure we enough free memory to snapshot.
411  *
412  *      Returns TRUE or FALSE after checking the number of available
413  *      free pages.
414  */
415
416 static int enough_free_mem(unsigned int nr_pages)
417 {
418         struct zone *zone;
419         unsigned int n = 0;
420
421         for_each_zone (zone)
422                 if (!is_highmem(zone))
423                         n += zone->free_pages;
424         pr_debug("swsusp: available memory: %u pages\n", n);
425         return n > (nr_pages + PAGES_FOR_IO +
426                 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
427 }
428
429 static int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed)
430 {
431         struct pbe *p;
432
433         for_each_pbe (p, pblist) {
434                 p->address = (unsigned long)alloc_image_page(gfp_mask, safe_needed);
435                 if (!p->address)
436                         return -ENOMEM;
437         }
438         return 0;
439 }
440
441 static struct pbe *swsusp_alloc(unsigned int nr_pages)
442 {
443         struct pbe *pblist;
444
445         if (!(pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, 0))) {
446                 printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
447                 return NULL;
448         }
449
450         if (alloc_data_pages(pblist, GFP_ATOMIC | __GFP_COLD, 0)) {
451                 printk(KERN_ERR "suspend: Allocating image pages failed.\n");
452                 swsusp_free();
453                 return NULL;
454         }
455
456         return pblist;
457 }
458
459 asmlinkage int swsusp_save(void)
460 {
461         unsigned int nr_pages;
462
463         pr_debug("swsusp: critical section: \n");
464
465         drain_local_pages();
466         nr_pages = count_data_pages();
467         printk("swsusp: Need to copy %u pages\n", nr_pages);
468
469         pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
470                  nr_pages,
471                  (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
472                  PAGES_FOR_IO, nr_free_pages());
473
474         if (!enough_free_mem(nr_pages)) {
475                 printk(KERN_ERR "swsusp: Not enough free memory\n");
476                 return -ENOMEM;
477         }
478
479         pagedir_nosave = swsusp_alloc(nr_pages);
480         if (!pagedir_nosave)
481                 return -ENOMEM;
482
483         /* During allocating of suspend pagedir, new cold pages may appear.
484          * Kill them.
485          */
486         drain_local_pages();
487         copy_data_pages(pagedir_nosave);
488
489         /*
490          * End of critical section. From now on, we can write to memory,
491          * but we should not touch disk. This specially means we must _not_
492          * touch swap space! Except we must write out our image of course.
493          */
494
495         nr_copy_pages = nr_pages;
496         nr_meta_pages = (nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT;
497
498         printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
499         return 0;
500 }
501
502 static void init_header(struct swsusp_info *info)
503 {
504         memset(info, 0, sizeof(struct swsusp_info));
505         info->version_code = LINUX_VERSION_CODE;
506         info->num_physpages = num_physpages;
507         memcpy(&info->uts, &system_utsname, sizeof(system_utsname));
508         info->cpus = num_online_cpus();
509         info->image_pages = nr_copy_pages;
510         info->pages = nr_copy_pages + nr_meta_pages + 1;
511         info->size = info->pages;
512         info->size <<= PAGE_SHIFT;
513 }
514
515 /**
516  *      pack_orig_addresses - the .orig_address fields of the PBEs from the
517  *      list starting at @pbe are stored in the array @buf[] (1 page)
518  */
519
520 static inline struct pbe *pack_orig_addresses(unsigned long *buf, struct pbe *pbe)
521 {
522         int j;
523
524         for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
525                 buf[j] = pbe->orig_address;
526                 pbe = pbe->next;
527         }
528         if (!pbe)
529                 for (; j < PAGE_SIZE / sizeof(long); j++)
530                         buf[j] = 0;
531         return pbe;
532 }
533
534 /**
535  *      snapshot_read_next - used for reading the system memory snapshot.
536  *
537  *      On the first call to it @handle should point to a zeroed
538  *      snapshot_handle structure.  The structure gets updated and a pointer
539  *      to it should be passed to this function every next time.
540  *
541  *      The @count parameter should contain the number of bytes the caller
542  *      wants to read from the snapshot.  It must not be zero.
543  *
544  *      On success the function returns a positive number.  Then, the caller
545  *      is allowed to read up to the returned number of bytes from the memory
546  *      location computed by the data_of() macro.  The number returned
547  *      may be smaller than @count, but this only happens if the read would
548  *      cross a page boundary otherwise.
549  *
550  *      The function returns 0 to indicate the end of data stream condition,
551  *      and a negative number is returned on error.  In such cases the
552  *      structure pointed to by @handle is not updated and should not be used
553  *      any more.
554  */
555
556 int snapshot_read_next(struct snapshot_handle *handle, size_t count)
557 {
558         if (handle->cur > nr_meta_pages + nr_copy_pages)
559                 return 0;
560         if (!buffer) {
561                 /* This makes the buffer be freed by swsusp_free() */
562                 buffer = alloc_image_page(GFP_ATOMIC, 0);
563                 if (!buffer)
564                         return -ENOMEM;
565         }
566         if (!handle->offset) {
567                 init_header((struct swsusp_info *)buffer);
568                 handle->buffer = buffer;
569                 handle->pbe = pagedir_nosave;
570         }
571         if (handle->prev < handle->cur) {
572                 if (handle->cur <= nr_meta_pages) {
573                         handle->pbe = pack_orig_addresses(buffer, handle->pbe);
574                         if (!handle->pbe)
575                                 handle->pbe = pagedir_nosave;
576                 } else {
577                         handle->buffer = (void *)handle->pbe->address;
578                         handle->pbe = handle->pbe->next;
579                 }
580                 handle->prev = handle->cur;
581         }
582         handle->buf_offset = handle->cur_offset;
583         if (handle->cur_offset + count >= PAGE_SIZE) {
584                 count = PAGE_SIZE - handle->cur_offset;
585                 handle->cur_offset = 0;
586                 handle->cur++;
587         } else {
588                 handle->cur_offset += count;
589         }
590         handle->offset += count;
591         return count;
592 }
593
594 /**
595  *      mark_unsafe_pages - mark the pages that cannot be used for storing
596  *      the image during resume, because they conflict with the pages that
597  *      had been used before suspend
598  */
599
600 static int mark_unsafe_pages(struct pbe *pblist)
601 {
602         struct zone *zone;
603         unsigned long pfn, max_zone_pfn;
604         struct pbe *p;
605
606         if (!pblist) /* a sanity check */
607                 return -EINVAL;
608
609         /* Clear page flags */
610         for_each_zone (zone) {
611                 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
612                 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
613                         if (pfn_valid(pfn))
614                                 ClearPageNosaveFree(pfn_to_page(pfn));
615         }
616
617         /* Mark orig addresses */
618         for_each_pbe (p, pblist) {
619                 if (virt_addr_valid(p->orig_address))
620                         SetPageNosaveFree(virt_to_page(p->orig_address));
621                 else
622                         return -EFAULT;
623         }
624
625         unsafe_pages = 0;
626
627         return 0;
628 }
629
630 static void copy_page_backup_list(struct pbe *dst, struct pbe *src)
631 {
632         /* We assume both lists contain the same number of elements */
633         while (src) {
634                 dst->orig_address = src->orig_address;
635                 dst = dst->next;
636                 src = src->next;
637         }
638 }
639
640 static int check_header(struct swsusp_info *info)
641 {
642         char *reason = NULL;
643
644         if (info->version_code != LINUX_VERSION_CODE)
645                 reason = "kernel version";
646         if (info->num_physpages != num_physpages)
647                 reason = "memory size";
648         if (strcmp(info->uts.sysname,system_utsname.sysname))
649                 reason = "system type";
650         if (strcmp(info->uts.release,system_utsname.release))
651                 reason = "kernel release";
652         if (strcmp(info->uts.version,system_utsname.version))
653                 reason = "version";
654         if (strcmp(info->uts.machine,system_utsname.machine))
655                 reason = "machine";
656         if (reason) {
657                 printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
658                 return -EPERM;
659         }
660         return 0;
661 }
662
663 /**
664  *      load header - check the image header and copy data from it
665  */
666
667 static int load_header(struct snapshot_handle *handle,
668                               struct swsusp_info *info)
669 {
670         int error;
671         struct pbe *pblist;
672
673         error = check_header(info);
674         if (!error) {
675                 pblist = alloc_pagedir(info->image_pages, GFP_ATOMIC, 0);
676                 if (!pblist)
677                         return -ENOMEM;
678                 pagedir_nosave = pblist;
679                 handle->pbe = pblist;
680                 nr_copy_pages = info->image_pages;
681                 nr_meta_pages = info->pages - info->image_pages - 1;
682         }
683         return error;
684 }
685
686 /**
687  *      unpack_orig_addresses - copy the elements of @buf[] (1 page) to
688  *      the PBEs in the list starting at @pbe
689  */
690
691 static inline struct pbe *unpack_orig_addresses(unsigned long *buf,
692                                                 struct pbe *pbe)
693 {
694         int j;
695
696         for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
697                 pbe->orig_address = buf[j];
698                 pbe = pbe->next;
699         }
700         return pbe;
701 }
702
703 /**
704  *      prepare_image - use metadata contained in the PBE list
705  *      pointed to by pagedir_nosave to mark the pages that will
706  *      be overwritten in the process of restoring the system
707  *      memory state from the image ("unsafe" pages) and allocate
708  *      memory for the image
709  *
710  *      The idea is to allocate the PBE list first and then
711  *      allocate as many pages as it's needed for the image data,
712  *      but not to assign these pages to the PBEs initially.
713  *      Instead, we just mark them as allocated and create a list
714  *      of "safe" which will be used later
715  */
716
717 struct safe_page {
718         struct safe_page *next;
719         char padding[PAGE_SIZE - sizeof(void *)];
720 };
721
722 static struct safe_page *safe_pages;
723
724 static int prepare_image(struct snapshot_handle *handle)
725 {
726         int error = 0;
727         unsigned int nr_pages = nr_copy_pages;
728         struct pbe *p, *pblist = NULL;
729
730         p = pagedir_nosave;
731         error = mark_unsafe_pages(p);
732         if (!error) {
733                 pblist = alloc_pagedir(nr_pages, GFP_ATOMIC, 1);
734                 if (pblist)
735                         copy_page_backup_list(pblist, p);
736                 free_pagedir(p, 0);
737                 if (!pblist)
738                         error = -ENOMEM;
739         }
740         safe_pages = NULL;
741         if (!error && nr_pages > unsafe_pages) {
742                 nr_pages -= unsafe_pages;
743                 while (nr_pages--) {
744                         struct safe_page *ptr;
745
746                         ptr = (struct safe_page *)get_zeroed_page(GFP_ATOMIC);
747                         if (!ptr) {
748                                 error = -ENOMEM;
749                                 break;
750                         }
751                         if (!PageNosaveFree(virt_to_page(ptr))) {
752                                 /* The page is "safe", add it to the list */
753                                 ptr->next = safe_pages;
754                                 safe_pages = ptr;
755                         }
756                         /* Mark the page as allocated */
757                         SetPageNosave(virt_to_page(ptr));
758                         SetPageNosaveFree(virt_to_page(ptr));
759                 }
760         }
761         if (!error) {
762                 pagedir_nosave = pblist;
763         } else {
764                 handle->pbe = NULL;
765                 swsusp_free();
766         }
767         return error;
768 }
769
770 static void *get_buffer(struct snapshot_handle *handle)
771 {
772         struct pbe *pbe = handle->pbe, *last = handle->last_pbe;
773         struct page *page = virt_to_page(pbe->orig_address);
774
775         if (PageNosave(page) && PageNosaveFree(page)) {
776                 /*
777                  * We have allocated the "original" page frame and we can
778                  * use it directly to store the read page
779                  */
780                 pbe->address = 0;
781                 if (last && last->next)
782                         last->next = NULL;
783                 return (void *)pbe->orig_address;
784         }
785         /*
786          * The "original" page frame has not been allocated and we have to
787          * use a "safe" page frame to store the read page
788          */
789         pbe->address = (unsigned long)safe_pages;
790         safe_pages = safe_pages->next;
791         if (last)
792                 last->next = pbe;
793         handle->last_pbe = pbe;
794         return (void *)pbe->address;
795 }
796
797 /**
798  *      snapshot_write_next - used for writing the system memory snapshot.
799  *
800  *      On the first call to it @handle should point to a zeroed
801  *      snapshot_handle structure.  The structure gets updated and a pointer
802  *      to it should be passed to this function every next time.
803  *
804  *      The @count parameter should contain the number of bytes the caller
805  *      wants to write to the image.  It must not be zero.
806  *
807  *      On success the function returns a positive number.  Then, the caller
808  *      is allowed to write up to the returned number of bytes to the memory
809  *      location computed by the data_of() macro.  The number returned
810  *      may be smaller than @count, but this only happens if the write would
811  *      cross a page boundary otherwise.
812  *
813  *      The function returns 0 to indicate the "end of file" condition,
814  *      and a negative number is returned on error.  In such cases the
815  *      structure pointed to by @handle is not updated and should not be used
816  *      any more.
817  */
818
819 int snapshot_write_next(struct snapshot_handle *handle, size_t count)
820 {
821         int error = 0;
822
823         if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages)
824                 return 0;
825         if (!buffer) {
826                 /* This makes the buffer be freed by swsusp_free() */
827                 buffer = alloc_image_page(GFP_ATOMIC, 0);
828                 if (!buffer)
829                         return -ENOMEM;
830         }
831         if (!handle->offset)
832                 handle->buffer = buffer;
833         handle->sync_read = 1;
834         if (handle->prev < handle->cur) {
835                 if (!handle->prev) {
836                         error = load_header(handle,
837                                         (struct swsusp_info *)buffer);
838                         if (error)
839                                 return error;
840                 } else if (handle->prev <= nr_meta_pages) {
841                         handle->pbe = unpack_orig_addresses(buffer,
842                                                         handle->pbe);
843                         if (!handle->pbe) {
844                                 error = prepare_image(handle);
845                                 if (error)
846                                         return error;
847                                 handle->pbe = pagedir_nosave;
848                                 handle->last_pbe = NULL;
849                                 handle->buffer = get_buffer(handle);
850                                 handle->sync_read = 0;
851                         }
852                 } else {
853                         handle->pbe = handle->pbe->next;
854                         handle->buffer = get_buffer(handle);
855                         handle->sync_read = 0;
856                 }
857                 handle->prev = handle->cur;
858         }
859         handle->buf_offset = handle->cur_offset;
860         if (handle->cur_offset + count >= PAGE_SIZE) {
861                 count = PAGE_SIZE - handle->cur_offset;
862                 handle->cur_offset = 0;
863                 handle->cur++;
864         } else {
865                 handle->cur_offset += count;
866         }
867         handle->offset += count;
868         return count;
869 }
870
871 int snapshot_image_loaded(struct snapshot_handle *handle)
872 {
873         return !(!handle->pbe || handle->pbe->next || !nr_copy_pages ||
874                 handle->cur <= nr_meta_pages + nr_copy_pages);
875 }