~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/mm/page_io.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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/swap.h>
 16 #include <linux/locks.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, int wait)
 37 {
 38         unsigned long offset;
 39         int zones[PAGE_SIZE/512];
 40         int zones_used;
 41         kdev_t dev = 0;
 42         int block_size;
 43         struct inode *swapf = 0;
 44 
 45         /* Don't allow too many pending pages in flight.. */
 46         if ((rw == WRITE) && atomic_read(&nr_async_pages) >
 47                         pager_daemon.swap_cluster * (1 << page_cluster))
 48                 wait = 1;
 49 
 50         if (rw == READ) {
 51                 ClearPageUptodate(page);
 52                 kstat.pswpin++;
 53         } else
 54                 kstat.pswpout++;
 55 
 56         get_swaphandle_info(entry, &offset, &dev, &swapf);
 57         if (dev) {
 58                 zones[0] = offset;
 59                 zones_used = 1;
 60                 block_size = PAGE_SIZE;
 61         } else if (swapf) {
 62                 int i, j;
 63                 unsigned int block = offset
 64                         << (PAGE_SHIFT - swapf->i_sb->s_blocksize_bits);
 65 
 66                 block_size = swapf->i_sb->s_blocksize;
 67                 for (i=0, j=0; j< PAGE_SIZE ; i++, j += block_size)
 68                         if (!(zones[i] = bmap(swapf,block++))) {
 69                                 printk("rw_swap_page: bad swap file\n");
 70                                 return 0;
 71                         }
 72                 zones_used = i;
 73                 dev = swapf->i_dev;
 74         } else {
 75                 return 0;
 76         }
 77         if (!wait) {
 78                 SetPageDecrAfter(page);
 79                 atomic_inc(&nr_async_pages);
 80         }
 81 
 82         /* block_size == PAGE_SIZE/zones_used */
 83         brw_page(rw, page, dev, zones, block_size);
 84 
 85         /* Note! For consistency we do all of the logic,
 86          * decrementing the page count, and unlocking the page in the
 87          * swap lock map - in the IO completion handler.
 88          */
 89         if (!wait)
 90                 return 1;
 91 
 92         wait_on_page(page);
 93         /* This shouldn't happen, but check to be sure. */
 94         if (page_count(page) == 0)
 95                 printk(KERN_ERR "rw_swap_page: page unused while waiting!\n");
 96 
 97         return 1;
 98 }
 99 
100 /*
101  * A simple wrapper so the base function doesn't need to enforce
102  * that all swap pages go through the swap cache! We verify that:
103  *  - the page is locked
104  *  - it's marked as being swap-cache
105  *  - it's associated with the swap inode
106  */
107 void rw_swap_page(int rw, struct page *page, int wait)
108 {
109         swp_entry_t entry;
110 
111         entry.val = page->index;
112 
113         if (!PageLocked(page))
114                 PAGE_BUG(page);
115         if (!PageSwapCache(page))
116                 PAGE_BUG(page);
117         if (page->mapping != &swapper_space)
118                 PAGE_BUG(page);
119         if (!rw_swap_page_base(rw, entry, page, wait))
120                 UnlockPage(page);
121 }
122 
123 /*
124  * The swap lock map insists that pages be in the page cache!
125  * Therefore we can't use it.  Later when we can remove the need for the
126  * lock map and we can reduce the number of functions exported.
127  */
128 void rw_swap_page_nolock(int rw, swp_entry_t entry, char *buf, int wait)
129 {
130         struct page *page = virt_to_page(buf);
131         
132         if (!PageLocked(page))
133                 PAGE_BUG(page);
134         if (PageSwapCache(page))
135                 PAGE_BUG(page);
136         if (page->mapping)
137                 PAGE_BUG(page);
138         /* needs sync_page to wait I/O completation */
139         page->mapping = &swapper_space;
140         if (!rw_swap_page_base(rw, entry, page, wait))
141                 UnlockPage(page);
142         page->mapping = NULL;
143 }
144 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.