]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - mm/swapfile.c
[PATCH] swap: scan_swap_map restyled
[mirror_ubuntu-artful-kernel.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
8#include <linux/config.h>
9#include <linux/mm.h>
10#include <linux/hugetlb.h>
11#include <linux/mman.h>
12#include <linux/slab.h>
13#include <linux/kernel_stat.h>
14#include <linux/swap.h>
15#include <linux/vmalloc.h>
16#include <linux/pagemap.h>
17#include <linux/namei.h>
18#include <linux/shm.h>
19#include <linux/blkdev.h>
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>
28#include <linux/syscalls.h>
29
30#include <asm/pgtable.h>
31#include <asm/tlbflush.h>
32#include <linux/swapops.h>
33
34DEFINE_SPINLOCK(swaplock);
35unsigned int nr_swapfiles;
36long total_swap_pages;
37static int swap_overflow;
38
39EXPORT_SYMBOL(total_swap_pages);
40
41static const char Bad_file[] = "Bad swap file entry ";
42static const char Unused_file[] = "Unused swap file entry ";
43static const char Bad_offset[] = "Bad swap offset entry ";
44static const char Unused_offset[] = "Unused swap offset entry ";
45
46struct swap_list_t swap_list = {-1, -1};
47
48struct swap_info_struct swap_info[MAX_SWAPFILES];
49
50static DECLARE_MUTEX(swapon_sem);
51
52/*
53 * We need this because the bdev->unplug_fn can sleep and we cannot
54 * hold swap_list_lock while calling the unplug_fn. And swap_list_lock
55 * cannot be turned into a semaphore.
56 */
57static DECLARE_RWSEM(swap_unplug_sem);
58
59#define SWAPFILE_CLUSTER 256
60
61void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page)
62{
63 swp_entry_t entry;
64
65 down_read(&swap_unplug_sem);
66 entry.val = page->private;
67 if (PageSwapCache(page)) {
68 struct block_device *bdev = swap_info[swp_type(entry)].bdev;
69 struct backing_dev_info *bdi;
70
71 /*
72 * If the page is removed from swapcache from under us (with a
73 * racy try_to_unuse/swapoff) we need an additional reference
74 * count to avoid reading garbage from page->private above. If
75 * the WARN_ON triggers during a swapoff it maybe the race
76 * condition and it's harmless. However if it triggers without
77 * swapoff it signals a problem.
78 */
79 WARN_ON(page_count(page) <= 1);
80
81 bdi = bdev->bd_inode->i_mapping->backing_dev_info;
ba32311e 82 blk_run_backing_dev(bdi, page);
1da177e4
LT
83 }
84 up_read(&swap_unplug_sem);
85}
86
6eb396dc 87static inline unsigned long scan_swap_map(struct swap_info_struct *si)
1da177e4 88{
7dfad418
HD
89 unsigned long offset, last_in_cluster;
90
1da177e4 91 /*
7dfad418
HD
92 * We try to cluster swap pages by allocating them sequentially
93 * in swap. Once we've allocated SWAPFILE_CLUSTER pages this
94 * way, however, we resort to first-free allocation, starting
95 * a new cluster. This prevents us from scattering swap pages
96 * all over the entire swap partition, so that we reduce
97 * overall disk seek times between swap pages. -- sct
98 * But we do now try to find an empty cluster. -Andrea
99 */
100
101 if (unlikely(!si->cluster_nr)) {
102 si->cluster_nr = SWAPFILE_CLUSTER - 1;
103 if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER)
104 goto lowest;
105
106 offset = si->lowest_bit;
107 last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
108
109 /* Locate the first empty (unaligned) cluster */
110 for (; last_in_cluster <= si->highest_bit; offset++) {
1da177e4 111 if (si->swap_map[offset])
7dfad418
HD
112 last_in_cluster = offset + SWAPFILE_CLUSTER;
113 else if (offset == last_in_cluster) {
114 si->cluster_next = offset-SWAPFILE_CLUSTER-1;
115 goto cluster;
1da177e4 116 }
7dfad418
HD
117 }
118 goto lowest;
1da177e4 119 }
7dfad418
HD
120
121 si->cluster_nr--;
122cluster:
123 offset = si->cluster_next;
124 if (offset > si->highest_bit)
125lowest: offset = si->lowest_bit;
126 if (!si->highest_bit)
127 goto no_page;
128 if (!si->swap_map[offset]) {
129got_page: if (offset == si->lowest_bit)
1da177e4
LT
130 si->lowest_bit++;
131 if (offset == si->highest_bit)
132 si->highest_bit--;
7dfad418
HD
133 si->inuse_pages++;
134 if (si->inuse_pages == si->pages) {
1da177e4
LT
135 si->lowest_bit = si->max;
136 si->highest_bit = 0;
137 }
138 si->swap_map[offset] = 1;
7dfad418 139 si->cluster_next = offset + 1;
1da177e4
LT
140 return offset;
141 }
7dfad418
HD
142
143 while (++offset <= si->highest_bit) {
144 if (!si->swap_map[offset])
145 goto got_page;
146 }
147 goto lowest;
148
149no_page:
1da177e4
LT
150 return 0;
151}
152
153swp_entry_t get_swap_page(void)
154{
fb4f88dc
HD
155 struct swap_info_struct *si;
156 pgoff_t offset;
157 int type, next;
158 int wrapped = 0;
1da177e4 159
1da177e4 160 swap_list_lock();
1da177e4 161 if (nr_swap_pages <= 0)
fb4f88dc
HD
162 goto noswap;
163 nr_swap_pages--;
164
165 for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) {
166 si = swap_info + type;
167 next = si->next;
168 if (next < 0 ||
169 (!wrapped && si->prio != swap_info[next].prio)) {
170 next = swap_list.head;
171 wrapped++;
1da177e4 172 }
fb4f88dc
HD
173
174 if (!si->highest_bit)
175 continue;
176 if (!(si->flags & SWP_WRITEOK))
177 continue;
178
179 swap_list.next = next;
180 swap_device_lock(si);
181 swap_list_unlock();
182 offset = scan_swap_map(si);
183 swap_device_unlock(si);
184 if (offset)
185 return swp_entry(type, offset);
186 swap_list_lock();
187 next = swap_list.next;
1da177e4 188 }
fb4f88dc
HD
189
190 nr_swap_pages++;
191noswap:
1da177e4 192 swap_list_unlock();
fb4f88dc 193 return (swp_entry_t) {0};
1da177e4
LT
194}
195
196static struct swap_info_struct * swap_info_get(swp_entry_t entry)
197{
198 struct swap_info_struct * p;
199 unsigned long offset, type;
200
201 if (!entry.val)
202 goto out;
203 type = swp_type(entry);
204 if (type >= nr_swapfiles)
205 goto bad_nofile;
206 p = & swap_info[type];
207 if (!(p->flags & SWP_USED))
208 goto bad_device;
209 offset = swp_offset(entry);
210 if (offset >= p->max)
211 goto bad_offset;
212 if (!p->swap_map[offset])
213 goto bad_free;
214 swap_list_lock();
1da177e4
LT
215 swap_device_lock(p);
216 return p;
217
218bad_free:
219 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
220 goto out;
221bad_offset:
222 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
223 goto out;
224bad_device:
225 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
226 goto out;
227bad_nofile:
228 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
229out:
230 return NULL;
231}
232
233static void swap_info_put(struct swap_info_struct * p)
234{
235 swap_device_unlock(p);
236 swap_list_unlock();
237}
238
239static int swap_entry_free(struct swap_info_struct *p, unsigned long offset)
240{
241 int count = p->swap_map[offset];
242
243 if (count < SWAP_MAP_MAX) {
244 count--;
245 p->swap_map[offset] = count;
246 if (!count) {
247 if (offset < p->lowest_bit)
248 p->lowest_bit = offset;
249 if (offset > p->highest_bit)
250 p->highest_bit = offset;
89d09a2c
HD
251 if (p->prio > swap_info[swap_list.next].prio)
252 swap_list.next = p - swap_info;
1da177e4
LT
253 nr_swap_pages++;
254 p->inuse_pages--;
255 }
256 }
257 return count;
258}
259
260/*
261 * Caller has made sure that the swapdevice corresponding to entry
262 * is still around or has not been recycled.
263 */
264void swap_free(swp_entry_t entry)
265{
266 struct swap_info_struct * p;
267
268 p = swap_info_get(entry);
269 if (p) {
270 swap_entry_free(p, swp_offset(entry));
271 swap_info_put(p);
272 }
273}
274
275/*
c475a8ab 276 * How many references to page are currently swapped out?
1da177e4 277 */
c475a8ab 278static inline int page_swapcount(struct page *page)
1da177e4 279{
c475a8ab
HD
280 int count = 0;
281 struct swap_info_struct *p;
1da177e4
LT
282 swp_entry_t entry;
283
284 entry.val = page->private;
285 p = swap_info_get(entry);
286 if (p) {
c475a8ab
HD
287 /* Subtract the 1 for the swap cache itself */
288 count = p->swap_map[swp_offset(entry)] - 1;
1da177e4
LT
289 swap_info_put(p);
290 }
c475a8ab 291 return count;
1da177e4
LT
292}
293
294/*
295 * We can use this swap cache entry directly
296 * if there are no other references to it.
1da177e4
LT
297 */
298int can_share_swap_page(struct page *page)
299{
c475a8ab
HD
300 int count;
301
302 BUG_ON(!PageLocked(page));
303 count = page_mapcount(page);
304 if (count <= 1 && PageSwapCache(page))
305 count += page_swapcount(page);
306 return count == 1;
1da177e4
LT
307}
308
309/*
310 * Work out if there are any other processes sharing this
311 * swap cache page. Free it if you can. Return success.
312 */
313int remove_exclusive_swap_page(struct page *page)
314{
315 int retval;
316 struct swap_info_struct * p;
317 swp_entry_t entry;
318
319 BUG_ON(PagePrivate(page));
320 BUG_ON(!PageLocked(page));
321
322 if (!PageSwapCache(page))
323 return 0;
324 if (PageWriteback(page))
325 return 0;
326 if (page_count(page) != 2) /* 2: us + cache */
327 return 0;
328
329 entry.val = page->private;
330 p = swap_info_get(entry);
331 if (!p)
332 return 0;
333
334 /* Is the only swap cache user the cache itself? */
335 retval = 0;
336 if (p->swap_map[swp_offset(entry)] == 1) {
337 /* Recheck the page count with the swapcache lock held.. */
338 write_lock_irq(&swapper_space.tree_lock);
339 if ((page_count(page) == 2) && !PageWriteback(page)) {
340 __delete_from_swap_cache(page);
341 SetPageDirty(page);
342 retval = 1;
343 }
344 write_unlock_irq(&swapper_space.tree_lock);
345 }
346 swap_info_put(p);
347
348 if (retval) {
349 swap_free(entry);
350 page_cache_release(page);
351 }
352
353 return retval;
354}
355
356/*
357 * Free the swap entry like above, but also try to
358 * free the page cache entry if it is the last user.
359 */
360void free_swap_and_cache(swp_entry_t entry)
361{
362 struct swap_info_struct * p;
363 struct page *page = NULL;
364
365 p = swap_info_get(entry);
366 if (p) {
367 if (swap_entry_free(p, swp_offset(entry)) == 1)
368 page = find_trylock_page(&swapper_space, entry.val);
369 swap_info_put(p);
370 }
371 if (page) {
372 int one_user;
373
374 BUG_ON(PagePrivate(page));
375 page_cache_get(page);
376 one_user = (page_count(page) == 2);
377 /* Only cache user (+us), or swap space full? Free it! */
378 if (!PageWriteback(page) && (one_user || vm_swap_full())) {
379 delete_from_swap_cache(page);
380 SetPageDirty(page);
381 }
382 unlock_page(page);
383 page_cache_release(page);
384 }
385}
386
387/*
388 * Always set the resulting pte to be nowrite (the same as COW pages
389 * after one process has exited). We don't know just how many PTEs will
390 * share this swap entry, so be cautious and let do_wp_page work out
391 * what to do if a write is requested later.
392 *
393 * vma->vm_mm->page_table_lock is held.
394 */
395static void unuse_pte(struct vm_area_struct *vma, pte_t *pte,
396 unsigned long addr, swp_entry_t entry, struct page *page)
397{
398 inc_mm_counter(vma->vm_mm, rss);
399 get_page(page);
400 set_pte_at(vma->vm_mm, addr, pte,
401 pte_mkold(mk_pte(page, vma->vm_page_prot)));
402 page_add_anon_rmap(page, vma, addr);
403 swap_free(entry);
404 /*
405 * Move the page to the active list so it is not
406 * immediately swapped out again after swapon.
407 */
408 activate_page(page);
409}
410
411static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
412 unsigned long addr, unsigned long end,
413 swp_entry_t entry, struct page *page)
414{
415 pte_t *pte;
416 pte_t swp_pte = swp_entry_to_pte(entry);
417
418 pte = pte_offset_map(pmd, addr);
419 do {
420 /*
421 * swapoff spends a _lot_ of time in this loop!
422 * Test inline before going to call unuse_pte.
423 */
424 if (unlikely(pte_same(*pte, swp_pte))) {
425 unuse_pte(vma, pte, addr, entry, page);
426 pte_unmap(pte);
427 return 1;
428 }
429 } while (pte++, addr += PAGE_SIZE, addr != end);
430 pte_unmap(pte - 1);
431 return 0;
432}
433
434static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
435 unsigned long addr, unsigned long end,
436 swp_entry_t entry, struct page *page)
437{
438 pmd_t *pmd;
439 unsigned long next;
440
441 pmd = pmd_offset(pud, addr);
442 do {
443 next = pmd_addr_end(addr, end);
444 if (pmd_none_or_clear_bad(pmd))
445 continue;
446 if (unuse_pte_range(vma, pmd, addr, next, entry, page))
447 return 1;
448 } while (pmd++, addr = next, addr != end);
449 return 0;
450}
451
452static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
453 unsigned long addr, unsigned long end,
454 swp_entry_t entry, struct page *page)
455{
456 pud_t *pud;
457 unsigned long next;
458
459 pud = pud_offset(pgd, addr);
460 do {
461 next = pud_addr_end(addr, end);
462 if (pud_none_or_clear_bad(pud))
463 continue;
464 if (unuse_pmd_range(vma, pud, addr, next, entry, page))
465 return 1;
466 } while (pud++, addr = next, addr != end);
467 return 0;
468}
469
470static int unuse_vma(struct vm_area_struct *vma,
471 swp_entry_t entry, struct page *page)
472{
473 pgd_t *pgd;
474 unsigned long addr, end, next;
475
476 if (page->mapping) {
477 addr = page_address_in_vma(page, vma);
478 if (addr == -EFAULT)
479 return 0;
480 else
481 end = addr + PAGE_SIZE;
482 } else {
483 addr = vma->vm_start;
484 end = vma->vm_end;
485 }
486
487 pgd = pgd_offset(vma->vm_mm, addr);
488 do {
489 next = pgd_addr_end(addr, end);
490 if (pgd_none_or_clear_bad(pgd))
491 continue;
492 if (unuse_pud_range(vma, pgd, addr, next, entry, page))
493 return 1;
494 } while (pgd++, addr = next, addr != end);
495 return 0;
496}
497
498static int unuse_mm(struct mm_struct *mm,
499 swp_entry_t entry, struct page *page)
500{
501 struct vm_area_struct *vma;
502
503 if (!down_read_trylock(&mm->mmap_sem)) {
504 /*
c475a8ab
HD
505 * Activate page so shrink_cache is unlikely to unmap its
506 * ptes while lock is dropped, so swapoff can make progress.
1da177e4 507 */
c475a8ab 508 activate_page(page);
1da177e4
LT
509 unlock_page(page);
510 down_read(&mm->mmap_sem);
511 lock_page(page);
512 }
513 spin_lock(&mm->page_table_lock);
514 for (vma = mm->mmap; vma; vma = vma->vm_next) {
515 if (vma->anon_vma && unuse_vma(vma, entry, page))
516 break;
517 }
518 spin_unlock(&mm->page_table_lock);
519 up_read(&mm->mmap_sem);
520 /*
521 * Currently unuse_mm cannot fail, but leave error handling
522 * at call sites for now, since we change it from time to time.
523 */
524 return 0;
525}
526
527/*
528 * Scan swap_map from current position to next entry still in use.
529 * Recycle to start on reaching the end, returning 0 when empty.
530 */
6eb396dc
HD
531static unsigned int find_next_to_unuse(struct swap_info_struct *si,
532 unsigned int prev)
1da177e4 533{
6eb396dc
HD
534 unsigned int max = si->max;
535 unsigned int i = prev;
1da177e4
LT
536 int count;
537
538 /*
539 * No need for swap_device_lock(si) here: we're just looking
540 * for whether an entry is in use, not modifying it; false
541 * hits are okay, and sys_swapoff() has already prevented new
542 * allocations from this area (while holding swap_list_lock()).
543 */
544 for (;;) {
545 if (++i >= max) {
546 if (!prev) {
547 i = 0;
548 break;
549 }
550 /*
551 * No entries in use at top of swap_map,
552 * loop back to start and recheck there.
553 */
554 max = prev + 1;
555 prev = 0;
556 i = 1;
557 }
558 count = si->swap_map[i];
559 if (count && count != SWAP_MAP_BAD)
560 break;
561 }
562 return i;
563}
564
565/*
566 * We completely avoid races by reading each swap page in advance,
567 * and then search for the process using it. All the necessary
568 * page table adjustments can then be made atomically.
569 */
570static int try_to_unuse(unsigned int type)
571{
572 struct swap_info_struct * si = &swap_info[type];
573 struct mm_struct *start_mm;
574 unsigned short *swap_map;
575 unsigned short swcount;
576 struct page *page;
577 swp_entry_t entry;
6eb396dc 578 unsigned int i = 0;
1da177e4
LT
579 int retval = 0;
580 int reset_overflow = 0;
581 int shmem;
582
583 /*
584 * When searching mms for an entry, a good strategy is to
585 * start at the first mm we freed the previous entry from
586 * (though actually we don't notice whether we or coincidence
587 * freed the entry). Initialize this start_mm with a hold.
588 *
589 * A simpler strategy would be to start at the last mm we
590 * freed the previous entry from; but that would take less
591 * advantage of mmlist ordering, which clusters forked mms
592 * together, child after parent. If we race with dup_mmap(), we
593 * prefer to resolve parent before child, lest we miss entries
594 * duplicated after we scanned child: using last mm would invert
595 * that. Though it's only a serious concern when an overflowed
596 * swap count is reset from SWAP_MAP_MAX, preventing a rescan.
597 */
598 start_mm = &init_mm;
599 atomic_inc(&init_mm.mm_users);
600
601 /*
602 * Keep on scanning until all entries have gone. Usually,
603 * one pass through swap_map is enough, but not necessarily:
604 * there are races when an instance of an entry might be missed.
605 */
606 while ((i = find_next_to_unuse(si, i)) != 0) {
607 if (signal_pending(current)) {
608 retval = -EINTR;
609 break;
610 }
611
612 /*
613 * Get a page for the entry, using the existing swap
614 * cache page if there is one. Otherwise, get a clean
615 * page and read the swap into it.
616 */
617 swap_map = &si->swap_map[i];
618 entry = swp_entry(type, i);
619 page = read_swap_cache_async(entry, NULL, 0);
620 if (!page) {
621 /*
622 * Either swap_duplicate() failed because entry
623 * has been freed independently, and will not be
624 * reused since sys_swapoff() already disabled
625 * allocation from here, or alloc_page() failed.
626 */
627 if (!*swap_map)
628 continue;
629 retval = -ENOMEM;
630 break;
631 }
632
633 /*
634 * Don't hold on to start_mm if it looks like exiting.
635 */
636 if (atomic_read(&start_mm->mm_users) == 1) {
637 mmput(start_mm);
638 start_mm = &init_mm;
639 atomic_inc(&init_mm.mm_users);
640 }
641
642 /*
643 * Wait for and lock page. When do_swap_page races with
644 * try_to_unuse, do_swap_page can handle the fault much
645 * faster than try_to_unuse can locate the entry. This
646 * apparently redundant "wait_on_page_locked" lets try_to_unuse
647 * defer to do_swap_page in such a case - in some tests,
648 * do_swap_page and try_to_unuse repeatedly compete.
649 */
650 wait_on_page_locked(page);
651 wait_on_page_writeback(page);
652 lock_page(page);
653 wait_on_page_writeback(page);
654
655 /*
656 * Remove all references to entry.
657 * Whenever we reach init_mm, there's no address space
658 * to search, but use it as a reminder to search shmem.
659 */
660 shmem = 0;
661 swcount = *swap_map;
662 if (swcount > 1) {
663 if (start_mm == &init_mm)
664 shmem = shmem_unuse(entry, page);
665 else
666 retval = unuse_mm(start_mm, entry, page);
667 }
668 if (*swap_map > 1) {
669 int set_start_mm = (*swap_map >= swcount);
670 struct list_head *p = &start_mm->mmlist;
671 struct mm_struct *new_start_mm = start_mm;
672 struct mm_struct *prev_mm = start_mm;
673 struct mm_struct *mm;
674
675 atomic_inc(&new_start_mm->mm_users);
676 atomic_inc(&prev_mm->mm_users);
677 spin_lock(&mmlist_lock);
678 while (*swap_map > 1 && !retval &&
679 (p = p->next) != &start_mm->mmlist) {
680 mm = list_entry(p, struct mm_struct, mmlist);
681 if (atomic_inc_return(&mm->mm_users) == 1) {
682 atomic_dec(&mm->mm_users);
683 continue;
684 }
685 spin_unlock(&mmlist_lock);
686 mmput(prev_mm);
687 prev_mm = mm;
688
689 cond_resched();
690
691 swcount = *swap_map;
692 if (swcount <= 1)
693 ;
694 else if (mm == &init_mm) {
695 set_start_mm = 1;
696 shmem = shmem_unuse(entry, page);
697 } else
698 retval = unuse_mm(mm, entry, page);
699 if (set_start_mm && *swap_map < swcount) {
700 mmput(new_start_mm);
701 atomic_inc(&mm->mm_users);
702 new_start_mm = mm;
703 set_start_mm = 0;
704 }
705 spin_lock(&mmlist_lock);
706 }
707 spin_unlock(&mmlist_lock);
708 mmput(prev_mm);
709 mmput(start_mm);
710 start_mm = new_start_mm;
711 }
712 if (retval) {
713 unlock_page(page);
714 page_cache_release(page);
715 break;
716 }
717
718 /*
719 * How could swap count reach 0x7fff when the maximum
720 * pid is 0x7fff, and there's no way to repeat a swap
721 * page within an mm (except in shmem, where it's the
722 * shared object which takes the reference count)?
723 * We believe SWAP_MAP_MAX cannot occur in Linux 2.4.
724 *
725 * If that's wrong, then we should worry more about
726 * exit_mmap() and do_munmap() cases described above:
727 * we might be resetting SWAP_MAP_MAX too early here.
728 * We know "Undead"s can happen, they're okay, so don't
729 * report them; but do report if we reset SWAP_MAP_MAX.
730 */
731 if (*swap_map == SWAP_MAP_MAX) {
732 swap_device_lock(si);
733 *swap_map = 1;
734 swap_device_unlock(si);
735 reset_overflow = 1;
736 }
737
738 /*
739 * If a reference remains (rare), we would like to leave
740 * the page in the swap cache; but try_to_unmap could
741 * then re-duplicate the entry once we drop page lock,
742 * so we might loop indefinitely; also, that page could
743 * not be swapped out to other storage meanwhile. So:
744 * delete from cache even if there's another reference,
745 * after ensuring that the data has been saved to disk -
746 * since if the reference remains (rarer), it will be
747 * read from disk into another page. Splitting into two
748 * pages would be incorrect if swap supported "shared
749 * private" pages, but they are handled by tmpfs files.
750 *
751 * Note shmem_unuse already deleted a swappage from
752 * the swap cache, unless the move to filepage failed:
753 * in which case it left swappage in cache, lowered its
754 * swap count to pass quickly through the loops above,
755 * and now we must reincrement count to try again later.
756 */
757 if ((*swap_map > 1) && PageDirty(page) && PageSwapCache(page)) {
758 struct writeback_control wbc = {
759 .sync_mode = WB_SYNC_NONE,
760 };
761
762 swap_writepage(page, &wbc);
763 lock_page(page);
764 wait_on_page_writeback(page);
765 }
766 if (PageSwapCache(page)) {
767 if (shmem)
768 swap_duplicate(entry);
769 else
770 delete_from_swap_cache(page);
771 }
772
773 /*
774 * So we could skip searching mms once swap count went
775 * to 1, we did not mark any present ptes as dirty: must
776 * mark page dirty so shrink_list will preserve it.
777 */
778 SetPageDirty(page);
779 unlock_page(page);
780 page_cache_release(page);
781
782 /*
783 * Make sure that we aren't completely killing
784 * interactive performance.
785 */
786 cond_resched();
787 }
788
789 mmput(start_mm);
790 if (reset_overflow) {
791 printk(KERN_WARNING "swapoff: cleared swap entry overflow\n");
792 swap_overflow = 0;
793 }
794 return retval;
795}
796
797/*
798 * After a successful try_to_unuse, if no swap is now in use, we know we
799 * can empty the mmlist. swap_list_lock must be held on entry and exit.
800 * Note that mmlist_lock nests inside swap_list_lock, and an mm must be
801 * added to the mmlist just after page_duplicate - before would be racy.
802 */
803static void drain_mmlist(void)
804{
805 struct list_head *p, *next;
806 unsigned int i;
807
808 for (i = 0; i < nr_swapfiles; i++)
809 if (swap_info[i].inuse_pages)
810 return;
811 spin_lock(&mmlist_lock);
812 list_for_each_safe(p, next, &init_mm.mmlist)
813 list_del_init(p);
814 spin_unlock(&mmlist_lock);
815}
816
817/*
818 * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
819 * corresponds to page offset `offset'.
820 */
821sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset)
822{
823 struct swap_extent *se = sis->curr_swap_extent;
824 struct swap_extent *start_se = se;
825
826 for ( ; ; ) {
827 struct list_head *lh;
828
829 if (se->start_page <= offset &&
830 offset < (se->start_page + se->nr_pages)) {
831 return se->start_block + (offset - se->start_page);
832 }
11d31886 833 lh = se->list.next;
1da177e4 834 if (lh == &sis->extent_list)
11d31886 835 lh = lh->next;
1da177e4
LT
836 se = list_entry(lh, struct swap_extent, list);
837 sis->curr_swap_extent = se;
838 BUG_ON(se == start_se); /* It *must* be present */
839 }
840}
841
842/*
843 * Free all of a swapdev's extent information
844 */
845static void destroy_swap_extents(struct swap_info_struct *sis)
846{
847 while (!list_empty(&sis->extent_list)) {
848 struct swap_extent *se;
849
850 se = list_entry(sis->extent_list.next,
851 struct swap_extent, list);
852 list_del(&se->list);
853 kfree(se);
854 }
1da177e4
LT
855}
856
857/*
858 * Add a block range (and the corresponding page range) into this swapdev's
11d31886 859 * extent list. The extent list is kept sorted in page order.
1da177e4 860 *
11d31886 861 * This function rather assumes that it is called in ascending page order.
1da177e4
LT
862 */
863static int
864add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
865 unsigned long nr_pages, sector_t start_block)
866{
867 struct swap_extent *se;
868 struct swap_extent *new_se;
869 struct list_head *lh;
870
11d31886
HD
871 lh = sis->extent_list.prev; /* The highest page extent */
872 if (lh != &sis->extent_list) {
1da177e4 873 se = list_entry(lh, struct swap_extent, list);
11d31886
HD
874 BUG_ON(se->start_page + se->nr_pages != start_page);
875 if (se->start_block + se->nr_pages == start_block) {
1da177e4
LT
876 /* Merge it */
877 se->nr_pages += nr_pages;
878 return 0;
879 }
1da177e4
LT
880 }
881
882 /*
883 * No merge. Insert a new extent, preserving ordering.
884 */
885 new_se = kmalloc(sizeof(*se), GFP_KERNEL);
886 if (new_se == NULL)
887 return -ENOMEM;
888 new_se->start_page = start_page;
889 new_se->nr_pages = nr_pages;
890 new_se->start_block = start_block;
891
11d31886 892 list_add_tail(&new_se->list, &sis->extent_list);
53092a74 893 return 1;
1da177e4
LT
894}
895
896/*
897 * A `swap extent' is a simple thing which maps a contiguous range of pages
898 * onto a contiguous range of disk blocks. An ordered list of swap extents
899 * is built at swapon time and is then used at swap_writepage/swap_readpage
900 * time for locating where on disk a page belongs.
901 *
902 * If the swapfile is an S_ISBLK block device, a single extent is installed.
903 * This is done so that the main operating code can treat S_ISBLK and S_ISREG
904 * swap files identically.
905 *
906 * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
907 * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
908 * swapfiles are handled *identically* after swapon time.
909 *
910 * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
911 * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
912 * some stray blocks are found which do not fall within the PAGE_SIZE alignment
913 * requirements, they are simply tossed out - we will never use those blocks
914 * for swapping.
915 *
b0d9bcd4 916 * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This
1da177e4
LT
917 * prevents root from shooting her foot off by ftruncating an in-use swapfile,
918 * which will scribble on the fs.
919 *
920 * The amount of disk space which a single swap extent represents varies.
921 * Typically it is in the 1-4 megabyte range. So we can have hundreds of
922 * extents in the list. To avoid much list walking, we cache the previous
923 * search location in `curr_swap_extent', and start new searches from there.
924 * This is extremely effective. The average number of iterations in
925 * map_swap_page() has been measured at about 0.3 per page. - akpm.
926 */
53092a74 927static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
1da177e4
LT
928{
929 struct inode *inode;
930 unsigned blocks_per_page;
931 unsigned long page_no;
932 unsigned blkbits;
933 sector_t probe_block;
934 sector_t last_block;
53092a74
HD
935 sector_t lowest_block = -1;
936 sector_t highest_block = 0;
937 int nr_extents = 0;
1da177e4
LT
938 int ret;
939
940 inode = sis->swap_file->f_mapping->host;
941 if (S_ISBLK(inode->i_mode)) {
942 ret = add_swap_extent(sis, 0, sis->max, 0);
53092a74 943 *span = sis->pages;
1da177e4
LT
944 goto done;
945 }
946
947 blkbits = inode->i_blkbits;
948 blocks_per_page = PAGE_SIZE >> blkbits;
949
950 /*
951 * Map all the blocks into the extent list. This code doesn't try
952 * to be very smart.
953 */
954 probe_block = 0;
955 page_no = 0;
956 last_block = i_size_read(inode) >> blkbits;
957 while ((probe_block + blocks_per_page) <= last_block &&
958 page_no < sis->max) {
959 unsigned block_in_page;
960 sector_t first_block;
961
962 first_block = bmap(inode, probe_block);
963 if (first_block == 0)
964 goto bad_bmap;
965
966 /*
967 * It must be PAGE_SIZE aligned on-disk
968 */
969 if (first_block & (blocks_per_page - 1)) {
970 probe_block++;
971 goto reprobe;
972 }
973
974 for (block_in_page = 1; block_in_page < blocks_per_page;
975 block_in_page++) {
976 sector_t block;
977
978 block = bmap(inode, probe_block + block_in_page);
979 if (block == 0)
980 goto bad_bmap;
981 if (block != first_block + block_in_page) {
982 /* Discontiguity */
983 probe_block++;
984 goto reprobe;
985 }
986 }
987
53092a74
HD
988 first_block >>= (PAGE_SHIFT - blkbits);
989 if (page_no) { /* exclude the header page */
990 if (first_block < lowest_block)
991 lowest_block = first_block;
992 if (first_block > highest_block)
993 highest_block = first_block;
994 }
995
1da177e4
LT
996 /*
997 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
998 */
53092a74
HD
999 ret = add_swap_extent(sis, page_no, 1, first_block);
1000 if (ret < 0)
1da177e4 1001 goto out;
53092a74 1002 nr_extents += ret;
1da177e4
LT
1003 page_no++;
1004 probe_block += blocks_per_page;
1005reprobe:
1006 continue;
1007 }
53092a74
HD
1008 ret = nr_extents;
1009 *span = 1 + highest_block - lowest_block;
1da177e4 1010 if (page_no == 0)
e2244ec2 1011 page_no = 1; /* force Empty message */
1da177e4 1012 sis->max = page_no;
e2244ec2 1013 sis->pages = page_no - 1;
1da177e4
LT
1014 sis->highest_bit = page_no - 1;
1015done:
1016 sis->curr_swap_extent = list_entry(sis->extent_list.prev,
1017 struct swap_extent, list);
1018 goto out;
1019bad_bmap:
1020 printk(KERN_ERR "swapon: swapfile has holes\n");
1021 ret = -EINVAL;
1022out:
1023 return ret;
1024}
1025
1026#if 0 /* We don't need this yet */
1027#include <linux/backing-dev.h>
1028int page_queue_congested(struct page *page)
1029{
1030 struct backing_dev_info *bdi;
1031
1032 BUG_ON(!PageLocked(page)); /* It pins the swap_info_struct */
1033
1034 if (PageSwapCache(page)) {
1035 swp_entry_t entry = { .val = page->private };
1036 struct swap_info_struct *sis;
1037
1038 sis = get_swap_info_struct(swp_type(entry));
1039 bdi = sis->bdev->bd_inode->i_mapping->backing_dev_info;
1040 } else
1041 bdi = page->mapping->backing_dev_info;
1042 return bdi_write_congested(bdi);
1043}
1044#endif
1045
1046asmlinkage long sys_swapoff(const char __user * specialfile)
1047{
1048 struct swap_info_struct * p = NULL;
1049 unsigned short *swap_map;
1050 struct file *swap_file, *victim;
1051 struct address_space *mapping;
1052 struct inode *inode;
1053 char * pathname;
1054 int i, type, prev;
1055 int err;
1056
1057 if (!capable(CAP_SYS_ADMIN))
1058 return -EPERM;
1059
1060 pathname = getname(specialfile);
1061 err = PTR_ERR(pathname);
1062 if (IS_ERR(pathname))
1063 goto out;
1064
1065 victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
1066 putname(pathname);
1067 err = PTR_ERR(victim);
1068 if (IS_ERR(victim))
1069 goto out;
1070
1071 mapping = victim->f_mapping;
1072 prev = -1;
1073 swap_list_lock();
1074 for (type = swap_list.head; type >= 0; type = swap_info[type].next) {
1075 p = swap_info + type;
1076 if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) {
1077 if (p->swap_file->f_mapping == mapping)
1078 break;
1079 }
1080 prev = type;
1081 }
1082 if (type < 0) {
1083 err = -EINVAL;
1084 swap_list_unlock();
1085 goto out_dput;
1086 }
1087 if (!security_vm_enough_memory(p->pages))
1088 vm_unacct_memory(p->pages);
1089 else {
1090 err = -ENOMEM;
1091 swap_list_unlock();
1092 goto out_dput;
1093 }
1094 if (prev < 0) {
1095 swap_list.head = p->next;
1096 } else {
1097 swap_info[prev].next = p->next;
1098 }
1099 if (type == swap_list.next) {
1100 /* just pick something that's safe... */
1101 swap_list.next = swap_list.head;
1102 }
1103 nr_swap_pages -= p->pages;
1104 total_swap_pages -= p->pages;
fb4f88dc 1105 swap_device_lock(p);
1da177e4 1106 p->flags &= ~SWP_WRITEOK;
fb4f88dc 1107 swap_device_unlock(p);
1da177e4 1108 swap_list_unlock();
fb4f88dc 1109
1da177e4
LT
1110 current->flags |= PF_SWAPOFF;
1111 err = try_to_unuse(type);
1112 current->flags &= ~PF_SWAPOFF;
1113
1114 /* wait for any unplug function to finish */
1115 down_write(&swap_unplug_sem);
1116 up_write(&swap_unplug_sem);
1117
1118 if (err) {
1119 /* re-insert swap space back into swap_list */
1120 swap_list_lock();
1121 for (prev = -1, i = swap_list.head; i >= 0; prev = i, i = swap_info[i].next)
1122 if (p->prio >= swap_info[i].prio)
1123 break;
1124 p->next = i;
1125 if (prev < 0)
1126 swap_list.head = swap_list.next = p - swap_info;
1127 else
1128 swap_info[prev].next = p - swap_info;
1129 nr_swap_pages += p->pages;
1130 total_swap_pages += p->pages;
1131 p->flags |= SWP_WRITEOK;
1132 swap_list_unlock();
1133 goto out_dput;
1134 }
4cd3bb10 1135 destroy_swap_extents(p);
1da177e4
LT
1136 down(&swapon_sem);
1137 swap_list_lock();
1138 drain_mmlist();
1139 swap_device_lock(p);
1140 swap_file = p->swap_file;
1141 p->swap_file = NULL;
1142 p->max = 0;
1143 swap_map = p->swap_map;
1144 p->swap_map = NULL;
1145 p->flags = 0;
1da177e4
LT
1146 swap_device_unlock(p);
1147 swap_list_unlock();
1148 up(&swapon_sem);
1149 vfree(swap_map);
1150 inode = mapping->host;
1151 if (S_ISBLK(inode->i_mode)) {
1152 struct block_device *bdev = I_BDEV(inode);
1153 set_blocksize(bdev, p->old_block_size);
1154 bd_release(bdev);
1155 } else {
1156 down(&inode->i_sem);
1157 inode->i_flags &= ~S_SWAPFILE;
1158 up(&inode->i_sem);
1159 }
1160 filp_close(swap_file, NULL);
1161 err = 0;
1162
1163out_dput:
1164 filp_close(victim, NULL);
1165out:
1166 return err;
1167}
1168
1169#ifdef CONFIG_PROC_FS
1170/* iterator */
1171static void *swap_start(struct seq_file *swap, loff_t *pos)
1172{
1173 struct swap_info_struct *ptr = swap_info;
1174 int i;
1175 loff_t l = *pos;
1176
1177 down(&swapon_sem);
1178
1179 for (i = 0; i < nr_swapfiles; i++, ptr++) {
1180 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1181 continue;
1182 if (!l--)
1183 return ptr;
1184 }
1185
1186 return NULL;
1187}
1188
1189static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
1190{
1191 struct swap_info_struct *ptr = v;
1192 struct swap_info_struct *endptr = swap_info + nr_swapfiles;
1193
1194 for (++ptr; ptr < endptr; ptr++) {
1195 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1196 continue;
1197 ++*pos;
1198 return ptr;
1199 }
1200
1201 return NULL;
1202}
1203
1204static void swap_stop(struct seq_file *swap, void *v)
1205{
1206 up(&swapon_sem);
1207}
1208
1209static int swap_show(struct seq_file *swap, void *v)
1210{
1211 struct swap_info_struct *ptr = v;
1212 struct file *file;
1213 int len;
1214
1215 if (v == swap_info)
1216 seq_puts(swap, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1217
1218 file = ptr->swap_file;
1219 len = seq_path(swap, file->f_vfsmnt, file->f_dentry, " \t\n\\");
6eb396dc 1220 seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
1da177e4
LT
1221 len < 40 ? 40 - len : 1, " ",
1222 S_ISBLK(file->f_dentry->d_inode->i_mode) ?
1223 "partition" : "file\t",
1224 ptr->pages << (PAGE_SHIFT - 10),
1225 ptr->inuse_pages << (PAGE_SHIFT - 10),
1226 ptr->prio);
1227 return 0;
1228}
1229
1230static struct seq_operations swaps_op = {
1231 .start = swap_start,
1232 .next = swap_next,
1233 .stop = swap_stop,
1234 .show = swap_show
1235};
1236
1237static int swaps_open(struct inode *inode, struct file *file)
1238{
1239 return seq_open(file, &swaps_op);
1240}
1241
1242static struct file_operations proc_swaps_operations = {
1243 .open = swaps_open,
1244 .read = seq_read,
1245 .llseek = seq_lseek,
1246 .release = seq_release,
1247};
1248
1249static int __init procswaps_init(void)
1250{
1251 struct proc_dir_entry *entry;
1252
1253 entry = create_proc_entry("swaps", 0, NULL);
1254 if (entry)
1255 entry->proc_fops = &proc_swaps_operations;
1256 return 0;
1257}
1258__initcall(procswaps_init);
1259#endif /* CONFIG_PROC_FS */
1260
1261/*
1262 * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
1263 *
1264 * The swapon system call
1265 */
1266asmlinkage long sys_swapon(const char __user * specialfile, int swap_flags)
1267{
1268 struct swap_info_struct * p;
1269 char *name = NULL;
1270 struct block_device *bdev = NULL;
1271 struct file *swap_file = NULL;
1272 struct address_space *mapping;
1273 unsigned int type;
1274 int i, prev;
1275 int error;
1276 static int least_priority;
1277 union swap_header *swap_header = NULL;
1278 int swap_header_version;
6eb396dc
HD
1279 unsigned int nr_good_pages = 0;
1280 int nr_extents = 0;
53092a74 1281 sector_t span;
1da177e4
LT
1282 unsigned long maxpages = 1;
1283 int swapfilesize;
1284 unsigned short *swap_map;
1285 struct page *page = NULL;
1286 struct inode *inode = NULL;
1287 int did_down = 0;
1288
1289 if (!capable(CAP_SYS_ADMIN))
1290 return -EPERM;
1291 swap_list_lock();
1292 p = swap_info;
1293 for (type = 0 ; type < nr_swapfiles ; type++,p++)
1294 if (!(p->flags & SWP_USED))
1295 break;
1296 error = -EPERM;
1297 /*
1298 * Test if adding another swap device is possible. There are
1299 * two limiting factors: 1) the number of bits for the swap
1300 * type swp_entry_t definition and 2) the number of bits for
1301 * the swap type in the swap ptes as defined by the different
1302 * architectures. To honor both limitations a swap entry
1303 * with swap offset 0 and swap type ~0UL is created, encoded
1304 * to a swap pte, decoded to a swp_entry_t again and finally
1305 * the swap type part is extracted. This will mask all bits
1306 * from the initial ~0UL that can't be encoded in either the
1307 * swp_entry_t or the architecture definition of a swap pte.
1308 */
1309 if (type > swp_type(pte_to_swp_entry(swp_entry_to_pte(swp_entry(~0UL,0))))) {
1310 swap_list_unlock();
1311 goto out;
1312 }
1313 if (type >= nr_swapfiles)
1314 nr_swapfiles = type+1;
1315 INIT_LIST_HEAD(&p->extent_list);
1316 p->flags = SWP_USED;
1da177e4
LT
1317 p->swap_file = NULL;
1318 p->old_block_size = 0;
1319 p->swap_map = NULL;
1320 p->lowest_bit = 0;
1321 p->highest_bit = 0;
1322 p->cluster_nr = 0;
1323 p->inuse_pages = 0;
1324 spin_lock_init(&p->sdev_lock);
1325 p->next = -1;
1326 if (swap_flags & SWAP_FLAG_PREFER) {
1327 p->prio =
1328 (swap_flags & SWAP_FLAG_PRIO_MASK)>>SWAP_FLAG_PRIO_SHIFT;
1329 } else {
1330 p->prio = --least_priority;
1331 }
1332 swap_list_unlock();
1333 name = getname(specialfile);
1334 error = PTR_ERR(name);
1335 if (IS_ERR(name)) {
1336 name = NULL;
1337 goto bad_swap_2;
1338 }
1339 swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
1340 error = PTR_ERR(swap_file);
1341 if (IS_ERR(swap_file)) {
1342 swap_file = NULL;
1343 goto bad_swap_2;
1344 }
1345
1346 p->swap_file = swap_file;
1347 mapping = swap_file->f_mapping;
1348 inode = mapping->host;
1349
1350 error = -EBUSY;
1351 for (i = 0; i < nr_swapfiles; i++) {
1352 struct swap_info_struct *q = &swap_info[i];
1353
1354 if (i == type || !q->swap_file)
1355 continue;
1356 if (mapping == q->swap_file->f_mapping)
1357 goto bad_swap;
1358 }
1359
1360 error = -EINVAL;
1361 if (S_ISBLK(inode->i_mode)) {
1362 bdev = I_BDEV(inode);
1363 error = bd_claim(bdev, sys_swapon);
1364 if (error < 0) {
1365 bdev = NULL;
1366 goto bad_swap;
1367 }
1368 p->old_block_size = block_size(bdev);
1369 error = set_blocksize(bdev, PAGE_SIZE);
1370 if (error < 0)
1371 goto bad_swap;
1372 p->bdev = bdev;
1373 } else if (S_ISREG(inode->i_mode)) {
1374 p->bdev = inode->i_sb->s_bdev;
1375 down(&inode->i_sem);
1376 did_down = 1;
1377 if (IS_SWAPFILE(inode)) {
1378 error = -EBUSY;
1379 goto bad_swap;
1380 }
1381 } else {
1382 goto bad_swap;
1383 }
1384
1385 swapfilesize = i_size_read(inode) >> PAGE_SHIFT;
1386
1387 /*
1388 * Read the swap header.
1389 */
1390 if (!mapping->a_ops->readpage) {
1391 error = -EINVAL;
1392 goto bad_swap;
1393 }
1394 page = read_cache_page(mapping, 0,
1395 (filler_t *)mapping->a_ops->readpage, swap_file);
1396 if (IS_ERR(page)) {
1397 error = PTR_ERR(page);
1398 goto bad_swap;
1399 }
1400 wait_on_page_locked(page);
1401 if (!PageUptodate(page))
1402 goto bad_swap;
1403 kmap(page);
1404 swap_header = page_address(page);
1405
1406 if (!memcmp("SWAP-SPACE",swap_header->magic.magic,10))
1407 swap_header_version = 1;
1408 else if (!memcmp("SWAPSPACE2",swap_header->magic.magic,10))
1409 swap_header_version = 2;
1410 else {
1411 printk("Unable to find swap-space signature\n");
1412 error = -EINVAL;
1413 goto bad_swap;
1414 }
1415
1416 switch (swap_header_version) {
1417 case 1:
1418 printk(KERN_ERR "version 0 swap is no longer supported. "
1419 "Use mkswap -v1 %s\n", name);
1420 error = -EINVAL;
1421 goto bad_swap;
1422 case 2:
1423 /* Check the swap header's sub-version and the size of
1424 the swap file and bad block lists */
1425 if (swap_header->info.version != 1) {
1426 printk(KERN_WARNING
1427 "Unable to handle swap header version %d\n",
1428 swap_header->info.version);
1429 error = -EINVAL;
1430 goto bad_swap;
1431 }
1432
1433 p->lowest_bit = 1;
1434 /*
1435 * Find out how many pages are allowed for a single swap
1436 * device. There are two limiting factors: 1) the number of
1437 * bits for the swap offset in the swp_entry_t type and
1438 * 2) the number of bits in the a swap pte as defined by
1439 * the different architectures. In order to find the
1440 * largest possible bit mask a swap entry with swap type 0
1441 * and swap offset ~0UL is created, encoded to a swap pte,
1442 * decoded to a swp_entry_t again and finally the swap
1443 * offset is extracted. This will mask all the bits from
1444 * the initial ~0UL mask that can't be encoded in either
1445 * the swp_entry_t or the architecture definition of a
1446 * swap pte.
1447 */
1448 maxpages = swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0,~0UL)))) - 1;
1449 if (maxpages > swap_header->info.last_page)
1450 maxpages = swap_header->info.last_page;
1451 p->highest_bit = maxpages - 1;
1452
1453 error = -EINVAL;
e2244ec2
HD
1454 if (!maxpages)
1455 goto bad_swap;
1456 if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
1457 goto bad_swap;
1da177e4
LT
1458 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
1459 goto bad_swap;
1460
1461 /* OK, set up the swap map and apply the bad block list */
1462 if (!(p->swap_map = vmalloc(maxpages * sizeof(short)))) {
1463 error = -ENOMEM;
1464 goto bad_swap;
1465 }
1466
1467 error = 0;
1468 memset(p->swap_map, 0, maxpages * sizeof(short));
1469 for (i=0; i<swap_header->info.nr_badpages; i++) {
1470 int page = swap_header->info.badpages[i];
1471 if (page <= 0 || page >= swap_header->info.last_page)
1472 error = -EINVAL;
1473 else
1474 p->swap_map[page] = SWAP_MAP_BAD;
1475 }
1476 nr_good_pages = swap_header->info.last_page -
1477 swap_header->info.nr_badpages -
1478 1 /* header page */;
1479 if (error)
1480 goto bad_swap;
1481 }
e2244ec2 1482
1da177e4
LT
1483 if (swapfilesize && maxpages > swapfilesize) {
1484 printk(KERN_WARNING
1485 "Swap area shorter than signature indicates\n");
1486 error = -EINVAL;
1487 goto bad_swap;
1488 }
e2244ec2
HD
1489 if (nr_good_pages) {
1490 p->swap_map[0] = SWAP_MAP_BAD;
1491 p->max = maxpages;
1492 p->pages = nr_good_pages;
53092a74
HD
1493 nr_extents = setup_swap_extents(p, &span);
1494 if (nr_extents < 0) {
1495 error = nr_extents;
e2244ec2 1496 goto bad_swap;
53092a74 1497 }
e2244ec2
HD
1498 nr_good_pages = p->pages;
1499 }
1da177e4
LT
1500 if (!nr_good_pages) {
1501 printk(KERN_WARNING "Empty swap-file\n");
1502 error = -EINVAL;
1503 goto bad_swap;
1504 }
1da177e4
LT
1505
1506 down(&swapon_sem);
1507 swap_list_lock();
1508 swap_device_lock(p);
1509 p->flags = SWP_ACTIVE;
1510 nr_swap_pages += nr_good_pages;
1511 total_swap_pages += nr_good_pages;
53092a74 1512
6eb396dc 1513 printk(KERN_INFO "Adding %uk swap on %s. "
53092a74
HD
1514 "Priority:%d extents:%d across:%lluk\n",
1515 nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
1516 nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10));
1da177e4
LT
1517
1518 /* insert swap space into swap_list: */
1519 prev = -1;
1520 for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
1521 if (p->prio >= swap_info[i].prio) {
1522 break;
1523 }
1524 prev = i;
1525 }
1526 p->next = i;
1527 if (prev < 0) {
1528 swap_list.head = swap_list.next = p - swap_info;
1529 } else {
1530 swap_info[prev].next = p - swap_info;
1531 }
1532 swap_device_unlock(p);
1533 swap_list_unlock();
1534 up(&swapon_sem);
1535 error = 0;
1536 goto out;
1537bad_swap:
1538 if (bdev) {
1539 set_blocksize(bdev, p->old_block_size);
1540 bd_release(bdev);
1541 }
4cd3bb10 1542 destroy_swap_extents(p);
1da177e4
LT
1543bad_swap_2:
1544 swap_list_lock();
1545 swap_map = p->swap_map;
1546 p->swap_file = NULL;
1547 p->swap_map = NULL;
1548 p->flags = 0;
1549 if (!(swap_flags & SWAP_FLAG_PREFER))
1550 ++least_priority;
1551 swap_list_unlock();
1da177e4
LT
1552 vfree(swap_map);
1553 if (swap_file)
1554 filp_close(swap_file, NULL);
1555out:
1556 if (page && !IS_ERR(page)) {
1557 kunmap(page);
1558 page_cache_release(page);
1559 }
1560 if (name)
1561 putname(name);
1562 if (did_down) {
1563 if (!error)
1564 inode->i_flags |= S_SWAPFILE;
1565 up(&inode->i_sem);
1566 }
1567 return error;
1568}
1569
1570void si_swapinfo(struct sysinfo *val)
1571{
1572 unsigned int i;
1573 unsigned long nr_to_be_unused = 0;
1574
1575 swap_list_lock();
1576 for (i = 0; i < nr_swapfiles; i++) {
1577 if (!(swap_info[i].flags & SWP_USED) ||
1578 (swap_info[i].flags & SWP_WRITEOK))
1579 continue;
1580 nr_to_be_unused += swap_info[i].inuse_pages;
1581 }
1582 val->freeswap = nr_swap_pages + nr_to_be_unused;
1583 val->totalswap = total_swap_pages + nr_to_be_unused;
1584 swap_list_unlock();
1585}
1586
1587/*
1588 * Verify that a swap entry is valid and increment its swap map count.
1589 *
1590 * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
1591 * "permanent", but will be reclaimed by the next swapoff.
1592 */
1593int swap_duplicate(swp_entry_t entry)
1594{
1595 struct swap_info_struct * p;
1596 unsigned long offset, type;
1597 int result = 0;
1598
1599 type = swp_type(entry);
1600 if (type >= nr_swapfiles)
1601 goto bad_file;
1602 p = type + swap_info;
1603 offset = swp_offset(entry);
1604
1605 swap_device_lock(p);
1606 if (offset < p->max && p->swap_map[offset]) {
1607 if (p->swap_map[offset] < SWAP_MAP_MAX - 1) {
1608 p->swap_map[offset]++;
1609 result = 1;
1610 } else if (p->swap_map[offset] <= SWAP_MAP_MAX) {
1611 if (swap_overflow++ < 5)
1612 printk(KERN_WARNING "swap_dup: swap entry overflow\n");
1613 p->swap_map[offset] = SWAP_MAP_MAX;
1614 result = 1;
1615 }
1616 }
1617 swap_device_unlock(p);
1618out:
1619 return result;
1620
1621bad_file:
1622 printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
1623 goto out;
1624}
1625
1626struct swap_info_struct *
1627get_swap_info_struct(unsigned type)
1628{
1629 return &swap_info[type];
1630}
1631
1632/*
1633 * swap_device_lock prevents swap_map being freed. Don't grab an extra
1634 * reference on the swaphandle, it doesn't matter if it becomes unused.
1635 */
1636int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
1637{
1638 int ret = 0, i = 1 << page_cluster;
1639 unsigned long toff;
1640 struct swap_info_struct *swapdev = swp_type(entry) + swap_info;
1641
1642 if (!page_cluster) /* no readahead */
1643 return 0;
1644 toff = (swp_offset(entry) >> page_cluster) << page_cluster;
1645 if (!toff) /* first page is swap header */
1646 toff++, i--;
1647 *offset = toff;
1648
1649 swap_device_lock(swapdev);
1650 do {
1651 /* Don't read-ahead past the end of the swap area */
1652 if (toff >= swapdev->max)
1653 break;
1654 /* Don't read in free or bad pages */
1655 if (!swapdev->swap_map[toff])
1656 break;
1657 if (swapdev->swap_map[toff] == SWAP_MAP_BAD)
1658 break;
1659 toff++;
1660 ret++;
1661 } while (--i);
1662 swap_device_unlock(swapdev);
1663 return ret;
1664}