Skip to content

Commit 8a0d613

Browse files
Jiri Slabyrjwysocki
authored andcommitted
PM / Hibernate: Separate block_io
Move block I/O operations to a separate file. It is because it will be used later not only by the swap writer. Signed-off-by: Jiri Slaby <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent d3c1b24 commit 8a0d613

File tree

4 files changed

+139
-112
lines changed

4 files changed

+139
-112
lines changed

kernel/power/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ obj-$(CONFIG_PM_SLEEP) += console.o
88
obj-$(CONFIG_FREEZER) += process.o
99
obj-$(CONFIG_SUSPEND) += suspend.o
1010
obj-$(CONFIG_PM_TEST_SUSPEND) += suspend_test.o
11-
obj-$(CONFIG_HIBERNATION) += hibernate.o snapshot.o swap.o user.o
11+
obj-$(CONFIG_HIBERNATION) += hibernate.o snapshot.o swap.o user.o \
12+
block_io.o
1213
obj-$(CONFIG_HIBERNATION_NVS) += hibernate_nvs.o
1314

1415
obj-$(CONFIG_MAGIC_SYSRQ) += poweroff.o

kernel/power/block_io.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* This file provides functions for block I/O operations on swap/file.
3+
*
4+
* Copyright (C) 1998,2001-2005 Pavel Machek <[email protected]>
5+
* Copyright (C) 2006 Rafael J. Wysocki <[email protected]>
6+
*
7+
* This file is released under the GPLv2.
8+
*/
9+
10+
#include <linux/bio.h>
11+
#include <linux/kernel.h>
12+
#include <linux/pagemap.h>
13+
#include <linux/swap.h>
14+
15+
#include "power.h"
16+
17+
/**
18+
* submit - submit BIO request.
19+
* @rw: READ or WRITE.
20+
* @off physical offset of page.
21+
* @page: page we're reading or writing.
22+
* @bio_chain: list of pending biod (for async reading)
23+
*
24+
* Straight from the textbook - allocate and initialize the bio.
25+
* If we're reading, make sure the page is marked as dirty.
26+
* Then submit it and, if @bio_chain == NULL, wait.
27+
*/
28+
static int submit(int rw, struct block_device *bdev, sector_t sector,
29+
struct page *page, struct bio **bio_chain)
30+
{
31+
const int bio_rw = rw | (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
32+
struct bio *bio;
33+
34+
bio = bio_alloc(__GFP_WAIT | __GFP_HIGH, 1);
35+
bio->bi_sector = sector;
36+
bio->bi_bdev = bdev;
37+
bio->bi_end_io = end_swap_bio_read;
38+
39+
if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
40+
printk(KERN_ERR "PM: Adding page to bio failed at %ld\n",
41+
sector);
42+
bio_put(bio);
43+
return -EFAULT;
44+
}
45+
46+
lock_page(page);
47+
bio_get(bio);
48+
49+
if (bio_chain == NULL) {
50+
submit_bio(bio_rw, bio);
51+
wait_on_page_locked(page);
52+
if (rw == READ)
53+
bio_set_pages_dirty(bio);
54+
bio_put(bio);
55+
} else {
56+
if (rw == READ)
57+
get_page(page); /* These pages are freed later */
58+
bio->bi_private = *bio_chain;
59+
*bio_chain = bio;
60+
submit_bio(bio_rw, bio);
61+
}
62+
return 0;
63+
}
64+
65+
int hib_bio_read_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
66+
{
67+
return submit(READ, hib_resume_bdev, page_off * (PAGE_SIZE >> 9),
68+
virt_to_page(addr), bio_chain);
69+
}
70+
71+
int hib_bio_write_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
72+
{
73+
return submit(WRITE, hib_resume_bdev, page_off * (PAGE_SIZE >> 9),
74+
virt_to_page(addr), bio_chain);
75+
}
76+
77+
int hib_wait_on_bio_chain(struct bio **bio_chain)
78+
{
79+
struct bio *bio;
80+
struct bio *next_bio;
81+
int ret = 0;
82+
83+
if (bio_chain == NULL)
84+
return 0;
85+
86+
bio = *bio_chain;
87+
if (bio == NULL)
88+
return 0;
89+
while (bio) {
90+
struct page *page;
91+
92+
next_bio = bio->bi_private;
93+
page = bio->bi_io_vec[0].bv_page;
94+
wait_on_page_locked(page);
95+
if (!PageUptodate(page) || PageError(page))
96+
ret = -EIO;
97+
put_page(page);
98+
bio_put(bio);
99+
bio = next_bio;
100+
}
101+
*bio_chain = NULL;
102+
return ret;
103+
}

