swsusp.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. * Andreas Steinmetz <ast@domdv.de>:
  34. * Added encrypted suspend option
  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 <linux/random.h>
  68. #include <linux/crypto.h>
  69. #include <asm/scatterlist.h>
  70. #include "power.h"
  71. #ifdef CONFIG_HIGHMEM
  72. int save_highmem(void);
  73. int restore_highmem(void);
  74. #else
  75. static int save_highmem(void) { return 0; }
  76. static int restore_highmem(void) { return 0; }
  77. #endif
  78. #define CIPHER "aes"
  79. #define MAXKEY 32
  80. #define MAXIV 32
  81. extern char resume_file[];
  82. /* Local variables that should not be affected by save */
  83. unsigned int nr_copy_pages __nosavedata = 0;
  84. /* Suspend pagedir is allocated before final copy, therefore it
  85. must be freed after resume
  86. Warning: this is even more evil than it seems. Pagedirs this file
  87. talks about are completely different from page directories used by
  88. MMU hardware.
  89. */
  90. suspend_pagedir_t *pagedir_nosave __nosavedata = NULL;
  91. #define SWSUSP_SIG "S1SUSPEND"
  92. static struct swsusp_header {
  93. char reserved[PAGE_SIZE - 20 - MAXKEY - MAXIV - sizeof(swp_entry_t)];
  94. u8 key_iv[MAXKEY+MAXIV];
  95. swp_entry_t swsusp_info;
  96. char orig_sig[10];
  97. char sig[10];
  98. } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
  99. static struct swsusp_info swsusp_info;
  100. /*
  101. * Saving part...
  102. */
  103. /* We memorize in swapfile_used what swap devices are used for suspension */
  104. #define SWAPFILE_UNUSED 0
  105. #define SWAPFILE_SUSPEND 1 /* This is the suspending device */
  106. #define SWAPFILE_IGNORED 2 /* Those are other swap devices ignored for suspension */
  107. static unsigned short swapfile_used[MAX_SWAPFILES];
  108. static unsigned short root_swap;
  109. static int write_page(unsigned long addr, swp_entry_t *loc);
  110. static int bio_read_page(pgoff_t page_off, void *page);
  111. static u8 key_iv[MAXKEY+MAXIV];
  112. #ifdef CONFIG_SWSUSP_ENCRYPT
  113. static int crypto_init(int mode, void **mem)
  114. {
  115. int error = 0;
  116. int len;
  117. char *modemsg;
  118. struct crypto_tfm *tfm;
  119. modemsg = mode ? "suspend not possible" : "resume not possible";
  120. tfm = crypto_alloc_tfm(CIPHER, CRYPTO_TFM_MODE_CBC);
  121. if(!tfm) {
  122. printk(KERN_ERR "swsusp: no tfm, %s\n", modemsg);
  123. error = -EINVAL;
  124. goto out;
  125. }
  126. if(MAXKEY < crypto_tfm_alg_min_keysize(tfm)) {
  127. printk(KERN_ERR "swsusp: key buffer too small, %s\n", modemsg);
  128. error = -ENOKEY;
  129. goto fail;
  130. }
  131. if (mode)
  132. get_random_bytes(key_iv, MAXKEY+MAXIV);
  133. len = crypto_tfm_alg_max_keysize(tfm);
  134. if (len > MAXKEY)
  135. len = MAXKEY;
  136. if (crypto_cipher_setkey(tfm, key_iv, len)) {
  137. printk(KERN_ERR "swsusp: key setup failure, %s\n", modemsg);
  138. error = -EKEYREJECTED;
  139. goto fail;
  140. }
  141. len = crypto_tfm_alg_ivsize(tfm);
  142. if (MAXIV < len) {
  143. printk(KERN_ERR "swsusp: iv buffer too small, %s\n", modemsg);
  144. error = -EOVERFLOW;
  145. goto fail;
  146. }
  147. crypto_cipher_set_iv(tfm, key_iv+MAXKEY, len);
  148. *mem=(void *)tfm;
  149. goto out;
  150. fail: crypto_free_tfm(tfm);
  151. out: return error;
  152. }
  153. static __inline__ void crypto_exit(void *mem)
  154. {
  155. crypto_free_tfm((struct crypto_tfm *)mem);
  156. }
  157. static __inline__ int crypto_write(struct pbe *p, void *mem)
  158. {
  159. int error = 0;
  160. struct scatterlist src, dst;
  161. src.page = virt_to_page(p->address);
  162. src.offset = 0;
  163. src.length = PAGE_SIZE;
  164. dst.page = virt_to_page((void *)&swsusp_header);
  165. dst.offset = 0;
  166. dst.length = PAGE_SIZE;
  167. error = crypto_cipher_encrypt((struct crypto_tfm *)mem, &dst, &src,
  168. PAGE_SIZE);
  169. if (!error)
  170. error = write_page((unsigned long)&swsusp_header,
  171. &(p->swap_address));
  172. return error;
  173. }
  174. static __inline__ int crypto_read(struct pbe *p, void *mem)
  175. {
  176. int error = 0;
  177. struct scatterlist src, dst;
  178. error = bio_read_page(swp_offset(p->swap_address), (void *)p->address);
  179. if (!error) {
  180. src.offset = 0;
  181. src.length = PAGE_SIZE;
  182. dst.offset = 0;
  183. dst.length = PAGE_SIZE;
  184. src.page = dst.page = virt_to_page((void *)p->address);
  185. error = crypto_cipher_decrypt((struct crypto_tfm *)mem, &dst,
  186. &src, PAGE_SIZE);
  187. }
  188. return error;
  189. }
  190. #else
  191. static __inline__ int crypto_init(int mode, void *mem)
  192. {
  193. return 0;
  194. }
  195. static __inline__ void crypto_exit(void *mem)
  196. {
  197. }
  198. static __inline__ int crypto_write(struct pbe *p, void *mem)
  199. {
  200. return write_page(p->address, &(p->swap_address));
  201. }
  202. static __inline__ int crypto_read(struct pbe *p, void *mem)
  203. {
  204. return bio_read_page(swp_offset(p->swap_address), (void *)p->address);
  205. }
  206. #endif
  207. static int mark_swapfiles(swp_entry_t prev)
  208. {
  209. int error;
  210. rw_swap_page_sync(READ,
  211. swp_entry(root_swap, 0),
  212. virt_to_page((unsigned long)&swsusp_header));
  213. if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
  214. !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
  215. memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
  216. memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
  217. memcpy(swsusp_header.key_iv, key_iv, MAXKEY+MAXIV);
  218. swsusp_header.swsusp_info = prev;
  219. error = rw_swap_page_sync(WRITE,
  220. swp_entry(root_swap, 0),
  221. virt_to_page((unsigned long)
  222. &swsusp_header));
  223. } else {
  224. pr_debug("swsusp: Partition is not swap space.\n");
  225. error = -ENODEV;
  226. }
  227. return error;
  228. }
  229. /*
  230. * Check whether the swap device is the specified resume
  231. * device, irrespective of whether they are specified by
  232. * identical names.
  233. *
  234. * (Thus, device inode aliasing is allowed. You can say /dev/hda4
  235. * instead of /dev/ide/host0/bus0/target0/lun0/part4 [if using devfs]
  236. * and they'll be considered the same device. This is *necessary* for
  237. * devfs, since the resume code can only recognize the form /dev/hda4,
  238. * but the suspend code would see the long name.)
  239. */
  240. static int is_resume_device(const struct swap_info_struct *swap_info)
  241. {
  242. struct file *file = swap_info->swap_file;
  243. struct inode *inode = file->f_dentry->d_inode;
  244. return S_ISBLK(inode->i_mode) &&
  245. swsusp_resume_device == MKDEV(imajor(inode), iminor(inode));
  246. }
  247. static int swsusp_swap_check(void) /* This is called before saving image */
  248. {
  249. int i, len;
  250. len=strlen(resume_file);
  251. root_swap = 0xFFFF;
  252. spin_lock(&swap_lock);
  253. for (i=0; i<MAX_SWAPFILES; i++) {
  254. if (!(swap_info[i].flags & SWP_WRITEOK)) {
  255. swapfile_used[i]=SWAPFILE_UNUSED;
  256. } else {
  257. if (!len) {
  258. printk(KERN_WARNING "resume= option should be used to set suspend device" );
  259. if (root_swap == 0xFFFF) {
  260. swapfile_used[i] = SWAPFILE_SUSPEND;
  261. root_swap = i;
  262. } else
  263. swapfile_used[i] = SWAPFILE_IGNORED;
  264. } else {
  265. /* we ignore all swap devices that are not the resume_file */
  266. if (is_resume_device(&swap_info[i])) {
  267. swapfile_used[i] = SWAPFILE_SUSPEND;
  268. root_swap = i;
  269. } else {
  270. swapfile_used[i] = SWAPFILE_IGNORED;
  271. }
  272. }
  273. }
  274. }
  275. spin_unlock(&swap_lock);
  276. return (root_swap != 0xffff) ? 0 : -ENODEV;
  277. }
  278. /**
  279. * This is called after saving image so modification
  280. * will be lost after resume... and that's what we want.
  281. * we make the device unusable. A new call to
  282. * lock_swapdevices can unlock the devices.
  283. */
  284. static void lock_swapdevices(void)
  285. {
  286. int i;
  287. spin_lock(&swap_lock);
  288. for (i = 0; i< MAX_SWAPFILES; i++)
  289. if (swapfile_used[i] == SWAPFILE_IGNORED) {
  290. swap_info[i].flags ^= SWP_WRITEOK;
  291. }
  292. spin_unlock(&swap_lock);
  293. }
  294. /**
  295. * write_page - Write one page to a fresh swap location.
  296. * @addr: Address we're writing.
  297. * @loc: Place to store the entry we used.
  298. *
  299. * Allocate a new swap entry and 'sync' it. Note we discard -EIO
  300. * errors. That is an artifact left over from swsusp. It did not
  301. * check the return of rw_swap_page_sync() at all, since most pages
  302. * written back to swap would return -EIO.
  303. * This is a partial improvement, since we will at least return other
  304. * errors, though we need to eventually fix the damn code.
  305. */
  306. static int write_page(unsigned long addr, swp_entry_t *loc)
  307. {
  308. swp_entry_t entry;
  309. int error = 0;
  310. entry = get_swap_page();
  311. if (swp_offset(entry) &&
  312. swapfile_used[swp_type(entry)] == SWAPFILE_SUSPEND) {
  313. error = rw_swap_page_sync(WRITE, entry,
  314. virt_to_page(addr));
  315. if (error == -EIO)
  316. error = 0;
  317. if (!error)
  318. *loc = entry;
  319. } else
  320. error = -ENOSPC;
  321. return error;
  322. }
  323. /**
  324. * data_free - Free the swap entries used by the saved image.
  325. *
  326. * Walk the list of used swap entries and free each one.
  327. * This is only used for cleanup when suspend fails.
  328. */
  329. static void data_free(void)
  330. {
  331. swp_entry_t entry;
  332. struct pbe *p;
  333. for_each_pbe (p, pagedir_nosave) {
  334. entry = p->swap_address;
  335. if (entry.val)
  336. swap_free(entry);
  337. else
  338. break;
  339. }
  340. }
  341. /**
  342. * data_write - Write saved image to swap.
  343. *
  344. * Walk the list of pages in the image and sync each one to swap.
  345. */
  346. static int data_write(void)
  347. {
  348. int error = 0, i = 0;
  349. unsigned int mod = nr_copy_pages / 100;
  350. struct pbe *p;
  351. void *tfm;
  352. if ((error = crypto_init(1, &tfm)))
  353. return error;
  354. if (!mod)
  355. mod = 1;
  356. printk( "Writing data to swap (%d pages)... ", nr_copy_pages );
  357. for_each_pbe (p, pagedir_nosave) {
  358. if (!(i%mod))
  359. printk( "\b\b\b\b%3d%%", i / mod );
  360. if ((error = crypto_write(p, tfm))) {
  361. crypto_exit(tfm);
  362. return error;
  363. }
  364. i++;
  365. }
  366. printk("\b\b\b\bdone\n");
  367. crypto_exit(tfm);
  368. return error;
  369. }
  370. static void dump_info(void)
  371. {
  372. pr_debug(" swsusp: Version: %u\n",swsusp_info.version_code);
  373. pr_debug(" swsusp: Num Pages: %ld\n",swsusp_info.num_physpages);
  374. pr_debug(" swsusp: UTS Sys: %s\n",swsusp_info.uts.sysname);
  375. pr_debug(" swsusp: UTS Node: %s\n",swsusp_info.uts.nodename);
  376. pr_debug(" swsusp: UTS Release: %s\n",swsusp_info.uts.release);
  377. pr_debug(" swsusp: UTS Version: %s\n",swsusp_info.uts.version);
  378. pr_debug(" swsusp: UTS Machine: %s\n",swsusp_info.uts.machine);
  379. pr_debug(" swsusp: UTS Domain: %s\n",swsusp_info.uts.domainname);
  380. pr_debug(" swsusp: CPUs: %d\n",swsusp_info.cpus);
  381. pr_debug(" swsusp: Image: %ld Pages\n",swsusp_info.image_pages);
  382. pr_debug(" swsusp: Pagedir: %ld Pages\n",swsusp_info.pagedir_pages);
  383. }
  384. static void init_header(void)
  385. {
  386. memset(&swsusp_info, 0, sizeof(swsusp_info));
  387. swsusp_info.version_code = LINUX_VERSION_CODE;
  388. swsusp_info.num_physpages = num_physpages;
  389. memcpy(&swsusp_info.uts, &system_utsname, sizeof(system_utsname));
  390. swsusp_info.suspend_pagedir = pagedir_nosave;
  391. swsusp_info.cpus = num_online_cpus();
  392. swsusp_info.image_pages = nr_copy_pages;
  393. }
  394. static int close_swap(void)
  395. {
  396. swp_entry_t entry;
  397. int error;
  398. dump_info();
  399. error = write_page((unsigned long)&swsusp_info, &entry);
  400. if (!error) {
  401. printk( "S" );
  402. error = mark_swapfiles(entry);
  403. printk( "|\n" );
  404. }
  405. return error;
  406. }
  407. /**
  408. * free_pagedir_entries - Free pages used by the page directory.
  409. *
  410. * This is used during suspend for error recovery.
  411. */
  412. static void free_pagedir_entries(void)
  413. {
  414. int i;
  415. for (i = 0; i < swsusp_info.pagedir_pages; i++)
  416. swap_free(swsusp_info.pagedir[i]);
  417. }
  418. /**
  419. * write_pagedir - Write the array of pages holding the page directory.
  420. * @last: Last swap entry we write (needed for header).
  421. */
  422. static int write_pagedir(void)
  423. {
  424. int error = 0;
  425. unsigned int n = 0;
  426. struct pbe *pbe;
  427. printk( "Writing pagedir...");
  428. for_each_pb_page (pbe, pagedir_nosave) {
  429. if ((error = write_page((unsigned long)pbe, &swsusp_info.pagedir[n++])))
  430. return error;
  431. }
  432. swsusp_info.pagedir_pages = n;
  433. printk("done (%u pages)\n", n);
  434. return error;
  435. }
  436. /**
  437. * enough_swap - Make sure we have enough swap to save the image.
  438. *
  439. * Returns TRUE or FALSE after checking the total amount of swap
  440. * space avaiable.
  441. *
  442. * FIXME: si_swapinfo(&i) returns all swap devices information.
  443. * We should only consider resume_device.
  444. */
  445. static int enough_swap(unsigned int nr_pages)
  446. {
  447. struct sysinfo i;
  448. si_swapinfo(&i);
  449. pr_debug("swsusp: available swap: %lu pages\n", i.freeswap);
  450. return i.freeswap > (nr_pages + PAGES_FOR_IO +
  451. (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
  452. }
  453. /**
  454. * write_suspend_image - Write entire image and metadata.
  455. *
  456. */
  457. static int write_suspend_image(void)
  458. {
  459. int error;
  460. if (!enough_swap(nr_copy_pages)) {
  461. printk(KERN_ERR "swsusp: Not enough free swap\n");
  462. return -ENOSPC;
  463. }
  464. init_header();
  465. if ((error = data_write()))
  466. goto FreeData;
  467. if ((error = write_pagedir()))
  468. goto FreePagedir;
  469. if ((error = close_swap()))
  470. goto FreePagedir;
  471. Done:
  472. memset(key_iv, 0, MAXKEY+MAXIV);
  473. return error;
  474. FreePagedir:
  475. free_pagedir_entries();
  476. FreeData:
  477. data_free();
  478. goto Done;
  479. }
  480. /* It is important _NOT_ to umount filesystems at this point. We want
  481. * them synced (in case something goes wrong) but we DO not want to mark
  482. * filesystem clean: it is not. (And it does not matter, if we resume
  483. * correctly, we'll mark system clean, anyway.)
  484. */
  485. int swsusp_write(void)
  486. {
  487. int error;
  488. if ((error = swsusp_swap_check())) {
  489. printk(KERN_ERR "swsusp: cannot find swap device, try swapon -a.\n");
  490. return error;
  491. }
  492. lock_swapdevices();
  493. error = write_suspend_image();
  494. /* This will unlock ignored swap devices since writing is finished */
  495. lock_swapdevices();
  496. return error;
  497. }
  498. int swsusp_suspend(void)
  499. {
  500. int error;
  501. if ((error = arch_prepare_suspend()))
  502. return error;
  503. local_irq_disable();
  504. /* At this point, device_suspend() has been called, but *not*
  505. * device_power_down(). We *must* device_power_down() now.
  506. * Otherwise, drivers for some devices (e.g. interrupt controllers)
  507. * become desynchronized with the actual state of the hardware
  508. * at resume time, and evil weirdness ensues.
  509. */
  510. if ((error = device_power_down(PMSG_FREEZE))) {
  511. printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
  512. goto Enable_irqs;
  513. }
  514. if ((error = save_highmem())) {
  515. printk(KERN_ERR "swsusp: Not enough free pages for highmem\n");
  516. goto Restore_highmem;
  517. }
  518. save_processor_state();
  519. if ((error = swsusp_arch_suspend()))
  520. printk(KERN_ERR "Error %d suspending\n", error);
  521. /* Restore control flow magically appears here */
  522. restore_processor_state();
  523. Restore_highmem:
  524. restore_highmem();
  525. device_power_up();
  526. Enable_irqs:
  527. local_irq_enable();
  528. return error;
  529. }
  530. int swsusp_resume(void)
  531. {
  532. int error;
  533. local_irq_disable();
  534. if (device_power_down(PMSG_FREEZE))
  535. printk(KERN_ERR "Some devices failed to power down, very bad\n");
  536. /* We'll ignore saved state, but this gets preempt count (etc) right */
  537. save_processor_state();
  538. error = swsusp_arch_resume();
  539. /* Code below is only ever reached in case of failure. Otherwise
  540. * execution continues at place where swsusp_arch_suspend was called
  541. */
  542. BUG_ON(!error);
  543. /* The only reason why swsusp_arch_resume() can fail is memory being
  544. * very tight, so we have to free it as soon as we can to avoid
  545. * subsequent failures
  546. */
  547. swsusp_free();
  548. restore_processor_state();
  549. restore_highmem();
  550. touch_softlockup_watchdog();
  551. device_power_up();
  552. local_irq_enable();
  553. return error;
  554. }
  555. /**
  556. * mark_unsafe_pages - mark the pages that cannot be used for storing
  557. * the image during resume, because they conflict with the pages that
  558. * had been used before suspend
  559. */
  560. static void mark_unsafe_pages(struct pbe *pblist)
  561. {
  562. struct zone *zone;
  563. unsigned long zone_pfn;
  564. struct pbe *p;
  565. if (!pblist) /* a sanity check */
  566. return;
  567. /* Clear page flags */
  568. for_each_zone (zone) {
  569. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  570. if (pfn_valid(zone_pfn + zone->zone_start_pfn))
  571. ClearPageNosaveFree(pfn_to_page(zone_pfn +
  572. zone->zone_start_pfn));
  573. }
  574. /* Mark orig addresses */
  575. for_each_pbe (p, pblist)
  576. SetPageNosaveFree(virt_to_page(p->orig_address));
  577. }
  578. static void copy_page_backup_list(struct pbe *dst, struct pbe *src)
  579. {
  580. /* We assume both lists contain the same number of elements */
  581. while (src) {
  582. dst->orig_address = src->orig_address;
  583. dst->swap_address = src->swap_address;
  584. dst = dst->next;
  585. src = src->next;
  586. }
  587. }
  588. /*
  589. * Using bio to read from swap.
  590. * This code requires a bit more work than just using buffer heads
  591. * but, it is the recommended way for 2.5/2.6.
  592. * The following are to signal the beginning and end of I/O. Bios
  593. * finish asynchronously, while we want them to happen synchronously.
  594. * A simple atomic_t, and a wait loop take care of this problem.
  595. */
  596. static atomic_t io_done = ATOMIC_INIT(0);
  597. static int end_io(struct bio *bio, unsigned int num, int err)
  598. {
  599. if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  600. panic("I/O error reading memory image");
  601. atomic_set(&io_done, 0);
  602. return 0;
  603. }
  604. static struct block_device *resume_bdev;
  605. /**
  606. * submit - submit BIO request.
  607. * @rw: READ or WRITE.
  608. * @off physical offset of page.
  609. * @page: page we're reading or writing.
  610. *
  611. * Straight from the textbook - allocate and initialize the bio.
  612. * If we're writing, make sure the page is marked as dirty.
  613. * Then submit it and wait.
  614. */
  615. static int submit(int rw, pgoff_t page_off, void *page)
  616. {
  617. int error = 0;
  618. struct bio *bio;
  619. bio = bio_alloc(GFP_ATOMIC, 1);
  620. if (!bio)
  621. return -ENOMEM;
  622. bio->bi_sector = page_off * (PAGE_SIZE >> 9);
  623. bio_get(bio);
  624. bio->bi_bdev = resume_bdev;
  625. bio->bi_end_io = end_io;
  626. if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
  627. printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
  628. error = -EFAULT;
  629. goto Done;
  630. }
  631. if (rw == WRITE)
  632. bio_set_pages_dirty(bio);
  633. atomic_set(&io_done, 1);
  634. submit_bio(rw | (1 << BIO_RW_SYNC), bio);
  635. while (atomic_read(&io_done))
  636. yield();
  637. Done:
  638. bio_put(bio);
  639. return error;
  640. }
  641. static int bio_read_page(pgoff_t page_off, void *page)
  642. {
  643. return submit(READ, page_off, page);
  644. }
  645. static int bio_write_page(pgoff_t page_off, void *page)
  646. {
  647. return submit(WRITE, page_off, page);
  648. }
  649. /*
  650. * Sanity check if this image makes sense with this kernel/swap context
  651. * I really don't think that it's foolproof but more than nothing..
  652. */
  653. static const char *sanity_check(void)
  654. {
  655. dump_info();
  656. if (swsusp_info.version_code != LINUX_VERSION_CODE)
  657. return "kernel version";
  658. if (swsusp_info.num_physpages != num_physpages)
  659. return "memory size";
  660. if (strcmp(swsusp_info.uts.sysname,system_utsname.sysname))
  661. return "system type";
  662. if (strcmp(swsusp_info.uts.release,system_utsname.release))
  663. return "kernel release";
  664. if (strcmp(swsusp_info.uts.version,system_utsname.version))
  665. return "version";
  666. if (strcmp(swsusp_info.uts.machine,system_utsname.machine))
  667. return "machine";
  668. #if 0
  669. /* We can't use number of online CPUs when we use hotplug to remove them ;-))) */
  670. if (swsusp_info.cpus != num_possible_cpus())
  671. return "number of cpus";
  672. #endif
  673. return NULL;
  674. }
  675. static int check_header(void)
  676. {
  677. const char *reason = NULL;
  678. int error;
  679. if ((error = bio_read_page(swp_offset(swsusp_header.swsusp_info), &swsusp_info)))
  680. return error;
  681. /* Is this same machine? */
  682. if ((reason = sanity_check())) {
  683. printk(KERN_ERR "swsusp: Resume mismatch: %s\n",reason);
  684. return -EPERM;
  685. }
  686. nr_copy_pages = swsusp_info.image_pages;
  687. return error;
  688. }
  689. static int check_sig(void)
  690. {
  691. int error;
  692. memset(&swsusp_header, 0, sizeof(swsusp_header));
  693. if ((error = bio_read_page(0, &swsusp_header)))
  694. return error;
  695. if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
  696. memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
  697. memcpy(key_iv, swsusp_header.key_iv, MAXKEY+MAXIV);
  698. memset(swsusp_header.key_iv, 0, MAXKEY+MAXIV);
  699. /*
  700. * Reset swap signature now.
  701. */
  702. error = bio_write_page(0, &swsusp_header);
  703. } else {
  704. return -EINVAL;
  705. }
  706. if (!error)
  707. pr_debug("swsusp: Signature found, resuming\n");
  708. return error;
  709. }
  710. /**
  711. * data_read - Read image pages from swap.
  712. *
  713. * You do not need to check for overlaps, check_pagedir()
  714. * already did that.
  715. */
  716. static int data_read(struct pbe *pblist)
  717. {
  718. struct pbe *p;
  719. int error = 0;
  720. int i = 0;
  721. int mod = swsusp_info.image_pages / 100;
  722. void *tfm;
  723. if ((error = crypto_init(0, &tfm)))
  724. return error;
  725. if (!mod)
  726. mod = 1;
  727. printk("swsusp: Reading image data (%lu pages): ",
  728. swsusp_info.image_pages);
  729. for_each_pbe (p, pblist) {
  730. if (!(i % mod))
  731. printk("\b\b\b\b%3d%%", i / mod);
  732. if ((error = crypto_read(p, tfm))) {
  733. crypto_exit(tfm);
  734. return error;
  735. }
  736. i++;
  737. }
  738. printk("\b\b\b\bdone\n");
  739. crypto_exit(tfm);
  740. return error;
  741. }
  742. /**
  743. * read_pagedir - Read page backup list pages from swap
  744. */
  745. static int read_pagedir(struct pbe *pblist)
  746. {
  747. struct pbe *pbpage, *p;
  748. unsigned int i = 0;
  749. int error;
  750. if (!pblist)
  751. return -EFAULT;
  752. printk("swsusp: Reading pagedir (%lu pages)\n",
  753. swsusp_info.pagedir_pages);
  754. for_each_pb_page (pbpage, pblist) {
  755. unsigned long offset = swp_offset(swsusp_info.pagedir[i++]);
  756. error = -EFAULT;
  757. if (offset) {
  758. p = (pbpage + PB_PAGE_SKIP)->next;
  759. error = bio_read_page(offset, (void *)pbpage);
  760. (pbpage + PB_PAGE_SKIP)->next = p;
  761. }
  762. if (error)
  763. break;
  764. }
  765. if (!error)
  766. BUG_ON(i != swsusp_info.pagedir_pages);
  767. return error;
  768. }
  769. static int check_suspend_image(void)
  770. {
  771. int error = 0;
  772. if ((error = check_sig()))
  773. return error;
  774. if ((error = check_header()))
  775. return error;
  776. return 0;
  777. }
  778. static int read_suspend_image(void)
  779. {
  780. int error = 0;
  781. struct pbe *p;
  782. if (!(p = alloc_pagedir(nr_copy_pages, GFP_ATOMIC, 0)))
  783. return -ENOMEM;
  784. if ((error = read_pagedir(p)))
  785. return error;
  786. create_pbe_list(p, nr_copy_pages);
  787. mark_unsafe_pages(p);
  788. pagedir_nosave = alloc_pagedir(nr_copy_pages, GFP_ATOMIC, 1);
  789. if (pagedir_nosave) {
  790. create_pbe_list(pagedir_nosave, nr_copy_pages);
  791. copy_page_backup_list(pagedir_nosave, p);
  792. }
  793. free_pagedir(p);
  794. if (!pagedir_nosave)
  795. return -ENOMEM;
  796. /* Allocate memory for the image and read the data from swap */
  797. error = alloc_data_pages(pagedir_nosave, GFP_ATOMIC, 1);
  798. if (!error)
  799. error = data_read(pagedir_nosave);
  800. return error;
  801. }
  802. /**
  803. * swsusp_check - Check for saved image in swap
  804. */
  805. int swsusp_check(void)
  806. {
  807. int error;
  808. resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
  809. if (!IS_ERR(resume_bdev)) {
  810. set_blocksize(resume_bdev, PAGE_SIZE);
  811. error = check_suspend_image();
  812. if (error)
  813. blkdev_put(resume_bdev);
  814. } else
  815. error = PTR_ERR(resume_bdev);
  816. if (!error)
  817. pr_debug("swsusp: resume file found\n");
  818. else
  819. pr_debug("swsusp: Error %d check for resume file\n", error);
  820. return error;
  821. }
  822. /**
  823. * swsusp_read - Read saved image from swap.
  824. */
  825. int swsusp_read(void)
  826. {
  827. int error;
  828. if (IS_ERR(resume_bdev)) {
  829. pr_debug("swsusp: block device not initialised\n");
  830. return PTR_ERR(resume_bdev);
  831. }
  832. error = read_suspend_image();
  833. blkdev_put(resume_bdev);
  834. memset(key_iv, 0, MAXKEY+MAXIV);
  835. if (!error)
  836. pr_debug("swsusp: Reading resume file was successful\n");
  837. else
  838. pr_debug("swsusp: Error %d resuming\n", error);
  839. return error;
  840. }
  841. /**
  842. * swsusp_close - close swap device.
  843. */
  844. void swsusp_close(void)
  845. {
  846. if (IS_ERR(resume_bdev)) {
  847. pr_debug("swsusp: block device not initialised\n");
  848. return;
  849. }
  850. blkdev_put(resume_bdev);
  851. }