page_io.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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_idx = 0;
  36. bio->bi_size = PAGE_SIZE;
  37. bio->bi_end_io = end_io;
  38. }
  39. return bio;
  40. }
  41. static void end_swap_bio_write(struct bio *bio, int err)
  42. {
  43. const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  44. struct page *page = bio->bi_io_vec[0].bv_page;
  45. if (!uptodate) {
  46. SetPageError(page);
  47. /*
  48. * We failed to write the page out to swap-space.
  49. * Re-dirty the page in order to avoid it being reclaimed.
  50. * Also print a dire warning that things will go BAD (tm)
  51. * very quickly.
  52. *
  53. * Also clear PG_reclaim to avoid rotate_reclaimable_page()
  54. */
  55. set_page_dirty(page);
  56. printk(KERN_ALERT "Write-error on swap-device (%u:%u:%Lu)\n",
  57. imajor(bio->bi_bdev->bd_inode),
  58. iminor(bio->bi_bdev->bd_inode),
  59. (unsigned long long)bio->bi_sector);
  60. ClearPageReclaim(page);
  61. }
  62. end_page_writeback(page);
  63. bio_put(bio);
  64. }
  65. void end_swap_bio_read(struct bio *bio, int err)
  66. {
  67. const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  68. struct page *page = bio->bi_io_vec[0].bv_page;
  69. if (!uptodate) {
  70. SetPageError(page);
  71. ClearPageUptodate(page);
  72. printk(KERN_ALERT "Read-error on swap-device (%u:%u:%Lu)\n",
  73. imajor(bio->bi_bdev->bd_inode),
  74. iminor(bio->bi_bdev->bd_inode),
  75. (unsigned long long)bio->bi_sector);
  76. } else {
  77. SetPageUptodate(page);
  78. }
  79. unlock_page(page);
  80. bio_put(bio);
  81. }
  82. int generic_swapfile_activate(struct swap_info_struct *sis,
  83. struct file *swap_file,
  84. sector_t *span)
  85. {
  86. struct address_space *mapping = swap_file->f_mapping;
  87. struct inode *inode = mapping->host;
  88. unsigned blocks_per_page;
  89. unsigned long page_no;
  90. unsigned blkbits;
  91. sector_t probe_block;
  92. sector_t last_block;
  93. sector_t lowest_block = -1;
  94. sector_t highest_block = 0;
  95. int nr_extents = 0;
  96. int ret;
  97. blkbits = inode->i_blkbits;
  98. blocks_per_page = PAGE_SIZE >> blkbits;
  99. /*
  100. * Map all the blocks into the extent list. This code doesn't try
  101. * to be very smart.
  102. */
  103. probe_block = 0;
  104. page_no = 0;
  105. last_block = i_size_read(inode) >> blkbits;
  106. while ((probe_block + blocks_per_page) <= last_block &&
  107. page_no < sis->max) {
  108. unsigned block_in_page;
  109. sector_t first_block;
  110. first_block = bmap(inode, probe_block);
  111. if (first_block == 0)
  112. goto bad_bmap;
  113. /*
  114. * It must be PAGE_SIZE aligned on-disk
  115. */
  116. if (first_block & (blocks_per_page - 1)) {
  117. probe_block++;
  118. goto reprobe;
  119. }
  120. for (block_in_page = 1; block_in_page < blocks_per_page;
  121. block_in_page++) {
  122. sector_t block;
  123. block = bmap(inode, probe_block + block_in_page);
  124. if (block == 0)
  125. goto bad_bmap;
  126. if (block != first_block + block_in_page) {
  127. /* Discontiguity */
  128. probe_block++;
  129. goto reprobe;
  130. }
  131. }
  132. first_block >>= (PAGE_SHIFT - blkbits);
  133. if (page_no) { /* exclude the header page */
  134. if (first_block < lowest_block)
  135. lowest_block = first_block;
  136. if (first_block > highest_block)
  137. highest_block = first_block;
  138. }
  139. /*
  140. * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
  141. */
  142. ret = add_swap_extent(sis, page_no, 1, first_block);
  143. if (ret < 0)
  144. goto out;
  145. nr_extents += ret;
  146. page_no++;
  147. probe_block += blocks_per_page;
  148. reprobe:
  149. continue;
  150. }
  151. ret = nr_extents;
  152. *span = 1 + highest_block - lowest_block;
  153. if (page_no == 0)
  154. page_no = 1; /* force Empty message */
  155. sis->max = page_no;
  156. sis->pages = page_no - 1;
  157. sis->highest_bit = page_no - 1;
  158. out:
  159. return ret;
  160. bad_bmap:
  161. printk(KERN_ERR "swapon: swapfile has holes\n");
  162. ret = -EINVAL;
  163. goto out;
  164. }
  165. /*
  166. * We may have stale swap cache pages in memory: notice
  167. * them here and get rid of the unnecessary final write.
  168. */
  169. int swap_writepage(struct page *page, struct writeback_control *wbc)
  170. {
  171. struct bio *bio;
  172. int ret = 0, rw = WRITE;
  173. struct swap_info_struct *sis = page_swap_info(page);
  174. if (try_to_free_swap(page)) {
  175. unlock_page(page);
  176. goto out;
  177. }
  178. if (frontswap_store(page) == 0) {
  179. set_page_writeback(page);
  180. unlock_page(page);
  181. end_page_writeback(page);
  182. goto out;
  183. }
  184. if (sis->flags & SWP_FILE) {
  185. struct kiocb kiocb;
  186. struct file *swap_file = sis->swap_file;
  187. struct address_space *mapping = swap_file->f_mapping;
  188. struct iovec iov = {
  189. .iov_base = kmap(page),
  190. .iov_len = PAGE_SIZE,
  191. };
  192. init_sync_kiocb(&kiocb, swap_file);
  193. kiocb.ki_pos = page_file_offset(page);
  194. kiocb.ki_left = PAGE_SIZE;
  195. kiocb.ki_nbytes = PAGE_SIZE;
  196. unlock_page(page);
  197. ret = mapping->a_ops->direct_IO(KERNEL_WRITE,
  198. &kiocb, &iov,
  199. kiocb.ki_pos, 1);
  200. kunmap(page);
  201. if (ret == PAGE_SIZE) {
  202. count_vm_event(PSWPOUT);
  203. ret = 0;
  204. }
  205. return ret;
  206. }
  207. bio = get_swap_bio(GFP_NOIO, page, end_swap_bio_write);
  208. if (bio == NULL) {
  209. set_page_dirty(page);
  210. unlock_page(page);
  211. ret = -ENOMEM;
  212. goto out;
  213. }
  214. if (wbc->sync_mode == WB_SYNC_ALL)
  215. rw |= REQ_SYNC;
  216. count_vm_event(PSWPOUT);
  217. set_page_writeback(page);
  218. unlock_page(page);
  219. submit_bio(rw, bio);
  220. out:
  221. return ret;
  222. }
  223. int swap_readpage(struct page *page)
  224. {
  225. struct bio *bio;
  226. int ret = 0;
  227. struct swap_info_struct *sis = page_swap_info(page);
  228. VM_BUG_ON(!PageLocked(page));
  229. VM_BUG_ON(PageUptodate(page));
  230. if (frontswap_load(page) == 0) {
  231. SetPageUptodate(page);
  232. unlock_page(page);
  233. goto out;
  234. }
  235. if (sis->flags & SWP_FILE) {
  236. struct file *swap_file = sis->swap_file;
  237. struct address_space *mapping = swap_file->f_mapping;
  238. ret = mapping->a_ops->readpage(swap_file, page);
  239. if (!ret)
  240. count_vm_event(PSWPIN);
  241. return ret;
  242. }
  243. bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
  244. if (bio == NULL) {
  245. unlock_page(page);
  246. ret = -ENOMEM;
  247. goto out;
  248. }
  249. count_vm_event(PSWPIN);
  250. submit_bio(READ, bio);
  251. out:
  252. return ret;
  253. }
  254. int swap_set_page_dirty(struct page *page)
  255. {
  256. struct swap_info_struct *sis = page_swap_info(page);
  257. if (sis->flags & SWP_FILE) {
  258. struct address_space *mapping = sis->swap_file->f_mapping;
  259. return mapping->a_ops->set_page_dirty(page);
  260. } else {
  261. return __set_page_dirty_no_writeback(page);
  262. }
  263. }