page_io.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. 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. int ret = 0;
  172. if (try_to_free_swap(page)) {
  173. unlock_page(page);
  174. goto out;
  175. }
  176. if (frontswap_store(page) == 0) {
  177. set_page_writeback(page);
  178. unlock_page(page);
  179. end_page_writeback(page);
  180. goto out;
  181. }
  182. ret = __swap_writepage(page, wbc, end_swap_bio_write);
  183. out:
  184. return ret;
  185. }
  186. int __swap_writepage(struct page *page, struct writeback_control *wbc,
  187. void (*end_write_func)(struct bio *, int))
  188. {
  189. struct bio *bio;
  190. int ret = 0, rw = WRITE;
  191. struct swap_info_struct *sis = page_swap_info(page);
  192. if (sis->flags & SWP_FILE) {
  193. struct kiocb kiocb;
  194. struct file *swap_file = sis->swap_file;
  195. struct address_space *mapping = swap_file->f_mapping;
  196. struct iovec iov = {
  197. .iov_base = kmap(page),
  198. .iov_len = PAGE_SIZE,
  199. };
  200. init_sync_kiocb(&kiocb, swap_file);
  201. kiocb.ki_pos = page_file_offset(page);
  202. kiocb.ki_left = PAGE_SIZE;
  203. kiocb.ki_nbytes = PAGE_SIZE;
  204. set_page_writeback(page);
  205. unlock_page(page);
  206. ret = mapping->a_ops->direct_IO(KERNEL_WRITE,
  207. &kiocb, &iov,
  208. kiocb.ki_pos, 1);
  209. kunmap(page);
  210. if (ret == PAGE_SIZE) {
  211. count_vm_event(PSWPOUT);
  212. ret = 0;
  213. } else {
  214. /*
  215. * In the case of swap-over-nfs, this can be a
  216. * temporary failure if the system has limited
  217. * memory for allocating transmit buffers.
  218. * Mark the page dirty and avoid
  219. * rotate_reclaimable_page but rate-limit the
  220. * messages but do not flag PageError like
  221. * the normal direct-to-bio case as it could
  222. * be temporary.
  223. */
  224. set_page_dirty(page);
  225. ClearPageReclaim(page);
  226. pr_err_ratelimited("Write error on dio swapfile (%Lu)\n",
  227. page_file_offset(page));
  228. }
  229. end_page_writeback(page);
  230. return ret;
  231. }
  232. bio = get_swap_bio(GFP_NOIO, page, end_write_func);
  233. if (bio == NULL) {
  234. set_page_dirty(page);
  235. unlock_page(page);
  236. ret = -ENOMEM;
  237. goto out;
  238. }
  239. if (wbc->sync_mode == WB_SYNC_ALL)
  240. rw |= REQ_SYNC;
  241. count_vm_event(PSWPOUT);
  242. set_page_writeback(page);
  243. unlock_page(page);
  244. submit_bio(rw, bio);
  245. out:
  246. return ret;
  247. }
  248. int swap_readpage(struct page *page)
  249. {
  250. struct bio *bio;
  251. int ret = 0;
  252. struct swap_info_struct *sis = page_swap_info(page);
  253. VM_BUG_ON(!PageLocked(page));
  254. VM_BUG_ON(PageUptodate(page));
  255. if (frontswap_load(page) == 0) {
  256. SetPageUptodate(page);
  257. unlock_page(page);
  258. goto out;
  259. }
  260. if (sis->flags & SWP_FILE) {
  261. struct file *swap_file = sis->swap_file;
  262. struct address_space *mapping = swap_file->f_mapping;
  263. ret = mapping->a_ops->readpage(swap_file, page);
  264. if (!ret)
  265. count_vm_event(PSWPIN);
  266. return ret;
  267. }
  268. bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
  269. if (bio == NULL) {
  270. unlock_page(page);
  271. ret = -ENOMEM;
  272. goto out;
  273. }
  274. count_vm_event(PSWPIN);
  275. submit_bio(READ, bio);
  276. out:
  277. return ret;
  278. }
  279. int swap_set_page_dirty(struct page *page)
  280. {
  281. struct swap_info_struct *sis = page_swap_info(page);
  282. if (sis->flags & SWP_FILE) {
  283. struct address_space *mapping = sis->swap_file->f_mapping;
  284. return mapping->a_ops->set_page_dirty(page);
  285. } else {
  286. return __set_page_dirty_no_writeback(page);
  287. }
  288. }