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