page_io.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/pagemap.h>
  15. #include <linux/swap.h>
  16. #include <linux/bio.h>
  17. #include <linux/swapops.h>
  18. #include <linux/writeback.h>
  19. #include <asm/pgtable.h>
  20. static struct bio *get_swap_bio(gfp_t gfp_flags,
  21. struct page *page, bio_end_io_t end_io)
  22. {
  23. struct bio *bio;
  24. bio = bio_alloc(gfp_flags, 1);
  25. if (bio) {
  26. bio->bi_sector = map_swap_page(page, &bio->bi_bdev);
  27. bio->bi_sector <<= PAGE_SHIFT - 9;
  28. bio->bi_io_vec[0].bv_page = page;
  29. bio->bi_io_vec[0].bv_len = PAGE_SIZE;
  30. bio->bi_io_vec[0].bv_offset = 0;
  31. bio->bi_vcnt = 1;
  32. bio->bi_idx = 0;
  33. bio->bi_size = PAGE_SIZE;
  34. bio->bi_end_io = end_io;
  35. }
  36. return bio;
  37. }
  38. static void end_swap_bio_write(struct bio *bio, int err)
  39. {
  40. const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  41. struct page *page = bio->bi_io_vec[0].bv_page;
  42. if (!uptodate) {
  43. SetPageError(page);
  44. /*
  45. * We failed to write the page out to swap-space.
  46. * Re-dirty the page in order to avoid it being reclaimed.
  47. * Also print a dire warning that things will go BAD (tm)
  48. * very quickly.
  49. *
  50. * Also clear PG_reclaim to avoid rotate_reclaimable_page()
  51. */
  52. set_page_dirty(page);
  53. printk(KERN_ALERT "Write-error on swap-device (%u:%u:%Lu)\n",
  54. imajor(bio->bi_bdev->bd_inode),
  55. iminor(bio->bi_bdev->bd_inode),
  56. (unsigned long long)bio->bi_sector);
  57. ClearPageReclaim(page);
  58. }
  59. end_page_writeback(page);
  60. bio_put(bio);
  61. }
  62. void end_swap_bio_read(struct bio *bio, int err)
  63. {
  64. const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  65. struct page *page = bio->bi_io_vec[0].bv_page;
  66. if (!uptodate) {
  67. SetPageError(page);
  68. ClearPageUptodate(page);
  69. printk(KERN_ALERT "Read-error on swap-device (%u:%u:%Lu)\n",
  70. imajor(bio->bi_bdev->bd_inode),
  71. iminor(bio->bi_bdev->bd_inode),
  72. (unsigned long long)bio->bi_sector);
  73. } else {
  74. SetPageUptodate(page);
  75. }
  76. unlock_page(page);
  77. bio_put(bio);
  78. }
  79. /*
  80. * We may have stale swap cache pages in memory: notice
  81. * them here and get rid of the unnecessary final write.
  82. */
  83. int swap_writepage(struct page *page, struct writeback_control *wbc)
  84. {
  85. struct bio *bio;
  86. int ret = 0, rw = WRITE;
  87. if (try_to_free_swap(page)) {
  88. unlock_page(page);
  89. goto out;
  90. }
  91. bio = get_swap_bio(GFP_NOIO, page, end_swap_bio_write);
  92. if (bio == NULL) {
  93. set_page_dirty(page);
  94. unlock_page(page);
  95. ret = -ENOMEM;
  96. goto out;
  97. }
  98. if (wbc->sync_mode == WB_SYNC_ALL)
  99. rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
  100. count_vm_event(PSWPOUT);
  101. set_page_writeback(page);
  102. unlock_page(page);
  103. submit_bio(rw, bio);
  104. out:
  105. return ret;
  106. }
  107. int swap_readpage(struct page *page)
  108. {
  109. struct bio *bio;
  110. int ret = 0;
  111. VM_BUG_ON(!PageLocked(page));
  112. VM_BUG_ON(PageUptodate(page));
  113. bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
  114. if (bio == NULL) {
  115. unlock_page(page);
  116. ret = -ENOMEM;
  117. goto out;
  118. }
  119. count_vm_event(PSWPIN);
  120. submit_bio(READ, bio);
  121. out:
  122. return ret;
  123. }