]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - mm/swapfile.c
[PATCH] swap: move destroy_swap_extents calls
[linux-2.6-omap-h63xx.git] / mm / swapfile.c
index da48405cd9a3199d9088d1221864fa77a76a108d..4b39e9501d44a97e2c7bae6ba2c01bc86f591760 100644 (file)
@@ -276,61 +276,37 @@ void swap_free(swp_entry_t entry)
 }
 
 /*
- * Check if we're the only user of a swap page,
- * when the page is locked.
+ * How many references to page are currently swapped out?
  */
-static int exclusive_swap_page(struct page *page)
+static inline int page_swapcount(struct page *page)
 {
-       int retval = 0;
-       struct swap_info_struct * p;
+       int count = 0;
+       struct swap_info_struct *p;
        swp_entry_t entry;
 
        entry.val = page->private;
        p = swap_info_get(entry);
        if (p) {
-               /* Is the only swap cache user the cache itself? */
-               if (p->swap_map[swp_offset(entry)] == 1) {
-                       /* Recheck the page count with the swapcache lock held.. */
-                       write_lock_irq(&swapper_space.tree_lock);
-                       if (page_count(page) == 2)
-                               retval = 1;
-                       write_unlock_irq(&swapper_space.tree_lock);
-               }
+               /* Subtract the 1 for the swap cache itself */
+               count = p->swap_map[swp_offset(entry)] - 1;
                swap_info_put(p);
        }
-       return retval;
+       return count;
 }
 
 /*
  * We can use this swap cache entry directly
  * if there are no other references to it.
- *
- * Here "exclusive_swap_page()" does the real
- * work, but we opportunistically check whether
- * we need to get all the locks first..
  */
 int can_share_swap_page(struct page *page)
 {
-       int retval = 0;
+       int count;
 
-       if (!PageLocked(page))
-               BUG();
-       switch (page_count(page)) {
-       case 3:
-               if (!PagePrivate(page))
-                       break;
-               /* Fallthrough */
-       case 2:
-               if (!PageSwapCache(page))
-                       break;
-               retval = exclusive_swap_page(page);
-               break;
-       case 1:
-               if (PageReserved(page))
-                       break;
-               retval = 1;
-       }
-       return retval;
+       BUG_ON(!PageLocked(page));
+       count = page_mapcount(page);
+       if (count <= 1 && PageSwapCache(page))
+               count += page_swapcount(page);
+       return count == 1;
 }
 
 /*
@@ -529,9 +505,10 @@ static int unuse_mm(struct mm_struct *mm,
 
        if (!down_read_trylock(&mm->mmap_sem)) {
                /*
-                * Our reference to the page stops try_to_unmap_one from
-                * unmapping its ptes, so swapoff can make progress.
+                * Activate page so shrink_cache is unlikely to unmap its
+                * ptes while lock is dropped, so swapoff can make progress.
                 */
+               activate_page(page);
                unlock_page(page);
                down_read(&mm->mmap_sem);
                lock_page(page);
@@ -949,7 +926,7 @@ add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
  * requirements, they are simply tossed out - we will never use those blocks
  * for swapping.
  *
- * For S_ISREG swapfiles we hold i_sem across the life of the swapon.  This
+ * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon.  This
  * prevents root from shooting her foot off by ftruncating an in-use swapfile,
  * which will scribble on the fs.
  *
@@ -1031,8 +1008,9 @@ reprobe:
        }
        ret = 0;
        if (page_no == 0)
-               ret = -EINVAL;
+               page_no = 1;    /* force Empty message */
        sis->max = page_no;
+       sis->pages = page_no - 1;
        sis->highest_bit = page_no - 1;
 done:
        sis->curr_swap_extent = list_entry(sis->extent_list.prev,
@@ -1151,6 +1129,7 @@ asmlinkage long sys_swapoff(const char __user * specialfile)
                swap_list_unlock();
                goto out_dput;
        }
+       destroy_swap_extents(p);
        down(&swapon_sem);
        swap_list_lock();
        drain_mmlist();
@@ -1161,7 +1140,6 @@ asmlinkage long sys_swapoff(const char __user * specialfile)
        swap_map = p->swap_map;
        p->swap_map = NULL;
        p->flags = 0;
-       destroy_swap_extents(p);
        swap_device_unlock(p);
        swap_list_unlock();
        up(&swapon_sem);
@@ -1469,6 +1447,10 @@ asmlinkage long sys_swapon(const char __user * specialfile, int swap_flags)
                p->highest_bit = maxpages - 1;
 
                error = -EINVAL;
+               if (!maxpages)
+                       goto bad_swap;
+               if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
+                       goto bad_swap;
                if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
                        goto bad_swap;
                
@@ -1493,25 +1475,27 @@ asmlinkage long sys_swapon(const char __user * specialfile, int swap_flags)
                if (error) 
                        goto bad_swap;
        }
-       
+
        if (swapfilesize && maxpages > swapfilesize) {
                printk(KERN_WARNING
                       "Swap area shorter than signature indicates\n");
                error = -EINVAL;
                goto bad_swap;
        }
+       if (nr_good_pages) {
+               p->swap_map[0] = SWAP_MAP_BAD;
+               p->max = maxpages;
+               p->pages = nr_good_pages;
+               error = setup_swap_extents(p);
+               if (error)
+                       goto bad_swap;
+               nr_good_pages = p->pages;
+       }
        if (!nr_good_pages) {
                printk(KERN_WARNING "Empty swap-file\n");
                error = -EINVAL;
                goto bad_swap;
        }
-       p->swap_map[0] = SWAP_MAP_BAD;
-       p->max = maxpages;
-       p->pages = nr_good_pages;
-
-       error = setup_swap_extents(p);
-       if (error)
-               goto bad_swap;
 
        down(&swapon_sem);
        swap_list_lock();
@@ -1547,6 +1531,7 @@ bad_swap:
                set_blocksize(bdev, p->old_block_size);
                bd_release(bdev);
        }
+       destroy_swap_extents(p);
 bad_swap_2:
        swap_list_lock();
        swap_map = p->swap_map;
@@ -1556,7 +1541,6 @@ bad_swap_2:
        if (!(swap_flags & SWAP_FLAG_PREFER))
                ++least_priority;
        swap_list_unlock();
-       destroy_swap_extents(p);
        vfree(swap_map);
        if (swap_file)
                filp_close(swap_file, NULL);