page_io.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * linux/mm/page_io.c
  3. *
  4. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  5. *
  6. * Swap reorganised 29.12.95,
  7. * Asynchronous swapping added 30.12.95. Stephen Tweedie
  8. * Removed race in async swapping. 14.4.1996. Bruno Haible
  9. * Add swap of shared pages through the page cache. 20.2.1998. Stephen Tweedie
  10. * Always use brw_page, life becomes simpler. 12 May 1998 Eric Biederman
  11. */
  12. #include <linux/mm.h>
  13. #include <linux/kernel_stat.h>
  14. #include <linux/gfp.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/swap.h>
  17. #include <linux/bio.h>
  18. #include <linux/swapops.h>
  19. #include <linux/writeback.h>
  20. #include <linux/frontswap.h>
  21. #include <asm/pgtable.h>
  22. static struct bio *get_swap_bio(gfp_t gfp_flags,
  23. struct page *page, bio_end_io_t end_io)
  24. {
  25. struct bio *bio;
  26. bio = bio_alloc(gfp_flags, 1);
  27. if (bio) {
  28. bio->bi_sector = map_swap_page(page, &bio->bi_bdev);
  29. bio->bi_sector <<= PAGE_SHIFT - 9;
  30. bio->bi_io_vec[0].bv_page = page;
  31. bio->bi_io_vec[0].bv_len = PAGE_SIZE;
  32. bio->bi_io_vec[0].bv_offset = 0;
  33. bio->bi_vcnt = 1;
  34. bio->bi_idx = 0;
  35. bio->bi_size = PAGE_SIZE;
  36. bio->bi_end_io = end_io;
  37. }
  38. return bio;
  39. }
  40. static void end_swap_bio_write(struct bio *bio, int err)
  41. {
  42. const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  43. struct page *page = bio->bi_io_vec[0].bv_page;
  44. if (!uptodate) {
  45. SetPageError(page);
  46. /*
  47. * We failed to write the page out to swap-space.
  48. * Re-dirty the page in order to avoid it being reclaimed.
  49. * Also print a dire warning that things will go BAD (tm)
  50. * very quickly.
  51. *
  52. * Also clear PG_reclaim to avoid rotate_reclaimable_page()
  53. */
  54. set_page_dirty(page);
  55. printk(KERN_ALERT "Write-error on swap-device (%u:%u:%Lu)\n",
  56. imajor(bio->bi_bdev->bd_inode),
  57. iminor(bio->bi_bdev->bd_inode),
  58. (unsigned long long)bio->bi_sector);
  59. ClearPageReclaim(page);
  60. }
  61. end_page_writeback(page);
  62. bio_put(bio);
  63. }
  64. void end_swap_bio_read(struct bio *bio, int err)
  65. {
  66. const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  67. struct page *page = bio->bi_io_vec[0].bv_page;
  68. if (!uptodate) {
  69. SetPageError(page);
  70. ClearPageUptodate(page);
  71. printk(KERN_ALERT "Read-error on swap-device (%u:%u:%Lu)\n",
  72. imajor(bio->bi_bdev->bd_inode),
  73. iminor(bio->bi_bdev->bd_inode),
  74. (unsigned long long)bio->bi_sector);
  75. } else {
  76. SetPageUptodate(page);
  77. }
  78. unlock_page(page);
  79. bio_put(bio);
  80. }
  81. /*
  82. * We may have stale swap cache pages in memory: notice
  83. * them here and get rid of the unnecessary final write.
  84. */
  85. int swap_writepage(struct page *page, struct writeback_control *wbc)
  86. {
  87. struct bio *bio;
  88. int ret = 0, rw = WRITE;
  89. if (try_to_free_swap(page)) {
  90. unlock_page(page);
  91. goto out;
  92. }
  93. if (frontswap_store(page) == 0) {
  94. set_page_writeback(page);
  95. unlock_page(page);
  96. end_page_writeback(page);
  97. goto out;
  98. }
  99. bio = get_swap_bio(GFP_NOIO, page, end_swap_bio_write);
  100. if (bio == NULL) {
  101. set_page_dirty(page);
  102. unlock_page(page);
  103. ret = -ENOMEM;
  104. goto out;
  105. }
  106. if (wbc->sync_mode == WB_SYNC_ALL)
  107. rw |= REQ_SYNC;
  108. count_vm_event(PSWPOUT);
  109. set_page_writeback(page);
  110. unlock_page(page);
  111. submit_bio(rw, bio);
  112. out:
  113. return ret;
  114. }
  115. int swap_readpage(struct page *page)
  116. {
  117. struct bio *bio;
  118. int ret = 0;
  119. VM_BUG_ON(!PageLocked(page));
  120. VM_BUG_ON(PageUptodate(page));
  121. if (frontswap_load(page) == 0) {
  122. SetPageUptodate(page);
  123. unlock_page(page);
  124. goto out;
  125. }
  126. bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
  127. if (bio == NULL) {
  128. unlock_page(page);
  129. ret = -ENOMEM;
  130. goto out;
  131. }
  132. count_vm_event(PSWPIN);
  133. submit_bio(READ, bio);
  134. out:
  135. return ret;
  136. }