snapshot.c 20 KB

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