swsusp: Add support for swap files
Subsystems (5)
PATCH 1/1 swsusp: Add support for swap files
Hi,
The following series of patches makes swsusp support swap files.
For now, it is only possible to suspend to a swap file using the in-kernel swsusp and the resume cannot be initiated from an initrd.
[Note to Pavel: I have added a couple of paragraphs to the documentation to clarify some things pointed out by Dave, but I hope the ACK still applies. ;-) ]
Greetings,
Rafael
Signature · 2 lines
You never change things by fighting the existing reality. R. Buckminster Fuller
PATCH 1/6 swsusp: Use partition device and offset to identify swap areas
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.
--- 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(-)
mm/swapfile.c +26 -12
@@ -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 ; }
include/linux/swap.h +1 -1
@@ -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 );
kernel/power/swap.c +1 -1
@@ -74,7 +74,7 @@ static int mark_swapfiles(swp_entry_t ststatic 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;
kernel/power/user.c +3 -2
@@ -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 {
PATCH 2/6 swsusp: Rearrange swap-handling code
In reply to Rafael J. WysockiRearrange the code in kernel/power/swap.c so that the next patch is more readable.
[This patch only moves the existing code.]
--- kernel/power/swap.c | 219 ++++++++++++++++++++++++++-------------------------- 1 file changed, 111 insertions(+), 108 deletions(-)
kernel/power/swap.c +111 -108
@@ -41,10 +41,120 @@ static struct swsusp_header { } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;/* -* Saving part... +* General things */ static unsigned short root_swap =0xffff ; +static struct block_device *resume_bdev; + +/** +* submit - submit BIO request. +* @rw: READ or WRITE. +* @off physical offset of page. +* @page: page we're reading or writing. +* @bio_chain: list of pending biod (for async reading) +* +* Straight from the textbook - allocate and initialize the bio. +* If we're reading, make sure the page is marked as dirty. +* Then submit it and, if @bio_chain == NULL, wait. +*/ +static int submit (int rw,pgoff_t page_off,struct page *page, +struct bio **bio_chain) +{ +struct bio *bio; + + bio =bio_alloc (GFP_ATOMIC ,1 ); +if (!bio) +return -ENOMEM ; + bio->bi_sector = page_off * (PAGE_SIZE >>9 ); + bio->bi_bdev = resume_bdev; + bio->bi_end_io = end_swap_bio_read; + +if (bio_add_page (bio, page,PAGE_SIZE ,0 ) <PAGE_SIZE ) { +printk ("swsusp: ERROR: adding page to bio at %ld\n" , page_off); +bio_put (bio); +return -EFAULT ; + } + +lock_page (page); +bio_get (bio); + +if (bio_chain ==NULL ) { +submit_bio (rw | (1 <<BIO_RW_SYNC ), bio); +wait_on_page_locked (page); +if (rw ==READ ) +bio_set_pages_dirty (bio); +bio_put (bio); + }else { +get_page (page); + bio->bi_private = *bio_chain; + *bio_chain = bio; +submit_bio (rw | (1 <<BIO_RW_SYNC ), bio); + } +return 0 ; +} + +static int bio_read_page (pgoff_t page_off,void *addr,struct bio **bio_chain) +{ +return submit (READ , page_off,virt_to_page (addr), bio_chain); +} + +static int bio_write_page (pgoff_t page_off,void *addr) +{ +return submit (WRITE , page_off,virt_to_page (addr),NULL ); +} + +static int wait_on_bio_chain (struct bio **bio_chain) +{ +struct bio *bio; +struct bio *next_bio; +int ret =0 ; + +if (bio_chain ==NULL ) +return 0 ; + + bio = *bio_chain; +if (bio ==NULL ) +return 0 ; +while (bio) { +struct page *page; + + next_bio = bio->bi_private; + page = bio->bi_io_vec[0 ].bv_page; +wait_on_page_locked (page); +if (!PageUptodate (page) ||PageError (page)) + ret = -EIO ; +put_page (page); +bio_put (bio); + bio = next_bio; + } + *bio_chain =NULL ; +return ret; +} + +static void show_speed (struct timeval *start,struct timeval *stop, +unsigned nr_pages,char *msg) +{ +s64 elapsed_centisecs64; +int centisecs; +int k; +int kps; + + elapsed_centisecs64 =timeval_to_ns (stop) -timeval_to_ns (start); +do_div (elapsed_centisecs64,NSEC_PER_SEC /100 ); + centisecs = elapsed_centisecs64; +if (centisecs ==0 ) + centisecs =1 ;/* avoid div-by-zero */ + k = nr_pages * (PAGE_SIZE /1024 ); + kps = (k *100 ) / centisecs; +printk ("%s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n" , msg, k, + centisecs /100 , centisecs %100 , + kps /1000 , (kps %1000 ) /10 ); +} + +/* +* Saving part +*/ static int mark_swapfiles (swp_entry_t start) {
@@ -166,26 +276,6 @@ static void release_swap_writer(struct s handle->bitmap =NULL ; } -static void show_speed (struct timeval *start,struct timeval *stop, -unsigned nr_pages,char *msg) -{ -s64 elapsed_centisecs64; -int centisecs; -int k; -int kps; - - elapsed_centisecs64 =timeval_to_ns (stop) -timeval_to_ns (start); -do_div (elapsed_centisecs64,NSEC_PER_SEC /100 ); - centisecs = elapsed_centisecs64; -if (centisecs ==0 ) - centisecs =1 ;/* avoid div-by-zero */ - k = nr_pages * (PAGE_SIZE /1024 ); - kps = (k *100 ) / centisecs; -printk ("%s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n" , msg, k, - centisecs /100 , centisecs %100 , - kps /1000 , (kps %1000 ) /10 ); -} -static int get_swap_writer (struct swap_map_handle *handle) { handle->cur = (struct swap_map_page *)get_zeroed_page (GFP_KERNEL );
@@ -205,34 +295,6 @@ static int get_swap_writer(struct swap_mreturn 0 ; } -static int wait_on_bio_chain (struct bio **bio_chain) -{ -struct bio *bio; -struct bio *next_bio; -int ret =0 ; - -if (bio_chain ==NULL ) -return 0 ; - - bio = *bio_chain; -if (bio ==NULL ) -return 0 ; -while (bio) { -struct page *page; - - next_bio = bio->bi_private; - page = bio->bi_io_vec[0 ].bv_page; -wait_on_page_locked (page); -if (!PageUptodate (page) ||PageError (page)) - ret = -EIO ; -put_page (page); -bio_put (bio); - bio = next_bio; - } - *bio_chain =NULL ; -return ret; -} -static int swap_write_page (struct swap_map_handle *handle,void *buf,struct bio **bio_chain) {
@@ -384,65 +446,6 @@ int swsusp_write(void)return error; } -static struct block_device *resume_bdev; - -/** -* submit - submit BIO request. -* @rw: READ or WRITE. -* @off physical offset of page. -* @page: page we're reading or writing. -* @bio_chain: list of pending biod (for async reading) -* -* Straight from the textbook - allocate and initialize the bio. -* If we're reading, make sure the page is marked as dirty. -* Then submit it and, if @bio_chain == NULL, wait. -*/ -static int submit (int rw,pgoff_t page_off,struct page *page, -struct bio **bio_chain) -{ -struct bio *bio; - - bio =bio_alloc (GFP_ATOMIC ,1 ); -if (!bio) -return -ENOMEM ; - bio->bi_sector = page_off * (PAGE_SIZE >>9 ); - bio->bi_bdev = resume_bdev; - bio->bi_end_io = end_swap_bio_read; - -if (bio_add_page (bio, page,PAGE_SIZE ,0 ) <PAGE_SIZE ) { -printk ("swsusp: ERROR: adding page to bio at %ld\n" , page_off); -bio_put (bio); -return -EFAULT ; - } - -lock_page (page); -bio_get (bio); - -if (bio_chain ==NULL ) { -submit_bio (rw | (1 <<BIO_RW_SYNC ), bio); -wait_on_page_locked (page); -if (rw ==READ ) -bio_set_pages_dirty (bio); -bio_put (bio); - }else { -get_page (page); - bio->bi_private = *bio_chain; - *bio_chain = bio; -submit_bio (rw | (1 <<BIO_RW_SYNC ), bio); - } -return 0 ; -} - -static int bio_read_page (pgoff_t page_off,void *addr,struct bio **bio_chain) -{ -return submit (READ , page_off,virt_to_page (addr), bio_chain); -} - -static int bio_write_page (pgoff_t page_off,void *addr) -{ -return submit (WRITE , page_off,virt_to_page (addr),NULL ); -} -/** * The following functions allow us to read data using a swap map * in a file-alike way
PATCH 3/6 swsusp: Use block device offsets to identify swap locations
In reply to Rafael J. WysockiMake swsusp use block device offsets instead of swap offsets to identify swap locations and make it use the same code paths for writing as well as for reading data.
This allows us to use the same code for handling swap files and swap partitions and to simplify the code, eg. by dropping rw_swap_page_sync().
--- include/linux/swap.h | 3 - kernel/power/power.h | 2 kernel/power/swap.c | 128 +++++++++++++++++++++++++------------------------- kernel/power/swsusp.c | 10 +-- kernel/power/user.c | 2 mm/page_io.c | 45 ----------------- mm/swapfile.c | 17 ++++++ 7 files changed, 91 insertions(+), 116 deletions(-)
include/linux/swap.h +1 -2
@@ -219,8 +219,6 @@ extern void swap_unplug_io_fn(struct bac/* linux/mm/page_io.c */ extern int swap_readpage (struct file *,struct page *);extern int swap_writepage (struct page *page,struct writeback_control *wbc); -extern int rw_swap_page_sync (int rw,swp_entry_t entry,struct page *page, -struct bio **bio_chain);extern int end_swap_bio_read (struct bio *bio,unsigned int bytes_done,int err);/* linux/mm/swap_state.c */
@@ -252,6 +250,7 @@ extern void free_swap_and_cache(swp_entrextern 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 sector_t swapdev_block (int ,pgoff_t );extern struct swap_info_struct *get_swap_info_struct (unsigned );extern int can_share_swap_page (struct page *);extern int remove_exclusive_swap_page (struct page *);
mm/swapfile.c +17 -0
@@ -945,6 +945,23 @@ sector_t map_swap_page(struct swap_info_ } } +#ifdef CONFIG_SOFTWARE_SUSPEND +/* +* Get the (PAGE_SIZE) block corresponding to given offset on the swapdev +* corresponding to given index in swap_info (swap type). +*/ +sector_t swapdev_block (int swap_type,pgoff_t offset) +{ +struct swap_info_struct *sis; + +if (swap_type >= nr_swapfiles) +return 0 ; + + sis = swap_info + swap_type; +return (sis->flags &SWP_WRITEOK ) ?map_swap_page (sis, offset) :0 ; +} +#endif /* CONFIG_SOFTWARE_SUSPEND */ +/* * Free all of a swapdev's extent information */
kernel/power/power.h +1 -1
@@ -141,7 +141,7 @@ struct bitmap_page {extern void free_bitmap (struct bitmap_page *bitmap);extern struct bitmap_page *alloc_bitmap (unsigned int nr_bits); -extern unsigned long alloc_swap_page (int swap,struct bitmap_page *bitmap); +extern loff_t alloc_swapdev_block (int swap,struct bitmap_page *bitmap);extern void free_all_swap_pages (int swap,struct bitmap_page *bitmap);extern int swsusp_check (void );
kernel/power/swsusp.c +5 -5
@@ -134,18 +134,18 @@ static int bitmap_set(struct bitmap_pagereturn 0 ; } -unsigned long alloc_swap_page (int swap,struct bitmap_page *bitmap) +loff_t alloc_swapdev_block (int swap,struct bitmap_page *bitmap) {unsigned long offset; offset =swp_offset (get_swap_page_of_type (swap));if (offset) { -if (bitmap_set (bitmap, offset)) { +if (bitmap_set (bitmap, offset))swap_free (swp_entry (swap, offset)); - offset =0 ; - } +else +return swapdev_block (swap, offset); } -return offset; +return 0 ; }void free_all_swap_pages (int swap,struct bitmap_page *bitmap)
kernel/power/user.c +1 -1
@@ -239,7 +239,7 @@ static int snapshot_ioctl(struct inode *break ; } } - offset =alloc_swap_page (data->swap, data->bitmap); + offset =alloc_swapdev_block (data->swap, data->bitmap);if (offset) { offset <<=PAGE_SHIFT ; error =put_user (offset, (loff_t __user *)arg);
kernel/power/swap.c +66 -62
265 lines not on this page · raw message
mm/page_io.c +0 -45
48 lines not on this page · raw message
Re: [PATCH -mm 3/6] swsusp: Use block device offsets to identify swap locations
Quoted 10 lines · On Sat, 23 Sep 2006 12:04:25 +0200 "Rafael J. Wysocki" <[email protected]> wrote:
Make swsusp use block device offsets instead of swap offsets to identify swap locations and make it use the same code paths for writing as well as for reading data.
This allows us to use the same code for handling swap files and swap partitions and to simplify the code, eg. by dropping
rw_swap_page_sync()...
+sector_t
swapdev_block(int swap_type, pgoff_t offset)
swapdev_block() returns sector_t.
Quoted 8 lines
kernel/power/swsusp.c +1
-2-unsigned long alloc_swap_page (int swap,struct bitmap_page *bitmap) +loff_t alloc_swapdev_block (int swap,struct bitmap_page *bitmap) {unsigned long offset; offset =swp_offset (get_swap_page_of_type (swap));if (offset) { -if (bitmap_set (bitmap, offset)) {
kernel/power/swsusp.c static int bitmap_set(struct bitmap_page · line 149
Quoted 10 lines
kernel/power/swsusp.c +4
-3+if (bitmap_set (bitmap, offset))swap_free (swp_entry (swap, offset)); - offset =0 ; - } +else +return swapdev_block (swap, offset); } -return offset; +return 0 ; }
But alloc_swapdev_block() returns loff_t.
kernel/power/user.c static int snapshot_ioctl(struct inode * · line 242
Quoted 11 lines
void
free_all_swap_pages(int swap, struct bitmap_page *bitmap)
Index:linux-2.6.18-rc7-mm1/kernel/power/user.c
===================================================================kernel/power/user.c +1
-1@@ -239,7 +239,7 @@ static int snapshot_ioctl(struct inode *break ; } } - offset =alloc_swap_page (data->swap, data->bitmap); + offset =alloc_swapdev_block (data->swap, data->bitmap);
`offset' is declared loff_t, yet it is holding a sector_t.
kernel/power/swap.c extern char resume_file[]; · line 38
Quoted 11 lines
===================================================================
kernel/power/swap.c +2
-2@@ -34,8 +34,8 @@ extern char resume_file[];#define SWSUSP_SIG "S1SUSPEND" static struct swsusp_header { - char reserved[PAGE_SIZE - 20 - sizeof(swp_entry_t)]; - swp_entry_t image; + char reserved[PAGE_SIZE - 20 - sizeof(loff_t)]; + loff_t image;
More possible sector_t/loff_t confusion.
Quoted 12 lines
kernel/power/swap.c +3
-6static int swsusp_swap_check (void )/* This is called before saving image */ { -int res =swap_type_of (swsusp_resume_device,0 ); +int res; -if (res >=0 ) { - root_swap = res; -return 0 ; - } -return res; + res =swap_type_of (swsusp_resume_device,0 ); +if (res <0 )
kernel/power/swap.c static int mark_swapfiles(swp_entry_t st · line 197
Quoted 10 lines
kernel/power/swap.c +9
-0+return res; + + root_swap = res; + resume_bdev =open_by_devnum (swsusp_resume_device,FMODE_WRITE ); +if (IS_ERR (resume_bdev)) +return PTR_ERR (resume_bdev); + +set_blocksize (resume_bdev,PAGE_SIZE ); +return 0 ; }
set_blocksize() can fail.
kernel/power/swap.c static int write_page(void *buf, unsigne · line 243
Quoted 2 lines
kernel/power/swap.c +1
-1-#define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(long) - 1) +#define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(loff_t) - 1)
I think this is dealing with sector_t's?
Re: [PATCH -mm 3/6] swsusp: Use block device offsets to identify swap locations
Quoted 21 lines · Andrew Morton
Quoted 10 lines · On Sat, 23 Sep 2006 12:04:25 +0200 "Rafael J. Wysocki" <[email protected]> wrote:
Make swsusp use block device offsets instead of swap offsets to identify swap locations and make it use the same code paths for writing as well as for reading data.
This allows us to use the same code for handling swap files and swap partitions and to simplify the code, eg. by dropping
rw_swap_page_sync()...
+sector_t
swapdev_block(int swap_type, pgoff_t offset)
swapdev_block()returnssector_t.Quoted 5 lines
kernel/power/swsusp.c +1
-1-unsigned long alloc_swap_page (int swap,struct bitmap_page *bitmap) +loff_t alloc_swapdev_block (int swap,struct bitmap_page *bitmap) {unsigned long offset;
Quoted 15 lines
kernel/power/swsusp.c static int bitmap_set(struct bitmap_page · line 149
Quoted 13 lines
kernel/power/swsusp.c +4
-4offset =swp_offset (get_swap_page_of_type (swap));if (offset) { -if (bitmap_set (bitmap, offset)) { +if (bitmap_set (bitmap, offset))swap_free (swp_entry (swap, offset)); - offset =0 ; - } +else +return swapdev_block (swap, offset); } -return offset; +return 0 ; }But
alloc_swapdev_block()returnsloff_t.
I'll change all that to sector_t.
Quoted 44 lines
kernel/power/user.c static int snapshot_ioctl(struct inode * · line 242
Quoted 11 lines
void
free_all_swap_pages(int swap, struct bitmap_page *bitmap)
Index:linux-2.6.18-rc7-mm1/kernel/power/user.c
===================================================================kernel/power/user.c +1
-1@@ -239,7 +239,7 @@ static int snapshot_ioctl(struct inode *break ; } } - offset =alloc_swap_page (data->swap, data->bitmap); + offset =alloc_swapdev_block (data->swap, data->bitmap);`offset' is declared
loff_t, yet it is holding asector_t.kernel/power/swap.c extern char resume_file[]; · line 38
Quoted 11 lines
===================================================================
kernel/power/swap.c +2
-2@@ -34,8 +34,8 @@ extern char resume_file[];#define SWSUSP_SIG "S1SUSPEND" static struct swsusp_header { - char reserved[PAGE_SIZE - 20 - sizeof(swp_entry_t)]; - swp_entry_t image; + char reserved[PAGE_SIZE - 20 - sizeof(loff_t)]; + loff_t image;More possible sector_t/loff_t confusion.
Quoted 16 lines
kernel/power/swap.c +7
-6static int swsusp_swap_check (void )/* This is called before saving image */ { -int res =swap_type_of (swsusp_resume_device,0 ); +int res; -if (res >=0 ) { - root_swap = res; -return 0 ; - } -return res; + res =swap_type_of (swsusp_resume_device,0 ); +if (res <0 ) +return res; + + root_swap = res; + resume_bdev =open_by_devnum (swsusp_resume_device,FMODE_WRITE );
Quoted 8 lines
kernel/power/swap.c static int mark_swapfiles(swp_entry_t st · line 197
Quoted 6 lines
kernel/power/swap.c +5
-0+if (IS_ERR (resume_bdev)) +return PTR_ERR (resume_bdev); + +set_blocksize (resume_bdev,PAGE_SIZE ); +return 0 ; }
set_blocksize()can fail.
Will check for that.
Quoted 5 lines
kernel/power/swap.c static int write_page(void *buf, unsigne · line 243
Quoted 3 lines
kernel/power/swap.c +1
-1-#define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(long) - 1) +#define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(loff_t) - 1)I think this is dealing with sector_t's?
Rafael J. Wysocki
Acked-by Pavel MachekRe: [PATCH -mm 3/6] swsusp: Use block device offsets to identify swap locations
Quoted 93 lines · Rafael J. Wysocki
Quoted 21 lines · Andrew Morton
Quoted 10 lines · On Sat, 23 Sep 2006 12:04:25 +0200 "Rafael J. Wysocki" <[email protected]> wrote:
Make swsusp use block device offsets instead of swap offsets to identify swap locations and make it use the same code paths for writing as well as for reading data.
This allows us to use the same code for handling swap files and swap partitions and to simplify the code, eg. by dropping
rw_swap_page_sync()...
+sector_t
swapdev_block(int swap_type, pgoff_t offset)
swapdev_block()returnssector_t.Quoted 5 lines
(quoted diff) +1
-1-unsigned long alloc_swap_page(int swap, struct bitmap_page *bitmap) +loff_t alloc_swapdev_block(int swap, struct bitmap_page *bitmap) { unsigned long offset;Quoted 15 lines
Review
Quoted 13 lines
(quoted diff) +4
-4offset = swp_offset(get_swap_page_of_type(swap)); if (offset) { - if (bitmap_set(bitmap, offset)) { + if (bitmap_set(bitmap, offset)) swap_free(swp_entry(swap, offset)); - offset = 0; - } + else + return swapdev_block(swap, offset); } - return offset; + return 0; }But
alloc_swapdev_block()returnsloff_t.I'll change all that to
sector_t.Quoted 44 lines
kernel/power/user.c static int snapshot_ioctl(struct inode * · line 242
Quoted 11 lines
void
free_all_swap_pages(int swap, struct bitmap_page *bitmap)
Index:linux-2.6.18-rc7-mm1/kernel/power/user.c
===================================================================kernel/power/user.c +1
-1@@ -239,7 +239,7 @@ static int snapshot_ioctl(struct inode *break ; } } - offset =alloc_swap_page (data->swap, data->bitmap); + offset =alloc_swapdev_block (data->swap, data->bitmap);`offset' is declared
loff_t, yet it is holding asector_t.kernel/power/swap.c extern char resume_file[]; · line 38
Quoted 11 lines
===================================================================
kernel/power/swap.c +2
-2@@ -34,8 +34,8 @@ extern char resume_file[];#define SWSUSP_SIG "S1SUSPEND" static struct swsusp_header { - char reserved[PAGE_SIZE - 20 - sizeof(swp_entry_t)]; - swp_entry_t image; + char reserved[PAGE_SIZE - 20 - sizeof(loff_t)]; + loff_t image;More possible sector_t/loff_t confusion.
Quoted 16 lines
kernel/power/swap.c +7
-6static int swsusp_swap_check (void )/* This is called before saving image */ { -int res =swap_type_of (swsusp_resume_device,0 ); +int res; -if (res >=0 ) { - root_swap = res; -return 0 ; - } -return res; + res =swap_type_of (swsusp_resume_device,0 ); +if (res <0 ) +return res; + + root_swap = res; + resume_bdev =open_by_devnum (swsusp_resume_device,FMODE_WRITE );Quoted 8 lines
kernel/power/swap.c
Quoted 6 lines
kernel/power/swap.c +5
-0+if (IS_ERR (resume_bdev)) +return PTR_ERR (resume_bdev); + +set_blocksize (resume_bdev,PAGE_SIZE ); +return 0 ; }
set_blocksize()can fail.
Quoted 1 line
Will check for that.
Updated patch follows.
---
Make swsusp use block device offsets instead of swap offsets to identify swap locations and make it use the same code paths for writing as well as for reading data.
This allows us to use the same code for handling swap files and swap partitions and to simplify the code, eg. by dropping rw_swap_page_sync().
--- include/linux/swap.h | 3 - kernel/power/power.h | 2 kernel/power/swap.c | 129 ++++++++++++++++++++++++++------------------------ kernel/power/swsusp.c | 10 +-- kernel/power/user.c | 7 +- mm/page_io.c | 45 ----------------- mm/swapfile.c | 17 ++++++ 7 files changed, 96 insertions(+), 117 deletions(-)
include/linux/swap.h +1 -2
@@ -219,8 +219,6 @@ extern void swap_unplug_io_fn(struct bac/* linux/mm/page_io.c */ extern int swap_readpage (struct file *,struct page *);extern int swap_writepage (struct page *page,struct writeback_control *wbc); -extern int rw_swap_page_sync (int rw,swp_entry_t entry,struct page *page, -struct bio **bio_chain);extern int end_swap_bio_read (struct bio *bio,unsigned int bytes_done,int err);/* linux/mm/swap_state.c */
@@ -252,6 +250,7 @@ extern void free_swap_and_cache(swp_entrextern 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 sector_t swapdev_block (int ,pgoff_t );extern struct swap_info_struct *get_swap_info_struct (unsigned );extern int can_share_swap_page (struct page *);extern int remove_exclusive_swap_page (struct page *);
mm/swapfile.c +17 -0
@@ -945,6 +945,23 @@ sector_t map_swap_page(struct swap_info_ } } +#ifdef CONFIG_SOFTWARE_SUSPEND +/* +* Get the (PAGE_SIZE) block corresponding to given offset on the swapdev +* corresponding to given index in swap_info (swap type). +*/ +sector_t swapdev_block (int swap_type,pgoff_t offset) +{ +struct swap_info_struct *sis; + +if (swap_type >= nr_swapfiles) +return 0 ; + + sis = swap_info + swap_type; +return (sis->flags &SWP_WRITEOK ) ?map_swap_page (sis, offset) :0 ; +} +#endif /* CONFIG_SOFTWARE_SUSPEND */ +/* * Free all of a swapdev's extent information */
kernel/power/power.h +1 -1
@@ -141,7 +141,7 @@ struct bitmap_page {extern void free_bitmap (struct bitmap_page *bitmap);extern struct bitmap_page *alloc_bitmap (unsigned int nr_bits); -extern unsigned long alloc_swap_page (int swap,struct bitmap_page *bitmap); +extern sector_t alloc_swapdev_block (int swap,struct bitmap_page *bitmap);extern void free_all_swap_pages (int swap,struct bitmap_page *bitmap);extern int swsusp_check (void );
kernel/power/swsusp.c +5 -5
@@ -134,18 +134,18 @@ static int bitmap_set(struct bitmap_pagereturn 0 ; } -unsigned long alloc_swap_page (int swap,struct bitmap_page *bitmap) +sector_t alloc_swapdev_block (int swap,struct bitmap_page *bitmap) {unsigned long offset; offset =swp_offset (get_swap_page_of_type (swap));if (offset) { -if (bitmap_set (bitmap, offset)) { +if (bitmap_set (bitmap, offset))swap_free (swp_entry (swap, offset)); - offset =0 ; - } +else +return swapdev_block (swap, offset); } -return offset; +return 0 ; }void free_all_swap_pages (int swap,struct bitmap_page *bitmap)
kernel/power/user.c +4 -3
@@ -125,7 +125,8 @@ static int snapshot_ioctl(struct inode * {int error =0 ;struct snapshot_data *data; -loff_t offset, avail; +loff_t avail; +sector_t offset;if (_IOC_TYPE (cmd) !=SNAPSHOT_IOC_MAGIC )return -ENOTTY ;
@@ -239,10 +240,10 @@ static int snapshot_ioctl(struct inode *break ; } } - offset =alloc_swap_page (data->swap, data->bitmap); + offset =alloc_swapdev_block (data->swap, data->bitmap);if (offset) { offset <<=PAGE_SHIFT ; - error =put_user (offset, (loff_t __user *)arg); + error =put_user (offset, (sector_t __user *)arg); }else { error = -ENOSPC ; }
kernel/power/swap.c +68 -61
266 lines not on this page · raw message
mm/page_io.c +0 -45
48 lines not on this page · raw message
---
PATCH 4/6 swsusp: Add resume_offset command line parameter
In reply to Rafael J. WysockiAdd the kernel command line parameter "resume_offset=" allowing us to specify the offset, in <PAGE_SIZE> units, from the beginning of the partition pointed to by the "resume=" parameter at which the swap header is located.
This offset can be determined, for example, by an application using the FIBMAP ioctl to obtain the swap header's block number for given file.
--- kernel/power/disk.c | 15 +++++++++++++++ kernel/power/power.h | 1 + kernel/power/swap.c | 15 ++++++++++----- 3 files changed, 26 insertions(+), 5 deletions(-)
kernel/power/disk.c +15 -0
@@ -27,6 +27,7 @@static int noresume =0 ;char resume_file[256 ] =CONFIG_PM_STD_PARTITION ;dev_t swsusp_resume_device; +sector_t swsusp_resume_block;/** * power_down - Shut machine down for hibernate.
@@ -404,6 +405,19 @@ static int __init resume_setup(char *strreturn 1 ; } +static int __init resume_offset_setup (char *str) +{ +loff_t offset; + +if (noresume) +return 1 ; + +if (sscanf (str,"%llu" , &offset) ==1 ) + swsusp_resume_block = offset; + +return 1 ; +} +static int __init noresume_setup (char *str) { noresume =1 ;
@@ -411,4 +425,5 @@ static int __init noresume_setup(char *s }__setup ("noresume" , noresume_setup); +__setup ("resume_offset=" , resume_offset_setup);__setup ("resume=" , resume_setup);
kernel/power/power.h +1 -0
@@ -42,6 +42,7 @@ extern const void __nosave_begin, __nosaextern unsigned long image_size;extern int in_suspend;extern dev_t swsusp_resume_device; +extern sector_t swsusp_resume_block; extern asmlinkage int swsusp_arch_suspend(void); extern asmlinkage int swsusp_arch_resume(void);
kernel/power/swap.c +10 -5
@@ -160,13 +160,14 @@ static int mark_swapfiles(loff_t start) {int error; -bio_read_page (0 , &swsusp_header,NULL ); +bio_read_page (swsusp_resume_block, &swsusp_header,NULL );if (!memcmp ("SWAP-SPACE" ,swsusp_header.sig,10 ) || !memcmp ("SWAPSPACE2" ,swsusp_header.sig,10 )) {memcpy (swsusp_header.orig_sig,swsusp_header.sig,10 );memcpy (swsusp_header.sig,SWSUSP_SIG ,10 ); swsusp_header.image = start; - error =bio_write_page (0 , &swsusp_header,NULL ); + error =bio_write_page (swsusp_resume_block, + &swsusp_header,NULL ); }else {printk (KERN_ERR "swsusp: Swap header not found!\n" ); error = -ENODEV ;
@@ -183,7 +184,7 @@ static int swsusp_swap_check(void) /* Th {int res; - res =swap_type_of (swsusp_resume_device,0 ); + res =swap_type_of (swsusp_resume_device, swsusp_resume_block);if (res <0 )return res;
@@ -606,12 +607,16 @@ int swsusp_check(void)if (!IS_ERR (resume_bdev)) {set_blocksize (resume_bdev,PAGE_SIZE );memset (&swsusp_header,0 ,sizeof (swsusp_header)); -if ((error =bio_read_page (0 , &swsusp_header,NULL ))) + error =bio_read_page (swsusp_resume_block, + &swsusp_header,NULL ); +if (error)return error; +if (!memcmp (SWSUSP_SIG , swsusp_header.sig,10 )) {memcpy (swsusp_header.sig, swsusp_header.orig_sig,10 );/* Reset swap signature now */ - error =bio_write_page (0 , &swsusp_header,NULL ); + error =bio_write_page (swsusp_resume_block, + &swsusp_header,NULL ); }else {return -EINVAL ; }
Re: [PATCH -mm 4/6] swsusp: Add resume_offset command line parameter
Quoted 3 lines · On Sat, 23 Sep 2006 12:08:25 +0200 "Rafael J. Wysocki" <[email protected]> wrote:
Add the kernel command line parameter "resume_offset=" allowing us to specify the offset, in <
PAGE_SIZE> units, from the beginning of the partition pointed to by the "resume=" parameter at which the swap header is located.
Is this description correct? I think it's in 512-byte units?
It certainly should be - a filesytem could start the swapfile at any sector_t.
Re: [PATCH -mm 4/6] swsusp: Add resume_offset command line parameter
Quoted 8 lines · Andrew Morton
Quoted 3 lines · On Sat, 23 Sep 2006 12:08:25 +0200 "Rafael J. Wysocki" <[email protected]> wrote:
Add the kernel command line parameter "resume_offset=" allowing us to specify the offset, in <
PAGE_SIZE> units, from the beginning of the partition pointed to by the "resume=" parameter at which the swap header is located.Is this description correct?
Yes, it is. Please have a look at setup_swap_extents(). :-)
Quoted 1 line
I think it's in 512-byte units?
Well, no.
Quoted 2 lines
It certainly should be - a filesytem could start the swapfile at any
sector_t.
If I understand the code correctly, it looks for the first page-aligned continuous region that contains as many blocks as to hold at least one page.
PATCH 5/6 mm: Print first block offset for swap areas
In reply to Rafael J. WysockiIn order to use a swap file with swsusp we need to know the offset at which its swap header is located. However, the swap header is always located in the first page block of the swap file and it's quite easy to make sys_swapon() print the offset of the swap file's (or swap partition's) first page block.
---
--- mm/swapfile.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
mm/swapfile.c +10 -5
@@ -1047,7 +1047,8 @@ add_swap_extent(struct swap_info_struct* This is extremely effective. The average number of iterations in * map_swap_page() has been measured at about 0.3 per page. - akpm. */ -static int setup_swap_extents (struct swap_info_struct *sis,sector_t *span) +static int setup_swap_extents (struct swap_info_struct *sis,sector_t *span, +sector_t *start) {struct inode *inode;unsigned blocks_per_page;
@@ -1060,6 +1061,7 @@ static int setup_swap_extents(struct swaint nr_extents =0 ;int ret; + *start =0 ; inode = sis->swap_file->f_mapping->host;if (S_ISBLK (inode->i_mode)) { ret =add_swap_extent (sis,0 , sis->max,0 );
@@ -1114,6 +1116,8 @@ static int setup_swap_extents(struct swa lowest_block = first_block;if (first_block > highest_block) highest_block = first_block; + }else { + *start = first_block; }/*
@@ -1407,7 +1411,7 @@ asmlinkage long sys_swapon(const char __int swap_header_version;unsigned int nr_good_pages =0 ;int nr_extents =0 ; -sector_t span; +sector_t span, start;unsigned long maxpages =1 ;int swapfilesize;unsigned short *swap_map;
@@ -1608,7 +1612,7 @@ asmlinkage long sys_swapon(const char __ p->swap_map[0 ] =SWAP_MAP_BAD ; p->max = maxpages; p->pages = nr_good_pages; - nr_extents =setup_swap_extents (p, &span); + nr_extents =setup_swap_extents (p, &span, &start);if (nr_extents <0 ) { error = nr_extents; goto bad_swap;
@@ -1628,9 +1632,10 @@ asmlinkage long sys_swapon(const char __ total_swap_pages += nr_good_pages;printk (KERN_INFO "Adding %uk swap on %s. " -"Priority:%d extents:%d across:%lluk\n" , +"Priority:%d extents:%d across:%lluk offset:%llu\n" , nr_good_pages<<(PAGE_SHIFT -10 ), name, p->prio, - nr_extents, (unsigned long long )span<<(PAGE_SHIFT -10 )); + nr_extents, (unsigned long long )span<<(PAGE_SHIFT -10 ), + (unsigned long long )start);/* insert swap space into swap_list: */ prev =-1 ;
Re: [PATCH -mm 5/6] mm: Print first block offset for swap areas
Quoted 4 lines · On Sat, 23 Sep 2006 12:10:54 +0200 "Rafael J. Wysocki" <[email protected]> wrote:
In order to use a swap file with swsusp we need to know the offset at which its swap header is located. However, the swap header is always located in the first page block of the swap file and it's quite easy to make
sys_swapon()print the offset of the swap file's (or swap partition's) first page block.
Why is this needed? The swapfile's pathname is present in /proc/swaps, so an application can read that, do the FIBMAP and rewrite grub.conf without needing to parse dmesg?
Re: [PATCH -mm 5/6] mm: Print first block offset for swap areas
Quoted 11 lines · Andrew Morton
Quoted 4 lines · On Sat, 23 Sep 2006 12:10:54 +0200 "Rafael J. Wysocki" <[email protected]> wrote:
In order to use a swap file with swsusp we need to know the offset at which its swap header is located. However, the swap header is always located in the first page block of the swap file and it's quite easy to make
sys_swapon()print the offset of the swap file's (or swap partition's) first page block.Why is this needed? The swapfile's pathname is present in
/proc/swaps, so an application can read that, do the FIBMAP and rewrite grub.conf without needing to parse dmesg?
Well, this is not needed, but it's useful if you have no such application and want to set up things manually. ;-)
Re: [PATCH -mm 5/6] mm: Print first block offset for swap areas
Quoted 9 lines · On Tue, 26 Sep 2006 22:13:25 +0200 "Rafael J. Wysocki" <[email protected]> wrote:
Quoted 8 lines · Andrew Morton
Quoted 4 lines · On Sat, 23 Sep 2006 12:10:54 +0200 "Rafael J. Wysocki" <[email protected]> wrote:
In order to use a swap file with swsusp we need to know the offset at which its swap header is located. However, the swap header is always located in the first page block of the swap file and it's quite easy to make
sys_swapon()print the offset of the swap file's (or swap partition's) first page block.
Quoted 6 lines
Quoted 3 lines
Why is this needed? The swapfile's pathname is present in
/proc/swaps, so an application can read that, do the FIBMAP and rewrite grub.conf without needing to parse dmesg?Well, this is not needed, but it's useful if you have no such application and want to set up things manually. ;-)
ick. We need such an application (probably doable in a python script?). Methinks that telling people to go poking in dmesg will simply delay the development and propagation of that application..
Re: [PATCH -mm 5/6] mm: Print first block offset for swap areas
Quoted 16 lines · Andrew Morton
Quoted 13 lines · On Tue, 26 Sep 2006 22:13:25 +0200 "Rafael J. Wysocki" <[email protected]> wrote:
Quoted 11 lines · Andrew Morton
Quoted 4 lines · On Sat, 23 Sep 2006 12:10:54 +0200 "Rafael J. Wysocki" <[email protected]> wrote:
In order to use a swap file with swsusp we need to know the offset at which its swap header is located. However, the swap header is always located in the first page block of the swap file and it's quite easy to make
sys_swapon()print the offset of the swap file's (or swap partition's) first page block.Why is this needed? The swapfile's pathname is present in
/proc/swaps, so an application can read that, do the FIBMAP and rewrite grub.conf without needing to parse dmesg?
Quoted 6 lines
Quoted 2 lines
Well, this is not needed, but it's useful if you have no such application and want to set up things manually. ;-)
ick. We need such an application (probably doable in a python script?). Methinks that telling people to go poking in dmesg will simply delay the development and propagation of that application..
Okay, let's drop the patch, then.
I'll redo the documentation ...
PATCH 6/6 swsusp: Document support for swap files
In reply to Rafael J. WysockiDocument the "resume_offset=" command line parameter as well as the way in which swap files are supported by swsusp.
--- Documentation/kernel-parameters.txt | 6 ++ Documentation/power/swsusp-and-swap-files.txt | 60 ++++++++++++++++++++++++++ Documentation/power/swsusp.txt | 18 ++----- 3 files changed, 71 insertions(+), 13 deletions(-)
Documentation/kernel-parameters.txt +6 -0
@@ -1370,6 +1370,12 @@ and is between 256 and 4096 characters. resume= [SWSUSP] Specify the partition device for software suspend + resume_offset= [SWSUSP] + Specify the offset from the beginning of the partition + given by "resume=" at which the swap header is located, + in <PAGE_SIZE> units (needed only for swap files). + See Documentation/power/swsusp-and-swap-files.txt + rhash_entries= [KNL,NET] Set number of hash buckets for route cache
Documentation/power/swsusp-and-swap-files.txt new file +60 -0
60 lines not on this page · raw message
Documentation/power/swsusp.txt +5 -13
@@ -297,20 +297,12 @@ system is shut down or suspended. Additi suspend image to prevent sensitive data from being stolen after resume. -Q: Why can't we suspend to a swap file? +Q: Can I suspend to a swap file? -A: Because accessing swap file needs the filesystem mounted, and -filesystem might do something wrong (like replaying the journal) -during mount. - -There are few ways to get that fixed: - -1) Probably could be solved by modifying every filesystem to support -some kind of "really read-only!" option. Patches welcome. - -2) suspend2 gets around that by storing absolute positions in on-disk -image (and blocksize), with resume parameter pointing directly to -suspend header. +A: Generally, yes, you can. However, it requires you to use the "resume=" and +"resume_offset=" kernel command line parameters, so the resume from a swap file +cannot be initiated from an initrd or initramfs image. See +swsusp-and-swap-files.txt for details. Q: Is there a maximum system RAM size that is supported by swsusp?
Re: [PATCH -mm 6/6] swsusp: Document support for swap files
Quoted 6 lines · Rafael J. Wysocki
Document the "resume_offset=" command line parameter as well as the way in which swap files are supported by swsusp.
Signed-off-by Rafael J. Wysocki <[email protected]>Acked-by Pavel Machek <[email protected]>---
If we drop the 5/6 patch, the documentation has to be updated, as follows:
---
Document the "resume_offset=" command line parameter as well as the way in which swap files are supported by swsusp.
--- Documentation/kernel-parameters.txt | 6 ++ Documentation/power/swsusp-and-swap-files.txt | 54 ++++++++++++++++++++++++++ Documentation/power/swsusp.txt | 20 ++------- 3 files changed, 66 insertions(+), 14 deletions(-)
Documentation/kernel-parameters.txt +6 -0
@@ -1368,6 +1368,12 @@ and is between 256 and 4096 characters. resume= [SWSUSP] Specify the partition device for software suspend + resume_offset= [SWSUSP] + Specify the offset from the beginning of the partition + given by "resume=" at which the swap header is located, + in <PAGE_SIZE> units (needed only for swap files). + See Documentation/power/swsusp-and-swap-files.txt + rhash_entries= [KNL,NET] Set number of hash buckets for route cache
Documentation/power/swsusp-and-swap-files.txt new file +54 -0
54 lines not on this page · raw message
Documentation/power/swsusp.txt +5 -13
@@ -297,20 +297,12 @@ system is shut down or suspended. Additi suspend image to prevent sensitive data from being stolen after resume. -Q: Why can't we suspend to a swap file? +Q: Can I suspend to a swap file? -A: Because accessing swap file needs the filesystem mounted, and -filesystem might do something wrong (like replaying the journal) -during mount. - -There are few ways to get that fixed: - -1) Probably could be solved by modifying every filesystem to support -some kind of "really read-only!" option. Patches welcome. - -2) suspend2 gets around that by storing absolute positions in on-disk -image (and blocksize), with resume parameter pointing directly to -suspend header. +A: Generally, yes, you can. However, it requires you to use the "resume=" and +"resume_offset=" kernel command line parameters, so the resume from a swap file +cannot be initiated from an initrd or initramfs image. See +swsusp-and-swap-files.txt for details. Q: Is there a maximum system RAM size that is supported by swsusp?
Hi!
Quoted 7 lines
The following series of patches makes swsusp support swap files.
For now, it is only possible to suspend to a swap file using the in-kernel swsusp and the resume cannot be initiated from an initrd.
[Note to Pavel: I have added a couple of paragraphs to the documentation to clarify some things pointed out by Dave, but I hope the ACK still applies. ;-) ]
Yes, thanks.
Out of my curiosity... can swap on file work on filesystems with 1K blocksize? ext2 can do that, iirc...
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Quoted 7 lines · Pavel Machek
Hi!
Quoted 5 lines
The following series of patches makes swsusp support swap files.
For now, it is only possible to suspend to a swap file using the in-kernel swsusp and the resume cannot be initiated from an initrd.
Quoted 7 lines
Quoted 2 lines
[Note to Pavel: I have added a couple of paragraphs to the documentation to clarify some things pointed out by Dave, but I hope the ACK still applies. ;-) ]
Yes, thanks.
Out of my curiosity... can swap on file work on filesystems with 1K blocksize? ext2 can do that, iirc...
I thinks so. Having looked at the code, I don't see any particular reason why not.
Greetings,
Rafael
Signature · 2 lines
You never change things by fighting the existing reality. R. Buckminster Fuller