Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / mm / swapfile.c
index ecb069e..9c7be87 100644 (file)
 #include <linux/vmalloc.h>
 #include <linux/pagemap.h>
 #include <linux/namei.h>
-#include <linux/shm.h>
+#include <linux/shmem_fs.h>
 #include <linux/blkdev.h>
 #include <linux/random.h>
 #include <linux/writeback.h>
 #include <linux/proc_fs.h>
 #include <linux/seq_file.h>
 #include <linux/init.h>
-#include <linux/module.h>
 #include <linux/ksm.h>
 #include <linux/rmap.h>
 #include <linux/security.h>
 #include <linux/capability.h>
 #include <linux/syscalls.h>
 #include <linux/memcontrol.h>
+#include <linux/poll.h>
+#include <linux/oom.h>
+#include <linux/frontswap.h>
+#include <linux/swapfile.h>
 
 #include <asm/pgtable.h>
 #include <asm/tlbflush.h>
@@ -41,7 +44,7 @@ static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
 static void free_swap_count_continuations(struct swap_info_struct *);
 static sector_t map_swap_entry(swp_entry_t, struct block_device**);
 
-static DEFINE_SPINLOCK(swap_lock);
+DEFINE_SPINLOCK(swap_lock);
 static unsigned int nr_swapfiles;
 long nr_swap_pages;
 long total_swap_pages;
@@ -52,12 +55,16 @@ static const char Unused_file[] = "Unused swap file entry ";
 static const char Bad_offset[] = "Bad swap offset entry ";
 static const char Unused_offset[] = "Unused swap offset entry ";
 
-static struct swap_list_t swap_list = {-1, -1};
+struct swap_list_t swap_list = {-1, -1};
 
-static struct swap_info_struct *swap_info[MAX_SWAPFILES];
+struct swap_info_struct *swap_info[MAX_SWAPFILES];
 
 static DEFINE_MUTEX(swapon_mutex);
 
+static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait);
+/* Activity counter to indicate that a swapon or swapoff has occurred */
+static atomic_t proc_poll_event = ATOMIC_INIT(0);
+
 static inline unsigned char swap_count(unsigned char ent)
 {
        return ent & ~SWAP_HAS_CACHE;   /* may include SWAP_HAS_CONT flag */
@@ -90,39 +97,6 @@ __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
 }
 
 /*
- * We need this because the bdev->unplug_fn can sleep and we cannot
- * hold swap_lock while calling the unplug_fn. And swap_lock
- * cannot be turned into a mutex.
- */
-static DECLARE_RWSEM(swap_unplug_sem);
-
-void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page)
-{
-       swp_entry_t entry;
-
-       down_read(&swap_unplug_sem);
-       entry.val = page_private(page);
-       if (PageSwapCache(page)) {
-               struct block_device *bdev = swap_info[swp_type(entry)]->bdev;
-               struct backing_dev_info *bdi;
-
-               /*
-                * If the page is removed from swapcache from under us (with a
-                * racy try_to_unuse/swapoff) we need an additional reference
-                * count to avoid reading garbage from page_private(page) above.
-                * If the WARN_ON triggers during a swapoff it maybe the race
-                * condition and it's harmless. However if it triggers without
-                * swapoff it signals a problem.
-                */
-               WARN_ON(page_count(page) <= 1);
-
-               bdi = bdev->bd_inode->i_mapping->backing_dev_info;
-               blk_run_backing_dev(bdi, page);
-       }
-       up_read(&swap_unplug_sem);
-}
-
-/*
  * swapon tell device that all the old swap contents can be discarded,
  * to allow the swap device to optimize its wear-levelling.
  */
@@ -139,7 +113,7 @@ static int discard_swap(struct swap_info_struct *si)
        nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
        if (nr_blocks) {
                err = blkdev_issue_discard(si->bdev, start_block,
-                               nr_blocks, GFP_KERNEL, DISCARD_FL_BARRIER);
+                               nr_blocks, GFP_KERNEL, 0);
                if (err)
                        return err;
                cond_resched();
@@ -150,7 +124,7 @@ static int discard_swap(struct swap_info_struct *si)
                nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
 
                err = blkdev_issue_discard(si->bdev, start_block,
-                               nr_blocks, GFP_KERNEL, DISCARD_FL_BARRIER);
+                               nr_blocks, GFP_KERNEL, 0);
                if (err)
                        break;
 
@@ -189,7 +163,7 @@ static void discard_swap_cluster(struct swap_info_struct *si,
                        start_block <<= PAGE_SHIFT - 9;
                        nr_blocks <<= PAGE_SHIFT - 9;
                        if (blkdev_issue_discard(si->bdev, start_block,
-                                   nr_blocks, GFP_NOIO, DISCARD_FL_BARRIER))
+                                   nr_blocks, GFP_NOIO, 0))
                                break;
                }
 
