public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Rafael J. Wysocki" <rjw@sisk.pl>
To: Pavel Machek <pavel@ucw.cz>
Cc: Andrew Morton <akpm@osdl.org>, LKML <linux-kernel@vger.kernel.org>
Subject: [PATCH -mm 1/6] swsusp: Use device and offset to intentify swap areas
Date: Wed, 20 Sep 2006 21:31:37 +0200	[thread overview]
Message-ID: <200609202131.38206.rjw@sisk.pl> (raw)
In-Reply-To: <200609202120.58082.rjw@sisk.pl>

The Linux kernel handles swap files almost in the same way as it handles swap
partitions and there are only two differences between these two types of swap
areas:
(1) swap files need not be contiguous,
(2) the header of a swap file is not in the first block of the partition that
holds it.  From the swsusp's point of view (1) is not a problem, because it is
already taken care of by the swap-handling code, but (2) has to be taken into
consideration.

In principle the location of a swap file's header may be determined with the
help of appropriate filesystem driver.  Unfortunately, however, it requires the
filesystem holding the swap file to be mounted, and if this filesystem is
journaled, it cannot be mounted during a resume from disk.  For this reason
we need some other means by which swap areas can be identified.

For example, to identify a swap area we can use the partition that holds the
area and the offset from the beginning of this partition at which the swap
header is located.

The following patch allows swsusp to identify swap areas this way.  It changes
swap_type_of() so that it takes an additional argument representing an offset
of the swap header within the partition represented by its first argument.

Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
---
 include/linux/swap.h |    2 +-
 kernel/power/swap.c  |    2 +-
 kernel/power/user.c  |    5 +++--
 mm/swapfile.c        |   38 ++++++++++++++++++++++++++------------
 4 files changed, 31 insertions(+), 16 deletions(-)

