4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 * Swap reorganised 29.12.95, Stephen Tweedie
8 #include <linux/config.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/acct.h>
28 #include <linux/backing-dev.h>
29 #include <linux/syscalls.h>
31 #include <asm/pgtable.h>
32 #include <asm/tlbflush.h>
33 #include <linux/swapops.h>
34 #include <linux/vs_memory.h>
36 DEFINE_SPINLOCK(swaplock);
37 unsigned int nr_swapfiles;
38 long total_swap_pages;
39 static int swap_overflow;
41 EXPORT_SYMBOL(total_swap_pages);
43 static const char Bad_file[] = "Bad swap file entry ";
44 static const char Unused_file[] = "Unused swap file entry ";
45 static const char Bad_offset[] = "Bad swap offset entry ";
46 static const char Unused_offset[] = "Unused swap offset entry ";
48 struct swap_list_t swap_list = {-1, -1};
50 struct swap_info_struct swap_info[MAX_SWAPFILES];
52 static DECLARE_MUTEX(swapon_sem);
55 * We need this because the bdev->unplug_fn can sleep and we cannot
56 * hold swap_list_lock while calling the unplug_fn. And swap_list_lock
57 * cannot be turned into a semaphore.
59 static DECLARE_RWSEM(swap_unplug_sem);
61 #define SWAPFILE_CLUSTER 256
63 void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page)
67 down_read(&swap_unplug_sem);
68 entry.val = page->private;
69 if (PageSwapCache(page)) {
70 struct block_device *bdev = swap_info[swp_type(entry)].bdev;
71 struct backing_dev_info *bdi;
74 * If the page is removed from swapcache from under us (with a
75 * racy try_to_unuse/swapoff) we need an additional reference
76 * count to avoid reading garbage from page->private above. If
77 * the WARN_ON triggers during a swapoff it maybe the race
78 * condition and it's harmless. However if it triggers without
79 * swapoff it signals a problem.
81 WARN_ON(page_count(page) <= 1);
83 bdi = bdev->bd_inode->i_mapping->backing_dev_info;
84 bdi->unplug_io_fn(bdi, page);
86 up_read(&swap_unplug_sem);
89 static inline int scan_swap_map(struct swap_info_struct *si)
93 * We try to cluster swap pages by allocating them
94 * sequentially in swap. Once we've allocated
95 * SWAPFILE_CLUSTER pages this way, however, we resort to
96 * first-free allocation, starting a new cluster. This
97 * prevents us from scattering swap pages all over the entire
98 * swap partition, so that we reduce overall disk seek times
99 * between swap pages. -- sct */
100 if (si->cluster_nr) {
101 while (si->cluster_next <= si->highest_bit) {
102 offset = si->cluster_next++;
103 if (si->swap_map[offset])
109 si->cluster_nr = SWAPFILE_CLUSTER;
111 /* try to find an empty (even not aligned) cluster. */
112 offset = si->lowest_bit;
114 if (offset+SWAPFILE_CLUSTER-1 <= si->highest_bit)
117 for (nr = offset; nr < offset+SWAPFILE_CLUSTER; nr++)
118 if (si->swap_map[nr])
121 goto check_next_cluster;
123 /* We found a completly empty cluster, so start
128 /* No luck, so now go finegrined as usual. -Andrea */
129 for (offset = si->lowest_bit; offset <= si->highest_bit ; offset++) {
130 if (si->swap_map[offset])
132 si->lowest_bit = offset+1;
134 if (offset == si->lowest_bit)
136 if (offset == si->highest_bit)
138 if (si->lowest_bit > si->highest_bit) {
139 si->lowest_bit = si->max;
142 si->swap_map[offset] = 1;
145 si->cluster_next = offset+1;
148 si->lowest_bit = si->max;
153 swp_entry_t get_swap_page(void)
155 struct swap_info_struct * p;
156 unsigned long offset;
158 int type, wrapped = 0;
160 entry.val = 0; /* Out of memory */
162 type = swap_list.next;
165 if (nr_swap_pages <= 0)
169 p = &swap_info[type];
170 if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) {
172 offset = scan_swap_map(p);
173 swap_device_unlock(p);
175 entry = swp_entry(type,offset);
176 type = swap_info[type].next;
178 p->prio != swap_info[type].prio) {
179 swap_list.next = swap_list.head;
181 swap_list.next = type;
188 if (type < 0 || p->prio != swap_info[type].prio) {
189 type = swap_list.head;
194 goto out; /* out of swap space */
201 static struct swap_info_struct * swap_info_get(swp_entry_t entry)
203 struct swap_info_struct * p;
204 unsigned long offset, type;
208 type = swp_type(entry);
209 if (type >= nr_swapfiles)
211 p = & swap_info[type];
212 if (!(p->flags & SWP_USED))
214 offset = swp_offset(entry);
215 if (offset >= p->max)
217 if (!p->swap_map[offset])
220 if (p->prio > swap_info[swap_list.next].prio)
221 swap_list.next = type;
226 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
229 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
232 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
235 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
240 static void swap_info_put(struct swap_info_struct * p)
242 swap_device_unlock(p);
246 static int swap_entry_free(struct swap_info_struct *p, unsigned long offset)
248 int count = p->swap_map[offset];
250 if (count < SWAP_MAP_MAX) {
252 p->swap_map[offset] = count;
254 if (offset < p->lowest_bit)
255 p->lowest_bit = offset;
256 if (offset > p->highest_bit)
257 p->highest_bit = offset;
266 * Caller has made sure that the swapdevice corresponding to entry
267 * is still around or has not been recycled.
269 void swap_free(swp_entry_t entry)
271 struct swap_info_struct * p;
273 p = swap_info_get(entry);
275 swap_entry_free(p, swp_offset(entry));
281 * Check if we're the only user of a swap page,
282 * when the page is locked.
284 static int exclusive_swap_page(struct page *page)
287 struct swap_info_struct * p;
290 entry.val = page->private;
291 p = swap_info_get(entry);
293 /* Is the only swap cache user the cache itself? */
294 if (p->swap_map[swp_offset(entry)] == 1) {
295 /* Recheck the page count with the swapcache lock held.. */
296 spin_lock_irq(&swapper_space.tree_lock);
297 if (page_count(page) == 2)
299 spin_unlock_irq(&swapper_space.tree_lock);
307 * We can use this swap cache entry directly
308 * if there are no other references to it.
310 * Here "exclusive_swap_page()" does the real
311 * work, but we opportunistically check whether
312 * we need to get all the locks first..
314 int can_share_swap_page(struct page *page)
318 if (!PageLocked(page))
320 switch (page_count(page)) {
322 if (!PagePrivate(page))
326 if (!PageSwapCache(page))
328 retval = exclusive_swap_page(page);
331 if (PageReserved(page))
339 * Work out if there are any other processes sharing this
340 * swap cache page. Free it if you can. Return success.
342 int remove_exclusive_swap_page(struct page *page)
345 struct swap_info_struct * p;
348 BUG_ON(PagePrivate(page));
349 BUG_ON(!PageLocked(page));
351 if (!PageSwapCache(page))
353 if (PageWriteback(page))
355 if (page_count(page) != 2) /* 2: us + cache */
358 entry.val = page->private;
359 p = swap_info_get(entry);
363 /* Is the only swap cache user the cache itself? */
365 if (p->swap_map[swp_offset(entry)] == 1) {
366 /* Recheck the page count with the swapcache lock held.. */
367 spin_lock_irq(&swapper_space.tree_lock);
368 if ((page_count(page) == 2) && !PageWriteback(page)) {
369 __delete_from_swap_cache(page);
373 spin_unlock_irq(&swapper_space.tree_lock);
379 page_cache_release(page);
386 * Free the swap entry like above, but also try to
387 * free the page cache entry if it is the last user.
389 void free_swap_and_cache(swp_entry_t entry)
391 struct swap_info_struct * p;
392 struct page *page = NULL;
394 p = swap_info_get(entry);
396 if (swap_entry_free(p, swp_offset(entry)) == 1) {
397 spin_lock_irq(&swapper_space.tree_lock);
398 page = radix_tree_lookup(&swapper_space.page_tree,
400 if (page && TestSetPageLocked(page))
402 spin_unlock_irq(&swapper_space.tree_lock);
409 BUG_ON(PagePrivate(page));
410 page_cache_get(page);
411 one_user = (page_count(page) == 2);
412 /* Only cache user (+us), or swap space full? Free it! */
413 if (!PageWriteback(page) && (one_user || vm_swap_full())) {
414 delete_from_swap_cache(page);
418 page_cache_release(page);
423 * The swap entry has been read in advance, and we return 1 to indicate
424 * that the page has been used or is no longer needed.
426 * Always set the resulting pte to be nowrite (the same as COW pages
427 * after one process has exited). We don't know just how many PTEs will
428 * share this swap entry, so be cautious and let do_wp_page work out
429 * what to do if a write is requested later.
431 /* vma->vm_mm->page_table_lock is held */
433 unuse_pte(struct vm_area_struct *vma, unsigned long address, pte_t *dir,
434 swp_entry_t entry, struct page *page)
436 // vma->vm_mm->rss++;
437 vx_rsspages_inc(vma->vm_mm);
439 set_pte(dir, pte_mkold(mk_pte(page, vma->vm_page_prot)));
440 page_add_anon_rmap(page, vma, address);
442 acct_update_integrals();
443 update_mem_hiwater();
446 /* vma->vm_mm->page_table_lock is held */
447 static unsigned long unuse_pmd(struct vm_area_struct *vma, pmd_t *dir,
448 unsigned long address, unsigned long end,
449 swp_entry_t entry, struct page *page)
452 pte_t swp_pte = swp_entry_to_pte(entry);
461 pte = pte_offset_map(dir, address);
464 * swapoff spends a _lot_ of time in this loop!
465 * Test inline before going to call unuse_pte.
467 if (unlikely(pte_same(*pte, swp_pte))) {
468 unuse_pte(vma, address, pte, entry, page);
472 * Move the page to the active list so it is not
473 * immediately swapped out again after swapon.
477 /* add 1 since address may be 0 */
480 address += PAGE_SIZE;
482 } while (address < end);
487 /* vma->vm_mm->page_table_lock is held */
488 static unsigned long unuse_pud(struct vm_area_struct *vma, pud_t *pud,
489 unsigned long address, unsigned long end,
490 swp_entry_t entry, struct page *page)
494 unsigned long foundaddr;
503 pmd = pmd_offset(pud, address);
505 next = (address + PMD_SIZE) & PMD_MASK;
506 if (next > end || !next)
508 foundaddr = unuse_pmd(vma, pmd, address, next, entry, page);
513 } while (address < end);
517 /* vma->vm_mm->page_table_lock is held */
518 static unsigned long unuse_pgd(struct vm_area_struct *vma, pgd_t *pgd,
519 unsigned long address, unsigned long end,
520 swp_entry_t entry, struct page *page)
524 unsigned long foundaddr;
533 pud = pud_offset(pgd, address);
535 next = (address + PUD_SIZE) & PUD_MASK;
536 if (next > end || !next)
538 foundaddr = unuse_pud(vma, pud, address, next, entry, page);
543 } while (address < end);
547 /* vma->vm_mm->page_table_lock is held */
548 static unsigned long unuse_vma(struct vm_area_struct *vma,
549 swp_entry_t entry, struct page *page)
552 unsigned long address, next, end;
553 unsigned long foundaddr;
556 address = page_address_in_vma(page, vma);
557 if (address == -EFAULT)
560 end = address + PAGE_SIZE;
562 address = vma->vm_start;
565 pgd = pgd_offset(vma->vm_mm, address);
567 next = (address + PGDIR_SIZE) & PGDIR_MASK;
568 if (next > end || !next)
570 foundaddr = unuse_pgd(vma, pgd, address, next, entry, page);
575 } while (address < end);
579 static int unuse_process(struct mm_struct * mm,
580 swp_entry_t entry, struct page* page)
582 struct vm_area_struct* vma;
583 unsigned long foundaddr = 0;
586 * Go through process' page directory.
588 if (!down_read_trylock(&mm->mmap_sem)) {
590 * Our reference to the page stops try_to_unmap_one from
591 * unmapping its ptes, so swapoff can make progress.
594 down_read(&mm->mmap_sem);
597 spin_lock(&mm->page_table_lock);
598 for (vma = mm->mmap; vma; vma = vma->vm_next) {
600 foundaddr = unuse_vma(vma, entry, page);
605 spin_unlock(&mm->page_table_lock);
606 up_read(&mm->mmap_sem);
608 * Currently unuse_process cannot fail, but leave error handling
609 * at call sites for now, since we change it from time to time.
615 * Scan swap_map from current position to next entry still in use.
616 * Recycle to start on reaching the end, returning 0 when empty.
618 static int find_next_to_unuse(struct swap_info_struct *si, int prev)
625 * No need for swap_device_lock(si) here: we're just looking
626 * for whether an entry is in use, not modifying it; false
627 * hits are okay, and sys_swapoff() has already prevented new
628 * allocations from this area (while holding swap_list_lock()).
637 * No entries in use at top of swap_map,
638 * loop back to start and recheck there.
644 count = si->swap_map[i];
645 if (count && count != SWAP_MAP_BAD)
652 * We completely avoid races by reading each swap page in advance,
653 * and then search for the process using it. All the necessary
654 * page table adjustments can then be made atomically.
656 static int try_to_unuse(unsigned int type)
658 struct swap_info_struct * si = &swap_info[type];
659 struct mm_struct *start_mm;
660 unsigned short *swap_map;
661 unsigned short swcount;
666 int reset_overflow = 0;
670 * When searching mms for an entry, a good strategy is to
671 * start at the first mm we freed the previous entry from
672 * (though actually we don't notice whether we or coincidence
673 * freed the entry). Initialize this start_mm with a hold.
675 * A simpler strategy would be to start at the last mm we
676 * freed the previous entry from; but that would take less
677 * advantage of mmlist ordering, which clusters forked mms
678 * together, child after parent. If we race with dup_mmap(), we
679 * prefer to resolve parent before child, lest we miss entries
680 * duplicated after we scanned child: using last mm would invert
681 * that. Though it's only a serious concern when an overflowed
682 * swap count is reset from SWAP_MAP_MAX, preventing a rescan.
685 atomic_inc(&init_mm.mm_users);
688 * Keep on scanning until all entries have gone. Usually,
689 * one pass through swap_map is enough, but not necessarily:
690 * there are races when an instance of an entry might be missed.
692 while ((i = find_next_to_unuse(si, i)) != 0) {
693 if (signal_pending(current)) {
699 * Get a page for the entry, using the existing swap
700 * cache page if there is one. Otherwise, get a clean
701 * page and read the swap into it.
703 swap_map = &si->swap_map[i];
704 entry = swp_entry(type, i);
705 page = read_swap_cache_async(entry, NULL, 0);
708 * Either swap_duplicate() failed because entry
709 * has been freed independently, and will not be
710 * reused since sys_swapoff() already disabled
711 * allocation from here, or alloc_page() failed.
720 * Don't hold on to start_mm if it looks like exiting.
722 if (atomic_read(&start_mm->mm_users) == 1) {
725 atomic_inc(&init_mm.mm_users);
729 * Wait for and lock page. When do_swap_page races with
730 * try_to_unuse, do_swap_page can handle the fault much
731 * faster than try_to_unuse can locate the entry. This
732 * apparently redundant "wait_on_page_locked" lets try_to_unuse
733 * defer to do_swap_page in such a case - in some tests,
734 * do_swap_page and try_to_unuse repeatedly compete.
736 wait_on_page_locked(page);
737 wait_on_page_writeback(page);
739 wait_on_page_writeback(page);
742 * Remove all references to entry.
743 * Whenever we reach init_mm, there's no address space
744 * to search, but use it as a reminder to search shmem.
749 if (start_mm == &init_mm)
750 shmem = shmem_unuse(entry, page);
752 retval = unuse_process(start_mm, entry, page);
755 int set_start_mm = (*swap_map >= swcount);
756 struct list_head *p = &start_mm->mmlist;
757 struct mm_struct *new_start_mm = start_mm;
758 struct mm_struct *prev_mm = start_mm;
759 struct mm_struct *mm;
761 atomic_inc(&new_start_mm->mm_users);
762 atomic_inc(&prev_mm->mm_users);
763 spin_lock(&mmlist_lock);
764 while (*swap_map > 1 && !retval &&
765 (p = p->next) != &start_mm->mmlist) {
766 mm = list_entry(p, struct mm_struct, mmlist);
767 if (atomic_inc_return(&mm->mm_users) == 1) {
768 atomic_dec(&mm->mm_users);
771 spin_unlock(&mmlist_lock);
780 else if (mm == &init_mm) {
782 shmem = shmem_unuse(entry, page);
784 retval = unuse_process(mm, entry, page);
785 if (set_start_mm && *swap_map < swcount) {
787 atomic_inc(&mm->mm_users);
791 spin_lock(&mmlist_lock);
793 spin_unlock(&mmlist_lock);
796 start_mm = new_start_mm;
800 page_cache_release(page);
805 * How could swap count reach 0x7fff when the maximum
806 * pid is 0x7fff, and there's no way to repeat a swap
807 * page within an mm (except in shmem, where it's the
808 * shared object which takes the reference count)?
809 * We believe SWAP_MAP_MAX cannot occur in Linux 2.4.
811 * If that's wrong, then we should worry more about
812 * exit_mmap() and do_munmap() cases described above:
813 * we might be resetting SWAP_MAP_MAX too early here.
814 * We know "Undead"s can happen, they're okay, so don't
815 * report them; but do report if we reset SWAP_MAP_MAX.
817 if (*swap_map == SWAP_MAP_MAX) {
818 swap_device_lock(si);
820 swap_device_unlock(si);
825 * If a reference remains (rare), we would like to leave
826 * the page in the swap cache; but try_to_unmap could
827 * then re-duplicate the entry once we drop page lock,
828 * so we might loop indefinitely; also, that page could
829 * not be swapped out to other storage meanwhile. So:
830 * delete from cache even if there's another reference,
831 * after ensuring that the data has been saved to disk -
832 * since if the reference remains (rarer), it will be
833 * read from disk into another page. Splitting into two
834 * pages would be incorrect if swap supported "shared
835 * private" pages, but they are handled by tmpfs files.
837 * Note shmem_unuse already deleted a swappage from
838 * the swap cache, unless the move to filepage failed:
839 * in which case it left swappage in cache, lowered its
840 * swap count to pass quickly through the loops above,
841 * and now we must reincrement count to try again later.
843 if ((*swap_map > 1) && PageDirty(page) && PageSwapCache(page)) {
844 struct writeback_control wbc = {
845 .sync_mode = WB_SYNC_NONE,
848 swap_writepage(page, &wbc);
850 wait_on_page_writeback(page);
852 if (PageSwapCache(page)) {
854 swap_duplicate(entry);
856 delete_from_swap_cache(page);
860 * So we could skip searching mms once swap count went
861 * to 1, we did not mark any present ptes as dirty: must
862 * mark page dirty so shrink_list will preserve it.
866 page_cache_release(page);
869 * Make sure that we aren't completely killing
870 * interactive performance.
876 if (reset_overflow) {
877 printk(KERN_WARNING "swapoff: cleared swap entry overflow\n");
884 * After a successful try_to_unuse, if no swap is now in use, we know we
885 * can empty the mmlist. swap_list_lock must be held on entry and exit.
886 * Note that mmlist_lock nests inside swap_list_lock, and an mm must be
887 * added to the mmlist just after page_duplicate - before would be racy.
889 static void drain_mmlist(void)
891 struct list_head *p, *next;
894 for (i = 0; i < nr_swapfiles; i++)
895 if (swap_info[i].inuse_pages)
897 spin_lock(&mmlist_lock);
898 list_for_each_safe(p, next, &init_mm.mmlist)
900 spin_unlock(&mmlist_lock);
904 * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
905 * corresponds to page offset `offset'.
907 sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset)
909 struct swap_extent *se = sis->curr_swap_extent;
910 struct swap_extent *start_se = se;
913 struct list_head *lh;
915 if (se->start_page <= offset &&
916 offset < (se->start_page + se->nr_pages)) {
917 return se->start_block + (offset - se->start_page);
920 if (lh == &sis->extent_list)
922 se = list_entry(lh, struct swap_extent, list);
923 sis->curr_swap_extent = se;
924 BUG_ON(se == start_se); /* It *must* be present */
929 * Free all of a swapdev's extent information
931 static void destroy_swap_extents(struct swap_info_struct *sis)
933 while (!list_empty(&sis->extent_list)) {
934 struct swap_extent *se;
936 se = list_entry(sis->extent_list.next,
937 struct swap_extent, list);
945 * Add a block range (and the corresponding page range) into this swapdev's
946 * extent list. The extent list is kept sorted in block order.
948 * This function rather assumes that it is called in ascending sector_t order.
949 * It doesn't look for extent coalescing opportunities.
952 add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
953 unsigned long nr_pages, sector_t start_block)
955 struct swap_extent *se;
956 struct swap_extent *new_se;
957 struct list_head *lh;
959 lh = sis->extent_list.next; /* The highest-addressed block */
960 while (lh != &sis->extent_list) {
961 se = list_entry(lh, struct swap_extent, list);
962 if (se->start_block + se->nr_pages == start_block &&
963 se->start_page + se->nr_pages == start_page) {
965 se->nr_pages += nr_pages;
972 * No merge. Insert a new extent, preserving ordering.
974 new_se = kmalloc(sizeof(*se), GFP_KERNEL);
977 new_se->start_page = start_page;
978 new_se->nr_pages = nr_pages;
979 new_se->start_block = start_block;
981 lh = sis->extent_list.prev; /* The lowest block */
982 while (lh != &sis->extent_list) {
983 se = list_entry(lh, struct swap_extent, list);
984 if (se->start_block > start_block)
988 list_add_tail(&new_se->list, lh);
994 * A `swap extent' is a simple thing which maps a contiguous range of pages
995 * onto a contiguous range of disk blocks. An ordered list of swap extents
996 * is built at swapon time and is then used at swap_writepage/swap_readpage
997 * time for locating where on disk a page belongs.
999 * If the swapfile is an S_ISBLK block device, a single extent is installed.
1000 * This is done so that the main operating code can treat S_ISBLK and S_ISREG
1001 * swap files identically.
1003 * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
1004 * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
1005 * swapfiles are handled *identically* after swapon time.
1007 * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
1008 * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
1009 * some stray blocks are found which do not fall within the PAGE_SIZE alignment
1010 * requirements, they are simply tossed out - we will never use those blocks
1013 * For S_ISREG swapfiles we hold i_sem across the life of the swapon. This
1014 * prevents root from shooting her foot off by ftruncating an in-use swapfile,
1015 * which will scribble on the fs.
1017 * The amount of disk space which a single swap extent represents varies.
1018 * Typically it is in the 1-4 megabyte range. So we can have hundreds of
1019 * extents in the list. To avoid much list walking, we cache the previous
1020 * search location in `curr_swap_extent', and start new searches from there.
1021 * This is extremely effective. The average number of iterations in
1022 * map_swap_page() has been measured at about 0.3 per page. - akpm.
1024 static int setup_swap_extents(struct swap_info_struct *sis)
1026 struct inode *inode;
1027 unsigned blocks_per_page;
1028 unsigned long page_no;
1030 sector_t probe_block;
1031 sector_t last_block;
1034 inode = sis->swap_file->f_mapping->host;
1035 if (S_ISBLK(inode->i_mode)) {
1036 ret = add_swap_extent(sis, 0, sis->max, 0);
1040 blkbits = inode->i_blkbits;
1041 blocks_per_page = PAGE_SIZE >> blkbits;
1044 * Map all the blocks into the extent list. This code doesn't try
1049 last_block = i_size_read(inode) >> blkbits;
1050 while ((probe_block + blocks_per_page) <= last_block &&
1051 page_no < sis->max) {
1052 unsigned block_in_page;
1053 sector_t first_block;
1055 first_block = bmap(inode, probe_block);
1056 if (first_block == 0)
1060 * It must be PAGE_SIZE aligned on-disk
1062 if (first_block & (blocks_per_page - 1)) {
1067 for (block_in_page = 1; block_in_page < blocks_per_page;
1071 block = bmap(inode, probe_block + block_in_page);
1074 if (block != first_block + block_in_page) {
1082 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
1084 ret = add_swap_extent(sis, page_no, 1,
1085 first_block >> (PAGE_SHIFT - blkbits));
1089 probe_block += blocks_per_page;
1097 sis->highest_bit = page_no - 1;
1099 sis->curr_swap_extent = list_entry(sis->extent_list.prev,
1100 struct swap_extent, list);
1103 printk(KERN_ERR "swapon: swapfile has holes\n");
1109 #if 0 /* We don't need this yet */
1110 #include <linux/backing-dev.h>
1111 int page_queue_congested(struct page *page)
1113 struct backing_dev_info *bdi;
1115 BUG_ON(!PageLocked(page)); /* It pins the swap_info_struct */
1117 if (PageSwapCache(page)) {
1118 swp_entry_t entry = { .val = page->private };
1119 struct swap_info_struct *sis;
1121 sis = get_swap_info_struct(swp_type(entry));
1122 bdi = sis->bdev->bd_inode->i_mapping->backing_dev_info;
1124 bdi = page->mapping->backing_dev_info;
1125 return bdi_write_congested(bdi);
1129 asmlinkage long sys_swapoff(const char __user * specialfile)
1131 struct swap_info_struct * p = NULL;
1132 unsigned short *swap_map;
1133 struct file *swap_file, *victim;
1134 struct address_space *mapping;
1135 struct inode *inode;
1140 if (!capable(CAP_SYS_ADMIN))
1143 pathname = getname(specialfile);
1144 err = PTR_ERR(pathname);
1145 if (IS_ERR(pathname))
1148 victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
1150 err = PTR_ERR(victim);
1154 mapping = victim->f_mapping;
1157 for (type = swap_list.head; type >= 0; type = swap_info[type].next) {
1158 p = swap_info + type;
1159 if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) {
1160 if (p->swap_file->f_mapping == mapping)
1170 if (!security_vm_enough_memory(p->pages))
1171 vm_unacct_memory(p->pages);
1178 swap_list.head = p->next;
1180 swap_info[prev].next = p->next;
1182 if (type == swap_list.next) {
1183 /* just pick something that's safe... */
1184 swap_list.next = swap_list.head;
1186 nr_swap_pages -= p->pages;
1187 total_swap_pages -= p->pages;
1188 p->flags &= ~SWP_WRITEOK;
1190 current->flags |= PF_SWAPOFF;
1191 err = try_to_unuse(type);
1192 current->flags &= ~PF_SWAPOFF;
1194 /* wait for any unplug function to finish */
1195 down_write(&swap_unplug_sem);
1196 up_write(&swap_unplug_sem);
1199 /* re-insert swap space back into swap_list */
1201 for (prev = -1, i = swap_list.head; i >= 0; prev = i, i = swap_info[i].next)
1202 if (p->prio >= swap_info[i].prio)
1206 swap_list.head = swap_list.next = p - swap_info;
1208 swap_info[prev].next = p - swap_info;
1209 nr_swap_pages += p->pages;
1210 total_swap_pages += p->pages;
1211 p->flags |= SWP_WRITEOK;
1218 swap_device_lock(p);
1219 swap_file = p->swap_file;
1220 p->swap_file = NULL;
1222 swap_map = p->swap_map;
1225 destroy_swap_extents(p);
1226 swap_device_unlock(p);
1230 inode = mapping->host;
1231 if (S_ISBLK(inode->i_mode)) {
1232 struct block_device *bdev = I_BDEV(inode);
1233 set_blocksize(bdev, p->old_block_size);
1236 down(&inode->i_sem);
1237 inode->i_flags &= ~S_SWAPFILE;
1240 filp_close(swap_file, NULL);
1244 filp_close(victim, NULL);
1249 #ifdef CONFIG_PROC_FS
1251 static void *swap_start(struct seq_file *swap, loff_t *pos)
1253 struct swap_info_struct *ptr = swap_info;
1259 for (i = 0; i < nr_swapfiles; i++, ptr++) {
1260 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1269 static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
1271 struct swap_info_struct *ptr = v;
1272 struct swap_info_struct *endptr = swap_info + nr_swapfiles;
1274 for (++ptr; ptr < endptr; ptr++) {
1275 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1284 static void swap_stop(struct seq_file *swap, void *v)
1289 static int swap_show(struct seq_file *swap, void *v)
1291 struct swap_info_struct *ptr = v;
1296 seq_puts(swap, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1298 file = ptr->swap_file;
1299 len = seq_path(swap, file->f_vfsmnt, file->f_dentry, " \t\n\\");
1300 seq_printf(swap, "%*s%s\t%d\t%ld\t%d\n",
1301 len < 40 ? 40 - len : 1, " ",
1302 S_ISBLK(file->f_dentry->d_inode->i_mode) ?
1303 "partition" : "file\t",
1304 ptr->pages << (PAGE_SHIFT - 10),
1305 ptr->inuse_pages << (PAGE_SHIFT - 10),
1310 static struct seq_operations swaps_op = {
1311 .start = swap_start,
1317 static int swaps_open(struct inode *inode, struct file *file)
1319 return seq_open(file, &swaps_op);
1322 static struct file_operations proc_swaps_operations = {
1325 .llseek = seq_lseek,
1326 .release = seq_release,
1329 static int __init procswaps_init(void)
1331 struct proc_dir_entry *entry;
1333 entry = create_proc_entry("swaps", 0, NULL);
1335 entry->proc_fops = &proc_swaps_operations;
1338 __initcall(procswaps_init);
1339 #endif /* CONFIG_PROC_FS */
1342 * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
1344 * The swapon system call
1346 asmlinkage long sys_swapon(const char __user * specialfile, int swap_flags)
1348 struct swap_info_struct * p;
1350 struct block_device *bdev = NULL;
1351 struct file *swap_file = NULL;
1352 struct address_space *mapping;
1356 static int least_priority;
1357 union swap_header *swap_header = NULL;
1358 int swap_header_version;
1359 int nr_good_pages = 0;
1360 unsigned long maxpages = 1;
1362 unsigned short *swap_map;
1363 struct page *page = NULL;
1364 struct inode *inode = NULL;
1367 if (!capable(CAP_SYS_ADMIN))
1371 for (type = 0 ; type < nr_swapfiles ; type++,p++)
1372 if (!(p->flags & SWP_USED))
1376 * Test if adding another swap device is possible. There are
1377 * two limiting factors: 1) the number of bits for the swap
1378 * type swp_entry_t definition and 2) the number of bits for
1379 * the swap type in the swap ptes as defined by the different
1380 * architectures. To honor both limitations a swap entry
1381 * with swap offset 0 and swap type ~0UL is created, encoded
1382 * to a swap pte, decoded to a swp_entry_t again and finally
1383 * the swap type part is extracted. This will mask all bits
1384 * from the initial ~0UL that can't be encoded in either the
1385 * swp_entry_t or the architecture definition of a swap pte.
1387 if (type > swp_type(pte_to_swp_entry(swp_entry_to_pte(swp_entry(~0UL,0))))) {
1391 if (type >= nr_swapfiles)
1392 nr_swapfiles = type+1;
1393 INIT_LIST_HEAD(&p->extent_list);
1394 p->flags = SWP_USED;
1396 p->swap_file = NULL;
1397 p->old_block_size = 0;
1403 spin_lock_init(&p->sdev_lock);
1405 if (swap_flags & SWAP_FLAG_PREFER) {
1407 (swap_flags & SWAP_FLAG_PRIO_MASK)>>SWAP_FLAG_PRIO_SHIFT;
1409 p->prio = --least_priority;
1412 name = getname(specialfile);
1413 error = PTR_ERR(name);
1418 swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
1419 error = PTR_ERR(swap_file);
1420 if (IS_ERR(swap_file)) {
1425 p->swap_file = swap_file;
1426 mapping = swap_file->f_mapping;
1427 inode = mapping->host;
1430 for (i = 0; i < nr_swapfiles; i++) {
1431 struct swap_info_struct *q = &swap_info[i];
1433 if (i == type || !q->swap_file)
1435 if (mapping == q->swap_file->f_mapping)
1440 if (S_ISBLK(inode->i_mode)) {
1441 bdev = I_BDEV(inode);
1442 error = bd_claim(bdev, sys_swapon);
1447 p->old_block_size = block_size(bdev);
1448 error = set_blocksize(bdev, PAGE_SIZE);
1452 } else if (S_ISREG(inode->i_mode)) {
1453 p->bdev = inode->i_sb->s_bdev;
1454 down(&inode->i_sem);
1456 if (IS_SWAPFILE(inode)) {
1464 swapfilesize = i_size_read(inode) >> PAGE_SHIFT;
1467 * Read the swap header.
1469 if (!mapping->a_ops->readpage) {
1473 page = read_cache_page(mapping, 0,
1474 (filler_t *)mapping->a_ops->readpage, swap_file);
1476 error = PTR_ERR(page);
1479 wait_on_page_locked(page);
1480 if (!PageUptodate(page))
1483 swap_header = page_address(page);
1485 if (!memcmp("SWAP-SPACE",swap_header->magic.magic,10))
1486 swap_header_version = 1;
1487 else if (!memcmp("SWAPSPACE2",swap_header->magic.magic,10))
1488 swap_header_version = 2;
1490 printk("Unable to find swap-space signature\n");
1495 switch (swap_header_version) {
1497 printk(KERN_ERR "version 0 swap is no longer supported. "
1498 "Use mkswap -v1 %s\n", name);
1502 /* Check the swap header's sub-version and the size of
1503 the swap file and bad block lists */
1504 if (swap_header->info.version != 1) {
1506 "Unable to handle swap header version %d\n",
1507 swap_header->info.version);
1514 * Find out how many pages are allowed for a single swap
1515 * device. There are two limiting factors: 1) the number of
1516 * bits for the swap offset in the swp_entry_t type and
1517 * 2) the number of bits in the a swap pte as defined by
1518 * the different architectures. In order to find the
1519 * largest possible bit mask a swap entry with swap type 0
1520 * and swap offset ~0UL is created, encoded to a swap pte,
1521 * decoded to a swp_entry_t again and finally the swap
1522 * offset is extracted. This will mask all the bits from
1523 * the initial ~0UL mask that can't be encoded in either
1524 * the swp_entry_t or the architecture definition of a
1527 maxpages = swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0,~0UL)))) - 1;
1528 if (maxpages > swap_header->info.last_page)
1529 maxpages = swap_header->info.last_page;
1530 p->highest_bit = maxpages - 1;
1533 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
1536 /* OK, set up the swap map and apply the bad block list */
1537 if (!(p->swap_map = vmalloc(maxpages * sizeof(short)))) {
1543 memset(p->swap_map, 0, maxpages * sizeof(short));
1544 for (i=0; i<swap_header->info.nr_badpages; i++) {
1545 int page = swap_header->info.badpages[i];
1546 if (page <= 0 || page >= swap_header->info.last_page)
1549 p->swap_map[page] = SWAP_MAP_BAD;
1551 nr_good_pages = swap_header->info.last_page -
1552 swap_header->info.nr_badpages -
1553 1 /* header page */;
1558 if (swapfilesize && maxpages > swapfilesize) {
1560 "Swap area shorter than signature indicates\n");
1564 if (!nr_good_pages) {
1565 printk(KERN_WARNING "Empty swap-file\n");
1569 p->swap_map[0] = SWAP_MAP_BAD;
1571 p->pages = nr_good_pages;
1573 error = setup_swap_extents(p);
1579 swap_device_lock(p);
1580 p->flags = SWP_ACTIVE;
1581 nr_swap_pages += nr_good_pages;
1582 total_swap_pages += nr_good_pages;
1583 printk(KERN_INFO "Adding %dk swap on %s. Priority:%d extents:%d\n",
1584 nr_good_pages<<(PAGE_SHIFT-10), name,
1585 p->prio, p->nr_extents);
1587 /* insert swap space into swap_list: */
1589 for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
1590 if (p->prio >= swap_info[i].prio) {
1597 swap_list.head = swap_list.next = p - swap_info;
1599 swap_info[prev].next = p - swap_info;
1601 swap_device_unlock(p);
1608 set_blocksize(bdev, p->old_block_size);
1613 swap_map = p->swap_map;
1614 p->swap_file = NULL;
1617 if (!(swap_flags & SWAP_FLAG_PREFER))
1620 destroy_swap_extents(p);
1623 filp_close(swap_file, NULL);
1625 if (page && !IS_ERR(page)) {
1627 page_cache_release(page);
1633 inode->i_flags |= S_SWAPFILE;
1639 void si_swapinfo(struct sysinfo *val)
1642 unsigned long nr_to_be_unused = 0;
1645 for (i = 0; i < nr_swapfiles; i++) {
1646 if (!(swap_info[i].flags & SWP_USED) ||
1647 (swap_info[i].flags & SWP_WRITEOK))
1649 nr_to_be_unused += swap_info[i].inuse_pages;
1651 val->freeswap = nr_swap_pages + nr_to_be_unused;
1652 val->totalswap = total_swap_pages + nr_to_be_unused;
1654 if (vx_flags(VXF_VIRT_MEM, 0))
1655 vx_vsi_swapinfo(val);
1659 * Verify that a swap entry is valid and increment its swap map count.
1661 * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
1662 * "permanent", but will be reclaimed by the next swapoff.
1664 int swap_duplicate(swp_entry_t entry)
1666 struct swap_info_struct * p;
1667 unsigned long offset, type;
1670 type = swp_type(entry);
1671 if (type >= nr_swapfiles)
1673 p = type + swap_info;
1674 offset = swp_offset(entry);
1676 swap_device_lock(p);
1677 if (offset < p->max && p->swap_map[offset]) {
1678 if (p->swap_map[offset] < SWAP_MAP_MAX - 1) {
1679 p->swap_map[offset]++;
1681 } else if (p->swap_map[offset] <= SWAP_MAP_MAX) {
1682 if (swap_overflow++ < 5)
1683 printk(KERN_WARNING "swap_dup: swap entry overflow\n");
1684 p->swap_map[offset] = SWAP_MAP_MAX;
1688 swap_device_unlock(p);
1693 printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
1697 struct swap_info_struct *
1698 get_swap_info_struct(unsigned type)
1700 return &swap_info[type];
1704 * swap_device_lock prevents swap_map being freed. Don't grab an extra
1705 * reference on the swaphandle, it doesn't matter if it becomes unused.
1707 int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
1709 int ret = 0, i = 1 << page_cluster;
1711 struct swap_info_struct *swapdev = swp_type(entry) + swap_info;
1713 if (!page_cluster) /* no readahead */
1715 toff = (swp_offset(entry) >> page_cluster) << page_cluster;
1716 if (!toff) /* first page is swap header */
1720 swap_device_lock(swapdev);
1722 /* Don't read-ahead past the end of the swap area */
1723 if (toff >= swapdev->max)
1725 /* Don't read in free or bad pages */
1726 if (!swapdev->swap_map[toff])
1728 if (swapdev->swap_map[toff] == SWAP_MAP_BAD)
1733 swap_device_unlock(swapdev);