]> bbs.cooldavid.org Git - net-next-2.6.git/blame - mm/page_io.c
[PATCH] slab: respect architecture and caller mandated alignment
[net-next-2.6.git] / mm / page_io.c
CommitLineData
1da177e4
LT
1/*
2 * linux/mm/page_io.c
3 *
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 *
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
11 */
12
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/writeback.h>
20#include <asm/pgtable.h>
21
dd0fc66f 22static struct bio *get_swap_bio(gfp_t gfp_flags, pgoff_t index,
1da177e4
LT
23 struct page *page, bio_end_io_t end_io)
24{
25 struct bio *bio;
26
27 bio = bio_alloc(gfp_flags, 1);
28 if (bio) {
29 struct swap_info_struct *sis;
30 swp_entry_t entry = { .val = index, };
31
32 sis = get_swap_info_struct(swp_type(entry));
33 bio->bi_sector = map_swap_page(sis, swp_offset(entry)) *
34 (PAGE_SIZE >> 9);
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;
39 bio->bi_vcnt = 1;
40 bio->bi_idx = 0;
41 bio->bi_size = PAGE_SIZE;
42 bio->bi_end_io = end_io;
43 }
44 return bio;
45}
46
47static int end_swap_bio_write(struct bio *bio, unsigned int bytes_done, int err)
48{
49 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
50 struct page *page = bio->bi_io_vec[0].bv_page;
51
52 if (bio->bi_size)
53 return 1;
54
55 if (!uptodate)
56 SetPageError(page);
57 end_page_writeback(page);
58 bio_put(bio);
59 return 0;
60}
61
62static int end_swap_bio_read(struct bio *bio, unsigned int bytes_done, int err)
63{
64 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
65 struct page *page = bio->bi_io_vec[0].bv_page;
66
67 if (bio->bi_size)
68 return 1;
69
70 if (!uptodate) {
71 SetPageError(page);
72 ClearPageUptodate(page);
73 } else {
74 SetPageUptodate(page);
75 }
76 unlock_page(page);
77 bio_put(bio);
78 return 0;
79}
80
81/*
82 * We may have stale swap cache pages in memory: notice
83 * them here and get rid of the unnecessary final write.
84 */
85int swap_writepage(struct page *page, struct writeback_control *wbc)
86{
87 struct bio *bio;
88 int ret = 0, rw = WRITE;
89
90 if (remove_exclusive_swap_page(page)) {
91 unlock_page(page);
92 goto out;
93 }
4c21e2f2
HD
94 bio = get_swap_bio(GFP_NOIO, page_private(page), page,
95 end_swap_bio_write);
1da177e4
LT
96 if (bio == NULL) {
97 set_page_dirty(page);
98 unlock_page(page);
99 ret = -ENOMEM;
100 goto out;
101 }
102 if (wbc->sync_mode == WB_SYNC_ALL)
103 rw |= (1 << BIO_RW_SYNC);
f8891e5e 104 count_vm_event(PSWPOUT);
1da177e4
LT
105 set_page_writeback(page);
106 unlock_page(page);
107 submit_bio(rw, bio);
108out:
109 return ret;
110}
111
112int swap_readpage(struct file *file, struct page *page)
113{
114 struct bio *bio;
115 int ret = 0;
116
117 BUG_ON(!PageLocked(page));
118 ClearPageUptodate(page);
4c21e2f2
HD
119 bio = get_swap_bio(GFP_KERNEL, page_private(page), page,
120 end_swap_bio_read);
1da177e4
LT
121 if (bio == NULL) {
122 unlock_page(page);
123 ret = -ENOMEM;
124 goto out;
125 }
f8891e5e 126 count_vm_event(PSWPIN);
1da177e4
LT
127 submit_bio(READ, bio);
128out:
129 return ret;
130}
131
648be318 132#ifdef CONFIG_SOFTWARE_SUSPEND
1da177e4
LT
133/*
134 * A scruffy utility function to read or write an arbitrary swap page
135 * and wait on the I/O. The caller must have a ref on the page.
136 *
137 * We use end_swap_bio_read() even for writes, because it happens to do what
138 * we want.
139 */
140int rw_swap_page_sync(int rw, swp_entry_t entry, struct page *page)
141{
142 struct bio *bio;
143 int ret = 0;
144
145 lock_page(page);
146
147 bio = get_swap_bio(GFP_KERNEL, entry.val, page, end_swap_bio_read);
148 if (bio == NULL) {
149 unlock_page(page);
150 ret = -ENOMEM;
151 goto out;
152 }
153
154 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
155 wait_on_page_locked(page);
156
157 if (!PageUptodate(page) || PageError(page))
158 ret = -EIO;
159out:
160 return ret;
161}
162#endif