page_io.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 <linux/aio.h>
  23. #include <linux/blkdev.h>
  24. #include <asm/pgtable.h>
  25. static struct bio *get_swap_bio(gfp_t gfp_flags,
  26. struct page *page, bio_end_io_t end_io)
  27. {
  28. struct bio *bio;
  29. bio = bio_alloc(gfp_flags, 1);
  30. if (bio) {
  31. bio->bi_sector = map_swap_page(page, &bio->bi_bdev);
  32. bio->bi_sector <<= PAGE_SHIFT - 9;
  33. bio->bi_io_vec[0].bv_page = page;
  34. bio->bi_io_vec[0].bv_len = PAGE_SIZE;
  35. bio->bi_io_vec[0].bv_offset = 0;
  36. bio->bi_vcnt = 1;
  37. bio->bi_size = PAGE_SIZE;
  38. bio->bi_end_io = end_io;
  39. }
  40. return bio;
  41. }
  42. void end_swap_bio_write(struct bio *bio, 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 (!uptodate) {
  47. SetPageError(page);
  48. /*
  49. * We failed to write the page out to swap-space.
  50. * Re-dirty the page in order to avoid it being reclaimed.
  51. * Also print a dire warning that things will go BAD (tm)
  52. * very quickly.
  53. *
  54. * Also clear PG_reclaim to avoid rotate_reclaimable_page()
  55. */
  56. set_page_dirty(page);
  57. printk(KERN_ALERT "Write-error on swap-device (%u:%u:%Lu)\n",
  58. imajor(bio->bi_bdev->bd_inode),
  59. iminor(bio->bi_bdev->bd_inode),
  60. (unsigned long long)bio->bi_sector);
  61. ClearPageReclaim(page);
  62. }
  63. end_page_writeback(page);
  64. bio_put(bio);
  65. }
  66. void end_swap_bio_read(struct bio *bio, int err)
  67. {
  68. const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  69. struct page *page = bio->bi_io_vec[0].bv_page;
  70. if (!uptodate) {
  71. SetPageError(page);
  72. ClearPageUptodate(page);
  73. printk(KERN_ALERT "Read-error on swap-device (%u:%u:%Lu)\n",
  74. imajor(bio->bi_bdev->bd_inode),
  75. iminor(bio->bi_bdev->bd_inode),
  76. (unsigned long long)bio->bi_sector);
  77. goto out;
  78. }
  79. SetPageUptodate(page);
  80. /*
  81. * There is no guarantee that the page is in swap cache - the software
  82. * suspend code (at least) uses end_swap_bio_read() against a non-
  83. * swapcache page. So we must check PG_swapcache before proceeding with
  84. * this optimization.
  85. */
  86. if (likely(PageSwapCache(page))) {
  87. struct swap_info_struct *sis;
  88. sis = page_swap_info(page);
  89. if (sis->flags & SWP_BLKDEV) {
  90. /*
  91. * The swap subsystem performs lazy swap slot freeing,
  92. * expecting that the page will be swapped out again.
  93. * So we can avoid an unnecessary write if the page
  94. * isn't redirtied.
  95. * This is good for real swap storage because we can
  96. * reduce unnecessary I/O and enhance wear-leveling
  97. * if an SSD is used as the as swap device.
  98. * But if in-memory swap device (eg zram) is used,
  99. * this causes a duplicated copy between uncompressed
  100. * data in VM-owned memory and compressed data in
  101. * zram-owned memory. So let's free zram-owned memory
  102. * and make the VM-owned decompressed page *dirty*,
  103. * so the page should be swapped out somewhere again if
  104. * we again wish to reclaim it.
  105. */
  106. struct gendisk *disk = sis->bdev->bd_disk;
  107. if (disk->fops->swap_slot_free_notify) {
  108. swp_entry_t entry;
  109. unsigned long offset;
  110. entry.val = page_private(page);
  111. offset = swp_offset(entry);
  112. SetPageDirty(page);
  113. disk->fops->swap_slot_free_notify(sis->bdev,
  114. offset);
  115. }
  116. }
  117. }
  118. out:
  119. unlock_page(page);
  120. bio_put(bio);
  121. }
  122. int generic_swapfile_activate(struct swap_info_struct *sis,
  123. struct file *swap_file,
  124. sector_t *span)
  125. {
  126. struct address_space *mapping = swap_file->f_mapping;
  127. struct inode *inode = mapping->host;
  128. unsigned blocks_per_page;
  129. unsigned long page_no;
  130. unsigned blkbits;
  131. sector_t probe_block;
  132. sector_t last_block;
  133. sector_t lowest_block = -1;
  134. sector_t highest_block = 0;
  135. int nr_extents = 0;
  136. int ret;
  137. blkbits = inode->i_blkbits;
  138. blocks_per_page = PAGE_SIZE >> blkbits;
  139. /*
  140. * Map all the blocks into the extent list. This code doesn't try
  141. * to be very smart.
  142. */
  143. probe_block = 0;
  144. page_no = 0;
  145. last_block = i_size_read(inode) >> blkbits;
  146. while ((probe_block + blocks_per_page) <= last_block &&
  147. page_no < sis->max) {
  148. unsigned block_in_page;
  149. sector_t first_block;
  150. first_block = bmap(inode, probe_block);
  151. if (first_block == 0)
  152. goto bad_bmap;
  153. /*
  154. * It must be PAGE_SIZE aligned on-disk
  155. */
  156. if (first_block & (blocks_per_page - 1)) {
  157. probe_block++;
  158. goto reprobe;
  159. }
  160. for (block_in_page = 1; block_in_page < blocks_per_page;
  161. block_in_page++) {
  162. sector_t block;
  163. block = bmap(inode, probe_block + block_in_page);
  164. if (block == 0)
  165. goto bad_bmap;
  166. if (block != first_block + block_in_page) {
  167. /* Discontiguity */
  168. probe_block++;
  169. goto reprobe;
  170. }
  171. }
  172. first_block >>= (PAGE_SHIFT - blkbits);
  173. if (page_no) { /* exclude the header page */
  174. if (first_block < lowest_block)
  175. lowest_block = first_block;
  176. if (first_block > highest_block)
  177. highest_block = first_block;
  178. }
  179. /*
  180. * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
  181. */
  182. ret = add_swap_extent(sis, page_no, 1, first_block);
  183. if (ret < 0)
  184. goto out;
  185. nr_extents += ret;
  186. page_no++;
  187. probe_block += blocks_per_page;
  188. reprobe:
  189. continue;
  190. }
  191. ret = nr_extents;
  192. *span = 1 + highest_block - lowest_block;
  193. if (page_no == 0)
  194. page_no = 1; /* force Empty message */
  195. sis->max = page_no;
  196. sis->pages = page_no - 1;
  197. sis->highest_bit = page_no - 1;
  198. out:
  199. return ret;
  200. bad_bmap:
  201. printk(KERN_ERR "swapon: swapfile has holes\n");
  202. ret = -EINVAL;
  203. goto out;
  204. }
  205. /*
  206. * We may have stale swap cache pages in memory: notice
  207. * them here and get rid of the unnecessary final write.
  208. */
  209. int swap_writepage(struct page *page, struct writeback_control *wbc)
  210. {
  211. int ret = 0;
  212. if (try_to_free_swap(page)) {
  213. unlock_page(page);
  214. goto out;
  215. }
  216. if (frontswap_store(page) == 0) {
  217. set_page_writeback(page);
  218. unlock_page(page);
  219. end_page_writeback(page);
  220. goto out;
  221. }
  222. ret = __swap_writepage(page, wbc, end_swap_bio_write);
  223. out:
  224. return ret;
  225. }
  226. int __swap_writepage(struct page *page, struct writeback_control *wbc,
  227. void (*end_write_func)(struct bio *, int))
  228. {
  229. struct bio *bio;
  230. int ret = 0, rw = WRITE;
  231. struct swap_info_struct *sis = page_swap_info(page);
  232. if (sis->flags & SWP_FILE) {
  233. struct kiocb kiocb;
  234. struct file *swap_file = sis->swap_file;
  235. struct address_space *mapping = swap_file->f_mapping;
  236. struct iovec iov = {
  237. .iov_base = kmap(page),
  238. .iov_len = PAGE_SIZE,
  239. };
  240. init_sync_kiocb(&kiocb, swap_file);
  241. kiocb.ki_pos = page_file_offset(page);
  242. kiocb.ki_left = PAGE_SIZE;
  243. kiocb.ki_nbytes = PAGE_SIZE;
  244. set_page_writeback(page);
  245. unlock_page(page);
  246. ret = mapping->a_ops->direct_IO(KERNEL_WRITE,
  247. &kiocb, &iov,
  248. kiocb.ki_pos, 1);
  249. kunmap(page);
  250. if (ret == PAGE_SIZE) {
  251. count_vm_event(PSWPOUT);
  252. ret = 0;
  253. } else {
  254. /*
  255. * In the case of swap-over-nfs, this can be a
  256. * temporary failure if the system has limited
  257. * memory for allocating transmit buffers.
  258. * Mark the page dirty and avoid
  259. * rotate_reclaimable_page but rate-limit the
  260. * messages but do not flag PageError like
  261. * the normal direct-to-bio case as it could
  262. * be temporary.
  263. */
  264. set_page_dirty(page);
  265. ClearPageReclaim(page);
  266. pr_err_ratelimited("Write error on dio swapfile (%Lu)\n",
  267. page_file_offset(page));
  268. }
  269. end_page_writeback(page);
  270. return ret;
  271. }
  272. bio = get_swap_bio(GFP_NOIO, page, end_write_func);
  273. if (bio == NULL) {
  274. set_page_dirty(page);
  275. unlock_page(page);
  276. ret = -ENOMEM;
  277. goto out;
  278. }
  279. if (wbc->sync_mode == WB_SYNC_ALL)
  280. rw |= REQ_SYNC;
  281. count_vm_event(PSWPOUT);
  282. set_page_writeback(page);
  283. unlock_page(page);
  284. submit_bio(rw, bio);
  285. out:
  286. return ret;
  287. }
  288. int swap_readpage(struct page *page)
  289. {
  290. struct bio *bio;
  291. int ret = 0;
  292. struct swap_info_struct *sis = page_swap_info(page);
  293. VM_BUG_ON(!PageLocked(page));
  294. VM_BUG_ON(PageUptodate(page));
  295. if (frontswap_load(page) == 0) {
  296. SetPageUptodate(page);
  297. unlock_page(page);
  298. goto out;
  299. }
  300. if (sis->flags & SWP_FILE) {
  301. struct file *swap_file = sis->swap_file;
  302. struct address_space *mapping = swap_file->f_mapping;
  303. ret = mapping->a_ops->readpage(swap_file, page);
  304. if (!ret)
  305. count_vm_event(PSWPIN);
  306. return ret;
  307. }
  308. bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
  309. if (bio == NULL) {
  310. unlock_page(page);
  311. ret = -ENOMEM;
  312. goto out;
  313. }
  314. count_vm_event(PSWPIN);
  315. submit_bio(READ, bio);
  316. out:
  317. return ret;
  318. }
  319. int swap_set_page_dirty(struct page *page)
  320. {
  321. struct swap_info_struct *sis = page_swap_info(page);
  322. if (sis->flags & SWP_FILE) {
  323. struct address_space *mapping = sis->swap_file->f_mapping;
  324. return mapping->a_ops->set_page_dirty(page);
  325. } else {
  326. return __set_page_dirty_no_writeback(page);
  327. }
  328. }