kernel/power/power.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@ extern int swsusp_read(unsigned int *flags_p);
142142
extern int swsusp_write(unsigned int flags);
143143
extern void swsusp_close(fmode_t);
144144

145+
/* kernel/power/block_io.c */
146+
extern struct block_device *hib_resume_bdev;
147+
148+
extern int hib_bio_read_page(pgoff_t page_off, void *addr,
149+
struct bio **bio_chain);
150+
extern int hib_bio_write_page(pgoff_t page_off, void *addr,
151+
struct bio **bio_chain);
152+
extern int hib_wait_on_bio_chain(struct bio **bio_chain);
153+
145154
struct timeval;
146155
/* kernel/power/swsusp.c */
147156
extern void swsusp_show_speed(struct timeval *, struct timeval *,

kernel/power/swap.c

Lines changed: 25 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -145,93 +145,7 @@ int swsusp_swap_in_use(void)
145145
*/
146146

147147
static unsigned short root_swap = 0xffff;
148-
static struct block_device *resume_bdev;
149-
150-
/**
151-
* submit - submit BIO request.
152-
* @rw: READ or WRITE.
153-
* @off physical offset of page.
154-
* @page: page we're reading or writing.
155-
* @bio_chain: list of pending biod (for async reading)
156-
*
157-
* Straight from the textbook - allocate and initialize the bio.
158-
* If we're reading, make sure the page is marked as dirty.
159-
* Then submit it and, if @bio_chain == NULL, wait.
160-
*/
161-
static int submit(int rw, pgoff_t page_off, struct page *page,
162-
struct bio **bio_chain)
163-
{
164-
const int bio_rw = rw | (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
165-
struct bio *bio;
166-
167-
bio = bio_alloc(__GFP_WAIT | __GFP_HIGH, 1);
168-
bio->bi_sector = page_off * (PAGE_SIZE >> 9);
169-
bio->bi_bdev = resume_bdev;
170-
bio->bi_end_io = end_swap_bio_read;
171-
172-
if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
173-
printk(KERN_ERR "PM: Adding page to bio failed at %ld\n",
174-
page_off);
175-
bio_put(bio);
176-
return -EFAULT;
177-
}
178-
179-
lock_page(page);
180-
bio_get(bio);
181-
182-
if (bio_chain == NULL) {
183-
submit_bio(bio_rw, bio);
184-
wait_on_page_locked(page);
185-
if (rw == READ)
186-
bio_set_pages_dirty(bio);
187-
bio_put(bio);
188-
} else {
189-
if (rw == READ)
190-
get_page(page); /* These pages are freed later */
191-
bio->bi_private = *bio_chain;
192-
*bio_chain = bio;
193-
submit_bio(bio_rw, bio);
194-
}
195-
return 0;
196-
}
197-
198-
static int bio_read_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
199-
{
200-
return submit(READ, page_off, virt_to_page(addr), bio_chain);
201-
}
202-
203-
static int bio_write_page(pgoff_t page_off, void *addr, struct bio **bio_chain)
204-
{
205-
return submit(WRITE, page_off, virt_to_page(addr), bio_chain);
206-
}
207-
208-
static int wait_on_bio_chain(struct bio **bio_chain)
209-
{
210-
struct bio *bio;
211-
struct bio *next_bio;
212-
int ret = 0;
213-
214-
if (bio_chain == NULL)
215-
return 0;
216-
217-
bio = *bio_chain;
218-
if (bio == NULL)
219-
return 0;
220-
while (bio) {
221-
struct page *page;
222-
223-
next_bio = bio->bi_private;
224-
page = bio->bi_io_vec[0].bv_page;
225-
wait_on_page_locked(page);
226-
if (!PageUptodate(page) || PageError(page))
227-
ret = -EIO;
228-
put_page(page);
229-
bio_put(bio);
230-
bio = next_bio;
231-
}
232-
*bio_chain = NULL;
233-
return ret;
234-
}
148+
struct block_device *hib_resume_bdev;
235149

236150
/*
237151
* Saving part
@@ -241,14 +155,14 @@ static int mark_swapfiles(sector_t start, unsigned int flags)
241155
{
242156
int error;
243157

244-
bio_read_page(swsusp_resume_block, swsusp_header, NULL);
158+
hib_bio_read_page(swsusp_resume_block, swsusp_header, NULL);
245159
if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) ||
246160
!memcmp("SWAPSPACE2",swsusp_header->sig, 10)) {
247161
memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10);
248162
memcpy(swsusp_header->sig,SWSUSP_SIG, 10);
249163
swsusp_header->image = start;
250164
swsusp_header->flags = flags;
251-
error = bio_write_page(swsusp_resume_block,
165+
error = hib_bio_write_page(swsusp_resume_block,
252166
swsusp_header, NULL);
253167
} else {
254168
printk(KERN_ERR "PM: Swap header not found!\n");
@@ -267,18 +181,18 @@ static int swsusp_swap_check(void) /* This is called before saving image */
267181
int res;
268182

269183
res = swap_type_of(swsusp_resume_device, swsusp_resume_block,
270-
&resume_bdev);
184+
&hib_resume_bdev);
271185
if (res < 0)
272186
return res;
273187

