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/buffer_head.h>
21 #include <linux/writeback.h>
22 #include <linux/frontswap.h>
23 #include <asm/pgtable.h>
25 static struct bio *get_swap_bio(gfp_t gfp_flags,
26 struct page *page, bio_end_io_t end_io)
30 bio = bio_alloc(gfp_flags, 1);
32 bio->bi_sector = map_swap_page(page, &bio->bi_bdev);
33 bio->bi_sector <<= PAGE_SHIFT - 9;
34 bio->bi_io_vec[0].bv_page = page;
35 bio->bi_io_vec[0].bv_len = PAGE_SIZE;
36 bio->bi_io_vec[0].bv_offset = 0;
39 bio->bi_size = PAGE_SIZE;
40 bio->bi_end_io = end_io;
45 static void end_swap_bio_write(struct bio *bio, int err)
47 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
48 struct page *page = bio->bi_io_vec[0].bv_page;
53 * We failed to write the page out to swap-space.
54 * Re-dirty the page in order to avoid it being reclaimed.
55 * Also print a dire warning that things will go BAD (tm)
58 * Also clear PG_reclaim to avoid rotate_reclaimable_page()
61 printk(KERN_ALERT "Write-error on swap-device (%u:%u:%Lu)\n",
62 imajor(bio->bi_bdev->bd_inode),
63 iminor(bio->bi_bdev->bd_inode),
64 (unsigned long long)bio->bi_sector);
65 ClearPageReclaim(page);
67 end_page_writeback(page);
71 void end_swap_bio_read(struct bio *bio, int err)
73 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
74 struct page *page = bio->bi_io_vec[0].bv_page;
78 ClearPageUptodate(page);
79 printk(KERN_ALERT "Read-error on swap-device (%u:%u:%Lu)\n",
80 imajor(bio->bi_bdev->bd_inode),
81 iminor(bio->bi_bdev->bd_inode),
82 (unsigned long long)bio->bi_sector);
84 SetPageUptodate(page);
91 * We may have stale swap cache pages in memory: notice
92 * them here and get rid of the unnecessary final write.
94 int swap_writepage(struct page *page, struct writeback_control *wbc)
97 int ret = 0, rw = WRITE;
98 struct swap_info_struct *sis = page_swap_info(page);
100 if (try_to_free_swap(page)) {
104 if (frontswap_store(page) == 0) {
105 set_page_writeback(page);
107 end_page_writeback(page);
111 if (sis->flags & SWP_FILE) {
113 struct file *swap_file = sis->swap_file;
114 struct address_space *mapping = swap_file->f_mapping;
116 .iov_base = page_address(page),
117 .iov_len = PAGE_SIZE,
120 init_sync_kiocb(&kiocb, swap_file);
121 kiocb.ki_pos = page_file_offset(page);
122 kiocb.ki_left = PAGE_SIZE;
123 kiocb.ki_nbytes = PAGE_SIZE;
126 ret = mapping->a_ops->direct_IO(KERNEL_WRITE,
129 if (ret == PAGE_SIZE) {
130 count_vm_event(PSWPOUT);
136 bio = get_swap_bio(GFP_NOIO, page, end_swap_bio_write);
138 set_page_dirty(page);
143 if (wbc->sync_mode == WB_SYNC_ALL)
145 count_vm_event(PSWPOUT);
146 set_page_writeback(page);
153 int swap_readpage(struct page *page)
157 struct swap_info_struct *sis = page_swap_info(page);
159 VM_BUG_ON(!PageLocked(page));
160 VM_BUG_ON(PageUptodate(page));
161 if (frontswap_load(page) == 0) {
162 SetPageUptodate(page);
167 if (sis->flags & SWP_FILE) {
168 struct file *swap_file = sis->swap_file;
169 struct address_space *mapping = swap_file->f_mapping;
171 ret = mapping->a_ops->readpage(swap_file, page);
173 count_vm_event(PSWPIN);
177 bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
183 count_vm_event(PSWPIN);
184 submit_bio(READ, bio);
189 int swap_set_page_dirty(struct page *page)
191 struct swap_info_struct *sis = page_swap_info(page);
193 if (sis->flags & SWP_FILE) {
194 struct address_space *mapping = sis->swap_file->f_mapping;
195 return mapping->a_ops->set_page_dirty(page);
197 return __set_page_dirty_no_writeback(page);