page_io.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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/buffer_head.h>
  20. #include <linux/writeback.h>
  21. #include <linux/frontswap.h>
  22. #include <asm/pgtable.h>
  23. static struct bio *get_swap_bio(gfp_t gfp_flags,
  24. struct page *page, bio_end_io_t end_io)
  25. {
  26. struct bio *bio;
  27. bio = bio_alloc(gfp_flags, 1);
  28. if (bio) {
  29. bio->bi_sector = map_swap_page(page, &bio->bi_bdev);
  30. bio->bi_sector <<= PAGE_SHIFT - 9;
  31. bio->bi_io_vec[0].bv_page = page;
  32. bio->bi_io_vec[0].bv_len = PAGE_SIZE;
  33. bio->bi_io_vec[0].bv_offset = 0;
  34. bio->bi_vcnt = 1;
  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. int generic_swapfile_activate(struct swap_info_struct *sis,
  82. struct file *swap_file,
  83. sector_t *span)
  84. {
  85. struct address_space *mapping = swap_file->f_mapping;
  86. struct inode *inode = mapping->host;
  87. unsigned blocks_per_page;
  88. unsigned long page_no;
  89. unsigned blkbits;
  90. sector_t probe_block;
  91. sector_t last_block;
  92. sector_t lowest_block = -1;
  93. sector_t highest_block = 0;
  94. int nr_extents = 0;
  95. int ret;
  96. blkbits = inode->i_blkbits;
  97. blocks_per_page = PAGE_SIZE >> blkbits;
  98. /*
  99. * Map all the blocks into the extent list. This code doesn't try
  100. * to be very smart.
  101. */
  102. probe_block = 0;
  103. page_no = 0;
  104. last_block = i_size_read(inode) >> blkbits;
  105. while ((probe_block + blocks_per_page) <= last_block &&
  106. page_no < sis->max) {
  107. unsigned block_in_page;
  108. sector_t first_block;
  109. first_block = bmap(inode, probe_block);
  110. if (first_block == 0)
  111. goto bad_bmap;
  112. /*
  113. * It must be PAGE_SIZE aligned on-disk
  114. */
  115. if (first_block & (blocks_per_page - 1)) {
  116. probe_block++;
  117. goto reprobe;
  118. }
  119. for (block_in_page = 1; block_in_page < blocks_per_page;
  120. block_in_page++) {
  121. sector_t block;
  122. block = bmap(inode, probe_block + block_in_page);
  123. if (block == 0)
  124. goto bad_bmap;
  125. if (block != first_block + block_in_page) {
  126. /* Discontiguity */
  127. probe_block++;
  128. goto reprobe;
  129. }
  130. }
  131. first_block >>= (PAGE_SHIFT - blkbits);
  132. if (page_no) { /* exclude the header page */
  133. if (first_block < lowest_block)
  134. lowest_block = first_block;
  135. if (first_block > highest_block)
  136. highest_block = first_block;
  137. }
  138. /*
  139. * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
  140. */
  141. ret = add_swap_extent(sis, page_no, 1, first_block);
  142. if (ret < 0)
  143. goto out;
  144. nr_extents += ret;
  145. page_no++;
  146. probe_block += blocks_per_page;
  147. reprobe:
  148. continue;
  149. }
  150. ret = nr_extents;
  151. *span = 1 + highest_block - lowest_block;
  152. if (page_no == 0)
  153. page_no = 1; /* force Empty message */
  154. sis->max = page_no;
  155. sis->pages = page_no - 1;
  156. sis->highest_bit = page_no - 1;
  157. out:
  158. return ret;
  159. bad_bmap:
  160. printk(KERN_ERR "swapon: swapfile has holes\n");
  161. ret = -EINVAL;
  162. goto out;
  163. }
  164. /*
  165. * We may have stale swap cache pages in memory: notice
  166. * them here and get rid of the unnecessary final write.
  167. */
  168. int swap_writepage(struct page *page, struct writeback_control *wbc)
  169. {
  170. struct bio *bio;
  171. int ret = 0, rw = WRITE;
  172. struct swap_info_struct *sis = page_swap_info(page);
  173. if (try_to_free_swap(page)) {
  174. unlock_page(page);
  175. goto out;
  176. }
  177. if (frontswap_store(page) == 0) {
  178. set_page_writeback(page);
  179. unlock_page(page);
  180. end_page_writeback(page);
  181. goto out;
  182. }
  183. if (sis->flags & SWP_FILE) {
  184. struct kiocb kiocb;
  185. struct file *swap_file = sis->swap_file;
  186. struct address_space *mapping = swap_file->f_mapping;
  187. struct iovec iov = {
  188. .iov_base = kmap(page),
  189. .iov_len = PAGE_SIZE,
  190. };
  191. init_sync_kiocb(&kiocb, swap_file);
  192. kiocb.ki_pos = page_file_offset(page);
  193. kiocb.ki_left = PAGE_SIZE;
  194. kiocb.ki_nbytes = PAGE_SIZE;
  195. unlock_page(page);
  196. ret = mapping->a_ops->direct_IO(KERNEL_WRITE,
  197. &kiocb, &iov,
  198. kiocb.ki_pos, 1);
  199. kunmap(page);
  200. if (ret == PAGE_SIZE) {
  201. count_vm_event(PSWPOUT);
  202. ret = 0;
  203. }
  204. return ret;
  205. }
  206. bio = get_swap_bio(GFP_NOIO, page, end_swap_bio_write);
  207. if (bio == NULL) {
  208. set_page_dirty(page);
  209. unlock_page(page);
  210. ret = -ENOMEM;
  211. goto out;
  212. }
  213. if (wbc->sync_mode == WB_SYNC_ALL)
  214. rw |= REQ_SYNC;
  215. count_vm_event(PSWPOUT);
  216. set_page_writeback(page);
  217. unlock_page(page);
  218. submit_bio(rw, bio);
  219. out:
  220. return ret;
  221. }
  222. int swap_readpage(struct page *page)
  223. {
  224. struct bio *bio;
  225. int ret = 0;
  226. struct swap_info_struct *sis = page_swap_info(page);
  227. VM_BUG_ON(!PageLocked(page));
  228. VM_BUG_ON(PageUptodate(page));
  229. if (frontswap_load(page) == 0) {
  230. SetPageUptodate(page);
  231. unlock_page(page);
  232. goto out;
  233. }
  234. if (sis->flags & SWP_FILE) {
  235. struct file *swap_file = sis->swap_file;
  236. struct address_space *mapping = swap_file->f_mapping;
  237. ret = mapping->a_ops->readpage(swap_file, page);
  238. if (!ret)
  239. count_vm_event(PSWPIN);
  240. return ret;
  241. }
  242. bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
  243. if (bio == NULL) {
  244. unlock_page(page);
  245. ret = -ENOMEM;
  246. goto out;
  247. }
  248. count_vm_event(PSWPIN);
  249. submit_bio(READ, bio);
  250. out:
  251. return ret;
  252. }
  253. int swap_set_page_dirty(struct page *page)
  254. {
  255. struct swap_info_struct *sis = page_swap_info(page);
  256. if (sis->flags & SWP_FILE) {
  257. struct address_space *mapping = sis->swap_file->f_mapping;
  258. return mapping->a_ops->set_page_dirty(page);
  259. } else {
  260. return __set_page_dirty_no_writeback(page);
  261. }
  262. }