274188
root_swap = res;
275-
res = blkdev_get(resume_bdev, FMODE_WRITE);
189+
res = blkdev_get(hib_resume_bdev, FMODE_WRITE);
276190
if (res)
277191
return res;
278192

279-
res = set_blocksize(resume_bdev, PAGE_SIZE);
193+
res = set_blocksize(hib_resume_bdev, PAGE_SIZE);
280194
if (res < 0)
281-
blkdev_put(resume_bdev, FMODE_WRITE);
195+
blkdev_put(hib_resume_bdev, FMODE_WRITE);
282196

283197
return res;
284198
}
@@ -309,7 +223,7 @@ static int write_page(void *buf, sector_t offset, struct bio **bio_chain)
309223
} else {
310224
src = buf;
311225
}
312-
return bio_write_page(offset, src, bio_chain);
226+
return hib_bio_write_page(offset, src, bio_chain);
313227
}
314228

315229
/*
@@ -380,7 +294,7 @@ static int swap_write_page(struct swap_map_handle *handle, void *buf,
380294
return error;
381295
handle->cur->entries[handle->k++] = offset;
382296
if (handle->k >= MAP_PAGE_ENTRIES) {
383-
error = wait_on_bio_chain(bio_chain);
297+
error = hib_wait_on_bio_chain(bio_chain);
384298
if (error)
385299
goto out;
386300
offset = alloc_swapdev_block(root_swap);
@@ -441,7 +355,7 @@ static int save_image(struct swap_map_handle *handle,
441355
printk(KERN_CONT "\b\b\b\b%3d%%", nr_pages / m);
442356
nr_pages++;
443357
}
444-
err2 = wait_on_bio_chain(&bio);
358+
err2 = hib_wait_on_bio_chain(&bio);
445359
do_gettimeofday(&stop);
446360
if (!ret)
447361
ret = err2;
@@ -553,7 +467,7 @@ static int get_swap_reader(struct swap_map_handle *handle, sector_t start)
553467
if (!handle->cur)
554468
return -ENOMEM;
555469

556-
error = bio_read_page(start, handle->cur, NULL);
470+
error = hib_bio_read_page(start, handle->cur, NULL);
557471
if (error) {
558472
release_swap_reader(handle);
559473
return error;
@@ -573,17 +487,17 @@ static int swap_read_page(struct swap_map_handle *handle, void *buf,
573487
offset = handle->cur->entries[handle->k];
574488
if (!offset)
575489
return -EFAULT;
576-
error = bio_read_page(offset, buf, bio_chain);
490+
error = hib_bio_read_page(offset, buf, bio_chain);
577491
if (error)
578492
return error;
579493
if (++handle->k >= MAP_PAGE_ENTRIES) {
580-
error = wait_on_bio_chain(bio_chain);
494+
error = hib_wait_on_bio_chain(bio_chain);
581495
handle->k = 0;
582496
offset = handle->cur->next_swap;
583497
if (!offset)
584498
release_swap_reader(handle);
585499
else if (!error)
586-
error = bio_read_page(offset, handle->cur, NULL);
500+
error = hib_bio_read_page(offset, handle->cur, NULL);
587501
}
588502
return error;
589503
}
@@ -622,14 +536,14 @@ static int load_image(struct swap_map_handle *handle,
622536
if (error)
623537
break;
624538
if (snapshot->sync_read)
625-
error = wait_on_bio_chain(&bio);
539+
error = hib_wait_on_bio_chain(&bio);
626540
if (error)
627541
break;
628542
if (!(nr_pages % m))
629543
printk("\b\b\b\b%3d%%", nr_pages / m);
630544
nr_pages++;
631545
}
632-
err2 = wait_on_bio_chain(&bio);
546+
err2 = hib_wait_on_bio_chain(&bio);
633547
do_gettimeofday(&stop);
634548
if (!error)
635549
error = err2;
@@ -686,31 +600,31 @@ int swsusp_check(void)
686600
{
687601
int error;
688602

689-
resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
690-
if (!IS_ERR(resume_bdev)) {
691-
set_blocksize(resume_bdev, PAGE_SIZE);
603+
hib_resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
604+
if (!IS_ERR(hib_resume_bdev)) {
605+
set_blocksize(hib_resume_bdev, PAGE_SIZE);
692606
memset(swsusp_header, 0, PAGE_SIZE);
693-
error = bio_read_page(swsusp_resume_block,
607+
error = hib_bio_read_page(swsusp_resume_block,
694608
swsusp_header, NULL);
695609
if (error)
696610
goto put;
697611

698612
if (!memcmp(SWSUSP_SIG, swsusp_header->sig, 10)) {
699613
memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
700614
/* Reset swap signature now */
701-
error = bio_write_page(swsusp_resume_block,
615+
error = hib_bio_write_page(swsusp_resume_block,
702616
swsusp_header, NULL);
703617
} else {
704618
error = -EINVAL;
705619
}
706620

707621
put:
708622
if (error)
709-
blkdev_put(resume_bdev, FMODE_READ);
623+
blkdev_put(hib_resume_bdev, FMODE_READ);
710624
else
711625
pr_debug("PM: Signature found, resuming\n");
712626
} else {
713-
error = PTR_ERR(resume_bdev);
627+
error = PTR_ERR(hib_resume_bdev);
714628
}
715629

716630
if (error)
@@ -725,12 +639,12 @@ int swsusp_check(void)
725639

726640
void swsusp_close(fmode_t mode)
727641
{
728-
if (IS_ERR(resume_bdev)) {
642+
if (IS_ERR(hib_resume_bdev)) {
729643
pr_debug("PM: Image device not initialised\n");
730644
return;
731645
}
732646

733-
blkdev_put(resume_bdev, mode);
647+
blkdev_put(hib_resume_bdev, mode);
734648
}
735649

736650
static int swsusp_header_init(void)

0 commit comments

Comments
 (0)