Commit | Line | Data |
---|---|---|
61159a31 RW |
1 | /* |
2 | * linux/kernel/power/swap.c | |
3 | * | |
4 | * This file provides functions for reading the suspend image from | |
5 | * and writing it to a swap partition. | |
6 | * | |
7 | * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz> | |
8 | * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl> | |
9 | * | |
10 | * This file is released under the GPLv2. | |
11 | * | |
12 | */ | |
13 | ||
14 | #include <linux/module.h> | |
61159a31 RW |
15 | #include <linux/file.h> |
16 | #include <linux/utsname.h> | |
61159a31 RW |
17 | #include <linux/delay.h> |
18 | #include <linux/bitops.h> | |
19 | #include <linux/genhd.h> | |
20 | #include <linux/device.h> | |
21 | #include <linux/buffer_head.h> | |
22 | #include <linux/bio.h> | |
546e0d27 | 23 | #include <linux/blkdev.h> |
61159a31 RW |
24 | #include <linux/swap.h> |
25 | #include <linux/swapops.h> | |
26 | #include <linux/pm.h> | |
27 | ||
28 | #include "power.h" | |
29 | ||
61159a31 RW |
30 | #define SWSUSP_SIG "S1SUSPEND" |
31 | ||
1b29c164 | 32 | struct swsusp_header { |
a634cc10 | 33 | char reserved[PAGE_SIZE - 20 - sizeof(sector_t) - sizeof(int)]; |
3aef83e0 | 34 | sector_t image; |
a634cc10 | 35 | unsigned int flags; /* Flags to pass to the "boot" kernel */ |
61159a31 RW |
36 | char orig_sig[10]; |
37 | char sig[10]; | |
1b29c164 VG |
38 | } __attribute__((packed)); |
39 | ||
40 | static struct swsusp_header *swsusp_header; | |
61159a31 RW |
41 | |
42 | /* | |
3fc6b34f | 43 | * General things |
61159a31 RW |
44 | */ |
45 | ||
46 | static unsigned short root_swap = 0xffff; | |
3fc6b34f RW |
47 | static struct block_device *resume_bdev; |
48 | ||
49 | /** | |
50 | * submit - submit BIO request. | |
51 | * @rw: READ or WRITE. | |
52 | * @off physical offset of page. | |
53 | * @page: page we're reading or writing. | |
54 | * @bio_chain: list of pending biod (for async reading) | |
55 | * | |
56 | * Straight from the textbook - allocate and initialize the bio. | |
57 | * If we're reading, make sure the page is marked as dirty. | |
58 | * Then submit it and, if @bio_chain == NULL, wait. | |
59 | */ | |
60 | static int submit(int rw, pgoff_t page_off, struct page *page, | |
61 | struct bio **bio_chain) | |
62 | { | |
93dbb393 | 63 | const int bio_rw = rw | (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG); |
3fc6b34f RW |
64 | struct bio *bio; |
65 | ||
85949121 | 66 | bio = bio_alloc(__GFP_WAIT | __GFP_HIGH, 1); |
3fc6b34f RW |
67 | bio->bi_sector = page_off * (PAGE_SIZE >> 9); |
68 | bio->bi_bdev = resume_bdev; | |
69 | bio->bi_end_io = end_swap_bio_read; | |
70 | ||
71 | if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) { | |
23976728 RW |
72 | printk(KERN_ERR "PM: Adding page to bio failed at %ld\n", |
73 | page_off); | |
3fc6b34f RW |
74 | bio_put(bio); |
75 | return -EFAULT; | |
76 | } | |
77 | ||
78 | lock_page(page); | |
79 | bio_get(bio); | |
80 | ||
81 | if (bio_chain == NULL) { | |
93dbb393 | 82 | submit_bio(bio_rw, bio); |
3fc6b34f RW |
83 | wait_on_page_locked(page); |
84 | if (rw == READ) | |
85 | bio_set_pages_dirty(bio); | |
86 | bio_put(bio); | |
87 | } else { | |
88 | if (rw == READ) | |
89 | get_page(page); /* These pages are freed later */ | |
90 | bio->bi_private = *bio_chain; | |
91 | *bio_chain = bio; | |
93dbb393 | 92 | submit_bio(bio_rw, bio); |
3fc6b34f RW |
93 | } |
94 | return 0; | |
95 | } | |
96 | ||
97 | static int bio_read_page(pgoff_t page_off, void *addr, struct bio **bio_chain) | |
98 | { | |
99 | return submit(READ, page_off, virt_to_page(addr), bio_chain); | |
100 | } | |
101 | ||
3aef83e0 | 102 | static int bio_write_page(pgoff_t page_off, void *addr, struct bio **bio_chain) |
3fc6b34f | 103 | { |
3aef83e0 | 104 | return submit(WRITE, page_off, virt_to_page(addr), bio_chain); |
3fc6b34f RW |
105 | } |
106 | ||
107 | static int wait_on_bio_chain(struct bio **bio_chain) | |
108 | { | |
109 | struct bio *bio; | |
110 | struct bio *next_bio; | |
111 | int ret = 0; | |
112 | ||
113 | if (bio_chain == NULL) | |
114 | return 0; | |
115 | ||
116 | bio = *bio_chain; | |
117 | if (bio == NULL) | |
118 | return 0; | |
119 | while (bio) { | |
120 | struct page *page; | |
121 | ||
122 | next_bio = bio->bi_private; | |
123 | page = bio->bi_io_vec[0].bv_page; | |
124 | wait_on_page_locked(page); | |
125 | if (!PageUptodate(page) || PageError(page)) | |
126 | ret = -EIO; | |
127 | put_page(page); | |
128 | bio_put(bio); | |
129 | bio = next_bio; | |
130 | } | |
131 | *bio_chain = NULL; | |
132 | return ret; | |
133 | } | |
134 | ||
3fc6b34f RW |
135 | /* |
136 | * Saving part | |
137 | */ | |
61159a31 | 138 | |
a634cc10 | 139 | static int mark_swapfiles(sector_t start, unsigned int flags) |
61159a31 RW |
140 | { |
141 | int error; | |
142 | ||
1b29c164 VG |
143 | bio_read_page(swsusp_resume_block, swsusp_header, NULL); |
144 | if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) || | |
145 | !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) { | |
146 | memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10); | |
147 | memcpy(swsusp_header->sig,SWSUSP_SIG, 10); | |
148 | swsusp_header->image = start; | |
a634cc10 | 149 | swsusp_header->flags = flags; |
9a154d9d | 150 | error = bio_write_page(swsusp_resume_block, |
1b29c164 | 151 | swsusp_header, NULL); |
61159a31 | 152 | } else { |
23976728 | 153 | printk(KERN_ERR "PM: Swap header not found!\n"); |
61159a31 RW |
154 | error = -ENODEV; |
155 | } | |
156 | return error; | |
157 | } | |
158 | ||
159 | /** | |
160 | * swsusp_swap_check - check if the resume device is a swap device | |
161 | * and get its index (if so) | |
162 | */ | |
163 | ||
164 | static int swsusp_swap_check(void) /* This is called before saving image */ | |
165 | { | |
3aef83e0 RW |
166 | int res; |
167 | ||
7bf23687 RW |
168 | res = swap_type_of(swsusp_resume_device, swsusp_resume_block, |
169 | &resume_bdev); | |
3aef83e0 RW |
170 | if (res < 0) |
171 | return res; | |
172 | ||
173 | root_swap = res; | |
572c4892 | 174 | res = blkdev_get(resume_bdev, FMODE_WRITE); |
7bf23687 RW |
175 | if (res) |
176 | return res; | |
3aef83e0 RW |
177 | |
178 | res = set_blocksize(resume_bdev, PAGE_SIZE); | |
179 | if (res < 0) | |
9a1c3542 | 180 | blkdev_put(resume_bdev, FMODE_WRITE); |
61159a31 | 181 | |
61159a31 RW |
182 | return res; |
183 | } | |
184 | ||
185 | /** | |
186 | * write_page - Write one page to given swap location. | |
187 | * @buf: Address we're writing. | |
188 | * @offset: Offset of the swap page we're writing to. | |
ab954160 | 189 | * @bio_chain: Link the next write BIO here |
61159a31 RW |
190 | */ |
191 | ||
3aef83e0 | 192 | static int write_page(void *buf, sector_t offset, struct bio **bio_chain) |
61159a31 | 193 | { |
3aef83e0 RW |
194 | void *src; |
195 | ||
196 | if (!offset) | |
197 | return -ENOSPC; | |
198 | ||
199 | if (bio_chain) { | |
85949121 | 200 | src = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH); |
3aef83e0 RW |
201 | if (src) { |
202 | memcpy(src, buf, PAGE_SIZE); | |
203 | } else { | |
204 | WARN_ON_ONCE(1); | |
205 | bio_chain = NULL; /* Go synchronous */ | |
206 | src = buf; | |
ab954160 | 207 | } |
3aef83e0 RW |
208 | } else { |
209 | src = buf; | |
61159a31 | 210 | } |
3aef83e0 | 211 | return bio_write_page(offset, src, bio_chain); |
61159a31 RW |
212 | } |
213 | ||
214 | /* | |
215 | * The swap map is a data structure used for keeping track of each page | |
216 | * written to a swap partition. It consists of many swap_map_page | |
217 | * structures that contain each an array of MAP_PAGE_SIZE swap entries. | |
218 | * These structures are stored on the swap and linked together with the | |
219 | * help of the .next_swap member. | |
220 | * | |
221 | * The swap map is created during suspend. The swap map pages are | |
222 | * allocated and populated one at a time, so we only need one memory | |
223 | * page to set up the entire structure. | |
224 | * | |
225 | * During resume we also only need to use one swap_map_page structure | |
226 | * at a time. | |
227 | */ | |
228 | ||
3aef83e0 | 229 | #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1) |
61159a31 RW |
230 | |
231 | struct swap_map_page { | |
3aef83e0 RW |
232 | sector_t entries[MAP_PAGE_ENTRIES]; |
233 | sector_t next_swap; | |
61159a31 RW |
234 | }; |
235 | ||
236 | /** | |
237 | * The swap_map_handle structure is used for handling swap in | |
238 | * a file-alike way | |
239 | */ | |
240 | ||
241 | struct swap_map_handle { | |
242 | struct swap_map_page *cur; | |
3aef83e0 | 243 | sector_t cur_swap; |
61159a31 RW |
244 | unsigned int k; |
245 | }; | |
246 | ||
247 | static void release_swap_writer(struct swap_map_handle *handle) | |
248 | { | |
249 | if (handle->cur) | |
250 | free_page((unsigned long)handle->cur); | |
251 | handle->cur = NULL; | |
61159a31 RW |
252 | } |
253 | ||
254 | static int get_swap_writer(struct swap_map_handle *handle) | |
255 | { | |
256 | handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL); | |
257 | if (!handle->cur) | |
258 | return -ENOMEM; | |
d1d241cc | 259 | handle->cur_swap = alloc_swapdev_block(root_swap); |
61159a31 RW |
260 | if (!handle->cur_swap) { |
261 | release_swap_writer(handle); | |
262 | return -ENOSPC; | |
263 | } | |
264 | handle->k = 0; | |
265 | return 0; | |
266 | } | |
267 | ||
ab954160 AM |
268 | static int swap_write_page(struct swap_map_handle *handle, void *buf, |
269 | struct bio **bio_chain) | |
270 | { | |
271 | int error = 0; | |
3aef83e0 | 272 | sector_t offset; |
61159a31 RW |
273 | |
274 | if (!handle->cur) | |
275 | return -EINVAL; | |
d1d241cc | 276 | offset = alloc_swapdev_block(root_swap); |
ab954160 | 277 | error = write_page(buf, offset, bio_chain); |
61159a31 RW |
278 | if (error) |
279 | return error; | |
280 | handle->cur->entries[handle->k++] = offset; | |
281 | if (handle->k >= MAP_PAGE_ENTRIES) { | |
ab954160 AM |
282 | error = wait_on_bio_chain(bio_chain); |
283 | if (error) | |
284 | goto out; | |
d1d241cc | 285 | offset = alloc_swapdev_block(root_swap); |
61159a31 RW |
286 | if (!offset) |
287 | return -ENOSPC; | |
288 | handle->cur->next_swap = offset; | |
ab954160 | 289 | error = write_page(handle->cur, handle->cur_swap, NULL); |
61159a31 | 290 | if (error) |
ab954160 | 291 | goto out; |
61159a31 RW |
292 | memset(handle->cur, 0, PAGE_SIZE); |
293 | handle->cur_swap = offset; | |
294 | handle->k = 0; | |
295 | } | |
59a49335 | 296 | out: |
ab954160 | 297 | return error; |
61159a31 RW |
298 | } |
299 | ||
300 | static int flush_swap_writer(struct swap_map_handle *handle) | |
301 | { | |
302 | if (handle->cur && handle->cur_swap) | |
ab954160 | 303 | return write_page(handle->cur, handle->cur_swap, NULL); |
61159a31 RW |
304 | else |
305 | return -EINVAL; | |
306 | } | |
307 | ||
308 | /** | |
309 | * save_image - save the suspend image data | |
310 | */ | |
311 | ||
312 | static int save_image(struct swap_map_handle *handle, | |
313 | struct snapshot_handle *snapshot, | |
3a4f7577 | 314 | unsigned int nr_to_write) |
61159a31 RW |
315 | { |
316 | unsigned int m; | |
317 | int ret; | |
318 | int error = 0; | |
3a4f7577 | 319 | int nr_pages; |
ab954160 AM |
320 | int err2; |
321 | struct bio *bio; | |
3a4f7577 AM |
322 | struct timeval start; |
323 | struct timeval stop; | |
61159a31 | 324 | |
23976728 RW |
325 | printk(KERN_INFO "PM: Saving image data pages (%u pages) ... ", |
326 | nr_to_write); | |
3a4f7577 | 327 | m = nr_to_write / 100; |
61159a31 RW |
328 | if (!m) |
329 | m = 1; | |
330 | nr_pages = 0; | |
ab954160 | 331 | bio = NULL; |
3a4f7577 | 332 | do_gettimeofday(&start); |
61159a31 RW |
333 | do { |
334 | ret = snapshot_read_next(snapshot, PAGE_SIZE); | |
335 | if (ret > 0) { | |
ab954160 AM |
336 | error = swap_write_page(handle, data_of(*snapshot), |
337 | &bio); | |
61159a31 RW |
338 | if (error) |
339 | break; | |
340 | if (!(nr_pages % m)) | |
341 | printk("\b\b\b\b%3d%%", nr_pages / m); | |
342 | nr_pages++; | |
343 | } | |
344 | } while (ret > 0); | |
ab954160 | 345 | err2 = wait_on_bio_chain(&bio); |
3a4f7577 | 346 | do_gettimeofday(&stop); |
ab954160 AM |
347 | if (!error) |
348 | error = err2; | |
61159a31 RW |
349 | if (!error) |
350 | printk("\b\b\b\bdone\n"); | |
0d3a9abe | 351 | swsusp_show_speed(&start, &stop, nr_to_write, "Wrote"); |
61159a31 RW |
352 | return error; |
353 | } | |
354 | ||
355 | /** | |
356 | * enough_swap - Make sure we have enough swap to save the image. | |
357 | * | |
358 | * Returns TRUE or FALSE after checking the total amount of swap | |
359 | * space avaiable from the resume partition. | |
360 | */ | |
361 | ||
362 | static int enough_swap(unsigned int nr_pages) | |
363 | { | |
364 | unsigned int free_swap = count_swap_pages(root_swap, 1); | |
365 | ||
23976728 | 366 | pr_debug("PM: Free swap pages: %u\n", free_swap); |
940864dd | 367 | return free_swap > nr_pages + PAGES_FOR_IO; |
61159a31 RW |
368 | } |
369 | ||
370 | /** | |
371 | * swsusp_write - Write entire image and metadata. | |
a634cc10 | 372 | * @flags: flags to pass to the "boot" kernel in the image header |
61159a31 RW |
373 | * |
374 | * It is important _NOT_ to umount filesystems at this point. We want | |
375 | * them synced (in case something goes wrong) but we DO not want to mark | |
376 | * filesystem clean: it is not. (And it does not matter, if we resume | |
377 | * correctly, we'll mark system clean, anyway.) | |
378 | */ | |
379 | ||
a634cc10 | 380 | int swsusp_write(unsigned int flags) |
61159a31 RW |
381 | { |
382 | struct swap_map_handle handle; | |
383 | struct snapshot_handle snapshot; | |
384 | struct swsusp_info *header; | |
61159a31 RW |
385 | int error; |
386 | ||
3aef83e0 RW |
387 | error = swsusp_swap_check(); |
388 | if (error) { | |
23976728 | 389 | printk(KERN_ERR "PM: Cannot find swap device, try " |
546e0d27 | 390 | "swapon -a.\n"); |
61159a31 RW |
391 | return error; |
392 | } | |
393 | memset(&snapshot, 0, sizeof(struct snapshot_handle)); | |
394 | error = snapshot_read_next(&snapshot, PAGE_SIZE); | |
3aef83e0 RW |
395 | if (error < PAGE_SIZE) { |
396 | if (error >= 0) | |
397 | error = -EFAULT; | |
398 | ||
399 | goto out; | |
400 | } | |
61159a31 RW |
401 | header = (struct swsusp_info *)data_of(snapshot); |
402 | if (!enough_swap(header->pages)) { | |
23976728 | 403 | printk(KERN_ERR "PM: Not enough free swap\n"); |
3aef83e0 RW |
404 | error = -ENOSPC; |
405 | goto out; | |
61159a31 RW |
406 | } |
407 | error = get_swap_writer(&handle); | |
408 | if (!error) { | |
3aef83e0 RW |
409 | sector_t start = handle.cur_swap; |
410 | ||
ab954160 | 411 | error = swap_write_page(&handle, header, NULL); |
712f403a AM |
412 | if (!error) |
413 | error = save_image(&handle, &snapshot, | |
414 | header->pages - 1); | |
3aef83e0 | 415 | |
712f403a AM |
416 | if (!error) { |
417 | flush_swap_writer(&handle); | |
23976728 | 418 | printk(KERN_INFO "PM: S"); |
a634cc10 | 419 | error = mark_swapfiles(start, flags); |
712f403a AM |
420 | printk("|\n"); |
421 | } | |
61159a31 RW |
422 | } |
423 | if (error) | |
d1d241cc RW |
424 | free_all_swap_pages(root_swap); |
425 | ||
61159a31 | 426 | release_swap_writer(&handle); |
59a49335 | 427 | out: |
c2dd0dae | 428 | swsusp_close(FMODE_WRITE); |
61159a31 RW |
429 | return error; |
430 | } | |
431 | ||
61159a31 RW |
432 | /** |
433 | * The following functions allow us to read data using a swap map | |
434 | * in a file-alike way | |
435 | */ | |
436 | ||
437 | static void release_swap_reader(struct swap_map_handle *handle) | |
438 | { | |
439 | if (handle->cur) | |
440 | free_page((unsigned long)handle->cur); | |
441 | handle->cur = NULL; | |
442 | } | |
443 | ||
3aef83e0 | 444 | static int get_swap_reader(struct swap_map_handle *handle, sector_t start) |
61159a31 RW |
445 | { |
446 | int error; | |
447 | ||
3aef83e0 | 448 | if (!start) |
61159a31 | 449 | return -EINVAL; |
3aef83e0 | 450 | |
85949121 | 451 | handle->cur = (struct swap_map_page *)get_zeroed_page(__GFP_WAIT | __GFP_HIGH); |
61159a31 RW |
452 | if (!handle->cur) |
453 | return -ENOMEM; | |
3aef83e0 RW |
454 | |
455 | error = bio_read_page(start, handle->cur, NULL); | |
61159a31 RW |
456 | if (error) { |
457 | release_swap_reader(handle); | |
458 | return error; | |
459 | } | |
460 | handle->k = 0; | |
461 | return 0; | |
462 | } | |
463 | ||
546e0d27 AM |
464 | static int swap_read_page(struct swap_map_handle *handle, void *buf, |
465 | struct bio **bio_chain) | |
61159a31 | 466 | { |
3aef83e0 | 467 | sector_t offset; |
61159a31 RW |
468 | int error; |
469 | ||
470 | if (!handle->cur) | |
471 | return -EINVAL; | |
472 | offset = handle->cur->entries[handle->k]; | |
473 | if (!offset) | |
474 | return -EFAULT; | |
546e0d27 | 475 | error = bio_read_page(offset, buf, bio_chain); |
61159a31 RW |
476 | if (error) |
477 | return error; | |
478 | if (++handle->k >= MAP_PAGE_ENTRIES) { | |
546e0d27 | 479 | error = wait_on_bio_chain(bio_chain); |
61159a31 RW |
480 | handle->k = 0; |
481 | offset = handle->cur->next_swap; | |
482 | if (!offset) | |
483 | release_swap_reader(handle); | |
546e0d27 AM |
484 | else if (!error) |
485 | error = bio_read_page(offset, handle->cur, NULL); | |
61159a31 RW |
486 | } |
487 | return error; | |
488 | } | |
489 | ||
490 | /** | |
491 | * load_image - load the image using the swap map handle | |
492 | * @handle and the snapshot handle @snapshot | |
493 | * (assume there are @nr_pages pages to load) | |
494 | */ | |
495 | ||
496 | static int load_image(struct swap_map_handle *handle, | |
497 | struct snapshot_handle *snapshot, | |
546e0d27 | 498 | unsigned int nr_to_read) |
61159a31 RW |
499 | { |
500 | unsigned int m; | |
61159a31 | 501 | int error = 0; |
8c002494 AM |
502 | struct timeval start; |
503 | struct timeval stop; | |
546e0d27 AM |
504 | struct bio *bio; |
505 | int err2; | |
506 | unsigned nr_pages; | |
61159a31 | 507 | |
23976728 RW |
508 | printk(KERN_INFO "PM: Loading image data pages (%u pages) ... ", |
509 | nr_to_read); | |
546e0d27 | 510 | m = nr_to_read / 100; |
61159a31 RW |
511 | if (!m) |
512 | m = 1; | |
513 | nr_pages = 0; | |
546e0d27 | 514 | bio = NULL; |
8c002494 | 515 | do_gettimeofday(&start); |
546e0d27 AM |
516 | for ( ; ; ) { |
517 | error = snapshot_write_next(snapshot, PAGE_SIZE); | |
518 | if (error <= 0) | |
519 | break; | |
520 | error = swap_read_page(handle, data_of(*snapshot), &bio); | |
521 | if (error) | |
522 | break; | |
523 | if (snapshot->sync_read) | |
524 | error = wait_on_bio_chain(&bio); | |
525 | if (error) | |
526 | break; | |
527 | if (!(nr_pages % m)) | |
528 | printk("\b\b\b\b%3d%%", nr_pages / m); | |
529 | nr_pages++; | |
530 | } | |
531 | err2 = wait_on_bio_chain(&bio); | |
8c002494 | 532 | do_gettimeofday(&stop); |
546e0d27 AM |
533 | if (!error) |
534 | error = err2; | |
e655a250 | 535 | if (!error) { |
61159a31 | 536 | printk("\b\b\b\bdone\n"); |
8357376d | 537 | snapshot_write_finalize(snapshot); |
e655a250 CK |
538 | if (!snapshot_image_loaded(snapshot)) |
539 | error = -ENODATA; | |
540 | } | |
0d3a9abe | 541 | swsusp_show_speed(&start, &stop, nr_to_read, "Read"); |
61159a31 RW |
542 | return error; |
543 | } | |
544 | ||
a634cc10 RW |
545 | /** |
546 | * swsusp_read - read the hibernation image. | |
547 | * @flags_p: flags passed by the "frozen" kernel in the image header should | |
548 | * be written into this memeory location | |
549 | */ | |
550 | ||
551 | int swsusp_read(unsigned int *flags_p) | |
61159a31 RW |
552 | { |
553 | int error; | |
554 | struct swap_map_handle handle; | |
555 | struct snapshot_handle snapshot; | |
556 | struct swsusp_info *header; | |
557 | ||
a634cc10 | 558 | *flags_p = swsusp_header->flags; |
61159a31 | 559 | if (IS_ERR(resume_bdev)) { |
23976728 | 560 | pr_debug("PM: Image device not initialised\n"); |
61159a31 RW |
561 | return PTR_ERR(resume_bdev); |
562 | } | |
563 | ||
564 | memset(&snapshot, 0, sizeof(struct snapshot_handle)); | |
565 | error = snapshot_write_next(&snapshot, PAGE_SIZE); | |
566 | if (error < PAGE_SIZE) | |
567 | return error < 0 ? error : -EFAULT; | |
568 | header = (struct swsusp_info *)data_of(snapshot); | |
1b29c164 | 569 | error = get_swap_reader(&handle, swsusp_header->image); |
61159a31 | 570 | if (!error) |
546e0d27 | 571 | error = swap_read_page(&handle, header, NULL); |
61159a31 RW |
572 | if (!error) |
573 | error = load_image(&handle, &snapshot, header->pages - 1); | |
574 | release_swap_reader(&handle); | |
575 | ||
9a1c3542 | 576 | blkdev_put(resume_bdev, FMODE_READ); |
61159a31 RW |
577 | |
578 | if (!error) | |
23976728 | 579 | pr_debug("PM: Image successfully loaded\n"); |
61159a31 | 580 | else |
23976728 | 581 | pr_debug("PM: Error %d resuming\n", error); |
61159a31 RW |
582 | return error; |
583 | } | |
584 | ||
585 | /** | |
586 | * swsusp_check - Check for swsusp signature in the resume device | |
587 | */ | |
588 | ||
589 | int swsusp_check(void) | |
590 | { | |
591 | int error; | |
592 | ||
593 | resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ); | |
594 | if (!IS_ERR(resume_bdev)) { | |
595 | set_blocksize(resume_bdev, PAGE_SIZE); | |
6373da1f | 596 | memset(swsusp_header, 0, PAGE_SIZE); |
9a154d9d | 597 | error = bio_read_page(swsusp_resume_block, |
1b29c164 | 598 | swsusp_header, NULL); |
9a154d9d | 599 | if (error) |
61159a31 | 600 | return error; |
9a154d9d | 601 | |
1b29c164 VG |
602 | if (!memcmp(SWSUSP_SIG, swsusp_header->sig, 10)) { |
603 | memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10); | |
61159a31 | 604 | /* Reset swap signature now */ |
9a154d9d | 605 | error = bio_write_page(swsusp_resume_block, |
1b29c164 | 606 | swsusp_header, NULL); |
61159a31 RW |
607 | } else { |
608 | return -EINVAL; | |
609 | } | |
610 | if (error) | |
9a1c3542 | 611 | blkdev_put(resume_bdev, FMODE_READ); |
61159a31 | 612 | else |
23976728 | 613 | pr_debug("PM: Signature found, resuming\n"); |
61159a31 RW |
614 | } else { |
615 | error = PTR_ERR(resume_bdev); | |
616 | } | |
617 | ||
618 | if (error) | |
23976728 | 619 | pr_debug("PM: Error %d checking image file\n", error); |
61159a31 RW |
620 | |
621 | return error; | |
622 | } | |
623 | ||
624 | /** | |
625 | * swsusp_close - close swap device. | |
626 | */ | |
627 | ||
c2dd0dae | 628 | void swsusp_close(fmode_t mode) |
61159a31 RW |
629 | { |
630 | if (IS_ERR(resume_bdev)) { | |
23976728 | 631 | pr_debug("PM: Image device not initialised\n"); |
61159a31 RW |
632 | return; |
633 | } | |
634 | ||
50c396d3 | 635 | blkdev_put(resume_bdev, mode); |
61159a31 | 636 | } |
1b29c164 VG |
637 | |
638 | static int swsusp_header_init(void) | |
639 | { | |
640 | swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL); | |
641 | if (!swsusp_header) | |
642 | panic("Could not allocate memory for swsusp_header\n"); | |
643 | return 0; | |
644 | } | |
645 | ||
646 | core_initcall(swsusp_header_init); |