[PATCH] get rid of <linux/locks.h>
[linux-flexiantxendom0-3.2.10.git] / mm / page_io.c
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/swapctl.h>
18
19 #include <asm/pgtable.h>
20
21 /*
22  * Reads or writes a swap page.
23  * wait=1: start I/O and wait for completion. wait=0: start asynchronous I/O.
24  *
25  * Important prevention of race condition: the caller *must* atomically 
26  * create a unique swap cache entry for this swap page before calling
27  * rw_swap_page, and must lock that page.  By ensuring that there is a
28  * single page of memory reserved for the swap entry, the normal VM page
29  * lock on that page also doubles as a lock on swap entries.  Having only
30  * one lock to deal with per swap entry (rather than locking swap and memory
31  * independently) also makes it easier to make certain swapping operations
32  * atomic, which is particularly important when we are trying to ensure 
33  * that shared pages stay shared while being swapped.
34  */
35
36 static int rw_swap_page_base(int rw, swp_entry_t entry, struct page *page)
37 {
38         unsigned long offset;
39         sector_t zones[PAGE_SIZE/512];
40         int zones_used;
41         int block_size;
42         struct inode *swapf = 0;
43         struct block_device *bdev;
44
45         if (rw == READ) {
46                 ClearPageUptodate(page);
47                 kstat.pswpin++;
48         } else
49                 kstat.pswpout++;
50
51         get_swaphandle_info(entry, &offset, &swapf);
52         bdev = swapf->i_bdev;
53         if (bdev) {
54                 zones[0] = offset;
55                 zones_used = 1;
56                 block_size = PAGE_SIZE;
57         } else {
58                 int i, j;
59                 unsigned int block = offset
60                         << (PAGE_SHIFT - swapf->i_sb->s_blocksize_bits);
61
62                 block_size = swapf->i_sb->s_blocksize;
63                 for (i=0, j=0; j< PAGE_SIZE ; i++, j += block_size)
64                         if (!(zones[i] = bmap(swapf,block++))) {
65                                 printk("rw_swap_page: bad swap file\n");
66                                 return 0;
67                         }
68                 zones_used = i;
69                 bdev = swapf->i_sb->s_bdev;
70         }
71
72         /* block_size == PAGE_SIZE/zones_used */
73         brw_page(rw, page, bdev, zones, block_size);
74
75         /* Note! For consistency we do all of the logic,
76          * decrementing the page count, and unlocking the page in the
77          * swap lock map - in the IO completion handler.
78          */
79         return 1;
80 }
81
82 /*
83  * A simple wrapper so the base function doesn't need to enforce
84  * that all swap pages go through the swap cache! We verify that:
85  *  - the page is locked
86  *  - it's marked as being swap-cache
87  *  - it's associated with the swap inode
88  */
89 void rw_swap_page(int rw, struct page *page)
90 {
91         swp_entry_t entry;
92
93         entry.val = page->index;
94
95         if (!PageLocked(page))
96                 PAGE_BUG(page);
97         if (!PageSwapCache(page))
98                 PAGE_BUG(page);
99         if (!rw_swap_page_base(rw, entry, page))
100                 unlock_page(page);
101 }
102
103 /*
104  * The swap lock map insists that pages be in the page cache!
105  * Therefore we can't use it.  Later when we can remove the need for the
106  * lock map and we can reduce the number of functions exported.
107  */
108 void rw_swap_page_nolock(int rw, swp_entry_t entry, char *buf)
109 {
110         struct page *page = virt_to_page(buf);
111         
112         if (!PageLocked(page))
113                 PAGE_BUG(page);
114         if (page->mapping)
115                 PAGE_BUG(page);
116         /* needs sync_page to wait I/O completation */
117         page->mapping = &swapper_space;
118         if (!rw_swap_page_base(rw, entry, page))
119                 unlock_page(page);
120         wait_on_page_locked(page);
121         page->mapping = NULL;
122 }