Cleanup syscall code to look more like it's mips64 equivalent.
[linux-2.6/linux-mips.git] / mm / page_io.c
blobfaf3e211a33a5167d8f137841d1832994e104d12
1 /*
2 * linux/mm/page_io.c
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
13 #include <linux/mm.h>
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>
24 static struct bio *
25 get_swap_bio(int gfp_flags, struct page *page, bio_end_io_t end_io)
27 struct bio *bio;
29 bio = bio_alloc(gfp_flags, 1);
30 if (bio) {
31 struct swap_info_struct *sis;
32 swp_entry_t entry;
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)) *
39 (PAGE_SIZE >> 9);
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;
44 bio->bi_vcnt = 1;
45 bio->bi_idx = 0;
46 bio->bi_size = PAGE_SIZE;
47 bio->bi_end_io = end_io;
49 return bio;
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;
57 if (bio->bi_size)
58 return 1;
60 if (!uptodate)
61 SetPageError(page);
62 end_page_writeback(page);
63 bio_put(bio);
64 return 0;
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;
72 if (bio->bi_size)
73 return 1;
75 if (!uptodate) {
76 SetPageError(page);
77 ClearPageUptodate(page);
78 } else {
79 SetPageUptodate(page);
81 unlock_page(page);
82 bio_put(bio);
83 return 0;
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)
92 struct bio *bio;
93 int ret = 0;
95 if (remove_exclusive_swap_page(page)) {
96 unlock_page(page);
97 goto out;
99 bio = get_swap_bio(GFP_NOIO, page, end_swap_bio_write);
100 if (bio == NULL) {
101 set_page_dirty(page);
102 unlock_page(page);
103 ret = -ENOMEM;
104 goto out;
106 inc_page_state(pswpout);
107 SetPageWriteback(page);
108 unlock_page(page);
109 submit_bio(WRITE, bio);
110 out:
111 return ret;
114 int swap_readpage(struct file *file, struct page *page)
116 struct bio *bio;
117 int ret = 0;
119 BUG_ON(!PageLocked(page));
120 ClearPageUptodate(page);
121 bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
122 if (bio == NULL) {
123 unlock_page(page);
124 ret = -ENOMEM;
125 goto out;
127 inc_page_state(pswpin);
128 submit_bio(READ, bio);
129 out:
130 return ret;
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)
146 int ret;
147 struct writeback_control swap_wbc = {
148 .sync_mode = WB_SYNC_ALL,
151 lock_page(page);
153 BUG_ON(page->mapping);
154 page->mapping = &swapper_space;
155 page->index = entry.val;
157 if (rw == READ) {
158 ret = swap_readpage(NULL, page);
159 wait_on_page_locked(page);
160 } else {
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)))
166 ret = -EIO;
167 return ret;