blob: 0579d9069b61d780abd75146ad870af8a4639425 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/mm.h>
9#include <linux/hugetlb.h>
10#include <linux/mman.h>
11#include <linux/slab.h>
12#include <linux/kernel_stat.h>
13#include <linux/swap.h>
14#include <linux/vmalloc.h>
15#include <linux/pagemap.h>
16#include <linux/namei.h>
17#include <linux/shm.h>
18#include <linux/blkdev.h>
Hugh Dickins20137a42009-01-06 14:39:54 -080019#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#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 Molnarfc0abb12006-01-18 17:42:33 -080028#include <linux/mutex.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080029#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/syscalls.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080031#include <linux/memcontrol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/pgtable.h>
34#include <asm/tlbflush.h>
35#include <linux/swapops.h>
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -080036#include <linux/page_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Adrian Bunk7c363b82008-07-25 19:46:24 -070038static DEFINE_SPINLOCK(swap_lock);
39static unsigned int nr_swapfiles;
Hugh Dickinsb9627162009-01-06 14:39:41 -080040long nr_swap_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041long total_swap_pages;
42static int swap_overflow;
Hugh Dickins78ecba02008-07-23 21:28:23 -070043static int least_priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static const char Bad_file[] = "Bad swap file entry ";
46static const char Unused_file[] = "Unused swap file entry ";
47static const char Bad_offset[] = "Bad swap offset entry ";
48static const char Unused_offset[] = "Unused swap offset entry ";
49
Adrian Bunk7c363b82008-07-25 19:46:24 -070050static struct swap_list_t swap_list = {-1, -1};
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080052static struct swap_info_struct swap_info[MAX_SWAPFILES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Ingo Molnarfc0abb12006-01-18 17:42:33 -080054static DEFINE_MUTEX(swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
57 * We need this because the bdev->unplug_fn can sleep and we cannot
Hugh Dickins5d337b92005-09-03 15:54:41 -070058 * hold swap_lock while calling the unplug_fn. And swap_lock
Ingo Molnarfc0abb12006-01-18 17:42:33 -080059 * cannot be turned into a mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 */
61static DECLARE_RWSEM(swap_unplug_sem);
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page)
64{
65 swp_entry_t entry;
66
67 down_read(&swap_unplug_sem);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -070068 entry.val = page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (PageSwapCache(page)) {
70 struct block_device *bdev = swap_info[swp_type(entry)].bdev;
71 struct backing_dev_info *bdi;
72
73 /*
74 * If the page is removed from swapcache from under us (with a
75 * racy try_to_unuse/swapoff) we need an additional reference
Hugh Dickins4c21e2f2005-10-29 18:16:40 -070076 * count to avoid reading garbage from page_private(page) above.
77 * If the WARN_ON triggers during a swapoff it maybe the race
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * condition and it's harmless. However if it triggers without
79 * swapoff it signals a problem.
80 */
81 WARN_ON(page_count(page) <= 1);
82
83 bdi = bdev->bd_inode->i_mapping->backing_dev_info;
McMullan, Jasonba323112005-05-16 21:53:40 -070084 blk_run_backing_dev(bdi, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86 up_read(&swap_unplug_sem);
87}
88
Hugh Dickins6a6ba832009-01-06 14:39:51 -080089/*
90 * swapon tell device that all the old swap contents can be discarded,
91 * to allow the swap device to optimize its wear-levelling.
92 */
93static int discard_swap(struct swap_info_struct *si)
94{
95 struct swap_extent *se;
96 int err = 0;
97
98 list_for_each_entry(se, &si->extent_list, list) {
99 sector_t start_block = se->start_block << (PAGE_SHIFT - 9);
Hugh Dickins858a29902009-01-06 14:39:56 -0800100 sector_t nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800101
102 if (se->start_page == 0) {
103 /* Do not discard the swap header page! */
104 start_block += 1 << (PAGE_SHIFT - 9);
105 nr_blocks -= 1 << (PAGE_SHIFT - 9);
106 if (!nr_blocks)
107 continue;
108 }
109
110 err = blkdev_issue_discard(si->bdev, start_block,
111 nr_blocks, GFP_KERNEL);
112 if (err)
113 break;
114
115 cond_resched();
116 }
117 return err; /* That will often be -EOPNOTSUPP */
118}
119
Hugh Dickins7992fde2009-01-06 14:39:53 -0800120/*
121 * swap allocation tell device that a cluster of swap can now be discarded,
122 * to allow the swap device to optimize its wear-levelling.
123 */
124static void discard_swap_cluster(struct swap_info_struct *si,
125 pgoff_t start_page, pgoff_t nr_pages)
126{
127 struct swap_extent *se = si->curr_swap_extent;
128 int found_extent = 0;
129
130 while (nr_pages) {
131 struct list_head *lh;
132
133 if (se->start_page <= start_page &&
134 start_page < se->start_page + se->nr_pages) {
135 pgoff_t offset = start_page - se->start_page;
136 sector_t start_block = se->start_block + offset;
Hugh Dickins858a29902009-01-06 14:39:56 -0800137 sector_t nr_blocks = se->nr_pages - offset;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800138
139 if (nr_blocks > nr_pages)
140 nr_blocks = nr_pages;
141 start_page += nr_blocks;
142 nr_pages -= nr_blocks;
143
144 if (!found_extent++)
145 si->curr_swap_extent = se;
146
147 start_block <<= PAGE_SHIFT - 9;
148 nr_blocks <<= PAGE_SHIFT - 9;
149 if (blkdev_issue_discard(si->bdev, start_block,
150 nr_blocks, GFP_NOIO))
151 break;
152 }
153
154 lh = se->list.next;
155 if (lh == &si->extent_list)
156 lh = lh->next;
157 se = list_entry(lh, struct swap_extent, list);
158 }
159}
160
161static int wait_for_discard(void *word)
162{
163 schedule();
164 return 0;
165}
166
Hugh Dickins048c27f2005-09-03 15:54:40 -0700167#define SWAPFILE_CLUSTER 256
168#define LATENCY_LIMIT 256
169
Hugh Dickins6eb396d2005-09-03 15:54:35 -0700170static inline unsigned long scan_swap_map(struct swap_info_struct *si)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800172 unsigned long offset;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800173 unsigned long scan_base;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800174 unsigned long last_in_cluster = 0;
Hugh Dickins048c27f2005-09-03 15:54:40 -0700175 int latency_ration = LATENCY_LIMIT;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800176 int found_free_cluster = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Hugh Dickins886bb7e2009-01-06 14:39:48 -0800178 /*
Hugh Dickins7dfad412005-09-03 15:54:38 -0700179 * We try to cluster swap pages by allocating them sequentially
180 * in swap. Once we've allocated SWAPFILE_CLUSTER pages this
181 * way, however, we resort to first-free allocation, starting
182 * a new cluster. This prevents us from scattering swap pages
183 * all over the entire swap partition, so that we reduce
184 * overall disk seek times between swap pages. -- sct
185 * But we do now try to find an empty cluster. -Andrea
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800186 * And we let swap pages go all over an SSD partition. Hugh
Hugh Dickins7dfad412005-09-03 15:54:38 -0700187 */
188
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700189 si->flags += SWP_SCANNING;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800190 scan_base = offset = si->cluster_next;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800191
192 if (unlikely(!si->cluster_nr--)) {
193 if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
194 si->cluster_nr = SWAPFILE_CLUSTER - 1;
195 goto checks;
196 }
Hugh Dickins7992fde2009-01-06 14:39:53 -0800197 if (si->flags & SWP_DISCARDABLE) {
198 /*
199 * Start range check on racing allocations, in case
200 * they overlap the cluster we eventually decide on
201 * (we scan without swap_lock to allow preemption).
202 * It's hardly conceivable that cluster_nr could be
203 * wrapped during our scan, but don't depend on it.
204 */
205 if (si->lowest_alloc)
206 goto checks;
207 si->lowest_alloc = si->max;
208 si->highest_alloc = 0;
209 }
Hugh Dickins5d337b92005-09-03 15:54:41 -0700210 spin_unlock(&swap_lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700211
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800212 /*
213 * If seek is expensive, start searching for new cluster from
214 * start of partition, to minimize the span of allocated swap.
215 * But if seek is cheap, search from our current position, so
216 * that swap is allocated from all over the partition: if the
217 * Flash Translation Layer only remaps within limited zones,
218 * we don't want to wear out the first zone too quickly.
219 */
220 if (!(si->flags & SWP_SOLIDSTATE))
221 scan_base = offset = si->lowest_bit;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700222 last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
223
224 /* Locate the first empty (unaligned) cluster */
225 for (; last_in_cluster <= si->highest_bit; offset++) {
226 if (si->swap_map[offset])
227 last_in_cluster = offset + SWAPFILE_CLUSTER;
228 else if (offset == last_in_cluster) {
Hugh Dickins5d337b92005-09-03 15:54:41 -0700229 spin_lock(&swap_lock);
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800230 offset -= SWAPFILE_CLUSTER - 1;
231 si->cluster_next = offset;
232 si->cluster_nr = SWAPFILE_CLUSTER - 1;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800233 found_free_cluster = 1;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800234 goto checks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Hugh Dickins048c27f2005-09-03 15:54:40 -0700236 if (unlikely(--latency_ration < 0)) {
237 cond_resched();
238 latency_ration = LATENCY_LIMIT;
239 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700240 }
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800241
242 offset = si->lowest_bit;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800243 last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
244
245 /* Locate the first empty (unaligned) cluster */
246 for (; last_in_cluster < scan_base; offset++) {
247 if (si->swap_map[offset])
248 last_in_cluster = offset + SWAPFILE_CLUSTER;
249 else if (offset == last_in_cluster) {
250 spin_lock(&swap_lock);
251 offset -= SWAPFILE_CLUSTER - 1;
252 si->cluster_next = offset;
253 si->cluster_nr = SWAPFILE_CLUSTER - 1;
254 found_free_cluster = 1;
255 goto checks;
256 }
257 if (unlikely(--latency_ration < 0)) {
258 cond_resched();
259 latency_ration = LATENCY_LIMIT;
260 }
261 }
262
263 offset = scan_base;
Hugh Dickins5d337b92005-09-03 15:54:41 -0700264 spin_lock(&swap_lock);
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800265 si->cluster_nr = SWAPFILE_CLUSTER - 1;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800266 si->lowest_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700268
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800269checks:
270 if (!(si->flags & SWP_WRITEOK))
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700271 goto no_page;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700272 if (!si->highest_bit)
273 goto no_page;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800274 if (offset > si->highest_bit)
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800275 scan_base = offset = si->lowest_bit;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800276 if (si->swap_map[offset])
277 goto scan;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700278
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800279 if (offset == si->lowest_bit)
280 si->lowest_bit++;
281 if (offset == si->highest_bit)
282 si->highest_bit--;
283 si->inuse_pages++;
284 if (si->inuse_pages == si->pages) {
285 si->lowest_bit = si->max;
286 si->highest_bit = 0;
287 }
288 si->swap_map[offset] = 1;
289 si->cluster_next = offset + 1;
290 si->flags -= SWP_SCANNING;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800291
292 if (si->lowest_alloc) {
293 /*
294 * Only set when SWP_DISCARDABLE, and there's a scan
295 * for a free cluster in progress or just completed.
296 */
297 if (found_free_cluster) {
298 /*
299 * To optimize wear-levelling, discard the
300 * old data of the cluster, taking care not to
301 * discard any of its pages that have already
302 * been allocated by racing tasks (offset has
303 * already stepped over any at the beginning).
304 */
305 if (offset < si->highest_alloc &&
306 si->lowest_alloc <= last_in_cluster)
307 last_in_cluster = si->lowest_alloc - 1;
308 si->flags |= SWP_DISCARDING;
309 spin_unlock(&swap_lock);
310
311 if (offset < last_in_cluster)
312 discard_swap_cluster(si, offset,
313 last_in_cluster - offset + 1);
314
315 spin_lock(&swap_lock);
316 si->lowest_alloc = 0;
317 si->flags &= ~SWP_DISCARDING;
318
319 smp_mb(); /* wake_up_bit advises this */
320 wake_up_bit(&si->flags, ilog2(SWP_DISCARDING));
321
322 } else if (si->flags & SWP_DISCARDING) {
323 /*
324 * Delay using pages allocated by racing tasks
325 * until the whole discard has been issued. We
326 * could defer that delay until swap_writepage,
327 * but it's easier to keep this self-contained.
328 */
329 spin_unlock(&swap_lock);
330 wait_on_bit(&si->flags, ilog2(SWP_DISCARDING),
331 wait_for_discard, TASK_UNINTERRUPTIBLE);
332 spin_lock(&swap_lock);
333 } else {
334 /*
335 * Note pages allocated by racing tasks while
336 * scan for a free cluster is in progress, so
337 * that its final discard can exclude them.
338 */
339 if (offset < si->lowest_alloc)
340 si->lowest_alloc = offset;
341 if (offset > si->highest_alloc)
342 si->highest_alloc = offset;
343 }
344 }
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800345 return offset;
346
347scan:
Hugh Dickins5d337b92005-09-03 15:54:41 -0700348 spin_unlock(&swap_lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700349 while (++offset <= si->highest_bit) {
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700350 if (!si->swap_map[offset]) {
Hugh Dickins5d337b92005-09-03 15:54:41 -0700351 spin_lock(&swap_lock);
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700352 goto checks;
353 }
Hugh Dickins048c27f2005-09-03 15:54:40 -0700354 if (unlikely(--latency_ration < 0)) {
355 cond_resched();
356 latency_ration = LATENCY_LIMIT;
357 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700358 }
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800359 offset = si->lowest_bit;
360 while (++offset < scan_base) {
361 if (!si->swap_map[offset]) {
362 spin_lock(&swap_lock);
363 goto checks;
364 }
365 if (unlikely(--latency_ration < 0)) {
366 cond_resched();
367 latency_ration = LATENCY_LIMIT;
368 }
369 }
Hugh Dickins5d337b92005-09-03 15:54:41 -0700370 spin_lock(&swap_lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700371
372no_page:
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700373 si->flags -= SWP_SCANNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return 0;
375}
376
377swp_entry_t get_swap_page(void)
378{
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700379 struct swap_info_struct *si;
380 pgoff_t offset;
381 int type, next;
382 int wrapped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Hugh Dickins5d337b92005-09-03 15:54:41 -0700384 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (nr_swap_pages <= 0)
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700386 goto noswap;
387 nr_swap_pages--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700389 for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) {
390 si = swap_info + type;
391 next = si->next;
392 if (next < 0 ||
393 (!wrapped && si->prio != swap_info[next].prio)) {
394 next = swap_list.head;
395 wrapped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700397
398 if (!si->highest_bit)
399 continue;
400 if (!(si->flags & SWP_WRITEOK))
401 continue;
402
403 swap_list.next = next;
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700404 offset = scan_swap_map(si);
Hugh Dickins5d337b92005-09-03 15:54:41 -0700405 if (offset) {
406 spin_unlock(&swap_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700407 return swp_entry(type, offset);
Hugh Dickins5d337b92005-09-03 15:54:41 -0700408 }
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700409 next = swap_list.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700411
412 nr_swap_pages++;
413noswap:
Hugh Dickins5d337b92005-09-03 15:54:41 -0700414 spin_unlock(&swap_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700415 return (swp_entry_t) {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Rafael J. Wysocki3a291a22006-01-06 00:16:37 -0800418swp_entry_t get_swap_page_of_type(int type)
419{
420 struct swap_info_struct *si;
421 pgoff_t offset;
422
423 spin_lock(&swap_lock);
424 si = swap_info + type;
425 if (si->flags & SWP_WRITEOK) {
426 nr_swap_pages--;
427 offset = scan_swap_map(si);
428 if (offset) {
429 spin_unlock(&swap_lock);
430 return swp_entry(type, offset);
431 }
432 nr_swap_pages++;
433 }
434 spin_unlock(&swap_lock);
435 return (swp_entry_t) {0};
436}
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438static struct swap_info_struct * swap_info_get(swp_entry_t entry)
439{
440 struct swap_info_struct * p;
441 unsigned long offset, type;
442
443 if (!entry.val)
444 goto out;
445 type = swp_type(entry);
446 if (type >= nr_swapfiles)
447 goto bad_nofile;
448 p = & swap_info[type];
449 if (!(p->flags & SWP_USED))
450 goto bad_device;
451 offset = swp_offset(entry);
452 if (offset >= p->max)
453 goto bad_offset;
454 if (!p->swap_map[offset])
455 goto bad_free;
Hugh Dickins5d337b92005-09-03 15:54:41 -0700456 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return p;
458
459bad_free:
460 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
461 goto out;
462bad_offset:
463 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
464 goto out;
465bad_device:
466 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
467 goto out;
468bad_nofile:
469 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
470out:
471 return NULL;
Hugh Dickins886bb7e2009-01-06 14:39:48 -0800472}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800474static int swap_entry_free(struct swap_info_struct *p, swp_entry_t ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800476 unsigned long offset = swp_offset(ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int count = p->swap_map[offset];
478
479 if (count < SWAP_MAP_MAX) {
480 count--;
481 p->swap_map[offset] = count;
482 if (!count) {
483 if (offset < p->lowest_bit)
484 p->lowest_bit = offset;
485 if (offset > p->highest_bit)
486 p->highest_bit = offset;
Hugh Dickins89d09a22005-09-03 15:54:36 -0700487 if (p->prio > swap_info[swap_list.next].prio)
488 swap_list.next = p - swap_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 nr_swap_pages++;
490 p->inuse_pages--;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800491 mem_cgroup_uncharge_swap(ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493 }
494 return count;
495}
496
497/*
498 * Caller has made sure that the swapdevice corresponding to entry
499 * is still around or has not been recycled.
500 */
501void swap_free(swp_entry_t entry)
502{
503 struct swap_info_struct * p;
504
505 p = swap_info_get(entry);
506 if (p) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800507 swap_entry_free(p, entry);
Hugh Dickins5d337b92005-09-03 15:54:41 -0700508 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510}
511
512/*
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700513 * How many references to page are currently swapped out?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 */
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700515static inline int page_swapcount(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700517 int count = 0;
518 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 swp_entry_t entry;
520
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700521 entry.val = page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 p = swap_info_get(entry);
523 if (p) {
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700524 /* Subtract the 1 for the swap cache itself */
525 count = p->swap_map[swp_offset(entry)] - 1;
Hugh Dickins5d337b92005-09-03 15:54:41 -0700526 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700528 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531/*
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800532 * We can write to an anon page without COW if there are no other references
533 * to it. And as a side-effect, free up its swap: because the old content
534 * on disk will never be read, and seeking back there to write new content
535 * later would only waste time away from clustering.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 */
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800537int reuse_swap_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700539 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Hugh Dickins51726b12009-01-06 14:39:25 -0800541 VM_BUG_ON(!PageLocked(page));
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700542 count = page_mapcount(page);
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800543 if (count <= 1 && PageSwapCache(page)) {
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700544 count += page_swapcount(page);
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800545 if (count == 1 && !PageWriteback(page)) {
546 delete_from_swap_cache(page);
547 SetPageDirty(page);
548 }
549 }
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700550 return count == 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553/*
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800554 * If swap is getting full, or if there are no more mappings of this page,
555 * then try_to_free_swap is called to free its swap space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 */
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800557int try_to_free_swap(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Hugh Dickins51726b12009-01-06 14:39:25 -0800559 VM_BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 if (!PageSwapCache(page))
562 return 0;
563 if (PageWriteback(page))
564 return 0;
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800565 if (page_swapcount(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return 0;
567
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800568 delete_from_swap_cache(page);
569 SetPageDirty(page);
570 return 1;
Rik van Riel68a223942008-10-18 20:26:23 -0700571}
572
573/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 * Free the swap entry like above, but also try to
575 * free the page cache entry if it is the last user.
576 */
Hugh Dickins2509ef22009-01-06 14:40:10 -0800577int free_swap_and_cache(swp_entry_t entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Hugh Dickins2509ef22009-01-06 14:40:10 -0800579 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 struct page *page = NULL;
581
Christoph Lameter06972122006-06-23 02:03:35 -0700582 if (is_migration_entry(entry))
Hugh Dickins2509ef22009-01-06 14:40:10 -0800583 return 1;
Christoph Lameter06972122006-06-23 02:03:35 -0700584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 p = swap_info_get(entry);
586 if (p) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800587 if (swap_entry_free(p, entry) == 1) {
Nick Piggin93fac702006-03-31 02:29:56 -0800588 page = find_get_page(&swapper_space, entry.val);
Nick Piggin8413ac92008-10-18 20:26:59 -0700589 if (page && !trylock_page(page)) {
Nick Piggin93fac702006-03-31 02:29:56 -0800590 page_cache_release(page);
591 page = NULL;
592 }
593 }
Hugh Dickins5d337b92005-09-03 15:54:41 -0700594 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596 if (page) {
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800597 /*
598 * Not mapped elsewhere, or swap space full? Free it!
599 * Also recheck PageSwapCache now page is locked (above).
600 */
Nick Piggin93fac702006-03-31 02:29:56 -0800601 if (PageSwapCache(page) && !PageWriteback(page) &&
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800602 (!page_mapped(page) || vm_swap_full())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 delete_from_swap_cache(page);
604 SetPageDirty(page);
605 }
606 unlock_page(page);
607 page_cache_release(page);
608 }
Hugh Dickins2509ef22009-01-06 14:40:10 -0800609 return p != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200612#ifdef CONFIG_HIBERNATION
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800613/*
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800614 * Find the swap type that corresponds to given device (if any).
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800615 *
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800616 * @offset - number of the PAGE_SIZE-sized block of the device, starting
617 * from 0, in which the swap header is expected to be located.
618 *
619 * This is needed for the suspend to disk (aka swsusp).
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800620 */
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800621int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800622{
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800623 struct block_device *bdev = NULL;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800624 int i;
625
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800626 if (device)
627 bdev = bdget(device);
628
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800629 spin_lock(&swap_lock);
630 for (i = 0; i < nr_swapfiles; i++) {
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800631 struct swap_info_struct *sis = swap_info + i;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800632
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800633 if (!(sis->flags & SWP_WRITEOK))
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800634 continue;
Rafael J. Wysockib6b5bce2006-08-27 01:23:25 -0700635
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800636 if (!bdev) {
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800637 if (bdev_p)
638 *bdev_p = sis->bdev;
639
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800640 spin_unlock(&swap_lock);
641 return i;
642 }
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800643 if (bdev == sis->bdev) {
644 struct swap_extent *se;
645
646 se = list_entry(sis->extent_list.next,
647 struct swap_extent, list);
648 if (se->start_block == offset) {
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800649 if (bdev_p)
650 *bdev_p = sis->bdev;
651
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800652 spin_unlock(&swap_lock);
653 bdput(bdev);
654 return i;
655 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800656 }
657 }
658 spin_unlock(&swap_lock);
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800659 if (bdev)
660 bdput(bdev);
661
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800662 return -ENODEV;
663}
664
665/*
666 * Return either the total number of swap pages of given type, or the number
667 * of free pages of that type (depending on @free)
668 *
669 * This is needed for software suspend
670 */
671unsigned int count_swap_pages(int type, int free)
672{
673 unsigned int n = 0;
674
675 if (type < nr_swapfiles) {
676 spin_lock(&swap_lock);
677 if (swap_info[type].flags & SWP_WRITEOK) {
678 n = swap_info[type].pages;
679 if (free)
680 n -= swap_info[type].inuse_pages;
681 }
682 spin_unlock(&swap_lock);
683 }
684 return n;
685}
686#endif
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688/*
Hugh Dickins72866f62005-10-29 18:15:55 -0700689 * No need to decide whether this PTE shares the swap entry with others,
690 * just let do_wp_page work it out if a write is requested later - to
691 * force COW, vm_page_prot omits write permission from any private vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 */
Hugh Dickins044d66c2008-02-07 00:14:04 -0800693static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 unsigned long addr, swp_entry_t entry, struct page *page)
695{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800696 struct mem_cgroup *ptr = NULL;
Hugh Dickins044d66c2008-02-07 00:14:04 -0800697 spinlock_t *ptl;
698 pte_t *pte;
699 int ret = 1;
700
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800701 if (mem_cgroup_try_charge_swapin(vma->vm_mm, page,
702 GFP_HIGHUSER_MOVABLE, &ptr))
Hugh Dickins044d66c2008-02-07 00:14:04 -0800703 ret = -ENOMEM;
704
705 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
706 if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) {
707 if (ret > 0)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800708 mem_cgroup_cancel_charge_swapin(ptr);
Hugh Dickins044d66c2008-02-07 00:14:04 -0800709 ret = 0;
710 goto out;
711 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800712
Hugh Dickins42946212005-10-29 18:16:05 -0700713 inc_mm_counter(vma->vm_mm, anon_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 get_page(page);
715 set_pte_at(vma->vm_mm, addr, pte,
716 pte_mkold(mk_pte(page, vma->vm_page_prot)));
717 page_add_anon_rmap(page, vma, addr);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800718 mem_cgroup_commit_charge_swapin(page, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 swap_free(entry);
720 /*
721 * Move the page to the active list so it is not
722 * immediately swapped out again after swapon.
723 */
724 activate_page(page);
Hugh Dickins044d66c2008-02-07 00:14:04 -0800725out:
726 pte_unmap_unlock(pte, ptl);
727 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
730static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
731 unsigned long addr, unsigned long end,
732 swp_entry_t entry, struct page *page)
733{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 pte_t swp_pte = swp_entry_to_pte(entry);
Hugh Dickins705e87c2005-10-29 18:16:27 -0700735 pte_t *pte;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800736 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Hugh Dickins044d66c2008-02-07 00:14:04 -0800738 /*
739 * We don't actually need pte lock while scanning for swp_pte: since
740 * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
741 * page table while we're scanning; though it could get zapped, and on
742 * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
743 * of unmatched parts which look like swp_pte, so unuse_pte must
744 * recheck under pte lock. Scanning without pte lock lets it be
745 * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
746 */
747 pte = pte_offset_map(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 do {
749 /*
750 * swapoff spends a _lot_ of time in this loop!
751 * Test inline before going to call unuse_pte.
752 */
753 if (unlikely(pte_same(*pte, swp_pte))) {
Hugh Dickins044d66c2008-02-07 00:14:04 -0800754 pte_unmap(pte);
755 ret = unuse_pte(vma, pmd, addr, entry, page);
756 if (ret)
757 goto out;
758 pte = pte_offset_map(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760 } while (pte++, addr += PAGE_SIZE, addr != end);
Hugh Dickins044d66c2008-02-07 00:14:04 -0800761 pte_unmap(pte - 1);
762out:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800763 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
767 unsigned long addr, unsigned long end,
768 swp_entry_t entry, struct page *page)
769{
770 pmd_t *pmd;
771 unsigned long next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800772 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 pmd = pmd_offset(pud, addr);
775 do {
776 next = pmd_addr_end(addr, end);
777 if (pmd_none_or_clear_bad(pmd))
778 continue;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800779 ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
780 if (ret)
781 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 } while (pmd++, addr = next, addr != end);
783 return 0;
784}
785
786static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
787 unsigned long addr, unsigned long end,
788 swp_entry_t entry, struct page *page)
789{
790 pud_t *pud;
791 unsigned long next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800792 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 pud = pud_offset(pgd, addr);
795 do {
796 next = pud_addr_end(addr, end);
797 if (pud_none_or_clear_bad(pud))
798 continue;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800799 ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
800 if (ret)
801 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 } while (pud++, addr = next, addr != end);
803 return 0;
804}
805
806static int unuse_vma(struct vm_area_struct *vma,
807 swp_entry_t entry, struct page *page)
808{
809 pgd_t *pgd;
810 unsigned long addr, end, next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800811 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 if (page->mapping) {
814 addr = page_address_in_vma(page, vma);
815 if (addr == -EFAULT)
816 return 0;
817 else
818 end = addr + PAGE_SIZE;
819 } else {
820 addr = vma->vm_start;
821 end = vma->vm_end;
822 }
823
824 pgd = pgd_offset(vma->vm_mm, addr);
825 do {
826 next = pgd_addr_end(addr, end);
827 if (pgd_none_or_clear_bad(pgd))
828 continue;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800829 ret = unuse_pud_range(vma, pgd, addr, next, entry, page);
830 if (ret)
831 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 } while (pgd++, addr = next, addr != end);
833 return 0;
834}
835
836static int unuse_mm(struct mm_struct *mm,
837 swp_entry_t entry, struct page *page)
838{
839 struct vm_area_struct *vma;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800840 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 if (!down_read_trylock(&mm->mmap_sem)) {
843 /*
Fernando Luis Vazquez Cao7d034312008-07-29 22:33:41 -0700844 * Activate page so shrink_inactive_list is unlikely to unmap
845 * its ptes while lock is dropped, so swapoff can make progress.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 */
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700847 activate_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 unlock_page(page);
849 down_read(&mm->mmap_sem);
850 lock_page(page);
851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 for (vma = mm->mmap; vma; vma = vma->vm_next) {
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800853 if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 break;
855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 up_read(&mm->mmap_sem);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800857 return (ret < 0)? ret: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
860/*
861 * Scan swap_map from current position to next entry still in use.
862 * Recycle to start on reaching the end, returning 0 when empty.
863 */
Hugh Dickins6eb396d2005-09-03 15:54:35 -0700864static unsigned int find_next_to_unuse(struct swap_info_struct *si,
865 unsigned int prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Hugh Dickins6eb396d2005-09-03 15:54:35 -0700867 unsigned int max = si->max;
868 unsigned int i = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 int count;
870
871 /*
Hugh Dickins5d337b92005-09-03 15:54:41 -0700872 * No need for swap_lock here: we're just looking
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 * for whether an entry is in use, not modifying it; false
874 * hits are okay, and sys_swapoff() has already prevented new
Hugh Dickins5d337b92005-09-03 15:54:41 -0700875 * allocations from this area (while holding swap_lock).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 */
877 for (;;) {
878 if (++i >= max) {
879 if (!prev) {
880 i = 0;
881 break;
882 }
883 /*
884 * No entries in use at top of swap_map,
885 * loop back to start and recheck there.
886 */
887 max = prev + 1;
888 prev = 0;
889 i = 1;
890 }
891 count = si->swap_map[i];
892 if (count && count != SWAP_MAP_BAD)
893 break;
894 }
895 return i;
896}
897
898/*
899 * We completely avoid races by reading each swap page in advance,
900 * and then search for the process using it. All the necessary
901 * page table adjustments can then be made atomically.
902 */
903static int try_to_unuse(unsigned int type)
904{
905 struct swap_info_struct * si = &swap_info[type];
906 struct mm_struct *start_mm;
907 unsigned short *swap_map;
908 unsigned short swcount;
909 struct page *page;
910 swp_entry_t entry;
Hugh Dickins6eb396d2005-09-03 15:54:35 -0700911 unsigned int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 int retval = 0;
913 int reset_overflow = 0;
914 int shmem;
915
916 /*
917 * When searching mms for an entry, a good strategy is to
918 * start at the first mm we freed the previous entry from
919 * (though actually we don't notice whether we or coincidence
920 * freed the entry). Initialize this start_mm with a hold.
921 *
922 * A simpler strategy would be to start at the last mm we
923 * freed the previous entry from; but that would take less
924 * advantage of mmlist ordering, which clusters forked mms
925 * together, child after parent. If we race with dup_mmap(), we
926 * prefer to resolve parent before child, lest we miss entries
927 * duplicated after we scanned child: using last mm would invert
928 * that. Though it's only a serious concern when an overflowed
929 * swap count is reset from SWAP_MAP_MAX, preventing a rescan.
930 */
931 start_mm = &init_mm;
932 atomic_inc(&init_mm.mm_users);
933
934 /*
935 * Keep on scanning until all entries have gone. Usually,
936 * one pass through swap_map is enough, but not necessarily:
937 * there are races when an instance of an entry might be missed.
938 */
939 while ((i = find_next_to_unuse(si, i)) != 0) {
940 if (signal_pending(current)) {
941 retval = -EINTR;
942 break;
943 }
944
Hugh Dickins886bb7e2009-01-06 14:39:48 -0800945 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 * Get a page for the entry, using the existing swap
947 * cache page if there is one. Otherwise, get a clean
Hugh Dickins886bb7e2009-01-06 14:39:48 -0800948 * page and read the swap into it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 */
950 swap_map = &si->swap_map[i];
951 entry = swp_entry(type, i);
Hugh Dickins02098fe2008-02-04 22:28:42 -0800952 page = read_swap_cache_async(entry,
953 GFP_HIGHUSER_MOVABLE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (!page) {
955 /*
956 * Either swap_duplicate() failed because entry
957 * has been freed independently, and will not be
958 * reused since sys_swapoff() already disabled
959 * allocation from here, or alloc_page() failed.
960 */
961 if (!*swap_map)
962 continue;
963 retval = -ENOMEM;
964 break;
965 }
966
967 /*
968 * Don't hold on to start_mm if it looks like exiting.
969 */
970 if (atomic_read(&start_mm->mm_users) == 1) {
971 mmput(start_mm);
972 start_mm = &init_mm;
973 atomic_inc(&init_mm.mm_users);
974 }
975
976 /*
977 * Wait for and lock page. When do_swap_page races with
978 * try_to_unuse, do_swap_page can handle the fault much
979 * faster than try_to_unuse can locate the entry. This
980 * apparently redundant "wait_on_page_locked" lets try_to_unuse
981 * defer to do_swap_page in such a case - in some tests,
982 * do_swap_page and try_to_unuse repeatedly compete.
983 */
984 wait_on_page_locked(page);
985 wait_on_page_writeback(page);
986 lock_page(page);
987 wait_on_page_writeback(page);
988
989 /*
990 * Remove all references to entry.
991 * Whenever we reach init_mm, there's no address space
992 * to search, but use it as a reminder to search shmem.
993 */
994 shmem = 0;
995 swcount = *swap_map;
996 if (swcount > 1) {
997 if (start_mm == &init_mm)
998 shmem = shmem_unuse(entry, page);
999 else
1000 retval = unuse_mm(start_mm, entry, page);
1001 }
1002 if (*swap_map > 1) {
1003 int set_start_mm = (*swap_map >= swcount);
1004 struct list_head *p = &start_mm->mmlist;
1005 struct mm_struct *new_start_mm = start_mm;
1006 struct mm_struct *prev_mm = start_mm;
1007 struct mm_struct *mm;
1008
1009 atomic_inc(&new_start_mm->mm_users);
1010 atomic_inc(&prev_mm->mm_users);
1011 spin_lock(&mmlist_lock);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001012 while (*swap_map > 1 && !retval && !shmem &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 (p = p->next) != &start_mm->mmlist) {
1014 mm = list_entry(p, struct mm_struct, mmlist);
Hugh Dickins70af7c52006-06-23 02:03:44 -07001015 if (!atomic_inc_not_zero(&mm->mm_users))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 spin_unlock(&mmlist_lock);
1018 mmput(prev_mm);
1019 prev_mm = mm;
1020
1021 cond_resched();
1022
1023 swcount = *swap_map;
1024 if (swcount <= 1)
1025 ;
1026 else if (mm == &init_mm) {
1027 set_start_mm = 1;
1028 shmem = shmem_unuse(entry, page);
1029 } else
1030 retval = unuse_mm(mm, entry, page);
1031 if (set_start_mm && *swap_map < swcount) {
1032 mmput(new_start_mm);
1033 atomic_inc(&mm->mm_users);
1034 new_start_mm = mm;
1035 set_start_mm = 0;
1036 }
1037 spin_lock(&mmlist_lock);
1038 }
1039 spin_unlock(&mmlist_lock);
1040 mmput(prev_mm);
1041 mmput(start_mm);
1042 start_mm = new_start_mm;
1043 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001044 if (shmem) {
1045 /* page has already been unlocked and released */
1046 if (shmem > 0)
1047 continue;
1048 retval = shmem;
1049 break;
1050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (retval) {
1052 unlock_page(page);
1053 page_cache_release(page);
1054 break;
1055 }
1056
1057 /*
1058 * How could swap count reach 0x7fff when the maximum
1059 * pid is 0x7fff, and there's no way to repeat a swap
1060 * page within an mm (except in shmem, where it's the
1061 * shared object which takes the reference count)?
1062 * We believe SWAP_MAP_MAX cannot occur in Linux 2.4.
1063 *
1064 * If that's wrong, then we should worry more about
1065 * exit_mmap() and do_munmap() cases described above:
1066 * we might be resetting SWAP_MAP_MAX too early here.
1067 * We know "Undead"s can happen, they're okay, so don't
1068 * report them; but do report if we reset SWAP_MAP_MAX.
1069 */
1070 if (*swap_map == SWAP_MAP_MAX) {
Hugh Dickins5d337b92005-09-03 15:54:41 -07001071 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 *swap_map = 1;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001073 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 reset_overflow = 1;
1075 }
1076
1077 /*
1078 * If a reference remains (rare), we would like to leave
1079 * the page in the swap cache; but try_to_unmap could
1080 * then re-duplicate the entry once we drop page lock,
1081 * so we might loop indefinitely; also, that page could
1082 * not be swapped out to other storage meanwhile. So:
1083 * delete from cache even if there's another reference,
1084 * after ensuring that the data has been saved to disk -
1085 * since if the reference remains (rarer), it will be
1086 * read from disk into another page. Splitting into two
1087 * pages would be incorrect if swap supported "shared
1088 * private" pages, but they are handled by tmpfs files.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 */
1090 if ((*swap_map > 1) && PageDirty(page) && PageSwapCache(page)) {
1091 struct writeback_control wbc = {
1092 .sync_mode = WB_SYNC_NONE,
1093 };
1094
1095 swap_writepage(page, &wbc);
1096 lock_page(page);
1097 wait_on_page_writeback(page);
1098 }
Hugh Dickins68bdc8d2009-01-06 14:39:37 -08001099
1100 /*
1101 * It is conceivable that a racing task removed this page from
1102 * swap cache just before we acquired the page lock at the top,
1103 * or while we dropped it in unuse_mm(). The page might even
1104 * be back in swap cache on another swap area: that we must not
1105 * delete, since it may not have been written out to swap yet.
1106 */
1107 if (PageSwapCache(page) &&
1108 likely(page_private(page) == entry.val))
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001109 delete_from_swap_cache(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 /*
1112 * So we could skip searching mms once swap count went
1113 * to 1, we did not mark any present ptes as dirty: must
Anderson Briglia2706a1b2007-07-15 23:38:09 -07001114 * mark page dirty so shrink_page_list will preserve it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 */
1116 SetPageDirty(page);
1117 unlock_page(page);
1118 page_cache_release(page);
1119
1120 /*
1121 * Make sure that we aren't completely killing
1122 * interactive performance.
1123 */
1124 cond_resched();
1125 }
1126
1127 mmput(start_mm);
1128 if (reset_overflow) {
1129 printk(KERN_WARNING "swapoff: cleared swap entry overflow\n");
1130 swap_overflow = 0;
1131 }
1132 return retval;
1133}
1134
1135/*
Hugh Dickins5d337b92005-09-03 15:54:41 -07001136 * After a successful try_to_unuse, if no swap is now in use, we know
1137 * we can empty the mmlist. swap_lock must be held on entry and exit.
1138 * Note that mmlist_lock nests inside swap_lock, and an mm must be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 * added to the mmlist just after page_duplicate - before would be racy.
1140 */
1141static void drain_mmlist(void)
1142{
1143 struct list_head *p, *next;
1144 unsigned int i;
1145
1146 for (i = 0; i < nr_swapfiles; i++)
1147 if (swap_info[i].inuse_pages)
1148 return;
1149 spin_lock(&mmlist_lock);
1150 list_for_each_safe(p, next, &init_mm.mmlist)
1151 list_del_init(p);
1152 spin_unlock(&mmlist_lock);
1153}
1154
1155/*
1156 * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
1157 * corresponds to page offset `offset'.
1158 */
1159sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset)
1160{
1161 struct swap_extent *se = sis->curr_swap_extent;
1162 struct swap_extent *start_se = se;
1163
1164 for ( ; ; ) {
1165 struct list_head *lh;
1166
1167 if (se->start_page <= offset &&
1168 offset < (se->start_page + se->nr_pages)) {
1169 return se->start_block + (offset - se->start_page);
1170 }
Hugh Dickins11d31882005-09-03 15:54:34 -07001171 lh = se->list.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (lh == &sis->extent_list)
Hugh Dickins11d31882005-09-03 15:54:34 -07001173 lh = lh->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 se = list_entry(lh, struct swap_extent, list);
1175 sis->curr_swap_extent = se;
1176 BUG_ON(se == start_se); /* It *must* be present */
1177 }
1178}
1179
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +02001180#ifdef CONFIG_HIBERNATION
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -08001181/*
1182 * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
1183 * corresponding to given index in swap_info (swap type).
1184 */
1185sector_t swapdev_block(int swap_type, pgoff_t offset)
1186{
1187 struct swap_info_struct *sis;
1188
1189 if (swap_type >= nr_swapfiles)
1190 return 0;
1191
1192 sis = swap_info + swap_type;
1193 return (sis->flags & SWP_WRITEOK) ? map_swap_page(sis, offset) : 0;
1194}
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +02001195#endif /* CONFIG_HIBERNATION */
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -08001196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197/*
1198 * Free all of a swapdev's extent information
1199 */
1200static void destroy_swap_extents(struct swap_info_struct *sis)
1201{
1202 while (!list_empty(&sis->extent_list)) {
1203 struct swap_extent *se;
1204
1205 se = list_entry(sis->extent_list.next,
1206 struct swap_extent, list);
1207 list_del(&se->list);
1208 kfree(se);
1209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
1212/*
1213 * Add a block range (and the corresponding page range) into this swapdev's
Hugh Dickins11d31882005-09-03 15:54:34 -07001214 * extent list. The extent list is kept sorted in page order.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 *
Hugh Dickins11d31882005-09-03 15:54:34 -07001216 * This function rather assumes that it is called in ascending page order.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 */
1218static int
1219add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
1220 unsigned long nr_pages, sector_t start_block)
1221{
1222 struct swap_extent *se;
1223 struct swap_extent *new_se;
1224 struct list_head *lh;
1225
Hugh Dickins11d31882005-09-03 15:54:34 -07001226 lh = sis->extent_list.prev; /* The highest page extent */
1227 if (lh != &sis->extent_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 se = list_entry(lh, struct swap_extent, list);
Hugh Dickins11d31882005-09-03 15:54:34 -07001229 BUG_ON(se->start_page + se->nr_pages != start_page);
1230 if (se->start_block + se->nr_pages == start_block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /* Merge it */
1232 se->nr_pages += nr_pages;
1233 return 0;
1234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236
1237 /*
1238 * No merge. Insert a new extent, preserving ordering.
1239 */
1240 new_se = kmalloc(sizeof(*se), GFP_KERNEL);
1241 if (new_se == NULL)
1242 return -ENOMEM;
1243 new_se->start_page = start_page;
1244 new_se->nr_pages = nr_pages;
1245 new_se->start_block = start_block;
1246
Hugh Dickins11d31882005-09-03 15:54:34 -07001247 list_add_tail(&new_se->list, &sis->extent_list);
Hugh Dickins53092a72005-09-03 15:54:34 -07001248 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
1251/*
1252 * A `swap extent' is a simple thing which maps a contiguous range of pages
1253 * onto a contiguous range of disk blocks. An ordered list of swap extents
1254 * is built at swapon time and is then used at swap_writepage/swap_readpage
1255 * time for locating where on disk a page belongs.
1256 *
1257 * If the swapfile is an S_ISBLK block device, a single extent is installed.
1258 * This is done so that the main operating code can treat S_ISBLK and S_ISREG
1259 * swap files identically.
1260 *
1261 * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
1262 * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
1263 * swapfiles are handled *identically* after swapon time.
1264 *
1265 * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
1266 * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
1267 * some stray blocks are found which do not fall within the PAGE_SIZE alignment
1268 * requirements, they are simply tossed out - we will never use those blocks
1269 * for swapping.
1270 *
Hugh Dickinsb0d9bcd2005-09-03 15:54:31 -07001271 * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 * prevents root from shooting her foot off by ftruncating an in-use swapfile,
1273 * which will scribble on the fs.
1274 *
1275 * The amount of disk space which a single swap extent represents varies.
1276 * Typically it is in the 1-4 megabyte range. So we can have hundreds of
1277 * extents in the list. To avoid much list walking, we cache the previous
1278 * search location in `curr_swap_extent', and start new searches from there.
1279 * This is extremely effective. The average number of iterations in
1280 * map_swap_page() has been measured at about 0.3 per page. - akpm.
1281 */
Hugh Dickins53092a72005-09-03 15:54:34 -07001282static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
1284 struct inode *inode;
1285 unsigned blocks_per_page;
1286 unsigned long page_no;
1287 unsigned blkbits;
1288 sector_t probe_block;
1289 sector_t last_block;
Hugh Dickins53092a72005-09-03 15:54:34 -07001290 sector_t lowest_block = -1;
1291 sector_t highest_block = 0;
1292 int nr_extents = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 int ret;
1294
1295 inode = sis->swap_file->f_mapping->host;
1296 if (S_ISBLK(inode->i_mode)) {
1297 ret = add_swap_extent(sis, 0, sis->max, 0);
Hugh Dickins53092a72005-09-03 15:54:34 -07001298 *span = sis->pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 goto done;
1300 }
1301
1302 blkbits = inode->i_blkbits;
1303 blocks_per_page = PAGE_SIZE >> blkbits;
1304
1305 /*
1306 * Map all the blocks into the extent list. This code doesn't try
1307 * to be very smart.
1308 */
1309 probe_block = 0;
1310 page_no = 0;
1311 last_block = i_size_read(inode) >> blkbits;
1312 while ((probe_block + blocks_per_page) <= last_block &&
1313 page_no < sis->max) {
1314 unsigned block_in_page;
1315 sector_t first_block;
1316
1317 first_block = bmap(inode, probe_block);
1318 if (first_block == 0)
1319 goto bad_bmap;
1320
1321 /*
1322 * It must be PAGE_SIZE aligned on-disk
1323 */
1324 if (first_block & (blocks_per_page - 1)) {
1325 probe_block++;
1326 goto reprobe;
1327 }
1328
1329 for (block_in_page = 1; block_in_page < blocks_per_page;
1330 block_in_page++) {
1331 sector_t block;
1332
1333 block = bmap(inode, probe_block + block_in_page);
1334 if (block == 0)
1335 goto bad_bmap;
1336 if (block != first_block + block_in_page) {
1337 /* Discontiguity */
1338 probe_block++;
1339 goto reprobe;
1340 }
1341 }
1342
Hugh Dickins53092a72005-09-03 15:54:34 -07001343 first_block >>= (PAGE_SHIFT - blkbits);
1344 if (page_no) { /* exclude the header page */
1345 if (first_block < lowest_block)
1346 lowest_block = first_block;
1347 if (first_block > highest_block)
1348 highest_block = first_block;
1349 }
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 /*
1352 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
1353 */
Hugh Dickins53092a72005-09-03 15:54:34 -07001354 ret = add_swap_extent(sis, page_no, 1, first_block);
1355 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 goto out;
Hugh Dickins53092a72005-09-03 15:54:34 -07001357 nr_extents += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 page_no++;
1359 probe_block += blocks_per_page;
1360reprobe:
1361 continue;
1362 }
Hugh Dickins53092a72005-09-03 15:54:34 -07001363 ret = nr_extents;
1364 *span = 1 + highest_block - lowest_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (page_no == 0)
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001366 page_no = 1; /* force Empty message */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 sis->max = page_no;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001368 sis->pages = page_no - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 sis->highest_bit = page_no - 1;
1370done:
1371 sis->curr_swap_extent = list_entry(sis->extent_list.prev,
1372 struct swap_extent, list);
1373 goto out;
1374bad_bmap:
1375 printk(KERN_ERR "swapon: swapfile has holes\n");
1376 ret = -EINVAL;
1377out:
1378 return ret;
1379}
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381asmlinkage long sys_swapoff(const char __user * specialfile)
1382{
1383 struct swap_info_struct * p = NULL;
1384 unsigned short *swap_map;
1385 struct file *swap_file, *victim;
1386 struct address_space *mapping;
1387 struct inode *inode;
1388 char * pathname;
1389 int i, type, prev;
1390 int err;
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (!capable(CAP_SYS_ADMIN))
1393 return -EPERM;
1394
1395 pathname = getname(specialfile);
1396 err = PTR_ERR(pathname);
1397 if (IS_ERR(pathname))
1398 goto out;
1399
1400 victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
1401 putname(pathname);
1402 err = PTR_ERR(victim);
1403 if (IS_ERR(victim))
1404 goto out;
1405
1406 mapping = victim->f_mapping;
1407 prev = -1;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001408 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 for (type = swap_list.head; type >= 0; type = swap_info[type].next) {
1410 p = swap_info + type;
Hugh Dickins22c6f8fd2009-01-06 14:39:48 -08001411 if (p->flags & SWP_WRITEOK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (p->swap_file->f_mapping == mapping)
1413 break;
1414 }
1415 prev = type;
1416 }
1417 if (type < 0) {
1418 err = -EINVAL;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001419 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 goto out_dput;
1421 }
1422 if (!security_vm_enough_memory(p->pages))
1423 vm_unacct_memory(p->pages);
1424 else {
1425 err = -ENOMEM;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001426 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 goto out_dput;
1428 }
1429 if (prev < 0) {
1430 swap_list.head = p->next;
1431 } else {
1432 swap_info[prev].next = p->next;
1433 }
1434 if (type == swap_list.next) {
1435 /* just pick something that's safe... */
1436 swap_list.next = swap_list.head;
1437 }
Hugh Dickins78ecba02008-07-23 21:28:23 -07001438 if (p->prio < 0) {
1439 for (i = p->next; i >= 0; i = swap_info[i].next)
1440 swap_info[i].prio = p->prio--;
1441 least_priority++;
1442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 nr_swap_pages -= p->pages;
1444 total_swap_pages -= p->pages;
1445 p->flags &= ~SWP_WRITEOK;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001446 spin_unlock(&swap_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -07001447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 current->flags |= PF_SWAPOFF;
1449 err = try_to_unuse(type);
1450 current->flags &= ~PF_SWAPOFF;
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (err) {
1453 /* re-insert swap space back into swap_list */
Hugh Dickins5d337b92005-09-03 15:54:41 -07001454 spin_lock(&swap_lock);
Hugh Dickins78ecba02008-07-23 21:28:23 -07001455 if (p->prio < 0)
1456 p->prio = --least_priority;
1457 prev = -1;
1458 for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (p->prio >= swap_info[i].prio)
1460 break;
Hugh Dickins78ecba02008-07-23 21:28:23 -07001461 prev = i;
1462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 p->next = i;
1464 if (prev < 0)
1465 swap_list.head = swap_list.next = p - swap_info;
1466 else
1467 swap_info[prev].next = p - swap_info;
1468 nr_swap_pages += p->pages;
1469 total_swap_pages += p->pages;
1470 p->flags |= SWP_WRITEOK;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001471 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 goto out_dput;
1473 }
Hugh Dickins52b7efdb2005-09-03 15:54:39 -07001474
1475 /* wait for any unplug function to finish */
1476 down_write(&swap_unplug_sem);
1477 up_write(&swap_unplug_sem);
1478
Hugh Dickins4cd3bb12005-09-03 15:54:33 -07001479 destroy_swap_extents(p);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001480 mutex_lock(&swapon_mutex);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001481 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 drain_mmlist();
Hugh Dickins5d337b92005-09-03 15:54:41 -07001483
1484 /* wait for anyone still in scan_swap_map */
1485 p->highest_bit = 0; /* cuts scans short */
1486 while (p->flags >= SWP_SCANNING) {
1487 spin_unlock(&swap_lock);
Nishanth Aravamudan13e4b572005-09-10 00:27:25 -07001488 schedule_timeout_uninterruptible(1);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001489 spin_lock(&swap_lock);
1490 }
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 swap_file = p->swap_file;
1493 p->swap_file = NULL;
1494 p->max = 0;
1495 swap_map = p->swap_map;
1496 p->swap_map = NULL;
1497 p->flags = 0;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001498 spin_unlock(&swap_lock);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001499 mutex_unlock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 vfree(swap_map);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08001501 /* Destroy swap account informatin */
1502 swap_cgroup_swapoff(type);
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 inode = mapping->host;
1505 if (S_ISBLK(inode->i_mode)) {
1506 struct block_device *bdev = I_BDEV(inode);
1507 set_blocksize(bdev, p->old_block_size);
1508 bd_release(bdev);
1509 } else {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001510 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 inode->i_flags &= ~S_SWAPFILE;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001512 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
1514 filp_close(swap_file, NULL);
1515 err = 0;
1516
1517out_dput:
1518 filp_close(victim, NULL);
1519out:
1520 return err;
1521}
1522
1523#ifdef CONFIG_PROC_FS
1524/* iterator */
1525static void *swap_start(struct seq_file *swap, loff_t *pos)
1526{
1527 struct swap_info_struct *ptr = swap_info;
1528 int i;
1529 loff_t l = *pos;
1530
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001531 mutex_lock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001533 if (!l)
1534 return SEQ_START_TOKEN;
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 for (i = 0; i < nr_swapfiles; i++, ptr++) {
1537 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1538 continue;
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001539 if (!--l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return ptr;
1541 }
1542
1543 return NULL;
1544}
1545
1546static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
1547{
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001548 struct swap_info_struct *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 struct swap_info_struct *endptr = swap_info + nr_swapfiles;
1550
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001551 if (v == SEQ_START_TOKEN)
1552 ptr = swap_info;
1553 else {
1554 ptr = v;
1555 ptr++;
1556 }
1557
1558 for (; ptr < endptr; ptr++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1560 continue;
1561 ++*pos;
1562 return ptr;
1563 }
1564
1565 return NULL;
1566}
1567
1568static void swap_stop(struct seq_file *swap, void *v)
1569{
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001570 mutex_unlock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
1573static int swap_show(struct seq_file *swap, void *v)
1574{
1575 struct swap_info_struct *ptr = v;
1576 struct file *file;
1577 int len;
1578
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001579 if (ptr == SEQ_START_TOKEN) {
1580 seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1581 return 0;
1582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 file = ptr->swap_file;
Jan Blunckc32c2f62008-02-14 19:38:43 -08001585 len = seq_path(swap, &file->f_path, " \t\n\\");
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001586 seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001587 len < 40 ? 40 - len : 1, " ",
1588 S_ISBLK(file->f_path.dentry->d_inode->i_mode) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 "partition" : "file\t",
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001590 ptr->pages << (PAGE_SHIFT - 10),
1591 ptr->inuse_pages << (PAGE_SHIFT - 10),
1592 ptr->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 return 0;
1594}
1595
Helge Deller15ad7cd2006-12-06 20:40:36 -08001596static const struct seq_operations swaps_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 .start = swap_start,
1598 .next = swap_next,
1599 .stop = swap_stop,
1600 .show = swap_show
1601};
1602
1603static int swaps_open(struct inode *inode, struct file *file)
1604{
1605 return seq_open(file, &swaps_op);
1606}
1607
Helge Deller15ad7cd2006-12-06 20:40:36 -08001608static const struct file_operations proc_swaps_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 .open = swaps_open,
1610 .read = seq_read,
1611 .llseek = seq_lseek,
1612 .release = seq_release,
1613};
1614
1615static int __init procswaps_init(void)
1616{
Denis V. Lunev3d71f862008-04-29 01:02:13 -07001617 proc_create("swaps", 0, NULL, &proc_swaps_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return 0;
1619}
1620__initcall(procswaps_init);
1621#endif /* CONFIG_PROC_FS */
1622
Jan Beulich17963162008-12-16 11:35:24 +00001623#ifdef MAX_SWAPFILES_CHECK
1624static int __init max_swapfiles_check(void)
1625{
1626 MAX_SWAPFILES_CHECK();
1627 return 0;
1628}
1629late_initcall(max_swapfiles_check);
1630#endif
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632/*
1633 * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
1634 *
1635 * The swapon system call
1636 */
1637asmlinkage long sys_swapon(const char __user * specialfile, int swap_flags)
1638{
1639 struct swap_info_struct * p;
1640 char *name = NULL;
1641 struct block_device *bdev = NULL;
1642 struct file *swap_file = NULL;
1643 struct address_space *mapping;
1644 unsigned int type;
1645 int i, prev;
1646 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 union swap_header *swap_header = NULL;
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001648 unsigned int nr_good_pages = 0;
1649 int nr_extents = 0;
Hugh Dickins53092a72005-09-03 15:54:34 -07001650 sector_t span;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 unsigned long maxpages = 1;
Hugh Dickins73fd8742009-01-06 14:39:47 -08001652 unsigned long swapfilepages;
Hugh Dickins78ecba02008-07-23 21:28:23 -07001653 unsigned short *swap_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 struct page *page = NULL;
1655 struct inode *inode = NULL;
1656 int did_down = 0;
1657
1658 if (!capable(CAP_SYS_ADMIN))
1659 return -EPERM;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001660 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 p = swap_info;
1662 for (type = 0 ; type < nr_swapfiles ; type++,p++)
1663 if (!(p->flags & SWP_USED))
1664 break;
1665 error = -EPERM;
Christoph Lameter06972122006-06-23 02:03:35 -07001666 if (type >= MAX_SWAPFILES) {
Hugh Dickins5d337b92005-09-03 15:54:41 -07001667 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 goto out;
1669 }
1670 if (type >= nr_swapfiles)
1671 nr_swapfiles = type+1;
Hugh Dickins78ecba02008-07-23 21:28:23 -07001672 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 INIT_LIST_HEAD(&p->extent_list);
1674 p->flags = SWP_USED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 p->next = -1;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001676 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 name = getname(specialfile);
1678 error = PTR_ERR(name);
1679 if (IS_ERR(name)) {
1680 name = NULL;
1681 goto bad_swap_2;
1682 }
1683 swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
1684 error = PTR_ERR(swap_file);
1685 if (IS_ERR(swap_file)) {
1686 swap_file = NULL;
1687 goto bad_swap_2;
1688 }
1689
1690 p->swap_file = swap_file;
1691 mapping = swap_file->f_mapping;
1692 inode = mapping->host;
1693
1694 error = -EBUSY;
1695 for (i = 0; i < nr_swapfiles; i++) {
1696 struct swap_info_struct *q = &swap_info[i];
1697
1698 if (i == type || !q->swap_file)
1699 continue;
1700 if (mapping == q->swap_file->f_mapping)
1701 goto bad_swap;
1702 }
1703
1704 error = -EINVAL;
1705 if (S_ISBLK(inode->i_mode)) {
1706 bdev = I_BDEV(inode);
1707 error = bd_claim(bdev, sys_swapon);
1708 if (error < 0) {
1709 bdev = NULL;
Rob Landleyf7b3a432005-09-22 21:44:27 -07001710 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 goto bad_swap;
1712 }
1713 p->old_block_size = block_size(bdev);
1714 error = set_blocksize(bdev, PAGE_SIZE);
1715 if (error < 0)
1716 goto bad_swap;
1717 p->bdev = bdev;
1718 } else if (S_ISREG(inode->i_mode)) {
1719 p->bdev = inode->i_sb->s_bdev;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001720 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 did_down = 1;
1722 if (IS_SWAPFILE(inode)) {
1723 error = -EBUSY;
1724 goto bad_swap;
1725 }
1726 } else {
1727 goto bad_swap;
1728 }
1729
Hugh Dickins73fd8742009-01-06 14:39:47 -08001730 swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 /*
1733 * Read the swap header.
1734 */
1735 if (!mapping->a_ops->readpage) {
1736 error = -EINVAL;
1737 goto bad_swap;
1738 }
Pekka Enberg090d2b12006-06-23 02:05:08 -07001739 page = read_mapping_page(mapping, 0, swap_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (IS_ERR(page)) {
1741 error = PTR_ERR(page);
1742 goto bad_swap;
1743 }
Hugh Dickins81e33972009-01-06 14:39:49 -08001744 swap_header = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Hugh Dickins81e33972009-01-06 14:39:49 -08001746 if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
Jesper Juhle97a3112006-01-11 01:50:28 +01001747 printk(KERN_ERR "Unable to find swap-space signature\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 error = -EINVAL;
1749 goto bad_swap;
1750 }
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001751
Hugh Dickins81e33972009-01-06 14:39:49 -08001752 /* swap partition endianess hack... */
1753 if (swab32(swap_header->info.version) == 1) {
1754 swab32s(&swap_header->info.version);
1755 swab32s(&swap_header->info.last_page);
1756 swab32s(&swap_header->info.nr_badpages);
1757 for (i = 0; i < swap_header->info.nr_badpages; i++)
1758 swab32s(&swap_header->info.badpages[i]);
1759 }
1760 /* Check the swap header's sub-version */
1761 if (swap_header->info.version != 1) {
1762 printk(KERN_WARNING
1763 "Unable to handle swap header version %d\n",
1764 swap_header->info.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 error = -EINVAL;
1766 goto bad_swap;
Hugh Dickins81e33972009-01-06 14:39:49 -08001767 }
1768
1769 p->lowest_bit = 1;
1770 p->cluster_next = 1;
1771
1772 /*
1773 * Find out how many pages are allowed for a single swap
1774 * device. There are two limiting factors: 1) the number of
1775 * bits for the swap offset in the swp_entry_t type and
1776 * 2) the number of bits in the a swap pte as defined by
1777 * the different architectures. In order to find the
1778 * largest possible bit mask a swap entry with swap type 0
1779 * and swap offset ~0UL is created, encoded to a swap pte,
1780 * decoded to a swp_entry_t again and finally the swap
1781 * offset is extracted. This will mask all the bits from
1782 * the initial ~0UL mask that can't be encoded in either
1783 * the swp_entry_t or the architecture definition of a
1784 * swap pte.
1785 */
1786 maxpages = swp_offset(pte_to_swp_entry(
1787 swp_entry_to_pte(swp_entry(0, ~0UL)))) - 1;
1788 if (maxpages > swap_header->info.last_page)
1789 maxpages = swap_header->info.last_page;
1790 p->highest_bit = maxpages - 1;
1791
1792 error = -EINVAL;
1793 if (!maxpages)
1794 goto bad_swap;
1795 if (swapfilepages && maxpages > swapfilepages) {
1796 printk(KERN_WARNING
1797 "Swap area shorter than signature indicates\n");
1798 goto bad_swap;
1799 }
1800 if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
1801 goto bad_swap;
1802 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
1803 goto bad_swap;
1804
1805 /* OK, set up the swap map and apply the bad block list */
1806 swap_map = vmalloc(maxpages * sizeof(short));
1807 if (!swap_map) {
1808 error = -ENOMEM;
1809 goto bad_swap;
1810 }
1811
1812 memset(swap_map, 0, maxpages * sizeof(short));
1813 for (i = 0; i < swap_header->info.nr_badpages; i++) {
1814 int page_nr = swap_header->info.badpages[i];
1815 if (page_nr <= 0 || page_nr >= swap_header->info.last_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 error = -EINVAL;
1817 goto bad_swap;
1818 }
Hugh Dickins81e33972009-01-06 14:39:49 -08001819 swap_map[page_nr] = SWAP_MAP_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08001821
1822 error = swap_cgroup_swapon(type, maxpages);
1823 if (error)
1824 goto bad_swap;
1825
Hugh Dickins81e33972009-01-06 14:39:49 -08001826 nr_good_pages = swap_header->info.last_page -
1827 swap_header->info.nr_badpages -
1828 1 /* header page */;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001829
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001830 if (nr_good_pages) {
Hugh Dickins78ecba02008-07-23 21:28:23 -07001831 swap_map[0] = SWAP_MAP_BAD;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001832 p->max = maxpages;
1833 p->pages = nr_good_pages;
Hugh Dickins53092a72005-09-03 15:54:34 -07001834 nr_extents = setup_swap_extents(p, &span);
1835 if (nr_extents < 0) {
1836 error = nr_extents;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001837 goto bad_swap;
Hugh Dickins53092a72005-09-03 15:54:34 -07001838 }
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001839 nr_good_pages = p->pages;
1840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (!nr_good_pages) {
1842 printk(KERN_WARNING "Empty swap-file\n");
1843 error = -EINVAL;
1844 goto bad_swap;
1845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Hugh Dickins20137a42009-01-06 14:39:54 -08001847 if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
1848 p->flags |= SWP_SOLIDSTATE;
Hugh Dickins20137a42009-01-06 14:39:54 -08001849 p->cluster_next = 1 + (random32() % p->highest_bit);
1850 }
Hugh Dickins6a6ba832009-01-06 14:39:51 -08001851 if (discard_swap(p) == 0)
1852 p->flags |= SWP_DISCARDABLE;
1853
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001854 mutex_lock(&swapon_mutex);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001855 spin_lock(&swap_lock);
Hugh Dickins78ecba02008-07-23 21:28:23 -07001856 if (swap_flags & SWAP_FLAG_PREFER)
1857 p->prio =
1858 (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
1859 else
1860 p->prio = --least_priority;
1861 p->swap_map = swap_map;
Hugh Dickins22c6f8fd2009-01-06 14:39:48 -08001862 p->flags |= SWP_WRITEOK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 nr_swap_pages += nr_good_pages;
1864 total_swap_pages += nr_good_pages;
Hugh Dickins53092a72005-09-03 15:54:34 -07001865
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001866 printk(KERN_INFO "Adding %uk swap on %s. "
Hugh Dickins20137a42009-01-06 14:39:54 -08001867 "Priority:%d extents:%d across:%lluk %s%s\n",
Hugh Dickins53092a72005-09-03 15:54:34 -07001868 nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
Hugh Dickins6a6ba832009-01-06 14:39:51 -08001869 nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
Hugh Dickins20137a42009-01-06 14:39:54 -08001870 (p->flags & SWP_SOLIDSTATE) ? "SS" : "",
1871 (p->flags & SWP_DISCARDABLE) ? "D" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 /* insert swap space into swap_list: */
1874 prev = -1;
1875 for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
1876 if (p->prio >= swap_info[i].prio) {
1877 break;
1878 }
1879 prev = i;
1880 }
1881 p->next = i;
1882 if (prev < 0) {
1883 swap_list.head = swap_list.next = p - swap_info;
1884 } else {
1885 swap_info[prev].next = p - swap_info;
1886 }
Hugh Dickins5d337b92005-09-03 15:54:41 -07001887 spin_unlock(&swap_lock);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001888 mutex_unlock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 error = 0;
1890 goto out;
1891bad_swap:
1892 if (bdev) {
1893 set_blocksize(bdev, p->old_block_size);
1894 bd_release(bdev);
1895 }
Hugh Dickins4cd3bb12005-09-03 15:54:33 -07001896 destroy_swap_extents(p);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08001897 swap_cgroup_swapoff(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898bad_swap_2:
Hugh Dickins5d337b92005-09-03 15:54:41 -07001899 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 p->swap_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 p->flags = 0;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001902 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 vfree(swap_map);
1904 if (swap_file)
1905 filp_close(swap_file, NULL);
1906out:
1907 if (page && !IS_ERR(page)) {
1908 kunmap(page);
1909 page_cache_release(page);
1910 }
1911 if (name)
1912 putname(name);
1913 if (did_down) {
1914 if (!error)
1915 inode->i_flags |= S_SWAPFILE;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001916 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
1918 return error;
1919}
1920
1921void si_swapinfo(struct sysinfo *val)
1922{
1923 unsigned int i;
1924 unsigned long nr_to_be_unused = 0;
1925
Hugh Dickins5d337b92005-09-03 15:54:41 -07001926 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 for (i = 0; i < nr_swapfiles; i++) {
1928 if (!(swap_info[i].flags & SWP_USED) ||
1929 (swap_info[i].flags & SWP_WRITEOK))
1930 continue;
1931 nr_to_be_unused += swap_info[i].inuse_pages;
1932 }
1933 val->freeswap = nr_swap_pages + nr_to_be_unused;
1934 val->totalswap = total_swap_pages + nr_to_be_unused;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001935 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
1938/*
1939 * Verify that a swap entry is valid and increment its swap map count.
1940 *
1941 * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
1942 * "permanent", but will be reclaimed by the next swapoff.
1943 */
1944int swap_duplicate(swp_entry_t entry)
1945{
1946 struct swap_info_struct * p;
1947 unsigned long offset, type;
1948 int result = 0;
1949
Christoph Lameter06972122006-06-23 02:03:35 -07001950 if (is_migration_entry(entry))
1951 return 1;
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 type = swp_type(entry);
1954 if (type >= nr_swapfiles)
1955 goto bad_file;
1956 p = type + swap_info;
1957 offset = swp_offset(entry);
1958
Hugh Dickins5d337b92005-09-03 15:54:41 -07001959 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if (offset < p->max && p->swap_map[offset]) {
1961 if (p->swap_map[offset] < SWAP_MAP_MAX - 1) {
1962 p->swap_map[offset]++;
1963 result = 1;
1964 } else if (p->swap_map[offset] <= SWAP_MAP_MAX) {
1965 if (swap_overflow++ < 5)
1966 printk(KERN_WARNING "swap_dup: swap entry overflow\n");
1967 p->swap_map[offset] = SWAP_MAP_MAX;
1968 result = 1;
1969 }
1970 }
Hugh Dickins5d337b92005-09-03 15:54:41 -07001971 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972out:
1973 return result;
1974
1975bad_file:
1976 printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
1977 goto out;
1978}
1979
1980struct swap_info_struct *
1981get_swap_info_struct(unsigned type)
1982{
1983 return &swap_info[type];
1984}
1985
1986/*
Hugh Dickins5d337b92005-09-03 15:54:41 -07001987 * swap_lock prevents swap_map being freed. Don't grab an extra
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 * reference on the swaphandle, it doesn't matter if it becomes unused.
1989 */
1990int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
1991{
Hugh Dickins89528982008-02-04 22:28:45 -08001992 struct swap_info_struct *si;
Hugh Dickins3f9e7942006-09-29 02:01:26 -07001993 int our_page_cluster = page_cluster;
Hugh Dickins89528982008-02-04 22:28:45 -08001994 pgoff_t target, toff;
1995 pgoff_t base, end;
1996 int nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Hugh Dickins3f9e7942006-09-29 02:01:26 -07001998 if (!our_page_cluster) /* no readahead */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 return 0;
Hugh Dickins89528982008-02-04 22:28:45 -08002000
2001 si = &swap_info[swp_type(entry)];
2002 target = swp_offset(entry);
2003 base = (target >> our_page_cluster) << our_page_cluster;
2004 end = base + (1 << our_page_cluster);
2005 if (!base) /* first page is swap header */
2006 base++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Hugh Dickins5d337b92005-09-03 15:54:41 -07002008 spin_lock(&swap_lock);
Hugh Dickins89528982008-02-04 22:28:45 -08002009 if (end > si->max) /* don't go beyond end of map */
2010 end = si->max;
2011
2012 /* Count contiguous allocated slots above our target */
2013 for (toff = target; ++toff < end; nr_pages++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 /* Don't read in free or bad pages */
Hugh Dickins89528982008-02-04 22:28:45 -08002015 if (!si->swap_map[toff])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 break;
Hugh Dickins89528982008-02-04 22:28:45 -08002017 if (si->swap_map[toff] == SWAP_MAP_BAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 break;
Hugh Dickins89528982008-02-04 22:28:45 -08002019 }
2020 /* Count contiguous allocated slots below our target */
2021 for (toff = target; --toff >= base; nr_pages++) {
2022 /* Don't read in free or bad pages */
2023 if (!si->swap_map[toff])
2024 break;
2025 if (si->swap_map[toff] == SWAP_MAP_BAD)
2026 break;
2027 }
Hugh Dickins5d337b92005-09-03 15:54:41 -07002028 spin_unlock(&swap_lock);
Hugh Dickins89528982008-02-04 22:28:45 -08002029
2030 /*
2031 * Indicate starting offset, and return number of pages to get:
2032 * if only 1, say 0, since there's then no readahead to be done.
2033 */
2034 *offset = ++toff;
2035 return nr_pages? ++nr_pages: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036}