tuxonice_bio_signature.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * kernel/power/tuxonice_bio_signature.c
  3. *
  4. * Copyright (C) 2004-2014 Nigel Cunningham (nigel at tuxonice net)
  5. *
  6. * Distributed under GPLv2.
  7. *
  8. */
  9. #include <linux/fs_uuid.h>
  10. #include "tuxonice.h"
  11. #include "tuxonice_sysfs.h"
  12. #include "tuxonice_modules.h"
  13. #include "tuxonice_prepare_image.h"
  14. #include "tuxonice_bio.h"
  15. #include "tuxonice_ui.h"
  16. #include "tuxonice_alloc.h"
  17. #include "tuxonice_io.h"
  18. #include "tuxonice_builtin.h"
  19. #include "tuxonice_bio_internal.h"
  20. struct sig_data *toi_sig_data;
  21. /* Struct of swap header pages */
  22. struct old_sig_data {
  23. dev_t device;
  24. unsigned long sector;
  25. int resume_attempted;
  26. int orig_sig_type;
  27. };
  28. union diskpage {
  29. union swap_header swh; /* swh.magic is the only member used */
  30. struct sig_data sig_data;
  31. struct old_sig_data old_sig_data;
  32. };
  33. union p_diskpage {
  34. union diskpage *pointer;
  35. char *ptr;
  36. unsigned long address;
  37. };
  38. char *toi_cur_sig_page;
  39. char *toi_orig_sig_page;
  40. int have_image;
  41. int have_old_image;
  42. int get_signature_page(void)
  43. {
  44. if (!toi_cur_sig_page) {
  45. toi_message(TOI_IO, TOI_VERBOSE, 0, "Allocating current signature page.");
  46. toi_cur_sig_page = (char *)toi_get_zeroed_page(38, TOI_ATOMIC_GFP);
  47. if (!toi_cur_sig_page) {
  48. pr_err("Failed to allocate memory for the current image signature.\n");
  49. return -ENOMEM;
  50. }
  51. toi_sig_data = (struct sig_data *)toi_cur_sig_page;
  52. }
  53. toi_message(TOI_IO, TOI_VERBOSE, 0, "Reading signature from dev %x, sector %lu.",
  54. (unsigned int) resume_block_device->bd_dev, resume_firstblock);
  55. return toi_bio_ops.bdev_page_io(READ, resume_block_device,
  56. resume_firstblock, virt_to_page(toi_cur_sig_page));
  57. }
  58. void forget_signature_page(void)
  59. {
  60. if (toi_cur_sig_page) {
  61. toi_sig_data = NULL;
  62. toi_message(TOI_IO, TOI_VERBOSE, 0, "Freeing toi_cur_sig_page (%p).",
  63. toi_cur_sig_page);
  64. toi_free_page(38, (unsigned long)toi_cur_sig_page);
  65. toi_cur_sig_page = NULL;
  66. }
  67. if (toi_orig_sig_page) {
  68. toi_message(TOI_IO, TOI_VERBOSE, 0, "Freeing toi_orig_sig_page (%p).",
  69. toi_orig_sig_page);
  70. toi_free_page(38, (unsigned long)toi_orig_sig_page);
  71. toi_orig_sig_page = NULL;
  72. }
  73. }
  74. /*
  75. * We need to ensure we use the signature page that's currently on disk,
  76. * so as to not remove the image header. Post-atomic-restore, the orig sig
  77. * page will be empty, so we can use that as our method of knowing that we
  78. * need to load the on-disk signature and not use the non-image sig in
  79. * memory. (We're going to powerdown after writing the change, so it's safe.
  80. */
  81. int toi_bio_mark_resume_attempted(int flag)
  82. {
  83. toi_message(TOI_IO, TOI_VERBOSE, 0, "Make resume attempted = %d.", flag);
  84. if (!toi_orig_sig_page) {
  85. forget_signature_page();
  86. get_signature_page();
  87. }
  88. toi_sig_data->resumed_before = flag;
  89. return toi_bio_ops.bdev_page_io(WRITE, resume_block_device,
  90. resume_firstblock, virt_to_page(toi_cur_sig_page));
  91. }
  92. int toi_bio_mark_have_image(void)
  93. {
  94. int result = 0;
  95. char buf[32];
  96. struct fs_info *fs_info;
  97. toi_message(TOI_IO, TOI_VERBOSE, 0, "Recording that an image exists.");
  98. memcpy(toi_sig_data->sig, tuxonice_signature, sizeof(tuxonice_signature));
  99. toi_sig_data->have_image = 1;
  100. toi_sig_data->resumed_before = 0;
  101. toi_sig_data->header_dev_t = get_header_dev_t();
  102. toi_sig_data->have_uuid = 0;
  103. #if 1 /* FIXME: check this uuid is mismatch, such that it will failed on toi_bio_read_header_init() !!! */
  104. fs_info = fs_info_from_block_dev(get_header_bdev());
  105. if (fs_info && !IS_ERR(fs_info)) {
  106. memcpy(toi_sig_data->header_uuid, &fs_info->uuid, 16);
  107. free_fs_info(fs_info);
  108. } else
  109. result = (int)PTR_ERR(fs_info);
  110. #endif
  111. if (!result) {
  112. toi_message(TOI_IO, TOI_VERBOSE, 0, "Got uuid for dev_t %s.",
  113. format_dev_t(buf, get_header_dev_t()));
  114. toi_sig_data->have_uuid = 1;
  115. } else
  116. toi_message(TOI_IO, TOI_VERBOSE, 0, "Could not get uuid for dev_t %s.",
  117. format_dev_t(buf, get_header_dev_t()));
  118. toi_sig_data->first_header_block = get_headerblock();
  119. have_image = 1;
  120. toi_message(TOI_IO, TOI_VERBOSE, 0, "header dev_t is %x. First block is %lu.",
  121. (unsigned int) toi_sig_data->header_dev_t, toi_sig_data->first_header_block);
  122. memcpy(toi_sig_data->sig2, tuxonice_signature, sizeof(tuxonice_signature));
  123. toi_sig_data->header_version = TOI_HEADER_VERSION;
  124. return toi_bio_ops.bdev_page_io(WRITE, resume_block_device,
  125. resume_firstblock, virt_to_page(toi_cur_sig_page));
  126. }
  127. int remove_old_signature(void)
  128. {
  129. union p_diskpage swap_header_page = (union p_diskpage)toi_cur_sig_page;
  130. char *orig_sig;
  131. char *header_start = (char *)toi_get_zeroed_page(38, TOI_ATOMIC_GFP);
  132. int result;
  133. struct block_device *header_bdev;
  134. struct old_sig_data *old_sig_data = &swap_header_page.pointer->old_sig_data;
  135. header_bdev = toi_open_bdev(NULL, old_sig_data->device, 1);
  136. result = toi_bio_ops.bdev_page_io(READ, header_bdev,
  137. old_sig_data->sector, virt_to_page(header_start));
  138. if (result)
  139. goto out;
  140. /*
  141. * TODO: Get the original contents of the first bytes of the swap
  142. * header page.
  143. */
  144. if (!old_sig_data->orig_sig_type)
  145. orig_sig = "SWAP-SPACE";
  146. else
  147. orig_sig = "SWAPSPACE2";
  148. memcpy(swap_header_page.pointer->swh.magic.magic, orig_sig, 10);
  149. memcpy(swap_header_page.ptr, header_start, 10);
  150. result = toi_bio_ops.bdev_page_io(WRITE, resume_block_device,
  151. resume_firstblock, virt_to_page(swap_header_page.ptr));
  152. out:
  153. toi_close_bdev(header_bdev);
  154. have_old_image = 0;
  155. toi_free_page(38, (unsigned long)header_start);
  156. return result;
  157. }
  158. /*
  159. * toi_bio_restore_original_signature - restore the original signature
  160. *
  161. * At boot time (aborting pre atomic-restore), toi_orig_sig_page gets used.
  162. * It will have the original signature page contents, stored in the image
  163. * header. Post atomic-restore, we use :toi_cur_sig_page, which will contain
  164. * the contents that were loaded when we started the cycle.
  165. */
  166. int toi_bio_restore_original_signature(void)
  167. {
  168. char *use = toi_orig_sig_page ? toi_orig_sig_page : toi_cur_sig_page;
  169. if (have_old_image)
  170. return remove_old_signature();
  171. if (!use) {
  172. pr_warn("toi_bio_restore_original_signature: No signature " "page loaded.\n");
  173. return 0;
  174. }
  175. toi_message(TOI_IO, TOI_VERBOSE, 0, "Recording that no image exists.");
  176. have_image = 0;
  177. toi_sig_data->have_image = 0;
  178. return toi_bio_ops.bdev_page_io(WRITE, resume_block_device,
  179. resume_firstblock, virt_to_page(use));
  180. }
  181. /*
  182. * check_for_signature - See whether we have an image.
  183. *
  184. * Returns 0 if no image, 1 if there is one, -1 if indeterminate.
  185. */
  186. int toi_check_for_signature(void)
  187. {
  188. union p_diskpage swap_header_page;
  189. int type;
  190. static const char * const normal_sigs[] = { "SWAP-SPACE", "SWAPSPACE2" };
  191. static const char * const swsusp_sigs[] = { "S1SUSP", "S2SUSP", "S1SUSPEND" };
  192. char *swap_header;
  193. if (!toi_cur_sig_page) {
  194. int result = get_signature_page();
  195. if (result)
  196. return result;
  197. }
  198. /*
  199. * Start by looking for the binary header.
  200. */
  201. if (!memcmp(tuxonice_signature, toi_cur_sig_page, sizeof(tuxonice_signature))) {
  202. have_image = toi_sig_data->have_image;
  203. toi_message(TOI_IO, TOI_VERBOSE, 0, "Have binary signature. Have image is %d.",
  204. have_image);
  205. if (have_image)
  206. toi_message(TOI_IO, TOI_VERBOSE, 0, "header dev_t is %x. First block is %lu.",
  207. (unsigned int) toi_sig_data->header_dev_t, toi_sig_data->first_header_block);
  208. return toi_sig_data->have_image;
  209. }
  210. /*
  211. * Failing that, try old file allocator headers.
  212. */
  213. if (!memcmp(HaveImage, toi_cur_sig_page, strlen(HaveImage))) {
  214. have_image = 1;
  215. return 1;
  216. }
  217. have_image = 0;
  218. if (!memcmp(NoImage, toi_cur_sig_page, strlen(NoImage)))
  219. return 0;
  220. /*
  221. * Nope? How about swap?
  222. */
  223. swap_header_page = (union p_diskpage)toi_cur_sig_page;
  224. swap_header = swap_header_page.pointer->swh.magic.magic;
  225. /* Normal swapspace? */
  226. for (type = 0; type < 2; type++)
  227. if (!memcmp(normal_sigs[type], swap_header, strlen(normal_sigs[type])))
  228. return 0;
  229. /* Swsusp or uswsusp? */
  230. for (type = 0; type < 3; type++)
  231. if (!memcmp(swsusp_sigs[type], swap_header, strlen(swsusp_sigs[type])))
  232. return 2;
  233. /* Old TuxOnIce version? */
  234. if (!memcmp(tuxonice_signature, swap_header, sizeof(tuxonice_signature) - 1)) {
  235. toi_message(TOI_IO, TOI_VERBOSE, 0, "Found old TuxOnIce " "signature.");
  236. have_old_image = 1;
  237. return 3;
  238. }
  239. return -1;
  240. }
  241. /*
  242. * Image_exists
  243. *
  244. * Returns -1 if don't know, otherwise 0 (no) or 1 (yes).
  245. */
  246. int toi_bio_image_exists(int quiet)
  247. {
  248. int result;
  249. char *msg = NULL;
  250. toi_message(TOI_IO, TOI_VERBOSE, 0, "toi_bio_image_exists.");
  251. if (!resume_dev_t) {
  252. if (!quiet)
  253. pr_warn("Not even trying to read header because resume_dev_t is not set.");
  254. return -1;
  255. }
  256. if (open_resume_dev_t(0, quiet))
  257. return -1;
  258. result = toi_check_for_signature();
  259. clear_toi_state(TOI_RESUMED_BEFORE);
  260. if (toi_sig_data->resumed_before)
  261. set_toi_state(TOI_RESUMED_BEFORE);
  262. if (quiet || result == -ENOMEM)
  263. return result;
  264. if (result == -1)
  265. msg = "TuxOnIce: Unable to find a signature. Could you have moved a swap file?\n";
  266. else if (!result)
  267. msg = "TuxOnIce: No image found.\n";
  268. else if (result == 1)
  269. msg = "TuxOnIce: Image found.\n";
  270. else if (result == 2)
  271. msg = "TuxOnIce: uswsusp or swsusp image found.\n";
  272. else if (result == 3)
  273. msg = "TuxOnIce: Old implementation's signature found.\n";
  274. pr_warn("%s", msg);
  275. return result;
  276. }
  277. int toi_bio_scan_for_image(int quiet)
  278. {
  279. struct block_device *bdev;
  280. int first = 1;
  281. if (!quiet)
  282. pr_debug("Scanning swap devices for TuxOnIce " "signature...\n");
  283. for (bdev = next_bdev_of_type(NULL, "swap"); bdev; bdev = next_bdev_of_type(bdev, "swap")) {
  284. int result;
  285. char name[255] = "";
  286. sprintf(name, "%u:%u", MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev));
  287. if (!quiet)
  288. pr_debug("- Trying %s.\n", name);
  289. resume_block_device = bdev;
  290. resume_dev_t = bdev->bd_dev;
  291. result = toi_check_for_signature();
  292. resume_block_device = NULL;
  293. resume_dev_t = MKDEV(0, 0);
  294. if (result == 1) {
  295. /* Got one! */
  296. strcpy(resume_file, name);
  297. next_bdev_of_type(bdev, NULL);
  298. if (!quiet)
  299. pr_debug(" ==> Image found on %s.\n", resume_file);
  300. return 1;
  301. }
  302. if (first) {
  303. strcpy(resume_file, name);
  304. first = 0;
  305. }
  306. forget_signature_page();
  307. }
  308. if (!quiet)
  309. pr_debug("TuxOnIce scan: No image found.\n");
  310. return 0;
  311. }
  312. int toi_bio_get_header_version(void)
  313. {
  314. return (memcmp(toi_sig_data->sig2, tuxonice_signature,
  315. sizeof(tuxonice_signature))) ? 0 : toi_sig_data->header_version;
  316. }