[PATCH] swsusp: use block device offsets to identify swap locations

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().

Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
This commit is contained in:
Rafael J. Wysocki 2006-12-06 20:34:10 -08:00 committed by Linus Torvalds
parent 3fc6b34f48
commit 3aef83e0ef
7 changed files with 96 additions and 117 deletions

View File

@ -218,8 +218,6 @@ extern void swap_unplug_io_fn(struct backing_dev_info *, struct page *);
/* linux/mm/page_io.c */ /* linux/mm/page_io.c */
extern int swap_readpage(struct file *, struct page *); extern int swap_readpage(struct file *, struct page *);
extern int swap_writepage(struct page *page, struct writeback_control *wbc); 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); extern int end_swap_bio_read(struct bio *bio, unsigned int bytes_done, int err);
/* linux/mm/swap_state.c */ /* linux/mm/swap_state.c */
@ -250,6 +248,7 @@ extern void free_swap_and_cache(swp_entry_t);
extern int swap_type_of(dev_t, sector_t); extern int swap_type_of(dev_t, sector_t);
extern unsigned int count_swap_pages(int, int); extern unsigned int count_swap_pages(int, int);
extern sector_t map_swap_page(struct swap_info_struct *, pgoff_t); 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 struct swap_info_struct *get_swap_info_struct(unsigned);
extern int can_share_swap_page(struct page *); extern int can_share_swap_page(struct page *);
extern int remove_exclusive_swap_page(struct page *); extern int remove_exclusive_swap_page(struct page *);

View File

