swsusp.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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. * On resume, for storing the PBE list and the image,
  543. * we can only use memory pages that do not conflict with the pages
  544. * which had been used before suspend.
  545. *
  546. * We don't know which pages are usable until we allocate them.
  547. *
  548. * Allocated but unusable (ie eaten) memory pages are marked so that
  549. * swsusp_free() can release them
  550. */
  551. unsigned long get_safe_page(gfp_t gfp_mask)
  552. {
  553. unsigned long m;
  554. do {
  555. m = get_zeroed_page(gfp_mask);
  556. if (m && PageNosaveFree(virt_to_page(m)))
  557. /* This is for swsusp_free() */
  558. SetPageNosave(virt_to_page(m));
  559. } while (m && PageNosaveFree(virt_to_page(m)));
  560. if (m) {
  561. /* This is for swsusp_free() */
  562. SetPageNosave(virt_to_page(m));
  563. SetPageNosaveFree(virt_to_page(m));
  564. }
  565. return m;
  566. }
  567. /**
  568. * check_pagedir - We ensure here that pages that the PBEs point to
  569. * won't collide with pages where we're going to restore from the loaded
  570. * pages later
  571. */
  572. static int check_pagedir(struct pbe *pblist)
  573. {
  574. struct pbe *p;
  575. /* This is necessary, so that we can free allocated pages
  576. * in case of failure
  577. */
  578. for_each_pbe (p, pblist)
  579. p->address = 0UL;
  580. for_each_pbe (p, pblist) {
  581. p->address = get_safe_page(GFP_ATOMIC);
  582. if (!p->address)
  583. return -ENOMEM;
  584. }
  585. return 0;
  586. }
  587. /**
  588. * swsusp_pagedir_relocate - It is possible, that some memory pages
  589. * occupied by the list of PBEs collide with pages where we're going to
  590. * restore from the loaded pages later. We relocate them here.
  591. */
  592. static struct pbe *swsusp_pagedir_relocate(struct pbe *pblist)
  593. {
  594. struct zone *zone;
  595. unsigned long zone_pfn;
  596. struct pbe *pbpage, *tail, *p;
  597. void *m;
  598. int rel = 0;
  599. if (!pblist) /* a sanity check */
  600. return NULL;
  601. pr_debug("swsusp: Relocating pagedir (%lu pages to check)\n",
  602. swsusp_info.pagedir_pages);
  603. /* Clear page flags */
  604. for_each_zone (zone) {
  605. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  606. if (pfn_valid(zone_pfn + zone->zone_start_pfn))
  607. ClearPageNosaveFree(pfn_to_page(zone_pfn +
  608. zone->zone_start_pfn));
  609. }
  610. /* Mark orig addresses */
  611. for_each_pbe (p, pblist)
  612. SetPageNosaveFree(virt_to_page(p->orig_address));
  613. tail = pblist + PB_PAGE_SKIP;
  614. /* Relocate colliding pages */
  615. for_each_pb_page (pbpage, pblist) {
  616. if (PageNosaveFree(virt_to_page((unsigned long)pbpage))) {
  617. m = (void *)get_safe_page(GFP_ATOMIC | __GFP_COLD);
  618. if (!m)
  619. return NULL;
  620. memcpy(m, (void *)pbpage, PAGE_SIZE);
  621. if (pbpage == pblist)
  622. pblist = (struct pbe *)m;
  623. else
  624. tail->next = (struct pbe *)m;
  625. pbpage = (struct pbe *)m;
  626. /* We have to link the PBEs again */
  627. for (p = pbpage; p < pbpage + PB_PAGE_SKIP; p++)
  628. if (p->next) /* needed to save the end */
  629. p->next = p + 1;
  630. rel++;
  631. }
  632. tail = pbpage + PB_PAGE_SKIP;
  633. }
  634. /* This is for swsusp_free() */
  635. for_each_pb_page (pbpage, pblist) {
  636. SetPageNosave(virt_to_page(pbpage));
  637. SetPageNosaveFree(virt_to_page(pbpage));
  638. }
  639. printk("swsusp: Relocated %d pages\n", rel);
  640. return pblist;
  641. }
  642. /*
  643. * Using bio to read from swap.
  644. * This code requires a bit more work than just using buffer heads
  645. * but, it is the recommended way for 2.5/2.6.
  646. * The following are to signal the beginning and end of I/O. Bios
  647. * finish asynchronously, while we want them to happen synchronously.
  648. * A simple atomic_t, and a wait loop take care of this problem.
  649. */
  650. static atomic_t io_done = ATOMIC_INIT(0);
  651. static int end_io(struct bio *bio, unsigned int num, int err)
  652. {
  653. if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  654. panic("I/O error reading memory image");
  655. atomic_set(&io_done, 0);
  656. return 0;
  657. }
  658. static struct block_device *resume_bdev;
  659. /**
  660. * submit - submit BIO request.
  661. * @rw: READ or WRITE.
  662. * @off physical offset of page.
  663. * @page: page we're reading or writing.
  664. *
  665. * Straight from the textbook - allocate and initialize the bio.
  666. * If we're writing, make sure the page is marked as dirty.
  667. * Then submit it and wait.
  668. */
  669. static int submit(int rw, pgoff_t page_off, void *page)
  670. {
  671. int error = 0;
  672. struct bio *bio;
  673. bio = bio_alloc(GFP_ATOMIC, 1);
  674. if (!bio)
  675. return -ENOMEM;
  676. bio->bi_sector = page_off * (PAGE_SIZE >> 9);
  677. bio_get(bio);
  678. bio->bi_bdev = resume_bdev;
  679. bio->bi_end_io = end_io;
  680. if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
  681. printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
  682. error = -EFAULT;
  683. goto Done;
  684. }
  685. if (rw == WRITE)
  686. bio_set_pages_dirty(bio);
  687. atomic_set(&io_done, 1);
  688. submit_bio(rw | (1 << BIO_RW_SYNC), bio);
  689. while (atomic_read(&io_done))
  690. yield();
  691. Done:
  692. bio_put(bio);
  693. return error;
  694. }
  695. static int bio_read_page(pgoff_t page_off, void *page)
  696. {
  697. return submit(READ, page_off, page);
  698. }
  699. static int bio_write_page(pgoff_t page_off, void *page)
  700. {
  701. return submit(WRITE, page_off, page);
  702. }
  703. /*
  704. * Sanity check if this image makes sense with this kernel/swap context
  705. * I really don't think that it's foolproof but more than nothing..
  706. */
  707. static const char *sanity_check(void)
  708. {
  709. dump_info();
  710. if (swsusp_info.version_code != LINUX_VERSION_CODE)
  711. return "kernel version";
  712. if (swsusp_info.num_physpages != num_physpages)
  713. return "memory size";
  714. if (strcmp(swsusp_info.uts.sysname,system_utsname.sysname))
  715. return "system type";
  716. if (strcmp(swsusp_info.uts.release,system_utsname.release))
  717. return "kernel release";
  718. if (strcmp(swsusp_info.uts.version,system_utsname.version))
  719. return "version";
  720. if (strcmp(swsusp_info.uts.machine,system_utsname.machine))
  721. return "machine";
  722. #if 0
  723. /* We can't use number of online CPUs when we use hotplug to remove them ;-))) */
  724. if (swsusp_info.cpus != num_possible_cpus())
  725. return "number of cpus";
  726. #endif
  727. return NULL;
  728. }
  729. static int check_header(void)
  730. {
  731. const char *reason = NULL;
  732. int error;
  733. if ((error = bio_read_page(swp_offset(swsusp_header.swsusp_info), &swsusp_info)))
  734. return error;
  735. /* Is this same machine? */
  736. if ((reason = sanity_check())) {
  737. printk(KERN_ERR "swsusp: Resume mismatch: %s\n",reason);
  738. return -EPERM;
  739. }
  740. nr_copy_pages = swsusp_info.image_pages;
  741. return error;
  742. }
  743. static int check_sig(void)
  744. {
  745. int error;
  746. memset(&swsusp_header, 0, sizeof(swsusp_header));
  747. if ((error = bio_read_page(0, &swsusp_header)))
  748. return error;
  749. if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
  750. memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
  751. memcpy(key_iv, swsusp_header.key_iv, MAXKEY+MAXIV);
  752. memset(swsusp_header.key_iv, 0, MAXKEY+MAXIV);
  753. /*
  754. * Reset swap signature now.
  755. */
  756. error = bio_write_page(0, &swsusp_header);
  757. } else {
  758. return -EINVAL;
  759. }
  760. if (!error)
  761. pr_debug("swsusp: Signature found, resuming\n");
  762. return error;
  763. }
  764. /**
  765. * data_read - Read image pages from swap.
  766. *
  767. * You do not need to check for overlaps, check_pagedir()
  768. * already did that.
  769. */
  770. static int data_read(struct pbe *pblist)
  771. {
  772. struct pbe *p;
  773. int error = 0;
  774. int i = 0;
  775. int mod = swsusp_info.image_pages / 100;
  776. void *tfm;
  777. if ((error = crypto_init(0, &tfm)))
  778. return error;
  779. if (!mod)
  780. mod = 1;
  781. printk("swsusp: Reading image data (%lu pages): ",
  782. swsusp_info.image_pages);
  783. for_each_pbe (p, pblist) {
  784. if (!(i % mod))
  785. printk("\b\b\b\b%3d%%", i / mod);
  786. if ((error = crypto_read(p, tfm))) {
  787. crypto_exit(tfm);
  788. return error;
  789. }
  790. i++;
  791. }
  792. printk("\b\b\b\bdone\n");
  793. crypto_exit(tfm);
  794. return error;
  795. }
  796. /**
  797. * read_pagedir - Read page backup list pages from swap
  798. */
  799. static int read_pagedir(struct pbe *pblist)
  800. {
  801. struct pbe *pbpage, *p;
  802. unsigned int i = 0;
  803. int error;
  804. if (!pblist)
  805. return -EFAULT;
  806. printk("swsusp: Reading pagedir (%lu pages)\n",
  807. swsusp_info.pagedir_pages);
  808. for_each_pb_page (pbpage, pblist) {
  809. unsigned long offset = swp_offset(swsusp_info.pagedir[i++]);
  810. error = -EFAULT;
  811. if (offset) {
  812. p = (pbpage + PB_PAGE_SKIP)->next;
  813. error = bio_read_page(offset, (void *)pbpage);
  814. (pbpage + PB_PAGE_SKIP)->next = p;
  815. }
  816. if (error)
  817. break;
  818. }
  819. if (!error)
  820. BUG_ON(i != swsusp_info.pagedir_pages);
  821. return error;
  822. }
  823. static int check_suspend_image(void)
  824. {
  825. int error = 0;
  826. if ((error = check_sig()))
  827. return error;
  828. if ((error = check_header()))
  829. return error;
  830. return 0;
  831. }
  832. static int read_suspend_image(void)
  833. {
  834. int error = 0;
  835. struct pbe *p;
  836. if (!(p = alloc_pagedir(nr_copy_pages)))
  837. return -ENOMEM;
  838. if ((error = read_pagedir(p)))
  839. return error;
  840. create_pbe_list(p, nr_copy_pages);
  841. if (!(pagedir_nosave = swsusp_pagedir_relocate(p)))
  842. return -ENOMEM;
  843. /* Allocate memory for the image and read the data from swap */
  844. error = check_pagedir(pagedir_nosave);
  845. if (!error)
  846. error = data_read(pagedir_nosave);
  847. return error;
  848. }
  849. /**
  850. * swsusp_check - Check for saved image in swap
  851. */
  852. int swsusp_check(void)
  853. {
  854. int error;
  855. resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
  856. if (!IS_ERR(resume_bdev)) {
  857. set_blocksize(resume_bdev, PAGE_SIZE);
  858. error = check_suspend_image();
  859. if (error)
  860. blkdev_put(resume_bdev);
  861. } else
  862. error = PTR_ERR(resume_bdev);
  863. if (!error)
  864. pr_debug("swsusp: resume file found\n");
  865. else
  866. pr_debug("swsusp: Error %d check for resume file\n", error);
  867. return error;
  868. }
  869. /**
  870. * swsusp_read - Read saved image from swap.
  871. */
  872. int swsusp_read(void)
  873. {
  874. int error;
  875. if (IS_ERR(resume_bdev)) {
  876. pr_debug("swsusp: block device not initialised\n");
  877. return PTR_ERR(resume_bdev);
  878. }
  879. error = read_suspend_image();
  880. blkdev_put(resume_bdev);
  881. memset(key_iv, 0, MAXKEY+MAXIV);
  882. if (!error)
  883. pr_debug("swsusp: Reading resume file was successful\n");
  884. else
  885. pr_debug("swsusp: Error %d resuming\n", error);
  886. return error;
  887. }
  888. /**
  889. * swsusp_close - close swap device.
  890. */
  891. void swsusp_close(void)
  892. {
  893. if (IS_ERR(resume_bdev)) {
  894. pr_debug("swsusp: block device not initialised\n");
  895. return;
  896. }
  897. blkdev_put(resume_bdev);
  898. }