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