swsusp.c 35 KB

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