block: fix max discard sectors limit
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / mm / page_io.c
CommitLineData
1da177e4
LT
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>
5a0e3ad6 15#include <linux/gfp.h>
1da177e4
LT
16#include <linux/pagemap.h>
17#include <linux/swap.h>
18#include <linux/bio.h>
19#include <linux/swapops.h>
62c230bc 20#include <linux/buffer_head.h>
1da177e4 21#include <linux/writeback.h>
38b5faf4 22#include <linux/frontswap.h>
1da177e4
LT
23#include <asm/pgtable.h>
24
f29ad6a9 25static struct bio *get_swap_bio(gfp_t gfp_flags,
1da177e4
LT
26 struct page *page, bio_end_io_t end_io)
27{
28 struct bio *bio;
29
30 bio = bio_alloc(gfp_flags, 1);
31 if (bio) {
d4906e1a 32 bio->bi_sector = map_swap_page(page, &bio->bi_bdev);
f29ad6a9 33 bio->bi_sector <<= PAGE_SHIFT - 9;
1da177e4
LT
34 bio->bi_io_vec[0].bv_page = page;
35 bio->bi_io_vec[0].bv_len = PAGE_SIZE;
36 bio->bi_io_vec[0].bv_offset = 0;
37 bio->bi_vcnt = 1;
1da177e4
LT
38 bio->bi_size = PAGE_SIZE;
39 bio->bi_end_io = end_io;
40 }
41 return bio;
42}
43
6712ecf8 44static void end_swap_bio_write(struct bio *bio, int err)
1da177e4
LT
45{
46 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
47 struct page *page = bio->bi_io_vec[0].bv_page;
48
6ddab3b9 49 if (!uptodate) {
1da177e4 50 SetPageError(page);
6ddab3b9
PZ
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 }
1da177e4
LT
66 end_page_writeback(page);
67 bio_put(bio);
1da177e4
LT
68}
69
6712ecf8 70void end_swap_bio_read(struct bio *bio, int err)
1da177e4
LT
71{
72 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
73 struct page *page = bio->bi_io_vec[0].bv_page;
74
1da177e4
LT
75 if (!uptodate) {
76 SetPageError(page);
77 ClearPageUptodate(page);
6ddab3b9
PZ
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);
1da177e4
LT
82 } else {
83 SetPageUptodate(page);
84 }
85 unlock_page(page);
86 bio_put(bio);
1da177e4
LT
87}
88
a509bc1a
MG
89int generic_swapfile_activate(struct swap_info_struct *sis,
90 struct file *swap_file,
91 sector_t *span)
92{
93 struct address_space *mapping = swap_file->f_mapping;
94 struct inode *inode = mapping->host;
95 unsigned blocks_per_page;
96 unsigned long page_no;
97 unsigned blkbits;
98 sector_t probe_block;
99 sector_t last_block;
100 sector_t lowest_block = -1;
101 sector_t highest_block = 0;
102 int nr_extents = 0;
103 int ret;
104
105 blkbits = inode->i_blkbits;
106 blocks_per_page = PAGE_SIZE >> blkbits;
107
108 /*
109 * Map all the blocks into the extent list. This code doesn't try
110 * to be very smart.
111 */
112 probe_block = 0;
113 page_no = 0;
114 last_block = i_size_read(inode) >> blkbits;
115 while ((probe_block + blocks_per_page) <= last_block &&
116 page_no < sis->max) {
117 unsigned block_in_page;
118 sector_t first_block;
119
120 first_block = bmap(inode, probe_block);
121 if (first_block == 0)
122 goto bad_bmap;
123
124 /*
125 * It must be PAGE_SIZE aligned on-disk
126 */
127 if (first_block & (blocks_per_page - 1)) {
128 probe_block++;
129 goto reprobe;
130 }
131
132 for (block_in_page = 1; block_in_page < blocks_per_page;
133 block_in_page++) {
134 sector_t block;
135
136 block = bmap(inode, probe_block + block_in_page);
137 if (block == 0)
138 goto bad_bmap;
139 if (block != first_block + block_in_page) {
140 /* Discontiguity */
141 probe_block++;
142 goto reprobe;
143 }
144 }
145
146 first_block >>= (PAGE_SHIFT - blkbits);
147 if (page_no) { /* exclude the header page */
148 if (first_block < lowest_block)
149 lowest_block = first_block;
150 if (first_block > highest_block)
151 highest_block = first_block;
152 }
153
154 /*
155 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
156 */
157 ret = add_swap_extent(sis, page_no, 1, first_block);
158 if (ret < 0)
159 goto out;
160 nr_extents += ret;
161 page_no++;
162 probe_block += blocks_per_page;
163reprobe:
164 continue;
165 }
166 ret = nr_extents;
167 *span = 1 + highest_block - lowest_block;
168 if (page_no == 0)
169 page_no = 1; /* force Empty message */
170 sis->max = page_no;
171 sis->pages = page_no - 1;
172 sis->highest_bit = page_no - 1;
173out:
174 return ret;
175bad_bmap:
176 printk(KERN_ERR "swapon: swapfile has holes\n");
177 ret = -EINVAL;
178 goto out;
179}
180
1da177e4
LT
181/*
182 * We may have stale swap cache pages in memory: notice
183 * them here and get rid of the unnecessary final write.
184 */
185int swap_writepage(struct page *page, struct writeback_control *wbc)
186{
187 struct bio *bio;
188 int ret = 0, rw = WRITE;
62c230bc 189 struct swap_info_struct *sis = page_swap_info(page);
1da177e4 190
a2c43eed 191 if (try_to_free_swap(page)) {
1da177e4
LT
192 unlock_page(page);
193 goto out;
194 }
165c8aed 195 if (frontswap_store(page) == 0) {
38b5faf4
DM
196 set_page_writeback(page);
197 unlock_page(page);
198 end_page_writeback(page);
199 goto out;
200 }
62c230bc
MG
201
202 if (sis->flags & SWP_FILE) {
203 struct kiocb kiocb;
204 struct file *swap_file = sis->swap_file;
205 struct address_space *mapping = swap_file->f_mapping;
206 struct iovec iov = {
5a178119 207 .iov_base = kmap(page),
62c230bc
MG
208 .iov_len = PAGE_SIZE,
209 };
210
211 init_sync_kiocb(&kiocb, swap_file);
212 kiocb.ki_pos = page_file_offset(page);
213 kiocb.ki_left = PAGE_SIZE;
214 kiocb.ki_nbytes = PAGE_SIZE;
215
216 unlock_page(page);
217 ret = mapping->a_ops->direct_IO(KERNEL_WRITE,
218 &kiocb, &iov,
219 kiocb.ki_pos, 1);
5a178119 220 kunmap(page);
62c230bc
MG
221 if (ret == PAGE_SIZE) {
222 count_vm_event(PSWPOUT);
223 ret = 0;
224 }
225 return ret;
226 }
227
f29ad6a9 228 bio = get_swap_bio(GFP_NOIO, page, end_swap_bio_write);
1da177e4
LT
229 if (bio == NULL) {
230 set_page_dirty(page);
231 unlock_page(page);
232 ret = -ENOMEM;
233 goto out;
234 }
235 if (wbc->sync_mode == WB_SYNC_ALL)
721a9602 236 rw |= REQ_SYNC;
f8891e5e 237 count_vm_event(PSWPOUT);
1da177e4
LT
238 set_page_writeback(page);
239 unlock_page(page);
240 submit_bio(rw, bio);
241out:
242 return ret;
243}
244
aca8bf32 245int swap_readpage(struct page *page)
1da177e4
LT
246{
247 struct bio *bio;
248 int ret = 0;
62c230bc 249 struct swap_info_struct *sis = page_swap_info(page);
1da177e4 250
51726b12
HD
251 VM_BUG_ON(!PageLocked(page));
252 VM_BUG_ON(PageUptodate(page));
165c8aed 253 if (frontswap_load(page) == 0) {
38b5faf4
DM
254 SetPageUptodate(page);
255 unlock_page(page);
256 goto out;
257 }
62c230bc
MG
258
259 if (sis->flags & SWP_FILE) {
260 struct file *swap_file = sis->swap_file;
261 struct address_space *mapping = swap_file->f_mapping;
262
263 ret = mapping->a_ops->readpage(swap_file, page);
264 if (!ret)
265 count_vm_event(PSWPIN);
266 return ret;
267 }
268
f29ad6a9 269 bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
1da177e4
LT
270 if (bio == NULL) {
271 unlock_page(page);
272 ret = -ENOMEM;
273 goto out;
274 }
f8891e5e 275 count_vm_event(PSWPIN);
1da177e4
LT
276 submit_bio(READ, bio);
277out:
278 return ret;
279}
62c230bc
MG
280
281int swap_set_page_dirty(struct page *page)
282{
283 struct swap_info_struct *sis = page_swap_info(page);
284
285 if (sis->flags & SWP_FILE) {
286 struct address_space *mapping = sis->swap_file->f_mapping;
287 return mapping->a_ops->set_page_dirty(page);
288 } else {
289 return __set_page_dirty_no_writeback(page);
290 }
291}