4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
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
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>
22 static struct bio *get_swap_bio(gfp_t gfp_flags, pgoff_t index,
23 struct page *page, bio_end_io_t end_io)
27 bio = bio_alloc(gfp_flags, 1);
29 struct swap_info_struct *sis;
30 swp_entry_t entry = { .val = index, };
32 sis = get_swap_info_struct(swp_type(entry));
33 bio->bi_sector = map_swap_page(sis, swp_offset(entry)) *
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;
41 bio->bi_size = PAGE_SIZE;
42 bio->bi_end_io = end_io;
47 static int end_swap_bio_write(struct bio *bio, unsigned int bytes_done, int err)
49 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
50 struct page *page = bio->bi_io_vec[0].bv_page;
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)
63 * Also clear PG_reclaim to avoid rotate_reclaimable_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);
72 end_page_writeback(page);
77 int end_swap_bio_read(struct bio *bio, unsigned int bytes_done, int err)
79 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
80 struct page *page = bio->bi_io_vec[0].bv_page;
87 ClearPageUptodate(page);
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);
93 SetPageUptodate(page);
101 * We may have stale swap cache pages in memory: notice
102 * them here and get rid of the unnecessary final write.
104 int swap_writepage(struct page *page, struct writeback_control *wbc)
107 int ret = 0, rw = WRITE;
109 if (remove_exclusive_swap_page(page)) {
113 bio = get_swap_bio(GFP_NOIO, page_private(page), page,
116 set_page_dirty(page);
121 if (wbc->sync_mode == WB_SYNC_ALL)
122 rw |= (1 << BIO_RW_SYNC);
123 count_vm_event(PSWPOUT);
124 set_page_writeback(page);
131 int swap_readpage(struct file *file, struct page *page)
136 BUG_ON(!PageLocked(page));
137 ClearPageUptodate(page);
138 bio = get_swap_bio(GFP_KERNEL, page_private(page), page,
145 count_vm_event(PSWPIN);
146 submit_bio(READ, bio);