blob: 8ffdc0d23c536fe04779a9c559af68f6d9470997 [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
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -070056/* For reference count accounting in swap_map */
57/* enum for swap_map[] handling. internal use only */
58enum {
59 SWAP_MAP = 0, /* ops for reference from swap users */
60 SWAP_CACHE, /* ops for reference from swap cache */
61};
62
63static inline int swap_count(unsigned short ent)
64{
65 return ent & SWAP_COUNT_MASK;
66}
67
68static inline bool swap_has_cache(unsigned short ent)
69{
70 return !!(ent & SWAP_HAS_CACHE);
71}
72
73static inline unsigned short encode_swapmap(int count, bool has_cache)
74{
75 unsigned short ret = count;
76
77 if (has_cache)
78 return SWAP_HAS_CACHE | ret;
79 return ret;
80}
81
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -070082/* returnes 1 if swap entry is freed */
83static int
84__try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
85{
86 int type = si - swap_info;
87 swp_entry_t entry = swp_entry(type, offset);
88 struct page *page;
89 int ret = 0;
90
91 page = find_get_page(&swapper_space, entry.val);
92 if (!page)
93 return 0;
94 /*
95 * This function is called from scan_swap_map() and it's called
96 * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here.
97 * We have to use trylock for avoiding deadlock. This is a special
98 * case and you should use try_to_free_swap() with explicit lock_page()
99 * in usual operations.
100 */
101 if (trylock_page(page)) {
102 ret = try_to_free_swap(page);
103 unlock_page(page);
104 }
105 page_cache_release(page);
106 return ret;
107}
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
110 * We need this because the bdev->unplug_fn can sleep and we cannot
Hugh Dickins5d337b92005-09-03 15:54:41 -0700111 * hold swap_lock while calling the unplug_fn. And swap_lock
Ingo Molnarfc0abb12006-01-18 17:42:33 -0800112 * cannot be turned into a mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 */
114static DECLARE_RWSEM(swap_unplug_sem);
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page)
117{
118 swp_entry_t entry;
119
120 down_read(&swap_unplug_sem);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700121 entry.val = page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (PageSwapCache(page)) {
123 struct block_device *bdev = swap_info[swp_type(entry)].bdev;
124 struct backing_dev_info *bdi;
125
126 /*
127 * If the page is removed from swapcache from under us (with a
128 * racy try_to_unuse/swapoff) we need an additional reference
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700129 * count to avoid reading garbage from page_private(page) above.
130 * If the WARN_ON triggers during a swapoff it maybe the race
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * condition and it's harmless. However if it triggers without
132 * swapoff it signals a problem.
133 */
134 WARN_ON(page_count(page) <= 1);
135
136 bdi = bdev->bd_inode->i_mapping->backing_dev_info;
McMullan, Jasonba323112005-05-16 21:53:40 -0700137 blk_run_backing_dev(bdi, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139 up_read(&swap_unplug_sem);
140}
141
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800142/*
143 * swapon tell device that all the old swap contents can be discarded,
144 * to allow the swap device to optimize its wear-levelling.
145 */
146static int discard_swap(struct swap_info_struct *si)
147{
148 struct swap_extent *se;
149 int err = 0;
150
151 list_for_each_entry(se, &si->extent_list, list) {
152 sector_t start_block = se->start_block << (PAGE_SHIFT - 9);
Hugh Dickins858a29902009-01-06 14:39:56 -0800153 sector_t nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800154
155 if (se->start_page == 0) {
156 /* Do not discard the swap header page! */
157 start_block += 1 << (PAGE_SHIFT - 9);
158 nr_blocks -= 1 << (PAGE_SHIFT - 9);
159 if (!nr_blocks)
160 continue;
161 }
162
163 err = blkdev_issue_discard(si->bdev, start_block,
164 nr_blocks, GFP_KERNEL);
165 if (err)
166 break;
167
168 cond_resched();
169 }
170 return err; /* That will often be -EOPNOTSUPP */
171}
172
Hugh Dickins7992fde2009-01-06 14:39:53 -0800173/*
174 * swap allocation tell device that a cluster of swap can now be discarded,
175 * to allow the swap device to optimize its wear-levelling.
176 */
177static void discard_swap_cluster(struct swap_info_struct *si,
178 pgoff_t start_page, pgoff_t nr_pages)
179{
180 struct swap_extent *se = si->curr_swap_extent;
181 int found_extent = 0;
182
183 while (nr_pages) {
184 struct list_head *lh;
185
186 if (se->start_page <= start_page &&
187 start_page < se->start_page + se->nr_pages) {
188 pgoff_t offset = start_page - se->start_page;
189 sector_t start_block = se->start_block + offset;
Hugh Dickins858a29902009-01-06 14:39:56 -0800190 sector_t nr_blocks = se->nr_pages - offset;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800191
192 if (nr_blocks > nr_pages)
193 nr_blocks = nr_pages;
194 start_page += nr_blocks;
195 nr_pages -= nr_blocks;
196
197 if (!found_extent++)
198 si->curr_swap_extent = se;
199
200 start_block <<= PAGE_SHIFT - 9;
201 nr_blocks <<= PAGE_SHIFT - 9;
202 if (blkdev_issue_discard(si->bdev, start_block,
203 nr_blocks, GFP_NOIO))
204 break;
205 }
206
207 lh = se->list.next;
208 if (lh == &si->extent_list)
209 lh = lh->next;
210 se = list_entry(lh, struct swap_extent, list);
211 }
212}
213
214static int wait_for_discard(void *word)
215{
216 schedule();
217 return 0;
218}
219
Hugh Dickins048c27f2005-09-03 15:54:40 -0700220#define SWAPFILE_CLUSTER 256
221#define LATENCY_LIMIT 256
222
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700223static inline unsigned long scan_swap_map(struct swap_info_struct *si,
224 int cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800226 unsigned long offset;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800227 unsigned long scan_base;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800228 unsigned long last_in_cluster = 0;
Hugh Dickins048c27f2005-09-03 15:54:40 -0700229 int latency_ration = LATENCY_LIMIT;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800230 int found_free_cluster = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Hugh Dickins886bb7e2009-01-06 14:39:48 -0800232 /*
Hugh Dickins7dfad412005-09-03 15:54:38 -0700233 * We try to cluster swap pages by allocating them sequentially
234 * in swap. Once we've allocated SWAPFILE_CLUSTER pages this
235 * way, however, we resort to first-free allocation, starting
236 * a new cluster. This prevents us from scattering swap pages
237 * all over the entire swap partition, so that we reduce
238 * overall disk seek times between swap pages. -- sct
239 * But we do now try to find an empty cluster. -Andrea
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800240 * And we let swap pages go all over an SSD partition. Hugh
Hugh Dickins7dfad412005-09-03 15:54:38 -0700241 */
242
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700243 si->flags += SWP_SCANNING;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800244 scan_base = offset = si->cluster_next;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800245
246 if (unlikely(!si->cluster_nr--)) {
247 if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
248 si->cluster_nr = SWAPFILE_CLUSTER - 1;
249 goto checks;
250 }
Hugh Dickins7992fde2009-01-06 14:39:53 -0800251 if (si->flags & SWP_DISCARDABLE) {
252 /*
253 * Start range check on racing allocations, in case
254 * they overlap the cluster we eventually decide on
255 * (we scan without swap_lock to allow preemption).
256 * It's hardly conceivable that cluster_nr could be
257 * wrapped during our scan, but don't depend on it.
258 */
259 if (si->lowest_alloc)
260 goto checks;
261 si->lowest_alloc = si->max;
262 si->highest_alloc = 0;
263 }
Hugh Dickins5d337b92005-09-03 15:54:41 -0700264 spin_unlock(&swap_lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700265
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800266 /*
267 * If seek is expensive, start searching for new cluster from
268 * start of partition, to minimize the span of allocated swap.
269 * But if seek is cheap, search from our current position, so
270 * that swap is allocated from all over the partition: if the
271 * Flash Translation Layer only remaps within limited zones,
272 * we don't want to wear out the first zone too quickly.
273 */
274 if (!(si->flags & SWP_SOLIDSTATE))
275 scan_base = offset = si->lowest_bit;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700276 last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
277
278 /* Locate the first empty (unaligned) cluster */
279 for (; last_in_cluster <= si->highest_bit; offset++) {
280 if (si->swap_map[offset])
281 last_in_cluster = offset + SWAPFILE_CLUSTER;
282 else if (offset == last_in_cluster) {
Hugh Dickins5d337b92005-09-03 15:54:41 -0700283 spin_lock(&swap_lock);
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800284 offset -= SWAPFILE_CLUSTER - 1;
285 si->cluster_next = offset;
286 si->cluster_nr = SWAPFILE_CLUSTER - 1;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800287 found_free_cluster = 1;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800288 goto checks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
Hugh Dickins048c27f2005-09-03 15:54:40 -0700290 if (unlikely(--latency_ration < 0)) {
291 cond_resched();
292 latency_ration = LATENCY_LIMIT;
293 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700294 }
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800295
296 offset = si->lowest_bit;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800297 last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
298
299 /* Locate the first empty (unaligned) cluster */
300 for (; last_in_cluster < scan_base; offset++) {
301 if (si->swap_map[offset])
302 last_in_cluster = offset + SWAPFILE_CLUSTER;
303 else if (offset == last_in_cluster) {
304 spin_lock(&swap_lock);
305 offset -= SWAPFILE_CLUSTER - 1;
306 si->cluster_next = offset;
307 si->cluster_nr = SWAPFILE_CLUSTER - 1;
308 found_free_cluster = 1;
309 goto checks;
310 }
311 if (unlikely(--latency_ration < 0)) {
312 cond_resched();
313 latency_ration = LATENCY_LIMIT;
314 }
315 }
316
317 offset = scan_base;
Hugh Dickins5d337b92005-09-03 15:54:41 -0700318 spin_lock(&swap_lock);
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800319 si->cluster_nr = SWAPFILE_CLUSTER - 1;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800320 si->lowest_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700322
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800323checks:
324 if (!(si->flags & SWP_WRITEOK))
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700325 goto no_page;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700326 if (!si->highest_bit)
327 goto no_page;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800328 if (offset > si->highest_bit)
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800329 scan_base = offset = si->lowest_bit;
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700330
331 /* reuse swap entry of cache-only swap if not busy. */
332 if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
333 int swap_was_freed;
334 spin_unlock(&swap_lock);
335 swap_was_freed = __try_to_reclaim_swap(si, offset);
336 spin_lock(&swap_lock);
337 /* entry was freed successfully, try to use this again */
338 if (swap_was_freed)
339 goto checks;
340 goto scan; /* check next one */
341 }
342
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800343 if (si->swap_map[offset])
344 goto scan;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700345
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800346 if (offset == si->lowest_bit)
347 si->lowest_bit++;
348 if (offset == si->highest_bit)
349 si->highest_bit--;
350 si->inuse_pages++;
351 if (si->inuse_pages == si->pages) {
352 si->lowest_bit = si->max;
353 si->highest_bit = 0;
354 }
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700355 if (cache == SWAP_CACHE) /* at usual swap-out via vmscan.c */
356 si->swap_map[offset] = encode_swapmap(0, true);
357 else /* at suspend */
358 si->swap_map[offset] = encode_swapmap(1, false);
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800359 si->cluster_next = offset + 1;
360 si->flags -= SWP_SCANNING;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800361
362 if (si->lowest_alloc) {
363 /*
364 * Only set when SWP_DISCARDABLE, and there's a scan
365 * for a free cluster in progress or just completed.
366 */
367 if (found_free_cluster) {
368 /*
369 * To optimize wear-levelling, discard the
370 * old data of the cluster, taking care not to
371 * discard any of its pages that have already
372 * been allocated by racing tasks (offset has
373 * already stepped over any at the beginning).
374 */
375 if (offset < si->highest_alloc &&
376 si->lowest_alloc <= last_in_cluster)
377 last_in_cluster = si->lowest_alloc - 1;
378 si->flags |= SWP_DISCARDING;
379 spin_unlock(&swap_lock);
380
381 if (offset < last_in_cluster)
382 discard_swap_cluster(si, offset,
383 last_in_cluster - offset + 1);
384
385 spin_lock(&swap_lock);
386 si->lowest_alloc = 0;
387 si->flags &= ~SWP_DISCARDING;
388
389 smp_mb(); /* wake_up_bit advises this */
390 wake_up_bit(&si->flags, ilog2(SWP_DISCARDING));
391
392 } else if (si->flags & SWP_DISCARDING) {
393 /*
394 * Delay using pages allocated by racing tasks
395 * until the whole discard has been issued. We
396 * could defer that delay until swap_writepage,
397 * but it's easier to keep this self-contained.
398 */
399 spin_unlock(&swap_lock);
400 wait_on_bit(&si->flags, ilog2(SWP_DISCARDING),
401 wait_for_discard, TASK_UNINTERRUPTIBLE);
402 spin_lock(&swap_lock);
403 } else {
404 /*
405 * Note pages allocated by racing tasks while
406 * scan for a free cluster is in progress, so
407 * that its final discard can exclude them.
408 */
409 if (offset < si->lowest_alloc)
410 si->lowest_alloc = offset;
411 if (offset > si->highest_alloc)
412 si->highest_alloc = offset;
413 }
414 }
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800415 return offset;
416
417scan:
Hugh Dickins5d337b92005-09-03 15:54:41 -0700418 spin_unlock(&swap_lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700419 while (++offset <= si->highest_bit) {
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700420 if (!si->swap_map[offset]) {
Hugh Dickins5d337b92005-09-03 15:54:41 -0700421 spin_lock(&swap_lock);
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700422 goto checks;
423 }
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700424 if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
425 spin_lock(&swap_lock);
426 goto checks;
427 }
Hugh Dickins048c27f2005-09-03 15:54:40 -0700428 if (unlikely(--latency_ration < 0)) {
429 cond_resched();
430 latency_ration = LATENCY_LIMIT;
431 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700432 }
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800433 offset = si->lowest_bit;
434 while (++offset < scan_base) {
435 if (!si->swap_map[offset]) {
436 spin_lock(&swap_lock);
437 goto checks;
438 }
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700439 if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
440 spin_lock(&swap_lock);
441 goto checks;
442 }
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800443 if (unlikely(--latency_ration < 0)) {
444 cond_resched();
445 latency_ration = LATENCY_LIMIT;
446 }
447 }
Hugh Dickins5d337b92005-09-03 15:54:41 -0700448 spin_lock(&swap_lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700449
450no_page:
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700451 si->flags -= SWP_SCANNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return 0;
453}
454
455swp_entry_t get_swap_page(void)
456{
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700457 struct swap_info_struct *si;
458 pgoff_t offset;
459 int type, next;
460 int wrapped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Hugh Dickins5d337b92005-09-03 15:54:41 -0700462 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (nr_swap_pages <= 0)
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700464 goto noswap;
465 nr_swap_pages--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700467 for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) {
468 si = swap_info + type;
469 next = si->next;
470 if (next < 0 ||
471 (!wrapped && si->prio != swap_info[next].prio)) {
472 next = swap_list.head;
473 wrapped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700475
476 if (!si->highest_bit)
477 continue;
478 if (!(si->flags & SWP_WRITEOK))
479 continue;
480
481 swap_list.next = next;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700482 /* This is called for allocating swap entry for cache */
483 offset = scan_swap_map(si, SWAP_CACHE);
Hugh Dickins5d337b92005-09-03 15:54:41 -0700484 if (offset) {
485 spin_unlock(&swap_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700486 return swp_entry(type, offset);
Hugh Dickins5d337b92005-09-03 15:54:41 -0700487 }
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700488 next = swap_list.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700490
491 nr_swap_pages++;
492noswap:
Hugh Dickins5d337b92005-09-03 15:54:41 -0700493 spin_unlock(&swap_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700494 return (swp_entry_t) {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700497/* The only caller of this function is now susupend routine */
Rafael J. Wysocki3a291a22006-01-06 00:16:37 -0800498swp_entry_t get_swap_page_of_type(int type)
499{
500 struct swap_info_struct *si;
501 pgoff_t offset;
502
503 spin_lock(&swap_lock);
504 si = swap_info + type;
505 if (si->flags & SWP_WRITEOK) {
506 nr_swap_pages--;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700507 /* This is called for allocating swap entry, not cache */
508 offset = scan_swap_map(si, SWAP_MAP);
Rafael J. Wysocki3a291a22006-01-06 00:16:37 -0800509 if (offset) {
510 spin_unlock(&swap_lock);
511 return swp_entry(type, offset);
512 }
513 nr_swap_pages++;
514 }
515 spin_unlock(&swap_lock);
516 return (swp_entry_t) {0};
517}
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519static struct swap_info_struct * swap_info_get(swp_entry_t entry)
520{
521 struct swap_info_struct * p;
522 unsigned long offset, type;
523
524 if (!entry.val)
525 goto out;
526 type = swp_type(entry);
527 if (type >= nr_swapfiles)
528 goto bad_nofile;
529 p = & swap_info[type];
530 if (!(p->flags & SWP_USED))
531 goto bad_device;
532 offset = swp_offset(entry);
533 if (offset >= p->max)
534 goto bad_offset;
535 if (!p->swap_map[offset])
536 goto bad_free;
Hugh Dickins5d337b92005-09-03 15:54:41 -0700537 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return p;
539
540bad_free:
541 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
542 goto out;
543bad_offset:
544 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
545 goto out;
546bad_device:
547 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
548 goto out;
549bad_nofile:
550 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
551out:
552 return NULL;
Hugh Dickins886bb7e2009-01-06 14:39:48 -0800553}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700555static int swap_entry_free(struct swap_info_struct *p,
556 swp_entry_t ent, int cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800558 unsigned long offset = swp_offset(ent);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700559 int count = swap_count(p->swap_map[offset]);
560 bool has_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700562 has_cache = swap_has_cache(p->swap_map[offset]);
563
564 if (cache == SWAP_MAP) { /* dropping usage count of swap */
565 if (count < SWAP_MAP_MAX) {
566 count--;
567 p->swap_map[offset] = encode_swapmap(count, has_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700569 } else { /* dropping swap cache flag */
570 VM_BUG_ON(!has_cache);
571 p->swap_map[offset] = encode_swapmap(count, false);
572
573 }
574 /* return code. */
575 count = p->swap_map[offset];
576 /* free if no reference */
577 if (!count) {
578 if (offset < p->lowest_bit)
579 p->lowest_bit = offset;
580 if (offset > p->highest_bit)
581 p->highest_bit = offset;
582 if (p->prio > swap_info[swap_list.next].prio)
583 swap_list.next = p - swap_info;
584 nr_swap_pages++;
585 p->inuse_pages--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700587 if (!swap_count(count))
588 mem_cgroup_uncharge_swap(ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return count;
590}
591
592/*
593 * Caller has made sure that the swapdevice corresponding to entry
594 * is still around or has not been recycled.
595 */
596void swap_free(swp_entry_t entry)
597{
598 struct swap_info_struct * p;
599
600 p = swap_info_get(entry);
601 if (p) {
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700602 swap_entry_free(p, entry, SWAP_MAP);
Hugh Dickins5d337b92005-09-03 15:54:41 -0700603 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605}
606
607/*
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700608 * Called after dropping swapcache to decrease refcnt to swap entries.
609 */
610void swapcache_free(swp_entry_t entry, struct page *page)
611{
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700612 struct swap_info_struct *p;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700613 int ret;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700614
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700615 p = swap_info_get(entry);
616 if (p) {
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700617 ret = swap_entry_free(p, entry, SWAP_CACHE);
618 if (page) {
619 bool swapout;
620 if (ret)
621 swapout = true; /* the end of swap out */
622 else
623 swapout = false; /* no more swap users! */
624 mem_cgroup_uncharge_swapcache(page, entry, swapout);
625 }
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700626 spin_unlock(&swap_lock);
627 }
628 return;
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700629}
630
631/*
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700632 * How many references to page are currently swapped out?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700634static inline int page_swapcount(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700636 int count = 0;
637 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 swp_entry_t entry;
639
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700640 entry.val = page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 p = swap_info_get(entry);
642 if (p) {
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700643 count = swap_count(p->swap_map[swp_offset(entry)]);
Hugh Dickins5d337b92005-09-03 15:54:41 -0700644 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700646 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649/*
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800650 * We can write to an anon page without COW if there are no other references
651 * to it. And as a side-effect, free up its swap: because the old content
652 * on disk will never be read, and seeking back there to write new content
653 * later would only waste time away from clustering.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 */
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800655int reuse_swap_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700657 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Hugh Dickins51726b12009-01-06 14:39:25 -0800659 VM_BUG_ON(!PageLocked(page));
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700660 count = page_mapcount(page);
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800661 if (count <= 1 && PageSwapCache(page)) {
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700662 count += page_swapcount(page);
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800663 if (count == 1 && !PageWriteback(page)) {
664 delete_from_swap_cache(page);
665 SetPageDirty(page);
666 }
667 }
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700668 return count == 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671/*
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800672 * If swap is getting full, or if there are no more mappings of this page,
673 * then try_to_free_swap is called to free its swap space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 */
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800675int try_to_free_swap(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Hugh Dickins51726b12009-01-06 14:39:25 -0800677 VM_BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 if (!PageSwapCache(page))
680 return 0;
681 if (PageWriteback(page))
682 return 0;
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800683 if (page_swapcount(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return 0;
685
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800686 delete_from_swap_cache(page);
687 SetPageDirty(page);
688 return 1;
Rik van Riel68a223942008-10-18 20:26:23 -0700689}
690
691/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 * Free the swap entry like above, but also try to
693 * free the page cache entry if it is the last user.
694 */
Hugh Dickins2509ef22009-01-06 14:40:10 -0800695int free_swap_and_cache(swp_entry_t entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Hugh Dickins2509ef22009-01-06 14:40:10 -0800697 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct page *page = NULL;
699
Christoph Lameter06972122006-06-23 02:03:35 -0700700 if (is_migration_entry(entry))
Hugh Dickins2509ef22009-01-06 14:40:10 -0800701 return 1;
Christoph Lameter06972122006-06-23 02:03:35 -0700702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 p = swap_info_get(entry);
704 if (p) {
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700705 if (swap_entry_free(p, entry, SWAP_MAP) == SWAP_HAS_CACHE) {
Nick Piggin93fac702006-03-31 02:29:56 -0800706 page = find_get_page(&swapper_space, entry.val);
Nick Piggin8413ac92008-10-18 20:26:59 -0700707 if (page && !trylock_page(page)) {
Nick Piggin93fac702006-03-31 02:29:56 -0800708 page_cache_release(page);
709 page = NULL;
710 }
711 }
Hugh Dickins5d337b92005-09-03 15:54:41 -0700712 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714 if (page) {
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800715 /*
716 * Not mapped elsewhere, or swap space full? Free it!
717 * Also recheck PageSwapCache now page is locked (above).
718 */
Nick Piggin93fac702006-03-31 02:29:56 -0800719 if (PageSwapCache(page) && !PageWriteback(page) &&
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800720 (!page_mapped(page) || vm_swap_full())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 delete_from_swap_cache(page);
722 SetPageDirty(page);
723 }
724 unlock_page(page);
725 page_cache_release(page);
726 }
Hugh Dickins2509ef22009-01-06 14:40:10 -0800727 return p != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200730#ifdef CONFIG_HIBERNATION
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800731/*
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800732 * Find the swap type that corresponds to given device (if any).
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800733 *
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800734 * @offset - number of the PAGE_SIZE-sized block of the device, starting
735 * from 0, in which the swap header is expected to be located.
736 *
737 * This is needed for the suspend to disk (aka swsusp).
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800738 */
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800739int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800740{
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800741 struct block_device *bdev = NULL;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800742 int i;
743
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800744 if (device)
745 bdev = bdget(device);
746
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800747 spin_lock(&swap_lock);
748 for (i = 0; i < nr_swapfiles; i++) {
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800749 struct swap_info_struct *sis = swap_info + i;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800750
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800751 if (!(sis->flags & SWP_WRITEOK))
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800752 continue;
Rafael J. Wysockib6b5bce2006-08-27 01:23:25 -0700753
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800754 if (!bdev) {
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800755 if (bdev_p)
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200756 *bdev_p = bdgrab(sis->bdev);
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800757
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800758 spin_unlock(&swap_lock);
759 return i;
760 }
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800761 if (bdev == sis->bdev) {
762 struct swap_extent *se;
763
764 se = list_entry(sis->extent_list.next,
765 struct swap_extent, list);
766 if (se->start_block == offset) {
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800767 if (bdev_p)
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200768 *bdev_p = bdgrab(sis->bdev);
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800769
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800770 spin_unlock(&swap_lock);
771 bdput(bdev);
772 return i;
773 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800774 }
775 }
776 spin_unlock(&swap_lock);
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800777 if (bdev)
778 bdput(bdev);
779
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800780 return -ENODEV;
781}
782
783/*
784 * Return either the total number of swap pages of given type, or the number
785 * of free pages of that type (depending on @free)
786 *
787 * This is needed for software suspend
788 */
789unsigned int count_swap_pages(int type, int free)
790{
791 unsigned int n = 0;
792
793 if (type < nr_swapfiles) {
794 spin_lock(&swap_lock);
795 if (swap_info[type].flags & SWP_WRITEOK) {
796 n = swap_info[type].pages;
797 if (free)
798 n -= swap_info[type].inuse_pages;
799 }
800 spin_unlock(&swap_lock);
801 }
802 return n;
803}
804#endif
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806/*
Hugh Dickins72866f62005-10-29 18:15:55 -0700807 * No need to decide whether this PTE shares the swap entry with others,
808 * just let do_wp_page work it out if a write is requested later - to
809 * force COW, vm_page_prot omits write permission from any private vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 */
Hugh Dickins044d66c2008-02-07 00:14:04 -0800811static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 unsigned long addr, swp_entry_t entry, struct page *page)
813{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800814 struct mem_cgroup *ptr = NULL;
Hugh Dickins044d66c2008-02-07 00:14:04 -0800815 spinlock_t *ptl;
816 pte_t *pte;
817 int ret = 1;
818
KAMEZAWA Hiroyuki85d9fc82009-01-29 14:25:13 -0800819 if (mem_cgroup_try_charge_swapin(vma->vm_mm, page, GFP_KERNEL, &ptr)) {
Hugh Dickins044d66c2008-02-07 00:14:04 -0800820 ret = -ENOMEM;
KAMEZAWA Hiroyuki85d9fc82009-01-29 14:25:13 -0800821 goto out_nolock;
822 }
Hugh Dickins044d66c2008-02-07 00:14:04 -0800823
824 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
825 if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) {
826 if (ret > 0)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800827 mem_cgroup_cancel_charge_swapin(ptr);
Hugh Dickins044d66c2008-02-07 00:14:04 -0800828 ret = 0;
829 goto out;
830 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800831
Hugh Dickins42946212005-10-29 18:16:05 -0700832 inc_mm_counter(vma->vm_mm, anon_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 get_page(page);
834 set_pte_at(vma->vm_mm, addr, pte,
835 pte_mkold(mk_pte(page, vma->vm_page_prot)));
836 page_add_anon_rmap(page, vma, addr);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800837 mem_cgroup_commit_charge_swapin(page, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 swap_free(entry);
839 /*
840 * Move the page to the active list so it is not
841 * immediately swapped out again after swapon.
842 */
843 activate_page(page);
Hugh Dickins044d66c2008-02-07 00:14:04 -0800844out:
845 pte_unmap_unlock(pte, ptl);
KAMEZAWA Hiroyuki85d9fc82009-01-29 14:25:13 -0800846out_nolock:
Hugh Dickins044d66c2008-02-07 00:14:04 -0800847 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
850static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
851 unsigned long addr, unsigned long end,
852 swp_entry_t entry, struct page *page)
853{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 pte_t swp_pte = swp_entry_to_pte(entry);
Hugh Dickins705e87c2005-10-29 18:16:27 -0700855 pte_t *pte;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800856 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Hugh Dickins044d66c2008-02-07 00:14:04 -0800858 /*
859 * We don't actually need pte lock while scanning for swp_pte: since
860 * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
861 * page table while we're scanning; though it could get zapped, and on
862 * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
863 * of unmatched parts which look like swp_pte, so unuse_pte must
864 * recheck under pte lock. Scanning without pte lock lets it be
865 * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
866 */
867 pte = pte_offset_map(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 do {
869 /*
870 * swapoff spends a _lot_ of time in this loop!
871 * Test inline before going to call unuse_pte.
872 */
873 if (unlikely(pte_same(*pte, swp_pte))) {
Hugh Dickins044d66c2008-02-07 00:14:04 -0800874 pte_unmap(pte);
875 ret = unuse_pte(vma, pmd, addr, entry, page);
876 if (ret)
877 goto out;
878 pte = pte_offset_map(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
880 } while (pte++, addr += PAGE_SIZE, addr != end);
Hugh Dickins044d66c2008-02-07 00:14:04 -0800881 pte_unmap(pte - 1);
882out:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800883 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
887 unsigned long addr, unsigned long end,
888 swp_entry_t entry, struct page *page)
889{
890 pmd_t *pmd;
891 unsigned long next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800892 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 pmd = pmd_offset(pud, addr);
895 do {
896 next = pmd_addr_end(addr, end);
897 if (pmd_none_or_clear_bad(pmd))
898 continue;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800899 ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
900 if (ret)
901 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 } while (pmd++, addr = next, addr != end);
903 return 0;
904}
905
906static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
907 unsigned long addr, unsigned long end,
908 swp_entry_t entry, struct page *page)
909{
910 pud_t *pud;
911 unsigned long next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800912 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 pud = pud_offset(pgd, addr);
915 do {
916 next = pud_addr_end(addr, end);
917 if (pud_none_or_clear_bad(pud))
918 continue;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800919 ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
920 if (ret)
921 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 } while (pud++, addr = next, addr != end);
923 return 0;
924}
925
926static int unuse_vma(struct vm_area_struct *vma,
927 swp_entry_t entry, struct page *page)
928{
929 pgd_t *pgd;
930 unsigned long addr, end, next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800931 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 if (page->mapping) {
934 addr = page_address_in_vma(page, vma);
935 if (addr == -EFAULT)
936 return 0;
937 else
938 end = addr + PAGE_SIZE;
939 } else {
940 addr = vma->vm_start;
941 end = vma->vm_end;
942 }
943
944 pgd = pgd_offset(vma->vm_mm, addr);
945 do {
946 next = pgd_addr_end(addr, end);
947 if (pgd_none_or_clear_bad(pgd))
948 continue;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800949 ret = unuse_pud_range(vma, pgd, addr, next, entry, page);
950 if (ret)
951 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 } while (pgd++, addr = next, addr != end);
953 return 0;
954}
955
956static int unuse_mm(struct mm_struct *mm,
957 swp_entry_t entry, struct page *page)
958{
959 struct vm_area_struct *vma;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800960 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 if (!down_read_trylock(&mm->mmap_sem)) {
963 /*
Fernando Luis Vazquez Cao7d034312008-07-29 22:33:41 -0700964 * Activate page so shrink_inactive_list is unlikely to unmap
965 * its ptes while lock is dropped, so swapoff can make progress.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 */
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700967 activate_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 unlock_page(page);
969 down_read(&mm->mmap_sem);
970 lock_page(page);
971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 for (vma = mm->mmap; vma; vma = vma->vm_next) {
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800973 if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 break;
975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 up_read(&mm->mmap_sem);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800977 return (ret < 0)? ret: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
980/*
981 * Scan swap_map from current position to next entry still in use.
982 * Recycle to start on reaching the end, returning 0 when empty.
983 */
Hugh Dickins6eb396d2005-09-03 15:54:35 -0700984static unsigned int find_next_to_unuse(struct swap_info_struct *si,
985 unsigned int prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Hugh Dickins6eb396d2005-09-03 15:54:35 -0700987 unsigned int max = si->max;
988 unsigned int i = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 int count;
990
991 /*
Hugh Dickins5d337b92005-09-03 15:54:41 -0700992 * No need for swap_lock here: we're just looking
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 * for whether an entry is in use, not modifying it; false
994 * hits are okay, and sys_swapoff() has already prevented new
Hugh Dickins5d337b92005-09-03 15:54:41 -0700995 * allocations from this area (while holding swap_lock).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 */
997 for (;;) {
998 if (++i >= max) {
999 if (!prev) {
1000 i = 0;
1001 break;
1002 }
1003 /*
1004 * No entries in use at top of swap_map,
1005 * loop back to start and recheck there.
1006 */
1007 max = prev + 1;
1008 prev = 0;
1009 i = 1;
1010 }
1011 count = si->swap_map[i];
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001012 if (count && swap_count(count) != SWAP_MAP_BAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 break;
1014 }
1015 return i;
1016}
1017
1018/*
1019 * We completely avoid races by reading each swap page in advance,
1020 * and then search for the process using it. All the necessary
1021 * page table adjustments can then be made atomically.
1022 */
1023static int try_to_unuse(unsigned int type)
1024{
1025 struct swap_info_struct * si = &swap_info[type];
1026 struct mm_struct *start_mm;
1027 unsigned short *swap_map;
1028 unsigned short swcount;
1029 struct page *page;
1030 swp_entry_t entry;
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001031 unsigned int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 int retval = 0;
1033 int reset_overflow = 0;
1034 int shmem;
1035
1036 /*
1037 * When searching mms for an entry, a good strategy is to
1038 * start at the first mm we freed the previous entry from
1039 * (though actually we don't notice whether we or coincidence
1040 * freed the entry). Initialize this start_mm with a hold.
1041 *
1042 * A simpler strategy would be to start at the last mm we
1043 * freed the previous entry from; but that would take less
1044 * advantage of mmlist ordering, which clusters forked mms
1045 * together, child after parent. If we race with dup_mmap(), we
1046 * prefer to resolve parent before child, lest we miss entries
1047 * duplicated after we scanned child: using last mm would invert
1048 * that. Though it's only a serious concern when an overflowed
1049 * swap count is reset from SWAP_MAP_MAX, preventing a rescan.
1050 */
1051 start_mm = &init_mm;
1052 atomic_inc(&init_mm.mm_users);
1053
1054 /*
1055 * Keep on scanning until all entries have gone. Usually,
1056 * one pass through swap_map is enough, but not necessarily:
1057 * there are races when an instance of an entry might be missed.
1058 */
1059 while ((i = find_next_to_unuse(si, i)) != 0) {
1060 if (signal_pending(current)) {
1061 retval = -EINTR;
1062 break;
1063 }
1064
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001065 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 * Get a page for the entry, using the existing swap
1067 * cache page if there is one. Otherwise, get a clean
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001068 * page and read the swap into it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
1070 swap_map = &si->swap_map[i];
1071 entry = swp_entry(type, i);
Hugh Dickins02098fe2008-02-04 22:28:42 -08001072 page = read_swap_cache_async(entry,
1073 GFP_HIGHUSER_MOVABLE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (!page) {
1075 /*
1076 * Either swap_duplicate() failed because entry
1077 * has been freed independently, and will not be
1078 * reused since sys_swapoff() already disabled
1079 * allocation from here, or alloc_page() failed.
1080 */
1081 if (!*swap_map)
1082 continue;
1083 retval = -ENOMEM;
1084 break;
1085 }
1086
1087 /*
1088 * Don't hold on to start_mm if it looks like exiting.
1089 */
1090 if (atomic_read(&start_mm->mm_users) == 1) {
1091 mmput(start_mm);
1092 start_mm = &init_mm;
1093 atomic_inc(&init_mm.mm_users);
1094 }
1095
1096 /*
1097 * Wait for and lock page. When do_swap_page races with
1098 * try_to_unuse, do_swap_page can handle the fault much
1099 * faster than try_to_unuse can locate the entry. This
1100 * apparently redundant "wait_on_page_locked" lets try_to_unuse
1101 * defer to do_swap_page in such a case - in some tests,
1102 * do_swap_page and try_to_unuse repeatedly compete.
1103 */
1104 wait_on_page_locked(page);
1105 wait_on_page_writeback(page);
1106 lock_page(page);
1107 wait_on_page_writeback(page);
1108
1109 /*
1110 * Remove all references to entry.
1111 * Whenever we reach init_mm, there's no address space
1112 * to search, but use it as a reminder to search shmem.
1113 */
1114 shmem = 0;
1115 swcount = *swap_map;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001116 if (swap_count(swcount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (start_mm == &init_mm)
1118 shmem = shmem_unuse(entry, page);
1119 else
1120 retval = unuse_mm(start_mm, entry, page);
1121 }
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001122 if (swap_count(*swap_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 int set_start_mm = (*swap_map >= swcount);
1124 struct list_head *p = &start_mm->mmlist;
1125 struct mm_struct *new_start_mm = start_mm;
1126 struct mm_struct *prev_mm = start_mm;
1127 struct mm_struct *mm;
1128
1129 atomic_inc(&new_start_mm->mm_users);
1130 atomic_inc(&prev_mm->mm_users);
1131 spin_lock(&mmlist_lock);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001132 while (swap_count(*swap_map) && !retval && !shmem &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 (p = p->next) != &start_mm->mmlist) {
1134 mm = list_entry(p, struct mm_struct, mmlist);
Hugh Dickins70af7c52006-06-23 02:03:44 -07001135 if (!atomic_inc_not_zero(&mm->mm_users))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 spin_unlock(&mmlist_lock);
1138 mmput(prev_mm);
1139 prev_mm = mm;
1140
1141 cond_resched();
1142
1143 swcount = *swap_map;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001144 if (!swap_count(swcount)) /* any usage ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 ;
1146 else if (mm == &init_mm) {
1147 set_start_mm = 1;
1148 shmem = shmem_unuse(entry, page);
1149 } else
1150 retval = unuse_mm(mm, entry, page);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001151
1152 if (set_start_mm &&
1153 swap_count(*swap_map) < swcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 mmput(new_start_mm);
1155 atomic_inc(&mm->mm_users);
1156 new_start_mm = mm;
1157 set_start_mm = 0;
1158 }
1159 spin_lock(&mmlist_lock);
1160 }
1161 spin_unlock(&mmlist_lock);
1162 mmput(prev_mm);
1163 mmput(start_mm);
1164 start_mm = new_start_mm;
1165 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001166 if (shmem) {
1167 /* page has already been unlocked and released */
1168 if (shmem > 0)
1169 continue;
1170 retval = shmem;
1171 break;
1172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (retval) {
1174 unlock_page(page);
1175 page_cache_release(page);
1176 break;
1177 }
1178
1179 /*
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001180 * How could swap count reach 0x7ffe ?
1181 * There's no way to repeat a swap page within an mm
1182 * (except in shmem, where it's the shared object which takes
1183 * the reference count)?
1184 * We believe SWAP_MAP_MAX cannot occur.(if occur, unsigned
1185 * short is too small....)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 * If that's wrong, then we should worry more about
1187 * exit_mmap() and do_munmap() cases described above:
1188 * we might be resetting SWAP_MAP_MAX too early here.
1189 * We know "Undead"s can happen, they're okay, so don't
1190 * report them; but do report if we reset SWAP_MAP_MAX.
1191 */
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001192 /* We might release the lock_page() in unuse_mm(). */
1193 if (!PageSwapCache(page) || page_private(page) != entry.val)
1194 goto retry;
1195
1196 if (swap_count(*swap_map) == SWAP_MAP_MAX) {
Hugh Dickins5d337b92005-09-03 15:54:41 -07001197 spin_lock(&swap_lock);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001198 *swap_map = encode_swapmap(0, true);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001199 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 reset_overflow = 1;
1201 }
1202
1203 /*
1204 * If a reference remains (rare), we would like to leave
1205 * the page in the swap cache; but try_to_unmap could
1206 * then re-duplicate the entry once we drop page lock,
1207 * so we might loop indefinitely; also, that page could
1208 * not be swapped out to other storage meanwhile. So:
1209 * delete from cache even if there's another reference,
1210 * after ensuring that the data has been saved to disk -
1211 * since if the reference remains (rarer), it will be
1212 * read from disk into another page. Splitting into two
1213 * pages would be incorrect if swap supported "shared
1214 * private" pages, but they are handled by tmpfs files.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 */
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001216 if (swap_count(*swap_map) &&
1217 PageDirty(page) && PageSwapCache(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 struct writeback_control wbc = {
1219 .sync_mode = WB_SYNC_NONE,
1220 };
1221
1222 swap_writepage(page, &wbc);
1223 lock_page(page);
1224 wait_on_page_writeback(page);
1225 }
Hugh Dickins68bdc8d2009-01-06 14:39:37 -08001226
1227 /*
1228 * It is conceivable that a racing task removed this page from
1229 * swap cache just before we acquired the page lock at the top,
1230 * or while we dropped it in unuse_mm(). The page might even
1231 * be back in swap cache on another swap area: that we must not
1232 * delete, since it may not have been written out to swap yet.
1233 */
1234 if (PageSwapCache(page) &&
1235 likely(page_private(page) == entry.val))
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001236 delete_from_swap_cache(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 /*
1239 * So we could skip searching mms once swap count went
1240 * to 1, we did not mark any present ptes as dirty: must
Anderson Briglia2706a1b2007-07-15 23:38:09 -07001241 * mark page dirty so shrink_page_list will preserve it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 */
1243 SetPageDirty(page);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001244retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 unlock_page(page);
1246 page_cache_release(page);
1247
1248 /*
1249 * Make sure that we aren't completely killing
1250 * interactive performance.
1251 */
1252 cond_resched();
1253 }
1254
1255 mmput(start_mm);
1256 if (reset_overflow) {
1257 printk(KERN_WARNING "swapoff: cleared swap entry overflow\n");
1258 swap_overflow = 0;
1259 }
1260 return retval;
1261}
1262
1263/*
Hugh Dickins5d337b92005-09-03 15:54:41 -07001264 * After a successful try_to_unuse, if no swap is now in use, we know
1265 * we can empty the mmlist. swap_lock must be held on entry and exit.
1266 * Note that mmlist_lock nests inside swap_lock, and an mm must be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 * added to the mmlist just after page_duplicate - before would be racy.
1268 */
1269static void drain_mmlist(void)
1270{
1271 struct list_head *p, *next;
1272 unsigned int i;
1273
1274 for (i = 0; i < nr_swapfiles; i++)
1275 if (swap_info[i].inuse_pages)
1276 return;
1277 spin_lock(&mmlist_lock);
1278 list_for_each_safe(p, next, &init_mm.mmlist)
1279 list_del_init(p);
1280 spin_unlock(&mmlist_lock);
1281}
1282
1283/*
1284 * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
1285 * corresponds to page offset `offset'.
1286 */
1287sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset)
1288{
1289 struct swap_extent *se = sis->curr_swap_extent;
1290 struct swap_extent *start_se = se;
1291
1292 for ( ; ; ) {
1293 struct list_head *lh;
1294
1295 if (se->start_page <= offset &&
1296 offset < (se->start_page + se->nr_pages)) {
1297 return se->start_block + (offset - se->start_page);
1298 }
Hugh Dickins11d31882005-09-03 15:54:34 -07001299 lh = se->list.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (lh == &sis->extent_list)
Hugh Dickins11d31882005-09-03 15:54:34 -07001301 lh = lh->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 se = list_entry(lh, struct swap_extent, list);
1303 sis->curr_swap_extent = se;
1304 BUG_ON(se == start_se); /* It *must* be present */
1305 }
1306}
1307
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +02001308#ifdef CONFIG_HIBERNATION
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -08001309/*
1310 * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
1311 * corresponding to given index in swap_info (swap type).
1312 */
1313sector_t swapdev_block(int swap_type, pgoff_t offset)
1314{
1315 struct swap_info_struct *sis;
1316
1317 if (swap_type >= nr_swapfiles)
1318 return 0;
1319
1320 sis = swap_info + swap_type;
1321 return (sis->flags & SWP_WRITEOK) ? map_swap_page(sis, offset) : 0;
1322}
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +02001323#endif /* CONFIG_HIBERNATION */
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -08001324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325/*
1326 * Free all of a swapdev's extent information
1327 */
1328static void destroy_swap_extents(struct swap_info_struct *sis)
1329{
1330 while (!list_empty(&sis->extent_list)) {
1331 struct swap_extent *se;
1332
1333 se = list_entry(sis->extent_list.next,
1334 struct swap_extent, list);
1335 list_del(&se->list);
1336 kfree(se);
1337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
1340/*
1341 * Add a block range (and the corresponding page range) into this swapdev's
Hugh Dickins11d31882005-09-03 15:54:34 -07001342 * extent list. The extent list is kept sorted in page order.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 *
Hugh Dickins11d31882005-09-03 15:54:34 -07001344 * This function rather assumes that it is called in ascending page order.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 */
1346static int
1347add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
1348 unsigned long nr_pages, sector_t start_block)
1349{
1350 struct swap_extent *se;
1351 struct swap_extent *new_se;
1352 struct list_head *lh;
1353
Hugh Dickins11d31882005-09-03 15:54:34 -07001354 lh = sis->extent_list.prev; /* The highest page extent */
1355 if (lh != &sis->extent_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 se = list_entry(lh, struct swap_extent, list);
Hugh Dickins11d31882005-09-03 15:54:34 -07001357 BUG_ON(se->start_page + se->nr_pages != start_page);
1358 if (se->start_block + se->nr_pages == start_block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /* Merge it */
1360 se->nr_pages += nr_pages;
1361 return 0;
1362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364
1365 /*
1366 * No merge. Insert a new extent, preserving ordering.
1367 */
1368 new_se = kmalloc(sizeof(*se), GFP_KERNEL);
1369 if (new_se == NULL)
1370 return -ENOMEM;
1371 new_se->start_page = start_page;
1372 new_se->nr_pages = nr_pages;
1373 new_se->start_block = start_block;
1374
Hugh Dickins11d31882005-09-03 15:54:34 -07001375 list_add_tail(&new_se->list, &sis->extent_list);
Hugh Dickins53092a72005-09-03 15:54:34 -07001376 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
1379/*
1380 * A `swap extent' is a simple thing which maps a contiguous range of pages
1381 * onto a contiguous range of disk blocks. An ordered list of swap extents
1382 * is built at swapon time and is then used at swap_writepage/swap_readpage
1383 * time for locating where on disk a page belongs.
1384 *
1385 * If the swapfile is an S_ISBLK block device, a single extent is installed.
1386 * This is done so that the main operating code can treat S_ISBLK and S_ISREG
1387 * swap files identically.
1388 *
1389 * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
1390 * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
1391 * swapfiles are handled *identically* after swapon time.
1392 *
1393 * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
1394 * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
1395 * some stray blocks are found which do not fall within the PAGE_SIZE alignment
1396 * requirements, they are simply tossed out - we will never use those blocks
1397 * for swapping.
1398 *
Hugh Dickinsb0d9bcd2005-09-03 15:54:31 -07001399 * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 * prevents root from shooting her foot off by ftruncating an in-use swapfile,
1401 * which will scribble on the fs.
1402 *
1403 * The amount of disk space which a single swap extent represents varies.
1404 * Typically it is in the 1-4 megabyte range. So we can have hundreds of
1405 * extents in the list. To avoid much list walking, we cache the previous
1406 * search location in `curr_swap_extent', and start new searches from there.
1407 * This is extremely effective. The average number of iterations in
1408 * map_swap_page() has been measured at about 0.3 per page. - akpm.
1409 */
Hugh Dickins53092a72005-09-03 15:54:34 -07001410static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
1412 struct inode *inode;
1413 unsigned blocks_per_page;
1414 unsigned long page_no;
1415 unsigned blkbits;
1416 sector_t probe_block;
1417 sector_t last_block;
Hugh Dickins53092a72005-09-03 15:54:34 -07001418 sector_t lowest_block = -1;
1419 sector_t highest_block = 0;
1420 int nr_extents = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 int ret;
1422
1423 inode = sis->swap_file->f_mapping->host;
1424 if (S_ISBLK(inode->i_mode)) {
1425 ret = add_swap_extent(sis, 0, sis->max, 0);
Hugh Dickins53092a72005-09-03 15:54:34 -07001426 *span = sis->pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 goto done;
1428 }
1429
1430 blkbits = inode->i_blkbits;
1431 blocks_per_page = PAGE_SIZE >> blkbits;
1432
1433 /*
1434 * Map all the blocks into the extent list. This code doesn't try
1435 * to be very smart.
1436 */
1437 probe_block = 0;
1438 page_no = 0;
1439 last_block = i_size_read(inode) >> blkbits;
1440 while ((probe_block + blocks_per_page) <= last_block &&
1441 page_no < sis->max) {
1442 unsigned block_in_page;
1443 sector_t first_block;
1444
1445 first_block = bmap(inode, probe_block);
1446 if (first_block == 0)
1447 goto bad_bmap;
1448
1449 /*
1450 * It must be PAGE_SIZE aligned on-disk
1451 */
1452 if (first_block & (blocks_per_page - 1)) {
1453 probe_block++;
1454 goto reprobe;
1455 }
1456
1457 for (block_in_page = 1; block_in_page < blocks_per_page;
1458 block_in_page++) {
1459 sector_t block;
1460
1461 block = bmap(inode, probe_block + block_in_page);
1462 if (block == 0)
1463 goto bad_bmap;
1464 if (block != first_block + block_in_page) {
1465 /* Discontiguity */
1466 probe_block++;
1467 goto reprobe;
1468 }
1469 }
1470
Hugh Dickins53092a72005-09-03 15:54:34 -07001471 first_block >>= (PAGE_SHIFT - blkbits);
1472 if (page_no) { /* exclude the header page */
1473 if (first_block < lowest_block)
1474 lowest_block = first_block;
1475 if (first_block > highest_block)
1476 highest_block = first_block;
1477 }
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 /*
1480 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
1481 */
Hugh Dickins53092a72005-09-03 15:54:34 -07001482 ret = add_swap_extent(sis, page_no, 1, first_block);
1483 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 goto out;
Hugh Dickins53092a72005-09-03 15:54:34 -07001485 nr_extents += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 page_no++;
1487 probe_block += blocks_per_page;
1488reprobe:
1489 continue;
1490 }
Hugh Dickins53092a72005-09-03 15:54:34 -07001491 ret = nr_extents;
1492 *span = 1 + highest_block - lowest_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (page_no == 0)
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001494 page_no = 1; /* force Empty message */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 sis->max = page_no;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001496 sis->pages = page_no - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 sis->highest_bit = page_no - 1;
1498done:
1499 sis->curr_swap_extent = list_entry(sis->extent_list.prev,
1500 struct swap_extent, list);
1501 goto out;
1502bad_bmap:
1503 printk(KERN_ERR "swapon: swapfile has holes\n");
1504 ret = -EINVAL;
1505out:
1506 return ret;
1507}
1508
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001509SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 struct swap_info_struct * p = NULL;
1512 unsigned short *swap_map;
1513 struct file *swap_file, *victim;
1514 struct address_space *mapping;
1515 struct inode *inode;
1516 char * pathname;
1517 int i, type, prev;
1518 int err;
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (!capable(CAP_SYS_ADMIN))
1521 return -EPERM;
1522
1523 pathname = getname(specialfile);
1524 err = PTR_ERR(pathname);
1525 if (IS_ERR(pathname))
1526 goto out;
1527
1528 victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
1529 putname(pathname);
1530 err = PTR_ERR(victim);
1531 if (IS_ERR(victim))
1532 goto out;
1533
1534 mapping = victim->f_mapping;
1535 prev = -1;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001536 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 for (type = swap_list.head; type >= 0; type = swap_info[type].next) {
1538 p = swap_info + type;
Hugh Dickins22c6f8f2009-01-06 14:39:48 -08001539 if (p->flags & SWP_WRITEOK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (p->swap_file->f_mapping == mapping)
1541 break;
1542 }
1543 prev = type;
1544 }
1545 if (type < 0) {
1546 err = -EINVAL;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001547 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 goto out_dput;
1549 }
1550 if (!security_vm_enough_memory(p->pages))
1551 vm_unacct_memory(p->pages);
1552 else {
1553 err = -ENOMEM;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001554 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 goto out_dput;
1556 }
1557 if (prev < 0) {
1558 swap_list.head = p->next;
1559 } else {
1560 swap_info[prev].next = p->next;
1561 }
1562 if (type == swap_list.next) {
1563 /* just pick something that's safe... */
1564 swap_list.next = swap_list.head;
1565 }
Hugh Dickins78ecba02008-07-23 21:28:23 -07001566 if (p->prio < 0) {
1567 for (i = p->next; i >= 0; i = swap_info[i].next)
1568 swap_info[i].prio = p->prio--;
1569 least_priority++;
1570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 nr_swap_pages -= p->pages;
1572 total_swap_pages -= p->pages;
1573 p->flags &= ~SWP_WRITEOK;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001574 spin_unlock(&swap_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -07001575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 current->flags |= PF_SWAPOFF;
1577 err = try_to_unuse(type);
1578 current->flags &= ~PF_SWAPOFF;
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (err) {
1581 /* re-insert swap space back into swap_list */
Hugh Dickins5d337b92005-09-03 15:54:41 -07001582 spin_lock(&swap_lock);
Hugh Dickins78ecba02008-07-23 21:28:23 -07001583 if (p->prio < 0)
1584 p->prio = --least_priority;
1585 prev = -1;
1586 for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (p->prio >= swap_info[i].prio)
1588 break;
Hugh Dickins78ecba02008-07-23 21:28:23 -07001589 prev = i;
1590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 p->next = i;
1592 if (prev < 0)
1593 swap_list.head = swap_list.next = p - swap_info;
1594 else
1595 swap_info[prev].next = p - swap_info;
1596 nr_swap_pages += p->pages;
1597 total_swap_pages += p->pages;
1598 p->flags |= SWP_WRITEOK;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001599 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 goto out_dput;
1601 }
Hugh Dickins52b7efdb2005-09-03 15:54:39 -07001602
1603 /* wait for any unplug function to finish */
1604 down_write(&swap_unplug_sem);
1605 up_write(&swap_unplug_sem);
1606
Hugh Dickins4cd3bb12005-09-03 15:54:33 -07001607 destroy_swap_extents(p);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001608 mutex_lock(&swapon_mutex);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001609 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 drain_mmlist();
Hugh Dickins5d337b92005-09-03 15:54:41 -07001611
1612 /* wait for anyone still in scan_swap_map */
1613 p->highest_bit = 0; /* cuts scans short */
1614 while (p->flags >= SWP_SCANNING) {
1615 spin_unlock(&swap_lock);
Nishanth Aravamudan13e4b572005-09-10 00:27:25 -07001616 schedule_timeout_uninterruptible(1);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001617 spin_lock(&swap_lock);
1618 }
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 swap_file = p->swap_file;
1621 p->swap_file = NULL;
1622 p->max = 0;
1623 swap_map = p->swap_map;
1624 p->swap_map = NULL;
1625 p->flags = 0;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001626 spin_unlock(&swap_lock);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001627 mutex_unlock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 vfree(swap_map);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08001629 /* Destroy swap account informatin */
1630 swap_cgroup_swapoff(type);
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 inode = mapping->host;
1633 if (S_ISBLK(inode->i_mode)) {
1634 struct block_device *bdev = I_BDEV(inode);
1635 set_blocksize(bdev, p->old_block_size);
1636 bd_release(bdev);
1637 } else {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001638 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 inode->i_flags &= ~S_SWAPFILE;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001640 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
1642 filp_close(swap_file, NULL);
1643 err = 0;
1644
1645out_dput:
1646 filp_close(victim, NULL);
1647out:
1648 return err;
1649}
1650
1651#ifdef CONFIG_PROC_FS
1652/* iterator */
1653static void *swap_start(struct seq_file *swap, loff_t *pos)
1654{
1655 struct swap_info_struct *ptr = swap_info;
1656 int i;
1657 loff_t l = *pos;
1658
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001659 mutex_lock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001661 if (!l)
1662 return SEQ_START_TOKEN;
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 for (i = 0; i < nr_swapfiles; i++, ptr++) {
1665 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1666 continue;
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001667 if (!--l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return ptr;
1669 }
1670
1671 return NULL;
1672}
1673
1674static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
1675{
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001676 struct swap_info_struct *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 struct swap_info_struct *endptr = swap_info + nr_swapfiles;
1678
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001679 if (v == SEQ_START_TOKEN)
1680 ptr = swap_info;
1681 else {
1682 ptr = v;
1683 ptr++;
1684 }
1685
1686 for (; ptr < endptr; ptr++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1688 continue;
1689 ++*pos;
1690 return ptr;
1691 }
1692
1693 return NULL;
1694}
1695
1696static void swap_stop(struct seq_file *swap, void *v)
1697{
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001698 mutex_unlock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
1701static int swap_show(struct seq_file *swap, void *v)
1702{
1703 struct swap_info_struct *ptr = v;
1704 struct file *file;
1705 int len;
1706
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001707 if (ptr == SEQ_START_TOKEN) {
1708 seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1709 return 0;
1710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 file = ptr->swap_file;
Jan Blunckc32c2f62008-02-14 19:38:43 -08001713 len = seq_path(swap, &file->f_path, " \t\n\\");
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001714 seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001715 len < 40 ? 40 - len : 1, " ",
1716 S_ISBLK(file->f_path.dentry->d_inode->i_mode) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 "partition" : "file\t",
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001718 ptr->pages << (PAGE_SHIFT - 10),
1719 ptr->inuse_pages << (PAGE_SHIFT - 10),
1720 ptr->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return 0;
1722}
1723
Helge Deller15ad7cd2006-12-06 20:40:36 -08001724static const struct seq_operations swaps_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 .start = swap_start,
1726 .next = swap_next,
1727 .stop = swap_stop,
1728 .show = swap_show
1729};
1730
1731static int swaps_open(struct inode *inode, struct file *file)
1732{
1733 return seq_open(file, &swaps_op);
1734}
1735
Helge Deller15ad7cd2006-12-06 20:40:36 -08001736static const struct file_operations proc_swaps_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 .open = swaps_open,
1738 .read = seq_read,
1739 .llseek = seq_lseek,
1740 .release = seq_release,
1741};
1742
1743static int __init procswaps_init(void)
1744{
Denis V. Lunev3d71f862008-04-29 01:02:13 -07001745 proc_create("swaps", 0, NULL, &proc_swaps_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 return 0;
1747}
1748__initcall(procswaps_init);
1749#endif /* CONFIG_PROC_FS */
1750
Jan Beulich17963162008-12-16 11:35:24 +00001751#ifdef MAX_SWAPFILES_CHECK
1752static int __init max_swapfiles_check(void)
1753{
1754 MAX_SWAPFILES_CHECK();
1755 return 0;
1756}
1757late_initcall(max_swapfiles_check);
1758#endif
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760/*
1761 * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
1762 *
1763 * The swapon system call
1764 */
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001765SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
1767 struct swap_info_struct * p;
1768 char *name = NULL;
1769 struct block_device *bdev = NULL;
1770 struct file *swap_file = NULL;
1771 struct address_space *mapping;
1772 unsigned int type;
1773 int i, prev;
1774 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 union swap_header *swap_header = NULL;
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001776 unsigned int nr_good_pages = 0;
1777 int nr_extents = 0;
Hugh Dickins53092a72005-09-03 15:54:34 -07001778 sector_t span;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 unsigned long maxpages = 1;
Hugh Dickins73fd8742009-01-06 14:39:47 -08001780 unsigned long swapfilepages;
Hugh Dickins78ecba02008-07-23 21:28:23 -07001781 unsigned short *swap_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 struct page *page = NULL;
1783 struct inode *inode = NULL;
1784 int did_down = 0;
1785
1786 if (!capable(CAP_SYS_ADMIN))
1787 return -EPERM;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001788 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 p = swap_info;
1790 for (type = 0 ; type < nr_swapfiles ; type++,p++)
1791 if (!(p->flags & SWP_USED))
1792 break;
1793 error = -EPERM;
Christoph Lameter06972122006-06-23 02:03:35 -07001794 if (type >= MAX_SWAPFILES) {
Hugh Dickins5d337b92005-09-03 15:54:41 -07001795 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 goto out;
1797 }
1798 if (type >= nr_swapfiles)
1799 nr_swapfiles = type+1;
Hugh Dickins78ecba02008-07-23 21:28:23 -07001800 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 INIT_LIST_HEAD(&p->extent_list);
1802 p->flags = SWP_USED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 p->next = -1;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001804 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 name = getname(specialfile);
1806 error = PTR_ERR(name);
1807 if (IS_ERR(name)) {
1808 name = NULL;
1809 goto bad_swap_2;
1810 }
1811 swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
1812 error = PTR_ERR(swap_file);
1813 if (IS_ERR(swap_file)) {
1814 swap_file = NULL;
1815 goto bad_swap_2;
1816 }
1817
1818 p->swap_file = swap_file;
1819 mapping = swap_file->f_mapping;
1820 inode = mapping->host;
1821
1822 error = -EBUSY;
1823 for (i = 0; i < nr_swapfiles; i++) {
1824 struct swap_info_struct *q = &swap_info[i];
1825
1826 if (i == type || !q->swap_file)
1827 continue;
1828 if (mapping == q->swap_file->f_mapping)
1829 goto bad_swap;
1830 }
1831
1832 error = -EINVAL;
1833 if (S_ISBLK(inode->i_mode)) {
1834 bdev = I_BDEV(inode);
1835 error = bd_claim(bdev, sys_swapon);
1836 if (error < 0) {
1837 bdev = NULL;
Rob Landleyf7b3a432005-09-22 21:44:27 -07001838 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 goto bad_swap;
1840 }
1841 p->old_block_size = block_size(bdev);
1842 error = set_blocksize(bdev, PAGE_SIZE);
1843 if (error < 0)
1844 goto bad_swap;
1845 p->bdev = bdev;
1846 } else if (S_ISREG(inode->i_mode)) {
1847 p->bdev = inode->i_sb->s_bdev;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001848 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 did_down = 1;
1850 if (IS_SWAPFILE(inode)) {
1851 error = -EBUSY;
1852 goto bad_swap;
1853 }
1854 } else {
1855 goto bad_swap;
1856 }
1857
Hugh Dickins73fd8742009-01-06 14:39:47 -08001858 swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860 /*
1861 * Read the swap header.
1862 */
1863 if (!mapping->a_ops->readpage) {
1864 error = -EINVAL;
1865 goto bad_swap;
1866 }
Pekka Enberg090d2b12006-06-23 02:05:08 -07001867 page = read_mapping_page(mapping, 0, swap_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 if (IS_ERR(page)) {
1869 error = PTR_ERR(page);
1870 goto bad_swap;
1871 }
Hugh Dickins81e33972009-01-06 14:39:49 -08001872 swap_header = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Hugh Dickins81e33972009-01-06 14:39:49 -08001874 if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
Jesper Juhle97a3112006-01-11 01:50:28 +01001875 printk(KERN_ERR "Unable to find swap-space signature\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 error = -EINVAL;
1877 goto bad_swap;
1878 }
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001879
Hugh Dickins81e33972009-01-06 14:39:49 -08001880 /* swap partition endianess hack... */
1881 if (swab32(swap_header->info.version) == 1) {
1882 swab32s(&swap_header->info.version);
1883 swab32s(&swap_header->info.last_page);
1884 swab32s(&swap_header->info.nr_badpages);
1885 for (i = 0; i < swap_header->info.nr_badpages; i++)
1886 swab32s(&swap_header->info.badpages[i]);
1887 }
1888 /* Check the swap header's sub-version */
1889 if (swap_header->info.version != 1) {
1890 printk(KERN_WARNING
1891 "Unable to handle swap header version %d\n",
1892 swap_header->info.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 error = -EINVAL;
1894 goto bad_swap;
Hugh Dickins81e33972009-01-06 14:39:49 -08001895 }
1896
1897 p->lowest_bit = 1;
1898 p->cluster_next = 1;
1899
1900 /*
1901 * Find out how many pages are allowed for a single swap
1902 * device. There are two limiting factors: 1) the number of
1903 * bits for the swap offset in the swp_entry_t type and
1904 * 2) the number of bits in the a swap pte as defined by
1905 * the different architectures. In order to find the
1906 * largest possible bit mask a swap entry with swap type 0
1907 * and swap offset ~0UL is created, encoded to a swap pte,
1908 * decoded to a swp_entry_t again and finally the swap
1909 * offset is extracted. This will mask all the bits from
1910 * the initial ~0UL mask that can't be encoded in either
1911 * the swp_entry_t or the architecture definition of a
1912 * swap pte.
1913 */
1914 maxpages = swp_offset(pte_to_swp_entry(
1915 swp_entry_to_pte(swp_entry(0, ~0UL)))) - 1;
1916 if (maxpages > swap_header->info.last_page)
1917 maxpages = swap_header->info.last_page;
1918 p->highest_bit = maxpages - 1;
1919
1920 error = -EINVAL;
1921 if (!maxpages)
1922 goto bad_swap;
1923 if (swapfilepages && maxpages > swapfilepages) {
1924 printk(KERN_WARNING
1925 "Swap area shorter than signature indicates\n");
1926 goto bad_swap;
1927 }
1928 if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
1929 goto bad_swap;
1930 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
1931 goto bad_swap;
1932
1933 /* OK, set up the swap map and apply the bad block list */
1934 swap_map = vmalloc(maxpages * sizeof(short));
1935 if (!swap_map) {
1936 error = -ENOMEM;
1937 goto bad_swap;
1938 }
1939
1940 memset(swap_map, 0, maxpages * sizeof(short));
1941 for (i = 0; i < swap_header->info.nr_badpages; i++) {
1942 int page_nr = swap_header->info.badpages[i];
1943 if (page_nr <= 0 || page_nr >= swap_header->info.last_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 error = -EINVAL;
1945 goto bad_swap;
1946 }
Hugh Dickins81e33972009-01-06 14:39:49 -08001947 swap_map[page_nr] = SWAP_MAP_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08001949
1950 error = swap_cgroup_swapon(type, maxpages);
1951 if (error)
1952 goto bad_swap;
1953
Hugh Dickins81e33972009-01-06 14:39:49 -08001954 nr_good_pages = swap_header->info.last_page -
1955 swap_header->info.nr_badpages -
1956 1 /* header page */;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001957
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001958 if (nr_good_pages) {
Hugh Dickins78ecba02008-07-23 21:28:23 -07001959 swap_map[0] = SWAP_MAP_BAD;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001960 p->max = maxpages;
1961 p->pages = nr_good_pages;
Hugh Dickins53092a72005-09-03 15:54:34 -07001962 nr_extents = setup_swap_extents(p, &span);
1963 if (nr_extents < 0) {
1964 error = nr_extents;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001965 goto bad_swap;
Hugh Dickins53092a72005-09-03 15:54:34 -07001966 }
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001967 nr_good_pages = p->pages;
1968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (!nr_good_pages) {
1970 printk(KERN_WARNING "Empty swap-file\n");
1971 error = -EINVAL;
1972 goto bad_swap;
1973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Hugh Dickins20137a42009-01-06 14:39:54 -08001975 if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
1976 p->flags |= SWP_SOLIDSTATE;
Hugh Dickins20137a42009-01-06 14:39:54 -08001977 p->cluster_next = 1 + (random32() % p->highest_bit);
1978 }
Hugh Dickins6a6ba832009-01-06 14:39:51 -08001979 if (discard_swap(p) == 0)
1980 p->flags |= SWP_DISCARDABLE;
1981
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001982 mutex_lock(&swapon_mutex);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001983 spin_lock(&swap_lock);
Hugh Dickins78ecba02008-07-23 21:28:23 -07001984 if (swap_flags & SWAP_FLAG_PREFER)
1985 p->prio =
1986 (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
1987 else
1988 p->prio = --least_priority;
1989 p->swap_map = swap_map;
Hugh Dickins22c6f8f2009-01-06 14:39:48 -08001990 p->flags |= SWP_WRITEOK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 nr_swap_pages += nr_good_pages;
1992 total_swap_pages += nr_good_pages;
Hugh Dickins53092a72005-09-03 15:54:34 -07001993
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001994 printk(KERN_INFO "Adding %uk swap on %s. "
Hugh Dickins20137a42009-01-06 14:39:54 -08001995 "Priority:%d extents:%d across:%lluk %s%s\n",
Hugh Dickins53092a72005-09-03 15:54:34 -07001996 nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
Hugh Dickins6a6ba832009-01-06 14:39:51 -08001997 nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
Hugh Dickins20137a42009-01-06 14:39:54 -08001998 (p->flags & SWP_SOLIDSTATE) ? "SS" : "",
1999 (p->flags & SWP_DISCARDABLE) ? "D" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 /* insert swap space into swap_list: */
2002 prev = -1;
2003 for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
2004 if (p->prio >= swap_info[i].prio) {
2005 break;
2006 }
2007 prev = i;
2008 }
2009 p->next = i;
2010 if (prev < 0) {
2011 swap_list.head = swap_list.next = p - swap_info;
2012 } else {
2013 swap_info[prev].next = p - swap_info;
2014 }
Hugh Dickins5d337b92005-09-03 15:54:41 -07002015 spin_unlock(&swap_lock);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002016 mutex_unlock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 error = 0;
2018 goto out;
2019bad_swap:
2020 if (bdev) {
2021 set_blocksize(bdev, p->old_block_size);
2022 bd_release(bdev);
2023 }
Hugh Dickins4cd3bb12005-09-03 15:54:33 -07002024 destroy_swap_extents(p);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08002025 swap_cgroup_swapoff(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026bad_swap_2:
Hugh Dickins5d337b92005-09-03 15:54:41 -07002027 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 p->swap_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 p->flags = 0;
Hugh Dickins5d337b92005-09-03 15:54:41 -07002030 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 vfree(swap_map);
2032 if (swap_file)
2033 filp_close(swap_file, NULL);
2034out:
2035 if (page && !IS_ERR(page)) {
2036 kunmap(page);
2037 page_cache_release(page);
2038 }
2039 if (name)
2040 putname(name);
2041 if (did_down) {
2042 if (!error)
2043 inode->i_flags |= S_SWAPFILE;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002044 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 }
2046 return error;
2047}
2048
2049void si_swapinfo(struct sysinfo *val)
2050{
2051 unsigned int i;
2052 unsigned long nr_to_be_unused = 0;
2053
Hugh Dickins5d337b92005-09-03 15:54:41 -07002054 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 for (i = 0; i < nr_swapfiles; i++) {
2056 if (!(swap_info[i].flags & SWP_USED) ||
2057 (swap_info[i].flags & SWP_WRITEOK))
2058 continue;
2059 nr_to_be_unused += swap_info[i].inuse_pages;
2060 }
2061 val->freeswap = nr_swap_pages + nr_to_be_unused;
2062 val->totalswap = total_swap_pages + nr_to_be_unused;
Hugh Dickins5d337b92005-09-03 15:54:41 -07002063 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
2066/*
2067 * Verify that a swap entry is valid and increment its swap map count.
2068 *
2069 * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
2070 * "permanent", but will be reclaimed by the next swapoff.
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002071 * Returns error code in following case.
2072 * - success -> 0
2073 * - swp_entry is invalid -> EINVAL
2074 * - swp_entry is migration entry -> EINVAL
2075 * - swap-cache reference is requested but there is already one. -> EEXIST
2076 * - swap-cache reference is requested but the entry is not used. -> ENOENT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 */
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002078static int __swap_duplicate(swp_entry_t entry, bool cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
2080 struct swap_info_struct * p;
2081 unsigned long offset, type;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002082 int result = -EINVAL;
2083 int count;
2084 bool has_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Christoph Lameter06972122006-06-23 02:03:35 -07002086 if (is_migration_entry(entry))
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002087 return -EINVAL;
Christoph Lameter06972122006-06-23 02:03:35 -07002088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 type = swp_type(entry);
2090 if (type >= nr_swapfiles)
2091 goto bad_file;
2092 p = type + swap_info;
2093 offset = swp_offset(entry);
2094
Hugh Dickins5d337b92005-09-03 15:54:41 -07002095 spin_lock(&swap_lock);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002096
2097 if (unlikely(offset >= p->max))
2098 goto unlock_out;
2099
2100 count = swap_count(p->swap_map[offset]);
2101 has_cache = swap_has_cache(p->swap_map[offset]);
2102
2103 if (cache == SWAP_CACHE) { /* called for swapcache/swapin-readahead */
2104
2105 /* set SWAP_HAS_CACHE if there is no cache and entry is used */
2106 if (!has_cache && count) {
2107 p->swap_map[offset] = encode_swapmap(count, true);
2108 result = 0;
2109 } else if (has_cache) /* someone added cache */
2110 result = -EEXIST;
2111 else if (!count) /* no users */
2112 result = -ENOENT;
2113
2114 } else if (count || has_cache) {
2115 if (count < SWAP_MAP_MAX - 1) {
2116 p->swap_map[offset] = encode_swapmap(count + 1,
2117 has_cache);
2118 result = 0;
2119 } else if (count <= SWAP_MAP_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (swap_overflow++ < 5)
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002121 printk(KERN_WARNING
2122 "swap_dup: swap entry overflow\n");
2123 p->swap_map[offset] = encode_swapmap(SWAP_MAP_MAX,
2124 has_cache);
2125 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 }
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002127 } else
2128 result = -ENOENT; /* unused swap entry */
2129unlock_out:
Hugh Dickins5d337b92005-09-03 15:54:41 -07002130 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131out:
2132 return result;
2133
2134bad_file:
2135 printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
2136 goto out;
2137}
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002138/*
2139 * increase reference count of swap entry by 1.
2140 */
2141void swap_duplicate(swp_entry_t entry)
2142{
2143 __swap_duplicate(entry, SWAP_MAP);
2144}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07002146/*
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002147 * @entry: swap entry for which we allocate swap cache.
2148 *
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07002149 * Called when allocating swap cache for exising swap entry,
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002150 * This can return error codes. Returns 0 at success.
2151 * -EBUSY means there is a swap cache.
2152 * Note: return code is different from swap_duplicate().
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07002153 */
2154int swapcache_prepare(swp_entry_t entry)
2155{
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002156 return __swap_duplicate(entry, SWAP_CACHE);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07002157}
2158
2159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160struct swap_info_struct *
2161get_swap_info_struct(unsigned type)
2162{
2163 return &swap_info[type];
2164}
2165
2166/*
Hugh Dickins5d337b92005-09-03 15:54:41 -07002167 * swap_lock prevents swap_map being freed. Don't grab an extra
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 * reference on the swaphandle, it doesn't matter if it becomes unused.
2169 */
2170int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
2171{
Hugh Dickins89528982008-02-04 22:28:45 -08002172 struct swap_info_struct *si;
Hugh Dickins3f9e7942006-09-29 02:01:26 -07002173 int our_page_cluster = page_cluster;
Hugh Dickins89528982008-02-04 22:28:45 -08002174 pgoff_t target, toff;
2175 pgoff_t base, end;
2176 int nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Hugh Dickins3f9e7942006-09-29 02:01:26 -07002178 if (!our_page_cluster) /* no readahead */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return 0;
Hugh Dickins89528982008-02-04 22:28:45 -08002180
2181 si = &swap_info[swp_type(entry)];
2182 target = swp_offset(entry);
2183 base = (target >> our_page_cluster) << our_page_cluster;
2184 end = base + (1 << our_page_cluster);
2185 if (!base) /* first page is swap header */
2186 base++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Hugh Dickins5d337b92005-09-03 15:54:41 -07002188 spin_lock(&swap_lock);
Hugh Dickins89528982008-02-04 22:28:45 -08002189 if (end > si->max) /* don't go beyond end of map */
2190 end = si->max;
2191
2192 /* Count contiguous allocated slots above our target */
2193 for (toff = target; ++toff < end; nr_pages++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 /* Don't read in free or bad pages */
Hugh Dickins89528982008-02-04 22:28:45 -08002195 if (!si->swap_map[toff])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 break;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002197 if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 break;
Hugh Dickins89528982008-02-04 22:28:45 -08002199 }
2200 /* Count contiguous allocated slots below our target */
2201 for (toff = target; --toff >= base; nr_pages++) {
2202 /* Don't read in free or bad pages */
2203 if (!si->swap_map[toff])
2204 break;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002205 if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
Hugh Dickins89528982008-02-04 22:28:45 -08002206 break;
2207 }
Hugh Dickins5d337b92005-09-03 15:54:41 -07002208 spin_unlock(&swap_lock);
Hugh Dickins89528982008-02-04 22:28:45 -08002209
2210 /*
2211 * Indicate starting offset, and return number of pages to get:
2212 * if only 1, say 0, since there's then no readahead to be done.
2213 */
2214 *offset = ++toff;
2215 return nr_pages? ++nr_pages: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}