swap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * linux/kernel/power/swap.c
  3. *
  4. * This file provides functions for reading the suspend image from
  5. * and writing it to a swap partition.
  6. *
  7. * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
  8. * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
  9. *
  10. * This file is released under the GPLv2.
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/smp_lock.h>
  15. #include <linux/file.h>
  16. #include <linux/utsname.h>
  17. #include <linux/version.h>
  18. #include <linux/delay.h>
  19. #include <linux/bitops.h>
  20. #include <linux/genhd.h>
  21. #include <linux/device.h>
  22. #include <linux/buffer_head.h>
  23. #include <linux/bio.h>
  24. #include <linux/swap.h>
  25. #include <linux/swapops.h>
  26. #include <linux/pm.h>
  27. #include "power.h"
  28. extern char resume_file[];
  29. #define SWSUSP_SIG "S1SUSPEND"
  30. static struct swsusp_header {
  31. char reserved[PAGE_SIZE - 20 - sizeof(swp_entry_t)];
  32. swp_entry_t image;
  33. char orig_sig[10];
  34. char sig[10];
  35. } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
  36. /*
  37. * Saving part...
  38. */
  39. static unsigned short root_swap = 0xffff;
  40. static int mark_swapfiles(swp_entry_t start)
  41. {
  42. int error;
  43. rw_swap_page_sync(READ,
  44. swp_entry(root_swap, 0),
  45. virt_to_page((unsigned long)&swsusp_header));
  46. if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
  47. !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
  48. memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
  49. memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
  50. swsusp_header.image = start;
  51. error = rw_swap_page_sync(WRITE,
  52. swp_entry(root_swap, 0),
  53. virt_to_page((unsigned long)
  54. &swsusp_header));
  55. } else {
  56. pr_debug("swsusp: Partition is not swap space.\n");
  57. error = -ENODEV;
  58. }
  59. return error;
  60. }
  61. /**
  62. * swsusp_swap_check - check if the resume device is a swap device
  63. * and get its index (if so)
  64. */
  65. static int swsusp_swap_check(void) /* This is called before saving image */
  66. {
  67. int res = swap_type_of(swsusp_resume_device);
  68. if (res >= 0) {
  69. root_swap = res;
  70. return 0;
  71. }
  72. return res;
  73. }
  74. /**
  75. * write_page - Write one page to given swap location.
  76. * @buf: Address we're writing.
  77. * @offset: Offset of the swap page we're writing to.
  78. */
  79. static int write_page(void *buf, unsigned long offset)
  80. {
  81. swp_entry_t entry;
  82. int error = -ENOSPC;
  83. if (offset) {
  84. entry = swp_entry(root_swap, offset);
  85. error = rw_swap_page_sync(WRITE, entry, virt_to_page(buf));
  86. }
  87. return error;
  88. }
  89. /*
  90. * The swap map is a data structure used for keeping track of each page
  91. * written to a swap partition. It consists of many swap_map_page
  92. * structures that contain each an array of MAP_PAGE_SIZE swap entries.
  93. * These structures are stored on the swap and linked together with the
  94. * help of the .next_swap member.
  95. *
  96. * The swap map is created during suspend. The swap map pages are
  97. * allocated and populated one at a time, so we only need one memory
  98. * page to set up the entire structure.
  99. *
  100. * During resume we also only need to use one swap_map_page structure
  101. * at a time.
  102. */
  103. #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(long) - 1)
  104. struct swap_map_page {
  105. unsigned long entries[MAP_PAGE_ENTRIES];
  106. unsigned long next_swap;
  107. };
  108. /**
  109. * The swap_map_handle structure is used for handling swap in
  110. * a file-alike way
  111. */
  112. struct swap_map_handle {
  113. struct swap_map_page *cur;
  114. unsigned long cur_swap;
  115. struct bitmap_page *bitmap;
  116. unsigned int k;
  117. };
  118. static void release_swap_writer(struct swap_map_handle *handle)
  119. {
  120. if (handle->cur)
  121. free_page((unsigned long)handle->cur);
  122. handle->cur = NULL;
  123. if (handle->bitmap)
  124. free_bitmap(handle->bitmap);
  125. handle->bitmap = NULL;
  126. }
  127. static int get_swap_writer(struct swap_map_handle *handle)
  128. {
  129. handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
  130. if (!handle->cur)
  131. return -ENOMEM;
  132. handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0));
  133. if (!handle->bitmap) {
  134. release_swap_writer(handle);
  135. return -ENOMEM;
  136. }
  137. handle->cur_swap = alloc_swap_page(root_swap, handle->bitmap);
  138. if (!handle->cur_swap) {
  139. release_swap_writer(handle);
  140. return -ENOSPC;
  141. }
  142. handle->k = 0;
  143. return 0;
  144. }
  145. static int swap_write_page(struct swap_map_handle *handle, void *buf)
  146. {
  147. int error;
  148. unsigned long offset;
  149. if (!handle->cur)
  150. return -EINVAL;
  151. offset = alloc_swap_page(root_swap, handle->bitmap);
  152. error = write_page(buf, offset);
  153. if (error)
  154. return error;
  155. handle->cur->entries[handle->k++] = offset;
  156. if (handle->k >= MAP_PAGE_ENTRIES) {
  157. offset = alloc_swap_page(root_swap, handle->bitmap);
  158. if (!offset)
  159. return -ENOSPC;
  160. handle->cur->next_swap = offset;
  161. error = write_page(handle->cur, handle->cur_swap);
  162. if (error)
  163. return error;
  164. memset(handle->cur, 0, PAGE_SIZE);
  165. handle->cur_swap = offset;
  166. handle->k = 0;
  167. }
  168. return 0;
  169. }
  170. static int flush_swap_writer(struct swap_map_handle *handle)
  171. {
  172. if (handle->cur && handle->cur_swap)
  173. return write_page(handle->cur, handle->cur_swap);
  174. else
  175. return -EINVAL;
  176. }
  177. /**
  178. * save_image - save the suspend image data
  179. */
  180. static int save_image(struct swap_map_handle *handle,
  181. struct snapshot_handle *snapshot,
  182. unsigned int nr_pages)
  183. {
  184. unsigned int m;
  185. int ret;
  186. int error = 0;
  187. printk("Saving image data pages (%u pages) ... ", nr_pages);
  188. m = nr_pages / 100;
  189. if (!m)
  190. m = 1;
  191. nr_pages = 0;
  192. do {
  193. ret = snapshot_read_next(snapshot, PAGE_SIZE);
  194. if (ret > 0) {
  195. error = swap_write_page(handle, data_of(*snapshot));
  196. if (error)
  197. break;
  198. if (!(nr_pages % m))
  199. printk("\b\b\b\b%3d%%", nr_pages / m);
  200. nr_pages++;
  201. }
  202. } while (ret > 0);
  203. if (!error)
  204. printk("\b\b\b\bdone\n");
  205. return error;
  206. }
  207. /**
  208. * enough_swap - Make sure we have enough swap to save the image.
  209. *
  210. * Returns TRUE or FALSE after checking the total amount of swap
  211. * space avaiable from the resume partition.
  212. */
  213. static int enough_swap(unsigned int nr_pages)
  214. {
  215. unsigned int free_swap = count_swap_pages(root_swap, 1);
  216. pr_debug("swsusp: free swap pages: %u\n", free_swap);
  217. return free_swap > (nr_pages + PAGES_FOR_IO +
  218. (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
  219. }
  220. /**
  221. * swsusp_write - Write entire image and metadata.
  222. *
  223. * It is important _NOT_ to umount filesystems at this point. We want
  224. * them synced (in case something goes wrong) but we DO not want to mark
  225. * filesystem clean: it is not. (And it does not matter, if we resume
  226. * correctly, we'll mark system clean, anyway.)
  227. */
  228. int swsusp_write(void)
  229. {
  230. struct swap_map_handle handle;
  231. struct snapshot_handle snapshot;
  232. struct swsusp_info *header;
  233. unsigned long start;
  234. int error;
  235. if ((error = swsusp_swap_check())) {
  236. printk(KERN_ERR "swsusp: Cannot find swap device, try swapon -a.\n");
  237. return error;
  238. }
  239. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  240. error = snapshot_read_next(&snapshot, PAGE_SIZE);
  241. if (error < PAGE_SIZE)
  242. return error < 0 ? error : -EFAULT;
  243. header = (struct swsusp_info *)data_of(snapshot);
  244. if (!enough_swap(header->pages)) {
  245. printk(KERN_ERR "swsusp: Not enough free swap\n");
  246. return -ENOSPC;
  247. }
  248. error = get_swap_writer(&handle);
  249. if (!error) {
  250. start = handle.cur_swap;
  251. error = swap_write_page(&handle, header);
  252. }
  253. if (!error)
  254. error = save_image(&handle, &snapshot, header->pages - 1);
  255. if (!error) {
  256. flush_swap_writer(&handle);
  257. printk("S");
  258. error = mark_swapfiles(swp_entry(root_swap, start));
  259. printk("|\n");
  260. }
  261. if (error)
  262. free_all_swap_pages(root_swap, handle.bitmap);
  263. release_swap_writer(&handle);
  264. return error;
  265. }
  266. /*
  267. * Using bio to read from swap.
  268. * This code requires a bit more work than just using buffer heads
  269. * but, it is the recommended way for 2.5/2.6.
  270. * The following are to signal the beginning and end of I/O. Bios
  271. * finish asynchronously, while we want them to happen synchronously.
  272. * A simple atomic_t, and a wait loop take care of this problem.
  273. */
  274. static atomic_t io_done = ATOMIC_INIT(0);
  275. static int end_io(struct bio *bio, unsigned int num, int err)
  276. {
  277. if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  278. panic("I/O error reading memory image");
  279. atomic_set(&io_done, 0);
  280. return 0;
  281. }
  282. static struct block_device *resume_bdev;
  283. /**
  284. * submit - submit BIO request.
  285. * @rw: READ or WRITE.
  286. * @off physical offset of page.
  287. * @page: page we're reading or writing.
  288. *
  289. * Straight from the textbook - allocate and initialize the bio.
  290. * If we're writing, make sure the page is marked as dirty.
  291. * Then submit it and wait.
  292. */
  293. static int submit(int rw, pgoff_t page_off, void *page)
  294. {
  295. int error = 0;
  296. struct bio *bio;
  297. bio = bio_alloc(GFP_ATOMIC, 1);
  298. if (!bio)
  299. return -ENOMEM;
  300. bio->bi_sector = page_off * (PAGE_SIZE >> 9);
  301. bio->bi_bdev = resume_bdev;
  302. bio->bi_end_io = end_io;
  303. if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
  304. printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
  305. error = -EFAULT;
  306. goto Done;
  307. }
  308. atomic_set(&io_done, 1);
  309. submit_bio(rw | (1 << BIO_RW_SYNC), bio);
  310. while (atomic_read(&io_done))
  311. yield();
  312. if (rw == READ)
  313. bio_set_pages_dirty(bio);
  314. Done:
  315. bio_put(bio);
  316. return error;
  317. }
  318. static int bio_read_page(pgoff_t page_off, void *page)
  319. {
  320. return submit(READ, page_off, page);
  321. }
  322. static int bio_write_page(pgoff_t page_off, void *page)
  323. {
  324. return submit(WRITE, page_off, page);
  325. }
  326. /**
  327. * The following functions allow us to read data using a swap map
  328. * in a file-alike way
  329. */
  330. static void release_swap_reader(struct swap_map_handle *handle)
  331. {
  332. if (handle->cur)
  333. free_page((unsigned long)handle->cur);
  334. handle->cur = NULL;
  335. }
  336. static int get_swap_reader(struct swap_map_handle *handle,
  337. swp_entry_t start)
  338. {
  339. int error;
  340. if (!swp_offset(start))
  341. return -EINVAL;
  342. handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
  343. if (!handle->cur)
  344. return -ENOMEM;
  345. error = bio_read_page(swp_offset(start), handle->cur);
  346. if (error) {
  347. release_swap_reader(handle);
  348. return error;
  349. }
  350. handle->k = 0;
  351. return 0;
  352. }
  353. static int swap_read_page(struct swap_map_handle *handle, void *buf)
  354. {
  355. unsigned long offset;
  356. int error;
  357. if (!handle->cur)
  358. return -EINVAL;
  359. offset = handle->cur->entries[handle->k];
  360. if (!offset)
  361. return -EFAULT;
  362. error = bio_read_page(offset, buf);
  363. if (error)
  364. return error;
  365. if (++handle->k >= MAP_PAGE_ENTRIES) {
  366. handle->k = 0;
  367. offset = handle->cur->next_swap;
  368. if (!offset)
  369. release_swap_reader(handle);
  370. else
  371. error = bio_read_page(offset, handle->cur);
  372. }
  373. return error;
  374. }
  375. /**
  376. * load_image - load the image using the swap map handle
  377. * @handle and the snapshot handle @snapshot
  378. * (assume there are @nr_pages pages to load)
  379. */
  380. static int load_image(struct swap_map_handle *handle,
  381. struct snapshot_handle *snapshot,
  382. unsigned int nr_pages)
  383. {
  384. unsigned int m;
  385. int ret;
  386. int error = 0;
  387. printk("Loading image data pages (%u pages) ... ", nr_pages);
  388. m = nr_pages / 100;
  389. if (!m)
  390. m = 1;
  391. nr_pages = 0;
  392. do {
  393. ret = snapshot_write_next(snapshot, PAGE_SIZE);
  394. if (ret > 0) {
  395. error = swap_read_page(handle, data_of(*snapshot));
  396. if (error)
  397. break;
  398. if (!(nr_pages % m))
  399. printk("\b\b\b\b%3d%%", nr_pages / m);
  400. nr_pages++;
  401. }
  402. } while (ret > 0);
  403. if (!error) {
  404. printk("\b\b\b\bdone\n");
  405. if (!snapshot_image_loaded(snapshot))
  406. error = -ENODATA;
  407. }
  408. return error;
  409. }
  410. int swsusp_read(void)
  411. {
  412. int error;
  413. struct swap_map_handle handle;
  414. struct snapshot_handle snapshot;
  415. struct swsusp_info *header;
  416. if (IS_ERR(resume_bdev)) {
  417. pr_debug("swsusp: block device not initialised\n");
  418. return PTR_ERR(resume_bdev);
  419. }
  420. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  421. error = snapshot_write_next(&snapshot, PAGE_SIZE);
  422. if (error < PAGE_SIZE)
  423. return error < 0 ? error : -EFAULT;
  424. header = (struct swsusp_info *)data_of(snapshot);
  425. error = get_swap_reader(&handle, swsusp_header.image);
  426. if (!error)
  427. error = swap_read_page(&handle, header);
  428. if (!error)
  429. error = load_image(&handle, &snapshot, header->pages - 1);
  430. release_swap_reader(&handle);
  431. blkdev_put(resume_bdev);
  432. if (!error)
  433. pr_debug("swsusp: Reading resume file was successful\n");
  434. else
  435. pr_debug("swsusp: Error %d resuming\n", error);
  436. return error;
  437. }
  438. /**
  439. * swsusp_check - Check for swsusp signature in the resume device
  440. */
  441. int swsusp_check(void)
  442. {
  443. int error;
  444. resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
  445. if (!IS_ERR(resume_bdev)) {
  446. set_blocksize(resume_bdev, PAGE_SIZE);
  447. memset(&swsusp_header, 0, sizeof(swsusp_header));
  448. if ((error = bio_read_page(0, &swsusp_header)))
  449. return error;
  450. if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
  451. memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
  452. /* Reset swap signature now */
  453. error = bio_write_page(0, &swsusp_header);
  454. } else {
  455. return -EINVAL;
  456. }
  457. if (error)
  458. blkdev_put(resume_bdev);
  459. else
  460. pr_debug("swsusp: Signature found, resuming\n");
  461. } else {
  462. error = PTR_ERR(resume_bdev);
  463. }
  464. if (error)
  465. pr_debug("swsusp: Error %d check for resume file\n", error);
  466. return error;
  467. }
  468. /**
  469. * swsusp_close - close swap device.
  470. */
  471. void swsusp_close(void)
  472. {
  473. if (IS_ERR(resume_bdev)) {
  474. pr_debug("swsusp: block device not initialised\n");
  475. return;
  476. }
  477. blkdev_put(resume_bdev);
  478. }