swsusp: Add support for swap files (rev. 2)
Subsystems (5)
cover swsusp: Add support for swap files (rev. 2)
Hi,
The following is the second version of the series of patches that adds support for swap files to swsusp.
Greetings,
Rafael
Signature · 2 lines
You never change things by fighting the existing reality. R. Buckminster Fuller
PATCH 1/5 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/5 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/5 swsusp: Use block device offsets to identify swap locations (rev. 2)
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 | 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/5 swsusp: Add resume_offset command line parameter (rev. 2)
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) +{ +sector_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(sector_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;
@@ -609,12 +610,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 ; }
PATCH 5/5 swsusp: Document support for swap files (rev. 2)
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 | 54 ++++++++++++++++++++++++++ Documentation/power/swsusp.txt | 18 ++------ 3 files changed, 65 insertions(+), 13 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?