@ -146,7 +146,7 @@ struct bitmap_page {
extern void free_bitmap(struct bitmap_page *bitmap); extern void free_bitmap(struct bitmap_page *bitmap);
extern struct bitmap_page *alloc_bitmap(unsigned int nr_bits); 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 void free_all_swap_pages(int swap, struct bitmap_page *bitmap);
extern int swsusp_check(void); extern int swsusp_check(void);

View File

@ -34,8 +34,8 @@ extern char resume_file[];
#define SWSUSP_SIG "S1SUSPEND" #define SWSUSP_SIG "S1SUSPEND"
static struct swsusp_header { static struct swsusp_header {
char reserved[PAGE_SIZE - 20 - sizeof(swp_entry_t)]; char reserved[PAGE_SIZE - 20 - sizeof(sector_t)];
swp_entry_t image; sector_t image;
char orig_sig[10]; char orig_sig[10];
char sig[10]; char sig[10];
} __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header; } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
@ -100,9 +100,9 @@ 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); return submit(READ, page_off, virt_to_page(addr), bio_chain);
} }
static int bio_write_page(pgoff_t page_off, void *addr) static int bio_write_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
{ {
return submit(WRITE, page_off, virt_to_page(addr), NULL); return submit(WRITE, page_off, virt_to_page(addr), bio_chain);
} }
static int wait_on_bio_chain(struct bio **bio_chain) static int wait_on_bio_chain(struct bio **bio_chain)
@ -157,22 +157,19 @@ static void show_speed(struct timeval *start, struct timeval *stop,
* Saving part * Saving part
*/ */
static int mark_swapfiles(swp_entry_t start) static int mark_swapfiles(sector_t start)
{ {
int error; int error;
rw_swap_page_sync(READ, swp_entry(root_swap, 0), bio_read_page(0, &swsusp_header, NULL);
virt_to_page((unsigned long)&swsusp_header), NULL);
if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) || if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
!memcmp("SWAPSPACE2",swsusp_header.sig, 10)) { !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10); memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
memcpy(swsusp_header.sig,SWSUSP_SIG, 10); memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
swsusp_header.image = start; swsusp_header.image = start;
error = rw_swap_page_sync(WRITE, swp_entry(root_swap, 0), error = bio_write_page(0, &swsusp_header, NULL);
virt_to_page((unsigned long)&swsusp_header),
NULL);
} else { } else {
pr_debug("swsusp: Partition is not swap space.\n"); printk(KERN_ERR "swsusp: Swap header not found!\n");
error = -ENODEV; error = -ENODEV;
} }
return error; return error;
@ -185,12 +182,21 @@ static int mark_swapfiles(swp_entry_t start)
static int swsusp_swap_check(void) /* This is called before saving image */ static int swsusp_swap_check(void) /* This is called before saving image */
{ {
int res = swap_type_of(swsusp_resume_device, 0); int 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);
if (IS_ERR(resume_bdev))
return PTR_ERR(resume_bdev);
res = set_blocksize(resume_bdev, PAGE_SIZE);
if (res < 0)
blkdev_put(resume_bdev);
if (res >= 0) {
root_swap = res;
return 0;
}
return res; return res;
} }
@ -201,36 +207,26 @@ static int swsusp_swap_check(void) /* This is called before saving image */
* @bio_chain: Link the next write BIO here * @bio_chain: Link the next write BIO here
*/ */
static int write_page(void *buf, unsigned long offset, struct bio **bio_chain) static int write_page(void *buf, sector_t offset, struct bio **bio_chain)
{ {
swp_entry_t entry; void *src;
int error = -ENOSPC;
if (offset) { if (!offset)
struct page *page = virt_to_page(buf); return -ENOSPC;
if (bio_chain) { if (bio_chain) {
/* src = (void *)__get_free_page(GFP_ATOMIC);
* Whether or not we successfully allocated a copy page, if (src) {
* we take a ref on the page here. It gets undone in memcpy(src, buf, PAGE_SIZE);
* wait_on_bio_chain(). } else {
*/ WARN_ON_ONCE(1);
struct page *page_copy; bio_chain = NULL; /* Go synchronous */
page_copy = alloc_page(GFP_ATOMIC); src = buf;
if (page_copy == NULL) {
WARN_ON_ONCE(1);
bio_chain = NULL; /* Go synchronous */
get_page(page);
} else {
memcpy(page_address(page_copy),
page_address(page), PAGE_SIZE);
page = page_copy;
}
} }
entry = swp_entry(root_swap, offset); } else {
error = rw_swap_page_sync(WRITE, entry, page, bio_chain); src = buf;
} }
return error; return bio_write_page(offset, src, bio_chain);
} }
/* /*
@ -248,11 +244,11 @@ static int write_page(void *buf, unsigned long offset, struct bio **bio_chain)
* at a time. * at a time.
*/ */
#define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(long) - 1) #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
struct swap_map_page { struct swap_map_page {
unsigned long entries[MAP_PAGE_ENTRIES]; sector_t entries[MAP_PAGE_ENTRIES];
unsigned long next_swap; sector_t next_swap;
}; };
/** /**
@ -262,7 +258,7 @@ struct swap_map_page {
struct swap_map_handle { struct swap_map_handle {
struct swap_map_page *cur; struct swap_map_page *cur;
unsigned long cur_swap; sector_t cur_swap;
struct bitmap_page *bitmap; struct bitmap_page *bitmap;
unsigned int k; unsigned int k;
}; };
@ -287,7 +283,7 @@ static int get_swap_writer(struct swap_map_handle *handle)
release_swap_writer(handle); release_swap_writer(handle);
return -ENOMEM; return -ENOMEM;
} }
handle->cur_swap = alloc_swap_page(root_swap, handle->bitmap); handle->cur_swap = alloc_swapdev_block(root_swap, handle->bitmap);
if (!handle->cur_swap) { if (!handle->cur_swap) {
release_swap_writer(handle); release_swap_writer(handle);
return -ENOSPC; return -ENOSPC;
@ -300,11 +296,11 @@ static int swap_write_page(struct swap_map_handle *handle, void *buf,
struct bio **bio_chain) struct bio **bio_chain)
{ {
int error = 0; int error = 0;
unsigned long offset; sector_t offset;
if (!handle->cur) if (!handle->cur)
return -EINVAL; return -EINVAL;
offset = alloc_swap_page(root_swap, handle->bitmap); offset = alloc_swapdev_block(root_swap, handle->bitmap);
error = write_page(buf, offset, bio_chain); error = write_page(buf, offset, bio_chain);
if (error) if (error)
return error; return error;
@ -313,7 +309,7 @@ static int swap_write_page(struct swap_map_handle *handle, void *buf,
error = wait_on_bio_chain(bio_chain); error = wait_on_bio_chain(bio_chain);
if (error) if (error)
goto out; goto out;
offset = alloc_swap_page(root_swap, handle->bitmap); offset = alloc_swapdev_block(root_swap, handle->bitmap);
if (!offset) if (!offset)
return -ENOSPC; return -ENOSPC;
handle->cur->next_swap = offset; handle->cur->next_swap = offset;
@ -413,37 +409,47 @@ int swsusp_write(void)
struct swsusp_info *header; struct swsusp_info *header;
int error; int error;
if ((error = swsusp_swap_check())) { error = swsusp_swap_check();
if (error) {
printk(KERN_ERR "swsusp: Cannot find swap device, try " printk(KERN_ERR "swsusp: Cannot find swap device, try "
"swapon -a.\n"); "swapon -a.\n");
return error; return error;
} }
memset(&snapshot, 0, sizeof(struct snapshot_handle)); memset(&snapshot, 0, sizeof(struct snapshot_handle));
error = snapshot_read_next(&snapshot, PAGE_SIZE); error = snapshot_read_next(&snapshot, PAGE_SIZE);
if (error < PAGE_SIZE) if (error < PAGE_SIZE) {
return error < 0 ? error : -EFAULT; if (error >= 0)
error = -EFAULT;
goto out;
}
header = (struct swsusp_info *)data_of(snapshot); header = (struct swsusp_info *)data_of(snapshot);
if (!enough_swap(header->pages)) { if (!enough_swap(header->pages)) {
printk(KERN_ERR "swsusp: Not enough free swap\n"); printk(KERN_ERR "swsusp: Not enough free swap\n");
return -ENOSPC; error = -ENOSPC;
goto out;
} }
error = get_swap_writer(&handle); error = get_swap_writer(&handle);
if (!error) { if (!error) {
unsigned long start = handle.cur_swap; sector_t start = handle.cur_swap;
error = swap_write_page(&handle, header, NULL); error = swap_write_page(&handle, header, NULL);
if (!error) if (!error)
error = save_image(&handle, &snapshot, error = save_image(&handle, &snapshot,
header->pages - 1); header->pages - 1);
if (!error) { if (!error) {
flush_swap_writer(&handle); flush_swap_writer(&handle);
printk("S"); printk("S");
error = mark_swapfiles(swp_entry(root_swap, start)); error = mark_swapfiles(start);
printk("|\n"); printk("|\n");
} }
} }
if (error) if (error)
free_all_swap_pages(root_swap, handle.bitmap); free_all_swap_pages(root_swap, handle.bitmap);
release_swap_writer(&handle); release_swap_writer(&handle);
out:
swsusp_close();
return error; return error;
} }
@ -459,17 +465,18 @@ static void release_swap_reader(struct swap_map_handle *handle)
handle->cur = NULL; handle->cur = NULL;
} }
static int get_swap_reader(struct swap_map_handle *handle, static int get_swap_reader(struct swap_map_handle *handle, sector_t start)
swp_entry_t start)
{ {
int error; int error;
if (!swp_offset(start)) if (!start)
return -EINVAL; return -EINVAL;
handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC); handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
if (!handle->cur) if (!handle->cur)
return -ENOMEM; return -ENOMEM;
error = bio_read_page(swp_offset(start), handle->cur, NULL);
error = bio_read_page(start, handle->cur, NULL);
if (error) { if (error) {
release_swap_reader(handle); release_swap_reader(handle);
return error; return error;
@ -481,7 +488,7 @@ static int get_swap_reader(struct swap_map_handle *handle,
static int swap_read_page(struct swap_map_handle *handle, void *buf, static int swap_read_page(struct swap_map_handle *handle, void *buf,
struct bio **bio_chain) struct bio **bio_chain)
{ {
unsigned long offset; sector_t offset;
int error; int error;
if (!handle->cur) if (!handle->cur)
@ -608,7 +615,7 @@ int swsusp_check(void)
if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) { if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10); memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
/* Reset swap signature now */ /* Reset swap signature now */
error = bio_write_page(0, &swsusp_header); error = bio_write_page(0, &swsusp_header, NULL);
} else { } else {
return -EINVAL; return -EINVAL;
} }

