page_io.c 3.2 KB

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