swap_info: change to array of pointers
[deliverable/linux.git] / mm / swapfile.c
CommitLineData
1da177e4
LT
1/*
2 * linux/mm/swapfile.c
3 *
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 * Swap reorganised 29.12.95, Stephen Tweedie
6 */
7
1da177e4
LT
8#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>
20137a49 19#include <linux/random.h>
1da177e4
LT
20#include <linux/writeback.h>
21#include <linux/proc_fs.h>
22#include <linux/seq_file.h>
23#include <linux/init.h>
24#include <linux/module.h>
25#include <linux/rmap.h>
26#include <linux/security.h>
27#include <linux/backing-dev.h>
fc0abb14 28#include <linux/mutex.h>
c59ede7b 29#include <linux/capability.h>
1da177e4 30#include <linux/syscalls.h>
8a9f3ccd 31#include <linux/memcontrol.h>
1da177e4
LT
32
33#include <asm/pgtable.h>
34#include <asm/tlbflush.h>
35#include <linux/swapops.h>
27a7faa0 36#include <linux/page_cgroup.h>
1da177e4 37
7c363b8c
AB
38static DEFINE_SPINLOCK(swap_lock);
39static unsigned int nr_swapfiles;
b962716b 40long nr_swap_pages;
1da177e4
LT
41long total_swap_pages;
42static int swap_overflow;
78ecba08 43static int least_priority;
1da177e4 44
1da177e4
LT
45static 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
7c363b8c 50static struct swap_list_t swap_list = {-1, -1};
1da177e4 51
efa90a98 52static struct swap_info_struct *swap_info[MAX_SWAPFILES];
1da177e4 53
fc0abb14 54static DEFINE_MUTEX(swapon_mutex);
1da177e4 55
355cfa73
KH
56/* 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
efa90a98 82/* returns 1 if swap entry is freed */
c9e44410
KH
83static int
84__try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
85{
efa90a98 86 swp_entry_t entry = swp_entry(si->type, offset);
c9e44410
KH
87 struct page *page;
88 int ret = 0;
89
90 page = find_get_page(&swapper_space, entry.val);
91 if (!page)
92 return 0;
93 /*
94 * This function is called from scan_swap_map() and it's called
95 * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here.
96 * We have to use trylock for avoiding deadlock. This is a special
97 * case and you should use try_to_free_swap() with explicit lock_page()
98 * in usual operations.
99 */
100 if (trylock_page(page)) {
101 ret = try_to_free_swap(page);
102 unlock_page(page);
103 }
104 page_cache_release(page);
105 return ret;
106}
355cfa73 107
1da177e4
LT
108/*
109 * We need this because the bdev->unplug_fn can sleep and we cannot
5d337b91 110 * hold swap_lock while calling the unplug_fn. And swap_lock
fc0abb14 111 * cannot be turned into a mutex.
1da177e4
LT
112 */
113static DECLARE_RWSEM(swap_unplug_sem);
114
1da177e4
LT
115void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page)
116{
117 swp_entry_t entry;
118
119 down_read(&swap_unplug_sem);
4c21e2f2 120 entry.val = page_private(page);
1da177e4 121 if (PageSwapCache(page)) {
efa90a98 122 struct block_device *bdev = swap_info[swp_type(entry)]->bdev;
1da177e4
LT
123 struct backing_dev_info *bdi;
124
125 /*
126 * If the page is removed from swapcache from under us (with a
127 * racy try_to_unuse/swapoff) we need an additional reference
4c21e2f2
HD
128 * count to avoid reading garbage from page_private(page) above.
129 * If the WARN_ON triggers during a swapoff it maybe the race
1da177e4
LT
130 * condition and it's harmless. However if it triggers without
131 * swapoff it signals a problem.
132 */
133 WARN_ON(page_count(page) <= 1);
134
135 bdi = bdev->bd_inode->i_mapping->backing_dev_info;
ba32311e 136 blk_run_backing_dev(bdi, page);
1da177e4
LT
137 }
138 up_read(&swap_unplug_sem);
139}
140
6a6ba831
HD
141/*
142 * swapon tell device that all the old swap contents can be discarded,
143 * to allow the swap device to optimize its wear-levelling.
144 */
145static int discard_swap(struct swap_info_struct *si)
146{
147 struct swap_extent *se;
148 int err = 0;
149
150 list_for_each_entry(se, &si->extent_list, list) {
151 sector_t start_block = se->start_block << (PAGE_SHIFT - 9);
858a2990 152 sector_t nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
6a6ba831
HD
153
154 if (se->start_page == 0) {
155 /* Do not discard the swap header page! */
156 start_block += 1 << (PAGE_SHIFT - 9);
157 nr_blocks -= 1 << (PAGE_SHIFT - 9);
158 if (!nr_blocks)
159 continue;
160 }
161
162 err = blkdev_issue_discard(si->bdev, start_block,
746cd1e7
CH
163 nr_blocks, GFP_KERNEL,
164 DISCARD_FL_BARRIER);
6a6ba831
HD
165 if (err)
166 break;
167
168 cond_resched();
169 }
170 return err; /* That will often be -EOPNOTSUPP */
171}
172
7992fde7
HD
173/*
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;
858a2990 190 sector_t nr_blocks = se->nr_pages - offset;
7992fde7
HD
191
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,
746cd1e7
CH
203 nr_blocks, GFP_NOIO,
204 DISCARD_FL_BARRIER))
7992fde7
HD
205 break;
206 }
207
208 lh = se->list.next;
209 if (lh == &si->extent_list)
210 lh = lh->next;
211 se = list_entry(lh, struct swap_extent, list);
212 }
213}
214
215static int wait_for_discard(void *word)
216{
217 schedule();
218 return 0;
219}
220
048c27fd
HD
221#define SWAPFILE_CLUSTER 256
222#define LATENCY_LIMIT 256
223
355cfa73
KH
224static inline unsigned long scan_swap_map(struct swap_info_struct *si,
225 int cache)
1da177e4 226{
ebebbbe9 227 unsigned long offset;
c60aa176 228 unsigned long scan_base;
7992fde7 229 unsigned long last_in_cluster = 0;
048c27fd 230 int latency_ration = LATENCY_LIMIT;
7992fde7 231 int found_free_cluster = 0;
7dfad418 232
886bb7e9 233 /*
7dfad418
HD
234 * We try to cluster swap pages by allocating them sequentially
235 * in swap. Once we've allocated SWAPFILE_CLUSTER pages this
236 * way, however, we resort to first-free allocation, starting
237 * a new cluster. This prevents us from scattering swap pages
238 * all over the entire swap partition, so that we reduce
239 * overall disk seek times between swap pages. -- sct
240 * But we do now try to find an empty cluster. -Andrea
c60aa176 241 * And we let swap pages go all over an SSD partition. Hugh
7dfad418
HD
242 */
243
52b7efdb 244 si->flags += SWP_SCANNING;
c60aa176 245 scan_base = offset = si->cluster_next;
ebebbbe9
HD
246
247 if (unlikely(!si->cluster_nr--)) {
248 if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
249 si->cluster_nr = SWAPFILE_CLUSTER - 1;
250 goto checks;
251 }
7992fde7
HD
252 if (si->flags & SWP_DISCARDABLE) {
253 /*
254 * Start range check on racing allocations, in case
255 * they overlap the cluster we eventually decide on
256 * (we scan without swap_lock to allow preemption).
257 * It's hardly conceivable that cluster_nr could be
258 * wrapped during our scan, but don't depend on it.
259 */
260 if (si->lowest_alloc)
261 goto checks;
262 si->lowest_alloc = si->max;
263 si->highest_alloc = 0;
264 }
5d337b91 265 spin_unlock(&swap_lock);
7dfad418 266
c60aa176
HD
267 /*
268 * If seek is expensive, start searching for new cluster from
269 * start of partition, to minimize the span of allocated swap.
270 * But if seek is cheap, search from our current position, so
271 * that swap is allocated from all over the partition: if the
272 * Flash Translation Layer only remaps within limited zones,
273 * we don't want to wear out the first zone too quickly.
274 */
275 if (!(si->flags & SWP_SOLIDSTATE))
276 scan_base = offset = si->lowest_bit;
7dfad418
HD
277 last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
278
279 /* Locate the first empty (unaligned) cluster */
280 for (; last_in_cluster <= si->highest_bit; offset++) {
1da177e4 281 if (si->swap_map[offset])
7dfad418
HD
282 last_in_cluster = offset + SWAPFILE_CLUSTER;
283 else if (offset == last_in_cluster) {
5d337b91 284 spin_lock(&swap_lock);
ebebbbe9
HD
285 offset -= SWAPFILE_CLUSTER - 1;
286 si->cluster_next = offset;
287 si->cluster_nr = SWAPFILE_CLUSTER - 1;
7992fde7 288 found_free_cluster = 1;
ebebbbe9 289 goto checks;
1da177e4 290 }
048c27fd
HD
291 if (unlikely(--latency_ration < 0)) {
292 cond_resched();
293 latency_ration = LATENCY_LIMIT;
294 }
7dfad418 295 }
ebebbbe9
HD
296
297 offset = si->lowest_bit;
c60aa176
HD
298 last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
299
300 /* Locate the first empty (unaligned) cluster */
301 for (; last_in_cluster < scan_base; offset++) {
302 if (si->swap_map[offset])
303 last_in_cluster = offset + SWAPFILE_CLUSTER;
304 else if (offset == last_in_cluster) {
305 spin_lock(&swap_lock);
306 offset -= SWAPFILE_CLUSTER - 1;
307 si->cluster_next = offset;
308 si->cluster_nr = SWAPFILE_CLUSTER - 1;
309 found_free_cluster = 1;
310 goto checks;
311 }
312 if (unlikely(--latency_ration < 0)) {
313 cond_resched();
314 latency_ration = LATENCY_LIMIT;
315 }
316 }
317
318 offset = scan_base;
5d337b91 319 spin_lock(&swap_lock);
ebebbbe9 320 si->cluster_nr = SWAPFILE_CLUSTER - 1;
7992fde7 321 si->lowest_alloc = 0;
1da177e4 322 }
7dfad418 323
ebebbbe9
HD
324checks:
325 if (!(si->flags & SWP_WRITEOK))
52b7efdb 326 goto no_page;
7dfad418
HD
327 if (!si->highest_bit)
328 goto no_page;
ebebbbe9 329 if (offset > si->highest_bit)
c60aa176 330 scan_base = offset = si->lowest_bit;
c9e44410
KH
331
332 /* reuse swap entry of cache-only swap if not busy. */
333 if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
334 int swap_was_freed;
335 spin_unlock(&swap_lock);
336 swap_was_freed = __try_to_reclaim_swap(si, offset);
337 spin_lock(&swap_lock);
338 /* entry was freed successfully, try to use this again */
339 if (swap_was_freed)
340 goto checks;
341 goto scan; /* check next one */
342 }
343
ebebbbe9
HD
344 if (si->swap_map[offset])
345 goto scan;
346
347 if (offset == si->lowest_bit)
348 si->lowest_bit++;
349 if (offset == si->highest_bit)
350 si->highest_bit--;
351 si->inuse_pages++;
352 if (si->inuse_pages == si->pages) {
353 si->lowest_bit = si->max;
354 si->highest_bit = 0;
1da177e4 355 }
355cfa73
KH
356 if (cache == SWAP_CACHE) /* at usual swap-out via vmscan.c */
357 si->swap_map[offset] = encode_swapmap(0, true);
358 else /* at suspend */
359 si->swap_map[offset] = encode_swapmap(1, false);
ebebbbe9
HD
360 si->cluster_next = offset + 1;
361 si->flags -= SWP_SCANNING;
7992fde7
HD
362
363 if (si->lowest_alloc) {
364 /*
365 * Only set when SWP_DISCARDABLE, and there's a scan
366 * for a free cluster in progress or just completed.
367 */
368 if (found_free_cluster) {
369 /*
370 * To optimize wear-levelling, discard the
371 * old data of the cluster, taking care not to
372 * discard any of its pages that have already
373 * been allocated by racing tasks (offset has
374 * already stepped over any at the beginning).
375 */
376 if (offset < si->highest_alloc &&
377 si->lowest_alloc <= last_in_cluster)
378 last_in_cluster = si->lowest_alloc - 1;
379 si->flags |= SWP_DISCARDING;
380 spin_unlock(&swap_lock);
381
382 if (offset < last_in_cluster)
383 discard_swap_cluster(si, offset,
384 last_in_cluster - offset + 1);
385
386 spin_lock(&swap_lock);
387 si->lowest_alloc = 0;
388 si->flags &= ~SWP_DISCARDING;
389
390 smp_mb(); /* wake_up_bit advises this */
391 wake_up_bit(&si->flags, ilog2(SWP_DISCARDING));
392
393 } else if (si->flags & SWP_DISCARDING) {
394 /*
395 * Delay using pages allocated by racing tasks
396 * until the whole discard has been issued. We
397 * could defer that delay until swap_writepage,
398 * but it's easier to keep this self-contained.
399 */
400 spin_unlock(&swap_lock);
401 wait_on_bit(&si->flags, ilog2(SWP_DISCARDING),
402 wait_for_discard, TASK_UNINTERRUPTIBLE);
403 spin_lock(&swap_lock);
404 } else {
405 /*
406 * Note pages allocated by racing tasks while
407 * scan for a free cluster is in progress, so
408 * that its final discard can exclude them.
409 */
410 if (offset < si->lowest_alloc)
411 si->lowest_alloc = offset;
412 if (offset > si->highest_alloc)
413 si->highest_alloc = offset;
414 }
415 }
ebebbbe9 416 return offset;
7dfad418 417
ebebbbe9 418scan:
5d337b91 419 spin_unlock(&swap_lock);
7dfad418 420 while (++offset <= si->highest_bit) {
52b7efdb 421 if (!si->swap_map[offset]) {
5d337b91 422 spin_lock(&swap_lock);
52b7efdb
HD
423 goto checks;
424 }
c9e44410
KH
425 if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
426 spin_lock(&swap_lock);
427 goto checks;
428 }
048c27fd
HD
429 if (unlikely(--latency_ration < 0)) {
430 cond_resched();
431 latency_ration = LATENCY_LIMIT;
432 }
7dfad418 433 }
c60aa176
HD
434 offset = si->lowest_bit;
435 while (++offset < scan_base) {
436 if (!si->swap_map[offset]) {
437 spin_lock(&swap_lock);
438 goto checks;
439 }
c9e44410
KH
440 if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
441 spin_lock(&swap_lock);
442 goto checks;
443 }
c60aa176
HD
444 if (unlikely(--latency_ration < 0)) {
445 cond_resched();
446 latency_ration = LATENCY_LIMIT;
447 }
448 }
5d337b91 449 spin_lock(&swap_lock);
7dfad418
HD
450
451no_page:
52b7efdb 452 si->flags -= SWP_SCANNING;
1da177e4
LT
453 return 0;
454}
455
456swp_entry_t get_swap_page(void)
457{
fb4f88dc
HD
458 struct swap_info_struct *si;
459 pgoff_t offset;
460 int type, next;
461 int wrapped = 0;
1da177e4 462
5d337b91 463 spin_lock(&swap_lock);
1da177e4 464 if (nr_swap_pages <= 0)
fb4f88dc
HD
465 goto noswap;
466 nr_swap_pages--;
467
468 for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) {
efa90a98 469 si = swap_info[type];
fb4f88dc
HD
470 next = si->next;
471 if (next < 0 ||
efa90a98 472 (!wrapped && si->prio != swap_info[next]->prio)) {
fb4f88dc
HD
473 next = swap_list.head;
474 wrapped++;
1da177e4 475 }
fb4f88dc
HD
476
477 if (!si->highest_bit)
478 continue;
479 if (!(si->flags & SWP_WRITEOK))
480 continue;
481
482 swap_list.next = next;
355cfa73
KH
483 /* This is called for allocating swap entry for cache */
484 offset = scan_swap_map(si, SWAP_CACHE);
5d337b91
HD
485 if (offset) {
486 spin_unlock(&swap_lock);
fb4f88dc 487 return swp_entry(type, offset);
5d337b91 488 }
fb4f88dc 489 next = swap_list.next;
1da177e4 490 }
fb4f88dc
HD
491
492 nr_swap_pages++;
493noswap:
5d337b91 494 spin_unlock(&swap_lock);
fb4f88dc 495 return (swp_entry_t) {0};
1da177e4
LT
496}
497
355cfa73 498/* The only caller of this function is now susupend routine */
3a291a20
RW
499swp_entry_t get_swap_page_of_type(int type)
500{
501 struct swap_info_struct *si;
502 pgoff_t offset;
503
504 spin_lock(&swap_lock);
efa90a98
HD
505 si = swap_info[type];
506 if (si && (si->flags & SWP_WRITEOK)) {
3a291a20 507 nr_swap_pages--;
355cfa73
KH
508 /* This is called for allocating swap entry, not cache */
509 offset = scan_swap_map(si, SWAP_MAP);
3a291a20
RW
510 if (offset) {
511 spin_unlock(&swap_lock);
512 return swp_entry(type, offset);
513 }
514 nr_swap_pages++;
515 }
516 spin_unlock(&swap_lock);
517 return (swp_entry_t) {0};
518}
519
1da177e4
LT
520static struct swap_info_struct * swap_info_get(swp_entry_t entry)
521{
522 struct swap_info_struct * p;
523 unsigned long offset, type;
524
525 if (!entry.val)
526 goto out;
527 type = swp_type(entry);
528 if (type >= nr_swapfiles)
529 goto bad_nofile;
efa90a98 530 p = swap_info[type];
1da177e4
LT
531 if (!(p->flags & SWP_USED))
532 goto bad_device;
533 offset = swp_offset(entry);
534 if (offset >= p->max)
535 goto bad_offset;
536 if (!p->swap_map[offset])
537 goto bad_free;
5d337b91 538 spin_lock(&swap_lock);
1da177e4
LT
539 return p;
540
541bad_free:
542 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
543 goto out;
544bad_offset:
545 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
546 goto out;
547bad_device:
548 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
549 goto out;
550bad_nofile:
551 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
552out:
553 return NULL;
886bb7e9 554}
1da177e4 555
355cfa73
KH
556static int swap_entry_free(struct swap_info_struct *p,
557 swp_entry_t ent, int cache)
1da177e4 558{
8c7c6e34 559 unsigned long offset = swp_offset(ent);
355cfa73
KH
560 int count = swap_count(p->swap_map[offset]);
561 bool has_cache;
562
563 has_cache = swap_has_cache(p->swap_map[offset]);
564
565 if (cache == SWAP_MAP) { /* dropping usage count of swap */
566 if (count < SWAP_MAP_MAX) {
567 count--;
568 p->swap_map[offset] = encode_swapmap(count, has_cache);
1da177e4 569 }
355cfa73
KH
570 } else { /* dropping swap cache flag */
571 VM_BUG_ON(!has_cache);
572 p->swap_map[offset] = encode_swapmap(count, false);
573
574 }
575 /* return code. */
576 count = p->swap_map[offset];
577 /* free if no reference */
578 if (!count) {
579 if (offset < p->lowest_bit)
580 p->lowest_bit = offset;
581 if (offset > p->highest_bit)
582 p->highest_bit = offset;
efa90a98
HD
583 if (swap_list.next >= 0 &&
584 p->prio > swap_info[swap_list.next]->prio)
585 swap_list.next = p->type;
355cfa73
KH
586 nr_swap_pages++;
587 p->inuse_pages--;
1da177e4 588 }
8a9478ca
KH
589 if (!swap_count(count))
590 mem_cgroup_uncharge_swap(ent);
1da177e4
LT
591 return count;
592}
593
594/*
595 * Caller has made sure that the swapdevice corresponding to entry
596 * is still around or has not been recycled.
597 */
598void swap_free(swp_entry_t entry)
599{
600 struct swap_info_struct * p;
601
602 p = swap_info_get(entry);
603 if (p) {
355cfa73 604 swap_entry_free(p, entry, SWAP_MAP);
5d337b91 605 spin_unlock(&swap_lock);
1da177e4
LT
606 }
607}
608
cb4b86ba
KH
609/*
610 * Called after dropping swapcache to decrease refcnt to swap entries.
611 */
612void swapcache_free(swp_entry_t entry, struct page *page)
613{
355cfa73 614 struct swap_info_struct *p;
8a9478ca 615 int ret;
355cfa73 616
355cfa73
KH
617 p = swap_info_get(entry);
618 if (p) {
8a9478ca
KH
619 ret = swap_entry_free(p, entry, SWAP_CACHE);
620 if (page) {
621 bool swapout;
622 if (ret)
623 swapout = true; /* the end of swap out */
624 else
625 swapout = false; /* no more swap users! */
626 mem_cgroup_uncharge_swapcache(page, entry, swapout);
627 }
355cfa73
KH
628 spin_unlock(&swap_lock);
629 }
630 return;
cb4b86ba
KH
631}
632
1da177e4 633/*
c475a8ab 634 * How many references to page are currently swapped out?
1da177e4 635 */
c475a8ab 636static inline int page_swapcount(struct page *page)
1da177e4 637{
c475a8ab
HD
638 int count = 0;
639 struct swap_info_struct *p;
1da177e4
LT
640 swp_entry_t entry;
641
4c21e2f2 642 entry.val = page_private(page);
1da177e4
LT
643 p = swap_info_get(entry);
644 if (p) {
355cfa73 645 count = swap_count(p->swap_map[swp_offset(entry)]);
5d337b91 646 spin_unlock(&swap_lock);
1da177e4 647 }
c475a8ab 648 return count;
1da177e4
LT
649}
650
651/*
7b1fe597
HD
652 * We can write to an anon page without COW if there are no other references
653 * to it. And as a side-effect, free up its swap: because the old content
654 * on disk will never be read, and seeking back there to write new content
655 * later would only waste time away from clustering.
1da177e4 656 */
7b1fe597 657int reuse_swap_page(struct page *page)
1da177e4 658{
c475a8ab
HD
659 int count;
660
51726b12 661 VM_BUG_ON(!PageLocked(page));
c475a8ab 662 count = page_mapcount(page);
7b1fe597 663 if (count <= 1 && PageSwapCache(page)) {
c475a8ab 664 count += page_swapcount(page);
7b1fe597
HD
665 if (count == 1 && !PageWriteback(page)) {
666 delete_from_swap_cache(page);
667 SetPageDirty(page);
668 }
669 }
c475a8ab 670 return count == 1;
1da177e4
LT
671}
672
673/*
a2c43eed
HD
674 * If swap is getting full, or if there are no more mappings of this page,
675 * then try_to_free_swap is called to free its swap space.
1da177e4 676 */
a2c43eed 677int try_to_free_swap(struct page *page)
1da177e4 678{
51726b12 679 VM_BUG_ON(!PageLocked(page));
1da177e4
LT
680
681 if (!PageSwapCache(page))
682 return 0;
683 if (PageWriteback(page))
684 return 0;
a2c43eed 685 if (page_swapcount(page))
1da177e4
LT
686 return 0;
687
a2c43eed
HD
688 delete_from_swap_cache(page);
689 SetPageDirty(page);
690 return 1;
68a22394
RR
691}
692
1da177e4
LT
693/*
694 * Free the swap entry like above, but also try to
695 * free the page cache entry if it is the last user.
696 */
2509ef26 697int free_swap_and_cache(swp_entry_t entry)
1da177e4 698{
2509ef26 699 struct swap_info_struct *p;
1da177e4
LT
700 struct page *page = NULL;
701
a7420aa5 702 if (non_swap_entry(entry))
2509ef26 703 return 1;
0697212a 704
1da177e4
LT
705 p = swap_info_get(entry);
706 if (p) {
355cfa73 707 if (swap_entry_free(p, entry, SWAP_MAP) == SWAP_HAS_CACHE) {
93fac704 708 page = find_get_page(&swapper_space, entry.val);
8413ac9d 709 if (page && !trylock_page(page)) {
93fac704
NP
710 page_cache_release(page);
711 page = NULL;
712 }
713 }
5d337b91 714 spin_unlock(&swap_lock);
1da177e4
LT
715 }
716 if (page) {
a2c43eed
HD
717 /*
718 * Not mapped elsewhere, or swap space full? Free it!
719 * Also recheck PageSwapCache now page is locked (above).
720 */
93fac704 721 if (PageSwapCache(page) && !PageWriteback(page) &&
a2c43eed 722 (!page_mapped(page) || vm_swap_full())) {
1da177e4
LT
723 delete_from_swap_cache(page);
724 SetPageDirty(page);
725 }
726 unlock_page(page);
727 page_cache_release(page);
728 }
2509ef26 729 return p != NULL;
1da177e4
LT
730}
731
b0cb1a19 732#ifdef CONFIG_HIBERNATION
f577eb30 733/*
915bae9e 734 * Find the swap type that corresponds to given device (if any).
f577eb30 735 *
915bae9e
RW
736 * @offset - number of the PAGE_SIZE-sized block of the device, starting
737 * from 0, in which the swap header is expected to be located.
738 *
739 * This is needed for the suspend to disk (aka swsusp).
f577eb30 740 */
7bf23687 741int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
f577eb30 742{
915bae9e 743 struct block_device *bdev = NULL;
efa90a98 744 int type;
f577eb30 745
915bae9e
RW
746 if (device)
747 bdev = bdget(device);
748
f577eb30 749 spin_lock(&swap_lock);
efa90a98
HD
750 for (type = 0; type < nr_swapfiles; type++) {
751 struct swap_info_struct *sis = swap_info[type];
f577eb30 752
915bae9e 753 if (!(sis->flags & SWP_WRITEOK))
f577eb30 754 continue;
b6b5bce3 755
915bae9e 756 if (!bdev) {
7bf23687 757 if (bdev_p)
dddac6a7 758 *bdev_p = bdgrab(sis->bdev);
7bf23687 759
6e1819d6 760 spin_unlock(&swap_lock);
efa90a98 761 return type;
6e1819d6 762 }
915bae9e
RW
763 if (bdev == sis->bdev) {
764 struct swap_extent *se;
765
766 se = list_entry(sis->extent_list.next,
767 struct swap_extent, list);
768 if (se->start_block == offset) {
7bf23687 769 if (bdev_p)
dddac6a7 770 *bdev_p = bdgrab(sis->bdev);
7bf23687 771
915bae9e
RW
772 spin_unlock(&swap_lock);
773 bdput(bdev);
efa90a98 774 return type;
915bae9e 775 }
f577eb30
RW
776 }
777 }
778 spin_unlock(&swap_lock);
915bae9e
RW
779 if (bdev)
780 bdput(bdev);
781
f577eb30
RW
782 return -ENODEV;
783}
784
785/*
786 * Return either the total number of swap pages of given type, or the number
787 * of free pages of that type (depending on @free)
788 *
789 * This is needed for software suspend
790 */
791unsigned int count_swap_pages(int type, int free)
792{
793 unsigned int n = 0;
794
efa90a98
HD
795 spin_lock(&swap_lock);
796 if ((unsigned int)type < nr_swapfiles) {
797 struct swap_info_struct *sis = swap_info[type];
798
799 if (sis->flags & SWP_WRITEOK) {
800 n = sis->pages;
f577eb30 801 if (free)
efa90a98 802 n -= sis->inuse_pages;
f577eb30 803 }
f577eb30 804 }
efa90a98 805 spin_unlock(&swap_lock);
f577eb30
RW
806 return n;
807}
808#endif
809
1da177e4 810/*
72866f6f
HD
811 * No need to decide whether this PTE shares the swap entry with others,
812 * just let do_wp_page work it out if a write is requested later - to
813 * force COW, vm_page_prot omits write permission from any private vma.
1da177e4 814 */
044d66c1 815static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
1da177e4
LT
816 unsigned long addr, swp_entry_t entry, struct page *page)
817{
7a81b88c 818 struct mem_cgroup *ptr = NULL;
044d66c1
HD
819 spinlock_t *ptl;
820 pte_t *pte;
821 int ret = 1;
822
85d9fc89 823 if (mem_cgroup_try_charge_swapin(vma->vm_mm, page, GFP_KERNEL, &ptr)) {
044d66c1 824 ret = -ENOMEM;
85d9fc89
KH
825 goto out_nolock;
826 }
044d66c1
HD
827
828 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
829 if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) {
830 if (ret > 0)
7a81b88c 831 mem_cgroup_cancel_charge_swapin(ptr);
044d66c1
HD
832 ret = 0;
833 goto out;
834 }
8a9f3ccd 835
4294621f 836 inc_mm_counter(vma->vm_mm, anon_rss);
1da177e4
LT
837 get_page(page);
838 set_pte_at(vma->vm_mm, addr, pte,
839 pte_mkold(mk_pte(page, vma->vm_page_prot)));
840 page_add_anon_rmap(page, vma, addr);
7a81b88c 841 mem_cgroup_commit_charge_swapin(page, ptr);
1da177e4
LT
842 swap_free(entry);
843 /*
844 * Move the page to the active list so it is not
845 * immediately swapped out again after swapon.
846 */
847 activate_page(page);
044d66c1
HD
848out:
849 pte_unmap_unlock(pte, ptl);
85d9fc89 850out_nolock:
044d66c1 851 return ret;
1da177e4
LT
852}
853
854static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
855 unsigned long addr, unsigned long end,
856 swp_entry_t entry, struct page *page)
857{
1da177e4 858 pte_t swp_pte = swp_entry_to_pte(entry);
705e87c0 859 pte_t *pte;
8a9f3ccd 860 int ret = 0;
1da177e4 861
044d66c1
HD
862 /*
863 * We don't actually need pte lock while scanning for swp_pte: since
864 * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
865 * page table while we're scanning; though it could get zapped, and on
866 * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
867 * of unmatched parts which look like swp_pte, so unuse_pte must
868 * recheck under pte lock. Scanning without pte lock lets it be
869 * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
870 */
871 pte = pte_offset_map(pmd, addr);
1da177e4
LT
872 do {
873 /*
874 * swapoff spends a _lot_ of time in this loop!
875 * Test inline before going to call unuse_pte.
876 */
877 if (unlikely(pte_same(*pte, swp_pte))) {
044d66c1
HD
878 pte_unmap(pte);
879 ret = unuse_pte(vma, pmd, addr, entry, page);
880 if (ret)
881 goto out;
882 pte = pte_offset_map(pmd, addr);
1da177e4
LT
883 }
884 } while (pte++, addr += PAGE_SIZE, addr != end);
044d66c1
HD
885 pte_unmap(pte - 1);
886out:
8a9f3ccd 887 return ret;
1da177e4
LT
888}
889
890static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
891 unsigned long addr, unsigned long end,
892 swp_entry_t entry, struct page *page)
893{
894 pmd_t *pmd;
895 unsigned long next;
8a9f3ccd 896 int ret;
1da177e4
LT
897
898 pmd = pmd_offset(pud, addr);
899 do {
900 next = pmd_addr_end(addr, end);
901 if (pmd_none_or_clear_bad(pmd))
902 continue;
8a9f3ccd
BS
903 ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
904 if (ret)
905 return ret;
1da177e4
LT
906 } while (pmd++, addr = next, addr != end);
907 return 0;
908}
909
910static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
911 unsigned long addr, unsigned long end,
912 swp_entry_t entry, struct page *page)
913{
914 pud_t *pud;
915 unsigned long next;
8a9f3ccd 916 int ret;
1da177e4
LT
917
918 pud = pud_offset(pgd, addr);
919 do {
920 next = pud_addr_end(addr, end);
921 if (pud_none_or_clear_bad(pud))
922 continue;
8a9f3ccd
BS
923 ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
924 if (ret)
925 return ret;
1da177e4
LT
926 } while (pud++, addr = next, addr != end);
927 return 0;
928}
929
930static int unuse_vma(struct vm_area_struct *vma,
931 swp_entry_t entry, struct page *page)
932{
933 pgd_t *pgd;
934 unsigned long addr, end, next;
8a9f3ccd 935 int ret;
1da177e4
LT
936
937 if (page->mapping) {
938 addr = page_address_in_vma(page, vma);
939 if (addr == -EFAULT)
940 return 0;
941 else
942 end = addr + PAGE_SIZE;
943 } else {
944 addr = vma->vm_start;
945 end = vma->vm_end;
946 }
947
948 pgd = pgd_offset(vma->vm_mm, addr);
949 do {
950 next = pgd_addr_end(addr, end);
951 if (pgd_none_or_clear_bad(pgd))
952 continue;
8a9f3ccd
BS
953 ret = unuse_pud_range(vma, pgd, addr, next, entry, page);
954 if (ret)
955 return ret;
1da177e4
LT
956 } while (pgd++, addr = next, addr != end);
957 return 0;
958}
959
960static int unuse_mm(struct mm_struct *mm,
961 swp_entry_t entry, struct page *page)
962{
963 struct vm_area_struct *vma;
8a9f3ccd 964 int ret = 0;
1da177e4
LT
965
966 if (!down_read_trylock(&mm->mmap_sem)) {
967 /*
7d03431c
FLVC
968 * Activate page so shrink_inactive_list is unlikely to unmap
969 * its ptes while lock is dropped, so swapoff can make progress.
1da177e4 970 */
c475a8ab 971 activate_page(page);
1da177e4
LT
972 unlock_page(page);
973 down_read(&mm->mmap_sem);
974 lock_page(page);
975 }
1da177e4 976 for (vma = mm->mmap; vma; vma = vma->vm_next) {
8a9f3ccd 977 if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
1da177e4
LT
978 break;
979 }
1da177e4 980 up_read(&mm->mmap_sem);
8a9f3ccd 981 return (ret < 0)? ret: 0;
1da177e4
LT
982}
983
984/*
985 * Scan swap_map from current position to next entry still in use.
986 * Recycle to start on reaching the end, returning 0 when empty.
987 */
6eb396dc
HD
988static unsigned int find_next_to_unuse(struct swap_info_struct *si,
989 unsigned int prev)
1da177e4 990{
6eb396dc
HD
991 unsigned int max = si->max;
992 unsigned int i = prev;
1da177e4
LT
993 int count;
994
995 /*
5d337b91 996 * No need for swap_lock here: we're just looking
1da177e4
LT
997 * for whether an entry is in use, not modifying it; false
998 * hits are okay, and sys_swapoff() has already prevented new
5d337b91 999 * allocations from this area (while holding swap_lock).
1da177e4
LT
1000 */
1001 for (;;) {
1002 if (++i >= max) {
1003 if (!prev) {
1004 i = 0;
1005 break;
1006 }
1007 /*
1008 * No entries in use at top of swap_map,
1009 * loop back to start and recheck there.
1010 */
1011 max = prev + 1;
1012 prev = 0;
1013 i = 1;
1014 }
1015 count = si->swap_map[i];
355cfa73 1016 if (count && swap_count(count) != SWAP_MAP_BAD)
1da177e4
LT
1017 break;
1018 }
1019 return i;
1020}
1021
1022/*
1023 * We completely avoid races by reading each swap page in advance,
1024 * and then search for the process using it. All the necessary
1025 * page table adjustments can then be made atomically.
1026 */
1027static int try_to_unuse(unsigned int type)
1028{
efa90a98 1029 struct swap_info_struct *si = swap_info[type];
1da177e4
LT
1030 struct mm_struct *start_mm;
1031 unsigned short *swap_map;
1032 unsigned short swcount;
1033 struct page *page;
1034 swp_entry_t entry;
6eb396dc 1035 unsigned int i = 0;
1da177e4
LT
1036 int retval = 0;
1037 int reset_overflow = 0;
1038 int shmem;
1039
1040 /*
1041 * When searching mms for an entry, a good strategy is to
1042 * start at the first mm we freed the previous entry from
1043 * (though actually we don't notice whether we or coincidence
1044 * freed the entry). Initialize this start_mm with a hold.
1045 *
1046 * A simpler strategy would be to start at the last mm we
1047 * freed the previous entry from; but that would take less
1048 * advantage of mmlist ordering, which clusters forked mms
1049 * together, child after parent. If we race with dup_mmap(), we
1050 * prefer to resolve parent before child, lest we miss entries
1051 * duplicated after we scanned child: using last mm would invert
1052 * that. Though it's only a serious concern when an overflowed
1053 * swap count is reset from SWAP_MAP_MAX, preventing a rescan.
1054 */
1055 start_mm = &init_mm;
1056 atomic_inc(&init_mm.mm_users);
1057
1058 /*
1059 * Keep on scanning until all entries have gone. Usually,
1060 * one pass through swap_map is enough, but not necessarily:
1061 * there are races when an instance of an entry might be missed.
1062 */
1063 while ((i = find_next_to_unuse(si, i)) != 0) {
1064 if (signal_pending(current)) {
1065 retval = -EINTR;
1066 break;
1067 }
1068
886bb7e9 1069 /*
1da177e4
LT
1070 * Get a page for the entry, using the existing swap
1071 * cache page if there is one. Otherwise, get a clean
886bb7e9 1072 * page and read the swap into it.
1da177e4
LT
1073 */
1074 swap_map = &si->swap_map[i];
1075 entry = swp_entry(type, i);
02098fea
HD
1076 page = read_swap_cache_async(entry,
1077 GFP_HIGHUSER_MOVABLE, NULL, 0);
1da177e4
LT
1078 if (!page) {
1079 /*
1080 * Either swap_duplicate() failed because entry
1081 * has been freed independently, and will not be
1082 * reused since sys_swapoff() already disabled
1083 * allocation from here, or alloc_page() failed.
1084 */
1085 if (!*swap_map)
1086 continue;
1087 retval = -ENOMEM;
1088 break;
1089 }
1090
1091 /*
1092 * Don't hold on to start_mm if it looks like exiting.
1093 */
1094 if (atomic_read(&start_mm->mm_users) == 1) {
1095 mmput(start_mm);
1096 start_mm = &init_mm;
1097 atomic_inc(&init_mm.mm_users);
1098 }
1099
1100 /*
1101 * Wait for and lock page. When do_swap_page races with
1102 * try_to_unuse, do_swap_page can handle the fault much
1103 * faster than try_to_unuse can locate the entry. This
1104 * apparently redundant "wait_on_page_locked" lets try_to_unuse
1105 * defer to do_swap_page in such a case - in some tests,
1106 * do_swap_page and try_to_unuse repeatedly compete.
1107 */
1108 wait_on_page_locked(page);
1109 wait_on_page_writeback(page);
1110 lock_page(page);
1111 wait_on_page_writeback(page);
1112
1113 /*
1114 * Remove all references to entry.
1115 * Whenever we reach init_mm, there's no address space
1116 * to search, but use it as a reminder to search shmem.
1117 */
1118 shmem = 0;
1119 swcount = *swap_map;
355cfa73 1120 if (swap_count(swcount)) {
1da177e4
LT
1121 if (start_mm == &init_mm)
1122 shmem = shmem_unuse(entry, page);
1123 else
1124 retval = unuse_mm(start_mm, entry, page);
1125 }
355cfa73 1126 if (swap_count(*swap_map)) {
1da177e4
LT
1127 int set_start_mm = (*swap_map >= swcount);
1128 struct list_head *p = &start_mm->mmlist;
1129 struct mm_struct *new_start_mm = start_mm;
1130 struct mm_struct *prev_mm = start_mm;
1131 struct mm_struct *mm;
1132
1133 atomic_inc(&new_start_mm->mm_users);
1134 atomic_inc(&prev_mm->mm_users);
1135 spin_lock(&mmlist_lock);
355cfa73 1136 while (swap_count(*swap_map) && !retval && !shmem &&
1da177e4
LT
1137 (p = p->next) != &start_mm->mmlist) {
1138 mm = list_entry(p, struct mm_struct, mmlist);
70af7c5c 1139 if (!atomic_inc_not_zero(&mm->mm_users))
1da177e4 1140 continue;
1da177e4
LT
1141 spin_unlock(&mmlist_lock);
1142 mmput(prev_mm);
1143 prev_mm = mm;
1144
1145 cond_resched();
1146
1147 swcount = *swap_map;
355cfa73 1148 if (!swap_count(swcount)) /* any usage ? */
1da177e4
LT
1149 ;
1150 else if (mm == &init_mm) {
1151 set_start_mm = 1;
1152 shmem = shmem_unuse(entry, page);
1153 } else
1154 retval = unuse_mm(mm, entry, page);
355cfa73 1155
32c5fc10 1156 if (set_start_mm && *swap_map < swcount) {
1da177e4
LT
1157 mmput(new_start_mm);
1158 atomic_inc(&mm->mm_users);
1159 new_start_mm = mm;
1160 set_start_mm = 0;
1161 }
1162 spin_lock(&mmlist_lock);
1163 }
1164 spin_unlock(&mmlist_lock);
1165 mmput(prev_mm);
1166 mmput(start_mm);
1167 start_mm = new_start_mm;
1168 }
2e0e26c7
HD
1169 if (shmem) {
1170 /* page has already been unlocked and released */
1171 if (shmem > 0)
1172 continue;
1173 retval = shmem;
1174 break;
1175 }
1da177e4
LT
1176 if (retval) {
1177 unlock_page(page);
1178 page_cache_release(page);
1179 break;
1180 }
1181
1182 /*
355cfa73
KH
1183 * How could swap count reach 0x7ffe ?
1184 * There's no way to repeat a swap page within an mm
1185 * (except in shmem, where it's the shared object which takes
1186 * the reference count)?
1187 * We believe SWAP_MAP_MAX cannot occur.(if occur, unsigned
1188 * short is too small....)
1da177e4
LT
1189 * If that's wrong, then we should worry more about
1190 * exit_mmap() and do_munmap() cases described above:
1191 * we might be resetting SWAP_MAP_MAX too early here.
1192 * We know "Undead"s can happen, they're okay, so don't
1193 * report them; but do report if we reset SWAP_MAP_MAX.
1194 */
355cfa73
KH
1195 /* We might release the lock_page() in unuse_mm(). */
1196 if (!PageSwapCache(page) || page_private(page) != entry.val)
1197 goto retry;
1198
1199 if (swap_count(*swap_map) == SWAP_MAP_MAX) {
5d337b91 1200 spin_lock(&swap_lock);
355cfa73 1201 *swap_map = encode_swapmap(0, true);
5d337b91 1202 spin_unlock(&swap_lock);
1da177e4
LT
1203 reset_overflow = 1;
1204 }
1205
1206 /*
1207 * If a reference remains (rare), we would like to leave
1208 * the page in the swap cache; but try_to_unmap could
1209 * then re-duplicate the entry once we drop page lock,
1210 * so we might loop indefinitely; also, that page could
1211 * not be swapped out to other storage meanwhile. So:
1212 * delete from cache even if there's another reference,
1213 * after ensuring that the data has been saved to disk -
1214 * since if the reference remains (rarer), it will be
1215 * read from disk into another page. Splitting into two
1216 * pages would be incorrect if swap supported "shared
1217 * private" pages, but they are handled by tmpfs files.
1da177e4 1218 */
355cfa73
KH
1219 if (swap_count(*swap_map) &&
1220 PageDirty(page) && PageSwapCache(page)) {
1da177e4
LT
1221 struct writeback_control wbc = {
1222 .sync_mode = WB_SYNC_NONE,
1223 };
1224
1225 swap_writepage(page, &wbc);
1226 lock_page(page);
1227 wait_on_page_writeback(page);
1228 }
68bdc8d6
HD
1229
1230 /*
1231 * It is conceivable that a racing task removed this page from
1232 * swap cache just before we acquired the page lock at the top,
1233 * or while we dropped it in unuse_mm(). The page might even
1234 * be back in swap cache on another swap area: that we must not
1235 * delete, since it may not have been written out to swap yet.
1236 */
1237 if (PageSwapCache(page) &&
1238 likely(page_private(page) == entry.val))
2e0e26c7 1239 delete_from_swap_cache(page);
1da177e4
LT
1240
1241 /*
1242 * So we could skip searching mms once swap count went
1243 * to 1, we did not mark any present ptes as dirty: must
2706a1b8 1244 * mark page dirty so shrink_page_list will preserve it.
1da177e4
LT
1245 */
1246 SetPageDirty(page);
355cfa73 1247retry:
1da177e4
LT
1248 unlock_page(page);
1249 page_cache_release(page);
1250
1251 /*
1252 * Make sure that we aren't completely killing
1253 * interactive performance.
1254 */
1255 cond_resched();
1256 }
1257
1258 mmput(start_mm);
1259 if (reset_overflow) {
1260 printk(KERN_WARNING "swapoff: cleared swap entry overflow\n");
1261 swap_overflow = 0;
1262 }
1263 return retval;
1264}
1265
1266/*
5d337b91
HD
1267 * After a successful try_to_unuse, if no swap is now in use, we know
1268 * we can empty the mmlist. swap_lock must be held on entry and exit.
1269 * Note that mmlist_lock nests inside swap_lock, and an mm must be
1da177e4
LT
1270 * added to the mmlist just after page_duplicate - before would be racy.
1271 */
1272static void drain_mmlist(void)
1273{
1274 struct list_head *p, *next;
efa90a98 1275 unsigned int type;
1da177e4 1276
efa90a98
HD
1277 for (type = 0; type < nr_swapfiles; type++)
1278 if (swap_info[type]->inuse_pages)
1da177e4
LT
1279 return;
1280 spin_lock(&mmlist_lock);
1281 list_for_each_safe(p, next, &init_mm.mmlist)
1282 list_del_init(p);
1283 spin_unlock(&mmlist_lock);
1284}
1285
1286/*
1287 * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
f29ad6a9
HD
1288 * corresponds to page offset `offset'. Note that the type of this function
1289 * is sector_t, but it returns page offset into the bdev, not sector offset.
1da177e4 1290 */
f29ad6a9 1291sector_t map_swap_page(swp_entry_t entry, struct block_device **bdev)
1da177e4 1292{
f29ad6a9
HD
1293 struct swap_info_struct *sis;
1294 struct swap_extent *start_se;
1295 struct swap_extent *se;
1296 pgoff_t offset;
1297
efa90a98 1298 sis = swap_info[swp_type(entry)];
f29ad6a9
HD
1299 *bdev = sis->bdev;
1300
1301 offset = swp_offset(entry);
1302 start_se = sis->curr_swap_extent;
1303 se = start_se;
1da177e4
LT
1304
1305 for ( ; ; ) {
1306 struct list_head *lh;
1307
1308 if (se->start_page <= offset &&
1309 offset < (se->start_page + se->nr_pages)) {
1310 return se->start_block + (offset - se->start_page);
1311 }
11d31886 1312 lh = se->list.next;
1da177e4 1313 if (lh == &sis->extent_list)
11d31886 1314 lh = lh->next;
1da177e4
LT
1315 se = list_entry(lh, struct swap_extent, list);
1316 sis->curr_swap_extent = se;
1317 BUG_ON(se == start_se); /* It *must* be present */
1318 }
1319}
1320
b0cb1a19 1321#ifdef CONFIG_HIBERNATION
3aef83e0
RW
1322/*
1323 * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
1324 * corresponding to given index in swap_info (swap type).
1325 */
efa90a98 1326sector_t swapdev_block(int type, pgoff_t offset)
3aef83e0 1327{
f29ad6a9 1328 struct block_device *bdev;
3aef83e0 1329
efa90a98 1330 if ((unsigned int)type >= nr_swapfiles)
3aef83e0 1331 return 0;
efa90a98
HD
1332 if (!(swap_info[type]->flags & SWP_WRITEOK))
1333 return 0;
1334 return map_swap_page(swp_entry(type, offset), &bdev);
3aef83e0 1335}
b0cb1a19 1336#endif /* CONFIG_HIBERNATION */
3aef83e0 1337
1da177e4
LT
1338/*
1339 * Free all of a swapdev's extent information
1340 */
1341static void destroy_swap_extents(struct swap_info_struct *sis)
1342{
1343 while (!list_empty(&sis->extent_list)) {
1344 struct swap_extent *se;
1345
1346 se = list_entry(sis->extent_list.next,
1347 struct swap_extent, list);
1348 list_del(&se->list);
1349 kfree(se);
1350 }
1da177e4
LT
1351}
1352
1353/*
1354 * Add a block range (and the corresponding page range) into this swapdev's
11d31886 1355 * extent list. The extent list is kept sorted in page order.
1da177e4 1356 *
11d31886 1357 * This function rather assumes that it is called in ascending page order.
1da177e4
LT
1358 */
1359static int
1360add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
1361 unsigned long nr_pages, sector_t start_block)
1362{
1363 struct swap_extent *se;
1364 struct swap_extent *new_se;
1365 struct list_head *lh;
1366
11d31886
HD
1367 lh = sis->extent_list.prev; /* The highest page extent */
1368 if (lh != &sis->extent_list) {
1da177e4 1369 se = list_entry(lh, struct swap_extent, list);
11d31886
HD
1370 BUG_ON(se->start_page + se->nr_pages != start_page);
1371 if (se->start_block + se->nr_pages == start_block) {
1da177e4
LT
1372 /* Merge it */
1373 se->nr_pages += nr_pages;
1374 return 0;
1375 }
1da177e4
LT
1376 }
1377
1378 /*
1379 * No merge. Insert a new extent, preserving ordering.
1380 */
1381 new_se = kmalloc(sizeof(*se), GFP_KERNEL);
1382 if (new_se == NULL)
1383 return -ENOMEM;
1384 new_se->start_page = start_page;
1385 new_se->nr_pages = nr_pages;
1386 new_se->start_block = start_block;
1387
11d31886 1388 list_add_tail(&new_se->list, &sis->extent_list);
53092a74 1389 return 1;
1da177e4
LT
1390}
1391
1392/*
1393 * A `swap extent' is a simple thing which maps a contiguous range of pages
1394 * onto a contiguous range of disk blocks. An ordered list of swap extents
1395 * is built at swapon time and is then used at swap_writepage/swap_readpage
1396 * time for locating where on disk a page belongs.
1397 *
1398 * If the swapfile is an S_ISBLK block device, a single extent is installed.
1399 * This is done so that the main operating code can treat S_ISBLK and S_ISREG
1400 * swap files identically.
1401 *
1402 * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
1403 * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
1404 * swapfiles are handled *identically* after swapon time.
1405 *
1406 * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
1407 * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
1408 * some stray blocks are found which do not fall within the PAGE_SIZE alignment
1409 * requirements, they are simply tossed out - we will never use those blocks
1410 * for swapping.
1411 *
b0d9bcd4 1412 * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This
1da177e4
LT
1413 * prevents root from shooting her foot off by ftruncating an in-use swapfile,
1414 * which will scribble on the fs.
1415 *
1416 * The amount of disk space which a single swap extent represents varies.
1417 * Typically it is in the 1-4 megabyte range. So we can have hundreds of
1418 * extents in the list. To avoid much list walking, we cache the previous
1419 * search location in `curr_swap_extent', and start new searches from there.
1420 * This is extremely effective. The average number of iterations in
1421 * map_swap_page() has been measured at about 0.3 per page. - akpm.
1422 */
53092a74 1423static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
1da177e4
LT
1424{
1425 struct inode *inode;
1426 unsigned blocks_per_page;
1427 unsigned long page_no;
1428 unsigned blkbits;
1429 sector_t probe_block;
1430 sector_t last_block;
53092a74
HD
1431 sector_t lowest_block = -1;
1432 sector_t highest_block = 0;
1433 int nr_extents = 0;
1da177e4
LT
1434 int ret;
1435
1436 inode = sis->swap_file->f_mapping->host;
1437 if (S_ISBLK(inode->i_mode)) {
1438 ret = add_swap_extent(sis, 0, sis->max, 0);
53092a74 1439 *span = sis->pages;
1da177e4
LT
1440 goto done;
1441 }
1442
1443 blkbits = inode->i_blkbits;
1444 blocks_per_page = PAGE_SIZE >> blkbits;
1445
1446 /*
1447 * Map all the blocks into the extent list. This code doesn't try
1448 * to be very smart.
1449 */
1450 probe_block = 0;
1451 page_no = 0;
1452 last_block = i_size_read(inode) >> blkbits;
1453 while ((probe_block + blocks_per_page) <= last_block &&
1454 page_no < sis->max) {
1455 unsigned block_in_page;
1456 sector_t first_block;
1457
1458 first_block = bmap(inode, probe_block);
1459 if (first_block == 0)
1460 goto bad_bmap;
1461
1462 /*
1463 * It must be PAGE_SIZE aligned on-disk
1464 */
1465 if (first_block & (blocks_per_page - 1)) {
1466 probe_block++;
1467 goto reprobe;
1468 }
1469
1470 for (block_in_page = 1; block_in_page < blocks_per_page;
1471 block_in_page++) {
1472 sector_t block;
1473
1474 block = bmap(inode, probe_block + block_in_page);
1475 if (block == 0)
1476 goto bad_bmap;
1477 if (block != first_block + block_in_page) {
1478 /* Discontiguity */
1479 probe_block++;
1480 goto reprobe;
1481 }
1482 }
1483
53092a74
HD
1484 first_block >>= (PAGE_SHIFT - blkbits);
1485 if (page_no) { /* exclude the header page */
1486 if (first_block < lowest_block)
1487 lowest_block = first_block;
1488 if (first_block > highest_block)
1489 highest_block = first_block;
1490 }
1491
1da177e4
LT
1492 /*
1493 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
1494 */
53092a74
HD
1495 ret = add_swap_extent(sis, page_no, 1, first_block);
1496 if (ret < 0)
1da177e4 1497 goto out;
53092a74 1498 nr_extents += ret;
1da177e4
LT
1499 page_no++;
1500 probe_block += blocks_per_page;
1501reprobe:
1502 continue;
1503 }
53092a74
HD
1504 ret = nr_extents;
1505 *span = 1 + highest_block - lowest_block;
1da177e4 1506 if (page_no == 0)
e2244ec2 1507 page_no = 1; /* force Empty message */
1da177e4 1508 sis->max = page_no;
e2244ec2 1509 sis->pages = page_no - 1;
1da177e4
LT
1510 sis->highest_bit = page_no - 1;
1511done:
1512 sis->curr_swap_extent = list_entry(sis->extent_list.prev,
1513 struct swap_extent, list);
1514 goto out;
1515bad_bmap:
1516 printk(KERN_ERR "swapon: swapfile has holes\n");
1517 ret = -EINVAL;
1518out:
1519 return ret;
1520}
1521
c4ea37c2 1522SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
1da177e4
LT
1523{
1524 struct swap_info_struct * p = NULL;
1525 unsigned short *swap_map;
1526 struct file *swap_file, *victim;
1527 struct address_space *mapping;
1528 struct inode *inode;
1529 char * pathname;
1530 int i, type, prev;
1531 int err;
886bb7e9 1532
1da177e4
LT
1533 if (!capable(CAP_SYS_ADMIN))
1534 return -EPERM;
1535
1536 pathname = getname(specialfile);
1537 err = PTR_ERR(pathname);
1538 if (IS_ERR(pathname))
1539 goto out;
1540
1541 victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
1542 putname(pathname);
1543 err = PTR_ERR(victim);
1544 if (IS_ERR(victim))
1545 goto out;
1546
1547 mapping = victim->f_mapping;
1548 prev = -1;
5d337b91 1549 spin_lock(&swap_lock);
efa90a98
HD
1550 for (type = swap_list.head; type >= 0; type = swap_info[type]->next) {
1551 p = swap_info[type];
22c6f8fd 1552 if (p->flags & SWP_WRITEOK) {
1da177e4
LT
1553 if (p->swap_file->f_mapping == mapping)
1554 break;
1555 }
1556 prev = type;
1557 }
1558 if (type < 0) {
1559 err = -EINVAL;
5d337b91 1560 spin_unlock(&swap_lock);
1da177e4
LT
1561 goto out_dput;
1562 }
1563 if (!security_vm_enough_memory(p->pages))
1564 vm_unacct_memory(p->pages);
1565 else {
1566 err = -ENOMEM;
5d337b91 1567 spin_unlock(&swap_lock);
1da177e4
LT
1568 goto out_dput;
1569 }
efa90a98 1570 if (prev < 0)
1da177e4 1571 swap_list.head = p->next;
efa90a98
HD
1572 else
1573 swap_info[prev]->next = p->next;
1da177e4
LT
1574 if (type == swap_list.next) {
1575 /* just pick something that's safe... */
1576 swap_list.next = swap_list.head;
1577 }
78ecba08 1578 if (p->prio < 0) {
efa90a98
HD
1579 for (i = p->next; i >= 0; i = swap_info[i]->next)
1580 swap_info[i]->prio = p->prio--;
78ecba08
HD
1581 least_priority++;
1582 }
1da177e4
LT
1583 nr_swap_pages -= p->pages;
1584 total_swap_pages -= p->pages;
1585 p->flags &= ~SWP_WRITEOK;
5d337b91 1586 spin_unlock(&swap_lock);
fb4f88dc 1587
35451bee 1588 current->flags |= PF_OOM_ORIGIN;
1da177e4 1589 err = try_to_unuse(type);
35451bee 1590 current->flags &= ~PF_OOM_ORIGIN;
1da177e4 1591
1da177e4
LT
1592 if (err) {
1593 /* re-insert swap space back into swap_list */
5d337b91 1594 spin_lock(&swap_lock);
78ecba08
HD
1595 if (p->prio < 0)
1596 p->prio = --least_priority;
1597 prev = -1;
efa90a98
HD
1598 for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
1599 if (p->prio >= swap_info[i]->prio)
1da177e4 1600 break;
78ecba08
HD
1601 prev = i;
1602 }
1da177e4
LT
1603 p->next = i;
1604 if (prev < 0)
efa90a98 1605 swap_list.head = swap_list.next = type;
1da177e4 1606 else
efa90a98 1607 swap_info[prev]->next = type;
1da177e4
LT
1608 nr_swap_pages += p->pages;
1609 total_swap_pages += p->pages;
1610 p->flags |= SWP_WRITEOK;
5d337b91 1611 spin_unlock(&swap_lock);
1da177e4
LT
1612 goto out_dput;
1613 }
52b7efdb
HD
1614
1615 /* wait for any unplug function to finish */
1616 down_write(&swap_unplug_sem);
1617 up_write(&swap_unplug_sem);
1618
5d337b91 1619 destroy_swap_extents(p);
fc0abb14 1620 mutex_lock(&swapon_mutex);
5d337b91
HD
1621 spin_lock(&swap_lock);
1622 drain_mmlist();
1623
52b7efdb 1624 /* wait for anyone still in scan_swap_map */
52b7efdb
HD
1625 p->highest_bit = 0; /* cuts scans short */
1626 while (p->flags >= SWP_SCANNING) {
5d337b91 1627 spin_unlock(&swap_lock);
13e4b57f 1628 schedule_timeout_uninterruptible(1);
5d337b91 1629 spin_lock(&swap_lock);
52b7efdb 1630 }
52b7efdb 1631
1da177e4
LT
1632 swap_file = p->swap_file;
1633 p->swap_file = NULL;
1634 p->max = 0;
1635 swap_map = p->swap_map;
1636 p->swap_map = NULL;
1637 p->flags = 0;
5d337b91 1638 spin_unlock(&swap_lock);
fc0abb14 1639 mutex_unlock(&swapon_mutex);
1da177e4 1640 vfree(swap_map);
27a7faa0
KH
1641 /* Destroy swap account informatin */
1642 swap_cgroup_swapoff(type);
1643
1da177e4
LT
1644 inode = mapping->host;
1645 if (S_ISBLK(inode->i_mode)) {
1646 struct block_device *bdev = I_BDEV(inode);
1647 set_blocksize(bdev, p->old_block_size);
1648 bd_release(bdev);
1649 } else {
1b1dcc1b 1650 mutex_lock(&inode->i_mutex);
1da177e4 1651 inode->i_flags &= ~S_SWAPFILE;
1b1dcc1b 1652 mutex_unlock(&inode->i_mutex);
1da177e4
LT
1653 }
1654 filp_close(swap_file, NULL);
1655 err = 0;
1656
1657out_dput:
1658 filp_close(victim, NULL);
1659out:
1660 return err;
1661}
1662
1663#ifdef CONFIG_PROC_FS
1664/* iterator */
1665static void *swap_start(struct seq_file *swap, loff_t *pos)
1666{
efa90a98
HD
1667 struct swap_info_struct *si;
1668 int type;
1da177e4
LT
1669 loff_t l = *pos;
1670
fc0abb14 1671 mutex_lock(&swapon_mutex);
1da177e4 1672
881e4aab
SS
1673 if (!l)
1674 return SEQ_START_TOKEN;
1675
efa90a98
HD
1676 for (type = 0; type < nr_swapfiles; type++) {
1677 smp_rmb(); /* read nr_swapfiles before swap_info[type] */
1678 si = swap_info[type];
1679 if (!(si->flags & SWP_USED) || !si->swap_map)
1da177e4 1680 continue;
881e4aab 1681 if (!--l)
efa90a98 1682 return si;
1da177e4
LT
1683 }
1684
1685 return NULL;
1686}
1687
1688static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
1689{
efa90a98
HD
1690 struct swap_info_struct *si = v;
1691 int type;
1da177e4 1692
881e4aab 1693 if (v == SEQ_START_TOKEN)
efa90a98
HD
1694 type = 0;
1695 else
1696 type = si->type + 1;
881e4aab 1697
efa90a98
HD
1698 for (; type < nr_swapfiles; type++) {
1699 smp_rmb(); /* read nr_swapfiles before swap_info[type] */
1700 si = swap_info[type];
1701 if (!(si->flags & SWP_USED) || !si->swap_map)
1da177e4
LT
1702 continue;
1703 ++*pos;
efa90a98 1704 return si;
1da177e4
LT
1705 }
1706
1707 return NULL;
1708}
1709
1710static void swap_stop(struct seq_file *swap, void *v)
1711{
fc0abb14 1712 mutex_unlock(&swapon_mutex);
1da177e4
LT
1713}
1714
1715static int swap_show(struct seq_file *swap, void *v)
1716{
efa90a98 1717 struct swap_info_struct *si = v;
1da177e4
LT
1718 struct file *file;
1719 int len;
1720
efa90a98 1721 if (si == SEQ_START_TOKEN) {
881e4aab
SS
1722 seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1723 return 0;
1724 }
1da177e4 1725
efa90a98 1726 file = si->swap_file;
c32c2f63 1727 len = seq_path(swap, &file->f_path, " \t\n\\");
6eb396dc 1728 seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
886bb7e9
HD
1729 len < 40 ? 40 - len : 1, " ",
1730 S_ISBLK(file->f_path.dentry->d_inode->i_mode) ?
1da177e4 1731 "partition" : "file\t",
efa90a98
HD
1732 si->pages << (PAGE_SHIFT - 10),
1733 si->inuse_pages << (PAGE_SHIFT - 10),
1734 si->prio);
1da177e4
LT
1735 return 0;
1736}
1737
15ad7cdc 1738static const struct seq_operations swaps_op = {
1da177e4
LT
1739 .start = swap_start,
1740 .next = swap_next,
1741 .stop = swap_stop,
1742 .show = swap_show
1743};
1744
1745static int swaps_open(struct inode *inode, struct file *file)
1746{
1747 return seq_open(file, &swaps_op);
1748}
1749
15ad7cdc 1750static const struct file_operations proc_swaps_operations = {
1da177e4
LT
1751 .open = swaps_open,
1752 .read = seq_read,
1753 .llseek = seq_lseek,
1754 .release = seq_release,
1755};
1756
1757static int __init procswaps_init(void)
1758{
3d71f86f 1759 proc_create("swaps", 0, NULL, &proc_swaps_operations);
1da177e4
LT
1760 return 0;
1761}
1762__initcall(procswaps_init);
1763#endif /* CONFIG_PROC_FS */
1764
1796316a
JB
1765#ifdef MAX_SWAPFILES_CHECK
1766static int __init max_swapfiles_check(void)
1767{
1768 MAX_SWAPFILES_CHECK();
1769 return 0;
1770}
1771late_initcall(max_swapfiles_check);
1772#endif
1773
1da177e4
LT
1774/*
1775 * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
1776 *
1777 * The swapon system call
1778 */
c4ea37c2 1779SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
1da177e4
LT
1780{
1781 struct swap_info_struct * p;
1782 char *name = NULL;
1783 struct block_device *bdev = NULL;
1784 struct file *swap_file = NULL;
1785 struct address_space *mapping;
1786 unsigned int type;
1787 int i, prev;
1788 int error;
1da177e4 1789 union swap_header *swap_header = NULL;
6eb396dc
HD
1790 unsigned int nr_good_pages = 0;
1791 int nr_extents = 0;
53092a74 1792 sector_t span;
1da177e4 1793 unsigned long maxpages = 1;
73fd8748 1794 unsigned long swapfilepages;
78ecba08 1795 unsigned short *swap_map = NULL;
1da177e4
LT
1796 struct page *page = NULL;
1797 struct inode *inode = NULL;
1798 int did_down = 0;
1799
1800 if (!capable(CAP_SYS_ADMIN))
1801 return -EPERM;
efa90a98
HD
1802
1803 p = kzalloc(sizeof(*p), GFP_KERNEL);
1804 if (!p)
1805 return -ENOMEM;
1806
5d337b91 1807 spin_lock(&swap_lock);
efa90a98
HD
1808 for (type = 0; type < nr_swapfiles; type++) {
1809 if (!(swap_info[type]->flags & SWP_USED))
1da177e4 1810 break;
efa90a98 1811 }
1da177e4 1812 error = -EPERM;
0697212a 1813 if (type >= MAX_SWAPFILES) {
5d337b91 1814 spin_unlock(&swap_lock);
efa90a98 1815 kfree(p);
1da177e4
LT
1816 goto out;
1817 }
1da177e4 1818 INIT_LIST_HEAD(&p->extent_list);
efa90a98
HD
1819 if (type >= nr_swapfiles) {
1820 p->type = type;
1821 swap_info[type] = p;
1822 /*
1823 * Write swap_info[type] before nr_swapfiles, in case a
1824 * racing procfs swap_start() or swap_next() is reading them.
1825 * (We never shrink nr_swapfiles, we never free this entry.)
1826 */
1827 smp_wmb();
1828 nr_swapfiles++;
1829 } else {
1830 kfree(p);
1831 p = swap_info[type];
1832 /*
1833 * Do not memset this entry: a racing procfs swap_next()
1834 * would be relying on p->type to remain valid.
1835 */
1836 }
1da177e4 1837 p->flags = SWP_USED;
1da177e4 1838 p->next = -1;
5d337b91 1839 spin_unlock(&swap_lock);
efa90a98 1840
1da177e4
LT
1841 name = getname(specialfile);
1842 error = PTR_ERR(name);
1843 if (IS_ERR(name)) {
1844 name = NULL;
1845 goto bad_swap_2;
1846 }
1847 swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
1848 error = PTR_ERR(swap_file);
1849 if (IS_ERR(swap_file)) {
1850 swap_file = NULL;
1851 goto bad_swap_2;
1852 }
1853
1854 p->swap_file = swap_file;
1855 mapping = swap_file->f_mapping;
1856 inode = mapping->host;
1857
1858 error = -EBUSY;
1859 for (i = 0; i < nr_swapfiles; i++) {
efa90a98 1860 struct swap_info_struct *q = swap_info[i];
1da177e4
LT
1861
1862 if (i == type || !q->swap_file)
1863 continue;
1864 if (mapping == q->swap_file->f_mapping)
1865 goto bad_swap;
1866 }
1867
1868 error = -EINVAL;
1869 if (S_ISBLK(inode->i_mode)) {
1870 bdev = I_BDEV(inode);
1871 error = bd_claim(bdev, sys_swapon);
1872 if (error < 0) {
1873 bdev = NULL;
f7b3a435 1874 error = -EINVAL;
1da177e4
LT
1875 goto bad_swap;
1876 }
1877 p->old_block_size = block_size(bdev);
1878 error = set_blocksize(bdev, PAGE_SIZE);
1879 if (error < 0)
1880 goto bad_swap;
1881 p->bdev = bdev;
1882 } else if (S_ISREG(inode->i_mode)) {
1883 p->bdev = inode->i_sb->s_bdev;
1b1dcc1b 1884 mutex_lock(&inode->i_mutex);
1da177e4
LT
1885 did_down = 1;
1886 if (IS_SWAPFILE(inode)) {
1887 error = -EBUSY;
1888 goto bad_swap;
1889 }
1890 } else {
1891 goto bad_swap;
1892 }
1893
73fd8748 1894 swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
1da177e4
LT
1895
1896 /*
1897 * Read the swap header.
1898 */
1899 if (!mapping->a_ops->readpage) {
1900 error = -EINVAL;
1901 goto bad_swap;
1902 }
090d2b18 1903 page = read_mapping_page(mapping, 0, swap_file);
1da177e4
LT
1904 if (IS_ERR(page)) {
1905 error = PTR_ERR(page);
1906 goto bad_swap;
1907 }
81e33971 1908 swap_header = kmap(page);
1da177e4 1909
81e33971 1910 if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
e97a3111 1911 printk(KERN_ERR "Unable to find swap-space signature\n");
1da177e4
LT
1912 error = -EINVAL;
1913 goto bad_swap;
1914 }
886bb7e9 1915
81e33971
HD
1916 /* swap partition endianess hack... */
1917 if (swab32(swap_header->info.version) == 1) {
1918 swab32s(&swap_header->info.version);
1919 swab32s(&swap_header->info.last_page);
1920 swab32s(&swap_header->info.nr_badpages);
1921 for (i = 0; i < swap_header->info.nr_badpages; i++)
1922 swab32s(&swap_header->info.badpages[i]);
1923 }
1924 /* Check the swap header's sub-version */
1925 if (swap_header->info.version != 1) {
1926 printk(KERN_WARNING
1927 "Unable to handle swap header version %d\n",
1928 swap_header->info.version);
1da177e4
LT
1929 error = -EINVAL;
1930 goto bad_swap;
81e33971 1931 }
1da177e4 1932
81e33971
HD
1933 p->lowest_bit = 1;
1934 p->cluster_next = 1;
efa90a98 1935 p->cluster_nr = 0;
52b7efdb 1936
81e33971
HD
1937 /*
1938 * Find out how many pages are allowed for a single swap
1939 * device. There are two limiting factors: 1) the number of
1940 * bits for the swap offset in the swp_entry_t type and
1941 * 2) the number of bits in the a swap pte as defined by
1942 * the different architectures. In order to find the
1943 * largest possible bit mask a swap entry with swap type 0
1944 * and swap offset ~0UL is created, encoded to a swap pte,
1945 * decoded to a swp_entry_t again and finally the swap
1946 * offset is extracted. This will mask all the bits from
1947 * the initial ~0UL mask that can't be encoded in either
1948 * the swp_entry_t or the architecture definition of a
1949 * swap pte.
1950 */
1951 maxpages = swp_offset(pte_to_swp_entry(
1952 swp_entry_to_pte(swp_entry(0, ~0UL)))) - 1;
1953 if (maxpages > swap_header->info.last_page)
1954 maxpages = swap_header->info.last_page;
1955 p->highest_bit = maxpages - 1;
1da177e4 1956
81e33971
HD
1957 error = -EINVAL;
1958 if (!maxpages)
1959 goto bad_swap;
1960 if (swapfilepages && maxpages > swapfilepages) {
1961 printk(KERN_WARNING
1962 "Swap area shorter than signature indicates\n");
1963 goto bad_swap;
1964 }
1965 if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
1966 goto bad_swap;
1967 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
1968 goto bad_swap;
cd105df4 1969
81e33971
HD
1970 /* OK, set up the swap map and apply the bad block list */
1971 swap_map = vmalloc(maxpages * sizeof(short));
1972 if (!swap_map) {
1973 error = -ENOMEM;
1974 goto bad_swap;
1975 }
1da177e4 1976
81e33971
HD
1977 memset(swap_map, 0, maxpages * sizeof(short));
1978 for (i = 0; i < swap_header->info.nr_badpages; i++) {
1979 int page_nr = swap_header->info.badpages[i];
1980 if (page_nr <= 0 || page_nr >= swap_header->info.last_page) {
1981 error = -EINVAL;
1da177e4 1982 goto bad_swap;
81e33971
HD
1983 }
1984 swap_map[page_nr] = SWAP_MAP_BAD;
1da177e4 1985 }
27a7faa0
KH
1986
1987 error = swap_cgroup_swapon(type, maxpages);
1988 if (error)
1989 goto bad_swap;
1990
81e33971
HD
1991 nr_good_pages = swap_header->info.last_page -
1992 swap_header->info.nr_badpages -
1993 1 /* header page */;
e2244ec2 1994
e2244ec2 1995 if (nr_good_pages) {
78ecba08 1996 swap_map[0] = SWAP_MAP_BAD;
e2244ec2
HD
1997 p->max = maxpages;
1998 p->pages = nr_good_pages;
53092a74
HD
1999 nr_extents = setup_swap_extents(p, &span);
2000 if (nr_extents < 0) {
2001 error = nr_extents;
e2244ec2 2002 goto bad_swap;
53092a74 2003 }
e2244ec2
HD
2004 nr_good_pages = p->pages;
2005 }
1da177e4
LT
2006 if (!nr_good_pages) {
2007 printk(KERN_WARNING "Empty swap-file\n");
2008 error = -EINVAL;
2009 goto bad_swap;
2010 }
1da177e4 2011
3bd0f0c7
SJ
2012 if (p->bdev) {
2013 if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
2014 p->flags |= SWP_SOLIDSTATE;
2015 p->cluster_next = 1 + (random32() % p->highest_bit);
2016 }
2017 if (discard_swap(p) == 0)
2018 p->flags |= SWP_DISCARDABLE;
20137a49 2019 }
6a6ba831 2020
fc0abb14 2021 mutex_lock(&swapon_mutex);
5d337b91 2022 spin_lock(&swap_lock);
78ecba08
HD
2023 if (swap_flags & SWAP_FLAG_PREFER)
2024 p->prio =
2025 (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
2026 else
2027 p->prio = --least_priority;
2028 p->swap_map = swap_map;
22c6f8fd 2029 p->flags |= SWP_WRITEOK;
1da177e4
LT
2030 nr_swap_pages += nr_good_pages;
2031 total_swap_pages += nr_good_pages;
53092a74 2032
6eb396dc 2033 printk(KERN_INFO "Adding %uk swap on %s. "
20137a49 2034 "Priority:%d extents:%d across:%lluk %s%s\n",
53092a74 2035 nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
6a6ba831 2036 nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
20137a49
HD
2037 (p->flags & SWP_SOLIDSTATE) ? "SS" : "",
2038 (p->flags & SWP_DISCARDABLE) ? "D" : "");
1da177e4
LT
2039
2040 /* insert swap space into swap_list: */
2041 prev = -1;
efa90a98
HD
2042 for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
2043 if (p->prio >= swap_info[i]->prio)
1da177e4 2044 break;
1da177e4
LT
2045 prev = i;
2046 }
2047 p->next = i;
efa90a98
HD
2048 if (prev < 0)
2049 swap_list.head = swap_list.next = type;
2050 else
2051 swap_info[prev]->next = type;
5d337b91 2052 spin_unlock(&swap_lock);
fc0abb14 2053 mutex_unlock(&swapon_mutex);
1da177e4
LT
2054 error = 0;
2055 goto out;
2056bad_swap:
2057 if (bdev) {
2058 set_blocksize(bdev, p->old_block_size);
2059 bd_release(bdev);
2060 }
4cd3bb10 2061 destroy_swap_extents(p);
27a7faa0 2062 swap_cgroup_swapoff(type);
1da177e4 2063bad_swap_2:
5d337b91 2064 spin_lock(&swap_lock);
1da177e4 2065 p->swap_file = NULL;
1da177e4 2066 p->flags = 0;
5d337b91 2067 spin_unlock(&swap_lock);
1da177e4
LT
2068 vfree(swap_map);
2069 if (swap_file)
2070 filp_close(swap_file, NULL);
2071out:
2072 if (page && !IS_ERR(page)) {
2073 kunmap(page);
2074 page_cache_release(page);
2075 }
2076 if (name)
2077 putname(name);
2078 if (did_down) {
2079 if (!error)
2080 inode->i_flags |= S_SWAPFILE;
1b1dcc1b 2081 mutex_unlock(&inode->i_mutex);
1da177e4
LT
2082 }
2083 return error;
2084}
2085
2086void si_swapinfo(struct sysinfo *val)
2087{
efa90a98 2088 unsigned int type;
1da177e4
LT
2089 unsigned long nr_to_be_unused = 0;
2090
5d337b91 2091 spin_lock(&swap_lock);
efa90a98
HD
2092 for (type = 0; type < nr_swapfiles; type++) {
2093 struct swap_info_struct *si = swap_info[type];
2094
2095 if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
2096 nr_to_be_unused += si->inuse_pages;
1da177e4
LT
2097 }
2098 val->freeswap = nr_swap_pages + nr_to_be_unused;
2099 val->totalswap = total_swap_pages + nr_to_be_unused;
5d337b91 2100 spin_unlock(&swap_lock);
1da177e4
LT
2101}
2102
2103/*
2104 * Verify that a swap entry is valid and increment its swap map count.
2105 *
2106 * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
2107 * "permanent", but will be reclaimed by the next swapoff.
355cfa73
KH
2108 * Returns error code in following case.
2109 * - success -> 0
2110 * - swp_entry is invalid -> EINVAL
2111 * - swp_entry is migration entry -> EINVAL
2112 * - swap-cache reference is requested but there is already one. -> EEXIST
2113 * - swap-cache reference is requested but the entry is not used. -> ENOENT
1da177e4 2114 */
355cfa73 2115static int __swap_duplicate(swp_entry_t entry, bool cache)
1da177e4
LT
2116{
2117 struct swap_info_struct * p;
2118 unsigned long offset, type;
355cfa73
KH
2119 int result = -EINVAL;
2120 int count;
2121 bool has_cache;
1da177e4 2122
a7420aa5 2123 if (non_swap_entry(entry))
355cfa73 2124 return -EINVAL;
0697212a 2125
1da177e4
LT
2126 type = swp_type(entry);
2127 if (type >= nr_swapfiles)
2128 goto bad_file;
efa90a98 2129 p = swap_info[type];
1da177e4
LT
2130 offset = swp_offset(entry);
2131
5d337b91 2132 spin_lock(&swap_lock);
355cfa73
KH
2133
2134 if (unlikely(offset >= p->max))
2135 goto unlock_out;
2136
2137 count = swap_count(p->swap_map[offset]);
2138 has_cache = swap_has_cache(p->swap_map[offset]);
2139
2140 if (cache == SWAP_CACHE) { /* called for swapcache/swapin-readahead */
2141
2142 /* set SWAP_HAS_CACHE if there is no cache and entry is used */
2143 if (!has_cache && count) {
2144 p->swap_map[offset] = encode_swapmap(count, true);
2145 result = 0;
2146 } else if (has_cache) /* someone added cache */
2147 result = -EEXIST;
2148 else if (!count) /* no users */
2149 result = -ENOENT;
2150
2151 } else if (count || has_cache) {
2152 if (count < SWAP_MAP_MAX - 1) {
2153 p->swap_map[offset] = encode_swapmap(count + 1,
2154 has_cache);
2155 result = 0;
2156 } else if (count <= SWAP_MAP_MAX) {
1da177e4 2157 if (swap_overflow++ < 5)
355cfa73
KH
2158 printk(KERN_WARNING
2159 "swap_dup: swap entry overflow\n");
2160 p->swap_map[offset] = encode_swapmap(SWAP_MAP_MAX,
2161 has_cache);
2162 result = 0;
1da177e4 2163 }
355cfa73
KH
2164 } else
2165 result = -ENOENT; /* unused swap entry */
2166unlock_out:
5d337b91 2167 spin_unlock(&swap_lock);
1da177e4
LT
2168out:
2169 return result;
2170
2171bad_file:
2172 printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
2173 goto out;
2174}
355cfa73
KH
2175/*
2176 * increase reference count of swap entry by 1.
2177 */
2178void swap_duplicate(swp_entry_t entry)
2179{
2180 __swap_duplicate(entry, SWAP_MAP);
2181}
1da177e4 2182
cb4b86ba 2183/*
355cfa73
KH
2184 * @entry: swap entry for which we allocate swap cache.
2185 *
cb4b86ba 2186 * Called when allocating swap cache for exising swap entry,
355cfa73
KH
2187 * This can return error codes. Returns 0 at success.
2188 * -EBUSY means there is a swap cache.
2189 * Note: return code is different from swap_duplicate().
cb4b86ba
KH
2190 */
2191int swapcache_prepare(swp_entry_t entry)
2192{
355cfa73 2193 return __swap_duplicate(entry, SWAP_CACHE);
cb4b86ba
KH
2194}
2195
1da177e4 2196/*
5d337b91 2197 * swap_lock prevents swap_map being freed. Don't grab an extra
1da177e4
LT
2198 * reference on the swaphandle, it doesn't matter if it becomes unused.
2199 */
2200int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
2201{
8952898b 2202 struct swap_info_struct *si;
3f9e7949 2203 int our_page_cluster = page_cluster;
8952898b
HD
2204 pgoff_t target, toff;
2205 pgoff_t base, end;
2206 int nr_pages = 0;
1da177e4 2207
3f9e7949 2208 if (!our_page_cluster) /* no readahead */
1da177e4 2209 return 0;
8952898b 2210
efa90a98 2211 si = swap_info[swp_type(entry)];
8952898b
HD
2212 target = swp_offset(entry);
2213 base = (target >> our_page_cluster) << our_page_cluster;
2214 end = base + (1 << our_page_cluster);
2215 if (!base) /* first page is swap header */
2216 base++;
1da177e4 2217
5d337b91 2218 spin_lock(&swap_lock);
8952898b
HD
2219 if (end > si->max) /* don't go beyond end of map */
2220 end = si->max;
2221
2222 /* Count contiguous allocated slots above our target */
2223 for (toff = target; ++toff < end; nr_pages++) {
2224 /* Don't read in free or bad pages */
2225 if (!si->swap_map[toff])
2226 break;
355cfa73 2227 if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
1da177e4 2228 break;
8952898b
HD
2229 }
2230 /* Count contiguous allocated slots below our target */
2231 for (toff = target; --toff >= base; nr_pages++) {
1da177e4 2232 /* Don't read in free or bad pages */
8952898b 2233 if (!si->swap_map[toff])
1da177e4 2234 break;
355cfa73 2235 if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
1da177e4 2236 break;
8952898b 2237 }
5d337b91 2238 spin_unlock(&swap_lock);
8952898b
HD
2239
2240 /*
2241 * Indicate starting offset, and return number of pages to get:
2242 * if only 1, say 0, since there's then no readahead to be done.
2243 */
2244 *offset = ++toff;
2245 return nr_pages? ++nr_pages: 0;
1da177e4 2246}
This page took 0.688315 seconds and 5 git commands to generate.