page_io.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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(unsigned int __nocast 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, end_swap_bio_write);
  83. if (bio == NULL) {
  84. set_page_dirty(page);
  85. unlock_page(page);
  86. ret = -ENOMEM;
  87. goto out;
  88. }
  89. if (wbc->sync_mode == WB_SYNC_ALL)
  90. rw |= (1 << BIO_RW_SYNC);
  91. inc_page_state(pswpout);
  92. set_page_writeback(page);
  93. unlock_page(page);
  94. submit_bio(rw, bio);
  95. out:
  96. return ret;
  97. }
  98. int swap_readpage(struct file *file, struct page *page)
  99. {
  100. struct bio *bio;
  101. int ret = 0;
  102. BUG_ON(!PageLocked(page));
  103. ClearPageUptodate(page);
  104. bio = get_swap_bio(GFP_KERNEL, page->private, page, end_swap_bio_read);
  105. if (bio == NULL) {
  106. unlock_page(page);
  107. ret = -ENOMEM;
  108. goto out;
  109. }
  110. inc_page_state(pswpin);
  111. submit_bio(READ, bio);
  112. out:
  113. return ret;
  114. }
  115. #ifdef CONFIG_SOFTWARE_SUSPEND
  116. /*
  117. * A scruffy utility function to read or write an arbitrary swap page
  118. * and wait on the I/O. The caller must have a ref on the page.
  119. *
  120. * We use end_swap_bio_read() even for writes, because it happens to do what
  121. * we want.
  122. */
  123. int rw_swap_page_sync(int rw, swp_entry_t entry, struct page *page)
  124. {
  125. struct bio *bio;
  126. int ret = 0;
  127. lock_page(page);
  128. bio = get_swap_bio(GFP_KERNEL, entry.val, page, end_swap_bio_read);
  129. if (bio == NULL) {
  130. unlock_page(page);
  131. ret = -ENOMEM;
  132. goto out;
  133. }
  134. submit_bio(rw | (1 << BIO_RW_SYNC), bio);
  135. wait_on_page_locked(page);
  136. if (!PageUptodate(page) || PageError(page))
  137. ret = -EIO;
  138. out:
  139. return ret;
  140. }
  141. #endif