static suspend_pagedir_t *create_suspend_pagedir(int nr_copy_pages) { int i; suspend_pagedir_t *pagedir; struct pbe *p; struct page *page; pagedir_order = get_bitmask_order(SUSPEND_PD_PAGES(nr_copy_pages)); p = pagedir = (suspend_pagedir_t *)__get_free_pages(GFP_ATOMIC | __GFP_COLD, pagedir_order); if(!pagedir) return NULL; page = virt_to_page(pagedir); for(i=0; i < 1<<pagedir_order; i++) SetPageNosave(page++); while(nr_copy_pages--) { p->address = get_zeroed_page(GFP_ATOMIC | __GFP_COLD); if(!p->address) { free_suspend_pagedir((unsigned long) pagedir); return NULL; } SetPageNosave(virt_to_page(p->address)); p->orig_address = 0; p++; } return pagedir; }
static void calc_order(void) { int diff; int order; order = get_bitmask_order(SUSPEND_PD_PAGES(pmdisk_pages)); pmdisk_pages += 1 << order; do { diff = get_bitmask_order(SUSPEND_PD_PAGES(pmdisk_pages)) - order; if (diff) { order += diff; pmdisk_pages += 1 << diff; } } while(diff); pagedir_order = order; }
static int write_pagedir(void) { unsigned long addr = (unsigned long)pm_pagedir_nosave; int error = 0; int n = SUSPEND_PD_PAGES(pmdisk_pages); int i; pmdisk_info.pagedir_pages = n; printk( "Writing pagedir (%d pages)\n", n); for (i = 0; i < n && !error; i++, addr += PAGE_SIZE) error = write_swap_page(addr,&pmdisk_info.pagedir[i]); return error; }
static void calc_order(void) { int diff = 0; int order = 0; do { diff = get_bitmask_order(SUSPEND_PD_PAGES(nr_copy_pages)) - order; if (diff) { order += diff; nr_copy_pages += 1 << diff; } } while(diff); pagedir_order = order; }
static int __init check_header(void) { const char * reason = NULL; int error; if ((error = bio_read_page(swp_offset(swsusp_header.swsusp_info), &swsusp_info))) return error; /* Is this same machine? */ if ((reason = sanity_check())) { printk(KERN_ERR "swsusp: Resume mismatch: %s\n",reason); return -EPERM; } nr_copy_pages = swsusp_info.image_pages; pagedir_order = get_bitmask_order(SUSPEND_PD_PAGES(nr_copy_pages)); return error; }
static int __read_suspend_image(struct block_device *bdev, union diskpage *cur, int noresume) { swp_entry_t next; int i, nr_pgdir_pages; #define PREPARENEXT \ { next = cur->link.next; \ next.val = swp_offset(next) * PAGE_SIZE; \ } if (bdev_read_page(bdev, 0, cur)) return -EIO; if ((!memcmp("SWAP-SPACE",cur->swh.magic.magic,10)) || (!memcmp("SWAPSPACE2",cur->swh.magic.magic,10))) { printk(KERN_ERR "%sThis is normal swap space\n", name_resume ); return -EINVAL; } PREPARENEXT; /* We have to read next position before we overwrite it */ if (!memcmp("S1",cur->swh.magic.magic,2)) memcpy(cur->swh.magic.magic,"SWAP-SPACE",10); else if (!memcmp("S2",cur->swh.magic.magic,2)) memcpy(cur->swh.magic.magic,"SWAPSPACE2",10); else { if (noresume) return -EINVAL; panic("%sUnable to find suspended-data signature (%.10s - misspelled?\n", name_resume, cur->swh.magic.magic); } if (noresume) { /* We don't do a sanity check here: we want to restore the swap whatever version of kernel made the suspend image; We need to write swap, but swap is *not* enabled so we must write the device directly */ printk("%s: Fixing swap signatures %s...\n", name_resume, resume_file); bdev_write_page(bdev, 0, cur); } printk( "%sSignature found, resuming\n", name_resume ); MDELAY(1000); if (bdev_read_page(bdev, next.val, cur)) return -EIO; if (sanity_check(&cur->sh)) /* Is this same machine? */ return -EPERM; PREPARENEXT; pagedir_save = cur->sh.suspend_pagedir; nr_copy_pages = cur->sh.num_pbes; nr_pgdir_pages = SUSPEND_PD_PAGES(nr_copy_pages); pagedir_order = get_bitmask_order(nr_pgdir_pages); pagedir_nosave = (suspend_pagedir_t *)__get_free_pages(GFP_ATOMIC, pagedir_order); if (!pagedir_nosave) return -ENOMEM; PRINTK( "%sReading pagedir, ", name_resume ); /* We get pages in reverse order of saving! */ for (i=nr_pgdir_pages-1; i>=0; i--) { BUG_ON (!next.val); cur = (union diskpage *)((char *) pagedir_nosave)+i; if (bdev_read_page(bdev, next.val, cur)) return -EIO; PREPARENEXT; } BUG_ON (next.val); if (relocate_pagedir()) return -ENOMEM; if (check_pagedir()) return -ENOMEM; printk( "Reading image data (%d pages): ", nr_copy_pages ); for(i=0; i < nr_copy_pages; i++) { swp_entry_t swap_address = (pagedir_nosave+i)->swap_address; if (!(i%100)) printk( "." ); /* You do not need to check for overlaps... ... check_pagedir already did this work */ if (bdev_read_page(bdev, swp_offset(swap_address) * PAGE_SIZE, (char *)((pagedir_nosave+i)->address))) return -EIO; } printk( "|\n" ); return 0; }
/** * write_suspend_image - Write entire image to disk. * * After writing suspend signature to the disk, suspend may no * longer fail: we have ready-to-run image in swap, and rollback * would happen on next reboot -- corrupting data. * * Note: The buffer we allocate to use to write the suspend header is * not freed; its not needed since system is going down anyway * (plus it causes oops and I'm lazy^H^H^H^Htoo busy). */ static int write_suspend_image(void) { int i; swp_entry_t entry, prev = { 0 }; int nr_pgdir_pages = SUSPEND_PD_PAGES(nr_copy_pages); union diskpage *cur, *buffer = (union diskpage *)get_zeroed_page(GFP_ATOMIC); unsigned long address; struct page *page; if (!buffer) return -ENOMEM; printk( "Writing data to swap (%d pages): ", nr_copy_pages ); for (i=0; i<nr_copy_pages; i++) { if (!(i%100)) printk( "." ); if (!(entry = get_swap_page()).val) panic("\nNot enough swapspace when writing data" ); if (swapfile_used[swp_type(entry)] != SWAPFILE_SUSPEND) panic("\nPage %d: not enough swapspace on suspend device", i ); address = (pagedir_nosave+i)->address; page = virt_to_page(address); rw_swap_page_sync(WRITE, entry, page); (pagedir_nosave+i)->swap_address = entry; } printk( "|\n" ); printk( "Writing pagedir (%d pages): ", nr_pgdir_pages); for (i=0; i<nr_pgdir_pages; i++) { cur = (union diskpage *)((char *) pagedir_nosave)+i; BUG_ON ((char *) cur != (((char *) pagedir_nosave) + i*PAGE_SIZE)); printk( "." ); if (!(entry = get_swap_page()).val) { printk(KERN_CRIT "Not enough swapspace when writing pgdir\n" ); panic("Don't know how to recover"); free_page((unsigned long) buffer); return -ENOSPC; } if(swapfile_used[swp_type(entry)] != SWAPFILE_SUSPEND) panic("\nNot enough swapspace for pagedir on suspend device" ); BUG_ON (sizeof(swp_entry_t) != sizeof(long)); BUG_ON (PAGE_SIZE % sizeof(struct pbe)); cur->link.next = prev; page = virt_to_page((unsigned long)cur); rw_swap_page_sync(WRITE, entry, page); prev = entry; } printk("H"); BUG_ON (sizeof(struct suspend_header) > PAGE_SIZE-sizeof(swp_entry_t)); BUG_ON (sizeof(union diskpage) != PAGE_SIZE); if (!(entry = get_swap_page()).val) panic( "\nNot enough swapspace when writing header" ); if (swapfile_used[swp_type(entry)] != SWAPFILE_SUSPEND) panic("\nNot enough swapspace for header on suspend device" ); cur = (void *) buffer; if (fill_suspend_header(&cur->sh)) panic("\nOut of memory while writing header"); cur->link.next = prev; page = virt_to_page((unsigned long)cur); rw_swap_page_sync(WRITE, entry, page); prev = entry; printk( "S" ); mark_swapfiles(prev, MARK_SWAP_SUSPEND); printk( "|\n" ); MDELAY(1000); return 0; }