swsusp.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  1. /*
  2. * linux/kernel/power/swsusp.c
  3. *
  4. * This file is to realize architecture-independent
  5. * machine suspend feature using pretty near only high-level routines
  6. *
  7. * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
  8. * Copyright (C) 1998,2001-2004 Pavel Machek <pavel@suse.cz>
  9. *
  10. * This file is released under the GPLv2.
  11. *
  12. * I'd like to thank the following people for their work:
  13. *
  14. * Pavel Machek <pavel@ucw.cz>:
  15. * Modifications, defectiveness pointing, being with me at the very beginning,
  16. * suspend to swap space, stop all tasks. Port to 2.4.18-ac and 2.5.17.
  17. *
  18. * Steve Doddi <dirk@loth.demon.co.uk>:
  19. * Support the possibility of hardware state restoring.
  20. *
  21. * Raph <grey.havens@earthling.net>:
  22. * Support for preserving states of network devices and virtual console
  23. * (including X and svgatextmode)
  24. *
  25. * Kurt Garloff <garloff@suse.de>:
  26. * Straightened the critical function in order to prevent compilers from
  27. * playing tricks with local variables.
  28. *
  29. * Andreas Mohr <a.mohr@mailto.de>
  30. *
  31. * Alex Badea <vampire@go.ro>:
  32. * Fixed runaway init
  33. *
  34. * Andreas Steinmetz <ast@domdv.de>:
  35. * Added encrypted suspend option
  36. *
  37. * More state savers are welcome. Especially for the scsi layer...
  38. *
  39. * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
  40. */
  41. #include <linux/module.h>
  42. #include <linux/mm.h>
  43. #include <linux/suspend.h>
  44. #include <linux/smp_lock.h>
  45. #include <linux/file.h>
  46. #include <linux/utsname.h>
  47. #include <linux/version.h>
  48. #include <linux/delay.h>
  49. #include <linux/reboot.h>
  50. #include <linux/bitops.h>
  51. #include <linux/vt_kern.h>
  52. #include <linux/kbd_kern.h>
  53. #include <linux/keyboard.h>
  54. #include <linux/spinlock.h>
  55. #include <linux/genhd.h>
  56. #include <linux/kernel.h>
  57. #include <linux/major.h>
  58. #include <linux/swap.h>
  59. #include <linux/pm.h>
  60. #include <linux/device.h>
  61. #include <linux/buffer_head.h>
  62. #include <linux/swapops.h>
  63. #include <linux/bootmem.h>
  64. #include <linux/syscalls.h>
  65. #include <linux/console.h>
  66. #include <linux/highmem.h>
  67. #include <linux/bio.h>
  68. #include <linux/mount.h>
  69. #include <asm/uaccess.h>
  70. #include <asm/mmu_context.h>
  71. #include <asm/pgtable.h>
  72. #include <asm/tlbflush.h>
  73. #include <asm/io.h>
  74. #include <linux/random.h>
  75. #include <linux/crypto.h>
  76. #include <asm/scatterlist.h>
  77. #include "power.h"
  78. #define CIPHER "aes"
  79. #define MAXKEY 32
  80. #define MAXIV 32
  81. /* References to section boundaries */
  82. extern const void __nosave_begin, __nosave_end;
  83. /* Variables to be preserved over suspend */
  84. static int nr_copy_pages_check;
  85. extern char resume_file[];
  86. /* Local variables that should not be affected by save */
  87. static unsigned int nr_copy_pages __nosavedata = 0;
  88. /* Suspend pagedir is allocated before final copy, therefore it
  89. must be freed after resume
  90. Warning: this is evil. There are actually two pagedirs at time of
  91. resume. One is "pagedir_save", which is empty frame allocated at
  92. time of suspend, that must be freed. Second is "pagedir_nosave",
  93. allocated at time of resume, that travels through memory not to
  94. collide with anything.
  95. Warning: this is even more evil than it seems. Pagedirs this file
  96. talks about are completely different from page directories used by
  97. MMU hardware.
  98. */
  99. suspend_pagedir_t *pagedir_nosave __nosavedata = NULL;
  100. static suspend_pagedir_t *pagedir_save;
  101. #define SWSUSP_SIG "S1SUSPEND"
  102. static struct swsusp_header {
  103. char reserved[PAGE_SIZE - 20 - MAXKEY - MAXIV - sizeof(swp_entry_t)];
  104. u8 key_iv[MAXKEY+MAXIV];
  105. swp_entry_t swsusp_info;
  106. char orig_sig[10];
  107. char sig[10];
  108. } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
  109. static struct swsusp_info swsusp_info;
  110. /*
  111. * XXX: We try to keep some more pages free so that I/O operations succeed
  112. * without paging. Might this be more?
  113. */
  114. #define PAGES_FOR_IO 512
  115. /*
  116. * Saving part...
  117. */
  118. /* We memorize in swapfile_used what swap devices are used for suspension */
  119. #define SWAPFILE_UNUSED 0
  120. #define SWAPFILE_SUSPEND 1 /* This is the suspending device */
  121. #define SWAPFILE_IGNORED 2 /* Those are other swap devices ignored for suspension */
  122. static unsigned short swapfile_used[MAX_SWAPFILES];
  123. static unsigned short root_swap;
  124. static int write_page(unsigned long addr, swp_entry_t * loc);
  125. static int bio_read_page(pgoff_t page_off, void * page);
  126. static u8 key_iv[MAXKEY+MAXIV];
  127. #ifdef CONFIG_SWSUSP_ENCRYPT
  128. static int crypto_init(int mode, void **mem)
  129. {
  130. int error = 0;
  131. int len;
  132. char *modemsg;
  133. struct crypto_tfm *tfm;
  134. modemsg = mode ? "suspend not possible" : "resume not possible";
  135. tfm = crypto_alloc_tfm(CIPHER, CRYPTO_TFM_MODE_CBC);
  136. if(!tfm) {
  137. printk(KERN_ERR "swsusp: no tfm, %s\n", modemsg);
  138. error = -EINVAL;
  139. goto out;
  140. }
  141. if(MAXKEY < crypto_tfm_alg_min_keysize(tfm)) {
  142. printk(KERN_ERR "swsusp: key buffer too small, %s\n", modemsg);
  143. error = -ENOKEY;
  144. goto fail;
  145. }
  146. if (mode)
  147. get_random_bytes(key_iv, MAXKEY+MAXIV);
  148. len = crypto_tfm_alg_max_keysize(tfm);
  149. if (len > MAXKEY)
  150. len = MAXKEY;
  151. if (crypto_cipher_setkey(tfm, key_iv, len)) {
  152. printk(KERN_ERR "swsusp: key setup failure, %s\n", modemsg);
  153. error = -EKEYREJECTED;
  154. goto fail;
  155. }
  156. len = crypto_tfm_alg_ivsize(tfm);
  157. if (MAXIV < len) {
  158. printk(KERN_ERR "swsusp: iv buffer too small, %s\n", modemsg);
  159. error = -EOVERFLOW;
  160. goto fail;
  161. }
  162. crypto_cipher_set_iv(tfm, key_iv+MAXKEY, len);
  163. *mem=(void *)tfm;
  164. goto out;
  165. fail: crypto_free_tfm(tfm);
  166. out: return error;
  167. }
  168. static __inline__ void crypto_exit(void *mem)
  169. {
  170. crypto_free_tfm((struct crypto_tfm *)mem);
  171. }
  172. static __inline__ int crypto_write(struct pbe *p, void *mem)
  173. {
  174. int error = 0;
  175. struct scatterlist src, dst;
  176. src.page = virt_to_page(p->address);
  177. src.offset = 0;
  178. src.length = PAGE_SIZE;
  179. dst.page = virt_to_page((void *)&swsusp_header);
  180. dst.offset = 0;
  181. dst.length = PAGE_SIZE;
  182. error = crypto_cipher_encrypt((struct crypto_tfm *)mem, &dst, &src,
  183. PAGE_SIZE);
  184. if (!error)
  185. error = write_page((unsigned long)&swsusp_header,
  186. &(p->swap_address));
  187. return error;
  188. }
  189. static __inline__ int crypto_read(struct pbe *p, void *mem)
  190. {
  191. int error = 0;
  192. struct scatterlist src, dst;
  193. error = bio_read_page(swp_offset(p->swap_address), (void *)p->address);
  194. if (!error) {
  195. src.offset = 0;
  196. src.length = PAGE_SIZE;
  197. dst.offset = 0;
  198. dst.length = PAGE_SIZE;
  199. src.page = dst.page = virt_to_page((void *)p->address);
  200. error = crypto_cipher_decrypt((struct crypto_tfm *)mem, &dst,
  201. &src, PAGE_SIZE);
  202. }
  203. return error;
  204. }
  205. #else
  206. static __inline__ int crypto_init(int mode, void *mem)
  207. {
  208. return 0;
  209. }
  210. static __inline__ void crypto_exit(void *mem)
  211. {
  212. }
  213. static __inline__ int crypto_write(struct pbe *p, void *mem)
  214. {
  215. return write_page(p->address, &(p->swap_address));
  216. }
  217. static __inline__ int crypto_read(struct pbe *p, void *mem)
  218. {
  219. return bio_read_page(swp_offset(p->swap_address), (void *)p->address);
  220. }
  221. #endif
  222. static int mark_swapfiles(swp_entry_t prev)
  223. {
  224. int error;
  225. rw_swap_page_sync(READ,
  226. swp_entry(root_swap, 0),
  227. virt_to_page((unsigned long)&swsusp_header));
  228. if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
  229. !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
  230. memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
  231. memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
  232. memcpy(swsusp_header.key_iv, key_iv, MAXKEY+MAXIV);
  233. swsusp_header.swsusp_info = prev;
  234. error = rw_swap_page_sync(WRITE,
  235. swp_entry(root_swap, 0),
  236. virt_to_page((unsigned long)
  237. &swsusp_header));
  238. } else {
  239. pr_debug("swsusp: Partition is not swap space.\n");
  240. error = -ENODEV;
  241. }
  242. return error;
  243. }
  244. /*
  245. * Check whether the swap device is the specified resume
  246. * device, irrespective of whether they are specified by
  247. * identical names.
  248. *
  249. * (Thus, device inode aliasing is allowed. You can say /dev/hda4
  250. * instead of /dev/ide/host0/bus0/target0/lun0/part4 [if using devfs]
  251. * and they'll be considered the same device. This is *necessary* for
  252. * devfs, since the resume code can only recognize the form /dev/hda4,
  253. * but the suspend code would see the long name.)
  254. */
  255. static int is_resume_device(const struct swap_info_struct *swap_info)
  256. {
  257. struct file *file = swap_info->swap_file;
  258. struct inode *inode = file->f_dentry->d_inode;
  259. return S_ISBLK(inode->i_mode) &&
  260. swsusp_resume_device == MKDEV(imajor(inode), iminor(inode));
  261. }
  262. static int swsusp_swap_check(void) /* This is called before saving image */
  263. {
  264. int i, len;
  265. len=strlen(resume_file);
  266. root_swap = 0xFFFF;
  267. spin_lock(&swap_lock);
  268. for (i=0; i<MAX_SWAPFILES; i++) {
  269. if (!(swap_info[i].flags & SWP_WRITEOK)) {
  270. swapfile_used[i]=SWAPFILE_UNUSED;
  271. } else {
  272. if (!len) {
  273. printk(KERN_WARNING "resume= option should be used to set suspend device" );
  274. if (root_swap == 0xFFFF) {
  275. swapfile_used[i] = SWAPFILE_SUSPEND;
  276. root_swap = i;
  277. } else
  278. swapfile_used[i] = SWAPFILE_IGNORED;
  279. } else {
  280. /* we ignore all swap devices that are not the resume_file */
  281. if (is_resume_device(&swap_info[i])) {
  282. swapfile_used[i] = SWAPFILE_SUSPEND;
  283. root_swap = i;
  284. } else {
  285. swapfile_used[i] = SWAPFILE_IGNORED;
  286. }
  287. }
  288. }
  289. }
  290. spin_unlock(&swap_lock);
  291. return (root_swap != 0xffff) ? 0 : -ENODEV;
  292. }
  293. /**
  294. * This is called after saving image so modification
  295. * will be lost after resume... and that's what we want.
  296. * we make the device unusable. A new call to
  297. * lock_swapdevices can unlock the devices.
  298. */
  299. static void lock_swapdevices(void)
  300. {
  301. int i;
  302. spin_lock(&swap_lock);
  303. for (i = 0; i< MAX_SWAPFILES; i++)
  304. if (swapfile_used[i] == SWAPFILE_IGNORED) {
  305. swap_info[i].flags ^= SWP_WRITEOK;
  306. }
  307. spin_unlock(&swap_lock);
  308. }
  309. /**
  310. * write_page - Write one page to a fresh swap location.
  311. * @addr: Address we're writing.
  312. * @loc: Place to store the entry we used.
  313. *
  314. * Allocate a new swap entry and 'sync' it. Note we discard -EIO
  315. * errors. That is an artifact left over from swsusp. It did not
  316. * check the return of rw_swap_page_sync() at all, since most pages
  317. * written back to swap would return -EIO.
  318. * This is a partial improvement, since we will at least return other
  319. * errors, though we need to eventually fix the damn code.
  320. */
  321. static int write_page(unsigned long addr, swp_entry_t * loc)
  322. {
  323. swp_entry_t entry;
  324. int error = 0;
  325. entry = get_swap_page();
  326. if (swp_offset(entry) &&
  327. swapfile_used[swp_type(entry)] == SWAPFILE_SUSPEND) {
  328. error = rw_swap_page_sync(WRITE, entry,
  329. virt_to_page(addr));
  330. if (error == -EIO)
  331. error = 0;
  332. if (!error)
  333. *loc = entry;
  334. } else
  335. error = -ENOSPC;
  336. return error;
  337. }
  338. /**
  339. * data_free - Free the swap entries used by the saved image.
  340. *
  341. * Walk the list of used swap entries and free each one.
  342. * This is only used for cleanup when suspend fails.
  343. */
  344. static void data_free(void)
  345. {
  346. swp_entry_t entry;
  347. struct pbe * p;
  348. for_each_pbe(p, pagedir_nosave) {
  349. entry = p->swap_address;
  350. if (entry.val)
  351. swap_free(entry);
  352. else
  353. break;
  354. }
  355. }
  356. /**
  357. * data_write - Write saved image to swap.
  358. *
  359. * Walk the list of pages in the image and sync each one to swap.
  360. */
  361. static int data_write(void)
  362. {
  363. int error = 0, i = 0;
  364. unsigned int mod = nr_copy_pages / 100;
  365. struct pbe *p;
  366. void *tfm;
  367. if ((error = crypto_init(1, &tfm)))
  368. return error;
  369. if (!mod)
  370. mod = 1;
  371. printk( "Writing data to swap (%d pages)... ", nr_copy_pages );
  372. for_each_pbe (p, pagedir_nosave) {
  373. if (!(i%mod))
  374. printk( "\b\b\b\b%3d%%", i / mod );
  375. if ((error = crypto_write(p, tfm))) {
  376. crypto_exit(tfm);
  377. return error;
  378. }
  379. i++;
  380. }
  381. printk("\b\b\b\bdone\n");
  382. crypto_exit(tfm);
  383. return error;
  384. }
  385. static void dump_info(void)
  386. {
  387. pr_debug(" swsusp: Version: %u\n",swsusp_info.version_code);
  388. pr_debug(" swsusp: Num Pages: %ld\n",swsusp_info.num_physpages);
  389. pr_debug(" swsusp: UTS Sys: %s\n",swsusp_info.uts.sysname);
  390. pr_debug(" swsusp: UTS Node: %s\n",swsusp_info.uts.nodename);
  391. pr_debug(" swsusp: UTS Release: %s\n",swsusp_info.uts.release);
  392. pr_debug(" swsusp: UTS Version: %s\n",swsusp_info.uts.version);
  393. pr_debug(" swsusp: UTS Machine: %s\n",swsusp_info.uts.machine);
  394. pr_debug(" swsusp: UTS Domain: %s\n",swsusp_info.uts.domainname);
  395. pr_debug(" swsusp: CPUs: %d\n",swsusp_info.cpus);
  396. pr_debug(" swsusp: Image: %ld Pages\n",swsusp_info.image_pages);
  397. pr_debug(" swsusp: Pagedir: %ld Pages\n",swsusp_info.pagedir_pages);
  398. }
  399. static void init_header(void)
  400. {
  401. memset(&swsusp_info, 0, sizeof(swsusp_info));
  402. swsusp_info.version_code = LINUX_VERSION_CODE;
  403. swsusp_info.num_physpages = num_physpages;
  404. memcpy(&swsusp_info.uts, &system_utsname, sizeof(system_utsname));
  405. swsusp_info.suspend_pagedir = pagedir_nosave;
  406. swsusp_info.cpus = num_online_cpus();
  407. swsusp_info.image_pages = nr_copy_pages;
  408. }
  409. static int close_swap(void)
  410. {
  411. swp_entry_t entry;
  412. int error;
  413. dump_info();
  414. error = write_page((unsigned long)&swsusp_info, &entry);
  415. if (!error) {
  416. printk( "S" );
  417. error = mark_swapfiles(entry);
  418. printk( "|\n" );
  419. }
  420. return error;
  421. }
  422. /**
  423. * free_pagedir_entries - Free pages used by the page directory.
  424. *
  425. * This is used during suspend for error recovery.
  426. */
  427. static void free_pagedir_entries(void)
  428. {
  429. int i;
  430. for (i = 0; i < swsusp_info.pagedir_pages; i++)
  431. swap_free(swsusp_info.pagedir[i]);
  432. }
  433. /**
  434. * write_pagedir - Write the array of pages holding the page directory.
  435. * @last: Last swap entry we write (needed for header).
  436. */
  437. static int write_pagedir(void)
  438. {
  439. int error = 0;
  440. unsigned n = 0;
  441. struct pbe * pbe;
  442. printk( "Writing pagedir...");
  443. for_each_pb_page (pbe, pagedir_nosave) {
  444. if ((error = write_page((unsigned long)pbe, &swsusp_info.pagedir[n++])))
  445. return error;
  446. }
  447. swsusp_info.pagedir_pages = n;
  448. printk("done (%u pages)\n", n);
  449. return error;
  450. }
  451. /**
  452. * write_suspend_image - Write entire image and metadata.
  453. *
  454. */
  455. static int write_suspend_image(void)
  456. {
  457. int error;
  458. init_header();
  459. if ((error = data_write()))
  460. goto FreeData;
  461. if ((error = write_pagedir()))
  462. goto FreePagedir;
  463. if ((error = close_swap()))
  464. goto FreePagedir;
  465. Done:
  466. memset(key_iv, 0, MAXKEY+MAXIV);
  467. return error;
  468. FreePagedir:
  469. free_pagedir_entries();
  470. FreeData:
  471. data_free();
  472. goto Done;
  473. }
  474. #ifdef CONFIG_HIGHMEM
  475. struct highmem_page {
  476. char *data;
  477. struct page *page;
  478. struct highmem_page *next;
  479. };
  480. static struct highmem_page *highmem_copy;
  481. static int save_highmem_zone(struct zone *zone)
  482. {
  483. unsigned long zone_pfn;
  484. mark_free_pages(zone);
  485. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
  486. struct page *page;
  487. struct highmem_page *save;
  488. void *kaddr;
  489. unsigned long pfn = zone_pfn + zone->zone_start_pfn;
  490. if (!(pfn%1000))
  491. printk(".");
  492. if (!pfn_valid(pfn))
  493. continue;
  494. page = pfn_to_page(pfn);
  495. /*
  496. * PageReserved results from rvmalloc() sans vmalloc_32()
  497. * and architectural memory reservations.
  498. *
  499. * rvmalloc should not cause this, because all implementations
  500. * appear to always be using vmalloc_32 on architectures with
  501. * highmem. This is a good thing, because we would like to save
  502. * rvmalloc pages.
  503. *
  504. * It appears to be triggered by pages which do not point to
  505. * valid memory (see arch/i386/mm/init.c:one_highpage_init(),
  506. * which sets PageReserved if the page does not point to valid
  507. * RAM.
  508. *
  509. * XXX: must remove usage of PageReserved!
  510. */
  511. if (PageReserved(page))
  512. continue;
  513. BUG_ON(PageNosave(page));
  514. if (PageNosaveFree(page))
  515. continue;
  516. save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
  517. if (!save)
  518. return -ENOMEM;
  519. save->next = highmem_copy;
  520. save->page = page;
  521. save->data = (void *) get_zeroed_page(GFP_ATOMIC);
  522. if (!save->data) {
  523. kfree(save);
  524. return -ENOMEM;
  525. }
  526. kaddr = kmap_atomic(page, KM_USER0);
  527. memcpy(save->data, kaddr, PAGE_SIZE);
  528. kunmap_atomic(kaddr, KM_USER0);
  529. highmem_copy = save;
  530. }
  531. return 0;
  532. }
  533. #endif /* CONFIG_HIGHMEM */
  534. static int save_highmem(void)
  535. {
  536. #ifdef CONFIG_HIGHMEM
  537. struct zone *zone;
  538. int res = 0;
  539. pr_debug("swsusp: Saving Highmem\n");
  540. for_each_zone (zone) {
  541. if (is_highmem(zone))
  542. res = save_highmem_zone(zone);
  543. if (res)
  544. return res;
  545. }
  546. #endif
  547. return 0;
  548. }
  549. static int restore_highmem(void)
  550. {
  551. #ifdef CONFIG_HIGHMEM
  552. printk("swsusp: Restoring Highmem\n");
  553. while (highmem_copy) {
  554. struct highmem_page *save = highmem_copy;
  555. void *kaddr;
  556. highmem_copy = save->next;
  557. kaddr = kmap_atomic(save->page, KM_USER0);
  558. memcpy(kaddr, save->data, PAGE_SIZE);
  559. kunmap_atomic(kaddr, KM_USER0);
  560. free_page((long) save->data);
  561. kfree(save);
  562. }
  563. #endif
  564. return 0;
  565. }
  566. static int pfn_is_nosave(unsigned long pfn)
  567. {
  568. unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
  569. unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
  570. return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
  571. }
  572. /**
  573. * saveable - Determine whether a page should be cloned or not.
  574. * @pfn: The page
  575. *
  576. * We save a page if it's Reserved, and not in the range of pages
  577. * statically defined as 'unsaveable', or if it isn't reserved, and
  578. * isn't part of a free chunk of pages.
  579. */
  580. static int saveable(struct zone * zone, unsigned long * zone_pfn)
  581. {
  582. unsigned long pfn = *zone_pfn + zone->zone_start_pfn;
  583. struct page * page;
  584. if (!pfn_valid(pfn))
  585. return 0;
  586. page = pfn_to_page(pfn);
  587. if (PageNosave(page))
  588. return 0;
  589. if (pfn_is_nosave(pfn)) {
  590. pr_debug("[nosave pfn 0x%lx]", pfn);
  591. return 0;
  592. }
  593. if (PageNosaveFree(page))
  594. return 0;
  595. return 1;
  596. }
  597. static void count_data_pages(void)
  598. {
  599. struct zone *zone;
  600. unsigned long zone_pfn;
  601. nr_copy_pages = 0;
  602. for_each_zone (zone) {
  603. if (is_highmem(zone))
  604. continue;
  605. mark_free_pages(zone);
  606. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  607. nr_copy_pages += saveable(zone, &zone_pfn);
  608. }
  609. }
  610. static void copy_data_pages(void)
  611. {
  612. struct zone *zone;
  613. unsigned long zone_pfn;
  614. struct pbe *pbe = pagedir_nosave, *p;
  615. pr_debug("copy_data_pages(): pages to copy: %d\n", nr_copy_pages);
  616. for_each_zone (zone) {
  617. if (is_highmem(zone))
  618. continue;
  619. mark_free_pages(zone);
  620. /* This is necessary for swsusp_free() */
  621. for_each_pb_page (p, pagedir_nosave)
  622. SetPageNosaveFree(virt_to_page(p));
  623. for_each_pbe(p, pagedir_nosave)
  624. SetPageNosaveFree(virt_to_page(p->address));
  625. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
  626. if (saveable(zone, &zone_pfn)) {
  627. struct page * page;
  628. page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
  629. BUG_ON(!pbe);
  630. pbe->orig_address = (unsigned long)page_address(page);
  631. /* copy_page is not usable for copying task structs. */
  632. memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE);
  633. pbe = pbe->next;
  634. }
  635. }
  636. }
  637. BUG_ON(pbe);
  638. }
  639. /**
  640. * free_pagedir - free pages allocated with alloc_pagedir()
  641. */
  642. static inline void free_pagedir(struct pbe *pblist)
  643. {
  644. struct pbe *pbe;
  645. while (pblist) {
  646. pbe = (pblist + PB_PAGE_SKIP)->next;
  647. ClearPageNosave(virt_to_page(pblist));
  648. ClearPageNosaveFree(virt_to_page(pblist));
  649. free_page((unsigned long)pblist);
  650. pblist = pbe;
  651. }
  652. }
  653. /**
  654. * fill_pb_page - Create a list of PBEs on a given memory page
  655. */
  656. static inline void fill_pb_page(struct pbe *pbpage)
  657. {
  658. struct pbe *p;
  659. p = pbpage;
  660. pbpage += PB_PAGE_SKIP;
  661. do
  662. p->next = p + 1;
  663. while (++p < pbpage);
  664. }
  665. /**
  666. * create_pbe_list - Create a list of PBEs on top of a given chain
  667. * of memory pages allocated with alloc_pagedir()
  668. */
  669. static void create_pbe_list(struct pbe *pblist, unsigned nr_pages)
  670. {
  671. struct pbe *pbpage, *p;
  672. unsigned num = PBES_PER_PAGE;
  673. for_each_pb_page (pbpage, pblist) {
  674. if (num >= nr_pages)
  675. break;
  676. fill_pb_page(pbpage);
  677. num += PBES_PER_PAGE;
  678. }
  679. if (pbpage) {
  680. for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
  681. p->next = p + 1;
  682. p->next = NULL;
  683. }
  684. pr_debug("create_pbe_list(): initialized %d PBEs\n", num);
  685. }
  686. static void *alloc_image_page(void)
  687. {
  688. void *res = (void *)get_zeroed_page(GFP_ATOMIC | __GFP_COLD);
  689. if (res) {
  690. SetPageNosave(virt_to_page(res));
  691. SetPageNosaveFree(virt_to_page(res));
  692. }
  693. return res;
  694. }
  695. /**
  696. * alloc_pagedir - Allocate the page directory.
  697. *
  698. * First, determine exactly how many pages we need and
  699. * allocate them.
  700. *
  701. * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
  702. * struct pbe elements (pbes) and the last element in the page points
  703. * to the next page.
  704. *
  705. * On each page we set up a list of struct_pbe elements.
  706. */
  707. static struct pbe * alloc_pagedir(unsigned nr_pages)
  708. {
  709. unsigned num;
  710. struct pbe *pblist, *pbe;
  711. if (!nr_pages)
  712. return NULL;
  713. pr_debug("alloc_pagedir(): nr_pages = %d\n", nr_pages);
  714. pblist = (struct pbe *)alloc_image_page();
  715. for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
  716. pbe = pbe->next, num += PBES_PER_PAGE) {
  717. pbe += PB_PAGE_SKIP;
  718. pbe->next = (struct pbe *)alloc_image_page();
  719. }
  720. if (!pbe) { /* get_zeroed_page() failed */
  721. free_pagedir(pblist);
  722. pblist = NULL;
  723. }
  724. return pblist;
  725. }
  726. /**
  727. * Free pages we allocated for suspend. Suspend pages are alocated
  728. * before atomic copy, so we need to free them after resume.
  729. */
  730. void swsusp_free(void)
  731. {
  732. struct zone *zone;
  733. unsigned long zone_pfn;
  734. for_each_zone(zone) {
  735. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  736. if (pfn_valid(zone_pfn + zone->zone_start_pfn)) {
  737. struct page * page;
  738. page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
  739. if (PageNosave(page) && PageNosaveFree(page)) {
  740. ClearPageNosave(page);
  741. ClearPageNosaveFree(page);
  742. free_page((long) page_address(page));
  743. }
  744. }
  745. }
  746. }
  747. /**
  748. * enough_free_mem - Make sure we enough free memory to snapshot.
  749. *
  750. * Returns TRUE or FALSE after checking the number of available
  751. * free pages.
  752. */
  753. static int enough_free_mem(void)
  754. {
  755. pr_debug("swsusp: available memory: %u pages\n", nr_free_pages());
  756. return nr_free_pages() > (nr_copy_pages + PAGES_FOR_IO +
  757. nr_copy_pages/PBES_PER_PAGE + !!(nr_copy_pages%PBES_PER_PAGE));
  758. }
  759. /**
  760. * enough_swap - Make sure we have enough swap to save the image.
  761. *
  762. * Returns TRUE or FALSE after checking the total amount of swap
  763. * space avaiable.
  764. *
  765. * FIXME: si_swapinfo(&i) returns all swap devices information.
  766. * We should only consider resume_device.
  767. */
  768. static int enough_swap(void)
  769. {
  770. struct sysinfo i;
  771. si_swapinfo(&i);
  772. pr_debug("swsusp: available swap: %lu pages\n", i.freeswap);
  773. return i.freeswap > (nr_copy_pages + PAGES_FOR_IO +
  774. nr_copy_pages/PBES_PER_PAGE + !!(nr_copy_pages%PBES_PER_PAGE));
  775. }
  776. static int swsusp_alloc(void)
  777. {
  778. struct pbe * p;
  779. pagedir_nosave = NULL;
  780. if (!(pagedir_save = alloc_pagedir(nr_copy_pages))) {
  781. printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
  782. return -ENOMEM;
  783. }
  784. create_pbe_list(pagedir_save, nr_copy_pages);
  785. pagedir_nosave = pagedir_save;
  786. for_each_pbe (p, pagedir_save) {
  787. p->address = (unsigned long)alloc_image_page();
  788. if (!p->address) {
  789. printk(KERN_ERR "suspend: Allocating image pages failed.\n");
  790. swsusp_free();
  791. return -ENOMEM;
  792. }
  793. }
  794. return 0;
  795. }
  796. static int suspend_prepare_image(void)
  797. {
  798. int error;
  799. pr_debug("swsusp: critical section: \n");
  800. if (save_highmem()) {
  801. printk(KERN_CRIT "swsusp: Not enough free pages for highmem\n");
  802. restore_highmem();
  803. return -ENOMEM;
  804. }
  805. drain_local_pages();
  806. count_data_pages();
  807. printk("swsusp: Need to copy %u pages\n", nr_copy_pages);
  808. nr_copy_pages_check = nr_copy_pages;
  809. pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
  810. nr_copy_pages,
  811. nr_copy_pages/PBES_PER_PAGE + !!(nr_copy_pages%PBES_PER_PAGE),
  812. PAGES_FOR_IO, nr_free_pages());
  813. if (!enough_free_mem()) {
  814. printk(KERN_ERR "swsusp: Not enough free memory\n");
  815. return -ENOMEM;
  816. }
  817. if (MAX_PBES < nr_copy_pages / PBES_PER_PAGE +
  818. !!(nr_copy_pages % PBES_PER_PAGE)) {
  819. printk(KERN_ERR "swsusp: Too many image pages\n");
  820. return -ENOSPC;
  821. }
  822. if (!enough_swap()) {
  823. printk(KERN_ERR "swsusp: Not enough free swap\n");
  824. return -ENOSPC;
  825. }
  826. error = swsusp_alloc();
  827. if (error)
  828. return error;
  829. /* During allocating of suspend pagedir, new cold pages may appear.
  830. * Kill them.
  831. */
  832. drain_local_pages();
  833. copy_data_pages();
  834. /*
  835. * End of critical section. From now on, we can write to memory,
  836. * but we should not touch disk. This specially means we must _not_
  837. * touch swap space! Except we must write out our image of course.
  838. */
  839. printk("swsusp: critical section/: done (%d pages copied)\n", nr_copy_pages );
  840. return 0;
  841. }
  842. /* It is important _NOT_ to umount filesystems at this point. We want
  843. * them synced (in case something goes wrong) but we DO not want to mark
  844. * filesystem clean: it is not. (And it does not matter, if we resume
  845. * correctly, we'll mark system clean, anyway.)
  846. */
  847. int swsusp_write(void)
  848. {
  849. int error;
  850. device_resume();
  851. lock_swapdevices();
  852. error = write_suspend_image();
  853. /* This will unlock ignored swap devices since writing is finished */
  854. lock_swapdevices();
  855. return error;
  856. }
  857. extern asmlinkage int swsusp_arch_suspend(void);
  858. extern asmlinkage int swsusp_arch_resume(void);
  859. asmlinkage int swsusp_save(void)
  860. {
  861. return suspend_prepare_image();
  862. }
  863. int swsusp_suspend(void)
  864. {
  865. int error;
  866. if ((error = arch_prepare_suspend()))
  867. return error;
  868. local_irq_disable();
  869. /* At this point, device_suspend() has been called, but *not*
  870. * device_power_down(). We *must* device_power_down() now.
  871. * Otherwise, drivers for some devices (e.g. interrupt controllers)
  872. * become desynchronized with the actual state of the hardware
  873. * at resume time, and evil weirdness ensues.
  874. */
  875. if ((error = device_power_down(PMSG_FREEZE))) {
  876. printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
  877. local_irq_enable();
  878. return error;
  879. }
  880. if ((error = swsusp_swap_check())) {
  881. printk(KERN_ERR "swsusp: cannot find swap device, try swapon -a.\n");
  882. device_power_up();
  883. local_irq_enable();
  884. return error;
  885. }
  886. save_processor_state();
  887. if ((error = swsusp_arch_suspend()))
  888. printk(KERN_ERR "Error %d suspending\n", error);
  889. /* Restore control flow magically appears here */
  890. restore_processor_state();
  891. BUG_ON (nr_copy_pages_check != nr_copy_pages);
  892. restore_highmem();
  893. device_power_up();
  894. local_irq_enable();
  895. return error;
  896. }
  897. int swsusp_resume(void)
  898. {
  899. int error;
  900. local_irq_disable();
  901. if (device_power_down(PMSG_FREEZE))
  902. printk(KERN_ERR "Some devices failed to power down, very bad\n");
  903. /* We'll ignore saved state, but this gets preempt count (etc) right */
  904. save_processor_state();
  905. error = swsusp_arch_resume();
  906. /* Code below is only ever reached in case of failure. Otherwise
  907. * execution continues at place where swsusp_arch_suspend was called
  908. */
  909. BUG_ON(!error);
  910. restore_processor_state();
  911. restore_highmem();
  912. touch_softlockup_watchdog();
  913. device_power_up();
  914. local_irq_enable();
  915. return error;
  916. }
  917. /**
  918. * On resume, for storing the PBE list and the image,
  919. * we can only use memory pages that do not conflict with the pages
  920. * which had been used before suspend.
  921. *
  922. * We don't know which pages are usable until we allocate them.
  923. *
  924. * Allocated but unusable (ie eaten) memory pages are linked together
  925. * to create a list, so that we can free them easily
  926. *
  927. * We could have used a type other than (void *)
  928. * for this purpose, but ...
  929. */
  930. static void **eaten_memory = NULL;
  931. static inline void eat_page(void *page)
  932. {
  933. void **c;
  934. c = eaten_memory;
  935. eaten_memory = page;
  936. *eaten_memory = c;
  937. }
  938. unsigned long get_usable_page(gfp_t gfp_mask)
  939. {
  940. unsigned long m;
  941. m = get_zeroed_page(gfp_mask);
  942. while (!PageNosaveFree(virt_to_page(m))) {
  943. eat_page((void *)m);
  944. m = get_zeroed_page(gfp_mask);
  945. if (!m)
  946. break;
  947. }
  948. return m;
  949. }
  950. void free_eaten_memory(void)
  951. {
  952. unsigned long m;
  953. void **c;
  954. int i = 0;
  955. c = eaten_memory;
  956. while (c) {
  957. m = (unsigned long)c;
  958. c = *c;
  959. free_page(m);
  960. i++;
  961. }
  962. eaten_memory = NULL;
  963. pr_debug("swsusp: %d unused pages freed\n", i);
  964. }
  965. /**
  966. * check_pagedir - We ensure here that pages that the PBEs point to
  967. * won't collide with pages where we're going to restore from the loaded
  968. * pages later
  969. */
  970. static int check_pagedir(struct pbe *pblist)
  971. {
  972. struct pbe *p;
  973. /* This is necessary, so that we can free allocated pages
  974. * in case of failure
  975. */
  976. for_each_pbe (p, pblist)
  977. p->address = 0UL;
  978. for_each_pbe (p, pblist) {
  979. p->address = get_usable_page(GFP_ATOMIC);
  980. if (!p->address)
  981. return -ENOMEM;
  982. }
  983. return 0;
  984. }
  985. /**
  986. * swsusp_pagedir_relocate - It is possible, that some memory pages
  987. * occupied by the list of PBEs collide with pages where we're going to
  988. * restore from the loaded pages later. We relocate them here.
  989. */
  990. static struct pbe * swsusp_pagedir_relocate(struct pbe *pblist)
  991. {
  992. struct zone *zone;
  993. unsigned long zone_pfn;
  994. struct pbe *pbpage, *tail, *p;
  995. void *m;
  996. int rel = 0, error = 0;
  997. if (!pblist) /* a sanity check */
  998. return NULL;
  999. pr_debug("swsusp: Relocating pagedir (%lu pages to check)\n",
  1000. swsusp_info.pagedir_pages);
  1001. /* Set page flags */
  1002. for_each_zone (zone) {
  1003. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  1004. SetPageNosaveFree(pfn_to_page(zone_pfn +
  1005. zone->zone_start_pfn));
  1006. }
  1007. /* Clear orig addresses */
  1008. for_each_pbe (p, pblist)
  1009. ClearPageNosaveFree(virt_to_page(p->orig_address));
  1010. tail = pblist + PB_PAGE_SKIP;
  1011. /* Relocate colliding pages */
  1012. for_each_pb_page (pbpage, pblist) {
  1013. if (!PageNosaveFree(virt_to_page((unsigned long)pbpage))) {
  1014. m = (void *)get_usable_page(GFP_ATOMIC | __GFP_COLD);
  1015. if (!m) {
  1016. error = -ENOMEM;
  1017. break;
  1018. }
  1019. memcpy(m, (void *)pbpage, PAGE_SIZE);
  1020. if (pbpage == pblist)
  1021. pblist = (struct pbe *)m;
  1022. else
  1023. tail->next = (struct pbe *)m;
  1024. eat_page((void *)pbpage);
  1025. pbpage = (struct pbe *)m;
  1026. /* We have to link the PBEs again */
  1027. for (p = pbpage; p < pbpage + PB_PAGE_SKIP; p++)
  1028. if (p->next) /* needed to save the end */
  1029. p->next = p + 1;
  1030. rel++;
  1031. }
  1032. tail = pbpage + PB_PAGE_SKIP;
  1033. }
  1034. if (error) {
  1035. printk("\nswsusp: Out of memory\n\n");
  1036. free_pagedir(pblist);
  1037. free_eaten_memory();
  1038. pblist = NULL;
  1039. /* Is this even worth handling? It should never ever happen, and we
  1040. have just lost user's state, anyway... */
  1041. } else
  1042. printk("swsusp: Relocated %d pages\n", rel);
  1043. return pblist;
  1044. }
  1045. /*
  1046. * Using bio to read from swap.
  1047. * This code requires a bit more work than just using buffer heads
  1048. * but, it is the recommended way for 2.5/2.6.
  1049. * The following are to signal the beginning and end of I/O. Bios
  1050. * finish asynchronously, while we want them to happen synchronously.
  1051. * A simple atomic_t, and a wait loop take care of this problem.
  1052. */
  1053. static atomic_t io_done = ATOMIC_INIT(0);
  1054. static int end_io(struct bio * bio, unsigned int num, int err)
  1055. {
  1056. if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  1057. panic("I/O error reading memory image");
  1058. atomic_set(&io_done, 0);
  1059. return 0;
  1060. }
  1061. static struct block_device * resume_bdev;
  1062. /**
  1063. * submit - submit BIO request.
  1064. * @rw: READ or WRITE.
  1065. * @off physical offset of page.
  1066. * @page: page we're reading or writing.
  1067. *
  1068. * Straight from the textbook - allocate and initialize the bio.
  1069. * If we're writing, make sure the page is marked as dirty.
  1070. * Then submit it and wait.
  1071. */
  1072. static int submit(int rw, pgoff_t page_off, void * page)
  1073. {
  1074. int error = 0;
  1075. struct bio * bio;
  1076. bio = bio_alloc(GFP_ATOMIC, 1);
  1077. if (!bio)
  1078. return -ENOMEM;
  1079. bio->bi_sector = page_off * (PAGE_SIZE >> 9);
  1080. bio_get(bio);
  1081. bio->bi_bdev = resume_bdev;
  1082. bio->bi_end_io = end_io;
  1083. if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
  1084. printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
  1085. error = -EFAULT;
  1086. goto Done;
  1087. }
  1088. if (rw == WRITE)
  1089. bio_set_pages_dirty(bio);
  1090. atomic_set(&io_done, 1);
  1091. submit_bio(rw | (1 << BIO_RW_SYNC), bio);
  1092. while (atomic_read(&io_done))
  1093. yield();
  1094. Done:
  1095. bio_put(bio);
  1096. return error;
  1097. }
  1098. static int bio_read_page(pgoff_t page_off, void * page)
  1099. {
  1100. return submit(READ, page_off, page);
  1101. }
  1102. static int bio_write_page(pgoff_t page_off, void * page)
  1103. {
  1104. return submit(WRITE, page_off, page);
  1105. }
  1106. /*
  1107. * Sanity check if this image makes sense with this kernel/swap context
  1108. * I really don't think that it's foolproof but more than nothing..
  1109. */
  1110. static const char * sanity_check(void)
  1111. {
  1112. dump_info();
  1113. if (swsusp_info.version_code != LINUX_VERSION_CODE)
  1114. return "kernel version";
  1115. if (swsusp_info.num_physpages != num_physpages)
  1116. return "memory size";
  1117. if (strcmp(swsusp_info.uts.sysname,system_utsname.sysname))
  1118. return "system type";
  1119. if (strcmp(swsusp_info.uts.release,system_utsname.release))
  1120. return "kernel release";
  1121. if (strcmp(swsusp_info.uts.version,system_utsname.version))
  1122. return "version";
  1123. if (strcmp(swsusp_info.uts.machine,system_utsname.machine))
  1124. return "machine";
  1125. #if 0
  1126. /* We can't use number of online CPUs when we use hotplug to remove them ;-))) */
  1127. if (swsusp_info.cpus != num_possible_cpus())
  1128. return "number of cpus";
  1129. #endif
  1130. return NULL;
  1131. }
  1132. static int check_header(void)
  1133. {
  1134. const char * reason = NULL;
  1135. int error;
  1136. if ((error = bio_read_page(swp_offset(swsusp_header.swsusp_info), &swsusp_info)))
  1137. return error;
  1138. /* Is this same machine? */
  1139. if ((reason = sanity_check())) {
  1140. printk(KERN_ERR "swsusp: Resume mismatch: %s\n",reason);
  1141. return -EPERM;
  1142. }
  1143. nr_copy_pages = swsusp_info.image_pages;
  1144. return error;
  1145. }
  1146. static int check_sig(void)
  1147. {
  1148. int error;
  1149. memset(&swsusp_header, 0, sizeof(swsusp_header));
  1150. if ((error = bio_read_page(0, &swsusp_header)))
  1151. return error;
  1152. if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
  1153. memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
  1154. memcpy(key_iv, swsusp_header.key_iv, MAXKEY+MAXIV);
  1155. memset(swsusp_header.key_iv, 0, MAXKEY+MAXIV);
  1156. /*
  1157. * Reset swap signature now.
  1158. */
  1159. error = bio_write_page(0, &swsusp_header);
  1160. } else {
  1161. return -EINVAL;
  1162. }
  1163. if (!error)
  1164. pr_debug("swsusp: Signature found, resuming\n");
  1165. return error;
  1166. }
  1167. /**
  1168. * data_read - Read image pages from swap.
  1169. *
  1170. * You do not need to check for overlaps, check_pagedir()
  1171. * already did that.
  1172. */
  1173. static int data_read(struct pbe *pblist)
  1174. {
  1175. struct pbe * p;
  1176. int error = 0;
  1177. int i = 0;
  1178. int mod = swsusp_info.image_pages / 100;
  1179. void *tfm;
  1180. if ((error = crypto_init(0, &tfm)))
  1181. return error;
  1182. if (!mod)
  1183. mod = 1;
  1184. printk("swsusp: Reading image data (%lu pages): ",
  1185. swsusp_info.image_pages);
  1186. for_each_pbe (p, pblist) {
  1187. if (!(i % mod))
  1188. printk("\b\b\b\b%3d%%", i / mod);
  1189. if ((error = crypto_read(p, tfm))) {
  1190. crypto_exit(tfm);
  1191. return error;
  1192. }
  1193. i++;
  1194. }
  1195. printk("\b\b\b\bdone\n");
  1196. crypto_exit(tfm);
  1197. return error;
  1198. }
  1199. /**
  1200. * read_pagedir - Read page backup list pages from swap
  1201. */
  1202. static int read_pagedir(struct pbe *pblist)
  1203. {
  1204. struct pbe *pbpage, *p;
  1205. unsigned i = 0;
  1206. int error;
  1207. if (!pblist)
  1208. return -EFAULT;
  1209. printk("swsusp: Reading pagedir (%lu pages)\n",
  1210. swsusp_info.pagedir_pages);
  1211. for_each_pb_page (pbpage, pblist) {
  1212. unsigned long offset = swp_offset(swsusp_info.pagedir[i++]);
  1213. error = -EFAULT;
  1214. if (offset) {
  1215. p = (pbpage + PB_PAGE_SKIP)->next;
  1216. error = bio_read_page(offset, (void *)pbpage);
  1217. (pbpage + PB_PAGE_SKIP)->next = p;
  1218. }
  1219. if (error)
  1220. break;
  1221. }
  1222. if (error)
  1223. free_pagedir(pblist);
  1224. else
  1225. BUG_ON(i != swsusp_info.pagedir_pages);
  1226. return error;
  1227. }
  1228. static int check_suspend_image(void)
  1229. {
  1230. int error = 0;
  1231. if ((error = check_sig()))
  1232. return error;
  1233. if ((error = check_header()))
  1234. return error;
  1235. return 0;
  1236. }
  1237. static int read_suspend_image(void)
  1238. {
  1239. int error = 0;
  1240. struct pbe *p;
  1241. if (!(p = alloc_pagedir(nr_copy_pages)))
  1242. return -ENOMEM;
  1243. if ((error = read_pagedir(p)))
  1244. return error;
  1245. create_pbe_list(p, nr_copy_pages);
  1246. if (!(pagedir_nosave = swsusp_pagedir_relocate(p)))
  1247. return -ENOMEM;
  1248. /* Allocate memory for the image and read the data from swap */
  1249. error = check_pagedir(pagedir_nosave);
  1250. if (!error)
  1251. error = data_read(pagedir_nosave);
  1252. if (error) { /* We fail cleanly */
  1253. free_eaten_memory();
  1254. for_each_pbe (p, pagedir_nosave)
  1255. if (p->address) {
  1256. free_page(p->address);
  1257. p->address = 0UL;
  1258. }
  1259. free_pagedir(pagedir_nosave);
  1260. }
  1261. return error;
  1262. }
  1263. /**
  1264. * swsusp_check - Check for saved image in swap
  1265. */
  1266. int swsusp_check(void)
  1267. {
  1268. int error;
  1269. resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
  1270. if (!IS_ERR(resume_bdev)) {
  1271. set_blocksize(resume_bdev, PAGE_SIZE);
  1272. error = check_suspend_image();
  1273. if (error)
  1274. blkdev_put(resume_bdev);
  1275. } else
  1276. error = PTR_ERR(resume_bdev);
  1277. if (!error)
  1278. pr_debug("swsusp: resume file found\n");
  1279. else
  1280. pr_debug("swsusp: Error %d check for resume file\n", error);
  1281. return error;
  1282. }
  1283. /**
  1284. * swsusp_read - Read saved image from swap.
  1285. */
  1286. int swsusp_read(void)
  1287. {
  1288. int error;
  1289. if (IS_ERR(resume_bdev)) {
  1290. pr_debug("swsusp: block device not initialised\n");
  1291. return PTR_ERR(resume_bdev);
  1292. }
  1293. error = read_suspend_image();
  1294. blkdev_put(resume_bdev);
  1295. memset(key_iv, 0, MAXKEY+MAXIV);
  1296. if (!error)
  1297. pr_debug("swsusp: Reading resume file was successful\n");
  1298. else
  1299. pr_debug("swsusp: Error %d resuming\n", error);
  1300. return error;
  1301. }
  1302. /**
  1303. * swsusp_close - close swap device.
  1304. */
  1305. void swsusp_close(void)
  1306. {
  1307. if (IS_ERR(resume_bdev)) {
  1308. pr_debug("swsusp: block device not initialised\n");
  1309. return;
  1310. }
  1311. blkdev_put(resume_bdev);
  1312. }