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/gfp.h>
16 #include <linux/pagemap.h>
17 #include <linux/swap.h>
18 #include <linux/bio.h>
19 #include <linux/swapops.h>
20 #include <linux/writeback.h>
21 #include <asm/pgtable.h>
23 static struct bio *get_swap_bio(gfp_t gfp_flags,
24 struct page *page, bio_end_io_t end_io)
28 bio = bio_alloc(gfp_flags, 1);
30 bio->bi_sector = map_swap_page(page, &bio->bi_bdev);
31 bio->bi_sector <<= PAGE_SHIFT - 9;
32 bio->bi_io_vec[0].bv_page = page;
33 bio->bi_io_vec[0].bv_len = PAGE_SIZE;
34 bio->bi_io_vec[0].bv_offset = 0;
37 bio->bi_size = PAGE_SIZE;
38 bio->bi_end_io = end_io;
43 static void end_swap_bio_write(struct bio *bio, int err)
45 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
46 struct page *page = bio->bi_io_vec[0].bv_page;
51 * We failed to write the page out to swap-space.
52 * Re-dirty the page in order to avoid it being reclaimed.
53 * Also print a dire warning that things will go BAD (tm)
56 * Also clear PG_reclaim to avoid rotate_reclaimable_page()
59 printk(KERN_ALERT "Write-error on swap-device (%u:%u:%Lu)\n",
60 imajor(bio->bi_bdev->bd_inode),
61 iminor(bio->bi_bdev->bd_inode),
62 (unsigned long long)bio->bi_sector);
63 ClearPageReclaim(page);
65 end_page_writeback(page);
69 void end_swap_bio_read(struct bio *bio, int err)
71 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
72 struct page *page = bio->bi_io_vec[0].bv_page;
76 ClearPageUptodate(page);
77 printk(KERN_ALERT "Read-error on swap-device (%u:%u:%Lu)\n",
78 imajor(bio->bi_bdev->bd_inode),
79 iminor(bio->bi_bdev->bd_inode),
80 (unsigned long long)bio->bi_sector);
82 SetPageUptodate(page);
89 * We may have stale swap cache pages in memory: notice
90 * them here and get rid of the unnecessary final write.
92 int swap_writepage(struct page *page, struct writeback_control *wbc)
95 int ret = 0, rw = WRITE;
97 if (try_to_free_swap(page)) {
101 bio = get_swap_bio(GFP_NOIO, page, end_swap_bio_write);
103 set_page_dirty(page);
108 if (wbc->sync_mode == WB_SYNC_ALL)
109 rw |= REQ_SYNC | REQ_UNPLUG;
110 count_vm_event(PSWPOUT);
111 set_page_writeback(page);
118 int swap_readpage(struct page *page)
123 VM_BUG_ON(!PageLocked(page));
124 VM_BUG_ON(PageUptodate(page));
125 bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
131 count_vm_event(PSWPIN);
132 submit_bio(READ, bio);