snapshot.c 20 KB

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