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