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 void end_swap_bio_write(struct bio *bio, int err)
49 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
50 struct page *page = bio->bi_io_vec[0].bv_page;
55 * We failed to write the page out to swap-space.
56 * Re-dirty the page in order to avoid it being reclaimed.
57 * Also print a dire warning that things will go BAD (tm)
60 * Also clear PG_reclaim to avoid rotate_reclaimable_page()
63 printk(KERN_ALERT "Write-error on swap-device (%u:%u:%Lu)\n",
64 imajor(bio->bi_bdev->bd_inode),
65 iminor(bio->bi_bdev->bd_inode),
66 (unsigned long long)bio->bi_sector);
67 ClearPageReclaim(page);
69 end_page_writeback(page);
73 void end_swap_bio_read(struct bio *bio, int err)
75 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
76 struct page *page = bio->bi_io_vec[0].bv_page;
80 ClearPageUptodate(page);
81 printk(KERN_ALERT "Read-error on swap-device (%u:%u:%Lu)\n",
82 imajor(bio->bi_bdev->bd_inode),
83 iminor(bio->bi_bdev->bd_inode),
84 (unsigned long long)bio->bi_sector);
86 SetPageUptodate(page);
93 * We may have stale swap cache pages in memory: notice
94 * them here and get rid of the unnecessary final write.
96 int swap_writepage(struct page *page, struct writeback_control *wbc)
99 int ret = 0, rw = WRITE;
101 if (try_to_free_swap(page)) {
105 bio = get_swap_bio(GFP_NOIO, page_private(page), page,
108 set_page_dirty(page);
113 if (wbc->sync_mode == WB_SYNC_ALL)
114 rw |= (1 << BIO_RW_SYNC);
115 count_vm_event(PSWPOUT);
116 set_page_writeback(page);
123 int swap_readpage(struct file *file, struct page *page)
128 VM_BUG_ON(!PageLocked(page));
129 VM_BUG_ON(PageUptodate(page));
130 bio = get_swap_bio(GFP_KERNEL, page_private(page), page,
137 count_vm_event(PSWPIN);
138 submit_bio(READ, bio);