swap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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@ucw.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/file.h>
  15. #include <linux/delay.h>
  16. #include <linux/bitops.h>
  17. #include <linux/genhd.h>
  18. #include <linux/device.h>
  19. #include <linux/buffer_head.h>
  20. #include <linux/bio.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/swap.h>
  23. #include <linux/swapops.h>
  24. #include <linux/pm.h>
  25. #include <linux/slab.h>
  26. #include "power.h"
  27. #define SWSUSP_SIG "S1SUSPEND"
  28. /*
  29. * The swap map is a data structure used for keeping track of each page
  30. * written to a swap partition. It consists of many swap_map_page
  31. * structures that contain each an array of MAP_PAGE_ENTRIES swap entries.
  32. * These structures are stored on the swap and linked together with the
  33. * help of the .next_swap member.
  34. *
  35. * The swap map is created during suspend. The swap map pages are
  36. * allocated and populated one at a time, so we only need one memory
  37. * page to set up the entire structure.
  38. *
  39. * During resume we also only need to use one swap_map_page structure
  40. * at a time.
  41. */
  42. #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
  43. struct swap_map_page {
  44. sector_t entries[MAP_PAGE_ENTRIES];
  45. sector_t next_swap;
  46. };
  47. /**
  48. * The swap_map_handle structure is used for handling swap in
  49. * a file-alike way
  50. */
  51. struct swap_map_handle {
  52. struct swap_map_page *cur;
  53. sector_t cur_swap;
  54. sector_t first_sector;
  55. unsigned int k;
  56. };
  57. struct swsusp_header {
  58. char reserved[PAGE_SIZE - 20 - sizeof(sector_t) - sizeof(int)];
  59. sector_t image;
  60. unsigned int flags; /* Flags to pass to the "boot" kernel */
  61. char orig_sig[10];
  62. char sig[10];
  63. } __attribute__((packed));
  64. static struct swsusp_header *swsusp_header;
  65. /**
  66. * The following functions are used for tracing the allocated
  67. * swap pages, so that they can be freed in case of an error.
  68. */
  69. struct swsusp_extent {
  70. struct rb_node node;
  71. unsigned long start;
  72. unsigned long end;
  73. };
  74. static struct rb_root swsusp_extents = RB_ROOT;
  75. static int swsusp_extents_insert(unsigned long swap_offset)
  76. {
  77. struct rb_node **new = &(swsusp_extents.rb_node);
  78. struct rb_node *parent = NULL;
  79. struct swsusp_extent *ext;
  80. /* Figure out where to put the new node */
  81. while (*new) {
  82. ext = container_of(*new, struct swsusp_extent, node);
  83. parent = *new;
  84. if (swap_offset < ext->start) {
  85. /* Try to merge */
  86. if (swap_offset == ext->start - 1) {
  87. ext->start--;
  88. return 0;
  89. }
  90. new = &((*new)->rb_left);
  91. } else if (swap_offset > ext->end) {
  92. /* Try to merge */
  93. if (swap_offset == ext->end + 1) {
  94. ext->end++;
  95. return 0;
  96. }
  97. new = &((*new)->rb_right);
  98. } else {
  99. /* It already is in the tree */
  100. return -EINVAL;
  101. }
  102. }
  103. /* Add the new node and rebalance the tree. */
  104. ext = kzalloc(sizeof(struct swsusp_extent), GFP_KERNEL);
  105. if (!ext)
  106. return -ENOMEM;
  107. ext->start = swap_offset;
  108. ext->end = swap_offset;
  109. rb_link_node(&ext->node, parent, new);
  110. rb_insert_color(&ext->node, &swsusp_extents);
  111. return 0;
  112. }
  113. /**
  114. * alloc_swapdev_block - allocate a swap page and register that it has
  115. * been allocated, so that it can be freed in case of an error.
  116. */
  117. sector_t alloc_swapdev_block(int swap)
  118. {
  119. unsigned long offset;
  120. offset = swp_offset(get_swap_page_of_type(swap));
  121. if (offset) {
  122. if (swsusp_extents_insert(offset))
  123. swap_free(swp_entry(swap, offset));
  124. else
  125. return swapdev_block(swap, offset);
  126. }
  127. return 0;
  128. }
  129. /**
  130. * free_all_swap_pages - free swap pages allocated for saving image data.
  131. * It also frees the extents used to register which swap entries had been
  132. * allocated.
  133. */
  134. void free_all_swap_pages(int swap)
  135. {
  136. struct rb_node *node;
  137. while ((node = swsusp_extents.rb_node)) {
  138. struct swsusp_extent *ext;
  139. unsigned long offset;
  140. ext = container_of(node, struct swsusp_extent, node);
  141. rb_erase(node, &swsusp_extents);
  142. for (offset = ext->start; offset <= ext->end; offset++)
  143. swap_free(swp_entry(swap, offset));
  144. kfree(ext);
  145. }
  146. }
  147. int swsusp_swap_in_use(void)
  148. {
  149. return (swsusp_extents.rb_node != NULL);
  150. }
  151. /*
  152. * General things
  153. */
  154. static unsigned short root_swap = 0xffff;
  155. struct block_device *hib_resume_bdev;
  156. /*
  157. * Saving part
  158. */
  159. static int mark_swapfiles(struct swap_map_handle *handle, unsigned int flags)
  160. {
  161. int error;
  162. hib_bio_read_page(swsusp_resume_block, swsusp_header, NULL);
  163. if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) ||
  164. !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) {
  165. memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10);
  166. memcpy(swsusp_header->sig,SWSUSP_SIG, 10);
  167. swsusp_header->image = handle->first_sector;
  168. swsusp_header->flags = flags;
  169. error = hib_bio_write_page(swsusp_resume_block,
  170. swsusp_header, NULL);
  171. } else {
  172. printk(KERN_ERR "PM: Swap header not found!\n");
  173. error = -ENODEV;
  174. }
  175. return error;
  176. }
  177. /**
  178. * swsusp_swap_check - check if the resume device is a swap device
  179. * and get its index (if so)
  180. *
  181. * This is called before saving image
  182. */
  183. static int swsusp_swap_check(void)
  184. {
  185. int res;
  186. res = swap_type_of(swsusp_resume_device, swsusp_resume_block,
  187. &hib_resume_bdev);
  188. if (res < 0)
  189. return res;
  190. root_swap = res;
  191. res = blkdev_get(hib_resume_bdev, FMODE_WRITE);
  192. if (res)
  193. return res;
  194. res = set_blocksize(hib_resume_bdev, PAGE_SIZE);
  195. if (res < 0)
  196. blkdev_put(hib_resume_bdev, FMODE_WRITE);
  197. return res;
  198. }
  199. /**
  200. * write_page - Write one page to given swap location.
  201. * @buf: Address we're writing.
  202. * @offset: Offset of the swap page we're writing to.
  203. * @bio_chain: Link the next write BIO here
  204. */
  205. static int write_page(void *buf, sector_t offset, struct bio **bio_chain)
  206. {
  207. void *src;
  208. if (!offset)
  209. return -ENOSPC;
  210. if (bio_chain) {
  211. src = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
  212. if (src) {
  213. memcpy(src, buf, PAGE_SIZE);
  214. } else {
  215. WARN_ON_ONCE(1);
  216. bio_chain = NULL; /* Go synchronous */
  217. src = buf;
  218. }
  219. } else {
  220. src = buf;
  221. }
  222. return hib_bio_write_page(offset, src, bio_chain);
  223. }
  224. static void release_swap_writer(struct swap_map_handle *handle)
  225. {
  226. if (handle->cur)
  227. free_page((unsigned long)handle->cur);
  228. handle->cur = NULL;
  229. }
  230. static int get_swap_writer(struct swap_map_handle *handle)
  231. {
  232. int ret;
  233. ret = swsusp_swap_check();
  234. if (ret) {
  235. if (ret != -ENOSPC)
  236. printk(KERN_ERR "PM: Cannot find swap device, try "
  237. "swapon -a.\n");
  238. return ret;
  239. }
  240. handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
  241. if (!handle->cur) {
  242. ret = -ENOMEM;
  243. goto err_close;
  244. }
  245. handle->cur_swap = alloc_swapdev_block(root_swap);
  246. if (!handle->cur_swap) {
  247. ret = -ENOSPC;
  248. goto err_rel;
  249. }
  250. handle->k = 0;
  251. handle->first_sector = handle->cur_swap;
  252. return 0;
  253. err_rel:
  254. release_swap_writer(handle);
  255. err_close:
  256. swsusp_close(FMODE_WRITE);
  257. return ret;
  258. }
  259. static int swap_write_page(struct swap_map_handle *handle, void *buf,
  260. struct bio **bio_chain)
  261. {
  262. int error = 0;
  263. sector_t offset;
  264. if (!handle->cur)
  265. return -EINVAL;
  266. offset = alloc_swapdev_block(root_swap);
  267. error = write_page(buf, offset, bio_chain);
  268. if (error)
  269. return error;
  270. handle->cur->entries[handle->k++] = offset;
  271. if (handle->k >= MAP_PAGE_ENTRIES) {
  272. error = hib_wait_on_bio_chain(bio_chain);
  273. if (error)
  274. goto out;
  275. offset = alloc_swapdev_block(root_swap);
  276. if (!offset)
  277. return -ENOSPC;
  278. handle->cur->next_swap = offset;
  279. error = write_page(handle->cur, handle->cur_swap, NULL);
  280. if (error)
  281. goto out;
  282. memset(handle->cur, 0, PAGE_SIZE);
  283. handle->cur_swap = offset;
  284. handle->k = 0;
  285. }
  286. out:
  287. return error;
  288. }
  289. static int flush_swap_writer(struct swap_map_handle *handle)
  290. {
  291. if (handle->cur && handle->cur_swap)
  292. return write_page(handle->cur, handle->cur_swap, NULL);
  293. else
  294. return -EINVAL;
  295. }
  296. static int swap_writer_finish(struct swap_map_handle *handle,
  297. unsigned int flags, int error)
  298. {
  299. if (!error) {
  300. flush_swap_writer(handle);
  301. printk(KERN_INFO "PM: S");
  302. error = mark_swapfiles(handle, flags);
  303. printk("|\n");
  304. }
  305. if (error)
  306. free_all_swap_pages(root_swap);
  307. release_swap_writer(handle);
  308. swsusp_close(FMODE_WRITE);
  309. return error;
  310. }
  311. /**
  312. * save_image - save the suspend image data
  313. */
  314. static int save_image(struct swap_map_handle *handle,
  315. struct snapshot_handle *snapshot,
  316. unsigned int nr_to_write)
  317. {
  318. unsigned int m;
  319. int ret;
  320. int nr_pages;
  321. int err2;
  322. struct bio *bio;
  323. struct timeval start;
  324. struct timeval stop;
  325. printk(KERN_INFO "PM: Saving image data pages (%u pages) ... ",
  326. nr_to_write);
  327. m = nr_to_write / 100;
  328. if (!m)
  329. m = 1;
  330. nr_pages = 0;
  331. bio = NULL;
  332. do_gettimeofday(&start);
  333. while (1) {
  334. ret = snapshot_read_next(snapshot);
  335. if (ret <= 0)
  336. break;
  337. ret = swap_write_page(handle, data_of(*snapshot), &bio);
  338. if (ret)
  339. break;
  340. if (!(nr_pages % m))
  341. printk(KERN_CONT "\b\b\b\b%3d%%", nr_pages / m);
  342. nr_pages++;
  343. }
  344. err2 = hib_wait_on_bio_chain(&bio);
  345. do_gettimeofday(&stop);
  346. if (!ret)
  347. ret = err2;
  348. if (!ret)
  349. printk(KERN_CONT "\b\b\b\bdone\n");
  350. else
  351. printk(KERN_CONT "\n");
  352. swsusp_show_speed(&start, &stop, nr_to_write, "Wrote");
  353. return ret;
  354. }
  355. /**
  356. * enough_swap - Make sure we have enough swap to save the image.
  357. *
  358. * Returns TRUE or FALSE after checking the total amount of swap
  359. * space avaiable from the resume partition.
  360. */
  361. static int enough_swap(unsigned int nr_pages)
  362. {
  363. unsigned int free_swap = count_swap_pages(root_swap, 1);
  364. pr_debug("PM: Free swap pages: %u\n", free_swap);
  365. return free_swap > nr_pages + PAGES_FOR_IO;
  366. }
  367. /**
  368. * swsusp_write - Write entire image and metadata.
  369. * @flags: flags to pass to the "boot" kernel in the image header
  370. *
  371. * It is important _NOT_ to umount filesystems at this point. We want
  372. * them synced (in case something goes wrong) but we DO not want to mark
  373. * filesystem clean: it is not. (And it does not matter, if we resume
  374. * correctly, we'll mark system clean, anyway.)
  375. */
  376. int swsusp_write(unsigned int flags)
  377. {
  378. struct swap_map_handle handle;
  379. struct snapshot_handle snapshot;
  380. struct swsusp_info *header;
  381. unsigned long pages;
  382. int error;
  383. pages = snapshot_get_image_size();
  384. error = get_swap_writer(&handle);
  385. if (error) {
  386. printk(KERN_ERR "PM: Cannot get swap writer\n");
  387. return error;
  388. }
  389. if (!enough_swap(pages)) {
  390. printk(KERN_ERR "PM: Not enough free swap\n");
  391. error = -ENOSPC;
  392. goto out_finish;
  393. }
  394. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  395. error = snapshot_read_next(&snapshot);
  396. if (error < PAGE_SIZE) {
  397. if (error >= 0)
  398. error = -EFAULT;
  399. goto out_finish;
  400. }
  401. header = (struct swsusp_info *)data_of(snapshot);
  402. error = swap_write_page(&handle, header, NULL);
  403. if (!error)
  404. error = save_image(&handle, &snapshot, pages - 1);
  405. out_finish:
  406. error = swap_writer_finish(&handle, flags, error);
  407. return error;
  408. }
  409. /**
  410. * The following functions allow us to read data using a swap map
  411. * in a file-alike way
  412. */
  413. static void release_swap_reader(struct swap_map_handle *handle)
  414. {
  415. if (handle->cur)
  416. free_page((unsigned long)handle->cur);
  417. handle->cur = NULL;
  418. }
  419. static int get_swap_reader(struct swap_map_handle *handle,
  420. unsigned int *flags_p)
  421. {
  422. int error;
  423. *flags_p = swsusp_header->flags;
  424. if (!swsusp_header->image) /* how can this happen? */
  425. return -EINVAL;
  426. handle->cur = (struct swap_map_page *)get_zeroed_page(__GFP_WAIT | __GFP_HIGH);
  427. if (!handle->cur)
  428. return -ENOMEM;
  429. error = hib_bio_read_page(swsusp_header->image, handle->cur, NULL);
  430. if (error) {
  431. release_swap_reader(handle);
  432. return error;
  433. }
  434. handle->k = 0;
  435. return 0;
  436. }
  437. static int swap_read_page(struct swap_map_handle *handle, void *buf,
  438. struct bio **bio_chain)
  439. {
  440. sector_t offset;
  441. int error;
  442. if (!handle->cur)
  443. return -EINVAL;
  444. offset = handle->cur->entries[handle->k];
  445. if (!offset)
  446. return -EFAULT;
  447. error = hib_bio_read_page(offset, buf, bio_chain);
  448. if (error)
  449. return error;
  450. if (++handle->k >= MAP_PAGE_ENTRIES) {
  451. error = hib_wait_on_bio_chain(bio_chain);
  452. handle->k = 0;
  453. offset = handle->cur->next_swap;
  454. if (!offset)
  455. release_swap_reader(handle);
  456. else if (!error)
  457. error = hib_bio_read_page(offset, handle->cur, NULL);
  458. }
  459. return error;
  460. }
  461. static int swap_reader_finish(struct swap_map_handle *handle)
  462. {
  463. release_swap_reader(handle);
  464. return 0;
  465. }
  466. /**
  467. * load_image - load the image using the swap map handle
  468. * @handle and the snapshot handle @snapshot
  469. * (assume there are @nr_pages pages to load)
  470. */
  471. static int load_image(struct swap_map_handle *handle,
  472. struct snapshot_handle *snapshot,
  473. unsigned int nr_to_read)
  474. {
  475. unsigned int m;
  476. int error = 0;
  477. struct timeval start;
  478. struct timeval stop;
  479. struct bio *bio;
  480. int err2;
  481. unsigned nr_pages;
  482. printk(KERN_INFO "PM: Loading image data pages (%u pages) ... ",
  483. nr_to_read);
  484. m = nr_to_read / 100;
  485. if (!m)
  486. m = 1;
  487. nr_pages = 0;
  488. bio = NULL;
  489. do_gettimeofday(&start);
  490. for ( ; ; ) {
  491. error = snapshot_write_next(snapshot);
  492. if (error <= 0)
  493. break;
  494. error = swap_read_page(handle, data_of(*snapshot), &bio);
  495. if (error)
  496. break;
  497. if (snapshot->sync_read)
  498. error = hib_wait_on_bio_chain(&bio);
  499. if (error)
  500. break;
  501. if (!(nr_pages % m))
  502. printk("\b\b\b\b%3d%%", nr_pages / m);
  503. nr_pages++;
  504. }
  505. err2 = hib_wait_on_bio_chain(&bio);
  506. do_gettimeofday(&stop);
  507. if (!error)
  508. error = err2;
  509. if (!error) {
  510. printk("\b\b\b\bdone\n");
  511. snapshot_write_finalize(snapshot);
  512. if (!snapshot_image_loaded(snapshot))
  513. error = -ENODATA;
  514. } else
  515. printk("\n");
  516. swsusp_show_speed(&start, &stop, nr_to_read, "Read");
  517. return error;
  518. }
  519. /**
  520. * swsusp_read - read the hibernation image.
  521. * @flags_p: flags passed by the "frozen" kernel in the image header should
  522. * be written into this memeory location
  523. */
  524. int swsusp_read(unsigned int *flags_p)
  525. {
  526. int error;
  527. struct swap_map_handle handle;
  528. struct snapshot_handle snapshot;
  529. struct swsusp_info *header;
  530. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  531. error = snapshot_write_next(&snapshot);
  532. if (error < PAGE_SIZE)
  533. return error < 0 ? error : -EFAULT;
  534. header = (struct swsusp_info *)data_of(snapshot);
  535. error = get_swap_reader(&handle, flags_p);
  536. if (error)
  537. goto end;
  538. if (!error)
  539. error = swap_read_page(&handle, header, NULL);
  540. if (!error)
  541. error = load_image(&handle, &snapshot, header->pages - 1);
  542. swap_reader_finish(&handle);
  543. end:
  544. if (!error)
  545. pr_debug("PM: Image successfully loaded\n");
  546. else
  547. pr_debug("PM: Error %d resuming\n", error);
  548. return error;
  549. }
  550. /**
  551. * swsusp_check - Check for swsusp signature in the resume device
  552. */
  553. int swsusp_check(void)
  554. {
  555. int error;
  556. hib_resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
  557. if (!IS_ERR(hib_resume_bdev)) {
  558. set_blocksize(hib_resume_bdev, PAGE_SIZE);
  559. memset(swsusp_header, 0, PAGE_SIZE);
  560. error = hib_bio_read_page(swsusp_resume_block,
  561. swsusp_header, NULL);
  562. if (error)
  563. goto put;
  564. if (!memcmp(SWSUSP_SIG, swsusp_header->sig, 10)) {
  565. memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
  566. /* Reset swap signature now */
  567. error = hib_bio_write_page(swsusp_resume_block,
  568. swsusp_header, NULL);
  569. } else {
  570. error = -EINVAL;
  571. }
  572. put:
  573. if (error)
  574. blkdev_put(hib_resume_bdev, FMODE_READ);
  575. else
  576. pr_debug("PM: Signature found, resuming\n");
  577. } else {
  578. error = PTR_ERR(hib_resume_bdev);
  579. }
  580. if (error)
  581. pr_debug("PM: Error %d checking image file\n", error);
  582. return error;
  583. }
  584. /**
  585. * swsusp_close - close swap device.
  586. */
  587. void swsusp_close(fmode_t mode)
  588. {
  589. if (IS_ERR(hib_resume_bdev)) {
  590. pr_debug("PM: Image device not initialised\n");
  591. return;
  592. }
  593. blkdev_put(hib_resume_bdev, mode);
  594. }
  595. static int swsusp_header_init(void)
  596. {
  597. swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
  598. if (!swsusp_header)
  599. panic("Could not allocate memory for swsusp_header\n");
  600. return 0;
  601. }
  602. core_initcall(swsusp_header_init);