[PATCH] swsusp: Introduce memory bitmaps
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / power / snapshot.c
CommitLineData
25761b6e 1/*
96bc7aec 2 * linux/kernel/power/snapshot.c
25761b6e 3 *
96bc7aec 4 * This file provide system snapshot/restore functionality.
25761b6e
RW
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
f577eb30 13#include <linux/version.h>
25761b6e
RW
14#include <linux/module.h>
15#include <linux/mm.h>
16#include <linux/suspend.h>
17#include <linux/smp_lock.h>
25761b6e 18#include <linux/delay.h>
25761b6e 19#include <linux/bitops.h>
25761b6e 20#include <linux/spinlock.h>
25761b6e 21#include <linux/kernel.h>
25761b6e
RW
22#include <linux/pm.h>
23#include <linux/device.h>
25761b6e
RW
24#include <linux/bootmem.h>
25#include <linux/syscalls.h>
26#include <linux/console.h>
27#include <linux/highmem.h>
25761b6e
RW
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
25761b6e
RW
35#include "power.h"
36
75534b50
RW
37/* List of PBEs used for creating and restoring the suspend image */
38struct pbe *restore_pblist;
39
f577eb30
RW
40static unsigned int nr_copy_pages;
41static unsigned int nr_meta_pages;
6e1819d6 42static unsigned long *buffer;
7088a5c0 43
25761b6e 44#ifdef CONFIG_HIGHMEM
3448097f 45unsigned int count_highmem_pages(void)
72a97e08
RW
46{
47 struct zone *zone;
48 unsigned long zone_pfn;
49 unsigned int n = 0;
50
51 for_each_zone (zone)
52 if (is_highmem(zone)) {
53 mark_free_pages(zone);
54 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; zone_pfn++) {
55 struct page *page;
56 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
57 if (!pfn_valid(pfn))
58 continue;
59 page = pfn_to_page(pfn);
60 if (PageReserved(page))
61 continue;
62 if (PageNosaveFree(page))
63 continue;
64 n++;
65 }
66 }
67 return n;
68}
69
25761b6e
RW
70struct highmem_page {
71 char *data;
72 struct page *page;
73 struct highmem_page *next;
74};
75
76static struct highmem_page *highmem_copy;
77
78static int save_highmem_zone(struct zone *zone)
79{
80 unsigned long zone_pfn;
81 mark_free_pages(zone);
82 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
83 struct page *page;
84 struct highmem_page *save;
85 void *kaddr;
86 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
87
ce6ed29f 88 if (!(pfn%10000))
25761b6e
RW
89 printk(".");
90 if (!pfn_valid(pfn))
91 continue;
92 page = pfn_to_page(pfn);
93 /*
94 * This condition results from rvmalloc() sans vmalloc_32()
95 * and architectural memory reservations. This should be
96 * corrected eventually when the cases giving rise to this
97 * are better understood.
98 */
c8adb494 99 if (PageReserved(page))
25761b6e 100 continue;
25761b6e
RW
101 BUG_ON(PageNosave(page));
102 if (PageNosaveFree(page))
103 continue;
104 save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
105 if (!save)
106 return -ENOMEM;
107 save->next = highmem_copy;
108 save->page = page;
109 save->data = (void *) get_zeroed_page(GFP_ATOMIC);
110 if (!save->data) {
111 kfree(save);
112 return -ENOMEM;
113 }
114 kaddr = kmap_atomic(page, KM_USER0);
115 memcpy(save->data, kaddr, PAGE_SIZE);
116 kunmap_atomic(kaddr, KM_USER0);
117 highmem_copy = save;
118 }
119 return 0;
120}
25761b6e 121
3448097f 122int save_highmem(void)
25761b6e 123{
25761b6e
RW
124 struct zone *zone;
125 int res = 0;
126
ce6ed29f 127 pr_debug("swsusp: Saving Highmem");
e4e4d665 128 drain_local_pages();
25761b6e
RW
129 for_each_zone (zone) {
130 if (is_highmem(zone))
131 res = save_highmem_zone(zone);
132 if (res)
133 return res;
134 }
ce6ed29f 135 printk("\n");
25761b6e
RW
136 return 0;
137}
138
3448097f 139int restore_highmem(void)
25761b6e 140{
25761b6e
RW
141 printk("swsusp: Restoring Highmem\n");
142 while (highmem_copy) {
143 struct highmem_page *save = highmem_copy;
144 void *kaddr;
145 highmem_copy = save->next;
146
147 kaddr = kmap_atomic(save->page, KM_USER0);
148 memcpy(kaddr, save->data, PAGE_SIZE);
149 kunmap_atomic(kaddr, KM_USER0);
150 free_page((long) save->data);
151 kfree(save);
152 }
25761b6e
RW
153 return 0;
154}
ce4ab001 155#else
7bff24e2
AB
156static inline unsigned int count_highmem_pages(void) {return 0;}
157static inline int save_highmem(void) {return 0;}
158static inline int restore_highmem(void) {return 0;}
0fbeb5a4 159#endif
25761b6e 160
f6143aa6
RW
161/**
162 * @safe_needed - on resume, for storing the PBE list and the image,
163 * we can only use memory pages that do not conflict with the pages
164 * used before suspend.
165 *
166 * The unsafe pages are marked with the PG_nosave_free flag
167 * and we count them using unsafe_pages
168 */
169
0bcd888d
RW
170#define PG_ANY 0
171#define PG_SAFE 1
172#define PG_UNSAFE_CLEAR 1
173#define PG_UNSAFE_KEEP 0
174
f6143aa6
RW
175static unsigned int unsafe_pages;
176
177static void *alloc_image_page(gfp_t gfp_mask, int safe_needed)
178{
179 void *res;
180
181 res = (void *)get_zeroed_page(gfp_mask);
182 if (safe_needed)
183 while (res && PageNosaveFree(virt_to_page(res))) {
184 /* The page is unsafe, mark it for swsusp_free() */
185 SetPageNosave(virt_to_page(res));
186 unsafe_pages++;
187 res = (void *)get_zeroed_page(gfp_mask);
188 }
189 if (res) {
190 SetPageNosave(virt_to_page(res));
191 SetPageNosaveFree(virt_to_page(res));
192 }
193 return res;
194}
195
196unsigned long get_safe_page(gfp_t gfp_mask)
197{
0bcd888d 198 return (unsigned long)alloc_image_page(gfp_mask, PG_SAFE);
f6143aa6
RW
199}
200
201/**
202 * free_image_page - free page represented by @addr, allocated with
203 * alloc_image_page (page flags set by it must be cleared)
204 */
205
206static inline void free_image_page(void *addr, int clear_nosave_free)
207{
208 ClearPageNosave(virt_to_page(addr));
209 if (clear_nosave_free)
210 ClearPageNosaveFree(virt_to_page(addr));
211 free_page((unsigned long)addr);
212}
213
b788db79
RW
214/* struct linked_page is used to build chains of pages */
215
216#define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *))
217
218struct linked_page {
219 struct linked_page *next;
220 char data[LINKED_PAGE_DATA_SIZE];
221} __attribute__((packed));
222
223static inline void
224free_list_of_pages(struct linked_page *list, int clear_page_nosave)
225{
226 while (list) {
227 struct linked_page *lp = list->next;
228
229 free_image_page(list, clear_page_nosave);
230 list = lp;
231 }
232}
233
234/**
235 * struct chain_allocator is used for allocating small objects out of
236 * a linked list of pages called 'the chain'.
237 *
238 * The chain grows each time when there is no room for a new object in
239 * the current page. The allocated objects cannot be freed individually.
240 * It is only possible to free them all at once, by freeing the entire
241 * chain.
242 *
243 * NOTE: The chain allocator may be inefficient if the allocated objects
244 * are not much smaller than PAGE_SIZE.
245 */
246
247struct chain_allocator {
248 struct linked_page *chain; /* the chain */
249 unsigned int used_space; /* total size of objects allocated out
250 * of the current page
251 */
252 gfp_t gfp_mask; /* mask for allocating pages */
253 int safe_needed; /* if set, only "safe" pages are allocated */
254};
255
256static void
257chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed)
258{
259 ca->chain = NULL;
260 ca->used_space = LINKED_PAGE_DATA_SIZE;
261 ca->gfp_mask = gfp_mask;
262 ca->safe_needed = safe_needed;
263}
264
265static void *chain_alloc(struct chain_allocator *ca, unsigned int size)
266{
267 void *ret;
268
269 if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) {
270 struct linked_page *lp;
271
272 lp = alloc_image_page(ca->gfp_mask, ca->safe_needed);
273 if (!lp)
274 return NULL;
275
276 lp->next = ca->chain;
277 ca->chain = lp;
278 ca->used_space = 0;
279 }
280 ret = ca->chain->data + ca->used_space;
281 ca->used_space += size;
282 return ret;
283}
284
285static void chain_free(struct chain_allocator *ca, int clear_page_nosave)
286{
287 free_list_of_pages(ca->chain, clear_page_nosave);
288 memset(ca, 0, sizeof(struct chain_allocator));
289}
290
291/**
292 * Data types related to memory bitmaps.
293 *
294 * Memory bitmap is a structure consiting of many linked lists of
295 * objects. The main list's elements are of type struct zone_bitmap
296 * and each of them corresonds to one zone. For each zone bitmap
297 * object there is a list of objects of type struct bm_block that
298 * represent each blocks of bit chunks in which information is
299 * stored.
300 *
301 * struct memory_bitmap contains a pointer to the main list of zone
302 * bitmap objects, a struct bm_position used for browsing the bitmap,
303 * and a pointer to the list of pages used for allocating all of the
304 * zone bitmap objects and bitmap block objects.
305 *
306 * NOTE: It has to be possible to lay out the bitmap in memory
307 * using only allocations of order 0. Additionally, the bitmap is
308 * designed to work with arbitrary number of zones (this is over the
309 * top for now, but let's avoid making unnecessary assumptions ;-).
310 *
311 * struct zone_bitmap contains a pointer to a list of bitmap block
312 * objects and a pointer to the bitmap block object that has been
313 * most recently used for setting bits. Additionally, it contains the
314 * pfns that correspond to the start and end of the represented zone.
315 *
316 * struct bm_block contains a pointer to the memory page in which
317 * information is stored (in the form of a block of bit chunks
318 * of type unsigned long each). It also contains the pfns that
319 * correspond to the start and end of the represented memory area and
320 * the number of bit chunks in the block.
321 *
322 * NOTE: Memory bitmaps are used for two types of operations only:
323 * "set a bit" and "find the next bit set". Moreover, the searching
324 * is always carried out after all of the "set a bit" operations
325 * on given bitmap.
326 */
327
328#define BM_END_OF_MAP (~0UL)
329
330#define BM_CHUNKS_PER_BLOCK (PAGE_SIZE / sizeof(long))
331#define BM_BITS_PER_CHUNK (sizeof(long) << 3)
332#define BM_BITS_PER_BLOCK (PAGE_SIZE << 3)
333
334struct bm_block {
335 struct bm_block *next; /* next element of the list */
336 unsigned long start_pfn; /* pfn represented by the first bit */
337 unsigned long end_pfn; /* pfn represented by the last bit plus 1 */
338 unsigned int size; /* number of bit chunks */
339 unsigned long *data; /* chunks of bits representing pages */
340};
341
342struct zone_bitmap {
343 struct zone_bitmap *next; /* next element of the list */
344 unsigned long start_pfn; /* minimal pfn in this zone */
345 unsigned long end_pfn; /* maximal pfn in this zone plus 1 */
346 struct bm_block *bm_blocks; /* list of bitmap blocks */
347 struct bm_block *cur_block; /* recently used bitmap block */
348};
349
350/* strcut bm_position is used for browsing memory bitmaps */
351
352struct bm_position {
353 struct zone_bitmap *zone_bm;
354 struct bm_block *block;
355 int chunk;
356 int bit;
357};
358
359struct memory_bitmap {
360 struct zone_bitmap *zone_bm_list; /* list of zone bitmaps */
361 struct linked_page *p_list; /* list of pages used to store zone
362 * bitmap objects and bitmap block
363 * objects
364 */
365 struct bm_position cur; /* most recently used bit position */
366};
367
368/* Functions that operate on memory bitmaps */
369
370static inline void memory_bm_reset_chunk(struct memory_bitmap *bm)
371{
372 bm->cur.chunk = 0;
373 bm->cur.bit = -1;
374}
375
376static void memory_bm_position_reset(struct memory_bitmap *bm)
377{
378 struct zone_bitmap *zone_bm;
379
380 zone_bm = bm->zone_bm_list;
381 bm->cur.zone_bm = zone_bm;
382 bm->cur.block = zone_bm->bm_blocks;
383 memory_bm_reset_chunk(bm);
384}
385
386static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free);
387
388/**
389 * create_bm_block_list - create a list of block bitmap objects
390 */
391
392static inline struct bm_block *
393create_bm_block_list(unsigned int nr_blocks, struct chain_allocator *ca)
394{
395 struct bm_block *bblist = NULL;
396
397 while (nr_blocks-- > 0) {
398 struct bm_block *bb;
399
400 bb = chain_alloc(ca, sizeof(struct bm_block));
401 if (!bb)
402 return NULL;
403
404 bb->next = bblist;
405 bblist = bb;
406 }
407 return bblist;
408}
409
410/**
411 * create_zone_bm_list - create a list of zone bitmap objects
412 */
413
414static inline struct zone_bitmap *
415create_zone_bm_list(unsigned int nr_zones, struct chain_allocator *ca)
416{
417 struct zone_bitmap *zbmlist = NULL;
418
419 while (nr_zones-- > 0) {
420 struct zone_bitmap *zbm;
421
422 zbm = chain_alloc(ca, sizeof(struct zone_bitmap));
423 if (!zbm)
424 return NULL;
425
426 zbm->next = zbmlist;
427 zbmlist = zbm;
428 }
429 return zbmlist;
430}
431
432/**
433 * memory_bm_create - allocate memory for a memory bitmap
434 */
435
436static int
437memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed)
438{
439 struct chain_allocator ca;
440 struct zone *zone;
441 struct zone_bitmap *zone_bm;
442 struct bm_block *bb;
443 unsigned int nr;
444
445 chain_init(&ca, gfp_mask, safe_needed);
446
447 /* Compute the number of zones */
448 nr = 0;
449 for_each_zone (zone)
450 if (populated_zone(zone) && !is_highmem(zone))
451 nr++;
452
453 /* Allocate the list of zones bitmap objects */
454 zone_bm = create_zone_bm_list(nr, &ca);
455 bm->zone_bm_list = zone_bm;
456 if (!zone_bm) {
457 chain_free(&ca, PG_UNSAFE_CLEAR);
458 return -ENOMEM;
459 }
460
461 /* Initialize the zone bitmap objects */
462 for_each_zone (zone) {
463 unsigned long pfn;
464
465 if (!populated_zone(zone) || is_highmem(zone))
466 continue;
467
468 zone_bm->start_pfn = zone->zone_start_pfn;
469 zone_bm->end_pfn = zone->zone_start_pfn + zone->spanned_pages;
470 /* Allocate the list of bitmap block objects */
471 nr = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
472 bb = create_bm_block_list(nr, &ca);
473 zone_bm->bm_blocks = bb;
474 zone_bm->cur_block = bb;
475 if (!bb)
476 goto Free;
477
478 nr = zone->spanned_pages;
479 pfn = zone->zone_start_pfn;
480 /* Initialize the bitmap block objects */
481 while (bb) {
482 unsigned long *ptr;
483
484 ptr = alloc_image_page(gfp_mask, safe_needed);
485 bb->data = ptr;
486 if (!ptr)
487 goto Free;
488
489 bb->start_pfn = pfn;
490 if (nr >= BM_BITS_PER_BLOCK) {
491 pfn += BM_BITS_PER_BLOCK;
492 bb->size = BM_CHUNKS_PER_BLOCK;
493 nr -= BM_BITS_PER_BLOCK;
494 } else {
495 /* This is executed only once in the loop */
496 pfn += nr;
497 bb->size = DIV_ROUND_UP(nr, BM_BITS_PER_CHUNK);
498 }
499 bb->end_pfn = pfn;
500 bb = bb->next;
501 }
502 zone_bm = zone_bm->next;
503 }
504 bm->p_list = ca.chain;
505 memory_bm_position_reset(bm);
506 return 0;
507
508Free:
509 bm->p_list = ca.chain;
510 memory_bm_free(bm, PG_UNSAFE_CLEAR);
511 return -ENOMEM;
512}
513
514/**
515 * memory_bm_free - free memory occupied by the memory bitmap @bm
516 */
517
518static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free)
519{
520 struct zone_bitmap *zone_bm;
521
522 /* Free the list of bit blocks for each zone_bitmap object */
523 zone_bm = bm->zone_bm_list;
524 while (zone_bm) {
525 struct bm_block *bb;
526
527 bb = zone_bm->bm_blocks;
528 while (bb) {
529 if (bb->data)
530 free_image_page(bb->data, clear_nosave_free);
531 bb = bb->next;
532 }
533 zone_bm = zone_bm->next;
534 }
535 free_list_of_pages(bm->p_list, clear_nosave_free);
536 bm->zone_bm_list = NULL;
537}
538
539/**
540 * memory_bm_set_bit - set the bit in the bitmap @bm that corresponds
541 * to given pfn. The cur_zone_bm member of @bm and the cur_block member
542 * of @bm->cur_zone_bm are updated.
543 *
544 * If the bit cannot be set, the function returns -EINVAL .
545 */
546
547static int
548memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
549{
550 struct zone_bitmap *zone_bm;
551 struct bm_block *bb;
552
553 /* Check if the pfn is from the current zone */
554 zone_bm = bm->cur.zone_bm;
555 if (pfn < zone_bm->start_pfn || pfn >= zone_bm->end_pfn) {
556 zone_bm = bm->zone_bm_list;
557 /* We don't assume that the zones are sorted by pfns */
558 while (pfn < zone_bm->start_pfn || pfn >= zone_bm->end_pfn) {
559 zone_bm = zone_bm->next;
560 if (unlikely(!zone_bm))
561 return -EINVAL;
562 }
563 bm->cur.zone_bm = zone_bm;
564 }
565 /* Check if the pfn corresponds to the current bitmap block */
566 bb = zone_bm->cur_block;
567 if (pfn < bb->start_pfn)
568 bb = zone_bm->bm_blocks;
569
570 while (pfn >= bb->end_pfn) {
571 bb = bb->next;
572 if (unlikely(!bb))
573 return -EINVAL;
574 }
575 zone_bm->cur_block = bb;
576 pfn -= bb->start_pfn;
577 set_bit(pfn % BM_BITS_PER_CHUNK, bb->data + pfn / BM_BITS_PER_CHUNK);
578 return 0;
579}
580
581/* Two auxiliary functions for memory_bm_next_pfn */
582
583/* Find the first set bit in the given chunk, if there is one */
584
585static inline int next_bit_in_chunk(int bit, unsigned long *chunk_p)
586{
587 bit++;
588 while (bit < BM_BITS_PER_CHUNK) {
589 if (test_bit(bit, chunk_p))
590 return bit;
591
592 bit++;
593 }
594 return -1;
595}
596
597/* Find a chunk containing some bits set in given block of bits */
598
599static inline int next_chunk_in_block(int n, struct bm_block *bb)
600{
601 n++;
602 while (n < bb->size) {
603 if (bb->data[n])
604 return n;
605
606 n++;
607 }
608 return -1;
609}
610
611/**
612 * memory_bm_next_pfn - find the pfn that corresponds to the next set bit
613 * in the bitmap @bm. If the pfn cannot be found, BM_END_OF_MAP is
614 * returned.
615 *
616 * It is required to run memory_bm_position_reset() before the first call to
617 * this function.
618 */
619
620static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm)
621{
622 struct zone_bitmap *zone_bm;
623 struct bm_block *bb;
624 int chunk;
625 int bit;
626
627 do {
628 bb = bm->cur.block;
629 do {
630 chunk = bm->cur.chunk;
631 bit = bm->cur.bit;
632 do {
633 bit = next_bit_in_chunk(bit, bb->data + chunk);
634 if (bit >= 0)
635 goto Return_pfn;
636
637 chunk = next_chunk_in_block(chunk, bb);
638 bit = -1;
639 } while (chunk >= 0);
640 bb = bb->next;
641 bm->cur.block = bb;
642 memory_bm_reset_chunk(bm);
643 } while (bb);
644 zone_bm = bm->cur.zone_bm->next;
645 if (zone_bm) {
646 bm->cur.zone_bm = zone_bm;
647 bm->cur.block = zone_bm->bm_blocks;
648 memory_bm_reset_chunk(bm);
649 }
650 } while (zone_bm);
651 memory_bm_position_reset(bm);
652 return BM_END_OF_MAP;
653
654Return_pfn:
655 bm->cur.chunk = chunk;
656 bm->cur.bit = bit;
657 return bb->start_pfn + chunk * BM_BITS_PER_CHUNK + bit;
658}
659
660/**
661 * snapshot_additional_pages - estimate the number of additional pages
662 * be needed for setting up the suspend image data structures for given
663 * zone (usually the returned value is greater than the exact number)
664 */
665
666unsigned int snapshot_additional_pages(struct zone *zone)
667{
668 unsigned int res;
669
670 res = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
671 res += DIV_ROUND_UP(res * sizeof(struct bm_block), PAGE_SIZE);
672 return res;
673}
674
f6143aa6
RW
675/**
676 * pfn_is_nosave - check if given pfn is in the 'nosave' section
677 */
678
ae83c5ee 679static inline int pfn_is_nosave(unsigned long pfn)
25761b6e
RW
680{
681 unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
682 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
683 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
684}
685
686/**
687 * saveable - Determine whether a page should be cloned or not.
688 * @pfn: The page
689 *
ae83c5ee
RW
690 * We save a page if it isn't Nosave, and is not in the range of pages
691 * statically defined as 'unsaveable', and it
692 * isn't a part of a free chunk of pages.
25761b6e
RW
693 */
694
ae83c5ee 695static struct page *saveable_page(unsigned long pfn)
25761b6e 696{
de491861 697 struct page *page;
25761b6e
RW
698
699 if (!pfn_valid(pfn))
ae83c5ee 700 return NULL;
25761b6e
RW
701
702 page = pfn_to_page(pfn);
ae83c5ee 703
25761b6e 704 if (PageNosave(page))
ae83c5ee 705 return NULL;
72a97e08 706 if (PageReserved(page) && pfn_is_nosave(pfn))
ae83c5ee 707 return NULL;
25761b6e 708 if (PageNosaveFree(page))
ae83c5ee 709 return NULL;
25761b6e 710
ae83c5ee 711 return page;
25761b6e
RW
712}
713
72a97e08 714unsigned int count_data_pages(void)
25761b6e
RW
715{
716 struct zone *zone;
ae83c5ee 717 unsigned long pfn, max_zone_pfn;
dc19d507 718 unsigned int n = 0;
25761b6e 719
25761b6e
RW
720 for_each_zone (zone) {
721 if (is_highmem(zone))
722 continue;
723 mark_free_pages(zone);
ae83c5ee
RW
724 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
725 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
726 n += !!saveable_page(pfn);
25761b6e 727 }
a0f49651 728 return n;
25761b6e
RW
729}
730
f623f0db
RW
731static inline void copy_data_page(long *dst, long *src)
732{
733 int n;
734
735 /* copy_page and memcpy are not usable for copying task structs. */
736 for (n = PAGE_SIZE / sizeof(long); n; n--)
737 *dst++ = *src++;
738}
739
b788db79
RW
740static void
741copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm)
25761b6e
RW
742{
743 struct zone *zone;
b788db79 744 unsigned long pfn;
25761b6e 745
25761b6e 746 for_each_zone (zone) {
b788db79
RW
747 unsigned long max_zone_pfn;
748
25761b6e
RW
749 if (is_highmem(zone))
750 continue;
b788db79 751
25761b6e 752 mark_free_pages(zone);
ae83c5ee 753 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
b788db79
RW
754 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
755 if (saveable_page(pfn))
756 memory_bm_set_bit(orig_bm, pfn);
25761b6e 757 }
b788db79
RW
758 memory_bm_position_reset(orig_bm);
759 memory_bm_position_reset(copy_bm);
760 do {
761 pfn = memory_bm_next_pfn(orig_bm);
762 if (likely(pfn != BM_END_OF_MAP)) {
763 struct page *page;
764 void *src;
765
766 page = pfn_to_page(pfn);
767 src = page_address(page);
768 page = pfn_to_page(memory_bm_next_pfn(copy_bm));
769 copy_data_page(page_address(page), src);
770 }
771 } while (pfn != BM_END_OF_MAP);
25761b6e
RW
772}
773
25761b6e
RW
774/**
775 * free_pagedir - free pages allocated with alloc_pagedir()
776 */
777
4a3b98a4 778static void free_pagedir(struct pbe *pblist, int clear_nosave_free)
25761b6e
RW
779{
780 struct pbe *pbe;
781
782 while (pblist) {
783 pbe = (pblist + PB_PAGE_SKIP)->next;
f6143aa6 784 free_image_page(pblist, clear_nosave_free);
25761b6e
RW
785 pblist = pbe;
786 }
787}
788
789/**
790 * fill_pb_page - Create a list of PBEs on a given memory page
791 */
792
cd560bb2 793static inline void fill_pb_page(struct pbe *pbpage, unsigned int n)
25761b6e
RW
794{
795 struct pbe *p;
796
797 p = pbpage;
cd560bb2 798 pbpage += n - 1;
25761b6e
RW
799 do
800 p->next = p + 1;
801 while (++p < pbpage);
802}
803
804/**
805 * create_pbe_list - Create a list of PBEs on top of a given chain
806 * of memory pages allocated with alloc_pagedir()
cd560bb2
RW
807 *
808 * This function assumes that pages allocated by alloc_image_page() will
809 * always be zeroed.
25761b6e
RW
810 */
811
7088a5c0 812static inline void create_pbe_list(struct pbe *pblist, unsigned int nr_pages)
25761b6e 813{
cd560bb2 814 struct pbe *pbpage;
dc19d507 815 unsigned int num = PBES_PER_PAGE;
25761b6e
RW
816
817 for_each_pb_page (pbpage, pblist) {
818 if (num >= nr_pages)
819 break;
820
cd560bb2 821 fill_pb_page(pbpage, PBES_PER_PAGE);
25761b6e
RW
822 num += PBES_PER_PAGE;
823 }
824 if (pbpage) {
cd560bb2
RW
825 num -= PBES_PER_PAGE;
826 fill_pb_page(pbpage, nr_pages - num);
25761b6e 827 }
25761b6e
RW
828}
829
25761b6e
RW
830/**
831 * alloc_pagedir - Allocate the page directory.
832 *
833 * First, determine exactly how many pages we need and
834 * allocate them.
835 *
836 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
837 * struct pbe elements (pbes) and the last element in the page points
838 * to the next page.
839 *
840 * On each page we set up a list of struct_pbe elements.
841 */
842
7bff24e2
AB
843static struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask,
844 int safe_needed)
25761b6e 845{
dc19d507 846 unsigned int num;
25761b6e
RW
847 struct pbe *pblist, *pbe;
848
849 if (!nr_pages)
850 return NULL;
851
054bd4c1 852 pblist = alloc_image_page(gfp_mask, safe_needed);
cd560bb2
RW
853 pbe = pblist;
854 for (num = PBES_PER_PAGE; num < nr_pages; num += PBES_PER_PAGE) {
855 if (!pbe) {
0bcd888d 856 free_pagedir(pblist, PG_UNSAFE_CLEAR);
cd560bb2
RW
857 return NULL;
858 }
25761b6e 859 pbe += PB_PAGE_SKIP;
054bd4c1 860 pbe->next = alloc_image_page(gfp_mask, safe_needed);
cd560bb2 861 pbe = pbe->next;
25761b6e 862 }
cd560bb2 863 create_pbe_list(pblist, nr_pages);
25761b6e
RW
864 return pblist;
865}
866
867/**
868 * Free pages we allocated for suspend. Suspend pages are alocated
869 * before atomic copy, so we need to free them after resume.
870 */
871
872void swsusp_free(void)
873{
874 struct zone *zone;
ae83c5ee 875 unsigned long pfn, max_zone_pfn;
25761b6e
RW
876
877 for_each_zone(zone) {
ae83c5ee
RW
878 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
879 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
880 if (pfn_valid(pfn)) {
881 struct page *page = pfn_to_page(pfn);
882
25761b6e
RW
883 if (PageNosave(page) && PageNosaveFree(page)) {
884 ClearPageNosave(page);
885 ClearPageNosaveFree(page);
886 free_page((long) page_address(page));
887 }
888 }
889 }
f577eb30
RW
890 nr_copy_pages = 0;
891 nr_meta_pages = 0;
75534b50 892 restore_pblist = NULL;
6e1819d6 893 buffer = NULL;
25761b6e
RW
894}
895
896
897/**
898 * enough_free_mem - Make sure we enough free memory to snapshot.
899 *
900 * Returns TRUE or FALSE after checking the number of available
901 * free pages.
902 */
903
dc19d507 904static int enough_free_mem(unsigned int nr_pages)
25761b6e 905{
e5e2fa78
RW
906 struct zone *zone;
907 unsigned int n = 0;
908
909 for_each_zone (zone)
910 if (!is_highmem(zone))
911 n += zone->free_pages;
912 pr_debug("swsusp: available memory: %u pages\n", n);
913 return n > (nr_pages + PAGES_FOR_IO +
a0f49651 914 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
25761b6e
RW
915}
916
b788db79
RW
917static int
918swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
919 unsigned int nr_pages)
054bd4c1 920{
b788db79 921 int error;
054bd4c1 922
b788db79
RW
923 error = memory_bm_create(orig_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
924 if (error)
925 goto Free;
25761b6e 926
b788db79
RW
927 error = memory_bm_create(copy_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
928 if (error)
929 goto Free;
25761b6e 930
b788db79
RW
931 while (nr_pages-- > 0) {
932 struct page *page = alloc_page(GFP_ATOMIC | __GFP_COLD);
933 if (!page)
934 goto Free;
25761b6e 935
b788db79
RW
936 SetPageNosave(page);
937 SetPageNosaveFree(page);
938 memory_bm_set_bit(copy_bm, page_to_pfn(page));
25761b6e 939 }
b788db79 940 return 0;
25761b6e 941
b788db79
RW
942Free:
943 swsusp_free();
944 return -ENOMEM;
25761b6e
RW
945}
946
b788db79
RW
947/* Memory bitmap used for marking saveable pages */
948static struct memory_bitmap orig_bm;
949/* Memory bitmap used for marking allocated pages that will contain the copies
950 * of saveable pages
951 */
952static struct memory_bitmap copy_bm;
953
2e32a43e 954asmlinkage int swsusp_save(void)
25761b6e 955{
dc19d507 956 unsigned int nr_pages;
25761b6e
RW
957
958 pr_debug("swsusp: critical section: \n");
25761b6e
RW
959
960 drain_local_pages();
a0f49651
RW
961 nr_pages = count_data_pages();
962 printk("swsusp: Need to copy %u pages\n", nr_pages);
25761b6e
RW
963
964 pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
a0f49651
RW
965 nr_pages,
966 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
25761b6e
RW
967 PAGES_FOR_IO, nr_free_pages());
968
a0f49651 969 if (!enough_free_mem(nr_pages)) {
25761b6e
RW
970 printk(KERN_ERR "swsusp: Not enough free memory\n");
971 return -ENOMEM;
972 }
973
b788db79 974 if (swsusp_alloc(&orig_bm, &copy_bm, nr_pages))
a0f49651 975 return -ENOMEM;
25761b6e
RW
976
977 /* During allocating of suspend pagedir, new cold pages may appear.
978 * Kill them.
979 */
980 drain_local_pages();
b788db79 981 copy_data_pages(&copy_bm, &orig_bm);
25761b6e
RW
982
983 /*
984 * End of critical section. From now on, we can write to memory,
985 * but we should not touch disk. This specially means we must _not_
986 * touch swap space! Except we must write out our image of course.
987 */
988
a0f49651 989 nr_copy_pages = nr_pages;
f577eb30 990 nr_meta_pages = (nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT;
a0f49651
RW
991
992 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
25761b6e
RW
993 return 0;
994}
f577eb30
RW
995
996static void init_header(struct swsusp_info *info)
997{
998 memset(info, 0, sizeof(struct swsusp_info));
999 info->version_code = LINUX_VERSION_CODE;
1000 info->num_physpages = num_physpages;
1001 memcpy(&info->uts, &system_utsname, sizeof(system_utsname));
1002 info->cpus = num_online_cpus();
1003 info->image_pages = nr_copy_pages;
1004 info->pages = nr_copy_pages + nr_meta_pages + 1;
6e1819d6
RW
1005 info->size = info->pages;
1006 info->size <<= PAGE_SHIFT;
f577eb30
RW
1007}
1008
1009/**
b788db79
RW
1010 * pack_addresses - the addresses corresponding to pfns found in the
1011 * bitmap @bm are stored in the array @buf[] (1 page)
f577eb30
RW
1012 */
1013
b788db79
RW
1014static inline void
1015pack_addresses(unsigned long *buf, struct memory_bitmap *bm)
f577eb30
RW
1016{
1017 int j;
1018
b788db79
RW
1019 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
1020 unsigned long pfn = memory_bm_next_pfn(bm);
1021
1022 if (unlikely(pfn == BM_END_OF_MAP))
1023 break;
1024
1025 buf[j] = (unsigned long)page_address(pfn_to_page(pfn));
f577eb30 1026 }
f577eb30
RW
1027}
1028
1029/**
1030 * snapshot_read_next - used for reading the system memory snapshot.
1031 *
1032 * On the first call to it @handle should point to a zeroed
1033 * snapshot_handle structure. The structure gets updated and a pointer
1034 * to it should be passed to this function every next time.
1035 *
1036 * The @count parameter should contain the number of bytes the caller
1037 * wants to read from the snapshot. It must not be zero.
1038 *
1039 * On success the function returns a positive number. Then, the caller
1040 * is allowed to read up to the returned number of bytes from the memory
1041 * location computed by the data_of() macro. The number returned
1042 * may be smaller than @count, but this only happens if the read would
1043 * cross a page boundary otherwise.
1044 *
1045 * The function returns 0 to indicate the end of data stream condition,
1046 * and a negative number is returned on error. In such cases the
1047 * structure pointed to by @handle is not updated and should not be used
1048 * any more.
1049 */
1050
1051int snapshot_read_next(struct snapshot_handle *handle, size_t count)
1052{
fb13a28b 1053 if (handle->cur > nr_meta_pages + nr_copy_pages)
f577eb30 1054 return 0;
b788db79 1055
f577eb30
RW
1056 if (!buffer) {
1057 /* This makes the buffer be freed by swsusp_free() */
0bcd888d 1058 buffer = alloc_image_page(GFP_ATOMIC, PG_ANY);
f577eb30
RW
1059 if (!buffer)
1060 return -ENOMEM;
1061 }
1062 if (!handle->offset) {
1063 init_header((struct swsusp_info *)buffer);
1064 handle->buffer = buffer;
b788db79
RW
1065 memory_bm_position_reset(&orig_bm);
1066 memory_bm_position_reset(&copy_bm);
f577eb30 1067 }
fb13a28b
RW
1068 if (handle->prev < handle->cur) {
1069 if (handle->cur <= nr_meta_pages) {
b788db79
RW
1070 memset(buffer, 0, PAGE_SIZE);
1071 pack_addresses(buffer, &orig_bm);
f577eb30 1072 } else {
b788db79
RW
1073 unsigned long pfn = memory_bm_next_pfn(&copy_bm);
1074
1075 handle->buffer = page_address(pfn_to_page(pfn));
f577eb30 1076 }
fb13a28b 1077 handle->prev = handle->cur;
f577eb30 1078 }
fb13a28b
RW
1079 handle->buf_offset = handle->cur_offset;
1080 if (handle->cur_offset + count >= PAGE_SIZE) {
1081 count = PAGE_SIZE - handle->cur_offset;
1082 handle->cur_offset = 0;
1083 handle->cur++;
f577eb30 1084 } else {
fb13a28b 1085 handle->cur_offset += count;
f577eb30
RW
1086 }
1087 handle->offset += count;
1088 return count;
1089}
1090
1091/**
1092 * mark_unsafe_pages - mark the pages that cannot be used for storing
1093 * the image during resume, because they conflict with the pages that
1094 * had been used before suspend
1095 */
1096
1097static int mark_unsafe_pages(struct pbe *pblist)
1098{
1099 struct zone *zone;
ae83c5ee 1100 unsigned long pfn, max_zone_pfn;
f577eb30
RW
1101 struct pbe *p;
1102
1103 if (!pblist) /* a sanity check */
1104 return -EINVAL;
1105
1106 /* Clear page flags */
1107 for_each_zone (zone) {
ae83c5ee
RW
1108 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1109 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1110 if (pfn_valid(pfn))
1111 ClearPageNosaveFree(pfn_to_page(pfn));
f577eb30
RW
1112 }
1113
1114 /* Mark orig addresses */
1115 for_each_pbe (p, pblist) {
1116 if (virt_addr_valid(p->orig_address))
1117 SetPageNosaveFree(virt_to_page(p->orig_address));
1118 else
1119 return -EFAULT;
1120 }
1121
968808b8
RW
1122 unsafe_pages = 0;
1123
f577eb30
RW
1124 return 0;
1125}
1126
1127static void copy_page_backup_list(struct pbe *dst, struct pbe *src)
1128{
1129 /* We assume both lists contain the same number of elements */
1130 while (src) {
1131 dst->orig_address = src->orig_address;
1132 dst = dst->next;
1133 src = src->next;
1134 }
1135}
1136
1137static int check_header(struct swsusp_info *info)
1138{
1139 char *reason = NULL;
1140
1141 if (info->version_code != LINUX_VERSION_CODE)
1142 reason = "kernel version";
1143 if (info->num_physpages != num_physpages)
1144 reason = "memory size";
1145 if (strcmp(info->uts.sysname,system_utsname.sysname))
1146 reason = "system type";
1147 if (strcmp(info->uts.release,system_utsname.release))
1148 reason = "kernel release";
1149 if (strcmp(info->uts.version,system_utsname.version))
1150 reason = "version";
1151 if (strcmp(info->uts.machine,system_utsname.machine))
1152 reason = "machine";
1153 if (reason) {
1154 printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
1155 return -EPERM;
1156 }
1157 return 0;
1158}
1159
1160/**
1161 * load header - check the image header and copy data from it
1162 */
1163
1164static int load_header(struct snapshot_handle *handle,
1165 struct swsusp_info *info)
1166{
1167 int error;
1168 struct pbe *pblist;
1169
1170 error = check_header(info);
1171 if (!error) {
0bcd888d 1172 pblist = alloc_pagedir(info->image_pages, GFP_ATOMIC, PG_ANY);
f577eb30
RW
1173 if (!pblist)
1174 return -ENOMEM;
75534b50 1175 restore_pblist = pblist;
f577eb30
RW
1176 handle->pbe = pblist;
1177 nr_copy_pages = info->image_pages;
1178 nr_meta_pages = info->pages - info->image_pages - 1;
1179 }
1180 return error;
1181}
1182
1183/**
1184 * unpack_orig_addresses - copy the elements of @buf[] (1 page) to
1185 * the PBEs in the list starting at @pbe
1186 */
1187
1188static inline struct pbe *unpack_orig_addresses(unsigned long *buf,
1189 struct pbe *pbe)
1190{
1191 int j;
1192
1193 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
1194 pbe->orig_address = buf[j];
1195 pbe = pbe->next;
1196 }
1197 return pbe;
1198}
1199
1200/**
968808b8 1201 * prepare_image - use metadata contained in the PBE list
75534b50 1202 * pointed to by restore_pblist to mark the pages that will
f577eb30 1203 * be overwritten in the process of restoring the system
968808b8
RW
1204 * memory state from the image ("unsafe" pages) and allocate
1205 * memory for the image
1206 *
1207 * The idea is to allocate the PBE list first and then
1208 * allocate as many pages as it's needed for the image data,
1209 * but not to assign these pages to the PBEs initially.
1210 * Instead, we just mark them as allocated and create a list
1211 * of "safe" which will be used later
f577eb30
RW
1212 */
1213
b788db79 1214static struct linked_page *safe_pages;
968808b8
RW
1215
1216static int prepare_image(struct snapshot_handle *handle)
f577eb30
RW
1217{
1218 int error = 0;
968808b8
RW
1219 unsigned int nr_pages = nr_copy_pages;
1220 struct pbe *p, *pblist = NULL;
f577eb30 1221
75534b50 1222 p = restore_pblist;
f577eb30
RW
1223 error = mark_unsafe_pages(p);
1224 if (!error) {
0bcd888d 1225 pblist = alloc_pagedir(nr_pages, GFP_ATOMIC, PG_SAFE);
f577eb30
RW
1226 if (pblist)
1227 copy_page_backup_list(pblist, p);
0bcd888d 1228 free_pagedir(p, PG_UNSAFE_KEEP);
f577eb30
RW
1229 if (!pblist)
1230 error = -ENOMEM;
1231 }
968808b8
RW
1232 safe_pages = NULL;
1233 if (!error && nr_pages > unsafe_pages) {
1234 nr_pages -= unsafe_pages;
1235 while (nr_pages--) {
b788db79 1236 struct linked_page *ptr;
968808b8 1237
b788db79 1238 ptr = (void *)get_zeroed_page(GFP_ATOMIC);
968808b8
RW
1239 if (!ptr) {
1240 error = -ENOMEM;
1241 break;
1242 }
1243 if (!PageNosaveFree(virt_to_page(ptr))) {
1244 /* The page is "safe", add it to the list */
1245 ptr->next = safe_pages;
1246 safe_pages = ptr;
1247 }
1248 /* Mark the page as allocated */
1249 SetPageNosave(virt_to_page(ptr));
1250 SetPageNosaveFree(virt_to_page(ptr));
1251 }
1252 }
f577eb30 1253 if (!error) {
75534b50 1254 restore_pblist = pblist;
f577eb30 1255 } else {
f577eb30 1256 handle->pbe = NULL;
968808b8 1257 swsusp_free();
f577eb30
RW
1258 }
1259 return error;
1260}
1261
968808b8
RW
1262static void *get_buffer(struct snapshot_handle *handle)
1263{
1264 struct pbe *pbe = handle->pbe, *last = handle->last_pbe;
1265 struct page *page = virt_to_page(pbe->orig_address);
1266
1267 if (PageNosave(page) && PageNosaveFree(page)) {
1268 /*
1269 * We have allocated the "original" page frame and we can
1270 * use it directly to store the read page
1271 */
1272 pbe->address = 0;
1273 if (last && last->next)
1274 last->next = NULL;
1275 return (void *)pbe->orig_address;
1276 }
1277 /*
1278 * The "original" page frame has not been allocated and we have to
1279 * use a "safe" page frame to store the read page
1280 */
1281 pbe->address = (unsigned long)safe_pages;
1282 safe_pages = safe_pages->next;
1283 if (last)
1284 last->next = pbe;
1285 handle->last_pbe = pbe;
1286 return (void *)pbe->address;
1287}
1288
f577eb30
RW
1289/**
1290 * snapshot_write_next - used for writing the system memory snapshot.
1291 *
1292 * On the first call to it @handle should point to a zeroed
1293 * snapshot_handle structure. The structure gets updated and a pointer
1294 * to it should be passed to this function every next time.
1295 *
1296 * The @count parameter should contain the number of bytes the caller
1297 * wants to write to the image. It must not be zero.
1298 *
1299 * On success the function returns a positive number. Then, the caller
1300 * is allowed to write up to the returned number of bytes to the memory
1301 * location computed by the data_of() macro. The number returned
1302 * may be smaller than @count, but this only happens if the write would
1303 * cross a page boundary otherwise.
1304 *
1305 * The function returns 0 to indicate the "end of file" condition,
1306 * and a negative number is returned on error. In such cases the
1307 * structure pointed to by @handle is not updated and should not be used
1308 * any more.
1309 */
1310
1311int snapshot_write_next(struct snapshot_handle *handle, size_t count)
1312{
f577eb30
RW
1313 int error = 0;
1314
fb13a28b 1315 if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages)
f577eb30
RW
1316 return 0;
1317 if (!buffer) {
1318 /* This makes the buffer be freed by swsusp_free() */
0bcd888d 1319 buffer = alloc_image_page(GFP_ATOMIC, PG_ANY);
f577eb30
RW
1320 if (!buffer)
1321 return -ENOMEM;
1322 }
1323 if (!handle->offset)
1324 handle->buffer = buffer;
546e0d27 1325 handle->sync_read = 1;
fb13a28b 1326 if (handle->prev < handle->cur) {
f577eb30 1327 if (!handle->prev) {
546e0d27
AM
1328 error = load_header(handle,
1329 (struct swsusp_info *)buffer);
f577eb30
RW
1330 if (error)
1331 return error;
1332 } else if (handle->prev <= nr_meta_pages) {
546e0d27
AM
1333 handle->pbe = unpack_orig_addresses(buffer,
1334 handle->pbe);
f577eb30 1335 if (!handle->pbe) {
968808b8 1336 error = prepare_image(handle);
f577eb30
RW
1337 if (error)
1338 return error;
75534b50 1339 handle->pbe = restore_pblist;
968808b8
RW
1340 handle->last_pbe = NULL;
1341 handle->buffer = get_buffer(handle);
546e0d27 1342 handle->sync_read = 0;
f577eb30
RW
1343 }
1344 } else {
1345 handle->pbe = handle->pbe->next;
968808b8 1346 handle->buffer = get_buffer(handle);
546e0d27 1347 handle->sync_read = 0;
f577eb30 1348 }
fb13a28b 1349 handle->prev = handle->cur;
f577eb30 1350 }
fb13a28b
RW
1351 handle->buf_offset = handle->cur_offset;
1352 if (handle->cur_offset + count >= PAGE_SIZE) {
1353 count = PAGE_SIZE - handle->cur_offset;
1354 handle->cur_offset = 0;
1355 handle->cur++;
f577eb30 1356 } else {
fb13a28b 1357 handle->cur_offset += count;
f577eb30
RW
1358 }
1359 handle->offset += count;
1360 return count;
1361}
1362
1363int snapshot_image_loaded(struct snapshot_handle *handle)
1364{
1365 return !(!handle->pbe || handle->pbe->next || !nr_copy_pages ||
fb13a28b 1366 handle->cur <= nr_meta_pages + nr_copy_pages);
f577eb30 1367}