swsusp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. /*
  2. * linux/kernel/power/swsusp.c
  3. *
  4. * This file provides code to write suspend image to swap and read it back.
  5. *
  6. * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
  7. * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
  8. *
  9. * This file is released under the GPLv2.
  10. *
  11. * I'd like to thank the following people for their work:
  12. *
  13. * Pavel Machek <pavel@ucw.cz>:
  14. * Modifications, defectiveness pointing, being with me at the very beginning,
  15. * suspend to swap space, stop all tasks. Port to 2.4.18-ac and 2.5.17.
  16. *
  17. * Steve Doddi <dirk@loth.demon.co.uk>:
  18. * Support the possibility of hardware state restoring.
  19. *
  20. * Raph <grey.havens@earthling.net>:
  21. * Support for preserving states of network devices and virtual console
  22. * (including X and svgatextmode)
  23. *
  24. * Kurt Garloff <garloff@suse.de>:
  25. * Straightened the critical function in order to prevent compilers from
  26. * playing tricks with local variables.
  27. *
  28. * Andreas Mohr <a.mohr@mailto.de>
  29. *
  30. * Alex Badea <vampire@go.ro>:
  31. * Fixed runaway init
  32. *
  33. * Rafael J. Wysocki <rjw@sisk.pl>
  34. * Added the swap map data structure and reworked the handling of swap
  35. *
  36. * More state savers are welcome. Especially for the scsi layer...
  37. *
  38. * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
  39. */
  40. #include <linux/module.h>
  41. #include <linux/mm.h>
  42. #include <linux/suspend.h>
  43. #include <linux/smp_lock.h>
  44. #include <linux/file.h>
  45. #include <linux/utsname.h>
  46. #include <linux/version.h>
  47. #include <linux/delay.h>
  48. #include <linux/bitops.h>
  49. #include <linux/spinlock.h>
  50. #include <linux/genhd.h>
  51. #include <linux/kernel.h>
  52. #include <linux/major.h>
  53. #include <linux/swap.h>
  54. #include <linux/pm.h>
  55. #include <linux/device.h>
  56. #include <linux/buffer_head.h>
  57. #include <linux/swapops.h>
  58. #include <linux/bootmem.h>
  59. #include <linux/syscalls.h>
  60. #include <linux/highmem.h>
  61. #include <linux/bio.h>
  62. #include <asm/uaccess.h>
  63. #include <asm/mmu_context.h>
  64. #include <asm/pgtable.h>
  65. #include <asm/tlbflush.h>
  66. #include <asm/io.h>
  67. #include "power.h"
  68. /*
  69. * Preferred image size in bytes (tunable via /sys/power/image_size).
  70. * When it is set to N, swsusp will do its best to ensure the image
  71. * size will not exceed N bytes, but if that is impossible, it will
  72. * try to create the smallest image possible.
  73. */
  74. unsigned long image_size = 500 * 1024 * 1024;
  75. #ifdef CONFIG_HIGHMEM
  76. unsigned int count_highmem_pages(void);
  77. int save_highmem(void);
  78. int restore_highmem(void);
  79. #else
  80. static int save_highmem(void) { return 0; }
  81. static int restore_highmem(void) { return 0; }
  82. static unsigned int count_highmem_pages(void) { return 0; }
  83. #endif
  84. extern char resume_file[];
  85. #define SWSUSP_SIG "S1SUSPEND"
  86. static struct swsusp_header {
  87. char reserved[PAGE_SIZE - 20 - sizeof(swp_entry_t)];
  88. swp_entry_t image;
  89. char orig_sig[10];
  90. char sig[10];
  91. } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
  92. static struct swsusp_info swsusp_info;
  93. /*
  94. * Saving part...
  95. */
  96. static unsigned short root_swap = 0xffff;
  97. static int mark_swapfiles(swp_entry_t start)
  98. {
  99. int error;
  100. rw_swap_page_sync(READ,
  101. swp_entry(root_swap, 0),
  102. virt_to_page((unsigned long)&swsusp_header));
  103. if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
  104. !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
  105. memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
  106. memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
  107. swsusp_header.image = start;
  108. error = rw_swap_page_sync(WRITE,
  109. swp_entry(root_swap, 0),
  110. virt_to_page((unsigned long)
  111. &swsusp_header));
  112. } else {
  113. pr_debug("swsusp: Partition is not swap space.\n");
  114. error = -ENODEV;
  115. }
  116. return error;
  117. }
  118. /*
  119. * Check whether the swap device is the specified resume
  120. * device, irrespective of whether they are specified by
  121. * identical names.
  122. *
  123. * (Thus, device inode aliasing is allowed. You can say /dev/hda4
  124. * instead of /dev/ide/host0/bus0/target0/lun0/part4 [if using devfs]
  125. * and they'll be considered the same device. This is *necessary* for
  126. * devfs, since the resume code can only recognize the form /dev/hda4,
  127. * but the suspend code would see the long name.)
  128. */
  129. static inline int is_resume_device(const struct swap_info_struct *swap_info)
  130. {
  131. struct file *file = swap_info->swap_file;
  132. struct inode *inode = file->f_dentry->d_inode;
  133. return S_ISBLK(inode->i_mode) &&
  134. swsusp_resume_device == MKDEV(imajor(inode), iminor(inode));
  135. }
  136. static int swsusp_swap_check(void) /* This is called before saving image */
  137. {
  138. int i;
  139. spin_lock(&swap_lock);
  140. for (i = 0; i < MAX_SWAPFILES; i++) {
  141. if (!(swap_info[i].flags & SWP_WRITEOK))
  142. continue;
  143. if (!swsusp_resume_device || is_resume_device(swap_info + i)) {
  144. spin_unlock(&swap_lock);
  145. root_swap = i;
  146. return 0;
  147. }
  148. }
  149. spin_unlock(&swap_lock);
  150. return -ENODEV;
  151. }
  152. /**
  153. * write_page - Write one page to a fresh swap location.
  154. * @addr: Address we're writing.
  155. * @loc: Place to store the entry we used.
  156. *
  157. * Allocate a new swap entry and 'sync' it. Note we discard -EIO
  158. * errors. That is an artifact left over from swsusp. It did not
  159. * check the return of rw_swap_page_sync() at all, since most pages
  160. * written back to swap would return -EIO.
  161. * This is a partial improvement, since we will at least return other
  162. * errors, though we need to eventually fix the damn code.
  163. */
  164. static int write_page(unsigned long addr, swp_entry_t *loc)
  165. {
  166. swp_entry_t entry;
  167. int error = -ENOSPC;
  168. entry = get_swap_page_of_type(root_swap);
  169. if (swp_offset(entry)) {
  170. error = rw_swap_page_sync(WRITE, entry, virt_to_page(addr));
  171. if (!error || error == -EIO)
  172. *loc = entry;
  173. }
  174. return error;
  175. }
  176. /**
  177. * Swap map-handling functions
  178. *
  179. * The swap map is a data structure used for keeping track of each page
  180. * written to the swap. It consists of many swap_map_page structures
  181. * that contain each an array of MAP_PAGE_SIZE swap entries.
  182. * These structures are linked together with the help of either the
  183. * .next (in memory) or the .next_swap (in swap) member.
  184. *
  185. * The swap map is created during suspend. At that time we need to keep
  186. * it in memory, because we have to free all of the allocated swap
  187. * entries if an error occurs. The memory needed is preallocated
  188. * so that we know in advance if there's enough of it.
  189. *
  190. * The first swap_map_page structure is filled with the swap entries that
  191. * correspond to the first MAP_PAGE_SIZE data pages written to swap and
  192. * so on. After the all of the data pages have been written, the order
  193. * of the swap_map_page structures in the map is reversed so that they
  194. * can be read from swap in the original order. This causes the data
  195. * pages to be loaded in exactly the same order in which they have been
  196. * saved.
  197. *
  198. * During resume we only need to use one swap_map_page structure
  199. * at a time, which means that we only need to use two memory pages for
  200. * reading the image - one for reading the swap_map_page structures
  201. * and the second for reading the data pages from swap.
  202. */
  203. #define MAP_PAGE_SIZE ((PAGE_SIZE - sizeof(swp_entry_t) - sizeof(void *)) \
  204. / sizeof(swp_entry_t))
  205. struct swap_map_page {
  206. swp_entry_t entries[MAP_PAGE_SIZE];
  207. swp_entry_t next_swap;
  208. struct swap_map_page *next;
  209. };
  210. static inline void free_swap_map(struct swap_map_page *swap_map)
  211. {
  212. struct swap_map_page *swp;
  213. while (swap_map) {
  214. swp = swap_map->next;
  215. free_page((unsigned long)swap_map);
  216. swap_map = swp;
  217. }
  218. }
  219. static struct swap_map_page *alloc_swap_map(unsigned int nr_pages)
  220. {
  221. struct swap_map_page *swap_map, *swp;
  222. unsigned n = 0;
  223. if (!nr_pages)
  224. return NULL;
  225. pr_debug("alloc_swap_map(): nr_pages = %d\n", nr_pages);
  226. swap_map = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
  227. swp = swap_map;
  228. for (n = MAP_PAGE_SIZE; n < nr_pages; n += MAP_PAGE_SIZE) {
  229. swp->next = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
  230. swp = swp->next;
  231. if (!swp) {
  232. free_swap_map(swap_map);
  233. return NULL;
  234. }
  235. }
  236. return swap_map;
  237. }
  238. /**
  239. * reverse_swap_map - reverse the order of pages in the swap map
  240. * @swap_map
  241. */
  242. static inline struct swap_map_page *reverse_swap_map(struct swap_map_page *swap_map)
  243. {
  244. struct swap_map_page *prev, *next;
  245. prev = NULL;
  246. while (swap_map) {
  247. next = swap_map->next;
  248. swap_map->next = prev;
  249. prev = swap_map;
  250. swap_map = next;
  251. }
  252. return prev;
  253. }
  254. /**
  255. * free_swap_map_entries - free the swap entries allocated to store
  256. * the swap map @swap_map (this is only called in case of an error)
  257. */
  258. static inline void free_swap_map_entries(struct swap_map_page *swap_map)
  259. {
  260. while (swap_map) {
  261. if (swap_map->next_swap.val)
  262. swap_free(swap_map->next_swap);
  263. swap_map = swap_map->next;
  264. }
  265. }
  266. /**
  267. * save_swap_map - save the swap map used for tracing the data pages
  268. * stored in the swap
  269. */
  270. static int save_swap_map(struct swap_map_page *swap_map, swp_entry_t *start)
  271. {
  272. swp_entry_t entry = (swp_entry_t){0};
  273. int error;
  274. while (swap_map) {
  275. swap_map->next_swap = entry;
  276. if ((error = write_page((unsigned long)swap_map, &entry)))
  277. return error;
  278. swap_map = swap_map->next;
  279. }
  280. *start = entry;
  281. return 0;
  282. }
  283. /**
  284. * free_image_entries - free the swap entries allocated to store
  285. * the image data pages (this is only called in case of an error)
  286. */
  287. static inline void free_image_entries(struct swap_map_page *swp)
  288. {
  289. unsigned k;
  290. while (swp) {
  291. for (k = 0; k < MAP_PAGE_SIZE; k++)
  292. if (swp->entries[k].val)
  293. swap_free(swp->entries[k]);
  294. swp = swp->next;
  295. }
  296. }
  297. /**
  298. * The swap_map_handle structure is used for handling the swap map in
  299. * a file-alike way
  300. */
  301. struct swap_map_handle {
  302. struct swap_map_page *cur;
  303. unsigned int k;
  304. };
  305. static inline void init_swap_map_handle(struct swap_map_handle *handle,
  306. struct swap_map_page *map)
  307. {
  308. handle->cur = map;
  309. handle->k = 0;
  310. }
  311. static inline int swap_map_write_page(struct swap_map_handle *handle,
  312. unsigned long addr)
  313. {
  314. int error;
  315. error = write_page(addr, handle->cur->entries + handle->k);
  316. if (error)
  317. return error;
  318. if (++handle->k >= MAP_PAGE_SIZE) {
  319. handle->cur = handle->cur->next;
  320. handle->k = 0;
  321. }
  322. return 0;
  323. }
  324. /**
  325. * save_image_data - save the data pages pointed to by the PBEs
  326. * from the list @pblist using the swap map handle @handle
  327. * (assume there are @nr_pages data pages to save)
  328. */
  329. static int save_image_data(struct pbe *pblist,
  330. struct swap_map_handle *handle,
  331. unsigned int nr_pages)
  332. {
  333. unsigned int m;
  334. struct pbe *p;
  335. int error = 0;
  336. printk("Saving image data pages (%u pages) ... ", nr_pages);
  337. m = nr_pages / 100;
  338. if (!m)
  339. m = 1;
  340. nr_pages = 0;
  341. for_each_pbe (p, pblist) {
  342. error = swap_map_write_page(handle, p->address);
  343. if (error)
  344. break;
  345. if (!(nr_pages % m))
  346. printk("\b\b\b\b%3d%%", nr_pages / m);
  347. nr_pages++;
  348. }
  349. if (!error)
  350. printk("\b\b\b\bdone\n");
  351. return error;
  352. }
  353. static void dump_info(void)
  354. {
  355. pr_debug(" swsusp: Version: %u\n",swsusp_info.version_code);
  356. pr_debug(" swsusp: Num Pages: %ld\n",swsusp_info.num_physpages);
  357. pr_debug(" swsusp: UTS Sys: %s\n",swsusp_info.uts.sysname);
  358. pr_debug(" swsusp: UTS Node: %s\n",swsusp_info.uts.nodename);
  359. pr_debug(" swsusp: UTS Release: %s\n",swsusp_info.uts.release);
  360. pr_debug(" swsusp: UTS Version: %s\n",swsusp_info.uts.version);
  361. pr_debug(" swsusp: UTS Machine: %s\n",swsusp_info.uts.machine);
  362. pr_debug(" swsusp: UTS Domain: %s\n",swsusp_info.uts.domainname);
  363. pr_debug(" swsusp: CPUs: %d\n",swsusp_info.cpus);
  364. pr_debug(" swsusp: Image: %ld Pages\n",swsusp_info.image_pages);
  365. pr_debug(" swsusp: Total: %ld Pages\n", swsusp_info.pages);
  366. }
  367. static void init_header(unsigned int nr_pages)
  368. {
  369. memset(&swsusp_info, 0, sizeof(swsusp_info));
  370. swsusp_info.version_code = LINUX_VERSION_CODE;
  371. swsusp_info.num_physpages = num_physpages;
  372. memcpy(&swsusp_info.uts, &system_utsname, sizeof(system_utsname));
  373. swsusp_info.cpus = num_online_cpus();
  374. swsusp_info.image_pages = nr_pages;
  375. swsusp_info.pages = nr_pages +
  376. ((nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT) + 1;
  377. }
  378. /**
  379. * pack_orig_addresses - the .orig_address fields of the PBEs from the
  380. * list starting at @pbe are stored in the array @buf[] (1 page)
  381. */
  382. static inline struct pbe *pack_orig_addresses(unsigned long *buf,
  383. struct pbe *pbe)
  384. {
  385. int j;
  386. for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
  387. buf[j] = pbe->orig_address;
  388. pbe = pbe->next;
  389. }
  390. if (!pbe)
  391. for (; j < PAGE_SIZE / sizeof(long); j++)
  392. buf[j] = 0;
  393. return pbe;
  394. }
  395. /**
  396. * save_image_metadata - save the .orig_address fields of the PBEs
  397. * from the list @pblist using the swap map handle @handle
  398. */
  399. static int save_image_metadata(struct pbe *pblist,
  400. struct swap_map_handle *handle)
  401. {
  402. unsigned long *buf;
  403. unsigned int n = 0;
  404. struct pbe *p;
  405. int error = 0;
  406. printk("Saving image metadata ... ");
  407. buf = (unsigned long *)get_zeroed_page(GFP_ATOMIC);
  408. if (!buf)
  409. return -ENOMEM;
  410. p = pblist;
  411. while (p) {
  412. p = pack_orig_addresses(buf, p);
  413. error = swap_map_write_page(handle, (unsigned long)buf);
  414. if (error)
  415. break;
  416. n++;
  417. }
  418. free_page((unsigned long)buf);
  419. if (!error)
  420. printk("done (%u pages saved)\n", n);
  421. return error;
  422. }
  423. /**
  424. * enough_swap - Make sure we have enough swap to save the image.
  425. *
  426. * Returns TRUE or FALSE after checking the total amount of swap
  427. * space avaiable from the resume partition.
  428. */
  429. static int enough_swap(unsigned int nr_pages)
  430. {
  431. unsigned int free_swap = swap_info[root_swap].pages -
  432. swap_info[root_swap].inuse_pages;
  433. pr_debug("swsusp: free swap pages: %u\n", free_swap);
  434. return free_swap > (nr_pages + PAGES_FOR_IO +
  435. (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
  436. }
  437. /**
  438. * swsusp_write - Write entire image and metadata.
  439. *
  440. * It is important _NOT_ to umount filesystems at this point. We want
  441. * them synced (in case something goes wrong) but we DO not want to mark
  442. * filesystem clean: it is not. (And it does not matter, if we resume
  443. * correctly, we'll mark system clean, anyway.)
  444. */
  445. int swsusp_write(struct pbe *pblist, unsigned int nr_pages)
  446. {
  447. struct swap_map_page *swap_map;
  448. struct swap_map_handle handle;
  449. swp_entry_t start;
  450. int error;
  451. if ((error = swsusp_swap_check())) {
  452. printk(KERN_ERR "swsusp: Cannot find swap device, try swapon -a.\n");
  453. return error;
  454. }
  455. if (!enough_swap(nr_pages)) {
  456. printk(KERN_ERR "swsusp: Not enough free swap\n");
  457. return -ENOSPC;
  458. }
  459. init_header(nr_pages);
  460. swap_map = alloc_swap_map(swsusp_info.pages);
  461. if (!swap_map)
  462. return -ENOMEM;
  463. init_swap_map_handle(&handle, swap_map);
  464. error = swap_map_write_page(&handle, (unsigned long)&swsusp_info);
  465. if (!error)
  466. error = save_image_metadata(pblist, &handle);
  467. if (!error)
  468. error = save_image_data(pblist, &handle, nr_pages);
  469. if (error)
  470. goto Free_image_entries;
  471. swap_map = reverse_swap_map(swap_map);
  472. error = save_swap_map(swap_map, &start);
  473. if (error)
  474. goto Free_map_entries;
  475. dump_info();
  476. printk( "S" );
  477. error = mark_swapfiles(start);
  478. printk( "|\n" );
  479. if (error)
  480. goto Free_map_entries;
  481. Free_swap_map:
  482. free_swap_map(swap_map);
  483. return error;
  484. Free_map_entries:
  485. free_swap_map_entries(swap_map);
  486. Free_image_entries:
  487. free_image_entries(swap_map);
  488. goto Free_swap_map;
  489. }
  490. /**
  491. * swsusp_shrink_memory - Try to free as much memory as needed
  492. *
  493. * ... but do not OOM-kill anyone
  494. *
  495. * Notice: all userland should be stopped before it is called, or
  496. * livelock is possible.
  497. */
  498. #define SHRINK_BITE 10000
  499. int swsusp_shrink_memory(void)
  500. {
  501. long size, tmp;
  502. struct zone *zone;
  503. unsigned long pages = 0;
  504. unsigned int i = 0;
  505. char *p = "-\\|/";
  506. printk("Shrinking memory... ");
  507. do {
  508. size = 2 * count_highmem_pages();
  509. size += size / 50 + count_data_pages();
  510. size += (size + PBES_PER_PAGE - 1) / PBES_PER_PAGE +
  511. PAGES_FOR_IO;
  512. tmp = size;
  513. for_each_zone (zone)
  514. if (!is_highmem(zone))
  515. tmp -= zone->free_pages;
  516. if (tmp > 0) {
  517. tmp = shrink_all_memory(SHRINK_BITE);
  518. if (!tmp)
  519. return -ENOMEM;
  520. pages += tmp;
  521. } else if (size > image_size / PAGE_SIZE) {
  522. tmp = shrink_all_memory(SHRINK_BITE);
  523. pages += tmp;
  524. }
  525. printk("\b%c", p[i++%4]);
  526. } while (tmp > 0);
  527. printk("\bdone (%lu pages freed)\n", pages);
  528. return 0;
  529. }
  530. int swsusp_suspend(void)
  531. {
  532. int error;
  533. if ((error = arch_prepare_suspend()))
  534. return error;
  535. local_irq_disable();
  536. /* At this point, device_suspend() has been called, but *not*
  537. * device_power_down(). We *must* device_power_down() now.
  538. * Otherwise, drivers for some devices (e.g. interrupt controllers)
  539. * become desynchronized with the actual state of the hardware
  540. * at resume time, and evil weirdness ensues.
  541. */
  542. if ((error = device_power_down(PMSG_FREEZE))) {
  543. printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
  544. goto Enable_irqs;
  545. }
  546. if ((error = save_highmem())) {
  547. printk(KERN_ERR "swsusp: Not enough free pages for highmem\n");
  548. goto Restore_highmem;
  549. }
  550. save_processor_state();
  551. if ((error = swsusp_arch_suspend()))
  552. printk(KERN_ERR "Error %d suspending\n", error);
  553. /* Restore control flow magically appears here */
  554. restore_processor_state();
  555. Restore_highmem:
  556. restore_highmem();
  557. device_power_up();
  558. Enable_irqs:
  559. local_irq_enable();
  560. return error;
  561. }
  562. int swsusp_resume(void)
  563. {
  564. int error;
  565. local_irq_disable();
  566. if (device_power_down(PMSG_FREEZE))
  567. printk(KERN_ERR "Some devices failed to power down, very bad\n");
  568. /* We'll ignore saved state, but this gets preempt count (etc) right */
  569. save_processor_state();
  570. error = swsusp_arch_resume();
  571. /* Code below is only ever reached in case of failure. Otherwise
  572. * execution continues at place where swsusp_arch_suspend was called
  573. */
  574. BUG_ON(!error);
  575. /* The only reason why swsusp_arch_resume() can fail is memory being
  576. * very tight, so we have to free it as soon as we can to avoid
  577. * subsequent failures
  578. */
  579. swsusp_free();
  580. restore_processor_state();
  581. restore_highmem();
  582. touch_softlockup_watchdog();
  583. device_power_up();
  584. local_irq_enable();
  585. return error;
  586. }
  587. /**
  588. * mark_unsafe_pages - mark the pages that cannot be used for storing
  589. * the image during resume, because they conflict with the pages that
  590. * had been used before suspend
  591. */
  592. static void mark_unsafe_pages(struct pbe *pblist)
  593. {
  594. struct zone *zone;
  595. unsigned long zone_pfn;
  596. struct pbe *p;
  597. if (!pblist) /* a sanity check */
  598. return;
  599. /* Clear page flags */
  600. for_each_zone (zone) {
  601. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  602. if (pfn_valid(zone_pfn + zone->zone_start_pfn))
  603. ClearPageNosaveFree(pfn_to_page(zone_pfn +
  604. zone->zone_start_pfn));
  605. }
  606. /* Mark orig addresses */
  607. for_each_pbe (p, pblist)
  608. SetPageNosaveFree(virt_to_page(p->orig_address));
  609. }
  610. static void copy_page_backup_list(struct pbe *dst, struct pbe *src)
  611. {
  612. /* We assume both lists contain the same number of elements */
  613. while (src) {
  614. dst->orig_address = src->orig_address;
  615. dst = dst->next;
  616. src = src->next;
  617. }
  618. }
  619. /*
  620. * Using bio to read from swap.
  621. * This code requires a bit more work than just using buffer heads
  622. * but, it is the recommended way for 2.5/2.6.
  623. * The following are to signal the beginning and end of I/O. Bios
  624. * finish asynchronously, while we want them to happen synchronously.
  625. * A simple atomic_t, and a wait loop take care of this problem.
  626. */
  627. static atomic_t io_done = ATOMIC_INIT(0);
  628. static int end_io(struct bio *bio, unsigned int num, int err)
  629. {
  630. if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  631. panic("I/O error reading memory image");
  632. atomic_set(&io_done, 0);
  633. return 0;
  634. }
  635. static struct block_device *resume_bdev;
  636. /**
  637. * submit - submit BIO request.
  638. * @rw: READ or WRITE.
  639. * @off physical offset of page.
  640. * @page: page we're reading or writing.
  641. *
  642. * Straight from the textbook - allocate and initialize the bio.
  643. * If we're writing, make sure the page is marked as dirty.
  644. * Then submit it and wait.
  645. */
  646. static int submit(int rw, pgoff_t page_off, void *page)
  647. {
  648. int error = 0;
  649. struct bio *bio;
  650. bio = bio_alloc(GFP_ATOMIC, 1);
  651. if (!bio)
  652. return -ENOMEM;
  653. bio->bi_sector = page_off * (PAGE_SIZE >> 9);
  654. bio->bi_bdev = resume_bdev;
  655. bio->bi_end_io = end_io;
  656. if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
  657. printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
  658. error = -EFAULT;
  659. goto Done;
  660. }
  661. atomic_set(&io_done, 1);
  662. submit_bio(rw | (1 << BIO_RW_SYNC), bio);
  663. while (atomic_read(&io_done))
  664. yield();
  665. if (rw == READ)
  666. bio_set_pages_dirty(bio);
  667. Done:
  668. bio_put(bio);
  669. return error;
  670. }
  671. static int bio_read_page(pgoff_t page_off, void *page)
  672. {
  673. return submit(READ, page_off, page);
  674. }
  675. static int bio_write_page(pgoff_t page_off, void *page)
  676. {
  677. return submit(WRITE, page_off, page);
  678. }
  679. /**
  680. * The following functions allow us to read data using a swap map
  681. * in a file-alike way
  682. */
  683. static inline void release_swap_map_reader(struct swap_map_handle *handle)
  684. {
  685. if (handle->cur)
  686. free_page((unsigned long)handle->cur);
  687. handle->cur = NULL;
  688. }
  689. static inline int get_swap_map_reader(struct swap_map_handle *handle,
  690. swp_entry_t start)
  691. {
  692. int error;
  693. if (!swp_offset(start))
  694. return -EINVAL;
  695. handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
  696. if (!handle->cur)
  697. return -ENOMEM;
  698. error = bio_read_page(swp_offset(start), handle->cur);
  699. if (error) {
  700. release_swap_map_reader(handle);
  701. return error;
  702. }
  703. handle->k = 0;
  704. return 0;
  705. }
  706. static inline int swap_map_read_page(struct swap_map_handle *handle, void *buf)
  707. {
  708. unsigned long offset;
  709. int error;
  710. if (!handle->cur)
  711. return -EINVAL;
  712. offset = swp_offset(handle->cur->entries[handle->k]);
  713. if (!offset)
  714. return -EINVAL;
  715. error = bio_read_page(offset, buf);
  716. if (error)
  717. return error;
  718. if (++handle->k >= MAP_PAGE_SIZE) {
  719. handle->k = 0;
  720. offset = swp_offset(handle->cur->next_swap);
  721. if (!offset)
  722. release_swap_map_reader(handle);
  723. else
  724. error = bio_read_page(offset, handle->cur);
  725. }
  726. return error;
  727. }
  728. static int check_header(void)
  729. {
  730. char *reason = NULL;
  731. dump_info();
  732. if (swsusp_info.version_code != LINUX_VERSION_CODE)
  733. reason = "kernel version";
  734. if (swsusp_info.num_physpages != num_physpages)
  735. reason = "memory size";
  736. if (strcmp(swsusp_info.uts.sysname,system_utsname.sysname))
  737. reason = "system type";
  738. if (strcmp(swsusp_info.uts.release,system_utsname.release))
  739. reason = "kernel release";
  740. if (strcmp(swsusp_info.uts.version,system_utsname.version))
  741. reason = "version";
  742. if (strcmp(swsusp_info.uts.machine,system_utsname.machine))
  743. reason = "machine";
  744. if (reason) {
  745. printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
  746. return -EPERM;
  747. }
  748. return 0;
  749. }
  750. /**
  751. * load_image_data - load the image data using the swap map handle
  752. * @handle and store them using the page backup list @pblist
  753. * (assume there are @nr_pages pages to load)
  754. */
  755. static int load_image_data(struct pbe *pblist,
  756. struct swap_map_handle *handle,
  757. unsigned int nr_pages)
  758. {
  759. int error;
  760. unsigned int m;
  761. struct pbe *p;
  762. if (!pblist)
  763. return -EINVAL;
  764. printk("Loading image data pages (%u pages) ... ", nr_pages);
  765. m = nr_pages / 100;
  766. if (!m)
  767. m = 1;
  768. nr_pages = 0;
  769. p = pblist;
  770. while (p) {
  771. error = swap_map_read_page(handle, (void *)p->address);
  772. if (error)
  773. break;
  774. p = p->next;
  775. if (!(nr_pages % m))
  776. printk("\b\b\b\b%3d%%", nr_pages / m);
  777. nr_pages++;
  778. }
  779. if (!error)
  780. printk("\b\b\b\bdone\n");
  781. return error;
  782. }
  783. /**
  784. * unpack_orig_addresses - copy the elements of @buf[] (1 page) to
  785. * the PBEs in the list starting at @pbe
  786. */
  787. static inline struct pbe *unpack_orig_addresses(unsigned long *buf,
  788. struct pbe *pbe)
  789. {
  790. int j;
  791. for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
  792. pbe->orig_address = buf[j];
  793. pbe = pbe->next;
  794. }
  795. return pbe;
  796. }
  797. /**
  798. * load_image_metadata - load the image metadata using the swap map
  799. * handle @handle and put them into the PBEs in the list @pblist
  800. */
  801. static int load_image_metadata(struct pbe *pblist, struct swap_map_handle *handle)
  802. {
  803. struct pbe *p;
  804. unsigned long *buf;
  805. unsigned int n = 0;
  806. int error = 0;
  807. printk("Loading image metadata ... ");
  808. buf = (unsigned long *)get_zeroed_page(GFP_ATOMIC);
  809. if (!buf)
  810. return -ENOMEM;
  811. p = pblist;
  812. while (p) {
  813. error = swap_map_read_page(handle, buf);
  814. if (error)
  815. break;
  816. p = unpack_orig_addresses(buf, p);
  817. n++;
  818. }
  819. free_page((unsigned long)buf);
  820. if (!error)
  821. printk("done (%u pages loaded)\n", n);
  822. return error;
  823. }
  824. int swsusp_read(struct pbe **pblist_ptr)
  825. {
  826. int error;
  827. struct pbe *p, *pblist;
  828. struct swap_map_handle handle;
  829. unsigned int nr_pages;
  830. if (IS_ERR(resume_bdev)) {
  831. pr_debug("swsusp: block device not initialised\n");
  832. return PTR_ERR(resume_bdev);
  833. }
  834. error = get_swap_map_reader(&handle, swsusp_header.image);
  835. if (!error)
  836. error = swap_map_read_page(&handle, &swsusp_info);
  837. if (!error)
  838. error = check_header();
  839. if (error)
  840. return error;
  841. nr_pages = swsusp_info.image_pages;
  842. p = alloc_pagedir(nr_pages, GFP_ATOMIC, 0);
  843. if (!p)
  844. return -ENOMEM;
  845. error = load_image_metadata(p, &handle);
  846. if (!error) {
  847. mark_unsafe_pages(p);
  848. pblist = alloc_pagedir(nr_pages, GFP_ATOMIC, 1);
  849. if (pblist)
  850. copy_page_backup_list(pblist, p);
  851. free_pagedir(p);
  852. if (!pblist)
  853. error = -ENOMEM;
  854. /* Allocate memory for the image and read the data from swap */
  855. if (!error)
  856. error = alloc_data_pages(pblist, GFP_ATOMIC, 1);
  857. if (!error) {
  858. release_eaten_pages();
  859. error = load_image_data(pblist, &handle, nr_pages);
  860. }
  861. if (!error)
  862. *pblist_ptr = pblist;
  863. }
  864. release_swap_map_reader(&handle);
  865. blkdev_put(resume_bdev);
  866. if (!error)
  867. pr_debug("swsusp: Reading resume file was successful\n");
  868. else
  869. pr_debug("swsusp: Error %d resuming\n", error);
  870. return error;
  871. }
  872. /**
  873. * swsusp_check - Check for swsusp signature in the resume device
  874. */
  875. int swsusp_check(void)
  876. {
  877. int error;
  878. resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
  879. if (!IS_ERR(resume_bdev)) {
  880. set_blocksize(resume_bdev, PAGE_SIZE);
  881. memset(&swsusp_header, 0, sizeof(swsusp_header));
  882. if ((error = bio_read_page(0, &swsusp_header)))
  883. return error;
  884. if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
  885. memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
  886. /* Reset swap signature now */
  887. error = bio_write_page(0, &swsusp_header);
  888. } else {
  889. return -EINVAL;
  890. }
  891. if (error)
  892. blkdev_put(resume_bdev);
  893. else
  894. pr_debug("swsusp: Signature found, resuming\n");
  895. } else {
  896. error = PTR_ERR(resume_bdev);
  897. }
  898. if (error)
  899. pr_debug("swsusp: Error %d check for resume file\n", error);
  900. return error;
  901. }
  902. /**
  903. * swsusp_close - close swap device.
  904. */
  905. void swsusp_close(void)
  906. {
  907. if (IS_ERR(resume_bdev)) {
  908. pr_debug("swsusp: block device not initialised\n");
  909. return;
  910. }
  911. blkdev_put(resume_bdev);
  912. }