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>
33 spinlock_t swaplock = SPIN_LOCK_UNLOCKED;
34 unsigned int nr_swapfiles;
36 static int swap_overflow;
38 EXPORT_SYMBOL(total_swap_pages);
40 static const char Bad_file[] = "Bad swap file entry ";
41 static const char Unused_file[] = "Unused swap file entry ";
42 static const char Bad_offset[] = "Bad swap offset entry ";
43 static const char Unused_offset[] = "Unused swap offset entry ";
45 struct swap_list_t swap_list = {-1, -1};
47 struct swap_info_struct swap_info[MAX_SWAPFILES];
49 static DECLARE_MUTEX(swapon_sem);
52 * We need this because the bdev->unplug_fn can sleep and we cannot
53 * hold swap_list_lock while calling the unplug_fn. And swap_list_lock
54 * cannot be turned into a semaphore.
56 static DECLARE_RWSEM(swap_unplug_sem);
58 #define SWAPFILE_CLUSTER 256
60 void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page)
64 down_read(&swap_unplug_sem);
65 entry.val = page->private;
66 if (PageSwapCache(page)) {
67 struct block_device *bdev = swap_info[swp_type(entry)].bdev;
68 struct backing_dev_info *bdi;
71 * If the page is removed from swapcache from under us (with a
72 * racy try_to_unuse/swapoff) we need an additional reference
73 * count to avoid reading garbage from page->private above. If
74 * the WARN_ON triggers during a swapoff it maybe the race
75 * condition and it's harmless. However if it triggers without
76 * swapoff it signals a problem.
78 WARN_ON(page_count(page) <= 1);
80 bdi = bdev->bd_inode->i_mapping->backing_dev_info;
81 bdi->unplug_io_fn(bdi, page);
83 up_read(&swap_unplug_sem);
86 static inline int scan_swap_map(struct swap_info_struct *si)
90 * We try to cluster swap pages by allocating them
91 * sequentially in swap. Once we've allocated
92 * SWAPFILE_CLUSTER pages this way, however, we resort to
93 * first-free allocation, starting a new cluster. This
94 * prevents us from scattering swap pages all over the entire
95 * swap partition, so that we reduce overall disk seek times
96 * between swap pages. -- sct */
98 while (si->cluster_next <= si->highest_bit) {
99 offset = si->cluster_next++;
100 if (si->swap_map[offset])
106 si->cluster_nr = SWAPFILE_CLUSTER;
108 /* try to find an empty (even not aligned) cluster. */
109 offset = si->lowest_bit;
111 if (offset+SWAPFILE_CLUSTER-1 <= si->highest_bit)
114 for (nr = offset; nr < offset+SWAPFILE_CLUSTER; nr++)
115 if (si->swap_map[nr])
118 goto check_next_cluster;
120 /* We found a completly empty cluster, so start
125 /* No luck, so now go finegrined as usual. -Andrea */
126 for (offset = si->lowest_bit; offset <= si->highest_bit ; offset++) {
127 if (si->swap_map[offset])
129 si->lowest_bit = offset+1;
131 if (offset == si->lowest_bit)
133 if (offset == si->highest_bit)
135 if (si->lowest_bit > si->highest_bit) {
136 si->lowest_bit = si->max;
139 si->swap_map[offset] = 1;
142 si->cluster_next = offset+1;
145 si->lowest_bit = si->max;
150 swp_entry_t get_swap_page(void)
152 struct swap_info_struct * p;
153 unsigned long offset;
155 int type, wrapped = 0;
157 entry.val = 0; /* Out of memory */
159 type = swap_list.next;
162 if (nr_swap_pages <= 0)
166 p = &swap_info[type];
167 if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) {
169 offset = scan_swap_map(p);
170 swap_device_unlock(p);
172 entry = swp_entry(type,offset);
173 type = swap_info[type].next;
175 p->prio != swap_info[type].prio) {
176 swap_list.next = swap_list.head;
178 swap_list.next = type;
185 if (type < 0 || p->prio != swap_info[type].prio) {
186 type = swap_list.head;
191 goto out; /* out of swap space */
198 static struct swap_info_struct * swap_info_get(swp_entry_t entry)
200 struct swap_info_struct * p;
201 unsigned long offset, type;
205 type = swp_type(entry);
206 if (type >= nr_swapfiles)
208 p = & swap_info[type];
209 if (!(p->flags & SWP_USED))
211 offset = swp_offset(entry);
212 if (offset >= p->max)
214 if (!p->swap_map[offset])
217 if (p->prio > swap_info[swap_list.next].prio)
218 swap_list.next = type;
223 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
226 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
229 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
232 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
237 static void swap_info_put(struct swap_info_struct * p)
239 swap_device_unlock(p);
243 static int swap_entry_free(struct swap_info_struct *p, unsigned long offset)
245 int count = p->swap_map[offset];
247 if (count < SWAP_MAP_MAX) {
249 p->swap_map[offset] = count;
251 if (offset < p->lowest_bit)
252 p->lowest_bit = offset;
253 if (offset > p->highest_bit)
254 p->highest_bit = offset;
263 * Caller has made sure that the swapdevice corresponding to entry
264 * is still around or has not been recycled.
266 void swap_free(swp_entry_t entry)
268 struct swap_info_struct * p;
270 p = swap_info_get(entry);
272 swap_entry_free(p, swp_offset(entry));
278 * Check if we're the only user of a swap page,
279 * when the page is locked.
281 static int exclusive_swap_page(struct page *page)
284 struct swap_info_struct * p;
287 entry.val = page->private;
288 p = swap_info_get(entry);
290 /* Is the only swap cache user the cache itself? */
291 if (p->swap_map[swp_offset(entry)] == 1) {
292 /* Recheck the page count with the swapcache lock held.. */
293 spin_lock_irq(&swapper_space.tree_lock);
294 if (page_count(page) == 2)
296 spin_unlock_irq(&swapper_space.tree_lock);
304 * We can use this swap cache entry directly
305 * if there are no other references to it.
307 * Here "exclusive_swap_page()" does the real
308 * work, but we opportunistically check whether
309 * we need to get all the locks first..
311 int can_share_swap_page(struct page *page)
315 if (!PageLocked(page))
317 switch (page_count(page)) {
319 if (!PagePrivate(page))
323 if (!PageSwapCache(page))
325 retval = exclusive_swap_page(page);
328 if (PageReserved(page))
336 * Work out if there are any other processes sharing this
337 * swap cache page. Free it if you can. Return success.
339 int remove_exclusive_swap_page(struct page *page)
342 struct swap_info_struct * p;
345 BUG_ON(PagePrivate(page));
346 BUG_ON(!PageLocked(page));
348 if (!PageSwapCache(page))
350 if (PageWriteback(page))
352 if (page_count(page) != 2) /* 2: us + cache */
355 entry.val = page->private;
356 p = swap_info_get(entry);
360 /* Is the only swap cache user the cache itself? */
362 if (p->swap_map[swp_offset(entry)] == 1) {
363 /* Recheck the page count with the swapcache lock held.. */
364 spin_lock_irq(&swapper_space.tree_lock);
365 if ((page_count(page) == 2) && !PageWriteback(page)) {
366 __delete_from_swap_cache(page);
370 spin_unlock_irq(&swapper_space.tree_lock);
376 page_cache_release(page);
383 * Free the swap entry like above, but also try to
384 * free the page cache entry if it is the last user.
386 void free_swap_and_cache(swp_entry_t entry)
388 struct swap_info_struct * p;
389 struct page *page = NULL;
391 p = swap_info_get(entry);
393 if (swap_entry_free(p, swp_offset(entry)) == 1) {
394 spin_lock_irq(&swapper_space.tree_lock);
395 page = radix_tree_lookup(&swapper_space.page_tree,
397 if (page && TestSetPageLocked(page))
399 spin_unlock_irq(&swapper_space.tree_lock);
406 BUG_ON(PagePrivate(page));
407 page_cache_get(page);
408 one_user = (page_count(page) == 2);
409 /* Only cache user (+us), or swap space full? Free it! */
410 if (!PageWriteback(page) && (one_user || vm_swap_full())) {
411 delete_from_swap_cache(page);
415 page_cache_release(page);
420 * The swap entry has been read in advance, and we return 1 to indicate
421 * that the page has been used or is no longer needed.
423 * Always set the resulting pte to be nowrite (the same as COW pages
424 * after one process has exited). We don't know just how many PTEs will
425 * share this swap entry, so be cautious and let do_wp_page work out
426 * what to do if a write is requested later.
428 /* vma->vm_mm->page_table_lock is held */
430 unuse_pte(struct vm_area_struct *vma, unsigned long address, pte_t *dir,
431 swp_entry_t entry, struct page *page)
433 // vma->vm_mm->rss++;
434 vx_rsspages_inc(vma->vm_mm);
436 set_pte(dir, pte_mkold(mk_pte(page, vma->vm_page_prot)));
437 page_add_anon_rmap(page, vma, address);
441 /* vma->vm_mm->page_table_lock is held */
442 static unsigned long unuse_pmd(struct vm_area_struct * vma, pmd_t *dir,
443 unsigned long address, unsigned long size, unsigned long offset,
444 swp_entry_t entry, struct page *page)
448 pte_t swp_pte = swp_entry_to_pte(entry);
457 pte = pte_offset_map(dir, address);
458 offset += address & PMD_MASK;
459 address &= ~PMD_MASK;
460 end = address + size;
465 * swapoff spends a _lot_ of time in this loop!
466 * Test inline before going to call unuse_pte.
468 if (unlikely(pte_same(*pte, swp_pte))) {
469 unuse_pte(vma, offset + address, pte, entry, page);
471 /* add 1 since address may be 0 */
472 return 1 + offset + address;
474 address += PAGE_SIZE;
476 } while (address && (address < end));
481 /* vma->vm_mm->page_table_lock is held */
482 static unsigned long unuse_pgd(struct vm_area_struct * vma, pgd_t *dir,
483 unsigned long address, unsigned long size,
484 swp_entry_t entry, struct page *page)
487 unsigned long offset, end;
488 unsigned long foundaddr;
497 pmd = pmd_offset(dir, address);
498 offset = address & PGDIR_MASK;
499 address &= ~PGDIR_MASK;
500 end = address + size;
501 if (end > PGDIR_SIZE)
506 foundaddr = unuse_pmd(vma, pmd, address, end - address,
507 offset, entry, page);
510 address = (address + PMD_SIZE) & PMD_MASK;
512 } while (address && (address < end));
516 /* vma->vm_mm->page_table_lock is held */
517 static unsigned long unuse_vma(struct vm_area_struct * vma, pgd_t *pgdir,
518 swp_entry_t entry, struct page *page)
520 unsigned long start = vma->vm_start, end = vma->vm_end;
521 unsigned long foundaddr;
526 foundaddr = unuse_pgd(vma, pgdir, start, end - start,
530 start = (start + PGDIR_SIZE) & PGDIR_MASK;
532 } while (start && (start < end));
536 static int unuse_process(struct mm_struct * mm,
537 swp_entry_t entry, struct page* page)
539 struct vm_area_struct* vma;
540 unsigned long foundaddr = 0;
543 * Go through process' page directory.
545 down_read(&mm->mmap_sem);
546 spin_lock(&mm->page_table_lock);
547 for (vma = mm->mmap; vma; vma = vma->vm_next) {
548 if (!is_vm_hugetlb_page(vma)) {
549 pgd_t * pgd = pgd_offset(mm, vma->vm_start);
550 foundaddr = unuse_vma(vma, pgd, entry, page);
555 spin_unlock(&mm->page_table_lock);
556 up_read(&mm->mmap_sem);
558 * Currently unuse_process cannot fail, but leave error handling
559 * at call sites for now, since we change it from time to time.
565 * Scan swap_map from current position to next entry still in use.
566 * Recycle to start on reaching the end, returning 0 when empty.
568 static int find_next_to_unuse(struct swap_info_struct *si, int prev)
575 * No need for swap_device_lock(si) here: we're just looking
576 * for whether an entry is in use, not modifying it; false
577 * hits are okay, and sys_swapoff() has already prevented new
578 * allocations from this area (while holding swap_list_lock()).
587 * No entries in use at top of swap_map,
588 * loop back to start and recheck there.
594 count = si->swap_map[i];
595 if (count && count != SWAP_MAP_BAD)
602 * We completely avoid races by reading each swap page in advance,
603 * and then search for the process using it. All the necessary
604 * page table adjustments can then be made atomically.
606 static int try_to_unuse(unsigned int type)
608 struct swap_info_struct * si = &swap_info[type];
609 struct mm_struct *start_mm;
610 unsigned short *swap_map;
611 unsigned short swcount;
616 int reset_overflow = 0;
620 * When searching mms for an entry, a good strategy is to
621 * start at the first mm we freed the previous entry from
622 * (though actually we don't notice whether we or coincidence
623 * freed the entry). Initialize this start_mm with a hold.
625 * A simpler strategy would be to start at the last mm we
626 * freed the previous entry from; but that would take less
627 * advantage of mmlist ordering (now preserved by swap_out()),
628 * which clusters forked address spaces together, most recent
629 * child immediately after parent. If we race with dup_mmap(),
630 * we very much want to resolve parent before child, otherwise
631 * we may miss some entries: using last mm would invert that.
634 atomic_inc(&init_mm.mm_users);
637 * Keep on scanning until all entries have gone. Usually,
638 * one pass through swap_map is enough, but not necessarily:
639 * mmput() removes mm from mmlist before exit_mmap() and its
640 * zap_page_range(). That's not too bad, those entries are
641 * on their way out, and handled faster there than here.
642 * do_munmap() behaves similarly, taking the range out of mm's
643 * vma list before zap_page_range(). But unfortunately, when
644 * unmapping a part of a vma, it takes the whole out first,
645 * then reinserts what's left after (might even reschedule if
646 * open() method called) - so swap entries may be invisible
647 * to swapoff for a while, then reappear - but that is rare.
649 while ((i = find_next_to_unuse(si, i))) {
650 if (signal_pending(current)) {
656 * Get a page for the entry, using the existing swap
657 * cache page if there is one. Otherwise, get a clean
658 * page and read the swap into it.
660 swap_map = &si->swap_map[i];
661 entry = swp_entry(type, i);
662 page = read_swap_cache_async(entry, NULL, 0);
665 * Either swap_duplicate() failed because entry
666 * has been freed independently, and will not be
667 * reused since sys_swapoff() already disabled
668 * allocation from here, or alloc_page() failed.
677 * Don't hold on to start_mm if it looks like exiting.
679 if (atomic_read(&start_mm->mm_users) == 1) {
682 atomic_inc(&init_mm.mm_users);
686 * Wait for and lock page. When do_swap_page races with
687 * try_to_unuse, do_swap_page can handle the fault much
688 * faster than try_to_unuse can locate the entry. This
689 * apparently redundant "wait_on_page_locked" lets try_to_unuse
690 * defer to do_swap_page in such a case - in some tests,
691 * do_swap_page and try_to_unuse repeatedly compete.
693 wait_on_page_locked(page);
694 wait_on_page_writeback(page);
696 wait_on_page_writeback(page);
699 * Remove all references to entry, without blocking.
700 * Whenever we reach init_mm, there's no address space
701 * to search, but use it as a reminder to search shmem.
706 if (start_mm == &init_mm)
707 shmem = shmem_unuse(entry, page);
709 retval = unuse_process(start_mm, entry, page);
712 int set_start_mm = (*swap_map >= swcount);
713 struct list_head *p = &start_mm->mmlist;
714 struct mm_struct *new_start_mm = start_mm;
715 struct mm_struct *prev_mm = start_mm;
716 struct mm_struct *mm;
718 atomic_inc(&new_start_mm->mm_users);
719 atomic_inc(&prev_mm->mm_users);
720 spin_lock(&mmlist_lock);
721 while (*swap_map > 1 && !retval &&
722 (p = p->next) != &start_mm->mmlist) {
723 mm = list_entry(p, struct mm_struct, mmlist);
724 atomic_inc(&mm->mm_users);
725 spin_unlock(&mmlist_lock);
734 else if (mm == &init_mm) {
736 shmem = shmem_unuse(entry, page);
738 retval = unuse_process(mm, entry, page);
739 if (set_start_mm && *swap_map < swcount) {
741 atomic_inc(&mm->mm_users);
745 spin_lock(&mmlist_lock);
747 spin_unlock(&mmlist_lock);
750 start_mm = new_start_mm;
754 page_cache_release(page);
759 * How could swap count reach 0x7fff when the maximum
760 * pid is 0x7fff, and there's no way to repeat a swap
761 * page within an mm (except in shmem, where it's the
762 * shared object which takes the reference count)?
763 * We believe SWAP_MAP_MAX cannot occur in Linux 2.4.
765 * If that's wrong, then we should worry more about
766 * exit_mmap() and do_munmap() cases described above:
767 * we might be resetting SWAP_MAP_MAX too early here.
768 * We know "Undead"s can happen, they're okay, so don't
769 * report them; but do report if we reset SWAP_MAP_MAX.
771 if (*swap_map == SWAP_MAP_MAX) {
772 swap_device_lock(si);
774 swap_device_unlock(si);
779 * If a reference remains (rare), we would like to leave
780 * the page in the swap cache; but try_to_unmap could
781 * then re-duplicate the entry once we drop page lock,
782 * so we might loop indefinitely; also, that page could
783 * not be swapped out to other storage meanwhile. So:
784 * delete from cache even if there's another reference,
785 * after ensuring that the data has been saved to disk -
786 * since if the reference remains (rarer), it will be
787 * read from disk into another page. Splitting into two
788 * pages would be incorrect if swap supported "shared
789 * private" pages, but they are handled by tmpfs files.
791 * Note shmem_unuse already deleted a swappage from
792 * the swap cache, unless the move to filepage failed:
793 * in which case it left swappage in cache, lowered its
794 * swap count to pass quickly through the loops above,
795 * and now we must reincrement count to try again later.
797 if ((*swap_map > 1) && PageDirty(page) && PageSwapCache(page)) {
798 struct writeback_control wbc = {
799 .sync_mode = WB_SYNC_NONE,
802 swap_writepage(page, &wbc);
804 wait_on_page_writeback(page);
806 if (PageSwapCache(page)) {
808 swap_duplicate(entry);
810 delete_from_swap_cache(page);
814 * So we could skip searching mms once swap count went
815 * to 1, we did not mark any present ptes as dirty: must
816 * mark page dirty so shrink_list will preserve it.
820 page_cache_release(page);
823 * Make sure that we aren't completely killing
824 * interactive performance.
830 if (reset_overflow) {
831 printk(KERN_WARNING "swapoff: cleared swap entry overflow\n");
838 * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
839 * corresponds to page offset `offset'.
841 sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset)
843 struct swap_extent *se = sis->curr_swap_extent;
844 struct swap_extent *start_se = se;
847 struct list_head *lh;
849 if (se->start_page <= offset &&
850 offset < (se->start_page + se->nr_pages)) {
851 return se->start_block + (offset - se->start_page);
854 if (lh == &sis->extent_list)
856 se = list_entry(lh, struct swap_extent, list);
857 sis->curr_swap_extent = se;
858 BUG_ON(se == start_se); /* It *must* be present */
863 * Free all of a swapdev's extent information
865 static void destroy_swap_extents(struct swap_info_struct *sis)
867 while (!list_empty(&sis->extent_list)) {
868 struct swap_extent *se;
870 se = list_entry(sis->extent_list.next,
871 struct swap_extent, list);
879 * Add a block range (and the corresponding page range) into this swapdev's
880 * extent list. The extent list is kept sorted in block order.
882 * This function rather assumes that it is called in ascending sector_t order.
883 * It doesn't look for extent coalescing opportunities.
886 add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
887 unsigned long nr_pages, sector_t start_block)
889 struct swap_extent *se;
890 struct swap_extent *new_se;
891 struct list_head *lh;
893 lh = sis->extent_list.next; /* The highest-addressed block */
894 while (lh != &sis->extent_list) {
895 se = list_entry(lh, struct swap_extent, list);
896 if (se->start_block + se->nr_pages == start_block &&
897 se->start_page + se->nr_pages == start_page) {
899 se->nr_pages += nr_pages;
906 * No merge. Insert a new extent, preserving ordering.
908 new_se = kmalloc(sizeof(*se), GFP_KERNEL);
911 new_se->start_page = start_page;
912 new_se->nr_pages = nr_pages;
913 new_se->start_block = start_block;
915 lh = sis->extent_list.prev; /* The lowest block */
916 while (lh != &sis->extent_list) {
917 se = list_entry(lh, struct swap_extent, list);
918 if (se->start_block > start_block)
922 list_add_tail(&new_se->list, lh);
928 * A `swap extent' is a simple thing which maps a contiguous range of pages
929 * onto a contiguous range of disk blocks. An ordered list of swap extents
930 * is built at swapon time and is then used at swap_writepage/swap_readpage
931 * time for locating where on disk a page belongs.
933 * If the swapfile is an S_ISBLK block device, a single extent is installed.
934 * This is done so that the main operating code can treat S_ISBLK and S_ISREG
935 * swap files identically.
937 * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
938 * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
939 * swapfiles are handled *identically* after swapon time.
941 * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
942 * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
943 * some stray blocks are found which do not fall within the PAGE_SIZE alignment
944 * requirements, they are simply tossed out - we will never use those blocks
947 * For S_ISREG swapfiles we hold i_sem across the life of the swapon. This
948 * prevents root from shooting her foot off by ftruncating an in-use swapfile,
949 * which will scribble on the fs.
951 * The amount of disk space which a single swap extent represents varies.
952 * Typically it is in the 1-4 megabyte range. So we can have hundreds of
953 * extents in the list. To avoid much list walking, we cache the previous
954 * search location in `curr_swap_extent', and start new searches from there.
955 * This is extremely effective. The average number of iterations in
956 * map_swap_page() has been measured at about 0.3 per page. - akpm.
958 static int setup_swap_extents(struct swap_info_struct *sis)
961 unsigned blocks_per_page;
962 unsigned long page_no;
964 sector_t probe_block;
968 inode = sis->swap_file->f_mapping->host;
969 if (S_ISBLK(inode->i_mode)) {
970 ret = add_swap_extent(sis, 0, sis->max, 0);
974 blkbits = inode->i_blkbits;
975 blocks_per_page = PAGE_SIZE >> blkbits;
978 * Map all the blocks into the extent list. This code doesn't try
983 last_block = i_size_read(inode) >> blkbits;
984 while ((probe_block + blocks_per_page) <= last_block &&
985 page_no < sis->max) {
986 unsigned block_in_page;
987 sector_t first_block;
989 first_block = bmap(inode, probe_block);
990 if (first_block == 0)
994 * It must be PAGE_SIZE aligned on-disk
996 if (first_block & (blocks_per_page - 1)) {
1001 for (block_in_page = 1; block_in_page < blocks_per_page;
1005 block = bmap(inode, probe_block + block_in_page);
1008 if (block != first_block + block_in_page) {
1016 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
1018 ret = add_swap_extent(sis, page_no, 1,
1019 first_block >> (PAGE_SHIFT - blkbits));
1023 probe_block += blocks_per_page;
1031 sis->highest_bit = page_no - 1;
1033 sis->curr_swap_extent = list_entry(sis->extent_list.prev,
1034 struct swap_extent, list);
1037 printk(KERN_ERR "swapon: swapfile has holes\n");
1043 #if 0 /* We don't need this yet */
1044 #include <linux/backing-dev.h>
1045 int page_queue_congested(struct page *page)
1047 struct backing_dev_info *bdi;
1049 BUG_ON(!PageLocked(page)); /* It pins the swap_info_struct */
1051 if (PageSwapCache(page)) {
1052 swp_entry_t entry = { .val = page->private };
1053 struct swap_info_struct *sis;
1055 sis = get_swap_info_struct(swp_type(entry));
1056 bdi = sis->bdev->bd_inode->i_mapping->backing_dev_info;
1058 bdi = page->mapping->backing_dev_info;
1059 return bdi_write_congested(bdi);
1063 asmlinkage long sys_swapoff(const char __user * specialfile)
1065 struct swap_info_struct * p = NULL;
1066 unsigned short *swap_map;
1067 struct file *swap_file, *victim;
1068 struct address_space *mapping;
1073 if (!capable(CAP_SYS_ADMIN))
1076 pathname = getname(specialfile);
1077 err = PTR_ERR(pathname);
1078 if (IS_ERR(pathname))
1081 victim = filp_open(pathname, O_RDWR, 0);
1083 err = PTR_ERR(victim);
1087 mapping = victim->f_mapping;
1090 for (type = swap_list.head; type >= 0; type = swap_info[type].next) {
1091 p = swap_info + type;
1092 if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) {
1093 if (p->swap_file->f_mapping == mapping)
1103 if (!security_vm_enough_memory(p->pages))
1104 vm_unacct_memory(p->pages);
1111 swap_list.head = p->next;
1113 swap_info[prev].next = p->next;
1115 if (type == swap_list.next) {
1116 /* just pick something that's safe... */
1117 swap_list.next = swap_list.head;
1119 nr_swap_pages -= p->pages;
1120 total_swap_pages -= p->pages;
1121 p->flags &= ~SWP_WRITEOK;
1123 current->flags |= PF_SWAPOFF;
1124 err = try_to_unuse(type);
1125 current->flags &= ~PF_SWAPOFF;
1127 /* wait for any unplug function to finish */
1128 down_write(&swap_unplug_sem);
1129 up_write(&swap_unplug_sem);
1132 /* re-insert swap space back into swap_list */
1134 for (prev = -1, i = swap_list.head; i >= 0; prev = i, i = swap_info[i].next)
1135 if (p->prio >= swap_info[i].prio)
1139 swap_list.head = swap_list.next = p - swap_info;
1141 swap_info[prev].next = p - swap_info;
1142 nr_swap_pages += p->pages;
1143 total_swap_pages += p->pages;
1144 p->flags |= SWP_WRITEOK;
1150 swap_device_lock(p);
1151 swap_file = p->swap_file;
1152 p->swap_file = NULL;
1154 swap_map = p->swap_map;
1157 destroy_swap_extents(p);
1158 swap_device_unlock(p);
1162 if (S_ISBLK(mapping->host->i_mode)) {
1163 struct block_device *bdev = I_BDEV(mapping->host);
1164 set_blocksize(bdev, p->old_block_size);
1167 up(&mapping->host->i_sem);
1169 filp_close(swap_file, NULL);
1173 filp_close(victim, NULL);
1178 #ifdef CONFIG_PROC_FS
1180 static void *swap_start(struct seq_file *swap, loff_t *pos)
1182 struct swap_info_struct *ptr = swap_info;
1188 for (i = 0; i < nr_swapfiles; i++, ptr++) {
1189 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1198 static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
1200 struct swap_info_struct *ptr = v;
1201 struct swap_info_struct *endptr = swap_info + nr_swapfiles;
1203 for (++ptr; ptr < endptr; ptr++) {
1204 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1213 static void swap_stop(struct seq_file *swap, void *v)
1218 static int swap_show(struct seq_file *swap, void *v)
1220 struct swap_info_struct *ptr = v;
1225 seq_puts(swap, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1227 file = ptr->swap_file;
1228 len = seq_path(swap, file->f_vfsmnt, file->f_dentry, " \t\n\\");
1229 seq_printf(swap, "%*s%s\t%d\t%ld\t%d\n",
1230 len < 40 ? 40 - len : 1, " ",
1231 S_ISBLK(file->f_dentry->d_inode->i_mode) ?
1232 "partition" : "file\t",
1233 ptr->pages << (PAGE_SHIFT - 10),
1234 ptr->inuse_pages << (PAGE_SHIFT - 10),
1239 static struct seq_operations swaps_op = {
1240 .start = swap_start,
1246 static int swaps_open(struct inode *inode, struct file *file)
1248 return seq_open(file, &swaps_op);
1251 static struct file_operations proc_swaps_operations = {
1254 .llseek = seq_lseek,
1255 .release = seq_release,
1258 static int __init procswaps_init(void)
1260 struct proc_dir_entry *entry;
1262 entry = create_proc_entry("swaps", 0, NULL);
1264 entry->proc_fops = &proc_swaps_operations;
1267 __initcall(procswaps_init);
1268 #endif /* CONFIG_PROC_FS */
1271 * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
1273 * The swapon system call
1275 asmlinkage long sys_swapon(const char __user * specialfile, int swap_flags)
1277 struct swap_info_struct * p;
1279 struct block_device *bdev = NULL;
1280 struct file *swap_file = NULL;
1281 struct address_space *mapping;
1285 static int least_priority;
1286 union swap_header *swap_header = 0;
1287 int swap_header_version;
1288 int nr_good_pages = 0;
1289 unsigned long maxpages = 1;
1291 unsigned short *swap_map;
1292 struct page *page = NULL;
1293 struct inode *inode = NULL;
1296 if (!capable(CAP_SYS_ADMIN))
1300 for (type = 0 ; type < nr_swapfiles ; type++,p++)
1301 if (!(p->flags & SWP_USED))
1305 * Test if adding another swap device is possible. There are
1306 * two limiting factors: 1) the number of bits for the swap
1307 * type swp_entry_t definition and 2) the number of bits for
1308 * the swap type in the swap ptes as defined by the different
1309 * architectures. To honor both limitations a swap entry
1310 * with swap offset 0 and swap type ~0UL is created, encoded
1311 * to a swap pte, decoded to a swp_entry_t again and finally
1312 * the swap type part is extracted. This will mask all bits
1313 * from the initial ~0UL that can't be encoded in either the
1314 * swp_entry_t or the architecture definition of a swap pte.
1316 if (type > swp_type(pte_to_swp_entry(swp_entry_to_pte(swp_entry(~0UL,0))))) {
1320 if (type >= nr_swapfiles)
1321 nr_swapfiles = type+1;
1322 INIT_LIST_HEAD(&p->extent_list);
1323 p->flags = SWP_USED;
1325 p->swap_file = NULL;
1326 p->old_block_size = 0;
1332 p->sdev_lock = SPIN_LOCK_UNLOCKED;
1334 if (swap_flags & SWAP_FLAG_PREFER) {
1336 (swap_flags & SWAP_FLAG_PRIO_MASK)>>SWAP_FLAG_PRIO_SHIFT;
1338 p->prio = --least_priority;
1341 name = getname(specialfile);
1342 error = PTR_ERR(name);
1347 swap_file = filp_open(name, O_RDWR, 0);
1348 error = PTR_ERR(swap_file);
1349 if (IS_ERR(swap_file)) {
1354 p->swap_file = swap_file;
1355 mapping = swap_file->f_mapping;
1356 inode = mapping->host;
1359 for (i = 0; i < nr_swapfiles; i++) {
1360 struct swap_info_struct *q = &swap_info[i];
1362 if (i == type || !q->swap_file)
1364 if (mapping == q->swap_file->f_mapping)
1369 if (S_ISBLK(inode->i_mode)) {
1370 bdev = I_BDEV(inode);
1371 error = bd_claim(bdev, sys_swapon);
1376 p->old_block_size = block_size(bdev);
1377 error = set_blocksize(bdev, PAGE_SIZE);
1381 } else if (S_ISREG(inode->i_mode)) {
1382 p->bdev = inode->i_sb->s_bdev;
1383 down(&inode->i_sem);
1389 swapfilesize = i_size_read(inode) >> PAGE_SHIFT;
1392 * Read the swap header.
1394 if (!mapping->a_ops->readpage) {
1398 page = read_cache_page(mapping, 0,
1399 (filler_t *)mapping->a_ops->readpage, swap_file);
1401 error = PTR_ERR(page);
1404 wait_on_page_locked(page);
1405 if (!PageUptodate(page))
1408 swap_header = page_address(page);
1410 if (!memcmp("SWAP-SPACE",swap_header->magic.magic,10))
1411 swap_header_version = 1;
1412 else if (!memcmp("SWAPSPACE2",swap_header->magic.magic,10))
1413 swap_header_version = 2;
1415 printk("Unable to find swap-space signature\n");
1420 switch (swap_header_version) {
1422 printk(KERN_ERR "version 0 swap is no longer supported. "
1423 "Use mkswap -v1 %s\n", name);
1427 /* Check the swap header's sub-version and the size of
1428 the swap file and bad block lists */
1429 if (swap_header->info.version != 1) {
1431 "Unable to handle swap header version %d\n",
1432 swap_header->info.version);
1439 * Find out how many pages are allowed for a single swap
1440 * device. There are two limiting factors: 1) the number of
1441 * bits for the swap offset in the swp_entry_t type and
1442 * 2) the number of bits in the a swap pte as defined by
1443 * the different architectures. In order to find the
1444 * largest possible bit mask a swap entry with swap type 0
1445 * and swap offset ~0UL is created, encoded to a swap pte,
1446 * decoded to a swp_entry_t again and finally the swap
1447 * offset is extracted. This will mask all the bits from
1448 * the initial ~0UL mask that can't be encoded in either
1449 * the swp_entry_t or the architecture definition of a
1452 maxpages = swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0,~0UL)))) - 1;
1453 if (maxpages > swap_header->info.last_page)
1454 maxpages = swap_header->info.last_page;
1455 p->highest_bit = maxpages - 1;
1458 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
1461 /* OK, set up the swap map and apply the bad block list */
1462 if (!(p->swap_map = vmalloc(maxpages * sizeof(short)))) {
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)
1474 p->swap_map[page] = SWAP_MAP_BAD;
1476 nr_good_pages = swap_header->info.last_page -
1477 swap_header->info.nr_badpages -
1478 1 /* header page */;
1483 if (swapfilesize && maxpages > swapfilesize) {
1485 "Swap area shorter than signature indicates\n");
1489 if (!nr_good_pages) {
1490 printk(KERN_WARNING "Empty swap-file\n");
1494 p->swap_map[0] = SWAP_MAP_BAD;
1496 p->pages = nr_good_pages;
1498 error = setup_swap_extents(p);
1504 swap_device_lock(p);
1505 p->flags = SWP_ACTIVE;
1506 nr_swap_pages += nr_good_pages;
1507 total_swap_pages += nr_good_pages;
1508 printk(KERN_INFO "Adding %dk swap on %s. Priority:%d extents:%d\n",
1509 nr_good_pages<<(PAGE_SHIFT-10), name,
1510 p->prio, p->nr_extents);
1512 /* insert swap space into swap_list: */
1514 for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
1515 if (p->prio >= swap_info[i].prio) {
1522 swap_list.head = swap_list.next = p - swap_info;
1524 swap_info[prev].next = p - swap_info;
1526 swap_device_unlock(p);
1533 set_blocksize(bdev, p->old_block_size);
1538 swap_map = p->swap_map;
1539 p->swap_file = NULL;
1542 if (!(swap_flags & SWAP_FLAG_PREFER))
1545 destroy_swap_extents(p);
1549 filp_close(swap_file, NULL);
1551 if (page && !IS_ERR(page)) {
1553 page_cache_release(page);
1557 if (error && did_down)
1562 void si_swapinfo(struct sysinfo *val)
1565 unsigned long nr_to_be_unused = 0;
1568 for (i = 0; i < nr_swapfiles; i++) {
1569 if (!(swap_info[i].flags & SWP_USED) ||
1570 (swap_info[i].flags & SWP_WRITEOK))
1572 nr_to_be_unused += swap_info[i].inuse_pages;
1574 val->freeswap = nr_swap_pages + nr_to_be_unused;
1575 val->totalswap = total_swap_pages + nr_to_be_unused;
1577 if (vx_flags(VXF_VIRT_MEM, 0))
1578 vx_vsi_swapinfo(val);
1582 * Verify that a swap entry is valid and increment its swap map count.
1584 * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
1585 * "permanent", but will be reclaimed by the next swapoff.
1587 int swap_duplicate(swp_entry_t entry)
1589 struct swap_info_struct * p;
1590 unsigned long offset, type;
1593 type = swp_type(entry);
1594 if (type >= nr_swapfiles)
1596 p = type + swap_info;
1597 offset = swp_offset(entry);
1599 swap_device_lock(p);
1600 if (offset < p->max && p->swap_map[offset]) {
1601 if (p->swap_map[offset] < SWAP_MAP_MAX - 1) {
1602 p->swap_map[offset]++;
1604 } else if (p->swap_map[offset] <= SWAP_MAP_MAX) {
1605 if (swap_overflow++ < 5)
1606 printk(KERN_WARNING "swap_dup: swap entry overflow\n");
1607 p->swap_map[offset] = SWAP_MAP_MAX;
1611 swap_device_unlock(p);
1616 printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
1620 struct swap_info_struct *
1621 get_swap_info_struct(unsigned type)
1623 return &swap_info[type];
1627 * swap_device_lock prevents swap_map being freed. Don't grab an extra
1628 * reference on the swaphandle, it doesn't matter if it becomes unused.
1630 int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
1632 int ret = 0, i = 1 << page_cluster;
1634 struct swap_info_struct *swapdev = swp_type(entry) + swap_info;
1636 if (!page_cluster) /* no readahead */
1638 toff = (swp_offset(entry) >> page_cluster) << page_cluster;
1639 if (!toff) /* first page is swap header */
1643 swap_device_lock(swapdev);
1645 /* Don't read-ahead past the end of the swap area */
1646 if (toff >= swapdev->max)
1648 /* Don't read in free or bad pages */
1649 if (!swapdev->swap_map[toff])
1651 if (swapdev->swap_map[toff] == SWAP_MAP_BAD)
1656 swap_device_unlock(swapdev);