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