Index: linux-2.6.18-rc7-mm1/mm/swapfile.c
===================================================================
--- linux-2.6.18-rc7-mm1.orig/mm/swapfile.c
+++ linux-2.6.18-rc7-mm1/mm/swapfile.c
@@ -427,34 +427,48 @@ void free_swap_and_cache(swp_entry_t ent
 
 #ifdef CONFIG_SOFTWARE_SUSPEND
 /*
- * Find the swap type that corresponds to given device (if any)
+ * Find the swap type that corresponds to given device (if any).
  *
- * This is needed for software suspend and is done in such a way that inode
- * aliasing is allowed.
+ * @offset - number of the PAGE_SIZE-sized block of the device, starting
+ * from 0, in which the swap header is expected to be located.
+ *
+ * This is needed for the suspend to disk (aka swsusp).
  */
-int swap_type_of(dev_t device)
+int swap_type_of(dev_t device, sector_t offset)
 {
+	struct block_device *bdev = NULL;
 	int i;
 
+	if (device)
+		bdev = bdget(device);
+
 	spin_lock(&swap_lock);
 	for (i = 0; i < nr_swapfiles; i++) {
-		struct inode *inode;
+		struct swap_info_struct *sis = swap_info + i;
 
-		if (!(swap_info[i].flags & SWP_WRITEOK))
+		if (!(sis->flags & SWP_WRITEOK))
 			continue;
 
-		if (!device) {
+		if (!bdev) {
 			spin_unlock(&swap_lock);
 			return i;
 		}
-		inode = swap_info[i].swap_file->f_dentry->d_inode;
-		if (S_ISBLK(inode->i_mode) &&
-		    device == MKDEV(imajor(inode), iminor(inode))) {
-			spin_unlock(&swap_lock);
-			return i;
+		if (bdev == sis->bdev) {
+			struct swap_extent *se;
+
+			se = list_entry(sis->extent_list.next,
+					struct swap_extent, list);
+			if (se->start_block == offset) {
+				spin_unlock(&swap_lock);
+				bdput(bdev);
+				return i;
+			}
 		}
 	}
 	spin_unlock(&swap_lock);
+	if (bdev)
+		bdput(bdev);
+
 	return -ENODEV;
 }
 
Index: linux-2.6.18-rc7-mm1/include/linux/swap.h
===================================================================
--- linux-2.6.18-rc7-mm1.orig/include/linux/swap.h
+++ linux-2.6.18-rc7-mm1/include/linux/swap.h
@@ -249,7 +249,7 @@ extern int swap_duplicate(swp_entry_t);
 extern int valid_swaphandles(swp_entry_t, unsigned long *);
 extern void swap_free(swp_entry_t);
 extern void free_swap_and_cache(swp_entry_t);
-extern int swap_type_of(dev_t);
+extern int swap_type_of(dev_t, sector_t);
 extern unsigned int count_swap_pages(int, int);
 extern sector_t map_swap_page(struct swap_info_struct *, pgoff_t);
 extern struct swap_info_struct *get_swap_info_struct(unsigned);
Index: linux-2.6.18-rc7-mm1/kernel/power/swap.c
===================================================================
--- linux-2.6.18-rc7-mm1.orig/kernel/power/swap.c
+++ linux-2.6.18-rc7-mm1/kernel/power/swap.c
@@ -74,7 +74,7 @@ static int mark_swapfiles(swp_entry_t st
 
 static int swsusp_swap_check(void) /* This is called before saving image */
 {
-	int res = swap_type_of(swsusp_resume_device);
+	int res = swap_type_of(swsusp_resume_device, 0);
 
 	if (res >= 0) {
 		root_swap = res;
Index: linux-2.6.18-rc7-mm1/kernel/power/user.c
===================================================================
--- linux-2.6.18-rc7-mm1.orig/kernel/power/user.c
+++ linux-2.6.18-rc7-mm1/kernel/power/user.c
@@ -54,7 +54,8 @@ static int snapshot_open(struct inode *i
 	filp->private_data = data;
 	memset(&data->handle, 0, sizeof(struct snapshot_handle));
 	if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
-		data->swap = swsusp_resume_device ? swap_type_of(swsusp_resume_device) : -1;
+		data->swap = swsusp_resume_device ?
+				swap_type_of(swsusp_resume_device, 0) : -1;
 		data->mode = O_RDONLY;
 	} else {
 		data->swap = -1;
@@ -264,7 +265,7 @@ static int snapshot_ioctl(struct inode *
 			 * so we need to recode them
 			 */
 			if (old_decode_dev(arg)) {
-				data->swap = swap_type_of(old_decode_dev(arg));
+				data->swap = swap_type_of(old_decode_dev(arg), 0);
 				if (data->swap < 0)
 					error = -ENODEV;
 			} else {


  reply	other threads:[~2006-09-20 21:20 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-09-20 19:20 [PATCH -mm 0/6] swsusp: Add support for swap files Rafael J. Wysocki
2006-09-20 19:31 ` Rafael J. Wysocki [this message]
2006-09-21 21:27   ` [PATCH -mm 1/6] swsusp: Use device and offset to intentify swap areas Pavel Machek
2006-09-20 19:34 ` [PATCH -mm 2/6] swsusp: Rearrange swap-handling code Rafael J. Wysocki
2006-09-21 21:28   ` Pavel Machek
2006-09-20 19:41 ` [PATCH -mm 3/6] swsusp: Use block device offsets to identify swap locations Rafael J. Wysocki
2006-09-21 21:30   ` Pavel Machek
2006-09-21 22:21     ` Rafael J. Wysocki
2006-09-20 19:46 ` [PATCH -mm 4/6] swsusp: Add resume_offset command line parameter Rafael J. Wysocki
2006-09-21 21:31   ` Pavel Machek
2006-09-21 22:18     ` Rafael J. Wysocki
2006-09-22 13:34       ` Pavel Machek
2006-09-20 19:54 ` [PATCH -mm 5/6] mm: Print first block offset for swap areas Rafael J. Wysocki
2006-09-21 21:32   ` Pavel Machek
2006-09-21 22:10     ` Dave Jones
2006-09-21 22:46       ` Rafael J. Wysocki
2006-09-21 23:58         ` Jason Lunz
2006-09-22 10:57           ` Rafael J. Wysocki
2006-09-22 14:13             ` Jason Lunz
2006-09-22 14:18               ` Pavel Machek
2006-09-22 14:35                 ` Jason Lunz
2006-09-22 14:37                   ` Pavel Machek
2006-09-22 14:32               ` Rafael J. Wysocki
2006-09-22 14:33                 ` Pavel Machek
2006-09-20 20:01 ` [PATCH -mm 6/6] swsusp: Document support for swap files Rafael J. Wysocki
2006-09-21 21:33   ` Pavel Machek
2006-09-22  1:01 ` [PATCH -mm 0/6] swsusp: Add " Nigel Cunningham
2006-09-22  5:23   ` Pavel Machek
2006-09-22 11:28     ` Rafael J. Wysocki
2006-09-22 22:18       ` Nigel Cunningham
2006-09-23 22:18         ` Rafael J. Wysocki
2006-09-23 22:49           ` Nigel Cunningham
2006-09-23 22:59             ` Pavel Machek
2006-09-23 23:24               ` Nigel Cunningham

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=200609202131.38206.rjw@sisk.pl \
    --to=rjw@sisk.pl \
    --cc=akpm@osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pavel@ucw.cz \
    /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