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/buffer_head.h> /* for block_sync_page() */
20 #include <linux/mpage.h>
21 #include <linux/writeback.h>
22 #include <asm/pgtable.h>
25 get_swap_bio(int gfp_flags
, struct page
*page
, bio_end_io_t end_io
)
29 bio
= bio_alloc(gfp_flags
, 1);
31 struct swap_info_struct
*sis
;
34 BUG_ON(!PageSwapCache(page
));
35 entry
.val
= page
->index
;
36 sis
= get_swap_info_struct(swp_type(entry
));
38 bio
->bi_sector
= map_swap_page(sis
, swp_offset(entry
)) *
40 bio
->bi_bdev
= sis
->bdev
;
41 bio
->bi_io_vec
[0].bv_page
= page
;
42 bio
->bi_io_vec
[0].bv_len
= PAGE_SIZE
;
43 bio
->bi_io_vec
[0].bv_offset
= 0;
46 bio
->bi_size
= PAGE_SIZE
;
47 bio
->bi_end_io
= end_io
;
52 static int end_swap_bio_write(struct bio
*bio
, unsigned int bytes_done
, int err
)
54 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
55 struct page
*page
= bio
->bi_io_vec
[0].bv_page
;
62 end_page_writeback(page
);
67 static int end_swap_bio_read(struct bio
*bio
, unsigned int bytes_done
, int err
)
69 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
70 struct page
*page
= bio
->bi_io_vec
[0].bv_page
;
77 ClearPageUptodate(page
);
79 SetPageUptodate(page
);
87 * We may have stale swap cache pages in memory: notice
88 * them here and get rid of the unnecessary final write.
90 int swap_writepage(struct page
*page
, struct writeback_control
*wbc
)
95 if (remove_exclusive_swap_page(page
)) {
99 bio
= get_swap_bio(GFP_NOIO
, page
, end_swap_bio_write
);
101 set_page_dirty(page
);
106 inc_page_state(pswpout
);
107 SetPageWriteback(page
);
109 submit_bio(WRITE
, bio
);
114 int swap_readpage(struct file
*file
, struct page
*page
)
119 BUG_ON(!PageLocked(page
));
120 ClearPageUptodate(page
);
121 bio
= get_swap_bio(GFP_KERNEL
, page
, end_swap_bio_read
);
127 inc_page_state(pswpin
);
128 submit_bio(READ
, bio
);
133 struct address_space_operations swap_aops
= {
134 .writepage
= swap_writepage
,
135 .readpage
= swap_readpage
,
136 .sync_page
= block_sync_page
,
137 .set_page_dirty
= __set_page_dirty_nobuffers
,
141 * A scruffy utility function to read or write an arbitrary swap page
142 * and wait on the I/O.
144 int rw_swap_page_sync(int rw
, swp_entry_t entry
, struct page
*page
)
147 struct writeback_control swap_wbc
= {
148 .sync_mode
= WB_SYNC_ALL
,
153 BUG_ON(page
->mapping
);
154 page
->mapping
= &swapper_space
;
155 page
->index
= entry
.val
;
158 ret
= swap_readpage(NULL
, page
);
159 wait_on_page_locked(page
);
161 ret
= swap_writepage(page
, &swap_wbc
);
162 wait_on_page_writeback(page
);
164 page
->mapping
= NULL
;
165 if (ret
== 0 && (!PageUptodate(page
) || PageError(page
)))