blob: 307a3e795290159caab708a39a3915c934cb2cf7 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/pagemap.h>
17#include <linux/swap.h>
18#include <linux/bio.h>
19#include <linux/swapops.h>
Mel Gorman62c230b2012-07-31 16:44:55 -070020#include <linux/buffer_head.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/writeback.h>
Dan Magenheimer38b5faf2012-04-09 17:08:06 -060022#include <linux/frontswap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/pgtable.h>
24
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -080025static struct bio *get_swap_bio(gfp_t gfp_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 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) {
Lee Schermerhornd4906e12009-12-14 17:58:49 -080032 bio->bi_sector = map_swap_page(page, &bio->bi_bdev);
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -080033 bio->bi_sector <<= PAGE_SHIFT - 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 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;
38 bio->bi_idx = 0;
39 bio->bi_size = PAGE_SIZE;
40 bio->bi_end_io = end_io;
41 }
42 return bio;
43}
44
NeilBrown6712ecf2007-09-27 12:47:43 +020045static void end_swap_bio_write(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
48 struct page *page = bio->bi_io_vec[0].bv_page;
49
Peter Zijlstra6ddab3b2006-09-25 23:31:26 -070050 if (!uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 SetPageError(page);
Peter Zijlstra6ddab3b2006-09-25 23:31:26 -070052 /*
53 * We failed to write the page out to swap-space.
54 * Re-dirty the page in order to avoid it being reclaimed.
55 * Also print a dire warning that things will go BAD (tm)
56 * very quickly.
57 *
58 * Also clear PG_reclaim to avoid rotate_reclaimable_page()
59 */
60 set_page_dirty(page);
61 printk(KERN_ALERT "Write-error on swap-device (%u:%u:%Lu)\n",
62 imajor(bio->bi_bdev->bd_inode),
63 iminor(bio->bi_bdev->bd_inode),
64 (unsigned long long)bio->bi_sector);
65 ClearPageReclaim(page);
66 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 end_page_writeback(page);
68 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
NeilBrown6712ecf2007-09-27 12:47:43 +020071void end_swap_bio_read(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
74 struct page *page = bio->bi_io_vec[0].bv_page;
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (!uptodate) {
77 SetPageError(page);
78 ClearPageUptodate(page);
Peter Zijlstra6ddab3b2006-09-25 23:31:26 -070079 printk(KERN_ALERT "Read-error on swap-device (%u:%u:%Lu)\n",
80 imajor(bio->bi_bdev->bd_inode),
81 iminor(bio->bi_bdev->bd_inode),
82 (unsigned long long)bio->bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 } else {
84 SetPageUptodate(page);
85 }
86 unlock_page(page);
87 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90/*
91 * We may have stale swap cache pages in memory: notice
92 * them here and get rid of the unnecessary final write.
93 */
94int swap_writepage(struct page *page, struct writeback_control *wbc)
95{
96 struct bio *bio;
97 int ret = 0, rw = WRITE;
Mel Gorman62c230b2012-07-31 16:44:55 -070098 struct swap_info_struct *sis = page_swap_info(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800100 if (try_to_free_swap(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 unlock_page(page);
102 goto out;
103 }
Konrad Rzeszutek Wilk165c8ae2012-05-15 11:32:15 -0400104 if (frontswap_store(page) == 0) {
Dan Magenheimer38b5faf2012-04-09 17:08:06 -0600105 set_page_writeback(page);
106 unlock_page(page);
107 end_page_writeback(page);
108 goto out;
109 }
Mel Gorman62c230b2012-07-31 16:44:55 -0700110
111 if (sis->flags & SWP_FILE) {
112 struct kiocb kiocb;
113 struct file *swap_file = sis->swap_file;
114 struct address_space *mapping = swap_file->f_mapping;
115 struct iovec iov = {
116 .iov_base = page_address(page),
117 .iov_len = PAGE_SIZE,
118 };
119
120 init_sync_kiocb(&kiocb, swap_file);
121 kiocb.ki_pos = page_file_offset(page);
122 kiocb.ki_left = PAGE_SIZE;
123 kiocb.ki_nbytes = PAGE_SIZE;
124
125 unlock_page(page);
126 ret = mapping->a_ops->direct_IO(KERNEL_WRITE,
127 &kiocb, &iov,
128 kiocb.ki_pos, 1);
129 if (ret == PAGE_SIZE) {
130 count_vm_event(PSWPOUT);
131 ret = 0;
132 }
133 return ret;
134 }
135
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -0800136 bio = get_swap_bio(GFP_NOIO, page, end_swap_bio_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (bio == NULL) {
138 set_page_dirty(page);
139 unlock_page(page);
140 ret = -ENOMEM;
141 goto out;
142 }
143 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +0100144 rw |= REQ_SYNC;
Christoph Lameterf8891e52006-06-30 01:55:45 -0700145 count_vm_event(PSWPOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 set_page_writeback(page);
147 unlock_page(page);
148 submit_bio(rw, bio);
149out:
150 return ret;
151}
152
Minchan Kimaca8bf32009-06-16 15:33:02 -0700153int swap_readpage(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 struct bio *bio;
156 int ret = 0;
Mel Gorman62c230b2012-07-31 16:44:55 -0700157 struct swap_info_struct *sis = page_swap_info(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Hugh Dickins51726b12009-01-06 14:39:25 -0800159 VM_BUG_ON(!PageLocked(page));
160 VM_BUG_ON(PageUptodate(page));
Konrad Rzeszutek Wilk165c8ae2012-05-15 11:32:15 -0400161 if (frontswap_load(page) == 0) {
Dan Magenheimer38b5faf2012-04-09 17:08:06 -0600162 SetPageUptodate(page);
163 unlock_page(page);
164 goto out;
165 }
Mel Gorman62c230b2012-07-31 16:44:55 -0700166
167 if (sis->flags & SWP_FILE) {
168 struct file *swap_file = sis->swap_file;
169 struct address_space *mapping = swap_file->f_mapping;
170
171 ret = mapping->a_ops->readpage(swap_file, page);
172 if (!ret)
173 count_vm_event(PSWPIN);
174 return ret;
175 }
176
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -0800177 bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (bio == NULL) {
179 unlock_page(page);
180 ret = -ENOMEM;
181 goto out;
182 }
Christoph Lameterf8891e52006-06-30 01:55:45 -0700183 count_vm_event(PSWPIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 submit_bio(READ, bio);
185out:
186 return ret;
187}
Mel Gorman62c230b2012-07-31 16:44:55 -0700188
189int swap_set_page_dirty(struct page *page)
190{
191 struct swap_info_struct *sis = page_swap_info(page);
192
193 if (sis->flags & SWP_FILE) {
194 struct address_space *mapping = sis->swap_file->f_mapping;
195 return mapping->a_ops->set_page_dirty(page);
196 } else {
197 return __set_page_dirty_no_writeback(page);
198 }
199}