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