Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/pagemap.h> |
| 17 | #include <linux/swap.h> |
| 18 | #include <linux/bio.h> |
| 19 | #include <linux/swapops.h> |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 20 | #include <linux/buffer_head.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/writeback.h> |
Dan Magenheimer | 38b5faf | 2012-04-09 17:08:06 -0600 | [diff] [blame] | 22 | #include <linux/frontswap.h> |
Kent Overstreet | a27bb33 | 2013-05-07 16:19:08 -0700 | [diff] [blame^] | 23 | #include <linux/aio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <asm/pgtable.h> |
| 25 | |
Hugh Dickins | f29ad6a | 2009-12-14 17:58:40 -0800 | [diff] [blame] | 26 | static struct bio *get_swap_bio(gfp_t gfp_flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | struct page *page, bio_end_io_t end_io) |
| 28 | { |
| 29 | struct bio *bio; |
| 30 | |
| 31 | bio = bio_alloc(gfp_flags, 1); |
| 32 | if (bio) { |
Lee Schermerhorn | d4906e1 | 2009-12-14 17:58:49 -0800 | [diff] [blame] | 33 | bio->bi_sector = map_swap_page(page, &bio->bi_bdev); |
Hugh Dickins | f29ad6a | 2009-12-14 17:58:40 -0800 | [diff] [blame] | 34 | bio->bi_sector <<= PAGE_SHIFT - 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | bio->bi_io_vec[0].bv_page = page; |
| 36 | bio->bi_io_vec[0].bv_len = PAGE_SIZE; |
| 37 | bio->bi_io_vec[0].bv_offset = 0; |
| 38 | bio->bi_vcnt = 1; |
| 39 | bio->bi_idx = 0; |
| 40 | bio->bi_size = PAGE_SIZE; |
| 41 | bio->bi_end_io = end_io; |
| 42 | } |
| 43 | return bio; |
| 44 | } |
| 45 | |
Seth Jennings | 1eec670 | 2013-04-29 15:08:35 -0700 | [diff] [blame] | 46 | void end_swap_bio_write(struct bio *bio, int err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
| 48 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 49 | struct page *page = bio->bi_io_vec[0].bv_page; |
| 50 | |
Peter Zijlstra | 6ddab3b | 2006-09-25 23:31:26 -0700 | [diff] [blame] | 51 | if (!uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | SetPageError(page); |
Peter Zijlstra | 6ddab3b | 2006-09-25 23:31:26 -0700 | [diff] [blame] | 53 | /* |
| 54 | * We failed to write the page out to swap-space. |
| 55 | * Re-dirty the page in order to avoid it being reclaimed. |
| 56 | * Also print a dire warning that things will go BAD (tm) |
| 57 | * very quickly. |
| 58 | * |
| 59 | * Also clear PG_reclaim to avoid rotate_reclaimable_page() |
| 60 | */ |
| 61 | set_page_dirty(page); |
| 62 | printk(KERN_ALERT "Write-error on swap-device (%u:%u:%Lu)\n", |
| 63 | imajor(bio->bi_bdev->bd_inode), |
| 64 | iminor(bio->bi_bdev->bd_inode), |
| 65 | (unsigned long long)bio->bi_sector); |
| 66 | ClearPageReclaim(page); |
| 67 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | end_page_writeback(page); |
| 69 | bio_put(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 72 | void end_swap_bio_read(struct bio *bio, int err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { |
| 74 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 75 | struct page *page = bio->bi_io_vec[0].bv_page; |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | if (!uptodate) { |
| 78 | SetPageError(page); |
| 79 | ClearPageUptodate(page); |
Peter Zijlstra | 6ddab3b | 2006-09-25 23:31:26 -0700 | [diff] [blame] | 80 | printk(KERN_ALERT "Read-error on swap-device (%u:%u:%Lu)\n", |
| 81 | imajor(bio->bi_bdev->bd_inode), |
| 82 | iminor(bio->bi_bdev->bd_inode), |
| 83 | (unsigned long long)bio->bi_sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } else { |
| 85 | SetPageUptodate(page); |
| 86 | } |
| 87 | unlock_page(page); |
| 88 | bio_put(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Mel Gorman | a509bc1 | 2012-07-31 16:44:57 -0700 | [diff] [blame] | 91 | int generic_swapfile_activate(struct swap_info_struct *sis, |
| 92 | struct file *swap_file, |
| 93 | sector_t *span) |
| 94 | { |
| 95 | struct address_space *mapping = swap_file->f_mapping; |
| 96 | struct inode *inode = mapping->host; |
| 97 | unsigned blocks_per_page; |
| 98 | unsigned long page_no; |
| 99 | unsigned blkbits; |
| 100 | sector_t probe_block; |
| 101 | sector_t last_block; |
| 102 | sector_t lowest_block = -1; |
| 103 | sector_t highest_block = 0; |
| 104 | int nr_extents = 0; |
| 105 | int ret; |
| 106 | |
| 107 | blkbits = inode->i_blkbits; |
| 108 | blocks_per_page = PAGE_SIZE >> blkbits; |
| 109 | |
| 110 | /* |
| 111 | * Map all the blocks into the extent list. This code doesn't try |
| 112 | * to be very smart. |
| 113 | */ |
| 114 | probe_block = 0; |
| 115 | page_no = 0; |
| 116 | last_block = i_size_read(inode) >> blkbits; |
| 117 | while ((probe_block + blocks_per_page) <= last_block && |
| 118 | page_no < sis->max) { |
| 119 | unsigned block_in_page; |
| 120 | sector_t first_block; |
| 121 | |
| 122 | first_block = bmap(inode, probe_block); |
| 123 | if (first_block == 0) |
| 124 | goto bad_bmap; |
| 125 | |
| 126 | /* |
| 127 | * It must be PAGE_SIZE aligned on-disk |
| 128 | */ |
| 129 | if (first_block & (blocks_per_page - 1)) { |
| 130 | probe_block++; |
| 131 | goto reprobe; |
| 132 | } |
| 133 | |
| 134 | for (block_in_page = 1; block_in_page < blocks_per_page; |
| 135 | block_in_page++) { |
| 136 | sector_t block; |
| 137 | |
| 138 | block = bmap(inode, probe_block + block_in_page); |
| 139 | if (block == 0) |
| 140 | goto bad_bmap; |
| 141 | if (block != first_block + block_in_page) { |
| 142 | /* Discontiguity */ |
| 143 | probe_block++; |
| 144 | goto reprobe; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | first_block >>= (PAGE_SHIFT - blkbits); |
| 149 | if (page_no) { /* exclude the header page */ |
| 150 | if (first_block < lowest_block) |
| 151 | lowest_block = first_block; |
| 152 | if (first_block > highest_block) |
| 153 | highest_block = first_block; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks |
| 158 | */ |
| 159 | ret = add_swap_extent(sis, page_no, 1, first_block); |
| 160 | if (ret < 0) |
| 161 | goto out; |
| 162 | nr_extents += ret; |
| 163 | page_no++; |
| 164 | probe_block += blocks_per_page; |
| 165 | reprobe: |
| 166 | continue; |
| 167 | } |
| 168 | ret = nr_extents; |
| 169 | *span = 1 + highest_block - lowest_block; |
| 170 | if (page_no == 0) |
| 171 | page_no = 1; /* force Empty message */ |
| 172 | sis->max = page_no; |
| 173 | sis->pages = page_no - 1; |
| 174 | sis->highest_bit = page_no - 1; |
| 175 | out: |
| 176 | return ret; |
| 177 | bad_bmap: |
| 178 | printk(KERN_ERR "swapon: swapfile has holes\n"); |
| 179 | ret = -EINVAL; |
| 180 | goto out; |
| 181 | } |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | /* |
| 184 | * We may have stale swap cache pages in memory: notice |
| 185 | * them here and get rid of the unnecessary final write. |
| 186 | */ |
| 187 | int swap_writepage(struct page *page, struct writeback_control *wbc) |
| 188 | { |
Seth Jennings | 2f772e6 | 2013-04-29 15:08:34 -0700 | [diff] [blame] | 189 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Hugh Dickins | a2c43ee | 2009-01-06 14:39:36 -0800 | [diff] [blame] | 191 | if (try_to_free_swap(page)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | unlock_page(page); |
| 193 | goto out; |
| 194 | } |
Konrad Rzeszutek Wilk | 165c8ae | 2012-05-15 11:32:15 -0400 | [diff] [blame] | 195 | if (frontswap_store(page) == 0) { |
Dan Magenheimer | 38b5faf | 2012-04-09 17:08:06 -0600 | [diff] [blame] | 196 | set_page_writeback(page); |
| 197 | unlock_page(page); |
| 198 | end_page_writeback(page); |
| 199 | goto out; |
| 200 | } |
Seth Jennings | 1eec670 | 2013-04-29 15:08:35 -0700 | [diff] [blame] | 201 | ret = __swap_writepage(page, wbc, end_swap_bio_write); |
Seth Jennings | 2f772e6 | 2013-04-29 15:08:34 -0700 | [diff] [blame] | 202 | out: |
| 203 | return ret; |
| 204 | } |
| 205 | |
Seth Jennings | 1eec670 | 2013-04-29 15:08:35 -0700 | [diff] [blame] | 206 | int __swap_writepage(struct page *page, struct writeback_control *wbc, |
| 207 | void (*end_write_func)(struct bio *, int)) |
Seth Jennings | 2f772e6 | 2013-04-29 15:08:34 -0700 | [diff] [blame] | 208 | { |
| 209 | struct bio *bio; |
| 210 | int ret = 0, rw = WRITE; |
| 211 | struct swap_info_struct *sis = page_swap_info(page); |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 212 | |
| 213 | if (sis->flags & SWP_FILE) { |
| 214 | struct kiocb kiocb; |
| 215 | struct file *swap_file = sis->swap_file; |
| 216 | struct address_space *mapping = swap_file->f_mapping; |
| 217 | struct iovec iov = { |
Mel Gorman | 5a17811 | 2012-07-31 16:45:02 -0700 | [diff] [blame] | 218 | .iov_base = kmap(page), |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 219 | .iov_len = PAGE_SIZE, |
| 220 | }; |
| 221 | |
| 222 | init_sync_kiocb(&kiocb, swap_file); |
| 223 | kiocb.ki_pos = page_file_offset(page); |
| 224 | kiocb.ki_left = PAGE_SIZE; |
| 225 | kiocb.ki_nbytes = PAGE_SIZE; |
| 226 | |
Mel Gorman | 0cdc444 | 2013-04-29 15:08:48 -0700 | [diff] [blame] | 227 | set_page_writeback(page); |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 228 | unlock_page(page); |
| 229 | ret = mapping->a_ops->direct_IO(KERNEL_WRITE, |
| 230 | &kiocb, &iov, |
| 231 | kiocb.ki_pos, 1); |
Mel Gorman | 5a17811 | 2012-07-31 16:45:02 -0700 | [diff] [blame] | 232 | kunmap(page); |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 233 | if (ret == PAGE_SIZE) { |
| 234 | count_vm_event(PSWPOUT); |
| 235 | ret = 0; |
Jerome Marchand | 2d30d31 | 2013-04-29 15:08:47 -0700 | [diff] [blame] | 236 | } else { |
Mel Gorman | 0cdc444 | 2013-04-29 15:08:48 -0700 | [diff] [blame] | 237 | /* |
| 238 | * In the case of swap-over-nfs, this can be a |
| 239 | * temporary failure if the system has limited |
| 240 | * memory for allocating transmit buffers. |
| 241 | * Mark the page dirty and avoid |
| 242 | * rotate_reclaimable_page but rate-limit the |
| 243 | * messages but do not flag PageError like |
| 244 | * the normal direct-to-bio case as it could |
| 245 | * be temporary. |
| 246 | */ |
Jerome Marchand | 2d30d31 | 2013-04-29 15:08:47 -0700 | [diff] [blame] | 247 | set_page_dirty(page); |
Mel Gorman | 0cdc444 | 2013-04-29 15:08:48 -0700 | [diff] [blame] | 248 | ClearPageReclaim(page); |
| 249 | pr_err_ratelimited("Write error on dio swapfile (%Lu)\n", |
| 250 | page_file_offset(page)); |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 251 | } |
Mel Gorman | 0cdc444 | 2013-04-29 15:08:48 -0700 | [diff] [blame] | 252 | end_page_writeback(page); |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 253 | return ret; |
| 254 | } |
| 255 | |
Seth Jennings | 1eec670 | 2013-04-29 15:08:35 -0700 | [diff] [blame] | 256 | bio = get_swap_bio(GFP_NOIO, page, end_write_func); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | if (bio == NULL) { |
| 258 | set_page_dirty(page); |
| 259 | unlock_page(page); |
| 260 | ret = -ENOMEM; |
| 261 | goto out; |
| 262 | } |
| 263 | if (wbc->sync_mode == WB_SYNC_ALL) |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 264 | rw |= REQ_SYNC; |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 265 | count_vm_event(PSWPOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | set_page_writeback(page); |
| 267 | unlock_page(page); |
| 268 | submit_bio(rw, bio); |
| 269 | out: |
| 270 | return ret; |
| 271 | } |
| 272 | |
Minchan Kim | aca8bf3 | 2009-06-16 15:33:02 -0700 | [diff] [blame] | 273 | int swap_readpage(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
| 275 | struct bio *bio; |
| 276 | int ret = 0; |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 277 | struct swap_info_struct *sis = page_swap_info(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
Hugh Dickins | 51726b1 | 2009-01-06 14:39:25 -0800 | [diff] [blame] | 279 | VM_BUG_ON(!PageLocked(page)); |
| 280 | VM_BUG_ON(PageUptodate(page)); |
Konrad Rzeszutek Wilk | 165c8ae | 2012-05-15 11:32:15 -0400 | [diff] [blame] | 281 | if (frontswap_load(page) == 0) { |
Dan Magenheimer | 38b5faf | 2012-04-09 17:08:06 -0600 | [diff] [blame] | 282 | SetPageUptodate(page); |
| 283 | unlock_page(page); |
| 284 | goto out; |
| 285 | } |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 286 | |
| 287 | if (sis->flags & SWP_FILE) { |
| 288 | struct file *swap_file = sis->swap_file; |
| 289 | struct address_space *mapping = swap_file->f_mapping; |
| 290 | |
| 291 | ret = mapping->a_ops->readpage(swap_file, page); |
| 292 | if (!ret) |
| 293 | count_vm_event(PSWPIN); |
| 294 | return ret; |
| 295 | } |
| 296 | |
Hugh Dickins | f29ad6a | 2009-12-14 17:58:40 -0800 | [diff] [blame] | 297 | bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | if (bio == NULL) { |
| 299 | unlock_page(page); |
| 300 | ret = -ENOMEM; |
| 301 | goto out; |
| 302 | } |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 303 | count_vm_event(PSWPIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | submit_bio(READ, bio); |
| 305 | out: |
| 306 | return ret; |
| 307 | } |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 308 | |
| 309 | int swap_set_page_dirty(struct page *page) |
| 310 | { |
| 311 | struct swap_info_struct *sis = page_swap_info(page); |
| 312 | |
| 313 | if (sis->flags & SWP_FILE) { |
| 314 | struct address_space *mapping = sis->swap_file->f_mapping; |
| 315 | return mapping->a_ops->set_page_dirty(page); |
| 316 | } else { |
| 317 | return __set_page_dirty_no_writeback(page); |
| 318 | } |
| 319 | } |