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/backing-dev.h>
29 #include <asm/pgtable.h>
30 #include <asm/tlbflush.h>
31 #include <linux/swapops.h>
32 #include <linux/vs_base.h>
33 #include <linux/vs_memory.h>
35 spinlock_t swaplock = SPIN_LOCK_UNLOCKED;
36 unsigned int nr_swapfiles;
37 long total_swap_pages;
38 static int swap_overflow;
40 EXPORT_SYMBOL(total_swap_pages);
42 static const char Bad_file[] = "Bad swap file entry ";
43 static const char Unused_file[] = "Unused swap file entry ";
44 static const char Bad_offset[] = "Bad swap offset entry ";
45 static const char Unused_offset[] = "Unused swap offset entry ";
47 struct swap_list_t swap_list = {-1, -1};
49 struct swap_info_struct swap_info[MAX_SWAPFILES];
51 static DECLARE_MUTEX(swapon_sem);
54 * We need this because the bdev->unplug_fn can sleep and we cannot
55 * hold swap_list_lock while calling the unplug_fn. And swap_list_lock
56 * cannot be turned into a semaphore.
58 static DECLARE_RWSEM(swap_unplug_sem);
60 #define SWAPFILE_CLUSTER 256
62 void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page)
66 down_read(&swap_unplug_sem);
67 entry.val = page->private;
68 if (PageSwapCache(page)) {
69 struct block_device *bdev = swap_info[swp_type(entry)].bdev;
70 struct backing_dev_info *bdi;
73 * If the page is removed from swapcache from under us (with a
74 * racy try_to_unuse/swapoff) we need an additional reference
75 * count to avoid reading garbage from page->private above. If
76 * the WARN_ON triggers during a swapoff it maybe the race
77 * condition and it's harmless. However if it triggers without
78 * swapoff it signals a problem.
80 WARN_ON(page_count(page) <= 1);
82 bdi = bdev->bd_inode->i_mapping->backing_dev_info;
83 bdi->unplug_io_fn(bdi, page);
85 up_read(&swap_unplug_sem);
88 static inline int scan_swap_map(struct swap_info_struct *si)
92 * We try to cluster swap pages by allocating them
93 * sequentially in swap. Once we've allocated
94 * SWAPFILE_CLUSTER pages this way, however, we resort to
95 * first-free allocation, starting a new cluster. This
96 * prevents us from scattering swap pages all over the entire
97 * swap partition, so that we reduce overall disk seek times
98 * between swap pages. -- sct */
100 while (si->cluster_next <= si->highest_bit) {
101 offset = si->cluster_next++;
102 if (si->swap_map[offset])
108 si->cluster_nr = SWAPFILE_CLUSTER;
110 /* try to find an empty (even not aligned) cluster. */
111 offset = si->lowest_bit;
113 if (offset+SWAPFILE_CLUSTER-1 <= si->highest_bit)
116 for (nr = offset; nr < offset+SWAPFILE_CLUSTER; nr++)
117 if (si->swap_map[nr])
120 goto check_next_cluster;
122 /* We found a completly empty cluster, so start
127 /* No luck, so now go finegrined as usual. -Andrea */
128 for (offset = si->lowest_bit; offset <= si->highest_bit ; offset++) {
129 if (si->swap_map[offset])
131 si->lowest_bit = offset+1;
133 if (offset == si->lowest_bit)
135 if (offset == si->highest_bit)
137 if (si->lowest_bit > si->highest_bit) {
138 si->lowest_bit = si->max;
141 si->swap_map[offset] = 1;
144 si->cluster_next = offset+1;
147 si->lowest_bit = si->max;
152 swp_entry_t get_swap_page(void)
154 struct swap_info_struct * p;
155 unsigned long offset;
157 int type, wrapped = 0;
159 entry.val = 0; /* Out of memory */
161 type = swap_list.next;
164 if (nr_swap_pages <= 0)
168 p = &swap_info[type];
169 if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) {
171 offset = scan_swap_map(p);
172 swap_device_unlock(p);
174 entry = swp_entry(type,offset);
175 type = swap_info[type].next;
177 p->prio != swap_info[type].prio) {
178 swap_list.next = swap_list.head;
180 swap_list.next = type;
187 if (type < 0 || p->prio != swap_info[type].prio) {
188 type = swap_list.head;
193 goto out; /* out of swap space */
200 static struct swap_info_struct * swap_info_get(swp_entry_t entry)
202 struct swap_info_struct * p;
203 unsigned long offset, type;
207 type = swp_type(entry);
208 if (type >= nr_swapfiles)
210 p = & swap_info[type];
211 if (!(p->flags & SWP_USED))
213 offset = swp_offset(entry);
214 if (offset >= p->max)
216 if (!p->swap_map[offset])
219 if (p->prio > swap_info[swap_list.next].prio)
220 swap_list.next = type;
225 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
228 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
231 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
234 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
239 static void swap_info_put(struct swap_info_struct * p)
241 swap_device_unlock(p);
245 static int swap_entry_free(struct swap_info_struct *p, unsigned long offset)
247 int count = p->swap_map[offset];
249 if (count < SWAP_MAP_MAX) {
251 p->swap_map[offset] = count;
253 if (offset < p->lowest_bit)
254 p->lowest_bit = offset;
255 if (offset > p->highest_bit)
256 p->highest_bit = offset;
265 * Caller has made sure that the swapdevice corresponding to entry
266 * is still around or has not been recycled.
268 void swap_free(swp_entry_t entry)
270 struct swap_info_struct * p;
272 p = swap_info_get(entry);
274 swap_entry_free(p, swp_offset(entry));
280 * Check if we're the only user of a swap page,
281 * when the page is locked.
283 static int exclusive_swap_page(struct page *page)
286 struct swap_info_struct * p;
289 entry.val = page->private;
290 p = swap_info_get(entry);
292 /* Is the only swap cache user the cache itself? */
293 if (p->swap_map[swp_offset(entry)] == 1) {
294 /* Recheck the page count with the swapcache lock held.. */
295 spin_lock_irq(&swapper_space.tree_lock);
296 if (page_count(page) == 2)
298 spin_unlock_irq(&swapper_space.tree_lock);
306 * We can use this swap cache entry directly
307 * if there are no other references to it.
309 * Here "exclusive_swap_page()" does the real
310 * work, but we opportunistically check whether
311 * we need to get all the locks first..
313 int can_share_swap_page(struct page *page)
317 if (!PageLocked(page))
319 switch (page_count(page)) {
321 if (!PagePrivate(page))
325 if (!PageSwapCache(page))
327 retval = exclusive_swap_page(page);
330 if (PageReserved(page))
338 * Work out if there are any other processes sharing this
339 * swap cache page. Free it if you can. Return success.
341 int remove_exclusive_swap_page(struct page *page)
344 struct swap_info_struct * p;
347 BUG_ON(PagePrivate(page));
348 BUG_ON(!PageLocked(page));
350 if (!PageSwapCache(page))
352 if (PageWriteback(page))
354 if (page_count(page) != 2) /* 2: us + cache */
357 entry.val = page->private;
358 p = swap_info_get(entry);
362 /* Is the only swap cache user the cache itself? */
364 if (p->swap_map[swp_offset(entry)] == 1) {
365 /* Recheck the page count with the swapcache lock held.. */
366 spin_lock_irq(&swapper_space.tree_lock);
367 if ((page_count(page) == 2) && !PageWriteback(page)) {
368 __delete_from_swap_cache(page);
372 spin_unlock_irq(&swapper_space.tree_lock);
378 page_cache_release(page);
385 * Free the swap entry like above, but also try to
386 * free the page cache entry if it is the last user.
388 void free_swap_and_cache(swp_entry_t entry)
390 struct swap_info_struct * p;
391 struct page *page = NULL;
393 p = swap_info_get(entry);
395 if (swap_entry_free(p, swp_offset(entry)) == 1) {
396 spin_lock_irq(&swapper_space.tree_lock);
397 page = radix_tree_lookup(&swapper_space.page_tree,
399 if (page && TestSetPageLocked(page))
401 spin_unlock_irq(&swapper_space.tree_lock);
408 BUG_ON(PagePrivate(page));
409 page_cache_get(page);
410 one_user = (page_count(page) == 2);
411 /* Only cache user (+us), or swap space full? Free it! */
412 if (!PageWriteback(page) && (one_user || vm_swap_full())) {
413 delete_from_swap_cache(page);
417 page_cache_release(page);
422 * The swap entry has been read in advance, and we return 1 to indicate
423 * that the page has been used or is no longer needed.
425 * Always set the resulting pte to be nowrite (the same as COW pages
426 * after one process has exited). We don't know just how many PTEs will
427 * share this swap entry, so be cautious and let do_wp_page work out
428 * what to do if a write is requested later.
430 /* vma->vm_mm->page_table_lock is held */
432 unuse_pte(struct vm_area_struct *vma, unsigned long address, pte_t *dir,
433 swp_entry_t entry, struct page *page)
435 // vma->vm_mm->rss++;
436 vx_rsspages_inc(vma->vm_mm);
438 set_pte(dir, pte_mkold(mk_pte(page, vma->vm_page_prot)));
439 page_add_anon_rmap(page, vma, address);
443 /* vma->vm_mm->page_table_lock is held */
444 static unsigned long unuse_pmd(struct vm_area_struct * vma, pmd_t *dir,
445 unsigned long address, unsigned long size, unsigned long offset,
446 swp_entry_t entry, struct page *page)
450 pte_t swp_pte = swp_entry_to_pte(entry);
459 pte = pte_offset_map(dir, address);
460 offset += address & PMD_MASK;
461 address &= ~PMD_MASK;
462 end = address + size;
467 * swapoff spends a _lot_ of time in this loop!
468 * Test inline before going to call unuse_pte.
470 if (unlikely(pte_same(*pte, swp_pte))) {
471 unuse_pte(vma, offset + address, pte, entry, page);
475 * Move the page to the active list so it is not
476 * immediately swapped out again after swapon.
480 /* add 1 since address may be 0 */
481 return 1 + offset + address;
483 address += PAGE_SIZE;
485 } while (address && (address < end));
490 /* vma->vm_mm->page_table_lock is held */
491 static unsigned long unuse_pgd(struct vm_area_struct * vma, pgd_t *dir,
492 unsigned long address, unsigned long size,
493 swp_entry_t entry, struct page *page)
496 unsigned long offset, end;
497 unsigned long foundaddr;
506 pmd = pmd_offset(dir, address);
507 offset = address & PGDIR_MASK;
508 address &= ~PGDIR_MASK;
509 end = address + size;
510 if (end > PGDIR_SIZE)
515 foundaddr = unuse_pmd(vma, pmd, address, end - address,
516 offset, entry, page);
519 address = (address + PMD_SIZE) & PMD_MASK;
521 } while (address && (address < end));
525 /* vma->vm_mm->page_table_lock is held */
526 static unsigned long unuse_vma(struct vm_area_struct * vma, pgd_t *pgdir,
527 swp_entry_t entry, struct page *page)
529 unsigned long start = vma->vm_start, end = vma->vm_end;
530 unsigned long foundaddr;
535 foundaddr = unuse_pgd(vma, pgdir, start, end - start,
539 start = (start + PGDIR_SIZE) & PGDIR_MASK;
541 } while (start && (start < end));
545 static int unuse_process(struct mm_struct * mm,
546 swp_entry_t entry, struct page* page)
548 struct vm_area_struct* vma;
549 unsigned long foundaddr = 0;
552 * Go through process' page directory.
554 if (!down_read_trylock(&mm->mmap_sem)) {
556 * Our reference to the page stops try_to_unmap_one from
557 * unmapping its ptes, so swapoff can make progress.
560 down_read(&mm->mmap_sem);
563 spin_lock(&mm->page_table_lock);
564 for (vma = mm->mmap; vma; vma = vma->vm_next) {
565 if (!is_vm_hugetlb_page(vma)) {
566 pgd_t * pgd = pgd_offset(mm, vma->vm_start);
567 foundaddr = unuse_vma(vma, pgd, entry, page);
572 spin_unlock(&mm->page_table_lock);
573 up_read(&mm->mmap_sem);
575 * Currently unuse_process cannot fail, but leave error handling
576 * at call sites for now, since we change it from time to time.
582 * Scan swap_map from current position to next entry still in use.
583 * Recycle to start on reaching the end, returning 0 when empty.
585 static int find_next_to_unuse(struct swap_info_struct *si, int prev)
592 * No need for swap_device_lock(si) here: we're just looking
593 * for whether an entry is in use, not modifying it; false
594 * hits are okay, and sys_swapoff() has already prevented new
595 * allocations from this area (while holding swap_list_lock()).
604 * No entries in use at top of swap_map,
605 * loop back to start and recheck there.
611 count = si->swap_map[i];
612 if (count && count != SWAP_MAP_BAD)
619 * We completely avoid races by reading each swap page in advance,
620 * and then search for the process using it. All the necessary
621 * page table adjustments can then be made atomically.
623 static int try_to_unuse(unsigned int type)
625 struct swap_info_struct * si = &swap_info[type];
626 struct mm_struct *start_mm;
627 unsigned short *swap_map;
628 unsigned short swcount;
633 int reset_overflow = 0;
637 * When searching mms for an entry, a good strategy is to
638 * start at the first mm we freed the previous entry from
639 * (though actually we don't notice whether we or coincidence
640 * freed the entry). Initialize this start_mm with a hold.
642 * A simpler strategy would be to start at the last mm we
643 * freed the previous entry from; but that would take less
644 * advantage of mmlist ordering (now preserved by swap_out()),
645 * which clusters forked address spaces together, most recent
646 * child immediately after parent. If we race with dup_mmap(),
647 * we very much want to resolve parent before child, otherwise
648 * we may miss some entries: using last mm would invert that.
651 atomic_inc(&init_mm.mm_users);
654 * Keep on scanning until all entries have gone. Usually,
655 * one pass through swap_map is enough, but not necessarily:
656 * mmput() removes mm from mmlist before exit_mmap() and its
657 * zap_page_range(). That's not too bad, those entries are
658 * on their way out, and handled faster there than here.
659 * do_munmap() behaves similarly, taking the range out of mm's
660 * vma list before zap_page_range(). But unfortunately, when
661 * unmapping a part of a vma, it takes the whole out first,
662 * then reinserts what's left after (might even reschedule if
663 * open() method called) - so swap entries may be invisible
664 * to swapoff for a while, then reappear - but that is rare.
666 while ((i = find_next_to_unuse(si, i)) != 0) {
667 if (signal_pending(current)) {
673 * Get a page for the entry, using the existing swap
674 * cache page if there is one. Otherwise, get a clean
675 * page and read the swap into it.
677 swap_map = &si->swap_map[i];
678 entry = swp_entry(type, i);
679 page = read_swap_cache_async(entry, NULL, 0);
682 * Either swap_duplicate() failed because entry
683 * has been freed independently, and will not be
684 * reused since sys_swapoff() already disabled
685 * allocation from here, or alloc_page() failed.
694 * Don't hold on to start_mm if it looks like exiting.
696 if (atomic_read(&start_mm->mm_users) == 1) {
699 atomic_inc(&init_mm.mm_users);
703 * Wait for and lock page. When do_swap_page races with
704 * try_to_unuse, do_swap_page can handle the fault much
705 * faster than try_to_unuse can locate the entry. This
706 * apparently redundant "wait_on_page_locked" lets try_to_unuse
707 * defer to do_swap_page in such a case - in some tests,
708 * do_swap_page and try_to_unuse repeatedly compete.
710 wait_on_page_locked(page);
711 wait_on_page_writeback(page);
713 wait_on_page_writeback(page);
716 * Remove all references to entry, without blocking.
717 * Whenever we reach init_mm, there's no address space
718 * to search, but use it as a reminder to search shmem.
723 if (start_mm == &init_mm)
724 shmem = shmem_unuse(entry, page);
726 retval = unuse_process(start_mm, entry, page);
729 int set_start_mm = (*swap_map >= swcount);
730 struct list_head *p = &start_mm->mmlist;
731 struct mm_struct *new_start_mm = start_mm;
732 struct mm_struct *prev_mm = start_mm;
733 struct mm_struct *mm;
735 atomic_inc(&new_start_mm->mm_users);
736 atomic_inc(&prev_mm->mm_users);
737 spin_lock(&mmlist_lock);
738 while (*swap_map > 1 && !retval &&
739 (p = p->next) != &start_mm->mmlist) {
740 mm = list_entry(p, struct mm_struct, mmlist);
741 atomic_inc(&mm->mm_users);
742 spin_unlock(&mmlist_lock);
751 else if (mm == &init_mm) {
753 shmem = shmem_unuse(entry, page);
755 retval = unuse_process(mm, entry, page);
756 if (set_start_mm && *swap_map < swcount) {
758 atomic_inc(&mm->mm_users);
762 spin_lock(&mmlist_lock);
764 spin_unlock(&mmlist_lock);
767 start_mm = new_start_mm;
771 page_cache_release(page);
776 * How could swap count reach 0x7fff when the maximum
777 * pid is 0x7fff, and there's no way to repeat a swap
778 * page within an mm (except in shmem, where it's the
779 * shared object which takes the reference count)?
780 * We believe SWAP_MAP_MAX cannot occur in Linux 2.4.
782 * If that's wrong, then we should worry more about
783 * exit_mmap() and do_munmap() cases described above:
784 * we might be resetting SWAP_MAP_MAX too early here.
785 * We know "Undead"s can happen, they're okay, so don't
786 * report them; but do report if we reset SWAP_MAP_MAX.
788 if (*swap_map == SWAP_MAP_MAX) {
789 swap_device_lock(si);
791 swap_device_unlock(si);
796 * If a reference remains (rare), we would like to leave
797 * the page in the swap cache; but try_to_unmap could
798 * then re-duplicate the entry once we drop page lock,
799 * so we might loop indefinitely; also, that page could
800 * not be swapped out to other storage meanwhile. So:
801 * delete from cache even if there's another reference,
802 * after ensuring that the data has been saved to disk -
803 * since if the reference remains (rarer), it will be
804 * read from disk into another page. Splitting into two
805 * pages would be incorrect if swap supported "shared
806 * private" pages, but they are handled by tmpfs files.
808 * Note shmem_unuse already deleted a swappage from
809 * the swap cache, unless the move to filepage failed:
810 * in which case it left swappage in cache, lowered its
811 * swap count to pass quickly through the loops above,
812 * and now we must reincrement count to try again later.
814 if ((*swap_map > 1) && PageDirty(page) && PageSwapCache(page)) {
815 struct writeback_control wbc = {
816 .sync_mode = WB_SYNC_NONE,
819 swap_writepage(page, &wbc);
821 wait_on_page_writeback(page);
823 if (PageSwapCache(page)) {
825 swap_duplicate(entry);
827 delete_from_swap_cache(page);
831 * So we could skip searching mms once swap count went
832 * to 1, we did not mark any present ptes as dirty: must
833 * mark page dirty so shrink_list will preserve it.
837 page_cache_release(page);
840 * Make sure that we aren't completely killing
841 * interactive performance.
847 if (reset_overflow) {
848 printk(KERN_WARNING "swapoff: cleared swap entry overflow\n");
855 * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
856 * corresponds to page offset `offset'.
858 sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset)
860 struct swap_extent *se = sis->curr_swap_extent;
861 struct swap_extent *start_se = se;
864 struct list_head *lh;
866 if (se->start_page <= offset &&
867 offset < (se->start_page + se->nr_pages)) {
868 return se->start_block + (offset - se->start_page);
871 if (lh == &sis->extent_list)
873 se = list_entry(lh, struct swap_extent, list);
874 sis->curr_swap_extent = se;
875 BUG_ON(se == start_se); /* It *must* be present */
880 * Free all of a swapdev's extent information
882 static void destroy_swap_extents(struct swap_info_struct *sis)
884 while (!list_empty(&sis->extent_list)) {
885 struct swap_extent *se;
887 se = list_entry(sis->extent_list.next,
888 struct swap_extent, list);
896 * Add a block range (and the corresponding page range) into this swapdev's
897 * extent list. The extent list is kept sorted in block order.
899 * This function rather assumes that it is called in ascending sector_t order.
900 * It doesn't look for extent coalescing opportunities.
903 add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
904 unsigned long nr_pages, sector_t start_block)
906 struct swap_extent *se;
907 struct swap_extent *new_se;
908 struct list_head *lh;
910 lh = sis->extent_list.next; /* The highest-addressed block */
911 while (lh != &sis->extent_list) {
912 se = list_entry(lh, struct swap_extent, list);
913 if (se->start_block + se->nr_pages == start_block &&
914 se->start_page + se->nr_pages == start_page) {
916 se->nr_pages += nr_pages;
923 * No merge. Insert a new extent, preserving ordering.
925 new_se = kmalloc(sizeof(*se), GFP_KERNEL);
928 new_se->start_page = start_page;
929 new_se->nr_pages = nr_pages;
930 new_se->start_block = start_block;
932 lh = sis->extent_list.prev; /* The lowest block */
933 while (lh != &sis->extent_list) {
934 se = list_entry(lh, struct swap_extent, list);
935 if (se->start_block > start_block)
939 list_add_tail(&new_se->list, lh);
945 * A `swap extent' is a simple thing which maps a contiguous range of pages
946 * onto a contiguous range of disk blocks. An ordered list of swap extents
947 * is built at swapon time and is then used at swap_writepage/swap_readpage
948 * time for locating where on disk a page belongs.
950 * If the swapfile is an S_ISBLK block device, a single extent is installed.
951 * This is done so that the main operating code can treat S_ISBLK and S_ISREG
952 * swap files identically.
954 * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
955 * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
956 * swapfiles are handled *identically* after swapon time.
958 * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
959 * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
960 * some stray blocks are found which do not fall within the PAGE_SIZE alignment
961 * requirements, they are simply tossed out - we will never use those blocks
964 * For S_ISREG swapfiles we hold i_sem across the life of the swapon. This
965 * prevents root from shooting her foot off by ftruncating an in-use swapfile,
966 * which will scribble on the fs.
968 * The amount of disk space which a single swap extent represents varies.
969 * Typically it is in the 1-4 megabyte range. So we can have hundreds of
970 * extents in the list. To avoid much list walking, we cache the previous
971 * search location in `curr_swap_extent', and start new searches from there.
972 * This is extremely effective. The average number of iterations in
973 * map_swap_page() has been measured at about 0.3 per page. - akpm.
975 static int setup_swap_extents(struct swap_info_struct *sis)
978 unsigned blocks_per_page;
979 unsigned long page_no;
981 sector_t probe_block;
985 inode = sis->swap_file->f_mapping->host;
986 if (S_ISBLK(inode->i_mode)) {
987 ret = add_swap_extent(sis, 0, sis->max, 0);
991 blkbits = inode->i_blkbits;
992 blocks_per_page = PAGE_SIZE >> blkbits;
995 * Map all the blocks into the extent list. This code doesn't try
1000 last_block = i_size_read(inode) >> blkbits;
1001 while ((probe_block + blocks_per_page) <= last_block &&
1002 page_no < sis->max) {
1003 unsigned block_in_page;
1004 sector_t first_block;
1006 first_block = bmap(inode, probe_block);
1007 if (first_block == 0)
1011 * It must be PAGE_SIZE aligned on-disk
1013 if (first_block & (blocks_per_page - 1)) {
1018 for (block_in_page = 1; block_in_page < blocks_per_page;
1022 block = bmap(inode, probe_block + block_in_page);
1025 if (block != first_block + block_in_page) {
1033 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
1035 ret = add_swap_extent(sis, page_no, 1,
1036 first_block >> (PAGE_SHIFT - blkbits));
1040 probe_block += blocks_per_page;
1048 sis->highest_bit = page_no - 1;
1050 sis->curr_swap_extent = list_entry(sis->extent_list.prev,
1051 struct swap_extent, list);
1054 printk(KERN_ERR "swapon: swapfile has holes\n");
1060 #if 0 /* We don't need this yet */
1061 #include <linux/backing-dev.h>
1062 int page_queue_congested(struct page *page)
1064 struct backing_dev_info *bdi;
1066 BUG_ON(!PageLocked(page)); /* It pins the swap_info_struct */
1068 if (PageSwapCache(page)) {
1069 swp_entry_t entry = { .val = page->private };
1070 struct swap_info_struct *sis;
1072 sis = get_swap_info_struct(swp_type(entry));
1073 bdi = sis->bdev->bd_inode->i_mapping->backing_dev_info;
1075 bdi = page->mapping->backing_dev_info;
1076 return bdi_write_congested(bdi);
1080 asmlinkage long sys_swapoff(const char __user * specialfile)
1082 struct swap_info_struct * p = NULL;
1083 unsigned short *swap_map;
1084 struct file *swap_file, *victim;
1085 struct address_space *mapping;
1086 struct inode *inode;
1091 if (!capable(CAP_SYS_ADMIN))
1094 pathname = getname(specialfile);
1095 err = PTR_ERR(pathname);
1096 if (IS_ERR(pathname))
1099 victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
1101 err = PTR_ERR(victim);
1105 mapping = victim->f_mapping;
1108 for (type = swap_list.head; type >= 0; type = swap_info[type].next) {
1109 p = swap_info + type;
1110 if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) {
1111 if (p->swap_file->f_mapping == mapping)
1121 if (!security_vm_enough_memory(p->pages))
1122 vm_unacct_memory(p->pages);
1129 swap_list.head = p->next;
1131 swap_info[prev].next = p->next;
1133 if (type == swap_list.next) {
1134 /* just pick something that's safe... */
1135 swap_list.next = swap_list.head;
1137 nr_swap_pages -= p->pages;
1138 total_swap_pages -= p->pages;
1139 p->flags &= ~SWP_WRITEOK;
1141 current->flags |= PF_SWAPOFF;
1142 err = try_to_unuse(type);
1143 current->flags &= ~PF_SWAPOFF;
1145 /* wait for any unplug function to finish */
1146 down_write(&swap_unplug_sem);
1147 up_write(&swap_unplug_sem);
1150 /* re-insert swap space back into swap_list */
1152 for (prev = -1, i = swap_list.head; i >= 0; prev = i, i = swap_info[i].next)
1153 if (p->prio >= swap_info[i].prio)
1157 swap_list.head = swap_list.next = p - swap_info;
1159 swap_info[prev].next = p - swap_info;
1160 nr_swap_pages += p->pages;
1161 total_swap_pages += p->pages;
1162 p->flags |= SWP_WRITEOK;
1168 swap_device_lock(p);
1169 swap_file = p->swap_file;
1170 p->swap_file = NULL;
1172 swap_map = p->swap_map;
1175 destroy_swap_extents(p);
1176 swap_device_unlock(p);
1180 inode = mapping->host;
1181 if (S_ISBLK(inode->i_mode)) {
1182 struct block_device *bdev = I_BDEV(inode);
1183 set_blocksize(bdev, p->old_block_size);
1186 down(&inode->i_sem);
1187 inode->i_flags &= ~S_SWAPFILE;
1190 filp_close(swap_file, NULL);
1194 filp_close(victim, NULL);
1199 #ifdef CONFIG_PROC_FS
1201 static void *swap_start(struct seq_file *swap, loff_t *pos)
1203 struct swap_info_struct *ptr = swap_info;
1209 for (i = 0; i < nr_swapfiles; i++, ptr++) {
1210 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1219 static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
1221 struct swap_info_struct *ptr = v;
1222 struct swap_info_struct *endptr = swap_info + nr_swapfiles;
1224 for (++ptr; ptr < endptr; ptr++) {
1225 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1234 static void swap_stop(struct seq_file *swap, void *v)
1239 static int swap_show(struct seq_file *swap, void *v)
1241 struct swap_info_struct *ptr = v;
1246 seq_puts(swap, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1248 file = ptr->swap_file;
1249 len = seq_path(swap, file->f_vfsmnt, file->f_dentry, " \t\n\\");
1250 seq_printf(swap, "%*s%s\t%d\t%ld\t%d\n",
1251 len < 40 ? 40 - len : 1, " ",
1252 S_ISBLK(file->f_dentry->d_inode->i_mode) ?
1253 "partition" : "file\t",
1254 ptr->pages << (PAGE_SHIFT - 10),
1255 ptr->inuse_pages << (PAGE_SHIFT - 10),
1260 static struct seq_operations swaps_op = {
1261 .start = swap_start,
1267 static int swaps_open(struct inode *inode, struct file *file)
1269 return seq_open(file, &swaps_op);
1272 static struct file_operations proc_swaps_operations = {
1275 .llseek = seq_lseek,
1276 .release = seq_release,
1279 static int __init procswaps_init(void)
1281 struct proc_dir_entry *entry;
1283 entry = create_proc_entry("swaps", 0, NULL);
1285 entry->proc_fops = &proc_swaps_operations;
1288 __initcall(procswaps_init);
1289 #endif /* CONFIG_PROC_FS */
1292 * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
1294 * The swapon system call
1296 asmlinkage long sys_swapon(const char __user * specialfile, int swap_flags)
1298 struct swap_info_struct * p;
1300 struct block_device *bdev = NULL;
1301 struct file *swap_file = NULL;
1302 struct address_space *mapping;
1306 static int least_priority;
1307 union swap_header *swap_header = NULL;
1308 int swap_header_version;
1309 int nr_good_pages = 0;
1310 unsigned long maxpages = 1;
1312 unsigned short *swap_map;
1313 struct page *page = NULL;
1314 struct inode *inode = NULL;
1317 if (!capable(CAP_SYS_ADMIN))
1321 for (type = 0 ; type < nr_swapfiles ; type++,p++)
1322 if (!(p->flags & SWP_USED))
1326 * Test if adding another swap device is possible. There are
1327 * two limiting factors: 1) the number of bits for the swap
1328 * type swp_entry_t definition and 2) the number of bits for
1329 * the swap type in the swap ptes as defined by the different
1330 * architectures. To honor both limitations a swap entry
1331 * with swap offset 0 and swap type ~0UL is created, encoded
1332 * to a swap pte, decoded to a swp_entry_t again and finally
1333 * the swap type part is extracted. This will mask all bits
1334 * from the initial ~0UL that can't be encoded in either the
1335 * swp_entry_t or the architecture definition of a swap pte.
1337 if (type > swp_type(pte_to_swp_entry(swp_entry_to_pte(swp_entry(~0UL,0))))) {
1341 if (type >= nr_swapfiles)
1342 nr_swapfiles = type+1;
1343 INIT_LIST_HEAD(&p->extent_list);
1344 p->flags = SWP_USED;
1346 p->swap_file = NULL;
1347 p->old_block_size = 0;
1353 p->sdev_lock = SPIN_LOCK_UNLOCKED;
1355 if (swap_flags & SWAP_FLAG_PREFER) {
1357 (swap_flags & SWAP_FLAG_PRIO_MASK)>>SWAP_FLAG_PRIO_SHIFT;
1359 p->prio = --least_priority;
1362 name = getname(specialfile);
1363 error = PTR_ERR(name);
1368 swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
1369 error = PTR_ERR(swap_file);
1370 if (IS_ERR(swap_file)) {
1375 p->swap_file = swap_file;
1376 mapping = swap_file->f_mapping;
1377 inode = mapping->host;
1380 for (i = 0; i < nr_swapfiles; i++) {
1381 struct swap_info_struct *q = &swap_info[i];
1383 if (i == type || !q->swap_file)
1385 if (mapping == q->swap_file->f_mapping)
1390 if (S_ISBLK(inode->i_mode)) {
1391 bdev = I_BDEV(inode);
1392 error = bd_claim(bdev, sys_swapon);
1397 p->old_block_size = block_size(bdev);
1398 error = set_blocksize(bdev, PAGE_SIZE);
1402 } else if (S_ISREG(inode->i_mode)) {
1403 p->bdev = inode->i_sb->s_bdev;
1404 down(&inode->i_sem);
1406 if (IS_SWAPFILE(inode)) {
1414 swapfilesize = i_size_read(inode) >> PAGE_SHIFT;
1417 * Read the swap header.
1419 if (!mapping->a_ops->readpage) {
1423 page = read_cache_page(mapping, 0,
1424 (filler_t *)mapping->a_ops->readpage, swap_file);
1426 error = PTR_ERR(page);
1429 wait_on_page_locked(page);
1430 if (!PageUptodate(page))
1433 swap_header = page_address(page);
1435 if (!memcmp("SWAP-SPACE",swap_header->magic.magic,10))
1436 swap_header_version = 1;
1437 else if (!memcmp("SWAPSPACE2",swap_header->magic.magic,10))
1438 swap_header_version = 2;
1440 printk("Unable to find swap-space signature\n");
1445 switch (swap_header_version) {
1447 printk(KERN_ERR "version 0 swap is no longer supported. "
1448 "Use mkswap -v1 %s\n", name);
1452 /* Check the swap header's sub-version and the size of
1453 the swap file and bad block lists */
1454 if (swap_header->info.version != 1) {
1456 "Unable to handle swap header version %d\n",
1457 swap_header->info.version);
1464 * Find out how many pages are allowed for a single swap
1465 * device. There are two limiting factors: 1) the number of
1466 * bits for the swap offset in the swp_entry_t type and
1467 * 2) the number of bits in the a swap pte as defined by
1468 * the different architectures. In order to find the
1469 * largest possible bit mask a swap entry with swap type 0
1470 * and swap offset ~0UL is created, encoded to a swap pte,
1471 * decoded to a swp_entry_t again and finally the swap
1472 * offset is extracted. This will mask all the bits from
1473 * the initial ~0UL mask that can't be encoded in either
1474 * the swp_entry_t or the architecture definition of a
1477 maxpages = swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0,~0UL)))) - 1;
1478 if (maxpages > swap_header->info.last_page)
1479 maxpages = swap_header->info.last_page;
1480 p->highest_bit = maxpages - 1;
1483 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
1486 /* OK, set up the swap map and apply the bad block list */
1487 if (!(p->swap_map = vmalloc(maxpages * sizeof(short)))) {
1493 memset(p->swap_map, 0, maxpages * sizeof(short));
1494 for (i=0; i<swap_header->info.nr_badpages; i++) {
1495 int page = swap_header->info.badpages[i];
1496 if (page <= 0 || page >= swap_header->info.last_page)
1499 p->swap_map[page] = SWAP_MAP_BAD;
1501 nr_good_pages = swap_header->info.last_page -
1502 swap_header->info.nr_badpages -
1503 1 /* header page */;
1508 if (swapfilesize && maxpages > swapfilesize) {
1510 "Swap area shorter than signature indicates\n");
1514 if (!nr_good_pages) {
1515 printk(KERN_WARNING "Empty swap-file\n");
1519 p->swap_map[0] = SWAP_MAP_BAD;
1521 p->pages = nr_good_pages;
1523 error = setup_swap_extents(p);
1529 swap_device_lock(p);
1530 p->flags = SWP_ACTIVE;
1531 nr_swap_pages += nr_good_pages;
1532 total_swap_pages += nr_good_pages;
1533 printk(KERN_INFO "Adding %dk swap on %s. Priority:%d extents:%d\n",
1534 nr_good_pages<<(PAGE_SHIFT-10), name,
1535 p->prio, p->nr_extents);
1537 /* insert swap space into swap_list: */
1539 for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
1540 if (p->prio >= swap_info[i].prio) {
1547 swap_list.head = swap_list.next = p - swap_info;
1549 swap_info[prev].next = p - swap_info;
1551 swap_device_unlock(p);
1558 set_blocksize(bdev, p->old_block_size);
1563 swap_map = p->swap_map;
1564 p->swap_file = NULL;
1567 if (!(swap_flags & SWAP_FLAG_PREFER))
1570 destroy_swap_extents(p);
1574 filp_close(swap_file, NULL);
1576 if (page && !IS_ERR(page)) {
1578 page_cache_release(page);
1584 inode->i_flags |= S_SWAPFILE;
1590 void si_swapinfo(struct sysinfo *val)
1593 unsigned long nr_to_be_unused = 0;
1596 for (i = 0; i < nr_swapfiles; i++) {
1597 if (!(swap_info[i].flags & SWP_USED) ||
1598 (swap_info[i].flags & SWP_WRITEOK))
1600 nr_to_be_unused += swap_info[i].inuse_pages;
1602 val->freeswap = nr_swap_pages + nr_to_be_unused;
1603 val->totalswap = total_swap_pages + nr_to_be_unused;
1605 if (vx_flags(VXF_VIRT_MEM, 0))
1606 vx_vsi_swapinfo(val);
1610 * Verify that a swap entry is valid and increment its swap map count.
1612 * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
1613 * "permanent", but will be reclaimed by the next swapoff.
1615 int swap_duplicate(swp_entry_t entry)
1617 struct swap_info_struct * p;
1618 unsigned long offset, type;
1621 type = swp_type(entry);
1622 if (type >= nr_swapfiles)
1624 p = type + swap_info;
1625 offset = swp_offset(entry);
1627 swap_device_lock(p);
1628 if (offset < p->max && p->swap_map[offset]) {
1629 if (p->swap_map[offset] < SWAP_MAP_MAX - 1) {
1630 p->swap_map[offset]++;
1632 } else if (p->swap_map[offset] <= SWAP_MAP_MAX) {
1633 if (swap_overflow++ < 5)
1634 printk(KERN_WARNING "swap_dup: swap entry overflow\n");
1635 p->swap_map[offset] = SWAP_MAP_MAX;
1639 swap_device_unlock(p);
1644 printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
1648 struct swap_info_struct *
1649 get_swap_info_struct(unsigned type)
1651 return &swap_info[type];
1655 * swap_device_lock prevents swap_map being freed. Don't grab an extra
1656 * reference on the swaphandle, it doesn't matter if it becomes unused.
1658 int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
1660 int ret = 0, i = 1 << page_cluster;
1662 struct swap_info_struct *swapdev = swp_type(entry) + swap_info;
1664 if (!page_cluster) /* no readahead */
1666 toff = (swp_offset(entry) >> page_cluster) << page_cluster;
1667 if (!toff) /* first page is swap header */
1671 swap_device_lock(swapdev);
1673 /* Don't read-ahead past the end of the swap area */
1674 if (toff >= swapdev->max)
1676 /* Don't read in free or bad pages */
1677 if (!swapdev->swap_map[toff])
1679 if (swapdev->swap_map[toff] == SWAP_MAP_BAD)
1684 swap_device_unlock(swapdev);