disable kprobes until it compiles with the current config
[linux-flexiantxendom0-3.2.10.git] / mm / page_io.c
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
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/buffer_head.h>  /* for block_sync_page() */
20 #include <linux/mpage.h>
21 #include <linux/writeback.h>
22 #include <asm/pgtable.h>
23
24 static struct bio *
25 get_swap_bio(int gfp_flags, struct page *page, bio_end_io_t end_io)
26 {
27         struct bio *bio;
28
29         bio = bio_alloc(gfp_flags, 1);
30         if (bio) {
31                 struct swap_info_struct *sis;
32                 swp_entry_t entry;
33
34                 BUG_ON(!PageSwapCache(page));
35                 entry.val = page->private;
36                 sis = get_swap_info_struct(swp_type(entry));
37
38                 bio->bi_sector = map_swap_page(sis, swp_offset(entry)) *
39                                         (PAGE_SIZE >> 9);
40                 bio->bi_bdev = sis->bdev;
41                 bio->bi_io_vec[0].bv_page = page;
42                 bio->bi_io_vec[0].bv_len = PAGE_SIZE;
43                 bio->bi_io_vec[0].bv_offset = 0;
44                 bio->bi_vcnt = 1;
45                 bio->bi_idx = 0;
46                 bio->bi_size = PAGE_SIZE;
47                 bio->bi_end_io = end_io;
48         }
49         return bio;
50 }
51
52 static int end_swap_bio_write(struct bio *bio, unsigned int bytes_done, int err)
53 {
54         const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
55         struct page *page = bio->bi_io_vec[0].bv_page;
56
57         if (bio->bi_size)
58                 return 1;
59
60         if (!uptodate)
61                 SetPageError(page);
62         end_page_writeback(page);
63         bio_put(bio);
64         return 0;
65 }
66
67 static int end_swap_bio_read(struct bio *bio, unsigned int bytes_done, int err)
68 {
69         const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
70         struct page *page = bio->bi_io_vec[0].bv_page;
71
72         if (bio->bi_size)
73                 return 1;
74
75         if (!uptodate) {
76                 SetPageError(page);
77                 ClearPageUptodate(page);
78         } else {
79                 SetPageUptodate(page);
80         }
81         unlock_page(page);
82         bio_put(bio);
83         return 0;
84 }
85
86 /*
87  * We may have stale swap cache pages in memory: notice
88  * them here and get rid of the unnecessary final write.
89  */
90 int swap_writepage(struct page *page, struct writeback_control *wbc)
91 {
92         struct bio *bio;
93         int ret = 0;
94
95         if (remove_exclusive_swap_page(page)) {
96                 unlock_page(page);
97                 goto out;
98         }
99         bio = get_swap_bio(GFP_NOIO, page, end_swap_bio_write);
100         if (bio == NULL) {
101                 set_page_dirty(page);
102                 unlock_page(page);
103                 ret = -ENOMEM;
104                 goto out;
105         }
106         inc_page_state(pswpout);
107         set_page_writeback(page);
108         unlock_page(page);
109         submit_bio(WRITE, bio);
110 out:
111         return ret;
112 }
113
114 int swap_readpage(struct file *file, struct page *page)
115 {
116         struct bio *bio;
117         int ret = 0;
118
119         BUG_ON(!PageLocked(page));
120         ClearPageUptodate(page);
121         bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
122         if (bio == NULL) {
123                 unlock_page(page);
124                 ret = -ENOMEM;
125                 goto out;
126         }
127         inc_page_state(pswpin);
128         submit_bio(READ, bio);
129 out:
130         return ret;
131 }
132
133 struct address_space_operations swap_aops = {
134         .writepage      = swap_writepage,
135         .readpage       = swap_readpage,
136         .sync_page      = block_sync_page,
137         .set_page_dirty = __set_page_dirty_nobuffers,
138 };
139
140 /*
141  * A scruffy utility function to read or write an arbitrary swap page
142  * and wait on the I/O.
143  */
144 int rw_swap_page_sync(int rw, swp_entry_t entry, struct page *page)
145 {
146         int ret;
147         struct writeback_control swap_wbc = {
148                 .sync_mode = WB_SYNC_ALL,
149         };
150
151         lock_page(page);
152
153         BUG_ON(page_mapping(page));
154         SetPageSwapCache(page);
155         page->private = entry.val;
156
157         if (rw == READ) {
158                 ret = swap_readpage(NULL, page);
159                 wait_on_page_locked(page);
160         } else {
161                 ret = swap_writepage(page, &swap_wbc);
162                 wait_on_page_writeback(page);
163         }
164         ClearPageSwapCache(page);
165         if (ret == 0 && (!PageUptodate(page) || PageError(page)))
166                 ret = -EIO;
167         return ret;
168 }