swsusp.c 33 KB

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