Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/mm/swapfile.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds |
| 5 | * Swap reorganised 29.12.95, Stephen Tweedie |
| 6 | */ |
| 7 | |
| 8 | #include <linux/config.h> |
| 9 | #include <linux/mm.h> |
| 10 | #include <linux/hugetlb.h> |
| 11 | #include <linux/mman.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/kernel_stat.h> |
| 14 | #include <linux/swap.h> |
| 15 | #include <linux/vmalloc.h> |
| 16 | #include <linux/pagemap.h> |
| 17 | #include <linux/namei.h> |
| 18 | #include <linux/shm.h> |
| 19 | #include <linux/blkdev.h> |
| 20 | #include <linux/writeback.h> |
| 21 | #include <linux/proc_fs.h> |
| 22 | #include <linux/seq_file.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/rmap.h> |
| 26 | #include <linux/security.h> |
| 27 | #include <linux/backing-dev.h> |
Ingo Molnar | fc0abb1 | 2006-01-18 17:42:33 -0800 | [diff] [blame] | 28 | #include <linux/mutex.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 29 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/syscalls.h> |
| 31 | |
| 32 | #include <asm/pgtable.h> |
| 33 | #include <asm/tlbflush.h> |
| 34 | #include <linux/swapops.h> |
| 35 | |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 36 | DEFINE_SPINLOCK(swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | unsigned int nr_swapfiles; |
| 38 | long total_swap_pages; |
| 39 | static int swap_overflow; |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | static const char Bad_file[] = "Bad swap file entry "; |
| 42 | static const char Unused_file[] = "Unused swap file entry "; |
| 43 | static const char Bad_offset[] = "Bad swap offset entry "; |
| 44 | static const char Unused_offset[] = "Unused swap offset entry "; |
| 45 | |
| 46 | struct swap_list_t swap_list = {-1, -1}; |
| 47 | |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 48 | static struct swap_info_struct swap_info[MAX_SWAPFILES]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Ingo Molnar | fc0abb1 | 2006-01-18 17:42:33 -0800 | [diff] [blame] | 50 | static DEFINE_MUTEX(swapon_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | /* |
| 53 | * We need this because the bdev->unplug_fn can sleep and we cannot |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 54 | * hold swap_lock while calling the unplug_fn. And swap_lock |
Ingo Molnar | fc0abb1 | 2006-01-18 17:42:33 -0800 | [diff] [blame] | 55 | * cannot be turned into a mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | */ |
| 57 | static DECLARE_RWSEM(swap_unplug_sem); |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page) |
| 60 | { |
| 61 | swp_entry_t entry; |
| 62 | |
| 63 | down_read(&swap_unplug_sem); |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 64 | entry.val = page_private(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | if (PageSwapCache(page)) { |
| 66 | struct block_device *bdev = swap_info[swp_type(entry)].bdev; |
| 67 | struct backing_dev_info *bdi; |
| 68 | |
| 69 | /* |
| 70 | * If the page is removed from swapcache from under us (with a |
| 71 | * racy try_to_unuse/swapoff) we need an additional reference |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 72 | * count to avoid reading garbage from page_private(page) above. |
| 73 | * If the WARN_ON triggers during a swapoff it maybe the race |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | * condition and it's harmless. However if it triggers without |
| 75 | * swapoff it signals a problem. |
| 76 | */ |
| 77 | WARN_ON(page_count(page) <= 1); |
| 78 | |
| 79 | bdi = bdev->bd_inode->i_mapping->backing_dev_info; |
McMullan, Jason | ba32311 | 2005-05-16 21:53:40 -0700 | [diff] [blame] | 80 | blk_run_backing_dev(bdi, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | up_read(&swap_unplug_sem); |
| 83 | } |
| 84 | |
Hugh Dickins | 048c27f | 2005-09-03 15:54:40 -0700 | [diff] [blame] | 85 | #define SWAPFILE_CLUSTER 256 |
| 86 | #define LATENCY_LIMIT 256 |
| 87 | |
Hugh Dickins | 6eb396d | 2005-09-03 15:54:35 -0700 | [diff] [blame] | 88 | static inline unsigned long scan_swap_map(struct swap_info_struct *si) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 90 | unsigned long offset, last_in_cluster; |
Hugh Dickins | 048c27f | 2005-09-03 15:54:40 -0700 | [diff] [blame] | 91 | int latency_ration = LATENCY_LIMIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 93 | /* |
| 94 | * We try to cluster swap pages by allocating them sequentially |
| 95 | * in swap. Once we've allocated SWAPFILE_CLUSTER pages this |
| 96 | * way, however, we resort to first-free allocation, starting |
| 97 | * a new cluster. This prevents us from scattering swap pages |
| 98 | * all over the entire swap partition, so that we reduce |
| 99 | * overall disk seek times between swap pages. -- sct |
| 100 | * But we do now try to find an empty cluster. -Andrea |
| 101 | */ |
| 102 | |
Hugh Dickins | 52b7efdb | 2005-09-03 15:54:39 -0700 | [diff] [blame] | 103 | si->flags += SWP_SCANNING; |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 104 | if (unlikely(!si->cluster_nr)) { |
| 105 | si->cluster_nr = SWAPFILE_CLUSTER - 1; |
| 106 | if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) |
| 107 | goto lowest; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 108 | spin_unlock(&swap_lock); |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 109 | |
| 110 | offset = si->lowest_bit; |
| 111 | last_in_cluster = offset + SWAPFILE_CLUSTER - 1; |
| 112 | |
| 113 | /* Locate the first empty (unaligned) cluster */ |
| 114 | for (; last_in_cluster <= si->highest_bit; offset++) { |
| 115 | if (si->swap_map[offset]) |
| 116 | last_in_cluster = offset + SWAPFILE_CLUSTER; |
| 117 | else if (offset == last_in_cluster) { |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 118 | spin_lock(&swap_lock); |
Akinobu Mita | 9b65ef5 | 2006-03-22 00:09:09 -0800 | [diff] [blame] | 119 | si->cluster_next = offset-SWAPFILE_CLUSTER+1; |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 120 | goto cluster; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
Hugh Dickins | 048c27f | 2005-09-03 15:54:40 -0700 | [diff] [blame] | 122 | if (unlikely(--latency_ration < 0)) { |
| 123 | cond_resched(); |
| 124 | latency_ration = LATENCY_LIMIT; |
| 125 | } |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 126 | } |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 127 | spin_lock(&swap_lock); |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 128 | goto lowest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 130 | |
| 131 | si->cluster_nr--; |
| 132 | cluster: |
| 133 | offset = si->cluster_next; |
| 134 | if (offset > si->highest_bit) |
| 135 | lowest: offset = si->lowest_bit; |
Hugh Dickins | 52b7efdb | 2005-09-03 15:54:39 -0700 | [diff] [blame] | 136 | checks: if (!(si->flags & SWP_WRITEOK)) |
| 137 | goto no_page; |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 138 | if (!si->highest_bit) |
| 139 | goto no_page; |
| 140 | if (!si->swap_map[offset]) { |
Hugh Dickins | 52b7efdb | 2005-09-03 15:54:39 -0700 | [diff] [blame] | 141 | if (offset == si->lowest_bit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | si->lowest_bit++; |
| 143 | if (offset == si->highest_bit) |
| 144 | si->highest_bit--; |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 145 | si->inuse_pages++; |
| 146 | if (si->inuse_pages == si->pages) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | si->lowest_bit = si->max; |
| 148 | si->highest_bit = 0; |
| 149 | } |
| 150 | si->swap_map[offset] = 1; |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 151 | si->cluster_next = offset + 1; |
Hugh Dickins | 52b7efdb | 2005-09-03 15:54:39 -0700 | [diff] [blame] | 152 | si->flags -= SWP_SCANNING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | return offset; |
| 154 | } |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 155 | |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 156 | spin_unlock(&swap_lock); |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 157 | while (++offset <= si->highest_bit) { |
Hugh Dickins | 52b7efdb | 2005-09-03 15:54:39 -0700 | [diff] [blame] | 158 | if (!si->swap_map[offset]) { |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 159 | spin_lock(&swap_lock); |
Hugh Dickins | 52b7efdb | 2005-09-03 15:54:39 -0700 | [diff] [blame] | 160 | goto checks; |
| 161 | } |
Hugh Dickins | 048c27f | 2005-09-03 15:54:40 -0700 | [diff] [blame] | 162 | if (unlikely(--latency_ration < 0)) { |
| 163 | cond_resched(); |
| 164 | latency_ration = LATENCY_LIMIT; |
| 165 | } |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 166 | } |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 167 | spin_lock(&swap_lock); |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 168 | goto lowest; |
| 169 | |
| 170 | no_page: |
Hugh Dickins | 52b7efdb | 2005-09-03 15:54:39 -0700 | [diff] [blame] | 171 | si->flags -= SWP_SCANNING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | swp_entry_t get_swap_page(void) |
| 176 | { |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 177 | struct swap_info_struct *si; |
| 178 | pgoff_t offset; |
| 179 | int type, next; |
| 180 | int wrapped = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 182 | spin_lock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | if (nr_swap_pages <= 0) |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 184 | goto noswap; |
| 185 | nr_swap_pages--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 187 | for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) { |
| 188 | si = swap_info + type; |
| 189 | next = si->next; |
| 190 | if (next < 0 || |
| 191 | (!wrapped && si->prio != swap_info[next].prio)) { |
| 192 | next = swap_list.head; |
| 193 | wrapped++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 195 | |
| 196 | if (!si->highest_bit) |
| 197 | continue; |
| 198 | if (!(si->flags & SWP_WRITEOK)) |
| 199 | continue; |
| 200 | |
| 201 | swap_list.next = next; |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 202 | offset = scan_swap_map(si); |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 203 | if (offset) { |
| 204 | spin_unlock(&swap_lock); |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 205 | return swp_entry(type, offset); |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 206 | } |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 207 | next = swap_list.next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 209 | |
| 210 | nr_swap_pages++; |
| 211 | noswap: |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 212 | spin_unlock(&swap_lock); |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 213 | return (swp_entry_t) {0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Rafael J. Wysocki | 3a291a2 | 2006-01-06 00:16:37 -0800 | [diff] [blame] | 216 | swp_entry_t get_swap_page_of_type(int type) |
| 217 | { |
| 218 | struct swap_info_struct *si; |
| 219 | pgoff_t offset; |
| 220 | |
| 221 | spin_lock(&swap_lock); |
| 222 | si = swap_info + type; |
| 223 | if (si->flags & SWP_WRITEOK) { |
| 224 | nr_swap_pages--; |
| 225 | offset = scan_swap_map(si); |
| 226 | if (offset) { |
| 227 | spin_unlock(&swap_lock); |
| 228 | return swp_entry(type, offset); |
| 229 | } |
| 230 | nr_swap_pages++; |
| 231 | } |
| 232 | spin_unlock(&swap_lock); |
| 233 | return (swp_entry_t) {0}; |
| 234 | } |
| 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | static struct swap_info_struct * swap_info_get(swp_entry_t entry) |
| 237 | { |
| 238 | struct swap_info_struct * p; |
| 239 | unsigned long offset, type; |
| 240 | |
| 241 | if (!entry.val) |
| 242 | goto out; |
| 243 | type = swp_type(entry); |
| 244 | if (type >= nr_swapfiles) |
| 245 | goto bad_nofile; |
| 246 | p = & swap_info[type]; |
| 247 | if (!(p->flags & SWP_USED)) |
| 248 | goto bad_device; |
| 249 | offset = swp_offset(entry); |
| 250 | if (offset >= p->max) |
| 251 | goto bad_offset; |
| 252 | if (!p->swap_map[offset]) |
| 253 | goto bad_free; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 254 | spin_lock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | return p; |
| 256 | |
| 257 | bad_free: |
| 258 | printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val); |
| 259 | goto out; |
| 260 | bad_offset: |
| 261 | printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val); |
| 262 | goto out; |
| 263 | bad_device: |
| 264 | printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val); |
| 265 | goto out; |
| 266 | bad_nofile: |
| 267 | printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val); |
| 268 | out: |
| 269 | return NULL; |
| 270 | } |
| 271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | static int swap_entry_free(struct swap_info_struct *p, unsigned long offset) |
| 273 | { |
| 274 | int count = p->swap_map[offset]; |
| 275 | |
| 276 | if (count < SWAP_MAP_MAX) { |
| 277 | count--; |
| 278 | p->swap_map[offset] = count; |
| 279 | if (!count) { |
| 280 | if (offset < p->lowest_bit) |
| 281 | p->lowest_bit = offset; |
| 282 | if (offset > p->highest_bit) |
| 283 | p->highest_bit = offset; |
Hugh Dickins | 89d09a2 | 2005-09-03 15:54:36 -0700 | [diff] [blame] | 284 | if (p->prio > swap_info[swap_list.next].prio) |
| 285 | swap_list.next = p - swap_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | nr_swap_pages++; |
| 287 | p->inuse_pages--; |
| 288 | } |
| 289 | } |
| 290 | return count; |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * Caller has made sure that the swapdevice corresponding to entry |
| 295 | * is still around or has not been recycled. |
| 296 | */ |
| 297 | void swap_free(swp_entry_t entry) |
| 298 | { |
| 299 | struct swap_info_struct * p; |
| 300 | |
| 301 | p = swap_info_get(entry); |
| 302 | if (p) { |
| 303 | swap_entry_free(p, swp_offset(entry)); |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 304 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
| 308 | /* |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 309 | * How many references to page are currently swapped out? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | */ |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 311 | static inline int page_swapcount(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | { |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 313 | int count = 0; |
| 314 | struct swap_info_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | swp_entry_t entry; |
| 316 | |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 317 | entry.val = page_private(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | p = swap_info_get(entry); |
| 319 | if (p) { |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 320 | /* Subtract the 1 for the swap cache itself */ |
| 321 | count = p->swap_map[swp_offset(entry)] - 1; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 322 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 324 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /* |
| 328 | * We can use this swap cache entry directly |
| 329 | * if there are no other references to it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | */ |
| 331 | int can_share_swap_page(struct page *page) |
| 332 | { |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 333 | int count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 335 | BUG_ON(!PageLocked(page)); |
| 336 | count = page_mapcount(page); |
| 337 | if (count <= 1 && PageSwapCache(page)) |
| 338 | count += page_swapcount(page); |
| 339 | return count == 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /* |
| 343 | * Work out if there are any other processes sharing this |
| 344 | * swap cache page. Free it if you can. Return success. |
| 345 | */ |
| 346 | int remove_exclusive_swap_page(struct page *page) |
| 347 | { |
| 348 | int retval; |
| 349 | struct swap_info_struct * p; |
| 350 | swp_entry_t entry; |
| 351 | |
| 352 | BUG_ON(PagePrivate(page)); |
| 353 | BUG_ON(!PageLocked(page)); |
| 354 | |
| 355 | if (!PageSwapCache(page)) |
| 356 | return 0; |
| 357 | if (PageWriteback(page)) |
| 358 | return 0; |
| 359 | if (page_count(page) != 2) /* 2: us + cache */ |
| 360 | return 0; |
| 361 | |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 362 | entry.val = page_private(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | p = swap_info_get(entry); |
| 364 | if (!p) |
| 365 | return 0; |
| 366 | |
| 367 | /* Is the only swap cache user the cache itself? */ |
| 368 | retval = 0; |
| 369 | if (p->swap_map[swp_offset(entry)] == 1) { |
| 370 | /* Recheck the page count with the swapcache lock held.. */ |
| 371 | write_lock_irq(&swapper_space.tree_lock); |
| 372 | if ((page_count(page) == 2) && !PageWriteback(page)) { |
| 373 | __delete_from_swap_cache(page); |
| 374 | SetPageDirty(page); |
| 375 | retval = 1; |
| 376 | } |
| 377 | write_unlock_irq(&swapper_space.tree_lock); |
| 378 | } |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 379 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
| 381 | if (retval) { |
| 382 | swap_free(entry); |
| 383 | page_cache_release(page); |
| 384 | } |
| 385 | |
| 386 | return retval; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Free the swap entry like above, but also try to |
| 391 | * free the page cache entry if it is the last user. |
| 392 | */ |
| 393 | void free_swap_and_cache(swp_entry_t entry) |
| 394 | { |
| 395 | struct swap_info_struct * p; |
| 396 | struct page *page = NULL; |
| 397 | |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame^] | 398 | if (is_migration_entry(entry)) |
| 399 | return; |
| 400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | p = swap_info_get(entry); |
| 402 | if (p) { |
Nick Piggin | 93fac70 | 2006-03-31 02:29:56 -0800 | [diff] [blame] | 403 | if (swap_entry_free(p, swp_offset(entry)) == 1) { |
| 404 | page = find_get_page(&swapper_space, entry.val); |
| 405 | if (page && unlikely(TestSetPageLocked(page))) { |
| 406 | page_cache_release(page); |
| 407 | page = NULL; |
| 408 | } |
| 409 | } |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 410 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } |
| 412 | if (page) { |
| 413 | int one_user; |
| 414 | |
| 415 | BUG_ON(PagePrivate(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | one_user = (page_count(page) == 2); |
| 417 | /* Only cache user (+us), or swap space full? Free it! */ |
Nick Piggin | 93fac70 | 2006-03-31 02:29:56 -0800 | [diff] [blame] | 418 | /* Also recheck PageSwapCache after page is locked (above) */ |
| 419 | if (PageSwapCache(page) && !PageWriteback(page) && |
| 420 | (one_user || vm_swap_full())) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | delete_from_swap_cache(page); |
| 422 | SetPageDirty(page); |
| 423 | } |
| 424 | unlock_page(page); |
| 425 | page_cache_release(page); |
| 426 | } |
| 427 | } |
| 428 | |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 429 | #ifdef CONFIG_SOFTWARE_SUSPEND |
| 430 | /* |
| 431 | * Find the swap type that corresponds to given device (if any) |
| 432 | * |
| 433 | * This is needed for software suspend and is done in such a way that inode |
| 434 | * aliasing is allowed. |
| 435 | */ |
| 436 | int swap_type_of(dev_t device) |
| 437 | { |
| 438 | int i; |
| 439 | |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 440 | spin_lock(&swap_lock); |
| 441 | for (i = 0; i < nr_swapfiles; i++) { |
| 442 | struct inode *inode; |
| 443 | |
| 444 | if (!(swap_info[i].flags & SWP_WRITEOK)) |
| 445 | continue; |
Rafael J. Wysocki | 6e1819d | 2006-03-23 03:00:03 -0800 | [diff] [blame] | 446 | if (!device) { |
| 447 | spin_unlock(&swap_lock); |
| 448 | return i; |
| 449 | } |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 450 | inode = swap_info->swap_file->f_dentry->d_inode; |
| 451 | if (S_ISBLK(inode->i_mode) && |
| 452 | device == MKDEV(imajor(inode), iminor(inode))) { |
| 453 | spin_unlock(&swap_lock); |
| 454 | return i; |
| 455 | } |
| 456 | } |
| 457 | spin_unlock(&swap_lock); |
| 458 | return -ENODEV; |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * Return either the total number of swap pages of given type, or the number |
| 463 | * of free pages of that type (depending on @free) |
| 464 | * |
| 465 | * This is needed for software suspend |
| 466 | */ |
| 467 | unsigned int count_swap_pages(int type, int free) |
| 468 | { |
| 469 | unsigned int n = 0; |
| 470 | |
| 471 | if (type < nr_swapfiles) { |
| 472 | spin_lock(&swap_lock); |
| 473 | if (swap_info[type].flags & SWP_WRITEOK) { |
| 474 | n = swap_info[type].pages; |
| 475 | if (free) |
| 476 | n -= swap_info[type].inuse_pages; |
| 477 | } |
| 478 | spin_unlock(&swap_lock); |
| 479 | } |
| 480 | return n; |
| 481 | } |
| 482 | #endif |
| 483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | /* |
Hugh Dickins | 72866f6 | 2005-10-29 18:15:55 -0700 | [diff] [blame] | 485 | * No need to decide whether this PTE shares the swap entry with others, |
| 486 | * just let do_wp_page work it out if a write is requested later - to |
| 487 | * force COW, vm_page_prot omits write permission from any private vma. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | */ |
| 489 | static void unuse_pte(struct vm_area_struct *vma, pte_t *pte, |
| 490 | unsigned long addr, swp_entry_t entry, struct page *page) |
| 491 | { |
Hugh Dickins | 4294621 | 2005-10-29 18:16:05 -0700 | [diff] [blame] | 492 | inc_mm_counter(vma->vm_mm, anon_rss); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | get_page(page); |
| 494 | set_pte_at(vma->vm_mm, addr, pte, |
| 495 | pte_mkold(mk_pte(page, vma->vm_page_prot))); |
| 496 | page_add_anon_rmap(page, vma, addr); |
| 497 | swap_free(entry); |
| 498 | /* |
| 499 | * Move the page to the active list so it is not |
| 500 | * immediately swapped out again after swapon. |
| 501 | */ |
| 502 | activate_page(page); |
| 503 | } |
| 504 | |
| 505 | static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, |
| 506 | unsigned long addr, unsigned long end, |
| 507 | swp_entry_t entry, struct page *page) |
| 508 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | pte_t swp_pte = swp_entry_to_pte(entry); |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 510 | pte_t *pte; |
| 511 | spinlock_t *ptl; |
| 512 | int found = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 514 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | do { |
| 516 | /* |
| 517 | * swapoff spends a _lot_ of time in this loop! |
| 518 | * Test inline before going to call unuse_pte. |
| 519 | */ |
| 520 | if (unlikely(pte_same(*pte, swp_pte))) { |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 521 | unuse_pte(vma, pte++, addr, entry, page); |
| 522 | found = 1; |
| 523 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | } |
| 525 | } while (pte++, addr += PAGE_SIZE, addr != end); |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 526 | pte_unmap_unlock(pte - 1, ptl); |
| 527 | return found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud, |
| 531 | unsigned long addr, unsigned long end, |
| 532 | swp_entry_t entry, struct page *page) |
| 533 | { |
| 534 | pmd_t *pmd; |
| 535 | unsigned long next; |
| 536 | |
| 537 | pmd = pmd_offset(pud, addr); |
| 538 | do { |
| 539 | next = pmd_addr_end(addr, end); |
| 540 | if (pmd_none_or_clear_bad(pmd)) |
| 541 | continue; |
| 542 | if (unuse_pte_range(vma, pmd, addr, next, entry, page)) |
| 543 | return 1; |
| 544 | } while (pmd++, addr = next, addr != end); |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd, |
| 549 | unsigned long addr, unsigned long end, |
| 550 | swp_entry_t entry, struct page *page) |
| 551 | { |
| 552 | pud_t *pud; |
| 553 | unsigned long next; |
| 554 | |
| 555 | pud = pud_offset(pgd, addr); |
| 556 | do { |
| 557 | next = pud_addr_end(addr, end); |
| 558 | if (pud_none_or_clear_bad(pud)) |
| 559 | continue; |
| 560 | if (unuse_pmd_range(vma, pud, addr, next, entry, page)) |
| 561 | return 1; |
| 562 | } while (pud++, addr = next, addr != end); |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | static int unuse_vma(struct vm_area_struct *vma, |
| 567 | swp_entry_t entry, struct page *page) |
| 568 | { |
| 569 | pgd_t *pgd; |
| 570 | unsigned long addr, end, next; |
| 571 | |
| 572 | if (page->mapping) { |
| 573 | addr = page_address_in_vma(page, vma); |
| 574 | if (addr == -EFAULT) |
| 575 | return 0; |
| 576 | else |
| 577 | end = addr + PAGE_SIZE; |
| 578 | } else { |
| 579 | addr = vma->vm_start; |
| 580 | end = vma->vm_end; |
| 581 | } |
| 582 | |
| 583 | pgd = pgd_offset(vma->vm_mm, addr); |
| 584 | do { |
| 585 | next = pgd_addr_end(addr, end); |
| 586 | if (pgd_none_or_clear_bad(pgd)) |
| 587 | continue; |
| 588 | if (unuse_pud_range(vma, pgd, addr, next, entry, page)) |
| 589 | return 1; |
| 590 | } while (pgd++, addr = next, addr != end); |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | static int unuse_mm(struct mm_struct *mm, |
| 595 | swp_entry_t entry, struct page *page) |
| 596 | { |
| 597 | struct vm_area_struct *vma; |
| 598 | |
| 599 | if (!down_read_trylock(&mm->mmap_sem)) { |
| 600 | /* |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 601 | * Activate page so shrink_cache is unlikely to unmap its |
| 602 | * ptes while lock is dropped, so swapoff can make progress. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | */ |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 604 | activate_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | unlock_page(page); |
| 606 | down_read(&mm->mmap_sem); |
| 607 | lock_page(page); |
| 608 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 610 | if (vma->anon_vma && unuse_vma(vma, entry, page)) |
| 611 | break; |
| 612 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | up_read(&mm->mmap_sem); |
| 614 | /* |
| 615 | * Currently unuse_mm cannot fail, but leave error handling |
| 616 | * at call sites for now, since we change it from time to time. |
| 617 | */ |
| 618 | return 0; |
| 619 | } |
| 620 | |
Christoph Lameter | a3351e5 | 2006-02-01 03:05:39 -0800 | [diff] [blame] | 621 | #ifdef CONFIG_MIGRATION |
| 622 | int remove_vma_swap(struct vm_area_struct *vma, struct page *page) |
| 623 | { |
| 624 | swp_entry_t entry = { .val = page_private(page) }; |
| 625 | |
| 626 | return unuse_vma(vma, entry, page); |
| 627 | } |
| 628 | #endif |
| 629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | /* |
| 631 | * Scan swap_map from current position to next entry still in use. |
| 632 | * Recycle to start on reaching the end, returning 0 when empty. |
| 633 | */ |
Hugh Dickins | 6eb396d | 2005-09-03 15:54:35 -0700 | [diff] [blame] | 634 | static unsigned int find_next_to_unuse(struct swap_info_struct *si, |
| 635 | unsigned int prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | { |
Hugh Dickins | 6eb396d | 2005-09-03 15:54:35 -0700 | [diff] [blame] | 637 | unsigned int max = si->max; |
| 638 | unsigned int i = prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | int count; |
| 640 | |
| 641 | /* |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 642 | * No need for swap_lock here: we're just looking |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | * for whether an entry is in use, not modifying it; false |
| 644 | * hits are okay, and sys_swapoff() has already prevented new |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 645 | * allocations from this area (while holding swap_lock). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | */ |
| 647 | for (;;) { |
| 648 | if (++i >= max) { |
| 649 | if (!prev) { |
| 650 | i = 0; |
| 651 | break; |
| 652 | } |
| 653 | /* |
| 654 | * No entries in use at top of swap_map, |
| 655 | * loop back to start and recheck there. |
| 656 | */ |
| 657 | max = prev + 1; |
| 658 | prev = 0; |
| 659 | i = 1; |
| 660 | } |
| 661 | count = si->swap_map[i]; |
| 662 | if (count && count != SWAP_MAP_BAD) |
| 663 | break; |
| 664 | } |
| 665 | return i; |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | * We completely avoid races by reading each swap page in advance, |
| 670 | * and then search for the process using it. All the necessary |
| 671 | * page table adjustments can then be made atomically. |
| 672 | */ |
| 673 | static int try_to_unuse(unsigned int type) |
| 674 | { |
| 675 | struct swap_info_struct * si = &swap_info[type]; |
| 676 | struct mm_struct *start_mm; |
| 677 | unsigned short *swap_map; |
| 678 | unsigned short swcount; |
| 679 | struct page *page; |
| 680 | swp_entry_t entry; |
Hugh Dickins | 6eb396d | 2005-09-03 15:54:35 -0700 | [diff] [blame] | 681 | unsigned int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | int retval = 0; |
| 683 | int reset_overflow = 0; |
| 684 | int shmem; |
| 685 | |
| 686 | /* |
| 687 | * When searching mms for an entry, a good strategy is to |
| 688 | * start at the first mm we freed the previous entry from |
| 689 | * (though actually we don't notice whether we or coincidence |
| 690 | * freed the entry). Initialize this start_mm with a hold. |
| 691 | * |
| 692 | * A simpler strategy would be to start at the last mm we |
| 693 | * freed the previous entry from; but that would take less |
| 694 | * advantage of mmlist ordering, which clusters forked mms |
| 695 | * together, child after parent. If we race with dup_mmap(), we |
| 696 | * prefer to resolve parent before child, lest we miss entries |
| 697 | * duplicated after we scanned child: using last mm would invert |
| 698 | * that. Though it's only a serious concern when an overflowed |
| 699 | * swap count is reset from SWAP_MAP_MAX, preventing a rescan. |
| 700 | */ |
| 701 | start_mm = &init_mm; |
| 702 | atomic_inc(&init_mm.mm_users); |
| 703 | |
| 704 | /* |
| 705 | * Keep on scanning until all entries have gone. Usually, |
| 706 | * one pass through swap_map is enough, but not necessarily: |
| 707 | * there are races when an instance of an entry might be missed. |
| 708 | */ |
| 709 | while ((i = find_next_to_unuse(si, i)) != 0) { |
| 710 | if (signal_pending(current)) { |
| 711 | retval = -EINTR; |
| 712 | break; |
| 713 | } |
| 714 | |
| 715 | /* |
| 716 | * Get a page for the entry, using the existing swap |
| 717 | * cache page if there is one. Otherwise, get a clean |
| 718 | * page and read the swap into it. |
| 719 | */ |
| 720 | swap_map = &si->swap_map[i]; |
| 721 | entry = swp_entry(type, i); |
| 722 | page = read_swap_cache_async(entry, NULL, 0); |
| 723 | if (!page) { |
| 724 | /* |
| 725 | * Either swap_duplicate() failed because entry |
| 726 | * has been freed independently, and will not be |
| 727 | * reused since sys_swapoff() already disabled |
| 728 | * allocation from here, or alloc_page() failed. |
| 729 | */ |
| 730 | if (!*swap_map) |
| 731 | continue; |
| 732 | retval = -ENOMEM; |
| 733 | break; |
| 734 | } |
| 735 | |
| 736 | /* |
| 737 | * Don't hold on to start_mm if it looks like exiting. |
| 738 | */ |
| 739 | if (atomic_read(&start_mm->mm_users) == 1) { |
| 740 | mmput(start_mm); |
| 741 | start_mm = &init_mm; |
| 742 | atomic_inc(&init_mm.mm_users); |
| 743 | } |
| 744 | |
| 745 | /* |
| 746 | * Wait for and lock page. When do_swap_page races with |
| 747 | * try_to_unuse, do_swap_page can handle the fault much |
| 748 | * faster than try_to_unuse can locate the entry. This |
| 749 | * apparently redundant "wait_on_page_locked" lets try_to_unuse |
| 750 | * defer to do_swap_page in such a case - in some tests, |
| 751 | * do_swap_page and try_to_unuse repeatedly compete. |
| 752 | */ |
| 753 | wait_on_page_locked(page); |
| 754 | wait_on_page_writeback(page); |
| 755 | lock_page(page); |
| 756 | wait_on_page_writeback(page); |
| 757 | |
| 758 | /* |
| 759 | * Remove all references to entry. |
| 760 | * Whenever we reach init_mm, there's no address space |
| 761 | * to search, but use it as a reminder to search shmem. |
| 762 | */ |
| 763 | shmem = 0; |
| 764 | swcount = *swap_map; |
| 765 | if (swcount > 1) { |
| 766 | if (start_mm == &init_mm) |
| 767 | shmem = shmem_unuse(entry, page); |
| 768 | else |
| 769 | retval = unuse_mm(start_mm, entry, page); |
| 770 | } |
| 771 | if (*swap_map > 1) { |
| 772 | int set_start_mm = (*swap_map >= swcount); |
| 773 | struct list_head *p = &start_mm->mmlist; |
| 774 | struct mm_struct *new_start_mm = start_mm; |
| 775 | struct mm_struct *prev_mm = start_mm; |
| 776 | struct mm_struct *mm; |
| 777 | |
| 778 | atomic_inc(&new_start_mm->mm_users); |
| 779 | atomic_inc(&prev_mm->mm_users); |
| 780 | spin_lock(&mmlist_lock); |
| 781 | while (*swap_map > 1 && !retval && |
| 782 | (p = p->next) != &start_mm->mmlist) { |
| 783 | mm = list_entry(p, struct mm_struct, mmlist); |
| 784 | if (atomic_inc_return(&mm->mm_users) == 1) { |
| 785 | atomic_dec(&mm->mm_users); |
| 786 | continue; |
| 787 | } |
| 788 | spin_unlock(&mmlist_lock); |
| 789 | mmput(prev_mm); |
| 790 | prev_mm = mm; |
| 791 | |
| 792 | cond_resched(); |
| 793 | |
| 794 | swcount = *swap_map; |
| 795 | if (swcount <= 1) |
| 796 | ; |
| 797 | else if (mm == &init_mm) { |
| 798 | set_start_mm = 1; |
| 799 | shmem = shmem_unuse(entry, page); |
| 800 | } else |
| 801 | retval = unuse_mm(mm, entry, page); |
| 802 | if (set_start_mm && *swap_map < swcount) { |
| 803 | mmput(new_start_mm); |
| 804 | atomic_inc(&mm->mm_users); |
| 805 | new_start_mm = mm; |
| 806 | set_start_mm = 0; |
| 807 | } |
| 808 | spin_lock(&mmlist_lock); |
| 809 | } |
| 810 | spin_unlock(&mmlist_lock); |
| 811 | mmput(prev_mm); |
| 812 | mmput(start_mm); |
| 813 | start_mm = new_start_mm; |
| 814 | } |
| 815 | if (retval) { |
| 816 | unlock_page(page); |
| 817 | page_cache_release(page); |
| 818 | break; |
| 819 | } |
| 820 | |
| 821 | /* |
| 822 | * How could swap count reach 0x7fff when the maximum |
| 823 | * pid is 0x7fff, and there's no way to repeat a swap |
| 824 | * page within an mm (except in shmem, where it's the |
| 825 | * shared object which takes the reference count)? |
| 826 | * We believe SWAP_MAP_MAX cannot occur in Linux 2.4. |
| 827 | * |
| 828 | * If that's wrong, then we should worry more about |
| 829 | * exit_mmap() and do_munmap() cases described above: |
| 830 | * we might be resetting SWAP_MAP_MAX too early here. |
| 831 | * We know "Undead"s can happen, they're okay, so don't |
| 832 | * report them; but do report if we reset SWAP_MAP_MAX. |
| 833 | */ |
| 834 | if (*swap_map == SWAP_MAP_MAX) { |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 835 | spin_lock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | *swap_map = 1; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 837 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | reset_overflow = 1; |
| 839 | } |
| 840 | |
| 841 | /* |
| 842 | * If a reference remains (rare), we would like to leave |
| 843 | * the page in the swap cache; but try_to_unmap could |
| 844 | * then re-duplicate the entry once we drop page lock, |
| 845 | * so we might loop indefinitely; also, that page could |
| 846 | * not be swapped out to other storage meanwhile. So: |
| 847 | * delete from cache even if there's another reference, |
| 848 | * after ensuring that the data has been saved to disk - |
| 849 | * since if the reference remains (rarer), it will be |
| 850 | * read from disk into another page. Splitting into two |
| 851 | * pages would be incorrect if swap supported "shared |
| 852 | * private" pages, but they are handled by tmpfs files. |
| 853 | * |
| 854 | * Note shmem_unuse already deleted a swappage from |
| 855 | * the swap cache, unless the move to filepage failed: |
| 856 | * in which case it left swappage in cache, lowered its |
| 857 | * swap count to pass quickly through the loops above, |
| 858 | * and now we must reincrement count to try again later. |
| 859 | */ |
| 860 | if ((*swap_map > 1) && PageDirty(page) && PageSwapCache(page)) { |
| 861 | struct writeback_control wbc = { |
| 862 | .sync_mode = WB_SYNC_NONE, |
| 863 | }; |
| 864 | |
| 865 | swap_writepage(page, &wbc); |
| 866 | lock_page(page); |
| 867 | wait_on_page_writeback(page); |
| 868 | } |
| 869 | if (PageSwapCache(page)) { |
| 870 | if (shmem) |
| 871 | swap_duplicate(entry); |
| 872 | else |
| 873 | delete_from_swap_cache(page); |
| 874 | } |
| 875 | |
| 876 | /* |
| 877 | * So we could skip searching mms once swap count went |
| 878 | * to 1, we did not mark any present ptes as dirty: must |
| 879 | * mark page dirty so shrink_list will preserve it. |
| 880 | */ |
| 881 | SetPageDirty(page); |
| 882 | unlock_page(page); |
| 883 | page_cache_release(page); |
| 884 | |
| 885 | /* |
| 886 | * Make sure that we aren't completely killing |
| 887 | * interactive performance. |
| 888 | */ |
| 889 | cond_resched(); |
| 890 | } |
| 891 | |
| 892 | mmput(start_mm); |
| 893 | if (reset_overflow) { |
| 894 | printk(KERN_WARNING "swapoff: cleared swap entry overflow\n"); |
| 895 | swap_overflow = 0; |
| 896 | } |
| 897 | return retval; |
| 898 | } |
| 899 | |
| 900 | /* |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 901 | * After a successful try_to_unuse, if no swap is now in use, we know |
| 902 | * we can empty the mmlist. swap_lock must be held on entry and exit. |
| 903 | * Note that mmlist_lock nests inside swap_lock, and an mm must be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | * added to the mmlist just after page_duplicate - before would be racy. |
| 905 | */ |
| 906 | static void drain_mmlist(void) |
| 907 | { |
| 908 | struct list_head *p, *next; |
| 909 | unsigned int i; |
| 910 | |
| 911 | for (i = 0; i < nr_swapfiles; i++) |
| 912 | if (swap_info[i].inuse_pages) |
| 913 | return; |
| 914 | spin_lock(&mmlist_lock); |
| 915 | list_for_each_safe(p, next, &init_mm.mmlist) |
| 916 | list_del_init(p); |
| 917 | spin_unlock(&mmlist_lock); |
| 918 | } |
| 919 | |
| 920 | /* |
| 921 | * Use this swapdev's extent info to locate the (PAGE_SIZE) block which |
| 922 | * corresponds to page offset `offset'. |
| 923 | */ |
| 924 | sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset) |
| 925 | { |
| 926 | struct swap_extent *se = sis->curr_swap_extent; |
| 927 | struct swap_extent *start_se = se; |
| 928 | |
| 929 | for ( ; ; ) { |
| 930 | struct list_head *lh; |
| 931 | |
| 932 | if (se->start_page <= offset && |
| 933 | offset < (se->start_page + se->nr_pages)) { |
| 934 | return se->start_block + (offset - se->start_page); |
| 935 | } |
Hugh Dickins | 11d3188 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 936 | lh = se->list.next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | if (lh == &sis->extent_list) |
Hugh Dickins | 11d3188 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 938 | lh = lh->next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | se = list_entry(lh, struct swap_extent, list); |
| 940 | sis->curr_swap_extent = se; |
| 941 | BUG_ON(se == start_se); /* It *must* be present */ |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | /* |
| 946 | * Free all of a swapdev's extent information |
| 947 | */ |
| 948 | static void destroy_swap_extents(struct swap_info_struct *sis) |
| 949 | { |
| 950 | while (!list_empty(&sis->extent_list)) { |
| 951 | struct swap_extent *se; |
| 952 | |
| 953 | se = list_entry(sis->extent_list.next, |
| 954 | struct swap_extent, list); |
| 955 | list_del(&se->list); |
| 956 | kfree(se); |
| 957 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | /* |
| 961 | * Add a block range (and the corresponding page range) into this swapdev's |
Hugh Dickins | 11d3188 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 962 | * extent list. The extent list is kept sorted in page order. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | * |
Hugh Dickins | 11d3188 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 964 | * This function rather assumes that it is called in ascending page order. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | */ |
| 966 | static int |
| 967 | add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, |
| 968 | unsigned long nr_pages, sector_t start_block) |
| 969 | { |
| 970 | struct swap_extent *se; |
| 971 | struct swap_extent *new_se; |
| 972 | struct list_head *lh; |
| 973 | |
Hugh Dickins | 11d3188 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 974 | lh = sis->extent_list.prev; /* The highest page extent */ |
| 975 | if (lh != &sis->extent_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | se = list_entry(lh, struct swap_extent, list); |
Hugh Dickins | 11d3188 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 977 | BUG_ON(se->start_page + se->nr_pages != start_page); |
| 978 | if (se->start_block + se->nr_pages == start_block) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | /* Merge it */ |
| 980 | se->nr_pages += nr_pages; |
| 981 | return 0; |
| 982 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | /* |
| 986 | * No merge. Insert a new extent, preserving ordering. |
| 987 | */ |
| 988 | new_se = kmalloc(sizeof(*se), GFP_KERNEL); |
| 989 | if (new_se == NULL) |
| 990 | return -ENOMEM; |
| 991 | new_se->start_page = start_page; |
| 992 | new_se->nr_pages = nr_pages; |
| 993 | new_se->start_block = start_block; |
| 994 | |
Hugh Dickins | 11d3188 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 995 | list_add_tail(&new_se->list, &sis->extent_list); |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 996 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | /* |
| 1000 | * A `swap extent' is a simple thing which maps a contiguous range of pages |
| 1001 | * onto a contiguous range of disk blocks. An ordered list of swap extents |
| 1002 | * is built at swapon time and is then used at swap_writepage/swap_readpage |
| 1003 | * time for locating where on disk a page belongs. |
| 1004 | * |
| 1005 | * If the swapfile is an S_ISBLK block device, a single extent is installed. |
| 1006 | * This is done so that the main operating code can treat S_ISBLK and S_ISREG |
| 1007 | * swap files identically. |
| 1008 | * |
| 1009 | * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap |
| 1010 | * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK |
| 1011 | * swapfiles are handled *identically* after swapon time. |
| 1012 | * |
| 1013 | * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks |
| 1014 | * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If |
| 1015 | * some stray blocks are found which do not fall within the PAGE_SIZE alignment |
| 1016 | * requirements, they are simply tossed out - we will never use those blocks |
| 1017 | * for swapping. |
| 1018 | * |
Hugh Dickins | b0d9bcd | 2005-09-03 15:54:31 -0700 | [diff] [blame] | 1019 | * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | * prevents root from shooting her foot off by ftruncating an in-use swapfile, |
| 1021 | * which will scribble on the fs. |
| 1022 | * |
| 1023 | * The amount of disk space which a single swap extent represents varies. |
| 1024 | * Typically it is in the 1-4 megabyte range. So we can have hundreds of |
| 1025 | * extents in the list. To avoid much list walking, we cache the previous |
| 1026 | * search location in `curr_swap_extent', and start new searches from there. |
| 1027 | * This is extremely effective. The average number of iterations in |
| 1028 | * map_swap_page() has been measured at about 0.3 per page. - akpm. |
| 1029 | */ |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1030 | static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | { |
| 1032 | struct inode *inode; |
| 1033 | unsigned blocks_per_page; |
| 1034 | unsigned long page_no; |
| 1035 | unsigned blkbits; |
| 1036 | sector_t probe_block; |
| 1037 | sector_t last_block; |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1038 | sector_t lowest_block = -1; |
| 1039 | sector_t highest_block = 0; |
| 1040 | int nr_extents = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | int ret; |
| 1042 | |
| 1043 | inode = sis->swap_file->f_mapping->host; |
| 1044 | if (S_ISBLK(inode->i_mode)) { |
| 1045 | ret = add_swap_extent(sis, 0, sis->max, 0); |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1046 | *span = sis->pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | goto done; |
| 1048 | } |
| 1049 | |
| 1050 | blkbits = inode->i_blkbits; |
| 1051 | blocks_per_page = PAGE_SIZE >> blkbits; |
| 1052 | |
| 1053 | /* |
| 1054 | * Map all the blocks into the extent list. This code doesn't try |
| 1055 | * to be very smart. |
| 1056 | */ |
| 1057 | probe_block = 0; |
| 1058 | page_no = 0; |
| 1059 | last_block = i_size_read(inode) >> blkbits; |
| 1060 | while ((probe_block + blocks_per_page) <= last_block && |
| 1061 | page_no < sis->max) { |
| 1062 | unsigned block_in_page; |
| 1063 | sector_t first_block; |
| 1064 | |
| 1065 | first_block = bmap(inode, probe_block); |
| 1066 | if (first_block == 0) |
| 1067 | goto bad_bmap; |
| 1068 | |
| 1069 | /* |
| 1070 | * It must be PAGE_SIZE aligned on-disk |
| 1071 | */ |
| 1072 | if (first_block & (blocks_per_page - 1)) { |
| 1073 | probe_block++; |
| 1074 | goto reprobe; |
| 1075 | } |
| 1076 | |
| 1077 | for (block_in_page = 1; block_in_page < blocks_per_page; |
| 1078 | block_in_page++) { |
| 1079 | sector_t block; |
| 1080 | |
| 1081 | block = bmap(inode, probe_block + block_in_page); |
| 1082 | if (block == 0) |
| 1083 | goto bad_bmap; |
| 1084 | if (block != first_block + block_in_page) { |
| 1085 | /* Discontiguity */ |
| 1086 | probe_block++; |
| 1087 | goto reprobe; |
| 1088 | } |
| 1089 | } |
| 1090 | |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1091 | first_block >>= (PAGE_SHIFT - blkbits); |
| 1092 | if (page_no) { /* exclude the header page */ |
| 1093 | if (first_block < lowest_block) |
| 1094 | lowest_block = first_block; |
| 1095 | if (first_block > highest_block) |
| 1096 | highest_block = first_block; |
| 1097 | } |
| 1098 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | /* |
| 1100 | * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks |
| 1101 | */ |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1102 | ret = add_swap_extent(sis, page_no, 1, first_block); |
| 1103 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | goto out; |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1105 | nr_extents += ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | page_no++; |
| 1107 | probe_block += blocks_per_page; |
| 1108 | reprobe: |
| 1109 | continue; |
| 1110 | } |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1111 | ret = nr_extents; |
| 1112 | *span = 1 + highest_block - lowest_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | if (page_no == 0) |
Hugh Dickins | e2244ec | 2005-09-03 15:54:32 -0700 | [diff] [blame] | 1114 | page_no = 1; /* force Empty message */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | sis->max = page_no; |
Hugh Dickins | e2244ec | 2005-09-03 15:54:32 -0700 | [diff] [blame] | 1116 | sis->pages = page_no - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | sis->highest_bit = page_no - 1; |
| 1118 | done: |
| 1119 | sis->curr_swap_extent = list_entry(sis->extent_list.prev, |
| 1120 | struct swap_extent, list); |
| 1121 | goto out; |
| 1122 | bad_bmap: |
| 1123 | printk(KERN_ERR "swapon: swapfile has holes\n"); |
| 1124 | ret = -EINVAL; |
| 1125 | out: |
| 1126 | return ret; |
| 1127 | } |
| 1128 | |
| 1129 | #if 0 /* We don't need this yet */ |
| 1130 | #include <linux/backing-dev.h> |
| 1131 | int page_queue_congested(struct page *page) |
| 1132 | { |
| 1133 | struct backing_dev_info *bdi; |
| 1134 | |
| 1135 | BUG_ON(!PageLocked(page)); /* It pins the swap_info_struct */ |
| 1136 | |
| 1137 | if (PageSwapCache(page)) { |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 1138 | swp_entry_t entry = { .val = page_private(page) }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | struct swap_info_struct *sis; |
| 1140 | |
| 1141 | sis = get_swap_info_struct(swp_type(entry)); |
| 1142 | bdi = sis->bdev->bd_inode->i_mapping->backing_dev_info; |
| 1143 | } else |
| 1144 | bdi = page->mapping->backing_dev_info; |
| 1145 | return bdi_write_congested(bdi); |
| 1146 | } |
| 1147 | #endif |
| 1148 | |
| 1149 | asmlinkage long sys_swapoff(const char __user * specialfile) |
| 1150 | { |
| 1151 | struct swap_info_struct * p = NULL; |
| 1152 | unsigned short *swap_map; |
| 1153 | struct file *swap_file, *victim; |
| 1154 | struct address_space *mapping; |
| 1155 | struct inode *inode; |
| 1156 | char * pathname; |
| 1157 | int i, type, prev; |
| 1158 | int err; |
| 1159 | |
| 1160 | if (!capable(CAP_SYS_ADMIN)) |
| 1161 | return -EPERM; |
| 1162 | |
| 1163 | pathname = getname(specialfile); |
| 1164 | err = PTR_ERR(pathname); |
| 1165 | if (IS_ERR(pathname)) |
| 1166 | goto out; |
| 1167 | |
| 1168 | victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0); |
| 1169 | putname(pathname); |
| 1170 | err = PTR_ERR(victim); |
| 1171 | if (IS_ERR(victim)) |
| 1172 | goto out; |
| 1173 | |
| 1174 | mapping = victim->f_mapping; |
| 1175 | prev = -1; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1176 | spin_lock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | for (type = swap_list.head; type >= 0; type = swap_info[type].next) { |
| 1178 | p = swap_info + type; |
| 1179 | if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) { |
| 1180 | if (p->swap_file->f_mapping == mapping) |
| 1181 | break; |
| 1182 | } |
| 1183 | prev = type; |
| 1184 | } |
| 1185 | if (type < 0) { |
| 1186 | err = -EINVAL; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1187 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | goto out_dput; |
| 1189 | } |
| 1190 | if (!security_vm_enough_memory(p->pages)) |
| 1191 | vm_unacct_memory(p->pages); |
| 1192 | else { |
| 1193 | err = -ENOMEM; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1194 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | goto out_dput; |
| 1196 | } |
| 1197 | if (prev < 0) { |
| 1198 | swap_list.head = p->next; |
| 1199 | } else { |
| 1200 | swap_info[prev].next = p->next; |
| 1201 | } |
| 1202 | if (type == swap_list.next) { |
| 1203 | /* just pick something that's safe... */ |
| 1204 | swap_list.next = swap_list.head; |
| 1205 | } |
| 1206 | nr_swap_pages -= p->pages; |
| 1207 | total_swap_pages -= p->pages; |
| 1208 | p->flags &= ~SWP_WRITEOK; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1209 | spin_unlock(&swap_lock); |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 1210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | current->flags |= PF_SWAPOFF; |
| 1212 | err = try_to_unuse(type); |
| 1213 | current->flags &= ~PF_SWAPOFF; |
| 1214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | if (err) { |
| 1216 | /* re-insert swap space back into swap_list */ |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1217 | spin_lock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | for (prev = -1, i = swap_list.head; i >= 0; prev = i, i = swap_info[i].next) |
| 1219 | if (p->prio >= swap_info[i].prio) |
| 1220 | break; |
| 1221 | p->next = i; |
| 1222 | if (prev < 0) |
| 1223 | swap_list.head = swap_list.next = p - swap_info; |
| 1224 | else |
| 1225 | swap_info[prev].next = p - swap_info; |
| 1226 | nr_swap_pages += p->pages; |
| 1227 | total_swap_pages += p->pages; |
| 1228 | p->flags |= SWP_WRITEOK; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1229 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | goto out_dput; |
| 1231 | } |
Hugh Dickins | 52b7efdb | 2005-09-03 15:54:39 -0700 | [diff] [blame] | 1232 | |
| 1233 | /* wait for any unplug function to finish */ |
| 1234 | down_write(&swap_unplug_sem); |
| 1235 | up_write(&swap_unplug_sem); |
| 1236 | |
Hugh Dickins | 4cd3bb1 | 2005-09-03 15:54:33 -0700 | [diff] [blame] | 1237 | destroy_swap_extents(p); |
Ingo Molnar | fc0abb1 | 2006-01-18 17:42:33 -0800 | [diff] [blame] | 1238 | mutex_lock(&swapon_mutex); |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1239 | spin_lock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | drain_mmlist(); |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1241 | |
| 1242 | /* wait for anyone still in scan_swap_map */ |
| 1243 | p->highest_bit = 0; /* cuts scans short */ |
| 1244 | while (p->flags >= SWP_SCANNING) { |
| 1245 | spin_unlock(&swap_lock); |
Nishanth Aravamudan | 13e4b57 | 2005-09-10 00:27:25 -0700 | [diff] [blame] | 1246 | schedule_timeout_uninterruptible(1); |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1247 | spin_lock(&swap_lock); |
| 1248 | } |
| 1249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | swap_file = p->swap_file; |
| 1251 | p->swap_file = NULL; |
| 1252 | p->max = 0; |
| 1253 | swap_map = p->swap_map; |
| 1254 | p->swap_map = NULL; |
| 1255 | p->flags = 0; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1256 | spin_unlock(&swap_lock); |
Ingo Molnar | fc0abb1 | 2006-01-18 17:42:33 -0800 | [diff] [blame] | 1257 | mutex_unlock(&swapon_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | vfree(swap_map); |
| 1259 | inode = mapping->host; |
| 1260 | if (S_ISBLK(inode->i_mode)) { |
| 1261 | struct block_device *bdev = I_BDEV(inode); |
| 1262 | set_blocksize(bdev, p->old_block_size); |
| 1263 | bd_release(bdev); |
| 1264 | } else { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1265 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | inode->i_flags &= ~S_SWAPFILE; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1267 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | } |
| 1269 | filp_close(swap_file, NULL); |
| 1270 | err = 0; |
| 1271 | |
| 1272 | out_dput: |
| 1273 | filp_close(victim, NULL); |
| 1274 | out: |
| 1275 | return err; |
| 1276 | } |
| 1277 | |
| 1278 | #ifdef CONFIG_PROC_FS |
| 1279 | /* iterator */ |
| 1280 | static void *swap_start(struct seq_file *swap, loff_t *pos) |
| 1281 | { |
| 1282 | struct swap_info_struct *ptr = swap_info; |
| 1283 | int i; |
| 1284 | loff_t l = *pos; |
| 1285 | |
Ingo Molnar | fc0abb1 | 2006-01-18 17:42:33 -0800 | [diff] [blame] | 1286 | mutex_lock(&swapon_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | |
| 1288 | for (i = 0; i < nr_swapfiles; i++, ptr++) { |
| 1289 | if (!(ptr->flags & SWP_USED) || !ptr->swap_map) |
| 1290 | continue; |
| 1291 | if (!l--) |
| 1292 | return ptr; |
| 1293 | } |
| 1294 | |
| 1295 | return NULL; |
| 1296 | } |
| 1297 | |
| 1298 | static void *swap_next(struct seq_file *swap, void *v, loff_t *pos) |
| 1299 | { |
| 1300 | struct swap_info_struct *ptr = v; |
| 1301 | struct swap_info_struct *endptr = swap_info + nr_swapfiles; |
| 1302 | |
| 1303 | for (++ptr; ptr < endptr; ptr++) { |
| 1304 | if (!(ptr->flags & SWP_USED) || !ptr->swap_map) |
| 1305 | continue; |
| 1306 | ++*pos; |
| 1307 | return ptr; |
| 1308 | } |
| 1309 | |
| 1310 | return NULL; |
| 1311 | } |
| 1312 | |
| 1313 | static void swap_stop(struct seq_file *swap, void *v) |
| 1314 | { |
Ingo Molnar | fc0abb1 | 2006-01-18 17:42:33 -0800 | [diff] [blame] | 1315 | mutex_unlock(&swapon_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | static int swap_show(struct seq_file *swap, void *v) |
| 1319 | { |
| 1320 | struct swap_info_struct *ptr = v; |
| 1321 | struct file *file; |
| 1322 | int len; |
| 1323 | |
| 1324 | if (v == swap_info) |
| 1325 | seq_puts(swap, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n"); |
| 1326 | |
| 1327 | file = ptr->swap_file; |
| 1328 | len = seq_path(swap, file->f_vfsmnt, file->f_dentry, " \t\n\\"); |
Hugh Dickins | 6eb396d | 2005-09-03 15:54:35 -0700 | [diff] [blame] | 1329 | seq_printf(swap, "%*s%s\t%u\t%u\t%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | len < 40 ? 40 - len : 1, " ", |
| 1331 | S_ISBLK(file->f_dentry->d_inode->i_mode) ? |
| 1332 | "partition" : "file\t", |
| 1333 | ptr->pages << (PAGE_SHIFT - 10), |
| 1334 | ptr->inuse_pages << (PAGE_SHIFT - 10), |
| 1335 | ptr->prio); |
| 1336 | return 0; |
| 1337 | } |
| 1338 | |
| 1339 | static struct seq_operations swaps_op = { |
| 1340 | .start = swap_start, |
| 1341 | .next = swap_next, |
| 1342 | .stop = swap_stop, |
| 1343 | .show = swap_show |
| 1344 | }; |
| 1345 | |
| 1346 | static int swaps_open(struct inode *inode, struct file *file) |
| 1347 | { |
| 1348 | return seq_open(file, &swaps_op); |
| 1349 | } |
| 1350 | |
| 1351 | static struct file_operations proc_swaps_operations = { |
| 1352 | .open = swaps_open, |
| 1353 | .read = seq_read, |
| 1354 | .llseek = seq_lseek, |
| 1355 | .release = seq_release, |
| 1356 | }; |
| 1357 | |
| 1358 | static int __init procswaps_init(void) |
| 1359 | { |
| 1360 | struct proc_dir_entry *entry; |
| 1361 | |
| 1362 | entry = create_proc_entry("swaps", 0, NULL); |
| 1363 | if (entry) |
| 1364 | entry->proc_fops = &proc_swaps_operations; |
| 1365 | return 0; |
| 1366 | } |
| 1367 | __initcall(procswaps_init); |
| 1368 | #endif /* CONFIG_PROC_FS */ |
| 1369 | |
| 1370 | /* |
| 1371 | * Written 01/25/92 by Simmule Turner, heavily changed by Linus. |
| 1372 | * |
| 1373 | * The swapon system call |
| 1374 | */ |
| 1375 | asmlinkage long sys_swapon(const char __user * specialfile, int swap_flags) |
| 1376 | { |
| 1377 | struct swap_info_struct * p; |
| 1378 | char *name = NULL; |
| 1379 | struct block_device *bdev = NULL; |
| 1380 | struct file *swap_file = NULL; |
| 1381 | struct address_space *mapping; |
| 1382 | unsigned int type; |
| 1383 | int i, prev; |
| 1384 | int error; |
| 1385 | static int least_priority; |
| 1386 | union swap_header *swap_header = NULL; |
| 1387 | int swap_header_version; |
Hugh Dickins | 6eb396d | 2005-09-03 15:54:35 -0700 | [diff] [blame] | 1388 | unsigned int nr_good_pages = 0; |
| 1389 | int nr_extents = 0; |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1390 | sector_t span; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | unsigned long maxpages = 1; |
| 1392 | int swapfilesize; |
| 1393 | unsigned short *swap_map; |
| 1394 | struct page *page = NULL; |
| 1395 | struct inode *inode = NULL; |
| 1396 | int did_down = 0; |
| 1397 | |
| 1398 | if (!capable(CAP_SYS_ADMIN)) |
| 1399 | return -EPERM; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1400 | spin_lock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | p = swap_info; |
| 1402 | for (type = 0 ; type < nr_swapfiles ; type++,p++) |
| 1403 | if (!(p->flags & SWP_USED)) |
| 1404 | break; |
| 1405 | error = -EPERM; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame^] | 1406 | if (type >= MAX_SWAPFILES) { |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1407 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | goto out; |
| 1409 | } |
| 1410 | if (type >= nr_swapfiles) |
| 1411 | nr_swapfiles = type+1; |
| 1412 | INIT_LIST_HEAD(&p->extent_list); |
| 1413 | p->flags = SWP_USED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | p->swap_file = NULL; |
| 1415 | p->old_block_size = 0; |
| 1416 | p->swap_map = NULL; |
| 1417 | p->lowest_bit = 0; |
| 1418 | p->highest_bit = 0; |
| 1419 | p->cluster_nr = 0; |
| 1420 | p->inuse_pages = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | p->next = -1; |
| 1422 | if (swap_flags & SWAP_FLAG_PREFER) { |
| 1423 | p->prio = |
| 1424 | (swap_flags & SWAP_FLAG_PRIO_MASK)>>SWAP_FLAG_PRIO_SHIFT; |
| 1425 | } else { |
| 1426 | p->prio = --least_priority; |
| 1427 | } |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1428 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | name = getname(specialfile); |
| 1430 | error = PTR_ERR(name); |
| 1431 | if (IS_ERR(name)) { |
| 1432 | name = NULL; |
| 1433 | goto bad_swap_2; |
| 1434 | } |
| 1435 | swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0); |
| 1436 | error = PTR_ERR(swap_file); |
| 1437 | if (IS_ERR(swap_file)) { |
| 1438 | swap_file = NULL; |
| 1439 | goto bad_swap_2; |
| 1440 | } |
| 1441 | |
| 1442 | p->swap_file = swap_file; |
| 1443 | mapping = swap_file->f_mapping; |
| 1444 | inode = mapping->host; |
| 1445 | |
| 1446 | error = -EBUSY; |
| 1447 | for (i = 0; i < nr_swapfiles; i++) { |
| 1448 | struct swap_info_struct *q = &swap_info[i]; |
| 1449 | |
| 1450 | if (i == type || !q->swap_file) |
| 1451 | continue; |
| 1452 | if (mapping == q->swap_file->f_mapping) |
| 1453 | goto bad_swap; |
| 1454 | } |
| 1455 | |
| 1456 | error = -EINVAL; |
| 1457 | if (S_ISBLK(inode->i_mode)) { |
| 1458 | bdev = I_BDEV(inode); |
| 1459 | error = bd_claim(bdev, sys_swapon); |
| 1460 | if (error < 0) { |
| 1461 | bdev = NULL; |
Rob Landley | f7b3a43 | 2005-09-22 21:44:27 -0700 | [diff] [blame] | 1462 | error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | goto bad_swap; |
| 1464 | } |
| 1465 | p->old_block_size = block_size(bdev); |
| 1466 | error = set_blocksize(bdev, PAGE_SIZE); |
| 1467 | if (error < 0) |
| 1468 | goto bad_swap; |
| 1469 | p->bdev = bdev; |
| 1470 | } else if (S_ISREG(inode->i_mode)) { |
| 1471 | p->bdev = inode->i_sb->s_bdev; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1472 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | did_down = 1; |
| 1474 | if (IS_SWAPFILE(inode)) { |
| 1475 | error = -EBUSY; |
| 1476 | goto bad_swap; |
| 1477 | } |
| 1478 | } else { |
| 1479 | goto bad_swap; |
| 1480 | } |
| 1481 | |
| 1482 | swapfilesize = i_size_read(inode) >> PAGE_SHIFT; |
| 1483 | |
| 1484 | /* |
| 1485 | * Read the swap header. |
| 1486 | */ |
| 1487 | if (!mapping->a_ops->readpage) { |
| 1488 | error = -EINVAL; |
| 1489 | goto bad_swap; |
| 1490 | } |
| 1491 | page = read_cache_page(mapping, 0, |
| 1492 | (filler_t *)mapping->a_ops->readpage, swap_file); |
| 1493 | if (IS_ERR(page)) { |
| 1494 | error = PTR_ERR(page); |
| 1495 | goto bad_swap; |
| 1496 | } |
| 1497 | wait_on_page_locked(page); |
| 1498 | if (!PageUptodate(page)) |
| 1499 | goto bad_swap; |
| 1500 | kmap(page); |
| 1501 | swap_header = page_address(page); |
| 1502 | |
| 1503 | if (!memcmp("SWAP-SPACE",swap_header->magic.magic,10)) |
| 1504 | swap_header_version = 1; |
| 1505 | else if (!memcmp("SWAPSPACE2",swap_header->magic.magic,10)) |
| 1506 | swap_header_version = 2; |
| 1507 | else { |
Jesper Juhl | e97a311 | 2006-01-11 01:50:28 +0100 | [diff] [blame] | 1508 | printk(KERN_ERR "Unable to find swap-space signature\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | error = -EINVAL; |
| 1510 | goto bad_swap; |
| 1511 | } |
| 1512 | |
| 1513 | switch (swap_header_version) { |
| 1514 | case 1: |
| 1515 | printk(KERN_ERR "version 0 swap is no longer supported. " |
| 1516 | "Use mkswap -v1 %s\n", name); |
| 1517 | error = -EINVAL; |
| 1518 | goto bad_swap; |
| 1519 | case 2: |
| 1520 | /* Check the swap header's sub-version and the size of |
| 1521 | the swap file and bad block lists */ |
| 1522 | if (swap_header->info.version != 1) { |
| 1523 | printk(KERN_WARNING |
| 1524 | "Unable to handle swap header version %d\n", |
| 1525 | swap_header->info.version); |
| 1526 | error = -EINVAL; |
| 1527 | goto bad_swap; |
| 1528 | } |
| 1529 | |
| 1530 | p->lowest_bit = 1; |
Hugh Dickins | 52b7efdb | 2005-09-03 15:54:39 -0700 | [diff] [blame] | 1531 | p->cluster_next = 1; |
| 1532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | /* |
| 1534 | * Find out how many pages are allowed for a single swap |
| 1535 | * device. There are two limiting factors: 1) the number of |
| 1536 | * bits for the swap offset in the swp_entry_t type and |
| 1537 | * 2) the number of bits in the a swap pte as defined by |
| 1538 | * the different architectures. In order to find the |
| 1539 | * largest possible bit mask a swap entry with swap type 0 |
| 1540 | * and swap offset ~0UL is created, encoded to a swap pte, |
| 1541 | * decoded to a swp_entry_t again and finally the swap |
| 1542 | * offset is extracted. This will mask all the bits from |
| 1543 | * the initial ~0UL mask that can't be encoded in either |
| 1544 | * the swp_entry_t or the architecture definition of a |
| 1545 | * swap pte. |
| 1546 | */ |
| 1547 | maxpages = swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0,~0UL)))) - 1; |
| 1548 | if (maxpages > swap_header->info.last_page) |
| 1549 | maxpages = swap_header->info.last_page; |
| 1550 | p->highest_bit = maxpages - 1; |
| 1551 | |
| 1552 | error = -EINVAL; |
Hugh Dickins | e2244ec | 2005-09-03 15:54:32 -0700 | [diff] [blame] | 1553 | if (!maxpages) |
| 1554 | goto bad_swap; |
| 1555 | if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode)) |
| 1556 | goto bad_swap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) |
| 1558 | goto bad_swap; |
Tobias Klauser | cd105df | 2006-01-08 01:00:59 -0800 | [diff] [blame] | 1559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | /* OK, set up the swap map and apply the bad block list */ |
| 1561 | if (!(p->swap_map = vmalloc(maxpages * sizeof(short)))) { |
| 1562 | error = -ENOMEM; |
| 1563 | goto bad_swap; |
| 1564 | } |
| 1565 | |
| 1566 | error = 0; |
| 1567 | memset(p->swap_map, 0, maxpages * sizeof(short)); |
Tobias Klauser | cd105df | 2006-01-08 01:00:59 -0800 | [diff] [blame] | 1568 | for (i = 0; i < swap_header->info.nr_badpages; i++) { |
| 1569 | int page_nr = swap_header->info.badpages[i]; |
| 1570 | if (page_nr <= 0 || page_nr >= swap_header->info.last_page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | error = -EINVAL; |
| 1572 | else |
Tobias Klauser | cd105df | 2006-01-08 01:00:59 -0800 | [diff] [blame] | 1573 | p->swap_map[page_nr] = SWAP_MAP_BAD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | } |
| 1575 | nr_good_pages = swap_header->info.last_page - |
| 1576 | swap_header->info.nr_badpages - |
| 1577 | 1 /* header page */; |
Tobias Klauser | cd105df | 2006-01-08 01:00:59 -0800 | [diff] [blame] | 1578 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | goto bad_swap; |
| 1580 | } |
Hugh Dickins | e2244ec | 2005-09-03 15:54:32 -0700 | [diff] [blame] | 1581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | if (swapfilesize && maxpages > swapfilesize) { |
| 1583 | printk(KERN_WARNING |
| 1584 | "Swap area shorter than signature indicates\n"); |
| 1585 | error = -EINVAL; |
| 1586 | goto bad_swap; |
| 1587 | } |
Hugh Dickins | e2244ec | 2005-09-03 15:54:32 -0700 | [diff] [blame] | 1588 | if (nr_good_pages) { |
| 1589 | p->swap_map[0] = SWAP_MAP_BAD; |
| 1590 | p->max = maxpages; |
| 1591 | p->pages = nr_good_pages; |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1592 | nr_extents = setup_swap_extents(p, &span); |
| 1593 | if (nr_extents < 0) { |
| 1594 | error = nr_extents; |
Hugh Dickins | e2244ec | 2005-09-03 15:54:32 -0700 | [diff] [blame] | 1595 | goto bad_swap; |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1596 | } |
Hugh Dickins | e2244ec | 2005-09-03 15:54:32 -0700 | [diff] [blame] | 1597 | nr_good_pages = p->pages; |
| 1598 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | if (!nr_good_pages) { |
| 1600 | printk(KERN_WARNING "Empty swap-file\n"); |
| 1601 | error = -EINVAL; |
| 1602 | goto bad_swap; |
| 1603 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | |
Ingo Molnar | fc0abb1 | 2006-01-18 17:42:33 -0800 | [diff] [blame] | 1605 | mutex_lock(&swapon_mutex); |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1606 | spin_lock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | p->flags = SWP_ACTIVE; |
| 1608 | nr_swap_pages += nr_good_pages; |
| 1609 | total_swap_pages += nr_good_pages; |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1610 | |
Hugh Dickins | 6eb396d | 2005-09-03 15:54:35 -0700 | [diff] [blame] | 1611 | printk(KERN_INFO "Adding %uk swap on %s. " |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1612 | "Priority:%d extents:%d across:%lluk\n", |
| 1613 | nr_good_pages<<(PAGE_SHIFT-10), name, p->prio, |
| 1614 | nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | |
| 1616 | /* insert swap space into swap_list: */ |
| 1617 | prev = -1; |
| 1618 | for (i = swap_list.head; i >= 0; i = swap_info[i].next) { |
| 1619 | if (p->prio >= swap_info[i].prio) { |
| 1620 | break; |
| 1621 | } |
| 1622 | prev = i; |
| 1623 | } |
| 1624 | p->next = i; |
| 1625 | if (prev < 0) { |
| 1626 | swap_list.head = swap_list.next = p - swap_info; |
| 1627 | } else { |
| 1628 | swap_info[prev].next = p - swap_info; |
| 1629 | } |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1630 | spin_unlock(&swap_lock); |
Ingo Molnar | fc0abb1 | 2006-01-18 17:42:33 -0800 | [diff] [blame] | 1631 | mutex_unlock(&swapon_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | error = 0; |
| 1633 | goto out; |
| 1634 | bad_swap: |
| 1635 | if (bdev) { |
| 1636 | set_blocksize(bdev, p->old_block_size); |
| 1637 | bd_release(bdev); |
| 1638 | } |
Hugh Dickins | 4cd3bb1 | 2005-09-03 15:54:33 -0700 | [diff] [blame] | 1639 | destroy_swap_extents(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | bad_swap_2: |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1641 | spin_lock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | swap_map = p->swap_map; |
| 1643 | p->swap_file = NULL; |
| 1644 | p->swap_map = NULL; |
| 1645 | p->flags = 0; |
| 1646 | if (!(swap_flags & SWAP_FLAG_PREFER)) |
| 1647 | ++least_priority; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1648 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | vfree(swap_map); |
| 1650 | if (swap_file) |
| 1651 | filp_close(swap_file, NULL); |
| 1652 | out: |
| 1653 | if (page && !IS_ERR(page)) { |
| 1654 | kunmap(page); |
| 1655 | page_cache_release(page); |
| 1656 | } |
| 1657 | if (name) |
| 1658 | putname(name); |
| 1659 | if (did_down) { |
| 1660 | if (!error) |
| 1661 | inode->i_flags |= S_SWAPFILE; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1662 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | } |
| 1664 | return error; |
| 1665 | } |
| 1666 | |
| 1667 | void si_swapinfo(struct sysinfo *val) |
| 1668 | { |
| 1669 | unsigned int i; |
| 1670 | unsigned long nr_to_be_unused = 0; |
| 1671 | |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1672 | spin_lock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | for (i = 0; i < nr_swapfiles; i++) { |
| 1674 | if (!(swap_info[i].flags & SWP_USED) || |
| 1675 | (swap_info[i].flags & SWP_WRITEOK)) |
| 1676 | continue; |
| 1677 | nr_to_be_unused += swap_info[i].inuse_pages; |
| 1678 | } |
| 1679 | val->freeswap = nr_swap_pages + nr_to_be_unused; |
| 1680 | val->totalswap = total_swap_pages + nr_to_be_unused; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1681 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | /* |
| 1685 | * Verify that a swap entry is valid and increment its swap map count. |
| 1686 | * |
| 1687 | * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as |
| 1688 | * "permanent", but will be reclaimed by the next swapoff. |
| 1689 | */ |
| 1690 | int swap_duplicate(swp_entry_t entry) |
| 1691 | { |
| 1692 | struct swap_info_struct * p; |
| 1693 | unsigned long offset, type; |
| 1694 | int result = 0; |
| 1695 | |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame^] | 1696 | if (is_migration_entry(entry)) |
| 1697 | return 1; |
| 1698 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | type = swp_type(entry); |
| 1700 | if (type >= nr_swapfiles) |
| 1701 | goto bad_file; |
| 1702 | p = type + swap_info; |
| 1703 | offset = swp_offset(entry); |
| 1704 | |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1705 | spin_lock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | if (offset < p->max && p->swap_map[offset]) { |
| 1707 | if (p->swap_map[offset] < SWAP_MAP_MAX - 1) { |
| 1708 | p->swap_map[offset]++; |
| 1709 | result = 1; |
| 1710 | } else if (p->swap_map[offset] <= SWAP_MAP_MAX) { |
| 1711 | if (swap_overflow++ < 5) |
| 1712 | printk(KERN_WARNING "swap_dup: swap entry overflow\n"); |
| 1713 | p->swap_map[offset] = SWAP_MAP_MAX; |
| 1714 | result = 1; |
| 1715 | } |
| 1716 | } |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1717 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | out: |
| 1719 | return result; |
| 1720 | |
| 1721 | bad_file: |
| 1722 | printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val); |
| 1723 | goto out; |
| 1724 | } |
| 1725 | |
| 1726 | struct swap_info_struct * |
| 1727 | get_swap_info_struct(unsigned type) |
| 1728 | { |
| 1729 | return &swap_info[type]; |
| 1730 | } |
| 1731 | |
| 1732 | /* |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1733 | * swap_lock prevents swap_map being freed. Don't grab an extra |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | * reference on the swaphandle, it doesn't matter if it becomes unused. |
| 1735 | */ |
| 1736 | int valid_swaphandles(swp_entry_t entry, unsigned long *offset) |
| 1737 | { |
| 1738 | int ret = 0, i = 1 << page_cluster; |
| 1739 | unsigned long toff; |
| 1740 | struct swap_info_struct *swapdev = swp_type(entry) + swap_info; |
| 1741 | |
| 1742 | if (!page_cluster) /* no readahead */ |
| 1743 | return 0; |
| 1744 | toff = (swp_offset(entry) >> page_cluster) << page_cluster; |
| 1745 | if (!toff) /* first page is swap header */ |
| 1746 | toff++, i--; |
| 1747 | *offset = toff; |
| 1748 | |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1749 | spin_lock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | do { |
| 1751 | /* Don't read-ahead past the end of the swap area */ |
| 1752 | if (toff >= swapdev->max) |
| 1753 | break; |
| 1754 | /* Don't read in free or bad pages */ |
| 1755 | if (!swapdev->swap_map[toff]) |
| 1756 | break; |
| 1757 | if (swapdev->swap_map[toff] == SWAP_MAP_BAD) |
| 1758 | break; |
| 1759 | toff++; |
| 1760 | ret++; |
| 1761 | } while (--i); |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1762 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | return ret; |
| 1764 | } |