swsusp.c 23 KB

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