linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Cesar Eduardo Barros <cesarb@cesarb.net>
To: Eric B Munson <emunson@mgebm.net>
Cc: linux-mm@kvack.org, Cesar Eduardo Barros <cesarb@cesarb.net>
Subject: [PATCHv2 16/24] sys_swapon: separate parsing of swapfile header
Date: Tue,  1 Mar 2011 20:28:40 -0300	[thread overview]
Message-ID: <1299022128-6239-17-git-send-email-cesarb@cesarb.net> (raw)
In-Reply-To: <1299022128-6239-1-git-send-email-cesarb@cesarb.net>

Move the code which parses and checks the swapfile header (except for
the bad block list) to a separate function. Only code movement, no
functional changes.

Signed-off-by: Cesar Eduardo Barros <cesarb@cesarb.net>
---
 mm/swapfile.c |  140 ++++++++++++++++++++++++++++++++-------------------------
 1 files changed, 78 insertions(+), 62 deletions(-)

diff --git a/mm/swapfile.c b/mm/swapfile.c
index f3f413b..a56e6fe 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1918,6 +1918,82 @@ static int claim_swapfile(struct swap_info_struct *p, struct inode *inode)
 	return 0;
 }
 
+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");
+		goto bad_swap;
+	}
+
+	/* swap partition endianess hack... */
+	if (swab32(swap_header->info.version) == 1) {
+		swab32s(&swap_header->info.version);
+		swab32s(&swap_header->info.last_page);
+		swab32s(&swap_header->info.nr_badpages);
+		for (i = 0; i < swap_header->info.nr_badpages; i++)
+			swab32s(&swap_header->info.badpages[i]);
+	}
+	/* Check the swap header's sub-version */
+	if (swap_header->info.version != 1) {
+		printk(KERN_WARNING
+		       "Unable to handle swap header version %d\n",
+		       swap_header->info.version);
+		goto bad_swap;
+	}
+
+	p->lowest_bit  = 1;
+	p->cluster_next = 1;
+	p->cluster_nr = 0;
+
+	/*
+	 * 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
+	 * and swap offset ~0UL is created, encoded to a swap pte,
+	 * 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.
+	 */
+	maxpages = swp_offset(pte_to_swp_entry(
+			swp_entry_to_pte(swp_entry(0, ~0UL)))) + 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 */
+		if ((unsigned int)maxpages == 0)
+			maxpages = UINT_MAX;
+	}
+	p->highest_bit = maxpages - 1;
+
+	if (!maxpages)
+		goto bad_swap;
+	swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
+	if (swapfilepages && maxpages > swapfilepages) {
+		printk(KERN_WARNING
+		       "Swap area shorter than signature indicates\n");
+		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;
+
+	return maxpages;
+
+bad_swap:
+	return 0;
+}
+
 SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 {
 	struct swap_info_struct *p;
@@ -1931,7 +2007,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 	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;
@@ -1989,71 +2064,12 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 	}
 	swap_header = kmap(page);
 
-	if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
-		printk(KERN_ERR "Unable to find swap-space signature\n");
+	maxpages = read_swap_header(p, swap_header, inode);
+	if (unlikely(!maxpages)) {
 		error = -EINVAL;
 		goto bad_swap;
 	}
 
