Commit 9625a5f2 authored by Hugh Dickins's avatar Hugh Dickins Committed by Linus Torvalds

swap_info: include first_swap_extent

Make better use of the space by folding first swap_extent into its
swap_info_struct, instead of just the list_head: swap partitions need
only that one, and for others it's used as a circular list anyway.

[jirislaby@gmail.com: fix crash on double swapon]
Signed-off-by: default avatarHugh Dickins <hugh.dickins@tiscali.co.uk>
Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Cc: Rik van Riel <riel@redhat.com>
Signed-off-by: default avatarJiri Slaby <jirislaby@gmail.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent efa90a98
...@@ -165,7 +165,7 @@ struct swap_info_struct { ...@@ -165,7 +165,7 @@ struct swap_info_struct {
signed char next; /* next type on the swap list */ signed char next; /* next type on the swap list */
struct file *swap_file; struct file *swap_file;
struct block_device *bdev; struct block_device *bdev;
struct list_head extent_list; struct swap_extent first_swap_extent;
struct swap_extent *curr_swap_extent; struct swap_extent *curr_swap_extent;
unsigned short *swap_map; unsigned short *swap_map;
unsigned int lowest_bit; unsigned int lowest_bit;
......
...@@ -145,23 +145,28 @@ void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page) ...@@ -145,23 +145,28 @@ void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page)
static int discard_swap(struct swap_info_struct *si) static int discard_swap(struct swap_info_struct *si)
{ {
struct swap_extent *se; struct swap_extent *se;
sector_t start_block;
sector_t nr_blocks;
int err = 0; int err = 0;
list_for_each_entry(se, &si->extent_list, list) { /* Do not discard the swap header page! */
sector_t start_block = se->start_block << (PAGE_SHIFT - 9); se = &si->first_swap_extent;
sector_t nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9); start_block = (se->start_block + 1) << (PAGE_SHIFT - 9);
nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
if (nr_blocks) {
err = blkdev_issue_discard(si->bdev, start_block,
nr_blocks, GFP_KERNEL, DISCARD_FL_BARRIER);
if (err)
return err;
cond_resched();
}
if (se->start_page == 0) { list_for_each_entry(se, &si->first_swap_extent.list, list) {
/* Do not discard the swap header page! */ start_block = se->start_block << (PAGE_SHIFT - 9);
start_block += 1 << (PAGE_SHIFT - 9); nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
nr_blocks -= 1 << (PAGE_SHIFT - 9);
if (!nr_blocks)
continue;
}
err = blkdev_issue_discard(si->bdev, start_block, err = blkdev_issue_discard(si->bdev, start_block,
nr_blocks, GFP_KERNEL, nr_blocks, GFP_KERNEL, DISCARD_FL_BARRIER);
DISCARD_FL_BARRIER);
if (err) if (err)
break; break;
...@@ -200,14 +205,11 @@ static void discard_swap_cluster(struct swap_info_struct *si, ...@@ -200,14 +205,11 @@ static void discard_swap_cluster(struct swap_info_struct *si,
start_block <<= PAGE_SHIFT - 9; start_block <<= PAGE_SHIFT - 9;
nr_blocks <<= PAGE_SHIFT - 9; nr_blocks <<= PAGE_SHIFT - 9;
if (blkdev_issue_discard(si->bdev, start_block, if (blkdev_issue_discard(si->bdev, start_block,
nr_blocks, GFP_NOIO, nr_blocks, GFP_NOIO, DISCARD_FL_BARRIER))
DISCARD_FL_BARRIER))
break; break;
} }
lh = se->list.next; lh = se->list.next;
if (lh == &si->extent_list)
lh = lh->next;
se = list_entry(lh, struct swap_extent, list); se = list_entry(lh, struct swap_extent, list);
} }
} }
...@@ -761,10 +763,8 @@ int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p) ...@@ -761,10 +763,8 @@ int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
return type; return type;
} }
if (bdev == sis->bdev) { if (bdev == sis->bdev) {
struct swap_extent *se; struct swap_extent *se = &sis->first_swap_extent;
se = list_entry(sis->extent_list.next,
struct swap_extent, list);
if (se->start_block == offset) { if (se->start_block == offset) {
if (bdev_p) if (bdev_p)
*bdev_p = bdgrab(sis->bdev); *bdev_p = bdgrab(sis->bdev);
...@@ -1310,8 +1310,6 @@ sector_t map_swap_page(swp_entry_t entry, struct block_device **bdev) ...@@ -1310,8 +1310,6 @@ sector_t map_swap_page(swp_entry_t entry, struct block_device **bdev)
return se->start_block + (offset - se->start_page); return se->start_block + (offset - se->start_page);
} }
lh = se->list.next; lh = se->list.next;
if (lh == &sis->extent_list)
lh = lh->next;
se = list_entry(lh, struct swap_extent, list); se = list_entry(lh, struct swap_extent, list);
sis->curr_swap_extent = se; sis->curr_swap_extent = se;
BUG_ON(se == start_se); /* It *must* be present */ BUG_ON(se == start_se); /* It *must* be present */
...@@ -1340,10 +1338,10 @@ sector_t swapdev_block(int type, pgoff_t offset) ...@@ -1340,10 +1338,10 @@ sector_t swapdev_block(int type, pgoff_t offset)
*/ */
static void destroy_swap_extents(struct swap_info_struct *sis) static void destroy_swap_extents(struct swap_info_struct *sis)
{ {
while (!list_empty(&sis->extent_list)) { while (!list_empty(&sis->first_swap_extent.list)) {
struct swap_extent *se; struct swap_extent *se;
se = list_entry(sis->extent_list.next, se = list_entry(sis->first_swap_extent.list.next,
struct swap_extent, list); struct swap_extent, list);
list_del(&se->list); list_del(&se->list);
kfree(se); kfree(se);
...@@ -1364,8 +1362,15 @@ add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, ...@@ -1364,8 +1362,15 @@ add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
struct swap_extent *new_se; struct swap_extent *new_se;
struct list_head *lh; struct list_head *lh;
lh = sis->extent_list.prev; /* The highest page extent */ if (start_page == 0) {
if (lh != &sis->extent_list) { se = &sis->first_swap_extent;
sis->curr_swap_extent = se;
se->start_page = 0;
se->nr_pages = nr_pages;
se->start_block = start_block;
return 1;
} else {
lh = sis->first_swap_extent.list.prev; /* Highest extent */
se = list_entry(lh, struct swap_extent, list); se = list_entry(lh, struct swap_extent, list);
BUG_ON(se->start_page + se->nr_pages != start_page); BUG_ON(se->start_page + se->nr_pages != start_page);
if (se->start_block + se->nr_pages == start_block) { if (se->start_block + se->nr_pages == start_block) {
...@@ -1385,7 +1390,7 @@ add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, ...@@ -1385,7 +1390,7 @@ add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
new_se->nr_pages = nr_pages; new_se->nr_pages = nr_pages;
new_se->start_block = start_block; new_se->start_block = start_block;
list_add_tail(&new_se->list, &sis->extent_list); list_add_tail(&new_se->list, &sis->first_swap_extent.list);
return 1; return 1;
} }
...@@ -1437,7 +1442,7 @@ static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) ...@@ -1437,7 +1442,7 @@ static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
if (S_ISBLK(inode->i_mode)) { if (S_ISBLK(inode->i_mode)) {
ret = add_swap_extent(sis, 0, sis->max, 0); ret = add_swap_extent(sis, 0, sis->max, 0);
*span = sis->pages; *span = sis->pages;
goto done; goto out;
} }
blkbits = inode->i_blkbits; blkbits = inode->i_blkbits;
...@@ -1508,15 +1513,12 @@ static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) ...@@ -1508,15 +1513,12 @@ static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
sis->max = page_no; sis->max = page_no;
sis->pages = page_no - 1; sis->pages = page_no - 1;
sis->highest_bit = page_no - 1; sis->highest_bit = page_no - 1;
done: out:
sis->curr_swap_extent = list_entry(sis->extent_list.prev, return ret;
struct swap_extent, list);
goto out;
bad_bmap: bad_bmap:
printk(KERN_ERR "swapon: swapfile has holes\n"); printk(KERN_ERR "swapon: swapfile has holes\n");
ret = -EINVAL; ret = -EINVAL;
out: goto out;
return ret;
} }
SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
...@@ -1815,7 +1817,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) ...@@ -1815,7 +1817,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
kfree(p); kfree(p);
goto out; goto out;
} }
INIT_LIST_HEAD(&p->extent_list);
if (type >= nr_swapfiles) { if (type >= nr_swapfiles) {
p->type = type; p->type = type;
swap_info[type] = p; swap_info[type] = p;
...@@ -1834,6 +1835,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) ...@@ -1834,6 +1835,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
* would be relying on p->type to remain valid. * would be relying on p->type to remain valid.
*/ */
} }
INIT_LIST_HEAD(&p->first_swap_extent.list);
p->flags = SWP_USED; p->flags = SWP_USED;
p->next = -1; p->next = -1;
spin_unlock(&swap_lock); spin_unlock(&swap_lock);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment