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 <linux/aio.h>
24 #include <asm/pgtable.h>
26 static struct bio
*get_swap_bio(gfp_t gfp_flags
,
27 struct page
*page
, bio_end_io_t end_io
)
31 bio
= bio_alloc(gfp_flags
, 1);
33 bio
->bi_sector
= map_swap_page(page
, &bio
->bi_bdev
);
34 bio
->bi_sector
<<= PAGE_SHIFT
- 9;
35 bio
->bi_io_vec
[0].bv_page
= page
;
36 bio
->bi_io_vec
[0].bv_len
= PAGE_SIZE
;
37 bio
->bi_io_vec
[0].bv_offset
= 0;
39 bio
->bi_size
= PAGE_SIZE
;
40 bio
->bi_end_io
= end_io
;
45 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
);
90 int generic_swapfile_activate(struct swap_info_struct
*sis
,
91 struct file
*swap_file
,
94 struct address_space
*mapping
= swap_file
->f_mapping
;
95 struct inode
*inode
= mapping
->host
;
96 unsigned blocks_per_page
;
97 unsigned long page_no
;
101 sector_t lowest_block
= -1;
102 sector_t highest_block
= 0;
106 blkbits
= inode
->i_blkbits
;
107 blocks_per_page
= PAGE_SIZE
>> blkbits
;
110 * Map all the blocks into the extent list. This code doesn't try
115 last_block
= i_size_read(inode
) >> blkbits
;
116 while ((probe_block
+ blocks_per_page
) <= last_block
&&
117 page_no
< sis
->max
) {
118 unsigned block_in_page
;
119 sector_t first_block
;
121 first_block
= bmap(inode
, probe_block
);
122 if (first_block
== 0)
126 * It must be PAGE_SIZE aligned on-disk
128 if (first_block
& (blocks_per_page
- 1)) {
133 for (block_in_page
= 1; block_in_page
< blocks_per_page
;
137 block
= bmap(inode
, probe_block
+ block_in_page
);
140 if (block
!= first_block
+ block_in_page
) {
147 first_block
>>= (PAGE_SHIFT
- blkbits
);
148 if (page_no
) { /* exclude the header page */
149 if (first_block
< lowest_block
)
150 lowest_block
= first_block
;
151 if (first_block
> highest_block
)
152 highest_block
= first_block
;
156 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
158 ret
= add_swap_extent(sis
, page_no
, 1, first_block
);
163 probe_block
+= blocks_per_page
;
168 *span
= 1 + highest_block
- lowest_block
;
170 page_no
= 1; /* force Empty message */
172 sis
->pages
= page_no
- 1;
173 sis
->highest_bit
= page_no
- 1;
177 printk(KERN_ERR
"swapon: swapfile has holes\n");
183 * We may have stale swap cache pages in memory: notice
184 * them here and get rid of the unnecessary final write.
186 int swap_writepage(struct page
*page
, struct writeback_control
*wbc
)
190 if (try_to_free_swap(page
)) {
194 if (frontswap_store(page
) == 0) {
195 set_page_writeback(page
);
197 end_page_writeback(page
);
200 ret
= __swap_writepage(page
, wbc
, end_swap_bio_write
);
205 int __swap_writepage(struct page
*page
, struct writeback_control
*wbc
,
206 void (*end_write_func
)(struct bio
*, int))
209 int ret
= 0, rw
= WRITE
;
210 struct swap_info_struct
*sis
= page_swap_info(page
);
212 if (sis
->flags
& SWP_FILE
) {
214 struct file
*swap_file
= sis
->swap_file
;
215 struct address_space
*mapping
= swap_file
->f_mapping
;
217 .iov_base
= kmap(page
),
218 .iov_len
= PAGE_SIZE
,
221 init_sync_kiocb(&kiocb
, swap_file
);
222 kiocb
.ki_pos
= page_file_offset(page
);
223 kiocb
.ki_left
= PAGE_SIZE
;
224 kiocb
.ki_nbytes
= PAGE_SIZE
;
226 set_page_writeback(page
);
228 ret
= mapping
->a_ops
->direct_IO(KERNEL_WRITE
,
232 if (ret
== PAGE_SIZE
) {
233 count_vm_event(PSWPOUT
);
237 * In the case of swap-over-nfs, this can be a
238 * temporary failure if the system has limited
239 * memory for allocating transmit buffers.
240 * Mark the page dirty and avoid
241 * rotate_reclaimable_page but rate-limit the
242 * messages but do not flag PageError like
243 * the normal direct-to-bio case as it could
246 set_page_dirty(page
);
247 ClearPageReclaim(page
);
248 pr_err_ratelimited("Write error on dio swapfile (%Lu)\n",
249 page_file_offset(page
));
251 end_page_writeback(page
);
255 bio
= get_swap_bio(GFP_NOIO
, page
, end_write_func
);
257 set_page_dirty(page
);
262 if (wbc
->sync_mode
== WB_SYNC_ALL
)
264 count_vm_event(PSWPOUT
);
265 set_page_writeback(page
);
272 int swap_readpage(struct page
*page
)
276 struct swap_info_struct
*sis
= page_swap_info(page
);
278 VM_BUG_ON(!PageLocked(page
));
279 VM_BUG_ON(PageUptodate(page
));
280 if (frontswap_load(page
) == 0) {
281 SetPageUptodate(page
);
286 if (sis
->flags
& SWP_FILE
) {
287 struct file
*swap_file
= sis
->swap_file
;
288 struct address_space
*mapping
= swap_file
->f_mapping
;
290 ret
= mapping
->a_ops
->readpage(swap_file
, page
);
292 count_vm_event(PSWPIN
);
296 bio
= get_swap_bio(GFP_KERNEL
, page
, end_swap_bio_read
);
302 count_vm_event(PSWPIN
);
303 submit_bio(READ
, bio
);
308 int swap_set_page_dirty(struct page
*page
)
310 struct swap_info_struct
*sis
= page_swap_info(page
);
312 if (sis
->flags
& SWP_FILE
) {
313 struct address_space
*mapping
= sis
->swap_file
->f_mapping
;
314 return mapping
->a_ops
->set_page_dirty(page
);
316 return __set_page_dirty_no_writeback(page
);