@@ -207,8 +181,8 @@ static int wait_for_discard(void *word)
 #define SWAPFILE_CLUSTER       256
 #define LATENCY_LIMIT          256
 
-static inline unsigned long scan_swap_map(struct swap_info_struct *si,
-                                         unsigned char usage)
+static unsigned long scan_swap_map(struct swap_info_struct *si,
+                                  unsigned char usage)
 {
        unsigned long offset;
        unsigned long scan_base;
@@ -574,6 +548,7 @@ static unsigned char swap_entry_free(struct swap_info_struct *p,
 
        /* free if no reference */
        if (!usage) {
+               struct gendisk *disk = p->bdev->bd_disk;
                if (offset < p->lowest_bit)
                        p->lowest_bit = offset;
                if (offset > p->highest_bit)
@@ -583,6 +558,10 @@ static unsigned char swap_entry_free(struct swap_info_struct *p,
                        swap_list.next = p->type;
                nr_swap_pages++;
                p->inuse_pages--;
+               frontswap_invalidate_page(p->type, offset);
+               if ((p->flags & SWP_BLKDEV) &&
+                               disk->fops->swap_slot_free_notify)
+                       disk->fops->swap_slot_free_notify(p->bdev, offset);
        }
 
        return usage;
@@ -679,6 +658,24 @@ int try_to_free_swap(struct page *page)
        if (page_swapcount(page))
                return 0;
 
+       /*
+        * Once hibernation has begun to create its image of memory,
+        * there's a danger that one of the calls to try_to_free_swap()
+        * - most probably a call from __try_to_reclaim_swap() while
+        * hibernation is allocating its own swap pages for the image,
+        * but conceivably even a call from memory reclaim - will free
+        * the swap from a page which has already been recorded in the
+        * image as a clean swapcache page, and then reuse its swap for
+        * another page of the image.  On waking from hibernation, the
+        * original page might be freed under memory pressure, then
+        * later read back in from swap, now with the wrong data.
+        *
+        * Hibration suspends storage while it is writing the image
+        * to disk so check that here.
+        */
+       if (pm_suspended_storage())
+               return 0;
+
        delete_from_swap_cache(page);
        SetPageDirty(page);
        return 1;
@@ -853,12 +850,13 @@ unsigned int count_swap_pages(int type, int free)
 static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
                unsigned long addr, swp_entry_t entry, struct page *page)
 {
-       struct mem_cgroup *ptr = NULL;
+       struct mem_cgroup *memcg;
        spinlock_t *ptl;
        pte_t *pte;
        int ret = 1;
 
-       if (mem_cgroup_try_charge_swapin(vma->vm_mm, page, GFP_KERNEL, &ptr)) {
+       if (mem_cgroup_try_charge_swapin(vma->vm_mm, page,
+                                        GFP_KERNEL, &memcg)) {
                ret = -ENOMEM;
                goto out_nolock;
        }
@@ -866,7 +864,7 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
        pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
        if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) {
                if (ret > 0)
-                       mem_cgroup_cancel_charge_swapin(ptr);
+                       mem_cgroup_cancel_charge_swapin(memcg);
                ret = 0;
                goto out;
        }
@@ -877,7 +875,7 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
        set_pte_at(vma->vm_mm, addr, pte,
                   pte_mkold(mk_pte(page, vma->vm_page_prot)));
        page_add_anon_rmap(page, vma, addr);
-       mem_cgroup_commit_charge_swapin(page, ptr);
+       mem_cgroup_commit_charge_swapin(page, memcg);
        swap_free(entry);
        /*
         * Move the page to the active list so it is not
@@ -937,7 +935,7 @@ static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
        pmd = pmd_offset(pud, addr);
        do {
                next = pmd_addr_end(addr, end);
-               if (pmd_none_or_clear_bad(pmd))
+               if (pmd_none_or_trans_huge_or_clear_bad(pmd))
                        continue;
                ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
                if (ret)
@@ -1021,11 +1019,12 @@ static int unuse_mm(struct mm_struct *mm,
 }
 
 /*
- * Scan swap_map from current position to next entry still in use.
+ * Scan swap_map (or frontswap_map if frontswap parameter is true)
+ * from current position to next entry still in use.
  * Recycle to start on reaching the end, returning 0 when empty.
  */
 static unsigned int find_next_to_unuse(struct swap_info_struct *si,
-                                       unsigned int prev)
+                                       unsigned int prev, bool frontswap)
 {
        unsigned int max = si->max;
        unsigned int i = prev;
@@ -1051,6 +1050,12 @@ static unsigned int find_next_to_unuse(struct swap_info_struct *si,
                        prev = 0;
                        i = 1;
                }
+               if (frontswap) {
+                       if (frontswap_test(si, i))
+                               break;
+                       else
+                               continue;
+               }
                count = si->swap_map[i];
                if (count && swap_count(count) != SWAP_MAP_BAD)
                        break;
@@ -1062,8 +1067,12 @@ static unsigned int find_next_to_unuse(struct swap_info_struct *si,
  * We completely avoid races by reading each swap page in advance,
  * and then search for the process using it.  All the necessary
  * page table adjustments can then be made atomically.
+ *
+ * if the boolean frontswap is true, only unuse pages_to_unuse pages;
+ * pages_to_unuse==0 means all pages; ignored if frontswap is false
  */
-static int try_to_unuse(unsigned int type)
+int try_to_unuse(unsigned int type, bool frontswap,
+                unsigned long pages_to_unuse)
 {
        struct swap_info_struct *si = swap_info[type];
        struct mm_struct *start_mm;
@@ -1096,7 +1105,7 @@ static int try_to_unuse(unsigned int type)
         * one pass through swap_map is enough, but not necessarily:
         * there are races when an instance of an entry might be missed.
         */
-       while ((i = find_next_to_unuse(si, i)) != 0) {
+       while ((i = find_next_to_unuse(si, i, frontswap)) != 0) {
                if (signal_pending(current)) {
                        retval = -EINTR;
                        break;
@@ -1263,6 +1272,10 @@ static int try_to_unuse(unsigned int type)
                 * interactive performance.
                 */
                cond_resched();
+               if (frontswap && pages_to_unuse > 0) {
+                       if (!--pages_to_unuse)
+                               break;
+               }
        }
 
        mmput(start_mm);
@@ -1521,6 +1534,39 @@ bad_bmap:
        goto out;
 }
 
+static void enable_swap_info(struct swap_info_struct *p, int prio,
+                               unsigned char *swap_map,
+                               unsigned long *frontswap_map)
+{
+       int i, prev;
+
+       spin_lock(&swap_lock);
+       if (prio >= 0)
+               p->prio = prio;
+       else
+               p->prio = --least_priority;
+       p->swap_map = swap_map;
+       frontswap_map_set(p, frontswap_map);
+       p->flags |= SWP_WRITEOK;
+       nr_swap_pages += p->pages;
+       total_swap_pages += p->pages;
+
+       /* insert swap space into swap_list: */
+       prev = -1;
+       for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
+               if (p->prio >= swap_info[i]->prio)
+                       break;
+               prev = i;
+       }
+       p->next = i;
+       if (prev < 0)
+               swap_list.head = swap_list.next = p->type;
+       else
+               swap_info[prev]->next = p->type;
+       frontswap_init(p->type);
+       spin_unlock(&swap_lock);
+}
+
 SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
 {
        struct swap_info_struct *p = NULL;
@@ -1529,12 +1575,15 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
        struct address_space *mapping;
        struct inode *inode;
        char *pathname;
+       int oom_score_adj;
        int i, type, prev;
        int err;
 
        if (!capable(CAP_SYS_ADMIN))
                return -EPERM;
 
+       BUG_ON(!current->mm);
+
        pathname = getname(specialfile);
        err = PTR_ERR(pathname);
        if (IS_ERR(pathname))
@@ -1562,7 +1611,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
                spin_unlock(&swap_lock);
                goto out_dput;
        }
-       if (!security_vm_enough_memory(p->pages))
+       if (!security_vm_enough_memory_mm(current->mm, p->pages))
                vm_unacct_memory(p->pages);
        else {
                err = -ENOMEM;
@@ -1587,37 +1636,22 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
        p->flags &= ~SWP_WRITEOK;
        spin_unlock(&swap_lock);
 
-       current->flags |= PF_OOM_ORIGIN;
-       err = try_to_unuse(type);
-       current->flags &= ~PF_OOM_ORIGIN;
+       oom_score_adj = test_set_oom_score_adj(OOM_SCORE_ADJ_MAX);
+       err = try_to_unuse(type, false, 0); /* force all pages to be unused */
+       compare_swap_oom_score_adj(OOM_SCORE_ADJ_MAX, oom_score_adj);
 
        if (err) {
+               /*
+                * reading p->prio and p->swap_map outside the lock is
+                * safe here because only sys_swapon and sys_swapoff
+                * change them, and there can be no other sys_swapon or
+                * sys_swapoff for this swap_info_struct at this point.
+                */
                /* re-insert swap space back into swap_list */
-               spin_lock(&swap_lock);
-               if (p->prio < 0)
-                       p->prio = --least_priority;
-               prev = -1;
-               for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
-                       if (p->prio >= swap_info[i]->prio)
-                               break;
-                       prev = i;
-               }
-               p->next = i;
-               if (prev < 0)
-                       swap_list.head = swap_list.next = type;
-               else
-                       swap_info[prev]->next = type;
-               nr_swap_pages += p->pages;
-               total_swap_pages += p->pages;
-               p->flags |= SWP_WRITEOK;
-               spin_unlock(&swap_lock);
+               enable_swap_info(p, p->prio, p->swap_map, frontswap_map_get(p));
                goto out_dput;
        }
 
-       /* wait for any unplug function to finish */
-       down_write(&swap_unplug_sem);
-       up_write(&swap_unplug_sem);
-
        destroy_swap_extents(p);
        if (p->flags & SWP_CONTINUED)
                free_swap_count_continuations(p);
@@ -1640,9 +1674,11 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
        swap_map = p->swap_map;
        p->swap_map = NULL;
        p->flags = 0;
+       frontswap_invalidate_area(type);
        spin_unlock(&swap_lock);
        mutex_unlock(&swapon_mutex);
        vfree(swap_map);
+       vfree(frontswap_map_get(p));
        /* Destroy swap account informatin */
        swap_cgroup_swapoff(type);
 
@@ -1650,7 +1686,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
        if (S_ISBLK(inode->i_mode)) {
                struct block_device *bdev = I_BDEV(inode);
                set_blocksize(bdev, p->old_block_size);
-               bd_release(bdev);
+               blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
        } else {
                mutex_lock(&inode->i_mutex);
                inode->i_flags &= ~S_SWAPFILE;
@@ -1658,6 +1694,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
        }
        filp_close(swap_file, NULL);
        err = 0;
+       atomic_inc(&proc_poll_event);
+       wake_up_interruptible(&proc_poll_wait);
 
 out_dput:
        filp_close(victim, NULL);
@@ -1666,6 +1704,20 @@ out:
 }
 
 #ifdef CONFIG_PROC_FS
+static unsigned swaps_poll(struct file *file, poll_table *wait)
+{
+       struct seq_file *seq = file->private_data;
+
+       poll_wait(file, &proc_poll_wait, wait);
+
+       if (seq->poll_event != atomic_read(&proc_poll_event)) {
+               seq->poll_event = atomic_read(&proc_poll_event);
+               return POLLIN | POLLRDNORM | POLLERR | POLLPRI;
+       }
+
+       return POLLIN | POLLRDNORM;
+}
+
 /* iterator */
 static void *swap_start(struct seq_file *swap, loff_t *pos)
 {
@@ -1749,7 +1801,16 @@ static const struct seq_operations swaps_op = {
 
 static int swaps_open(struct inode *inode, struct file *file)
 {
-       return seq_open(file, &swaps_op);
+       struct seq_file *seq;
+       int ret;
+
+       ret = seq_open(file, &swaps_op);
+       if (ret)
+               return ret;
+
+       seq = file->private_data;
+       seq->poll_event = atomic_read(&proc_poll_event);
+       return 0;
 }
 
 static const struct file_operations proc_swaps_operations = {
@@ -1757,6 +1818,7 @@ static const struct file_operations proc_swaps_operations = {
        .read           = seq_read,
        .llseek         = seq_lseek,
        .release        = seq_release,
+       .poll           = swaps_poll,
 };
 
 static int __init procswaps_init(void)
@@ -1776,49 +1838,24 @@ static int __init max_swapfiles_check(void)
 late_initcall(max_swapfiles_check);
 #endif
 
-/*
- * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
- *
- * The swapon system call
- */
-SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
+static struct swap_info_struct *alloc_swap_info(void)
 {
        struct swap_info_struct *p;
-       char *name = NULL;
-       struct block_device *bdev = NULL;
-       struct file *swap_file = NULL;
-       struct address_space *mapping;
        unsigned int type;
-       int i, prev;
-       int error;
-       union swap_header *swap_header;
-       unsigned int nr_good_pages;
-       int nr_extents = 0;
-       sector_t span;
-       unsigned long maxpages;
-       unsigned long swapfilepages;
-       unsigned char *swap_map = NULL;
-       struct page *page = NULL;
-       struct inode *inode = NULL;
-       int did_down = 0;
-
-       if (!capable(CAP_SYS_ADMIN))
-               return -EPERM;
 
        p = kzalloc(sizeof(*p), GFP_KERNEL);
        if (!p)
-               return -ENOMEM;
+               return ERR_PTR(-ENOMEM);
 
        spin_lock(&swap_lock);
        for (type = 0; type < nr_swapfiles; type++) {
                if (!(swap_info[type]->flags & SWP_USED))
                        break;
        }
-       error = -EPERM;
        if (type >= MAX_SWAPFILES) {
                spin_unlock(&swap_lock);
                kfree(p);
-               goto out;
+               return ERR_PTR(-EPERM);
        }
        if (type >= nr_swapfiles) {
                p->type = type;
@@ -1843,80 +1880,49 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
        p->next = -1;
        spin_unlock(&swap_lock);
 
-       name = getname(specialfile);
-       error = PTR_ERR(name);
-       if (IS_ERR(name)) {
-               name = NULL;
-               goto bad_swap_2;
-       }
-       swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
-       error = PTR_ERR(swap_file);
-       if (IS_ERR(swap_file)) {
-               swap_file = NULL;
-               goto bad_swap_2;
-       }
-
-       p->swap_file = swap_file;
-       mapping = swap_file->f_mapping;
-       inode = mapping->host;
-
-       error = -EBUSY;
-       for (i = 0; i < nr_swapfiles; i++) {
-               struct swap_info_struct *q = swap_info[i];
+       return p;
+}
 
-               if (i == type || !q->swap_file)
-                       continue;
-               if (mapping == q->swap_file->f_mapping)
-                       goto bad_swap;
-       }
+static int claim_swapfile(struct swap_info_struct *p, struct inode *inode)
+{
+       int error;
 
-       error = -EINVAL;
        if (S_ISBLK(inode->i_mode)) {
-               bdev = I_BDEV(inode);
-               error = bd_claim(bdev, sys_swapon);
+               p->bdev = bdgrab(I_BDEV(inode));
+               error = blkdev_get(p->bdev,
+                                  FMODE_READ | FMODE_WRITE | FMODE_EXCL,
+                                  sys_swapon);
                if (error < 0) {
-                       bdev = NULL;
-                       error = -EINVAL;
-                       goto bad_swap;
+                       p->bdev = NULL;
+                       return -EINVAL;
                }
-               p->old_block_size = block_size(bdev);
-               error = set_blocksize(bdev, PAGE_SIZE);
+               p->old_block_size = block_size(p->bdev);
+               error = set_blocksize(p->bdev, PAGE_SIZE);
                if (error < 0)
-                       goto bad_swap;
-               p->bdev = bdev;
+                       return error;
                p->flags |= SWP_BLKDEV;
        } else if (S_ISREG(inode->i_mode)) {
                p->bdev = inode->i_sb->s_bdev;
                mutex_lock(&inode->i_mutex);
-               did_down = 1;
-               if (IS_SWAPFILE(inode)) {
-                       error = -EBUSY;
-                       goto bad_swap;
-               }
-       } else {
-               goto bad_swap;
-       }
+               if (IS_SWAPFILE(inode))
+                       return -EBUSY;
+       } else
+               return -EINVAL;
 
-       swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
+       return 0;
+}
 
-       /*
-        * Read the swap header.
-        */
-       if (!mapping->a_ops->readpage) {
-               error = -EINVAL;
-               goto bad_swap;
-       }
-       page = read_mapping_page(mapping, 0, swap_file);
-       if (IS_ERR(page)) {
-               error = PTR_ERR(page);
-               goto bad_swap;
-       }
-       swap_header = kmap(page);
+static unsigned long read_swap_header(struct swap_info_struct *p,
+                                       union swap_header *swap_header,
+                                       struct inode *inode)
+{
+       int i;
+       unsigned long maxpages;
+       unsigned long swapfilepages;
 
        if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
                printk(KERN_ERR "Unable to find swap-space signature\n");
-               error = -EINVAL;
-               goto bad_swap;
+               return 0;
        }
 
        /* swap partition endianess hack... */
@@ -1932,8 +1938,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
                printk(KERN_WARNING
                       "Unable to handle swap header version %d\n",
                       swap_header->info.version);
-               error = -EINVAL;
-               goto bad_swap;
+               return 0;
        }
 
        p->lowest_bit  = 1;
@@ -1942,20 +1947,24 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 
        /*
         * Find out how many pages are allowed for a single swap
-        * device. There are two limiting factors: 1) the number of
-        * bits for the swap offset in the swp_entry_t type and
-        * 2) the number of bits in the a swap pte as defined by
-        * the different architectures. In order to find the
-        * largest possible bit mask a swap entry with swap type 0
+        * device. There are three limiting factors: 1) the number
+        * of bits for the swap offset in the swp_entry_t type, and
+        * 2) the number of bits in the swap pte as defined by the
+        * the different architectures, and 3) the number of free bits
+        * in an exceptional radix_tree entry. In order to find the
+        * largest possible bit mask, a swap entry with swap type 0
         * and swap offset ~0UL is created, encoded to a swap pte,
-        * decoded to a swp_entry_t again and finally the swap
+        * decoded to a swp_entry_t again, and finally the swap
         * offset is extracted. This will mask all the bits from
         * the initial ~0UL mask that can't be encoded in either
         * the swp_entry_t or the architecture definition of a
-        * swap pte.
+        * swap pte.  Then the same is done for a radix_tree entry.
         */
        maxpages = swp_offset(pte_to_swp_entry(
-                       swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1;
+                       swp_entry_to_pte(swp_entry(0, ~0UL))));
+       maxpages = swp_offset(radix_to_swp_entry(
+                       swp_to_radix_entry(swp_entry(0, maxpages)))) + 1;
+
        if (maxpages > swap_header->info.last_page) {
                maxpages = swap_header->info.last_page + 1;
                /* p->max is an unsigned int: don't overflow it */
@@ -1964,121 +1973,214 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
        }
        p->highest_bit = maxpages - 1;
 
-       error = -EINVAL;
        if (!maxpages)
-               goto bad_swap;
+               return 0;
+       swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
        if (swapfilepages && maxpages > swapfilepages) {
                printk(KERN_WARNING
                       "Swap area shorter than signature indicates\n");
-               goto bad_swap;
+               return 0;
        }
        if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
-               goto bad_swap;
+               return 0;
        if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
-               goto bad_swap;
+               return 0;
 
-       /* OK, set up the swap map and apply the bad block list */
-       swap_map = vmalloc(maxpages);
-       if (!swap_map) {
-               error = -ENOMEM;
-               goto bad_swap;
-       }
+       return maxpages;
+}
+
+static int setup_swap_map_and_extents(struct swap_info_struct *p,
+                                       union swap_header *swap_header,
+                                       unsigned char *swap_map,
+                                       unsigned long maxpages,
+                                       sector_t *span)
+{
+       int i;
+       unsigned int nr_good_pages;
+       int nr_extents;
 
-       memset(swap_map, 0, maxpages);
        nr_good_pages = maxpages - 1;   /* omit header page */
 
        for (i = 0; i < swap_header->info.nr_badpages; i++) {
                unsigned int page_nr = swap_header->info.badpages[i];
-               if (page_nr == 0 || page_nr > swap_header->info.last_page) {
-                       error = -EINVAL;
-                       goto bad_swap;
-               }
+               if (page_nr == 0 || page_nr > swap_header->info.last_page)
+                       return -EINVAL;
                if (page_nr < maxpages) {
                        swap_map[page_nr] = SWAP_MAP_BAD;
                        nr_good_pages--;
                }
        }
 
-       error = swap_cgroup_swapon(type, maxpages);
-       if (error)
-               goto bad_swap;
-
        if (nr_good_pages) {
                swap_map[0] = SWAP_MAP_BAD;
                p->max = maxpages;
                p->pages = nr_good_pages;
-               nr_extents = setup_swap_extents(p, &span);
-               if (nr_extents < 0) {
-                       error = nr_extents;
-                       goto bad_swap;
-               }
+               nr_extents = setup_swap_extents(p, span);
+               if (nr_extents < 0)
+                       return nr_extents;
                nr_good_pages = p->pages;
        }
        if (!nr_good_pages) {
                printk(KERN_WARNING "Empty swap-file\n");
+               return -EINVAL;
+       }
+
+       return nr_extents;
+}
+
+SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
+{
+       struct swap_info_struct *p;
+       char *name;
+       struct file *swap_file = NULL;
+       struct address_space *mapping;
+       int i;
+       int prio;
+       int error;
+       union swap_header *swap_header;
+       int nr_extents;
+       sector_t span;
+       unsigned long maxpages;
+       unsigned char *swap_map = NULL;
+       unsigned long *frontswap_map = NULL;
+       struct page *page = NULL;
+       struct inode *inode = NULL;
+
+       if (swap_flags & ~SWAP_FLAGS_VALID)
+               return -EINVAL;
+
+       if (!capable(CAP_SYS_ADMIN))
+               return -EPERM;
+
+       p = alloc_swap_info();
+       if (IS_ERR(p))
+               return PTR_ERR(p);
+
+       name = getname(specialfile);
+       if (IS_ERR(name)) {
+               error = PTR_ERR(name);
+               name = NULL;
+               goto bad_swap;
+       }
+       swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
+       if (IS_ERR(swap_file)) {
+               error = PTR_ERR(swap_file);
+               swap_file = NULL;
+               goto bad_swap;
+       }
+
+       p->swap_file = swap_file;
+       mapping = swap_file->f_mapping;
+
+       for (i = 0; i < nr_swapfiles; i++) {
+               struct swap_info_struct *q = swap_info[i];
+
+               if (q == p || !q->swap_file)
+                       continue;
+               if (mapping == q->swap_file->f_mapping) {
+                       error = -EBUSY;
+                       goto bad_swap;
+               }
+       }
+
+       inode = mapping->host;
+       /* If S_ISREG(inode->i_mode) will do mutex_lock(&inode->i_mutex); */
+       error = claim_swapfile(p, inode);
+       if (unlikely(error))
+               goto bad_swap;
+
+       /*
+        * Read the swap header.
+        */
+       if (!mapping->a_ops->readpage) {
                error = -EINVAL;
                goto bad_swap;
        }
+       page = read_mapping_page(mapping, 0, swap_file);
+       if (IS_ERR(page)) {
+               error = PTR_ERR(page);
+               goto bad_swap;
+       }
+       swap_header = kmap(page);
+
+       maxpages = read_swap_header(p, swap_header, inode);
+       if (unlikely(!maxpages)) {
+               error = -EINVAL;
+               goto bad_swap;
+       }
+
+       /* OK, set up the swap map and apply the bad block list */
+       swap_map = vzalloc(maxpages);
+       if (!swap_map) {
+               error = -ENOMEM;
+               goto bad_swap;
+       }
+
+       error = swap_cgroup_swapon(p->type, maxpages);
+       if (error)
+               goto bad_swap;
+
+       nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map,
+               maxpages, &span);
+       if (unlikely(nr_extents < 0)) {
+               error = nr_extents;
+               goto bad_swap;
+       }
+       /* frontswap enabled? set up bit-per-page map for frontswap */
+       if (frontswap_enabled)
+               frontswap_map = vzalloc(maxpages / sizeof(long));
 
        if (p->bdev) {
                if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
                        p->flags |= SWP_SOLIDSTATE;
                        p->cluster_next = 1 + (random32() % p->highest_bit);
                }
-               if (discard_swap(p) == 0)
+               if ((swap_flags & SWAP_FLAG_DISCARD) && discard_swap(p) == 0)
                        p->flags |= SWP_DISCARDABLE;
        }
 
        mutex_lock(&swapon_mutex);
-       spin_lock(&swap_lock);
+       prio = -1;
        if (swap_flags & SWAP_FLAG_PREFER)
-               p->prio =
+               prio =
                  (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
-       else
-               p->prio = --least_priority;
-       p->swap_map = swap_map;
-       p->flags |= SWP_WRITEOK;
-       nr_swap_pages += nr_good_pages;
-       total_swap_pages += nr_good_pages;
+       enable_swap_info(p, prio, swap_map, frontswap_map);
 
        printk(KERN_INFO "Adding %uk swap on %s.  "
-                       "Priority:%d extents:%d across:%lluk %s%s\n",
-               nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
+                       "Priority:%d extents:%d across:%lluk %s%s%s\n",
+               p->pages<<(PAGE_SHIFT-10), name, p->prio,
                nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
                (p->flags & SWP_SOLIDSTATE) ? "SS" : "",
-               (p->flags & SWP_DISCARDABLE) ? "D" : "");
+               (p->flags & SWP_DISCARDABLE) ? "D" : "",
+               (frontswap_map) ? "FS" : "");
 
-       /* insert swap space into swap_list: */
-       prev = -1;
-       for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
-               if (p->prio >= swap_info[i]->prio)
-                       break;
-               prev = i;
-       }
-       p->next = i;
-       if (prev < 0)
-               swap_list.head = swap_list.next = type;
-       else
-               swap_info[prev]->next = type;
-       spin_unlock(&swap_lock);
        mutex_unlock(&swapon_mutex);
+       atomic_inc(&proc_poll_event);
+       wake_up_interruptible(&proc_poll_wait);
+
+       if (S_ISREG(inode->i_mode))
+               inode->i_flags |= S_SWAPFILE;
        error = 0;
        goto out;
 bad_swap:
-       if (bdev) {
-               set_blocksize(bdev, p->old_block_size);
-               bd_release(bdev);
+       if (inode && S_ISBLK(inode->i_mode) && p->bdev) {
+               set_blocksize(p->bdev, p->old_block_size);
+               blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
        }
        destroy_swap_extents(p);
-       swap_cgroup_swapoff(type);
-bad_swap_2:
+       swap_cgroup_swapoff(p->type);
        spin_lock(&swap_lock);
        p->swap_file = NULL;
        p->flags = 0;
        spin_unlock(&swap_lock);
        vfree(swap_map);
-       if (swap_file)
+       if (swap_file) {
+               if (inode && S_ISREG(inode->i_mode)) {
+                       mutex_unlock(&inode->i_mutex);
+                       inode = NULL;
+               }
                filp_close(swap_file, NULL);
+       }
 out:
        if (page && !IS_ERR(page)) {
                kunmap(page);
@@ -2086,11 +2188,8 @@ out:
        }
        if (name)
                putname(name);
-       if (did_down) {
-               if (!error)
-                       inode->i_flags |= S_SWAPFILE;
+       if (inode && S_ISREG(inode->i_mode))
                mutex_unlock(&inode->i_mutex);
-       }
        return error;
 }
 
@@ -2222,58 +2321,6 @@ int swapcache_prepare(swp_entry_t entry)
 }
 
 /*
- * swap_lock prevents swap_map being freed. Don't grab an extra
- * reference on the swaphandle, it doesn't matter if it becomes unused.
- */
-int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
-{
-       struct swap_info_struct *si;
-       int our_page_cluster = page_cluster;
-       pgoff_t target, toff;
-       pgoff_t base, end;
-       int nr_pages = 0;
-
-       if (!our_page_cluster)  /* no readahead */
-               return 0;
-
-       si = swap_info[swp_type(entry)];
-       target = swp_offset(entry);
-       base = (target >> our_page_cluster) << our_page_cluster;
-       end = base + (1 << our_page_cluster);
-       if (!base)              /* first page is swap header */
-               base++;
-
-       spin_lock(&swap_lock);
-       if (end > si->max)      /* don't go beyond end of map */
-               end = si->max;
-
-       /* Count contiguous allocated slots above our target */
-       for (toff = target; ++toff < end; nr_pages++) {
-               /* Don't read in free or bad pages */
-               if (!si->swap_map[toff])
-                       break;
-               if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
-                       break;
-       }
-       /* Count contiguous allocated slots below our target */
-       for (toff = target; --toff >= base; nr_pages++) {
-               /* Don't read in free or bad pages */
-               if (!si->swap_map[toff])
-                       break;
-               if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
-                       break;
-       }
-       spin_unlock(&swap_lock);
-
-       /*
-        * Indicate starting offset, and return number of pages to get:
-        * if only 1, say 0, since there's then no readahead to be done.
-        */
-       *offset = ++toff;
-       return nr_pages? ++nr_pages: 0;
-}
-
-/*
  * add_swap_count_continuation - called when a swap count is duplicated
  * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
  * page of the original vmalloc'ed swap_map, to hold the continuation count
@@ -2359,9 +2406,9 @@ int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask)
                if (!(count & COUNT_CONTINUED))
                        goto out;
 
-               map = kmap_atomic(list_page, KM_USER0) + offset;
+               map = kmap_atomic(list_page) + offset;
                count = *map;
-               kunmap_atomic(map, KM_USER0);
+               kunmap_atomic(map);
 
                /*
                 * If this continuation count now has some space in it,
@@ -2404,7 +2451,7 @@ static bool swap_count_continued(struct swap_info_struct *si,
 
        offset &= ~PAGE_MASK;
        page = list_entry(head->lru.next, struct page, lru);
-       map = kmap_atomic(page, KM_USER0) + offset;
+       map = kmap_atomic(page) + offset;
 
        if (count == SWAP_MAP_MAX)      /* initial increment from swap_map */
                goto init_map;          /* jump over SWAP_CONT_MAX checks */
@@ -2414,26 +2461,26 @@ static bool swap_count_continued(struct swap_info_struct *si,
                 * Think of how you add 1 to 999
                 */
                while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) {
-                       kunmap_atomic(map, KM_USER0);
+                       kunmap_atomic(map);
                        page = list_entry(page->lru.next, struct page, lru);
                        BUG_ON(page == head);
-                       map = kmap_atomic(page, KM_USER0) + offset;
+                       map = kmap_atomic(page) + offset;
                }
                if (*map == SWAP_CONT_MAX) {
-                       kunmap_atomic(map, KM_USER0);
+                       kunmap_atomic(map);
                        page = list_entry(page->lru.next, struct page, lru);
                        if (page == head)
                                return false;   /* add count continuation */
-                       map = kmap_atomic(page, KM_USER0) + offset;
+                       map = kmap_atomic(page) + offset;
 init_map:              *map = 0;               /* we didn't zero the page */
                }
                *map += 1;
-               kunmap_atomic(map, KM_USER0);
+               kunmap_atomic(map);
                page = list_entry(page->lru.prev, struct page, lru);
                while (page != head) {
-                       map = kmap_atomic(page, KM_USER0) + offset;
+                       map = kmap_atomic(page) + offset;
                        *map = COUNT_CONTINUED;
-                       kunmap_atomic(map, KM_USER0);
+                       kunmap_atomic(map);
                        page = list_entry(page->lru.prev, struct page, lru);
                }
                return true;                    /* incremented */
@@ -2444,22 +2491,22 @@ init_map:               *map = 0;               /* we didn't zero the page */
                 */
                BUG_ON(count != COUNT_CONTINUED);
                while (*map == COUNT_CONTINUED) {
-                       kunmap_atomic(map, KM_USER0);
+                       kunmap_atomic(map);
                        page = list_entry(page->lru.next, struct page, lru);
                        BUG_ON(page == head);
-                       map = kmap_atomic(page, KM_USER0) + offset;
+                       map = kmap_atomic(page) + offset;
                }
                BUG_ON(*map == 0);
                *map -= 1;
                if (*map == 0)
                        count = 0;
-               kunmap_atomic(map, KM_USER0);
+               kunmap_atomic(map);
                page = list_entry(page->lru.prev, struct page, lru);
                while (page != head) {
-                       map = kmap_atomic(page, KM_USER0) + offset;
+                       map = kmap_atomic(page) + offset;
                        *map = SWAP_CONT_MAX | count;
                        count = COUNT_CONTINUED;
-                       kunmap_atomic(map, KM_USER0);
+                       kunmap_atomic(map);
                        page = list_entry(page->lru.prev, struct page, lru);
                }
                return count == COUNT_CONTINUED;