blob: a19af956ee1bb6f55d87df3a3905a486820a49a7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
13#include <linux/mm.h>
14#include <linux/kernel_stat.h>
15#include <linux/pagemap.h>
16#include <linux/swap.h>
17#include <linux/bio.h>
18#include <linux/swapops.h>
19#include <linux/writeback.h>
20#include <asm/pgtable.h>
21
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -080022static struct bio *get_swap_bio(gfp_t gfp_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 struct page *page, bio_end_io_t end_io)
24{
25 struct bio *bio;
26
27 bio = bio_alloc(gfp_flags, 1);
28 if (bio) {
Lee Schermerhornd4906e12009-12-14 17:58:49 -080029 bio->bi_sector = map_swap_page(page, &bio->bi_bdev);
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -080030 bio->bi_sector <<= PAGE_SHIFT - 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 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
NeilBrown6712ecf2007-09-27 12:47:43 +020042static void end_swap_bio_write(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
45 struct page *page = bio->bi_io_vec[0].bv_page;
46
Peter Zijlstra6ddab3b2006-09-25 23:31:26 -070047 if (!uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 SetPageError(page);
Peter Zijlstra6ddab3b2006-09-25 23:31:26 -070049 /*
50 * We failed to write the page out to swap-space.
51 * Re-dirty the page in order to avoid it being reclaimed.
52 * Also print a dire warning that things will go BAD (tm)
53 * very quickly.
54 *
55 * Also clear PG_reclaim to avoid rotate_reclaimable_page()
56 */
57 set_page_dirty(page);
58 printk(KERN_ALERT "Write-error on swap-device (%u:%u:%Lu)\n",
59 imajor(bio->bi_bdev->bd_inode),
60 iminor(bio->bi_bdev->bd_inode),
61 (unsigned long long)bio->bi_sector);
62 ClearPageReclaim(page);
63 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 end_page_writeback(page);
65 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
NeilBrown6712ecf2007-09-27 12:47:43 +020068void end_swap_bio_read(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
71 struct page *page = bio->bi_io_vec[0].bv_page;
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (!uptodate) {
74 SetPageError(page);
75 ClearPageUptodate(page);
Peter Zijlstra6ddab3b2006-09-25 23:31:26 -070076 printk(KERN_ALERT "Read-error on swap-device (%u:%u:%Lu)\n",
77 imajor(bio->bi_bdev->bd_inode),
78 iminor(bio->bi_bdev->bd_inode),
79 (unsigned long long)bio->bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 } else {
81 SetPageUptodate(page);
82 }
83 unlock_page(page);
84 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87/*
88 * We may have stale swap cache pages in memory: notice
89 * them here and get rid of the unnecessary final write.
90 */
91int swap_writepage(struct page *page, struct writeback_control *wbc)
92{
93 struct bio *bio;
94 int ret = 0, rw = WRITE;
95
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -080096 if (try_to_free_swap(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 unlock_page(page);
98 goto out;
99 }
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -0800100 bio = get_swap_bio(GFP_NOIO, page, end_swap_bio_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (bio == NULL) {
102 set_page_dirty(page);
103 unlock_page(page);
104 ret = -ENOMEM;
105 goto out;
106 }
107 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe93dbb392009-02-16 10:25:40 +0100108 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700109 count_vm_event(PSWPOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 set_page_writeback(page);
111 unlock_page(page);
112 submit_bio(rw, bio);
113out:
114 return ret;
115}
116
Minchan Kimaca8bf32009-06-16 15:33:02 -0700117int swap_readpage(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 struct bio *bio;
120 int ret = 0;
121
Hugh Dickins51726b12009-01-06 14:39:25 -0800122 VM_BUG_ON(!PageLocked(page));
123 VM_BUG_ON(PageUptodate(page));
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -0800124 bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (bio == NULL) {
126 unlock_page(page);
127 ret = -ENOMEM;
128 goto out;
129 }
Christoph Lameterf8891e52006-06-30 01:55:45 -0700130 count_vm_event(PSWPIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 submit_bio(READ, bio);
132out:
133 return ret;
134}