diff options
author | Jiri Slaby <jslaby@suse.cz> | 2010-05-01 17:52:34 -0400 |
---|---|---|
committer | Rafael J. Wysocki <rjw@sisk.pl> | 2010-05-10 17:08:18 -0400 |
commit | 8a0d613fa12e1b7f7f71ca88ed7dc2a3de95121a (patch) | |
tree | 1012509bb830002ba7c41562c26ad211631a26c0 | |
parent | d3c1b24c50e8b2bbc840322caf26c7eada594d21 (diff) |
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 <jslaby@suse.cz>
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
-rw-r--r-- | kernel/power/Makefile | 3 | ||||
-rw-r--r-- | kernel/power/block_io.c | 103 | ||||
-rw-r--r-- | kernel/power/power.h | 9 | ||||
-rw-r--r-- | kernel/power/swap.c | 136 |
4 files changed, 139 insertions, 112 deletions
diff --git a/kernel/power/Makefile b/kernel/power/Makefile index 43191815f874..524e058dcf06 100644 --- a/kernel/power/Makefile +++ b/kernel/power/Makefile | |||
@@ -8,7 +8,8 @@ obj-$(CONFIG_PM_SLEEP) += console.o | |||
8 | obj-$(CONFIG_FREEZER) += process.o | 8 | obj-$(CONFIG_FREEZER) += process.o |
9 | obj-$(CONFIG_SUSPEND) += suspend.o | 9 | obj-$(CONFIG_SUSPEND) += suspend.o |
10 | obj-$(CONFIG_PM_TEST_SUSPEND) += suspend_test.o | 10 | 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 | ||
12 | obj-$(CONFIG_HIBERNATION_NVS) += hibernate_nvs.o | 13 | obj-$(CONFIG_HIBERNATION_NVS) += hibernate_nvs.o |
13 | 14 | ||
14 | obj-$(CONFIG_MAGIC_SYSRQ) += poweroff.o | 15 | obj-$(CONFIG_MAGIC_SYSRQ) += poweroff.o |
diff --git a/kernel/power/block_io.c b/kernel/power/block_io.c new file mode 100644 index 000000000000..2a2f8aed8e59 --- /dev/null +++ b/kernel/power/block_io.c | |||
@@ -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 <pavel@ucw.cz> | ||
5 | * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl> | ||
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 | } | ||
diff --git a/kernel/power/power.h b/kernel/power/power.h index b1e207dde1c2..006270fe382d 100644 --- a/kernel/power/power.h +++ b/kernel/power/power.h | |||
@@ -142,6 +142,15 @@ extern int swsusp_read(unsigned int *flags_p); | |||
142 | extern int swsusp_write(unsigned int flags); | 142 | extern int swsusp_write(unsigned int flags); |
143 | extern void swsusp_close(fmode_t); | 143 | extern void swsusp_close(fmode_t); |
144 | 144 | ||
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 | |||
145 | struct timeval; | 154 | struct timeval; |
146 | /* kernel/power/swsusp.c */ | 155 | /* kernel/power/swsusp.c */ |
147 | extern void swsusp_show_speed(struct timeval *, struct timeval *, | 156 | extern void swsusp_show_speed(struct timeval *, struct timeval *, |
diff --git a/kernel/power/swap.c b/kernel/power/swap.c index 7f2a17e4067b..1b1ab6fcf386 100644 --- a/kernel/power/swap.c +++ b/kernel/power/swap.c | |||
@@ -145,93 +145,7 @@ int swsusp_swap_in_use(void) | |||
145 | */ | 145 | */ |
146 | 146 | ||
147 | static unsigned short root_swap = 0xffff; | 147 | static unsigned short root_swap = 0xffff; |
148 | static struct block_device *resume_bdev; | 148 | struct block_device *hib_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 | } | ||
235 | 149 | ||
236 | /* | 150 | /* |
237 | * Saving part | 151 | * Saving part |
@@ -241,14 +155,14 @@ static int mark_swapfiles(sector_t start, unsigned int flags) | |||
241 | { | 155 | { |
242 | int error; | 156 | int error; |
243 | 157 | ||
244 | bio_read_page(swsusp_resume_block, swsusp_header, NULL); | 158 | hib_bio_read_page(swsusp_resume_block, swsusp_header, NULL); |
245 | if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) || | 159 | if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) || |
246 | !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) { | 160 | !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) { |
247 | memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10); | 161 | memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10); |
248 | memcpy(swsusp_header->sig,SWSUSP_SIG, 10); | 162 | memcpy(swsusp_header->sig,SWSUSP_SIG, 10); |
249 | swsusp_header->image = start; | 163 | swsusp_header->image = start; |
250 | swsusp_header->flags = flags; | 164 | swsusp_header->flags = flags; |
251 | error = bio_write_page(swsusp_resume_block, | 165 | error = hib_bio_write_page(swsusp_resume_block, |
252 | swsusp_header, NULL); | 166 | swsusp_header, NULL); |
253 | } else { | 167 | } else { |
254 | printk(KERN_ERR "PM: Swap header not found!\n"); | 168 | 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 */ | |||
267 | int res; | 181 | int res; |
268 | 182 | ||
269 | res = swap_type_of(swsusp_resume_device, swsusp_resume_block, | 183 | res = swap_type_of(swsusp_resume_device, swsusp_resume_block, |
270 | &resume_bdev); | 184 | &hib_resume_bdev); |
271 | if (res < 0) | 185 | if (res < 0) |
272 | return res; | 186 | return res; |
273 | 187 | ||
274 | root_swap = res; | 188 | root_swap = res; |
275 | res = blkdev_get(resume_bdev, FMODE_WRITE); | 189 | res = blkdev_get(hib_resume_bdev, FMODE_WRITE); |
276 | if (res) | 190 | if (res) |
277 | return res; | 191 | return res; |
278 | 192 | ||
279 | res = set_blocksize(resume_bdev, PAGE_SIZE); | 193 | res = set_blocksize(hib_resume_bdev, PAGE_SIZE); |
280 | if (res < 0) | 194 | if (res < 0) |
281 | blkdev_put(resume_bdev, FMODE_WRITE); | 195 | blkdev_put(hib_resume_bdev, FMODE_WRITE); |
282 | 196 | ||
283 | return res; | 197 | return res; |
284 | } | 198 | } |
@@ -309,7 +223,7 @@ static int write_page(void *buf, sector_t offset, struct bio **bio_chain) | |||
309 | } else { | 223 | } else { |
310 | src = buf; | 224 | src = buf; |
311 | } | 225 | } |
312 | return bio_write_page(offset, src, bio_chain); | 226 | return hib_bio_write_page(offset, src, bio_chain); |
313 | } | 227 | } |
314 | 228 | ||
315 | /* | 229 | /* |
@@ -380,7 +294,7 @@ static int swap_write_page(struct swap_map_handle *handle, void *buf, | |||
380 | return error; | 294 | return error; |
381 | handle->cur->entries[handle->k++] = offset; | 295 | handle->cur->entries[handle->k++] = offset; |
382 | if (handle->k >= MAP_PAGE_ENTRIES) { | 296 | if (handle->k >= MAP_PAGE_ENTRIES) { |
383 | error = wait_on_bio_chain(bio_chain); | 297 | error = hib_wait_on_bio_chain(bio_chain); |
384 | if (error) | 298 | if (error) |
385 | goto out; | 299 | goto out; |
386 | offset = alloc_swapdev_block(root_swap); | 300 | offset = alloc_swapdev_block(root_swap); |
@@ -441,7 +355,7 @@ static int save_image(struct swap_map_handle *handle, | |||
441 | printk(KERN_CONT "\b\b\b\b%3d%%", nr_pages / m); | 355 | printk(KERN_CONT "\b\b\b\b%3d%%", nr_pages / m); |
442 | nr_pages++; | 356 | nr_pages++; |
443 | } | 357 | } |
444 | err2 = wait_on_bio_chain(&bio); | 358 | err2 = hib_wait_on_bio_chain(&bio); |
445 | do_gettimeofday(&stop); | 359 | do_gettimeofday(&stop); |
446 | if (!ret) | 360 | if (!ret) |
447 | ret = err2; | 361 | ret = err2; |
@@ -553,7 +467,7 @@ static int get_swap_reader(struct swap_map_handle *handle, sector_t start) | |||
553 | if (!handle->cur) | 467 | if (!handle->cur) |
554 | return -ENOMEM; | 468 | return -ENOMEM; |
555 | 469 | ||
556 | error = bio_read_page(start, handle->cur, NULL); | 470 | error = hib_bio_read_page(start, handle->cur, NULL); |
557 | if (error) { | 471 | if (error) { |
558 | release_swap_reader(handle); | 472 | release_swap_reader(handle); |
559 | return error; | 473 | return error; |
@@ -573,17 +487,17 @@ static int swap_read_page(struct swap_map_handle *handle, void *buf, | |||
573 | offset = handle->cur->entries[handle->k]; | 487 | offset = handle->cur->entries[handle->k]; |
574 | if (!offset) | 488 | if (!offset) |
575 | return -EFAULT; | 489 | return -EFAULT; |
576 | error = bio_read_page(offset, buf, bio_chain); | 490 | error = hib_bio_read_page(offset, buf, bio_chain); |
577 | if (error) | 491 | if (error) |
578 | return error; | 492 | return error; |
579 | if (++handle->k >= MAP_PAGE_ENTRIES) { | 493 | if (++handle->k >= MAP_PAGE_ENTRIES) { |
580 | error = wait_on_bio_chain(bio_chain); | 494 | error = hib_wait_on_bio_chain(bio_chain); |
581 | handle->k = 0; | 495 | handle->k = 0; |
582 | offset = handle->cur->next_swap; | 496 | offset = handle->cur->next_swap; |
583 | if (!offset) | 497 | if (!offset) |
584 | release_swap_reader(handle); | 498 | release_swap_reader(handle); |
585 | else if (!error) | 499 | else if (!error) |
586 | error = bio_read_page(offset, handle->cur, NULL); | 500 | error = hib_bio_read_page(offset, handle->cur, NULL); |
587 | } | 501 | } |
588 | return error; | 502 | return error; |
589 | } | 503 | } |
@@ -622,14 +536,14 @@ static int load_image(struct swap_map_handle *handle, | |||
622 | if (error) | 536 | if (error) |
623 | break; | 537 | break; |
624 | if (snapshot->sync_read) | 538 | if (snapshot->sync_read) |
625 | error = wait_on_bio_chain(&bio); | 539 | error = hib_wait_on_bio_chain(&bio); |
626 | if (error) | 540 | if (error) |
627 | break; | 541 | break; |
628 | if (!(nr_pages % m)) | 542 | if (!(nr_pages % m)) |
629 | printk("\b\b\b\b%3d%%", nr_pages / m); | 543 | printk("\b\b\b\b%3d%%", nr_pages / m); |
630 | nr_pages++; | 544 | nr_pages++; |
631 | } | 545 | } |
632 | err2 = wait_on_bio_chain(&bio); | 546 | err2 = hib_wait_on_bio_chain(&bio); |
633 | do_gettimeofday(&stop); | 547 | do_gettimeofday(&stop); |
634 | if (!error) | 548 | if (!error) |
635 | error = err2; | 549 | error = err2; |
@@ -686,11 +600,11 @@ int swsusp_check(void) | |||
686 | { | 600 | { |
687 | int error; | 601 | int error; |
688 | 602 | ||
689 | resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ); | 603 | hib_resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ); |
690 | if (!IS_ERR(resume_bdev)) { | 604 | if (!IS_ERR(hib_resume_bdev)) { |
691 | set_blocksize(resume_bdev, PAGE_SIZE); | 605 | set_blocksize(hib_resume_bdev, PAGE_SIZE); |
692 | memset(swsusp_header, 0, PAGE_SIZE); | 606 | memset(swsusp_header, 0, PAGE_SIZE); |
693 | error = bio_read_page(swsusp_resume_block, | 607 | error = hib_bio_read_page(swsusp_resume_block, |
694 | swsusp_header, NULL); | 608 | swsusp_header, NULL); |
695 | if (error) | 609 | if (error) |
696 | goto put; | 610 | goto put; |
@@ -698,7 +612,7 @@ int swsusp_check(void) | |||
698 | if (!memcmp(SWSUSP_SIG, swsusp_header->sig, 10)) { | 612 | if (!memcmp(SWSUSP_SIG, swsusp_header->sig, 10)) { |
699 | memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10); | 613 | memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10); |
700 | /* Reset swap signature now */ | 614 | /* Reset swap signature now */ |
701 | error = bio_write_page(swsusp_resume_block, | 615 | error = hib_bio_write_page(swsusp_resume_block, |
702 | swsusp_header, NULL); | 616 | swsusp_header, NULL); |
703 | } else { | 617 | } else { |
704 | error = -EINVAL; | 618 | error = -EINVAL; |
@@ -706,11 +620,11 @@ int swsusp_check(void) | |||
706 | 620 | ||
707 | put: | 621 | put: |
708 | if (error) | 622 | if (error) |
709 | blkdev_put(resume_bdev, FMODE_READ); | 623 | blkdev_put(hib_resume_bdev, FMODE_READ); |
710 | else | 624 | else |
711 | pr_debug("PM: Signature found, resuming\n"); | 625 | pr_debug("PM: Signature found, resuming\n"); |
712 | } else { | 626 | } else { |
713 | error = PTR_ERR(resume_bdev); | 627 | error = PTR_ERR(hib_resume_bdev); |
714 | } | 628 | } |
715 | 629 | ||
716 | if (error) | 630 | if (error) |
@@ -725,12 +639,12 @@ put: | |||
725 | 639 | ||
726 | void swsusp_close(fmode_t mode) | 640 | void swsusp_close(fmode_t mode) |
727 | { | 641 | { |
728 | if (IS_ERR(resume_bdev)) { | 642 | if (IS_ERR(hib_resume_bdev)) { |
729 | pr_debug("PM: Image device not initialised\n"); | 643 | pr_debug("PM: Image device not initialised\n"); |
730 | return; | 644 | return; |
731 | } | 645 | } |
732 | 646 | ||
733 | blkdev_put(resume_bdev, mode); | 647 | blkdev_put(hib_resume_bdev, mode); |
734 | } | 648 | } |
735 | 649 | ||
736 | static int swsusp_header_init(void) | 650 | static int swsusp_header_init(void) |