swsusp.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  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. int i;
  348. for (i = 0; i < nr_copy_pages; i++) {
  349. entry = (pagedir_nosave + i)->swap_address;
  350. if (entry.val)
  351. swap_free(entry);
  352. else
  353. break;
  354. (pagedir_nosave + i)->swap_address = (swp_entry_t){0};
  355. }
  356. }
  357. /**
  358. * data_write - Write saved image to swap.
  359. *
  360. * Walk the list of pages in the image and sync each one to swap.
  361. */
  362. static int data_write(void)
  363. {
  364. int error = 0, i = 0;
  365. unsigned int mod = nr_copy_pages / 100;
  366. struct pbe *p;
  367. void *tfm;
  368. if ((error = crypto_init(1, &tfm)))
  369. return error;
  370. if (!mod)
  371. mod = 1;
  372. printk( "Writing data to swap (%d pages)... ", nr_copy_pages );
  373. for_each_pbe (p, pagedir_nosave) {
  374. if (!(i%mod))
  375. printk( "\b\b\b\b%3d%%", i / mod );
  376. if ((error = crypto_write(p, tfm))) {
  377. crypto_exit(tfm);
  378. return error;
  379. }
  380. i++;
  381. }
  382. printk("\b\b\b\bdone\n");
  383. crypto_exit(tfm);
  384. return error;
  385. }
  386. static void dump_info(void)
  387. {
  388. pr_debug(" swsusp: Version: %u\n",swsusp_info.version_code);
  389. pr_debug(" swsusp: Num Pages: %ld\n",swsusp_info.num_physpages);
  390. pr_debug(" swsusp: UTS Sys: %s\n",swsusp_info.uts.sysname);
  391. pr_debug(" swsusp: UTS Node: %s\n",swsusp_info.uts.nodename);
  392. pr_debug(" swsusp: UTS Release: %s\n",swsusp_info.uts.release);
  393. pr_debug(" swsusp: UTS Version: %s\n",swsusp_info.uts.version);
  394. pr_debug(" swsusp: UTS Machine: %s\n",swsusp_info.uts.machine);
  395. pr_debug(" swsusp: UTS Domain: %s\n",swsusp_info.uts.domainname);
  396. pr_debug(" swsusp: CPUs: %d\n",swsusp_info.cpus);
  397. pr_debug(" swsusp: Image: %ld Pages\n",swsusp_info.image_pages);
  398. pr_debug(" swsusp: Pagedir: %ld Pages\n",swsusp_info.pagedir_pages);
  399. }
  400. static void init_header(void)
  401. {
  402. memset(&swsusp_info, 0, sizeof(swsusp_info));
  403. swsusp_info.version_code = LINUX_VERSION_CODE;
  404. swsusp_info.num_physpages = num_physpages;
  405. memcpy(&swsusp_info.uts, &system_utsname, sizeof(system_utsname));
  406. swsusp_info.suspend_pagedir = pagedir_nosave;
  407. swsusp_info.cpus = num_online_cpus();
  408. swsusp_info.image_pages = nr_copy_pages;
  409. }
  410. static int close_swap(void)
  411. {
  412. swp_entry_t entry;
  413. int error;
  414. dump_info();
  415. error = write_page((unsigned long)&swsusp_info, &entry);
  416. if (!error) {
  417. printk( "S" );
  418. error = mark_swapfiles(entry);
  419. printk( "|\n" );
  420. }
  421. return error;
  422. }
  423. /**
  424. * free_pagedir_entries - Free pages used by the page directory.
  425. *
  426. * This is used during suspend for error recovery.
  427. */
  428. static void free_pagedir_entries(void)
  429. {
  430. int i;
  431. for (i = 0; i < swsusp_info.pagedir_pages; i++)
  432. swap_free(swsusp_info.pagedir[i]);
  433. }
  434. /**
  435. * write_pagedir - Write the array of pages holding the page directory.
  436. * @last: Last swap entry we write (needed for header).
  437. */
  438. static int write_pagedir(void)
  439. {
  440. int error = 0;
  441. unsigned n = 0;
  442. struct pbe * pbe;
  443. printk( "Writing pagedir...");
  444. for_each_pb_page (pbe, pagedir_nosave) {
  445. if ((error = write_page((unsigned long)pbe, &swsusp_info.pagedir[n++])))
  446. return error;
  447. }
  448. swsusp_info.pagedir_pages = n;
  449. printk("done (%u pages)\n", n);
  450. return error;
  451. }
  452. /**
  453. * write_suspend_image - Write entire image and metadata.
  454. *
  455. */
  456. static int write_suspend_image(void)
  457. {
  458. int error;
  459. init_header();
  460. if ((error = data_write()))
  461. goto FreeData;
  462. if ((error = write_pagedir()))
  463. goto FreePagedir;
  464. if ((error = close_swap()))
  465. goto FreePagedir;
  466. Done:
  467. memset(key_iv, 0, MAXKEY+MAXIV);
  468. return error;
  469. FreePagedir:
  470. free_pagedir_entries();
  471. FreeData:
  472. data_free();
  473. goto Done;
  474. }
  475. #ifdef CONFIG_HIGHMEM
  476. struct highmem_page {
  477. char *data;
  478. struct page *page;
  479. struct highmem_page *next;
  480. };
  481. static struct highmem_page *highmem_copy;
  482. static int save_highmem_zone(struct zone *zone)
  483. {
  484. unsigned long zone_pfn;
  485. mark_free_pages(zone);
  486. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
  487. struct page *page;
  488. struct highmem_page *save;
  489. void *kaddr;
  490. unsigned long pfn = zone_pfn + zone->zone_start_pfn;
  491. if (!(pfn%1000))
  492. printk(".");
  493. if (!pfn_valid(pfn))
  494. continue;
  495. page = pfn_to_page(pfn);
  496. /*
  497. * This condition results from rvmalloc() sans vmalloc_32()
  498. * and architectural memory reservations. This should be
  499. * corrected eventually when the cases giving rise to this
  500. * are better understood.
  501. */
  502. if (PageReserved(page)) {
  503. printk("highmem reserved page?!\n");
  504. continue;
  505. }
  506. BUG_ON(PageNosave(page));
  507. if (PageNosaveFree(page))
  508. continue;
  509. save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
  510. if (!save)
  511. return -ENOMEM;
  512. save->next = highmem_copy;
  513. save->page = page;
  514. save->data = (void *) get_zeroed_page(GFP_ATOMIC);
  515. if (!save->data) {
  516. kfree(save);
  517. return -ENOMEM;
  518. }
  519. kaddr = kmap_atomic(page, KM_USER0);
  520. memcpy(save->data, kaddr, PAGE_SIZE);
  521. kunmap_atomic(kaddr, KM_USER0);
  522. highmem_copy = save;
  523. }
  524. return 0;
  525. }
  526. #endif /* CONFIG_HIGHMEM */
  527. static int save_highmem(void)
  528. {
  529. #ifdef CONFIG_HIGHMEM
  530. struct zone *zone;
  531. int res = 0;
  532. pr_debug("swsusp: Saving Highmem\n");
  533. for_each_zone (zone) {
  534. if (is_highmem(zone))
  535. res = save_highmem_zone(zone);
  536. if (res)
  537. return res;
  538. }
  539. #endif
  540. return 0;
  541. }
  542. static int restore_highmem(void)
  543. {
  544. #ifdef CONFIG_HIGHMEM
  545. printk("swsusp: Restoring Highmem\n");
  546. while (highmem_copy) {
  547. struct highmem_page *save = highmem_copy;
  548. void *kaddr;
  549. highmem_copy = save->next;
  550. kaddr = kmap_atomic(save->page, KM_USER0);
  551. memcpy(kaddr, save->data, PAGE_SIZE);
  552. kunmap_atomic(kaddr, KM_USER0);
  553. free_page((long) save->data);
  554. kfree(save);
  555. }
  556. #endif
  557. return 0;
  558. }
  559. static int pfn_is_nosave(unsigned long pfn)
  560. {
  561. unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
  562. unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
  563. return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
  564. }
  565. /**
  566. * saveable - Determine whether a page should be cloned or not.
  567. * @pfn: The page
  568. *
  569. * We save a page if it's Reserved, and not in the range of pages
  570. * statically defined as 'unsaveable', or if it isn't reserved, and
  571. * isn't part of a free chunk of pages.
  572. */
  573. static int saveable(struct zone * zone, unsigned long * zone_pfn)
  574. {
  575. unsigned long pfn = *zone_pfn + zone->zone_start_pfn;
  576. struct page * page;
  577. if (!pfn_valid(pfn))
  578. return 0;
  579. page = pfn_to_page(pfn);
  580. BUG_ON(PageReserved(page) && PageNosave(page));
  581. if (PageNosave(page))
  582. return 0;
  583. if (PageReserved(page) && pfn_is_nosave(pfn)) {
  584. pr_debug("[nosave pfn 0x%lx]", pfn);
  585. return 0;
  586. }
  587. if (PageNosaveFree(page))
  588. return 0;
  589. return 1;
  590. }
  591. static void count_data_pages(void)
  592. {
  593. struct zone *zone;
  594. unsigned long zone_pfn;
  595. nr_copy_pages = 0;
  596. for_each_zone (zone) {
  597. if (is_highmem(zone))
  598. continue;
  599. mark_free_pages(zone);
  600. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  601. nr_copy_pages += saveable(zone, &zone_pfn);
  602. }
  603. }
  604. static void copy_data_pages(void)
  605. {
  606. struct zone *zone;
  607. unsigned long zone_pfn;
  608. struct pbe * pbe = pagedir_nosave;
  609. pr_debug("copy_data_pages(): pages to copy: %d\n", nr_copy_pages);
  610. for_each_zone (zone) {
  611. if (is_highmem(zone))
  612. continue;
  613. mark_free_pages(zone);
  614. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
  615. if (saveable(zone, &zone_pfn)) {
  616. struct page * page;
  617. page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
  618. BUG_ON(!pbe);
  619. pbe->orig_address = (long) page_address(page);
  620. /* copy_page is not usable for copying task structs. */
  621. memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE);
  622. pbe = pbe->next;
  623. }
  624. }
  625. }
  626. BUG_ON(pbe);
  627. }
  628. /**
  629. * calc_nr - Determine the number of pages needed for a pbe list.
  630. */
  631. static int calc_nr(int nr_copy)
  632. {
  633. return nr_copy + (nr_copy+PBES_PER_PAGE-2)/(PBES_PER_PAGE-1);
  634. }
  635. /**
  636. * free_pagedir - free pages allocated with alloc_pagedir()
  637. */
  638. static inline void free_pagedir(struct pbe *pblist)
  639. {
  640. struct pbe *pbe;
  641. while (pblist) {
  642. pbe = (pblist + PB_PAGE_SKIP)->next;
  643. free_page((unsigned long)pblist);
  644. pblist = pbe;
  645. }
  646. }
  647. /**
  648. * fill_pb_page - Create a list of PBEs on a given memory page
  649. */
  650. static inline void fill_pb_page(struct pbe *pbpage)
  651. {
  652. struct pbe *p;
  653. p = pbpage;
  654. pbpage += PB_PAGE_SKIP;
  655. do
  656. p->next = p + 1;
  657. while (++p < pbpage);
  658. }
  659. /**
  660. * create_pbe_list - Create a list of PBEs on top of a given chain
  661. * of memory pages allocated with alloc_pagedir()
  662. */
  663. static void create_pbe_list(struct pbe *pblist, unsigned nr_pages)
  664. {
  665. struct pbe *pbpage, *p;
  666. unsigned num = PBES_PER_PAGE;
  667. for_each_pb_page (pbpage, pblist) {
  668. if (num >= nr_pages)
  669. break;
  670. fill_pb_page(pbpage);
  671. num += PBES_PER_PAGE;
  672. }
  673. if (pbpage) {
  674. for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
  675. p->next = p + 1;
  676. p->next = NULL;
  677. }
  678. pr_debug("create_pbe_list(): initialized %d PBEs\n", num);
  679. }
  680. /**
  681. * alloc_pagedir - Allocate the page directory.
  682. *
  683. * First, determine exactly how many pages we need and
  684. * allocate them.
  685. *
  686. * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
  687. * struct pbe elements (pbes) and the last element in the page points
  688. * to the next page.
  689. *
  690. * On each page we set up a list of struct_pbe elements.
  691. */
  692. static struct pbe * alloc_pagedir(unsigned nr_pages)
  693. {
  694. unsigned num;
  695. struct pbe *pblist, *pbe;
  696. if (!nr_pages)
  697. return NULL;
  698. pr_debug("alloc_pagedir(): nr_pages = %d\n", nr_pages);
  699. pblist = (struct pbe *)get_zeroed_page(GFP_ATOMIC | __GFP_COLD);
  700. for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
  701. pbe = pbe->next, num += PBES_PER_PAGE) {
  702. pbe += PB_PAGE_SKIP;
  703. pbe->next = (struct pbe *)get_zeroed_page(GFP_ATOMIC | __GFP_COLD);
  704. }
  705. if (!pbe) { /* get_zeroed_page() failed */
  706. free_pagedir(pblist);
  707. pblist = NULL;
  708. }
  709. return pblist;
  710. }
  711. /**
  712. * free_image_pages - Free pages allocated for snapshot
  713. */
  714. static void free_image_pages(void)
  715. {
  716. struct pbe * p;
  717. for_each_pbe (p, pagedir_save) {
  718. if (p->address) {
  719. ClearPageNosave(virt_to_page(p->address));
  720. free_page(p->address);
  721. p->address = 0;
  722. }
  723. }
  724. }
  725. /**
  726. * alloc_image_pages - Allocate pages for the snapshot.
  727. */
  728. static int alloc_image_pages(void)
  729. {
  730. struct pbe * p;
  731. for_each_pbe (p, pagedir_save) {
  732. p->address = get_zeroed_page(GFP_ATOMIC | __GFP_COLD);
  733. if (!p->address)
  734. return -ENOMEM;
  735. SetPageNosave(virt_to_page(p->address));
  736. }
  737. return 0;
  738. }
  739. /* Free pages we allocated for suspend. Suspend pages are alocated
  740. * before atomic copy, so we need to free them after resume.
  741. */
  742. void swsusp_free(void)
  743. {
  744. BUG_ON(PageNosave(virt_to_page(pagedir_save)));
  745. BUG_ON(PageNosaveFree(virt_to_page(pagedir_save)));
  746. free_image_pages();
  747. free_pagedir(pagedir_save);
  748. }
  749. /**
  750. * enough_free_mem - Make sure we enough free memory to snapshot.
  751. *
  752. * Returns TRUE or FALSE after checking the number of available
  753. * free pages.
  754. */
  755. static int enough_free_mem(void)
  756. {
  757. if (nr_free_pages() < (nr_copy_pages + PAGES_FOR_IO)) {
  758. pr_debug("swsusp: Not enough free pages: Have %d\n",
  759. nr_free_pages());
  760. return 0;
  761. }
  762. return 1;
  763. }
  764. /**
  765. * enough_swap - Make sure we have enough swap to save the image.
  766. *
  767. * Returns TRUE or FALSE after checking the total amount of swap
  768. * space avaiable.
  769. *
  770. * FIXME: si_swapinfo(&i) returns all swap devices information.
  771. * We should only consider resume_device.
  772. */
  773. static int enough_swap(void)
  774. {
  775. struct sysinfo i;
  776. si_swapinfo(&i);
  777. if (i.freeswap < (nr_copy_pages + PAGES_FOR_IO)) {
  778. pr_debug("swsusp: Not enough swap. Need %ld\n",i.freeswap);
  779. return 0;
  780. }
  781. return 1;
  782. }
  783. static int swsusp_alloc(void)
  784. {
  785. int error;
  786. pagedir_nosave = NULL;
  787. nr_copy_pages = calc_nr(nr_copy_pages);
  788. nr_copy_pages_check = nr_copy_pages;
  789. pr_debug("suspend: (pages needed: %d + %d free: %d)\n",
  790. nr_copy_pages, PAGES_FOR_IO, nr_free_pages());
  791. if (!enough_free_mem())
  792. return -ENOMEM;
  793. if (!enough_swap())
  794. return -ENOSPC;
  795. if (!(pagedir_save = alloc_pagedir(nr_copy_pages))) {
  796. printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
  797. return -ENOMEM;
  798. }
  799. create_pbe_list(pagedir_save, nr_copy_pages);
  800. pagedir_nosave = pagedir_save;
  801. if ((error = alloc_image_pages())) {
  802. printk(KERN_ERR "suspend: Allocating image pages failed.\n");
  803. swsusp_free();
  804. return error;
  805. }
  806. return 0;
  807. }
  808. static int suspend_prepare_image(void)
  809. {
  810. int error;
  811. pr_debug("swsusp: critical section: \n");
  812. if (save_highmem()) {
  813. printk(KERN_CRIT "Suspend machine: Not enough free pages for highmem\n");
  814. restore_highmem();
  815. return -ENOMEM;
  816. }
  817. drain_local_pages();
  818. count_data_pages();
  819. printk("swsusp: Need to copy %u pages\n", nr_copy_pages);
  820. error = swsusp_alloc();
  821. if (error)
  822. return error;
  823. /* During allocating of suspend pagedir, new cold pages may appear.
  824. * Kill them.
  825. */
  826. drain_local_pages();
  827. copy_data_pages();
  828. /*
  829. * End of critical section. From now on, we can write to memory,
  830. * but we should not touch disk. This specially means we must _not_
  831. * touch swap space! Except we must write out our image of course.
  832. */
  833. printk("swsusp: critical section/: done (%d pages copied)\n", nr_copy_pages );
  834. return 0;
  835. }
  836. /* It is important _NOT_ to umount filesystems at this point. We want
  837. * them synced (in case something goes wrong) but we DO not want to mark
  838. * filesystem clean: it is not. (And it does not matter, if we resume
  839. * correctly, we'll mark system clean, anyway.)
  840. */
  841. int swsusp_write(void)
  842. {
  843. int error;
  844. device_resume();
  845. lock_swapdevices();
  846. error = write_suspend_image();
  847. /* This will unlock ignored swap devices since writing is finished */
  848. lock_swapdevices();
  849. return error;
  850. }
  851. extern asmlinkage int swsusp_arch_suspend(void);
  852. extern asmlinkage int swsusp_arch_resume(void);
  853. asmlinkage int swsusp_save(void)
  854. {
  855. return suspend_prepare_image();
  856. }
  857. int swsusp_suspend(void)
  858. {
  859. int error;
  860. if ((error = arch_prepare_suspend()))
  861. return error;
  862. local_irq_disable();
  863. /* At this point, device_suspend() has been called, but *not*
  864. * device_power_down(). We *must* device_power_down() now.
  865. * Otherwise, drivers for some devices (e.g. interrupt controllers)
  866. * become desynchronized with the actual state of the hardware
  867. * at resume time, and evil weirdness ensues.
  868. */
  869. if ((error = device_power_down(PMSG_FREEZE))) {
  870. printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
  871. local_irq_enable();
  872. return error;
  873. }
  874. if ((error = swsusp_swap_check())) {
  875. printk(KERN_ERR "swsusp: cannot find swap device, try swapon -a.\n");
  876. device_power_up();
  877. local_irq_enable();
  878. return error;
  879. }
  880. save_processor_state();
  881. if ((error = swsusp_arch_suspend()))
  882. printk(KERN_ERR "Error %d suspending\n", error);
  883. /* Restore control flow magically appears here */
  884. restore_processor_state();
  885. BUG_ON (nr_copy_pages_check != nr_copy_pages);
  886. restore_highmem();
  887. device_power_up();
  888. local_irq_enable();
  889. return error;
  890. }
  891. int swsusp_resume(void)
  892. {
  893. int error;
  894. local_irq_disable();
  895. if (device_power_down(PMSG_FREEZE))
  896. printk(KERN_ERR "Some devices failed to power down, very bad\n");
  897. /* We'll ignore saved state, but this gets preempt count (etc) right */
  898. save_processor_state();
  899. error = swsusp_arch_resume();
  900. /* Code below is only ever reached in case of failure. Otherwise
  901. * execution continues at place where swsusp_arch_suspend was called
  902. */
  903. BUG_ON(!error);
  904. restore_processor_state();
  905. restore_highmem();
  906. touch_softlockup_watchdog();
  907. device_power_up();
  908. local_irq_enable();
  909. return error;
  910. }
  911. /**
  912. * On resume, for storing the PBE list and the image,
  913. * we can only use memory pages that do not conflict with the pages
  914. * which had been used before suspend.
  915. *
  916. * We don't know which pages are usable until we allocate them.
  917. *
  918. * Allocated but unusable (ie eaten) memory pages are linked together
  919. * to create a list, so that we can free them easily
  920. *
  921. * We could have used a type other than (void *)
  922. * for this purpose, but ...
  923. */
  924. static void **eaten_memory = NULL;
  925. static inline void eat_page(void *page)
  926. {
  927. void **c;
  928. c = eaten_memory;
  929. eaten_memory = page;
  930. *eaten_memory = c;
  931. }
  932. static unsigned long get_usable_page(unsigned gfp_mask)
  933. {
  934. unsigned long m;
  935. m = get_zeroed_page(gfp_mask);
  936. while (!PageNosaveFree(virt_to_page(m))) {
  937. eat_page((void *)m);
  938. m = get_zeroed_page(gfp_mask);
  939. if (!m)
  940. break;
  941. }
  942. return m;
  943. }
  944. static void free_eaten_memory(void)
  945. {
  946. unsigned long m;
  947. void **c;
  948. int i = 0;
  949. c = eaten_memory;
  950. while (c) {
  951. m = (unsigned long)c;
  952. c = *c;
  953. free_page(m);
  954. i++;
  955. }
  956. eaten_memory = NULL;
  957. pr_debug("swsusp: %d unused pages freed\n", i);
  958. }
  959. /**
  960. * check_pagedir - We ensure here that pages that the PBEs point to
  961. * won't collide with pages where we're going to restore from the loaded
  962. * pages later
  963. */
  964. static int check_pagedir(struct pbe *pblist)
  965. {
  966. struct pbe *p;
  967. /* This is necessary, so that we can free allocated pages
  968. * in case of failure
  969. */
  970. for_each_pbe (p, pblist)
  971. p->address = 0UL;
  972. for_each_pbe (p, pblist) {
  973. p->address = get_usable_page(GFP_ATOMIC);
  974. if (!p->address)
  975. return -ENOMEM;
  976. }
  977. return 0;
  978. }
  979. /**
  980. * swsusp_pagedir_relocate - It is possible, that some memory pages
  981. * occupied by the list of PBEs collide with pages where we're going to
  982. * restore from the loaded pages later. We relocate them here.
  983. */
  984. static struct pbe * swsusp_pagedir_relocate(struct pbe *pblist)
  985. {
  986. struct zone *zone;
  987. unsigned long zone_pfn;
  988. struct pbe *pbpage, *tail, *p;
  989. void *m;
  990. int rel = 0, error = 0;
  991. if (!pblist) /* a sanity check */
  992. return NULL;
  993. pr_debug("swsusp: Relocating pagedir (%lu pages to check)\n",
  994. swsusp_info.pagedir_pages);
  995. /* Set page flags */
  996. for_each_zone (zone) {
  997. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  998. SetPageNosaveFree(pfn_to_page(zone_pfn +
  999. zone->zone_start_pfn));
  1000. }
  1001. /* Clear orig addresses */
  1002. for_each_pbe (p, pblist)
  1003. ClearPageNosaveFree(virt_to_page(p->orig_address));
  1004. tail = pblist + PB_PAGE_SKIP;
  1005. /* Relocate colliding pages */
  1006. for_each_pb_page (pbpage, pblist) {
  1007. if (!PageNosaveFree(virt_to_page((unsigned long)pbpage))) {
  1008. m = (void *)get_usable_page(GFP_ATOMIC | __GFP_COLD);
  1009. if (!m) {
  1010. error = -ENOMEM;
  1011. break;
  1012. }
  1013. memcpy(m, (void *)pbpage, PAGE_SIZE);
  1014. if (pbpage == pblist)
  1015. pblist = (struct pbe *)m;
  1016. else
  1017. tail->next = (struct pbe *)m;
  1018. eat_page((void *)pbpage);
  1019. pbpage = (struct pbe *)m;
  1020. /* We have to link the PBEs again */
  1021. for (p = pbpage; p < pbpage + PB_PAGE_SKIP; p++)
  1022. if (p->next) /* needed to save the end */
  1023. p->next = p + 1;
  1024. rel++;
  1025. }
  1026. tail = pbpage + PB_PAGE_SKIP;
  1027. }
  1028. if (error) {
  1029. printk("\nswsusp: Out of memory\n\n");
  1030. free_pagedir(pblist);
  1031. free_eaten_memory();
  1032. pblist = NULL;
  1033. /* Is this even worth handling? It should never ever happen, and we
  1034. have just lost user's state, anyway... */
  1035. } else
  1036. printk("swsusp: Relocated %d pages\n", rel);
  1037. return pblist;
  1038. }
  1039. /*
  1040. * Using bio to read from swap.
  1041. * This code requires a bit more work than just using buffer heads
  1042. * but, it is the recommended way for 2.5/2.6.
  1043. * The following are to signal the beginning and end of I/O. Bios
  1044. * finish asynchronously, while we want them to happen synchronously.
  1045. * A simple atomic_t, and a wait loop take care of this problem.
  1046. */
  1047. static atomic_t io_done = ATOMIC_INIT(0);
  1048. static int end_io(struct bio * bio, unsigned int num, int err)
  1049. {
  1050. if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  1051. panic("I/O error reading memory image");
  1052. atomic_set(&io_done, 0);
  1053. return 0;
  1054. }
  1055. static struct block_device * resume_bdev;
  1056. /**
  1057. * submit - submit BIO request.
  1058. * @rw: READ or WRITE.
  1059. * @off physical offset of page.
  1060. * @page: page we're reading or writing.
  1061. *
  1062. * Straight from the textbook - allocate and initialize the bio.
  1063. * If we're writing, make sure the page is marked as dirty.
  1064. * Then submit it and wait.
  1065. */
  1066. static int submit(int rw, pgoff_t page_off, void * page)
  1067. {
  1068. int error = 0;
  1069. struct bio * bio;
  1070. bio = bio_alloc(GFP_ATOMIC, 1);
  1071. if (!bio)
  1072. return -ENOMEM;
  1073. bio->bi_sector = page_off * (PAGE_SIZE >> 9);
  1074. bio_get(bio);
  1075. bio->bi_bdev = resume_bdev;
  1076. bio->bi_end_io = end_io;
  1077. if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
  1078. printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
  1079. error = -EFAULT;
  1080. goto Done;
  1081. }
  1082. if (rw == WRITE)
  1083. bio_set_pages_dirty(bio);
  1084. atomic_set(&io_done, 1);
  1085. submit_bio(rw | (1 << BIO_RW_SYNC), bio);
  1086. while (atomic_read(&io_done))
  1087. yield();
  1088. Done:
  1089. bio_put(bio);
  1090. return error;
  1091. }
  1092. static int bio_read_page(pgoff_t page_off, void * page)
  1093. {
  1094. return submit(READ, page_off, page);
  1095. }
  1096. static int bio_write_page(pgoff_t page_off, void * page)
  1097. {
  1098. return submit(WRITE, page_off, page);
  1099. }
  1100. /*
  1101. * Sanity check if this image makes sense with this kernel/swap context
  1102. * I really don't think that it's foolproof but more than nothing..
  1103. */
  1104. static const char * sanity_check(void)
  1105. {
  1106. dump_info();
  1107. if (swsusp_info.version_code != LINUX_VERSION_CODE)
  1108. return "kernel version";
  1109. if (swsusp_info.num_physpages != num_physpages)
  1110. return "memory size";
  1111. if (strcmp(swsusp_info.uts.sysname,system_utsname.sysname))
  1112. return "system type";
  1113. if (strcmp(swsusp_info.uts.release,system_utsname.release))
  1114. return "kernel release";
  1115. if (strcmp(swsusp_info.uts.version,system_utsname.version))
  1116. return "version";
  1117. if (strcmp(swsusp_info.uts.machine,system_utsname.machine))
  1118. return "machine";
  1119. #if 0
  1120. /* We can't use number of online CPUs when we use hotplug to remove them ;-))) */
  1121. if (swsusp_info.cpus != num_possible_cpus())
  1122. return "number of cpus";
  1123. #endif
  1124. return NULL;
  1125. }
  1126. static int check_header(void)
  1127. {
  1128. const char * reason = NULL;
  1129. int error;
  1130. if ((error = bio_read_page(swp_offset(swsusp_header.swsusp_info), &swsusp_info)))
  1131. return error;
  1132. /* Is this same machine? */
  1133. if ((reason = sanity_check())) {
  1134. printk(KERN_ERR "swsusp: Resume mismatch: %s\n",reason);
  1135. return -EPERM;
  1136. }
  1137. nr_copy_pages = swsusp_info.image_pages;
  1138. return error;
  1139. }
  1140. static int check_sig(void)
  1141. {
  1142. int error;
  1143. memset(&swsusp_header, 0, sizeof(swsusp_header));
  1144. if ((error = bio_read_page(0, &swsusp_header)))
  1145. return error;
  1146. if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
  1147. memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
  1148. memcpy(key_iv, swsusp_header.key_iv, MAXKEY+MAXIV);
  1149. memset(swsusp_header.key_iv, 0, MAXKEY+MAXIV);
  1150. /*
  1151. * Reset swap signature now.
  1152. */
  1153. error = bio_write_page(0, &swsusp_header);
  1154. } else {
  1155. return -EINVAL;
  1156. }
  1157. if (!error)
  1158. pr_debug("swsusp: Signature found, resuming\n");
  1159. return error;
  1160. }
  1161. /**
  1162. * data_read - Read image pages from swap.
  1163. *
  1164. * You do not need to check for overlaps, check_pagedir()
  1165. * already did that.
  1166. */
  1167. static int data_read(struct pbe *pblist)
  1168. {
  1169. struct pbe * p;
  1170. int error = 0;
  1171. int i = 0;
  1172. int mod = swsusp_info.image_pages / 100;
  1173. void *tfm;
  1174. if ((error = crypto_init(0, &tfm)))
  1175. return error;
  1176. if (!mod)
  1177. mod = 1;
  1178. printk("swsusp: Reading image data (%lu pages): ",
  1179. swsusp_info.image_pages);
  1180. for_each_pbe (p, pblist) {
  1181. if (!(i % mod))
  1182. printk("\b\b\b\b%3d%%", i / mod);
  1183. if ((error = crypto_read(p, tfm))) {
  1184. crypto_exit(tfm);
  1185. return error;
  1186. }
  1187. i++;
  1188. }
  1189. printk("\b\b\b\bdone\n");
  1190. crypto_exit(tfm);
  1191. return error;
  1192. }
  1193. /**
  1194. * read_pagedir - Read page backup list pages from swap
  1195. */
  1196. static int read_pagedir(struct pbe *pblist)
  1197. {
  1198. struct pbe *pbpage, *p;
  1199. unsigned i = 0;
  1200. int error;
  1201. if (!pblist)
  1202. return -EFAULT;
  1203. printk("swsusp: Reading pagedir (%lu pages)\n",
  1204. swsusp_info.pagedir_pages);
  1205. for_each_pb_page (pbpage, pblist) {
  1206. unsigned long offset = swp_offset(swsusp_info.pagedir[i++]);
  1207. error = -EFAULT;
  1208. if (offset) {
  1209. p = (pbpage + PB_PAGE_SKIP)->next;
  1210. error = bio_read_page(offset, (void *)pbpage);
  1211. (pbpage + PB_PAGE_SKIP)->next = p;
  1212. }
  1213. if (error)
  1214. break;
  1215. }
  1216. if (error)
  1217. free_page((unsigned long)pblist);
  1218. BUG_ON(i != swsusp_info.pagedir_pages);
  1219. return error;
  1220. }
  1221. static int check_suspend_image(void)
  1222. {
  1223. int error = 0;
  1224. if ((error = check_sig()))
  1225. return error;
  1226. if ((error = check_header()))
  1227. return error;
  1228. return 0;
  1229. }
  1230. static int read_suspend_image(void)
  1231. {
  1232. int error = 0;
  1233. struct pbe *p;
  1234. if (!(p = alloc_pagedir(nr_copy_pages)))
  1235. return -ENOMEM;
  1236. if ((error = read_pagedir(p)))
  1237. return error;
  1238. create_pbe_list(p, nr_copy_pages);
  1239. if (!(pagedir_nosave = swsusp_pagedir_relocate(p)))
  1240. return -ENOMEM;
  1241. /* Allocate memory for the image and read the data from swap */
  1242. error = check_pagedir(pagedir_nosave);
  1243. free_eaten_memory();
  1244. if (!error)
  1245. error = data_read(pagedir_nosave);
  1246. if (error) { /* We fail cleanly */
  1247. for_each_pbe (p, pagedir_nosave)
  1248. if (p->address) {
  1249. free_page(p->address);
  1250. p->address = 0UL;
  1251. }
  1252. free_pagedir(pagedir_nosave);
  1253. }
  1254. return error;
  1255. }
  1256. /**
  1257. * swsusp_check - Check for saved image in swap
  1258. */
  1259. int swsusp_check(void)
  1260. {
  1261. int error;
  1262. resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
  1263. if (!IS_ERR(resume_bdev)) {
  1264. set_blocksize(resume_bdev, PAGE_SIZE);
  1265. error = check_suspend_image();
  1266. if (error)
  1267. blkdev_put(resume_bdev);
  1268. } else
  1269. error = PTR_ERR(resume_bdev);
  1270. if (!error)
  1271. pr_debug("swsusp: resume file found\n");
  1272. else
  1273. pr_debug("swsusp: Error %d check for resume file\n", error);
  1274. return error;
  1275. }
  1276. /**
  1277. * swsusp_read - Read saved image from swap.
  1278. */
  1279. int swsusp_read(void)
  1280. {
  1281. int error;
  1282. if (IS_ERR(resume_bdev)) {
  1283. pr_debug("swsusp: block device not initialised\n");
  1284. return PTR_ERR(resume_bdev);
  1285. }
  1286. error = read_suspend_image();
  1287. blkdev_put(resume_bdev);
  1288. memset(key_iv, 0, MAXKEY+MAXIV);
  1289. if (!error)
  1290. pr_debug("swsusp: Reading resume file was successful\n");
  1291. else
  1292. pr_debug("swsusp: Error %d resuming\n", error);
  1293. return error;
  1294. }
  1295. /**
  1296. * swsusp_close - close swap device.
  1297. */
  1298. void swsusp_close(void)
  1299. {
  1300. if (IS_ERR(resume_bdev)) {
  1301. pr_debug("swsusp: block device not initialised\n");
  1302. return;
  1303. }
  1304. blkdev_put(resume_bdev);
  1305. }