View File

@ -134,18 +134,18 @@ static int bitmap_set(struct bitmap_page *bitmap, unsigned long bit)
return 0; return 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; unsigned long offset;
offset = swp_offset(get_swap_page_of_type(swap)); offset = swp_offset(get_swap_page_of_type(swap));
if (offset) { if (offset) {
if (bitmap_set(bitmap, offset)) { if (bitmap_set(bitmap, offset))
swap_free(swp_entry(swap, 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) void free_all_swap_pages(int swap, struct bitmap_page *bitmap)

View File

@ -126,7 +126,8 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp,
{ {
int error = 0; int error = 0;
struct snapshot_data *data; struct snapshot_data *data;
loff_t offset, avail; loff_t avail;
sector_t offset;
if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC) if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
return -ENOTTY; return -ENOTTY;
@ -240,10 +241,10 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp,
break; break;
} }
} }
offset = alloc_swap_page(data->swap, data->bitmap); offset = alloc_swapdev_block(data->swap, data->bitmap);
if (offset) { if (offset) {
offset <<= PAGE_SHIFT; offset <<= PAGE_SHIFT;
error = put_user(offset, (loff_t __user *)arg); error = put_user(offset, (sector_t __user *)arg);
} else { } else {
error = -ENOSPC; error = -ENOSPC;
} }

View File

@ -147,48 +147,3 @@ int swap_readpage(struct file *file, struct page *page)
out: out:
return ret; return ret;
} }
#ifdef CONFIG_SOFTWARE_SUSPEND
/*
* A scruffy utility function to read or write an arbitrary swap page
* and wait on the I/O. The caller must have a ref on the page.
*
* We use end_swap_bio_read() even for writes, because it happens to do what
* we want.
*/
int rw_swap_page_sync(int rw, swp_entry_t entry, struct page *page,
struct bio **bio_chain)
{
struct bio *bio;
int ret = 0;
int bio_rw;
lock_page(page);
bio = get_swap_bio(GFP_KERNEL, entry.val, page, end_swap_bio_read);
if (bio == NULL) {
unlock_page(page);
ret = -ENOMEM;
goto out;
}
bio_rw = rw;
if (!bio_chain)
bio_rw |= (1 << BIO_RW_SYNC);
if (bio_chain)
bio_get(bio);
submit_bio(bio_rw, bio);
if (bio_chain == NULL) {
wait_on_page_locked(page);
if (!PageUptodate(page) || PageError(page))
ret = -EIO;
}
if (bio_chain) {
bio->bi_private = *bio_chain;
*bio_chain = bio;
}
out:
return ret;
}
#endif

View File

@ -945,6 +945,23 @@ sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset)
} }
} }
#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 * Free all of a swapdev's extent information
*/ */