page_io.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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, pgoff_t index,
  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. struct swap_info_struct *sis;
  27. swp_entry_t entry = { .val = index, };
  28. sis = get_swap_info_struct(swp_type(entry));
  29. bio->bi_sector = map_swap_page(sis, swp_offset(entry)) *
  30. (PAGE_SIZE >> 9);
  31. bio->bi_bdev = sis->bdev;
  32. bio->bi_io_vec[0].bv_page = page;
  33. bio->bi_io_vec[0].bv_len = PAGE_SIZE;
  34. bio->bi_io_vec[0].bv_offset = 0;
  35. bio->bi_vcnt = 1;
  36. bio->bi_idx = 0;
  37. bio->bi_size = PAGE_SIZE;
  38. bio->bi_end_io = end_io;
  39. }
  40. return bio;
  41. }
  42. static int end_swap_bio_write(struct bio *bio, unsigned int bytes_done, int err)
  43. {
  44. const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  45. struct page *page = bio->bi_io_vec[0].bv_page;
  46. if (bio->bi_size)
  47. return 1;
  48. if (!uptodate)
  49. SetPageError(page);
  50. end_page_writeback(page);
  51. bio_put(bio);
  52. return 0;
  53. }
  54. static int end_swap_bio_read(struct bio *bio, unsigned int bytes_done, int err)
  55. {
  56. const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  57. struct page *page = bio->bi_io_vec[0].bv_page;
  58. if (bio->bi_size)
  59. return 1;
  60. if (!uptodate) {
  61. SetPageError(page);
  62. ClearPageUptodate(page);
  63. } else {
  64. SetPageUptodate(page);
  65. }
  66. unlock_page(page);
  67. bio_put(bio);
  68. return 0;
  69. }
  70. /*
  71. * We may have stale swap cache pages in memory: notice
  72. * them here and get rid of the unnecessary final write.
  73. */
  74. int swap_writepage(struct page *page, struct writeback_control *wbc)
  75. {
  76. struct bio *bio;
  77. int ret = 0, rw = WRITE;
  78. if (remove_exclusive_swap_page(page)) {
  79. unlock_page(page);
  80. goto out;
  81. }
  82. bio = get_swap_bio(GFP_NOIO, page_private(page), page,
  83. end_swap_bio_write);
  84. if (bio == NULL) {
  85. set_page_dirty(page);
  86. unlock_page(page);
  87. ret = -ENOMEM;
  88. goto out;
  89. }
  90. if (wbc->sync_mode == WB_SYNC_ALL)
  91. rw |= (1 << BIO_RW_SYNC);
  92. inc_page_state(pswpout);
  93. set_page_writeback(page);
  94. unlock_page(page);
  95. submit_bio(rw, bio);
  96. out:
  97. return ret;
  98. }
  99. int swap_readpage(struct file *file, struct page *page)
  100. {
  101. struct bio *bio;
  102. int ret = 0;
  103. BUG_ON(!PageLocked(page));
  104. ClearPageUptodate(page);
  105. bio = get_swap_bio(GFP_KERNEL, page_private(page), page,
  106. end_swap_bio_read);
  107. if (bio == NULL) {
  108. unlock_page(page);
  109. ret = -ENOMEM;
  110. goto out;
  111. }
  112. inc_page_state(pswpin);
  113. submit_bio(READ, bio);
  114. out:
  115. return ret;
  116. }
  117. #ifdef CONFIG_SOFTWARE_SUSPEND
  118. /*
  119. * A scruffy utility function to read or write an arbitrary swap page
  120. * and wait on the I/O. The caller must have a ref on the page.
  121. *
  122. * We use end_swap_bio_read() even for writes, because it happens to do what
  123. * we want.
  124. */
  125. int rw_swap_page_sync(int rw, swp_entry_t entry, struct page *page)
  126. {
  127. struct bio *bio;
  128. int ret = 0;
  129. lock_page(page);
  130. bio = get_swap_bio(GFP_KERNEL, entry.val, page, end_swap_bio_read);
  131. if (bio == NULL) {
  132. unlock_page(page);
  133. ret = -ENOMEM;
  134. goto out;
  135. }
  136. submit_bio(rw | (1 << BIO_RW_SYNC), bio);
  137. wait_on_page_locked(page);
  138. if (!PageUptodate(page) || PageError(page))
  139. ret = -EIO;
  140. out:
  141. return ret;
  142. }
  143. #endif