aboutsummaryrefslogtreecommitdiffstats
path: root/mm/swapfile.c
diff options
context:
space:
mode:
authorCesar Eduardo Barros <cesarb@cesarb.net>2011-03-22 19:33:32 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2011-03-22 20:44:08 -0400
commit915d4d7bc0d719f2f0907273c01967d38751c625 (patch)
treedeeab9a10407fa37dedb24d1adc5a83626f740f6 /mm/swapfile.c
parent1421ef3cd15b87ef949e965efeb1e527479d3f75 (diff)
sys_swapon: separate parsing of bad blocks and extents
Move the code which parses the bad block list and the extents to a separate function. Only code movement, no functional changes. This change uses the fact that, after the success path, nr_good_pages == p->pages. Signed-off-by: Cesar Eduardo Barros <cesarb@cesarb.net> Tested-by: Eric B Munson <emunson@mgebm.net> Acked-by: Eric B Munson <emunson@mgebm.net> Reviewed-by: Pekka Enberg <penberg@kernel.org> Reviewed-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: Hugh Dickins <hughd@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm/swapfile.c')
-rw-r--r--mm/swapfile.c83
1 files changed, 54 insertions, 29 deletions
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 10f2b33805f6..a0f39f9dc5a5 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1991,6 +1991,54 @@ static unsigned long read_swap_header(struct swap_info_struct *p,
1991 return maxpages; 1991 return maxpages;
1992} 1992}
1993 1993
1994static int setup_swap_map_and_extents(struct swap_info_struct *p,
1995 union swap_header *swap_header,
1996 unsigned char *swap_map,
1997 unsigned long maxpages,
1998 sector_t *span)
1999{
2000 int i;
2001 int error;
2002 unsigned int nr_good_pages;
2003 int nr_extents;
2004
2005 nr_good_pages = maxpages - 1; /* omit header page */
2006
2007 for (i = 0; i < swap_header->info.nr_badpages; i++) {
2008 unsigned int page_nr = swap_header->info.badpages[i];
2009 if (page_nr == 0 || page_nr > swap_header->info.last_page) {
2010 error = -EINVAL;
2011 goto bad_swap;
2012 }
2013 if (page_nr < maxpages) {
2014 swap_map[page_nr] = SWAP_MAP_BAD;
2015 nr_good_pages--;
2016 }
2017 }
2018
2019 if (nr_good_pages) {
2020 swap_map[0] = SWAP_MAP_BAD;
2021 p->max = maxpages;
2022 p->pages = nr_good_pages;
2023 nr_extents = setup_swap_extents(p, span);
2024 if (nr_extents < 0) {
2025 error = nr_extents;
2026 goto bad_swap;
2027 }
2028 nr_good_pages = p->pages;
2029 }
2030 if (!nr_good_pages) {
2031 printk(KERN_WARNING "Empty swap-file\n");
2032 error = -EINVAL;
2033 goto bad_swap;
2034 }
2035
2036 return nr_extents;
2037
2038bad_swap:
2039 return error;
2040}
2041
1994SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) 2042SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
1995{ 2043{
1996 struct swap_info_struct *p; 2044 struct swap_info_struct *p;
@@ -2001,7 +2049,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
2001 int error; 2049 int error;
2002 union swap_header *swap_header; 2050 union swap_header *swap_header;
2003 unsigned int nr_good_pages; 2051 unsigned int nr_good_pages;
2004 int nr_extents = 0; 2052 int nr_extents;
2005 sector_t span; 2053 sector_t span;
2006 unsigned long maxpages; 2054 unsigned long maxpages;
2007 unsigned char *swap_map = NULL; 2055 unsigned char *swap_map = NULL;
@@ -2078,36 +2126,13 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
2078 if (error) 2126 if (error)
2079 goto bad_swap; 2127 goto bad_swap;
2080 2128
2081 nr_good_pages = maxpages - 1; /* omit header page */ 2129 nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map,
2082 2130 maxpages, &span);
2083 for (i = 0; i < swap_header->info.nr_badpages; i++) { 2131 if (unlikely(nr_extents < 0)) {
2084 unsigned int page_nr = swap_header->info.badpages[i]; 2132 error = nr_extents;
2085 if (page_nr == 0 || page_nr > swap_header->info.last_page) {
2086 error = -EINVAL;
2087 goto bad_swap;
2088 }
2089 if (page_nr < maxpages) {
2090 swap_map[page_nr] = SWAP_MAP_BAD;
2091 nr_good_pages--;
2092 }
2093 }
2094
2095 if (nr_good_pages) {
2096 swap_map[0] = SWAP_MAP_BAD;
2097 p->max = maxpages;
2098 p->pages = nr_good_pages;
2099 nr_extents = setup_swap_extents(p, &span);
2100 if (nr_extents < 0) {
2101 error = nr_extents;
2102 goto bad_swap;
2103 }
2104 nr_good_pages = p->pages;
2105 }
2106 if (!nr_good_pages) {
2107 printk(KERN_WARNING "Empty swap-file\n");
2108 error = -EINVAL;
2109 goto bad_swap; 2133 goto bad_swap;
2110 } 2134 }
2135 nr_good_pages = p->pages;
2111 2136
2112 if (p->bdev) { 2137 if (p->bdev) {
2113 if (blk_queue_nonrot(bdev_get_queue(p->bdev))) { 2138 if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {