- Merge 2.6.37-rc3-git6 with Xen.
[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/gfp.h>
16 #include <linux/pagemap.h>
17 #include <linux/swap.h>
18 #include <linux/bio.h>
19 #include <linux/swapops.h>
20 #include <linux/buffer_head.h>
21 #include <linux/writeback.h>
22 #include <asm/pgtable.h>
23
24 static struct bio *get_swap_bio(gfp_t gfp_flags,
25                                 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                 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_idx = 0;
38                 bio->bi_size = PAGE_SIZE;
39                 bio->bi_end_io = end_io;
40         }
41         return bio;
42 }
43
44 static void end_swap_bio_write(struct bio *bio, int err)
45 {
46         const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
47         struct page *page = bio->bi_io_vec[0].bv_page;
48
49         if (!uptodate) {
50                 SetPageError(page);
51                 /*
52                  * We failed to write the page out to swap-space.
53                  * Re-dirty the page in order to avoid it being reclaimed.
54                  * Also print a dire warning that things will go BAD (tm)
55                  * very quickly.
56                  *
57                  * Also clear PG_reclaim to avoid rotate_reclaimable_page()
58                  */
59                 set_page_dirty(page);
60                 printk(KERN_ALERT "Write-error on swap-device (%u:%u:%Lu)\n",
61                                 imajor(bio->bi_bdev->bd_inode),
62                                 iminor(bio->bi_bdev->bd_inode),
63                                 (unsigned long long)bio->bi_sector);
64                 ClearPageReclaim(page);
65         }
66         end_page_writeback(page);
67         bio_put(bio);
68 }
69
70 void end_swap_bio_read(struct bio *bio, int err)
71 {
72         const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
73         struct page *page = bio->bi_io_vec[0].bv_page;
74
75         if (!uptodate) {
76                 SetPageError(page);
77                 ClearPageUptodate(page);
78                 printk(KERN_ALERT "Read-error on swap-device (%u:%u:%Lu)\n",
79                                 imajor(bio->bi_bdev->bd_inode),
80                                 iminor(bio->bi_bdev->bd_inode),
81                                 (unsigned long long)bio->bi_sector);
82         } else {
83                 SetPageUptodate(page);
84         }
85         unlock_page(page);
86         bio_put(bio);
87 }
88
89 /*
90  * We may have stale swap cache pages in memory: notice
91  * them here and get rid of the unnecessary final write.
92  */
93 int swap_writepage(struct page *page, struct writeback_control *wbc)
94 {
95         struct bio *bio;
96         int ret = 0, rw = WRITE;
97         struct swap_info_struct *sis = page_swap_info(page);
98
99         if (try_to_free_swap(page)) {
100                 unlock_page(page);
101                 goto out;
102         }
103
104         if (sis->flags & SWP_FILE) {
105                 struct file *swap_file = sis->swap_file;
106                 struct address_space *mapping = swap_file->f_mapping;
107
108                 ret = mapping->a_ops->swap_out(swap_file, page, wbc);
109                 if (!ret)
110                         count_vm_event(PSWPOUT);
111                 return ret;
112         }
113
114         if (preswap_put(page) == 1) {
115                 set_page_writeback(page);
116                 unlock_page(page);
117                 end_page_writeback(page);
118                 goto out;
119         }
120
121         bio = get_swap_bio(GFP_NOIO, page, end_swap_bio_write);
122         if (bio == NULL) {
123                 set_page_dirty(page);
124                 unlock_page(page);
125                 ret = -ENOMEM;
126                 goto out;
127         }
128         if (wbc->sync_mode == WB_SYNC_ALL)
129                 rw |= REQ_SYNC | REQ_UNPLUG;
130         count_vm_event(PSWPOUT);
131         set_page_writeback(page);
132         unlock_page(page);
133         submit_bio(rw, bio);
134 out:
135         return ret;
136 }
137
138 /* this comment ensure the patch applies to swap_sync_page
139  * and not swap_set_page_dirty by mistake
140  */
141 void swap_sync_page(struct page *page)
142 {
143         struct swap_info_struct *sis = page_swap_info(page);
144
145         if (!sis)
146                 return;
147         if (sis->flags & SWP_FILE) {
148                 struct address_space *mapping = sis->swap_file->f_mapping;
149
150                 if (mapping->a_ops->sync_page)
151                         mapping->a_ops->sync_page(page);
152         } else {
153                 block_sync_page(page);
154         }
155 }
156
157 int swap_set_page_dirty(struct page *page)
158 {
159         struct swap_info_struct *sis = page_swap_info(page);
160
161         if (sis->flags & SWP_FILE) {
162                 struct address_space *mapping = sis->swap_file->f_mapping;
163
164                 return mapping->a_ops->set_page_dirty(page);
165         } else {
166                 return __set_page_dirty_nobuffers(page);
167         }
168 }
169
170 int swap_readpage(struct page *page)
171 {
172         struct bio *bio;
173         int ret = 0;
174         struct swap_info_struct *sis = page_swap_info(page);
175
176         VM_BUG_ON(!PageLocked(page));
177         VM_BUG_ON(PageUptodate(page));
178
179         if (sis->flags & SWP_FILE) {
180                 struct file *swap_file = sis->swap_file;
181                 struct address_space *mapping = swap_file->f_mapping;
182
183                 ret = mapping->a_ops->swap_in(swap_file, page);
184                 if (!ret)
185                         count_vm_event(PSWPIN);
186                 return ret;
187         }
188
189         if (preswap_get(page) == 1) {
190                 SetPageUptodate(page);
191                 unlock_page(page);
192                 goto out;
193         }
194
195         bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
196         if (bio == NULL) {
197                 unlock_page(page);
198                 ret = -ENOMEM;
199                 goto out;
200         }
201         count_vm_event(PSWPIN);
202         submit_bio(READ, bio);
203 out:
204         return ret;
205 }