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> |
| 15 | #include <linux/pagemap.h> |
| 16 | #include <linux/swap.h> |
| 17 | #include <linux/bio.h> |
| 18 | #include <linux/swapops.h> |
| 19 | #include <linux/writeback.h> |
| 20 | #include <asm/pgtable.h> |
| 21 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 22 | static struct bio *get_swap_bio(gfp_t gfp_flags, pgoff_t index, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | struct page *page, bio_end_io_t end_io) |
| 24 | { |
| 25 | struct bio *bio; |
| 26 | |
| 27 | bio = bio_alloc(gfp_flags, 1); |
| 28 | if (bio) { |
| 29 | struct swap_info_struct *sis; |
| 30 | swp_entry_t entry = { .val = index, }; |
| 31 | |
| 32 | sis = get_swap_info_struct(swp_type(entry)); |
| 33 | bio->bi_sector = map_swap_page(sis, swp_offset(entry)) * |
| 34 | (PAGE_SIZE >> 9); |
| 35 | bio->bi_bdev = sis->bdev; |
| 36 | bio->bi_io_vec[0].bv_page = page; |
| 37 | bio->bi_io_vec[0].bv_len = PAGE_SIZE; |
| 38 | bio->bi_io_vec[0].bv_offset = 0; |
| 39 | bio->bi_vcnt = 1; |
| 40 | bio->bi_idx = 0; |
| 41 | bio->bi_size = PAGE_SIZE; |
| 42 | bio->bi_end_io = end_io; |
| 43 | } |
| 44 | return bio; |
| 45 | } |
| 46 | |
| 47 | static int end_swap_bio_write(struct bio *bio, unsigned int bytes_done, int err) |
| 48 | { |
| 49 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 50 | struct page *page = bio->bi_io_vec[0].bv_page; |
| 51 | |
| 52 | if (bio->bi_size) |
| 53 | return 1; |
| 54 | |
Peter Zijlstra | 6ddab3b | 2006-09-25 23:31:26 -0700 | [diff] [blame] | 55 | if (!uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | SetPageError(page); |
Peter Zijlstra | 6ddab3b | 2006-09-25 23:31:26 -0700 | [diff] [blame] | 57 | /* |
| 58 | * We failed to write the page out to swap-space. |
| 59 | * Re-dirty the page in order to avoid it being reclaimed. |
| 60 | * Also print a dire warning that things will go BAD (tm) |
| 61 | * very quickly. |
| 62 | * |
| 63 | * Also clear PG_reclaim to avoid rotate_reclaimable_page() |
| 64 | */ |
| 65 | set_page_dirty(page); |
| 66 | printk(KERN_ALERT "Write-error on swap-device (%u:%u:%Lu)\n", |
| 67 | imajor(bio->bi_bdev->bd_inode), |
| 68 | iminor(bio->bi_bdev->bd_inode), |
| 69 | (unsigned long long)bio->bi_sector); |
| 70 | ClearPageReclaim(page); |
| 71 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | end_page_writeback(page); |
| 73 | bio_put(bio); |
| 74 | return 0; |
| 75 | } |
| 76 | |
Andrew Morton | 546e0d2 | 2006-09-25 23:32:44 -0700 | [diff] [blame] | 77 | int end_swap_bio_read(struct bio *bio, unsigned int bytes_done, int err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
| 79 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 80 | struct page *page = bio->bi_io_vec[0].bv_page; |
| 81 | |
| 82 | if (bio->bi_size) |
| 83 | return 1; |
| 84 | |
| 85 | if (!uptodate) { |
| 86 | SetPageError(page); |
| 87 | ClearPageUptodate(page); |
Peter Zijlstra | 6ddab3b | 2006-09-25 23:31:26 -0700 | [diff] [blame] | 88 | printk(KERN_ALERT "Read-error on swap-device (%u:%u:%Lu)\n", |
| 89 | imajor(bio->bi_bdev->bd_inode), |
| 90 | iminor(bio->bi_bdev->bd_inode), |
| 91 | (unsigned long long)bio->bi_sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } else { |
| 93 | SetPageUptodate(page); |
| 94 | } |
| 95 | unlock_page(page); |
| 96 | bio_put(bio); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * We may have stale swap cache pages in memory: notice |
| 102 | * them here and get rid of the unnecessary final write. |
| 103 | */ |
| 104 | int swap_writepage(struct page *page, struct writeback_control *wbc) |
| 105 | { |
| 106 | struct bio *bio; |
| 107 | int ret = 0, rw = WRITE; |
| 108 | |
| 109 | if (remove_exclusive_swap_page(page)) { |
| 110 | unlock_page(page); |
| 111 | goto out; |
| 112 | } |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 113 | bio = get_swap_bio(GFP_NOIO, page_private(page), page, |
| 114 | end_swap_bio_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | if (bio == NULL) { |
| 116 | set_page_dirty(page); |
| 117 | unlock_page(page); |
| 118 | ret = -ENOMEM; |
| 119 | goto out; |
| 120 | } |
| 121 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 122 | rw |= (1 << BIO_RW_SYNC); |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 123 | count_vm_event(PSWPOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | set_page_writeback(page); |
| 125 | unlock_page(page); |
| 126 | submit_bio(rw, bio); |
| 127 | out: |
| 128 | return ret; |
| 129 | } |
| 130 | |
| 131 | int swap_readpage(struct file *file, struct page *page) |
| 132 | { |
| 133 | struct bio *bio; |
| 134 | int ret = 0; |
| 135 | |
| 136 | BUG_ON(!PageLocked(page)); |
| 137 | ClearPageUptodate(page); |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 138 | bio = get_swap_bio(GFP_KERNEL, page_private(page), page, |
| 139 | end_swap_bio_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | if (bio == NULL) { |
| 141 | unlock_page(page); |
| 142 | ret = -ENOMEM; |
| 143 | goto out; |
| 144 | } |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 145 | count_vm_event(PSWPIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | submit_bio(READ, bio); |
| 147 | out: |
| 148 | return ret; |
| 149 | } |
| 150 | |
Pavel Machek | 648be31 | 2005-06-25 14:55:09 -0700 | [diff] [blame] | 151 | #ifdef CONFIG_SOFTWARE_SUSPEND |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | /* |
| 153 | * A scruffy utility function to read or write an arbitrary swap page |
| 154 | * and wait on the I/O. The caller must have a ref on the page. |
| 155 | * |
| 156 | * We use end_swap_bio_read() even for writes, because it happens to do what |
| 157 | * we want. |
| 158 | */ |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 159 | int rw_swap_page_sync(int rw, swp_entry_t entry, struct page *page, |
| 160 | struct bio **bio_chain) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
| 162 | struct bio *bio; |
| 163 | int ret = 0; |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 164 | int bio_rw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | lock_page(page); |
| 167 | |
| 168 | bio = get_swap_bio(GFP_KERNEL, entry.val, page, end_swap_bio_read); |
| 169 | if (bio == NULL) { |
| 170 | unlock_page(page); |
| 171 | ret = -ENOMEM; |
| 172 | goto out; |
| 173 | } |
| 174 | |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 175 | bio_rw = rw; |
| 176 | if (!bio_chain) |
| 177 | bio_rw |= (1 << BIO_RW_SYNC); |
| 178 | if (bio_chain) |
| 179 | bio_get(bio); |
| 180 | submit_bio(bio_rw, bio); |
| 181 | if (bio_chain == NULL) { |
| 182 | wait_on_page_locked(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Andrew Morton | ab95416 | 2006-09-25 23:32:42 -0700 | [diff] [blame] | 184 | if (!PageUptodate(page) || PageError(page)) |
| 185 | ret = -EIO; |
| 186 | } |
| 187 | if (bio_chain) { |
| 188 | bio->bi_private = *bio_chain; |
| 189 | *bio_chain = bio; |
| 190 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | out: |
| 192 | return ret; |
| 193 | } |
| 194 | #endif |