-	/* swap partition endianess hack... */
-	if (swab32(swap_header->info.version) == 1) {
-		swab32s(&swap_header->info.version);
-		swab32s(&swap_header->info.last_page);
-		swab32s(&swap_header->info.nr_badpages);
-		for (i = 0; i < swap_header->info.nr_badpages; i++)
-			swab32s(&swap_header->info.badpages[i]);
-	}
-	/* Check the swap header's sub-version */
-	if (swap_header->info.version != 1) {
-		printk(KERN_WARNING
-		       "Unable to handle swap header version %d\n",
-		       swap_header->info.version);
-		error = -EINVAL;
-		goto bad_swap;
-	}
-
-	p->lowest_bit  = 1;
-	p->cluster_next = 1;
-	p->cluster_nr = 0;
-
-	/*
-	 * 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
-	 * and swap offset ~0UL is created, encoded to a swap pte,
-	 * 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.
-	 */
-	maxpages = swp_offset(pte_to_swp_entry(
-			swp_entry_to_pte(swp_entry(0, ~0UL)))) + 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 */
-		if ((unsigned int)maxpages == 0)
-			maxpages = UINT_MAX;
-	}
-	p->highest_bit = maxpages - 1;
-
-	error = -EINVAL;
-	if (!maxpages)
-		goto bad_swap;
-	swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
-	if (swapfilepages && maxpages > swapfilepages) {
-		printk(KERN_WARNING
-		       "Swap area shorter than signature indicates\n");
-		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;
-
 	/* OK, set up the swap map and apply the bad block list */
 	swap_map = vzalloc(maxpages);
 	if (!swap_map) {
-- 
1.7.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

  parent reply	other threads:[~2011-03-01 23:29 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-12 18:48 [PATCH 00/24] Refactor sys_swapon Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 01/24] sys_swapon: use vzalloc instead of vmalloc/memset Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 02/24] sys_swapon: remove changelog from function comment Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 03/24] sys_swapon: do not depend on "type" after allocation Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 04/24] sys_swapon: separate swap_info allocation Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 05/24] sys_swapon: simplify error return from " Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 06/24] sys_swapon: simplify error flow in alloc_swap_info Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 07/24] sys_swapon: remove initial value of name variable Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 08/24] sys_swapon: move setting of error nearer use Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 09/24] sys_swapon: remove did_down variable Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 10/24] sys_swapon: remove bdev variable Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 11/24] sys_swapon: do only cleanup in the cleanup blocks Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 12/24] sys_swapon: use a single error label Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 13/24] sys_swapon: separate bdev claim and inode lock Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 14/24] sys_swapon: simplify error flow in claim_swapfile Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 15/24] sys_swapon: move setting of swapfilepages near use Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 16/24] sys_swapon: separate parsing of swapfile header Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 17/24] sys_swapon: simplify error flow in read_swap_header Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 18/24] sys_swapon: call swap_cgroup_swapon earlier Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 19/24] sys_swapon: separate parsing of bad blocks and extents Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 20/24] sys_swapon: simplify error flow in setup_swap_map_and_extents Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 21/24] sys_swapon: remove nr_good_pages variable Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 22/24] sys_swapon: move printk outside lock Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 23/24] sys_swapoff: change order to match sys_swapon Cesar Eduardo Barros
2011-02-12 18:49 ` [PATCH 24/24] sys_swapon: separate final enabling of the swapfile Cesar Eduardo Barros
2011-03-01 18:20 ` [PATCH 00/24] Refactor sys_swapon Eric B Munson
2011-03-01 23:23   ` Cesar Eduardo Barros
2011-03-01 23:28     ` [PATCHv2 " Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 01/24] sys_swapon: use vzalloc instead of vmalloc/memset Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 02/24] sys_swapon: remove changelog from function comment Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 03/24] sys_swapon: do not depend on "type" after allocation Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 04/24] sys_swapon: separate swap_info allocation Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 05/24] sys_swapon: simplify error return from " Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 06/24] sys_swapon: simplify error flow in alloc_swap_info Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 07/24] sys_swapon: remove initial value of name variable Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 08/24] sys_swapon: move setting of error nearer use Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 09/24] sys_swapon: remove did_down variable Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 10/24] sys_swapon: remove bdev variable Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 11/24] sys_swapon: do only cleanup in the cleanup blocks Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 12/24] sys_swapon: use a single error label Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 13/24] sys_swapon: separate bdev claim and inode lock Cesar Eduardo Barros
2011-03-02 21:40         ` Eric B Munson
2011-03-02 23:02           ` Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 14/24] sys_swapon: simplify error flow in claim_swapfile Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 15/24] sys_swapon: move setting of swapfilepages near use Cesar Eduardo Barros
2011-03-01 23:28       ` Cesar Eduardo Barros [this message]
2011-03-01 23:28       ` [PATCHv2 17/24] sys_swapon: simplify error flow in read_swap_header Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 18/24] sys_swapon: call swap_cgroup_swapon earlier Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 19/24] sys_swapon: separate parsing of bad blocks and extents Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 20/24] sys_swapon: simplify error flow in setup_swap_map_and_extents Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 21/24] sys_swapon: remove nr_good_pages variable Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 22/24] sys_swapon: move printk outside lock Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 23/24] sys_swapoff: change order to match sys_swapon Cesar Eduardo Barros
2011-03-01 23:28       ` [PATCHv2 24/24] sys_swapon: separate final enabling of the swapfile Cesar Eduardo Barros
2011-03-02 21:06       ` [PATCHv2 00/24] Refactor sys_swapon Eric B Munson
2011-03-02 21:43       ` Eric B Munson
2011-03-03 16:15       ` Eric B Munson
2011-03-04 22:54         ` Cesar Eduardo Barros
2011-03-04 22:58           ` Eric B Munson
2011-03-05 16:42 Cesar Eduardo Barros
2011-03-05 16:42 ` [PATCHv2 16/24] sys_swapon: separate parsing of swapfile header Cesar Eduardo Barros
2011-03-07  9:57   ` KAMEZAWA Hiroyuki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1299022128-6239-17-git-send-email-cesarb@cesarb.net \
    --to=cesarb@cesarb.net \
    --cc=emunson@mgebm.net \
    --cc=linux-mm@kvack.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox