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