Skip to contentSkip to conversation

Filewriter

PATCH New Nigel Cunningham 1 participant 36 messages Jun 26, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Filewriter

Add the Suspend2 filewriter, which implements support for writing an image to ordinary files, whether on a filesystem or a raw device.

#1 lore raw

PATCH 1/35 Header file for suspend_file.c

Header for the filewriter, which allows us to write an image to a simple
file: a partition, a file on a filesystem or possibly (untested) things like /dev/ttyS0.

 kernel/power/suspend_file.c |   96 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 96 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c new file +96 -0
new file mode 100644
index 0000000..525e482
@@ -0,0 +1,96 @@
+/*
+ * kernel/power/suspend_file.c
+ *
+ * Copyright 2005-2006 Nigel Cunningham <[email protected]>
+ *
+ * Distributed under GPLv2.
+ * 
+ * This file encapsulates functions for usage of a simple file as a
+ * backing store. It is based upon the swapwriter, and shares the
+ * same basic working. Here, though, we have nothing to do with
+ * swapspace, and only one device to worry about.
+ *
+ * The user can just
+ *
+ * echo Suspend2 > /path/to/my_file
+ *
+ * and
+ *
+ * echo /path/to/my_file > /proc/software_suspend/filewriter_target
+ *
+ * then put what they find in /proc/software_suspend/resume2
+ * as their resume2= parameter in lilo.conf (and rerun lilo if using it).
+ *
+ * Having done this, they're ready to suspend and resume.
+ *
+ * TODO:
+ * - File resizing.
+ */
+
+#include <linux/suspend.h>
+#include <linux/module.h>
+#include <linux/blkdev.h>
+#include <linux/file.h>
+#include <linux/stat.h>
+#include <linux/mount.h>
+#include <linux/statfs.h>
+#include <linux/syscalls.h>
+#include <linux/namei.h>
+#include <linux/fs.h>
+
+#include "suspend2.h"
+#include "suspend2_common.h"
+#include "version.h"
+#include "proc.h"
+#include "modules.h"
+#include "ui.h"
+#include "extent.h"
+#include "io.h"
+#include "storage.h"
+#include "block_io.h"
+
+static struct suspend_module_ops filewriterops;
+
+/* Details of our target.  */
+
+char filewriter_target[256];
+static struct inode *target_inode;
+static struct file *target_file;
+static struct block_device *filewriter_target_bdev;
+static dev_t resume_dev_t;
+static int used_devt = 0;
+static int setting_filewriter_target = 0;
+static sector_t target_firstblock = 0, target_header_start = 0;
+static int target_storage_available = 0;
+static int target_claim = 0;
+
+static char HaveImage[] = "HaveImage\n";
+static char NoImage[] =   "Suspend2\n";
+#define sig_size (sizeof(HaveImage) + 1)
+
+struct filewriter_header {
+	char sig[sig_size];
+	int resumed_before;
+	unsigned long first_header_block;
+};
+
+extern dev_t ROOT_DEV;
+extern char *__initdata root_device_name;
+
+/* Header_pages must be big enough for signature */
+static int header_pages, main_pages;
+
+#define target_is_normal_file() (S_ISREG(target_inode->i_mode))
+
+static struct suspend_bdev_info devinfo;
+
+/* Extent chain for blocks */
+static struct extent_chain block_chain;
+
+/* Signature operations */
+enum {
+	GET_IMAGE_EXISTS,
+	INVALIDATE,
+	MARK_RESUME_ATTEMPTED,
+};
+

--
Nigel Cunningham nigel at suspend2 dot net

#2 lore raw

PATCH 2/35 Filewriter set_devinfo patch.

In reply to Nigel Cunningham

Tell the block io routines which device and blocks to use.

 kernel/power/suspend_file.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +11 -0
index 525e482..36db6b8 100644
@@ -94,3 +94,14 @@ enum {
 	MARK_RESUME_ATTEMPTED,
 };
 
+static void set_devinfo(struct block_device *bdev, int target_blkbits)
+{
+	devinfo.bdev = bdev;
+	if (!target_blkbits) {
+		devinfo.bmap_shift = devinfo.blocks_per_page = 0;
+	} else {
+		devinfo.bmap_shift = target_blkbits - 9;
+		devinfo.blocks_per_page = (1 << (PAGE_SHIFT - target_blkbits));
+	}
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#3 lore raw

PATCH 3/35 Get filewriter storage available.

In reply to Nigel Cunningham

Return the amount of storage available to the filewriter, in pages.

 kernel/power/suspend_file.c |   31 +++++++++++++++++++++++++++++++
 1 files changed, 31 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +31 -0
index 36db6b8..caa86bc 100644
@@ -105,3 +105,34 @@ static void set_devinfo(struct block_dev
 	}
 }
 
+static int filewriter_storage_available(void)
+{
+	int result = 0;
+	struct block_device *bdev=filewriter_target_bdev;
+
+	if (!target_inode)
+		return 0;
+
+	switch (target_inode->i_mode & S_IFMT) {
+		case S_IFSOCK:
+		case S_IFCHR:
+		case S_IFIFO: /* Socket, Char, Fifo */
+			return -1;
+		case S_IFREG: /* Regular file: current size - holes + free
+				 space on part */
+			result = target_storage_available;
+			break;
+		case S_IFBLK: /* Block device */
+			if (!bdev->bd_disk) {
+				printk("bdev->bd_disk null.\n");
+				return 0;
+			}
+
+			result = (bdev->bd_part ?
+				bdev->bd_part->nr_sects :
+				bdev->bd_disk->capacity) >> (PAGE_SHIFT - 9);
+	}
+
+	return result;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#4 lore raw

PATCH 4/35 Does a filewriter page have contiguous blocks?

In reply to Nigel Cunningham

To simplify things, pages in the file being used by the filewriter which don't have contiguous blocks are ignored. This function determines whether a given page meets this criteria by bmapping each sector in the page to check.

 kernel/power/suspend_file.c |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +18 -0
index caa86bc..41aaed4 100644
@@ -136,3 +136,21 @@ static int filewriter_storage_available(
 	return result;
 }
 
+static int has_contiguous_blocks(int page_num)
+{
+	int j;
+	sector_t last = 0;
+
+	for (j = 0; j < devinfo.blocks_per_page; j++) {
+		sector_t this = bmap(target_inode,
+				page_num * devinfo.blocks_per_page + j);
+
+		if (!this || (last && (last + 1) != this))
+			break;
+
+		last = this;
+	}
+			
+	return (j == devinfo.blocks_per_page);
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#5 lore raw

PATCH 5/35 Filewriter storage available less ignored pages.

In reply to Nigel Cunningham

Return the number of pages the filewriter will actually use in the provided storage.

 kernel/power/suspend_file.c |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +14 -0
index 41aaed4..9046b80 100644
@@ -154,3 +154,17 @@ static int has_contiguous_blocks(int pag
 	return (j == devinfo.blocks_per_page);
 }
 
+static int size_ignoring_ignored_pages(void)
+{
+	int mappable = 0, i;
+	
+	if (target_is_normal_file()) {
+		for (i = 0; i < (target_inode->i_size >> PAGE_SHIFT) ; i++)
+			if (has_contiguous_blocks(i))
+				mappable++;
+	
+		return mappable;
+	} else
+		return filewriter_storage_available();
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#6 lore raw

PATCH 6/35 Filewriter populate block list.

In reply to Nigel Cunningham

Populate the extent chain that contains the list of sectors we'll use for writing the image.

 kernel/power/suspend_file.c |   67 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 67 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +67 -0
index 9046b80..97f97be 100644
@@ -168,3 +168,70 @@ static int size_ignoring_ignored_pages(v
 		return filewriter_storage_available();
 }
 
+static void __populate_block_list(int min, int max)
+{
+	if (test_action_state(SUSPEND_TEST_BIO))
+		printk("Adding extent %d-%d.\n", min << devinfo.bmap_shift,
+		        ((max + 1) << devinfo.bmap_shift) - 1);
+
+	suspend_add_to_extent_chain(&block_chain, min, max);
+}
+
+static void populate_block_list(void)
+{
+	int i;
+	
+	if (block_chain.first)
+		suspend_put_extent_chain(&block_chain);
+
+	if (target_is_normal_file()) {
+		int extent_min = -1, extent_max = -1, got_header = 0;
+
+		for (i = 0;
+		     i < (target_inode->i_size >> PAGE_SHIFT);
+		     i++) {
+			sector_t new_sector;
+
+			if (!has_contiguous_blocks(i))
+				continue;
+
+			new_sector = bmap(target_inode,
+				(i * devinfo.blocks_per_page));
+
+			/* 
+			 * Ignore the first block in the file.
+			 * It gets the header.
+			 */
+			if (new_sector == target_firstblock >> devinfo.bmap_shift) {
+				got_header = 1;
+				continue;
+			}
+
+			/* 
+			 * I'd love to be able to fill in holes and resize 
+			 * files, but not yet...
+			 */
+
+			if (new_sector == extent_max + 1)
+				extent_max+= devinfo.blocks_per_page;
+			else {
+				if (extent_min > -1)
+					__populate_block_list(extent_min,
+							extent_max);
+
+				extent_min = new_sector;
+				extent_max = extent_min +
+					devinfo.blocks_per_page - 1;
+			}
+		}
+		if (extent_min > -1)
+			__populate_block_list(extent_min, extent_max);
+
+		BUG_ON(!got_header);
+	} else
+		if (target_storage_available > 0)
+			__populate_block_list(devinfo.blocks_per_page, 
+				(min(main_pages, target_storage_available) + 1) *
+			 	devinfo.blocks_per_page - 1);
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#7 lore raw

PATCH 7/35 Filewriter cleanup.

In reply to Nigel Cunningham

Cleanup when returning control to userspace.

 kernel/power/suspend_file.c |   24 ++++++++++++++++++++++++
 1 files changed, 24 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +24 -0
index 97f97be..e2ad8dc 100644
@@ -235,3 +235,27 @@ static void populate_block_list(void)
 			 	devinfo.blocks_per_page - 1);
 }
 
+static void filewriter_cleanup(int finishing_cycle)
+{
+	if (filewriter_target_bdev) {
+		if (target_claim) {
+			bd_release(filewriter_target_bdev);
+			target_claim = 0;
+		}
+
+		if (used_devt) {
+			blkdev_put(filewriter_target_bdev);
+			used_devt = 0;
+		}
+		filewriter_target_bdev = NULL;
+		target_inode = NULL;
+		set_devinfo(NULL, 0);
+		target_storage_available = 0;
+	}
+
+	if (target_file > 0) {
+		filp_close(target_file, NULL);
+		target_file = NULL;
+	}
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#8 lore raw

PATCH 8/35 Reopen the resume dev_t.

In reply to Nigel Cunningham

Having processed the resume2= setting once to get the dev_t it refers to, we can just use the dev_t again afterwards. This routine reopens the bdev at some later stage, getting afresh the inode and device info - something might have been modified by the user since our last use, and it would be wrong to keep the device open all the time anyway.

 kernel/power/suspend_file.c |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +19 -0
index e2ad8dc..7e06a06 100644
@@ -259,3 +259,22 @@ static void filewriter_cleanup(int finis
 	}
 }
 
+/* 
+ * reopen_resume_devt
+ *
+ * Having opened resume2= once, we remember the major and
+ * minor nodes and use them to reopen the bdev for checking
+ * whether an image exists (possibly when starting a resume).
+ */
+static void reopen_resume_devt(void)
+{
+	filewriter_target_bdev = open_by_devnum(resume_dev_t, FMODE_READ);
+	if (IS_ERR(filewriter_target_bdev)) {
+		printk("Got a dev_num (%lx) but failed to open it.\n",
+				(unsigned long) resume_dev_t);
+		return;
+	}
+	target_inode = filewriter_target_bdev->bd_inode;
+	set_devinfo(filewriter_target_bdev, target_inode->i_blkbits);
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#9 lore raw

PATCH 9/35 Get filewriter target info.

In reply to Nigel Cunningham

Given the name of a target (eg /dev/hda3, /my-suspend-file, /dev/ttyS0), attempt to access it and discover the properties (inode, block size, dev_t, first block, storage available) we need to work with it.

 kernel/power/suspend_file.c |   87 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 87 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +87 -0
index 7e06a06..6851977 100644
@@ -278,3 +278,90 @@ static void reopen_resume_devt(void)
 	set_devinfo(filewriter_target_bdev, target_inode->i_blkbits);
 }
 
+static void filewriter_get_target_info(char *target, int get_size,
+		int resume2)
+{
+	if (target_file)
+		filewriter_cleanup(0);
+
+	if (!target || !strlen(target))
+		return;
+
+	target_file = filp_open(target, O_RDWR, 0);
+
+	if (IS_ERR(target_file) || !target_file) {
+
+		if (!resume2) {
+			printk("Open file %s returned %p.\n",
+					target, target_file);
+			target_file = NULL;
+			return;
+		}
+
+		target_file = NULL;
+		resume_dev_t = name_to_dev_t(target);
+		if (!resume_dev_t) {
+			printk("Open file %s returned %p and name_to_devt "
+					"failed.\n",
+					target, target_file);
+			if (!resume_dev_t) {
+				struct kstat stat;
+				int error = vfs_stat(target, &stat);
+				if (error) {
+					printk("Stating the file also failed."
+						" Nothing more we can do.\n");
+					return;
+				}
+				resume_dev_t = stat.rdev;
+			}
+			return;
+		}
+	     	filewriter_target_bdev = open_by_devnum(resume_dev_t, FMODE_READ);
+		if (IS_ERR(filewriter_target_bdev)) {
+			printk("Got a dev_num (%lx) but failed to open it.\n",
+					(unsigned long) resume_dev_t);
+			return;
+		}
+		used_devt = 1;
+		target_inode = filewriter_target_bdev->bd_inode;
+	} else
+		target_inode = target_file->f_mapping->host;
+
+	if (S_ISLNK(target_inode->i_mode) ||
+	    S_ISDIR(target_inode->i_mode) ||
+	    S_ISSOCK(target_inode->i_mode) ||
+	    S_ISFIFO(target_inode->i_mode)) {
+		printk("The filewriter works with regular files, character "
+				"files and block devices.\n");
+		goto cleanup;
+	}
+
+	if (!used_devt) {
+		if (S_ISBLK(target_inode->i_mode)) {
+			filewriter_target_bdev = I_BDEV(target_inode);
+			if (!bd_claim(filewriter_target_bdev, &filewriterops))
+				target_claim = 1;
+		} else
+			filewriter_target_bdev = target_inode->i_sb->s_bdev;
+		resume_dev_t = filewriter_target_bdev->bd_dev;
+	}
+
+	set_devinfo(filewriter_target_bdev, target_inode->i_blkbits);
+
+	if (get_size)
+		target_storage_available = size_ignoring_ignored_pages();
+
+	if (!resume2)
+		target_firstblock = bmap(target_inode, 0) << devinfo.bmap_shift;
+	
+	return;
+cleanup:
+	target_inode = NULL;
+	if (target_file) {
+		filp_close(target_file, NULL);
+		target_file = NULL;
+	}
+	set_devinfo(NULL, 0);
+	target_storage_available = 0;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#10 lore raw

PATCH 10/35 Parse filewriter signature.

In reply to Nigel Cunningham

Given a pointer to what we expect to be a filewriter signature, return whether it is unrecognised (-1), a file without an image (0) or a file with an image to resume from (1).

 kernel/power/suspend_file.c |   20 ++++++++++++++++++++
 1 files changed, 20 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +20 -0
index 6851977..d5f0310 100644
@@ -365,3 +365,23 @@ cleanup:
 	target_storage_available = 0;
 }
 
+int parse_signature(struct filewriter_header *header)
+{
+	int have_image = !memcmp(HaveImage, header->sig, sizeof(HaveImage) - 1);
+	int no_image_header = !memcmp(NoImage, header->sig, sizeof(NoImage) - 1);
+
+	if (no_image_header)
+		return 0;
+
+	if (!have_image)
+		return -1;
+
+	if (header->resumed_before)
+		set_suspend_state(SUSPEND_RESUMED_BEFORE);
+	else
+		clear_suspend_state(SUSPEND_RESUMED_BEFORE);
+
+	target_header_start = header->first_header_block;
+	return 1;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#11 lore raw

PATCH 11/35 Set the filewriter signature to say we have an image.

In reply to Nigel Cunningham

Given a pointer to a buffer containng the filewriter signature, modify it so that it will be recognised as an image that we haven't attempted to resume from before.

 kernel/power/suspend_file.c |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +15 -0
index d5f0310..002f73c 100644
@@ -385,3 +385,18 @@ int parse_signature(struct filewriter_he
 	return 1;
 }
 
+/* prepare_signature */
+
+static int prepare_signature(struct filewriter_header *current_header,
+		unsigned long first_header_block)
+{
+	/* 
+	 * Explicitly put the \0 that clears the 'tried to resume from
+	 * this image before' flag.
+	 */
+	strncpy(current_header->sig, HaveImage, sizeof(HaveImage));
+	current_header->resumed_before = 0;
+	current_header->first_header_block = first_header_block;
+	return 0;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#12 lore raw

PATCH 12/35 Get filewriter storage allocated.

In reply to Nigel Cunningham

Return the amount of filewriter storage that is currently allocated.

 kernel/power/suspend_file.c |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +15 -0
index 002f73c..74d97a0 100644
@@ -400,3 +400,18 @@ static int prepare_signature(struct file
 	return 0;
 }
 
+static int filewriter_storage_allocated(void)
+{
+	int result;
+
+	if (!target_inode)
+		return 0;
+
+	if (target_is_normal_file()) {
+		result = (int) target_storage_available;
+	} else
+		result = header_pages + main_pages;
+
+	return result;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#13 lore raw

PATCH 13/35 Release allocated filewriter storage.

In reply to Nigel Cunningham

Free storage allocated for filewriter use.

 kernel/power/suspend_file.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +12 -0
index 74d97a0..257acca 100644
@@ -415,3 +415,15 @@ static int filewriter_storage_allocated(
 	return result;
 }
 
+static int filewriter_release_storage(void)
+{
+	if ((test_action_state(SUSPEND_KEEP_IMAGE)) &&
+	     test_suspend_state(SUSPEND_NOW_RESUMING))
+		return 0;
+
+	suspend_put_extent_chain(&block_chain);
+
+	header_pages = main_pages = 0;
+	return 0;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#14 lore raw

PATCH 14/35 Allocate filewriter header storage

In reply to Nigel Cunningham

Having already allocated storage, set aside a number of pages at the start of the image for the header and record where pageset2 will start.

 kernel/power/suspend_file.c |   21 +++++++++++++++++++++
 1 files changed, 21 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +21 -0
index 257acca..6b04eb5 100644
@@ -427,3 +427,24 @@ static int filewriter_release_storage(vo
 	return 0;
 }
 
+static int filewriter_allocate_header_space(int space_requested)
+{
+	int i;
+
+	if (!block_chain.first)
+		return 0;
+
+	suspend_extent_state_goto_start(&suspend_writer_posn);
+	suspend_bio_ops.forward_one_page(); /* To first page */
+	
+	for (i = 0; i < space_requested; i++)
+		if (suspend_bio_ops.forward_one_page())
+			return -ENOSPC;
+
+	/* The end of header pages will be the start of pageset 2 */
+	suspend_extent_state_save(&suspend_writer_posn,
+			&suspend_writer_posn_save[2]);
+	header_pages = space_requested;
+	return 0;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#15 lore raw

PATCH 15/35 Allocate filewriter storage.

In reply to Nigel Cunningham

Allocate storage for the body of the image, preserving any existing allocation for the header. This routine will never shrink the amount allocated.

 kernel/power/suspend_file.c |   32 ++++++++++++++++++++++++++++++++
 1 files changed, 32 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +32 -0
index 6b04eb5..cdc0261 100644
@@ -448,3 +448,35 @@ static int filewriter_allocate_header_sp
 	return 0;
 }
 
+static int filewriter_allocate_storage(int space_requested)
+{
+	int result = 0, prev_header_pages;
+	int blocks_to_get = (space_requested << devinfo.bmap_shift) -
+		block_chain.size;
+	
+	/* Only release_storage reduces the size */
+	if (blocks_to_get < 1)
+		return 0;
+
+	main_pages = space_requested;
+
+	populate_block_list();
+
+	suspend_message(SUSPEND_WRITER, SUSPEND_MEDIUM, 0,
+		"Finished with block_chain.size == %d.\n",
+		block_chain.size);
+
+	if (block_chain.size < (header_pages + main_pages)) {
+		printk("Block chain size (%d) < header pages (%d) + main pages (%d) (=%d).\n",
+				block_chain.size,
+				header_pages, main_pages,
+				header_pages + main_pages);
+		result = -ENOSPC;
+	}
+
+	prev_header_pages = header_pages;
+	header_pages = 0;
+	filewriter_allocate_header_space(prev_header_pages);
+	return result;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#16 lore raw

PATCH 16/35 Filewriter header writing init/cleanup.

In reply to Nigel Cunningham

Initialisation and cleanup routines for writing the image header.

 kernel/power/suspend_file.c |   64 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 64 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +64 -0
index cdc0261..46527f1 100644
@@ -480,3 +480,67 @@ static int filewriter_allocate_storage(i
 	return result;
 }
 
+static int filewriter_write_header_init(void)
+{
+	suspend_extent_state_goto_start(&suspend_writer_posn);
+
+	suspend_writer_buffer = (char *) get_zeroed_page(GFP_ATOMIC);
+	suspend_writer_buffer_posn = suspend_header_bytes_used = 0;
+
+	/* Info needed to bootstrap goes at the start of the header.
+	 * First we save the basic info needed for reading, including the number
+	 * of header pages. Then we save the structs containing data needed
+	 * for reading the header pages back.
+	 * Note that even if header pages take more than one page, when we
+	 * read back the info, we will have restored the location of the
+	 * next header page by the time we go to use it.
+	 */
+
+	suspend_bio_ops.rw_header_chunk(WRITE, &filewriterops,
+			(char *) &suspend_writer_posn_save, 
+			sizeof(suspend_writer_posn_save));
+
+	suspend_bio_ops.rw_header_chunk(WRITE, &filewriterops,
+			(char *) &devinfo, sizeof(devinfo));
+
+	suspend_serialise_extent_chain(&filewriterops, &block_chain);
+	
+	return 0;
+}
+
+static int filewriter_write_header_cleanup(void)
+{
+	struct filewriter_header *header;
+
+	/* Write any unsaved data */
+	if (suspend_writer_buffer_posn)
+		suspend_bio_ops.write_header_chunk_finish();
+
+	suspend_bio_ops.finish_all_io();
+
+	suspend_extent_state_goto_start(&suspend_writer_posn);
+	suspend_bio_ops.forward_one_page();
+
+	/* Adjust image header */
+	suspend_bio_ops.bdev_page_io(READ, filewriter_target_bdev,
+			target_firstblock,
+			virt_to_page(suspend_writer_buffer));
+
+	header = (struct filewriter_header *) suspend_writer_buffer;
+
+	prepare_signature(header,
+			suspend_writer_posn.current_offset <<
+			devinfo.bmap_shift);
+		
+	suspend_bio_ops.bdev_page_io(WRITE, filewriter_target_bdev,
+			target_firstblock,
+			virt_to_page(suspend_writer_buffer));
+
+	free_page((unsigned long) suspend_writer_buffer);
+	suspend_writer_buffer = NULL;
+	
+	suspend_bio_ops.finish_all_io();
+
+	return 0;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#17 lore raw

PATCH 17/35 Filewriter initrd image support.

In reply to Nigel Cunningham

First cut support for making the initrd itself the image.

 kernel/power/suspend_file.c |   38 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 38 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +38 -0
index 46527f1..6f298fc 100644
@@ -544,3 +544,41 @@ static int filewriter_write_header_clean
 	return 0;
 }
 
+/* HEADER READING */
+
+#ifdef CONFIG_DEVFS_FS
+int  create_dev(char *name, dev_t dev, char *devfs_name);
+#else
+static int create_dev(char *name, dev_t dev, char *devfs_name)
+{
+	sys_unlink(name);
+	return sys_mknod(name, S_IFBLK|0600, new_encode_dev(dev));
+}
+#endif
+
+static int rd_init(void)
+{
+	suspend_writer_buffer_posn = 0;
+
+	create_dev("/dev/root", ROOT_DEV, root_device_name);
+	create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, 0), NULL);
+
+	suspend_read_fd = sys_open("/dev/root", O_RDONLY, 0);
+	if (suspend_read_fd < 0)
+		goto out;
+	
+	sys_read(suspend_read_fd, suspend_writer_buffer, BLOCK_SIZE);
+
+	memcpy(&suspend_writer_posn_save,
+		suspend_writer_buffer + suspend_writer_buffer_posn,
+		sizeof(suspend_writer_posn_save));
+	
+	suspend_writer_buffer_posn += sizeof(suspend_writer_posn_save);
+
+	return 0;
+out:
+	sys_unlink("/dev/ram");
+	sys_unlink("/dev/root");
+	return -EIO;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#18 lore raw

PATCH 18/35 Header reading initialisation specific to files.

In reply to Nigel Cunningham

Initialisation specific to files (as opposed to reading from an initrd).

 kernel/power/suspend_file.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +12 -0
index 6f298fc..1dd082f 100644
@@ -582,3 +582,15 @@ out:
 	return -EIO;
 }
 
+static int file_init(void)
+{
+	suspend_writer_buffer_posn = 0;
+
+	/* Read filewriter configuration */
+	suspend_bio_ops.bdev_page_io(READ, filewriter_target_bdev,
+			target_header_start,
+			virt_to_page((unsigned long) suspend_writer_buffer));
+	
+	return 0;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#19 lore raw

PATCH 19/35 Filewriter header reading initialisation.

In reply to Nigel Cunningham

Shared initialisation when reading a filewriter image header.

 kernel/power/suspend_file.c |   62 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 62 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +62 -0
index 1dd082f..da50422 100644
@@ -594,3 +594,65 @@ static int file_init(void)
 	return 0;
 }
 
+/*
+ * read_header_init()
+ * 
+ * Ramdisk support based heavily on init/do_mounts_rd.c
+ *
+ * Description:
+ * 1. Attempt to read the device specified with resume2=.
+ * 2. Check the contents of the header for our signature.
+ * 3. Warn, ignore, reset and/or continue as appropriate.
+ * 4. If continuing, read the filewriter configuration section
+ *    of the header and set up block device info so we can read
+ *    the rest of the header & image.
+ *
+ * Returns:
+ * May not return if user choose to reboot at a warning.
+ * -EINVAL if cannot resume at this time. Booting should continue
+ * normally.
+ */
+
+static int filewriter_read_header_init(void)
+{
+	int result;
+	struct block_device *tmp;
+
+	suspend_writer_buffer = (char *) get_zeroed_page(GFP_ATOMIC);
+	
+	if (test_suspend_state(SUSPEND_TRY_RESUME_RD))
+		result = rd_init();
+	else
+		result = file_init();
+	
+	if (result) {
+		printk("Filewriter read header init: Failed to initialise "
+				"reading the first page of data.\n");
+		return result;
+	}
+
+	memcpy(&suspend_writer_posn_save,
+	       suspend_writer_buffer + suspend_writer_buffer_posn,
+	       sizeof(suspend_writer_posn_save));
+	
+	suspend_writer_buffer_posn += sizeof(suspend_writer_posn_save);
+
+	tmp = devinfo.bdev;
+
+	memcpy(&devinfo,
+	       suspend_writer_buffer + suspend_writer_buffer_posn,
+	       sizeof(devinfo));
+
+	devinfo.bdev = tmp;
+	suspend_writer_buffer_posn += sizeof(devinfo);
+
+	suspend_extent_state_goto_start(&suspend_writer_posn);
+	suspend_bio_ops.set_extra_page_forward();
+
+	suspend_header_bytes_used = suspend_writer_buffer_posn;
+
+	suspend_load_extent_chain(&block_chain);
+	
+	return 0;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#20 lore raw

PATCH 20/35 Filewriter image header read cleanup.

In reply to Nigel Cunningham

Cleanup routine for reading a filewriter image header.

 kernel/power/suspend_file.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +7 -0
index da50422..8e4431c 100644
@@ -656,3 +656,10 @@ static int filewriter_read_header_init(v
 	return 0;
 }
 
+static int filewriter_read_header_cleanup(void)
+{
+	free_page((unsigned long) suspend_writer_buffer);
+	suspend_writer_buffer = NULL;
+	return 0;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#21 lore raw

PATCH 21/35 Make modifications to the filewriter signature.

In reply to Nigel Cunningham

Implement support for invalidating an image or marking that we've attempted to resume from the image before.

 kernel/power/suspend_file.c |   51 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 51 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +51 -0
index 8e4431c..4df05c8 100644
@@ -663,3 +663,54 @@ static int filewriter_read_header_cleanu
 	return 0;
 }
 
+static int filewriter_signature_op(int op)
+{
+	char *cur;
+	int result = 0, changed = 0;
+	struct filewriter_header *header;
+	
+	if(filewriter_target_bdev <= 0)
+		return -1;
+
+	cur = (char *) get_zeroed_page(GFP_ATOMIC);
+	if (!cur) {
+		printk("Unable to allocate a page for reading the image "
+				"signature.\n");
+		return -ENOMEM;
+	}
+
+	suspend_bio_ops.bdev_page_io(READ, filewriter_target_bdev,
+			target_firstblock,
+			virt_to_page(cur));
+
+	header = (struct filewriter_header *) cur;
+	result = parse_signature(header);
+		
+	switch (op) {
+		case INVALIDATE:
+			if (result == -1)
+				goto out;
+
+			strcpy(header->sig, NoImage);
+			header->resumed_before = 0;
+			result = changed = 1;
+			break;
+		case MARK_RESUME_ATTEMPTED:
+			if (result == 1) {
+				header->resumed_before = 1;
+				changed = 1;
+			}
+			break;
+	}
+
+	if (changed)
+		suspend_bio_ops.bdev_page_io(WRITE, filewriter_target_bdev,
+				target_firstblock,
+				virt_to_page(cur));
+
+out:
+	suspend_bio_ops.finish_all_io();
+	free_page((unsigned long) cur);
+	return result;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#22 lore raw

PATCH 22/35 Filewriter debug info routine.

In reply to Nigel Cunningham

Fill a buffer with the debugging information that the filewriter provides.

 kernel/power/suspend_file.c |   23 +++++++++++++++++++++++
 1 files changed, 23 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +23 -0
index 4df05c8..a4871c7 100644
@@ -714,3 +714,26 @@ out:
 	return result;
 }
 
+/* Print debug info
+ *
+ * Description:
+ */
+
+static int filewriter_print_debug_stats(char *buffer, int size)
+{
+	int len = 0;
+	
+	if (suspend_active_writer != &filewriterops) {
+		len = snprintf_used(buffer, size, "- Filewriter inactive.\n");
+		return len;
+	}
+
+	len = snprintf_used(buffer, size, "- Filewriter active.\n");
+
+	len+= snprintf_used(buffer+len, size-len, "  Storage available for image: "
+			"%ld pages.\n",
+			filewriter_storage_allocated());
+
+	return len;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#23 lore raw

PATCH 23/35 Filewriter storage needed.

In reply to Nigel Cunningham

Return the amount of space in the image header required by the filewriter.

 kernel/power/suspend_file.c |   20 ++++++++++++++++++++
 1 files changed, 20 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +20 -0
index a4871c7..4f9d6f5 100644
@@ -737,3 +737,23 @@ static int filewriter_print_debug_stats(
 	return len;
 }
 
+/*
+ * Storage needed
+ *
+ * Returns amount of space in the image header required
+ * for the filewriter's data.
+ *
+ * We ensure the space is allocated, but actually save the
+ * data from write_header_init and therefore don't also define a
+ * save_config_info routine.
+ */
+static unsigned long filewriter_storage_needed(void)
+{
+	return sig_size + strlen(filewriter_target) + 1 +
+		3 * sizeof(struct extent_iterate_saved_state) +
+		sizeof(devinfo) +
+		sizeof(struct extent_chain) - 2 * sizeof(void *) +
+		(2 * sizeof(unsigned long) *
+		 (block_chain.allocs - block_chain.frees));
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#24 lore raw

PATCH 24/35 Invalidate filewriter image.

In reply to Nigel Cunningham

Invalidate an image stored by the filewriter.

 kernel/power/suspend_file.c |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +18 -0
index 4f9d6f5..04f9cd0 100644
@@ -757,3 +757,21 @@ static unsigned long filewriter_storage_
 		 (block_chain.allocs - block_chain.frees));
 }
 
+/* 
+ * filewriter_invalidate_image
+ * 
+ */
+static int filewriter_invalidate_image(void)
+{
+	int result;
+
+	if (nr_suspends > 0)
+		filewriter_release_storage();
+
+	result = filewriter_signature_op(INVALIDATE);
+	if (result == 1 && !nr_suspends)
+		printk(KERN_WARNING name_suspend "Image invalidated.\n");
+
+	return result;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#25 lore raw

PATCH 25/35 Does a filewriter image exist?

In reply to Nigel Cunningham

Return whether an image exists at the current resume2= location.

 kernel/power/suspend_file.c |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +13 -0
index 04f9cd0..5a1bfe6 100644
@@ -775,3 +775,16 @@ static int filewriter_invalidate_image(v
 	return result;
 }
 
+/*
+ * Image_exists
+ *
+ */
+
+static int filewriter_image_exists(void)
+{
+	if (!filewriter_target_bdev)
+		reopen_resume_devt();
+
+	return filewriter_signature_op(GET_IMAGE_EXISTS);
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#26 lore raw

PATCH 26/35 Filewriter mark that attempted to resume.

In reply to Nigel Cunningham

Set the flag in an image header that says we've tried to resume before.

 kernel/power/suspend_file.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +11 -0
index 5a1bfe6..20d0807 100644
@@ -788,3 +788,14 @@ static int filewriter_image_exists(void)
 	return filewriter_signature_op(GET_IMAGE_EXISTS);
 }
 
+/*
+ * Mark resume attempted.
+ *
+ * Record that we tried to resume from this image.
+ */
+
+static void filewriter_mark_resume_attempted(void)
+{
+	filewriter_signature_op(MARK_RESUME_ATTEMPTED);
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#27 lore raw

PATCH 27/35 Set filewriter resume2, having been given a filename.

In reply to Nigel Cunningham

Set the resume2= value when the user has echoed to filewriter_target and we've verified that an image exists in that file. The user can then read the value and modify their bootloader or initrd to set the value to the same thing when resuming.

 kernel/power/suspend_file.c |   29 +++++++++++++++++++++++++++++
 1 files changed, 29 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +29 -0
index 20d0807..2e19aae 100644
@@ -799,3 +799,32 @@ static void filewriter_mark_resume_attem
 	filewriter_signature_op(MARK_RESUME_ATTEMPTED);
 }
 
+static void filewriter_set_resume2(void)
+{
+	char *buffer = (char *) get_zeroed_page(GFP_ATOMIC);
+	char *buffer2 = (char *) get_zeroed_page(GFP_ATOMIC);
+	unsigned long sector = bmap(target_inode, 0);
+	int offset = 0;
+
+	if (filewriter_target_bdev) {
+		set_devinfo(filewriter_target_bdev, target_inode->i_blkbits);
+
+		bdevname(filewriter_target_bdev, buffer2);
+		offset += snprintf(buffer + offset, PAGE_SIZE - offset, 
+				"/dev/%s", buffer2);
+		
+		if (sector)
+			offset += snprintf(buffer + offset, PAGE_SIZE - offset,
+				":0x%lx", sector << devinfo.bmap_shift);
+	} else
+		offset += snprintf(buffer + offset, PAGE_SIZE - offset,
+				"%s is not a valid target.", filewriter_target);
+			
+	sprintf(resume2_file, "file:%s", buffer);
+
+	free_page((unsigned long) buffer);
+	free_page((unsigned long) buffer2);
+
+	suspend_attempt_to_parse_resume_device();
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#28 lore raw

PATCH 28/35 Test filewriter target value helper.

In reply to Nigel Cunningham

The helper called either when the user echoes a new value into filewriter_target, or when resume2= is checked on boot or when it is reset.

 kernel/power/suspend_file.c |   30 ++++++++++++++++++++++++++++++
 1 files changed, 30 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +30 -0
index 2e19aae..9947323 100644
@@ -828,3 +828,33 @@ static void filewriter_set_resume2(void)
 	suspend_attempt_to_parse_resume_device();
 }
 
+static int __test_filewriter_target(char *target, int resume_time)
+{
+	filewriter_get_target_info(target, 0, resume_time);
+	if (filewriter_signature_op(GET_IMAGE_EXISTS) > -1) {
+		printk(name_suspend "Filewriter: File signature found.\n");
+		if (!resume_time)
+			filewriter_set_resume2();
+		
+		suspend_bio_ops.set_devinfo(&devinfo);
+		suspend_writer_posn.chains = &block_chain;
+		suspend_writer_posn.num_chains = 1;
+
+		set_suspend_state(SUSPEND_CAN_SUSPEND);
+		return 0;
+	}
+
+	clear_suspend_state(SUSPEND_CAN_SUSPEND);
+
+	if (*target)
+		printk(name_suspend
+			"Filewriter: Sorry. No signature found at %s.\n",
+			target);
+	else
+		if (!resume_time)
+			printk(name_suspend
+				"Filewriter: Sorry. Target is not set for suspending.\n");
+
+	return 1;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#29 lore raw

PATCH 29/35 Test filewriter target.

In reply to Nigel Cunningham

Test the filewriter target when the filewriter_target proc entry is written.

 kernel/power/suspend_file.c |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +14 -0
index 9947323..56de7d9 100644
@@ -858,3 +858,17 @@ static int __test_filewriter_target(char
 	return 1;
 }
 
+static void test_filewriter_target(void)
+{
+	int cant_suspend;
+
+	setting_filewriter_target = 1;
+       	
+	printk(name_suspend "Filewriter: Testing whether you can suspend:\n");
+	cant_suspend =__test_filewriter_target(filewriter_target, 0);
+
+	printk(name_suspend "Suspending %sabled.\n",  cant_suspend ? "dis" : "en");
+	
+	setting_filewriter_target = 0;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#30 lore raw

PATCH 30/35 Filewriter parse resume2 parameter.

In reply to Nigel Cunningham

Parse a resume2= value to see if it is for the filewriter's use (file:
prefix or filewriter is the only writer registered), and whether it points to a device we can open and a header we recognise.

 kernel/power/suspend_file.c |  116 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 116 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +116 -0
index 56de7d9..ee371ce 100644
@@ -872,3 +872,119 @@ static void test_filewriter_target(void)
 	setting_filewriter_target = 0;
 }
 
+/*
+ * Parse Image Location
+ *
+ * Attempt to parse a resume2= parameter.
+ * Swap Writer accepts:
+ * resume2=file:DEVNAME[:FIRSTBLOCK]
+ *
+ * Where:
+ * DEVNAME is convertable to a dev_t by name_to_dev_t
+ * FIRSTBLOCK is the location of the first block in the file.
+ * BLOCKSIZE is the logical blocksize >= SECTOR_SIZE & <= PAGE_SIZE, 
+ * mod SECTOR_SIZE == 0 of the device.
+ * Data is validated by attempting to read a header from the
+ * location given. Failure will result in filewriter refusing to
+ * save an image, and a reboot with correct parameters will be
+ * necessary.
+ */
+
+static int filewriter_parse_sig_location(char *commandline, int only_writer)
+{
+	char *thischar, *devstart = NULL, *colon = NULL, *at_symbol = NULL;
+	int result = -EINVAL, target_blocksize = 0;
+
+	if (strncmp(commandline, "file:", 5)) {
+		if (!only_writer)
+			return 1;
+	} else
+		commandline += 5;
+
+	/* 
+	 * Don't check signature again if we're beginning a cycle. If we already
+	 * did the initialisation successfully, assume we'll be okay when it comes
+	 * to resuming.
+	 */
+	if (filewriter_target_bdev)
+		return 0;
+	
+	devstart = thischar = commandline;
+	while ((*thischar != ':') && (*thischar != '@') &&
+		((thischar - commandline) < 250) && (*thischar))
+		thischar++;
+
+	if (*thischar == ':') {
+		colon = thischar;
+		*colon = 0;
+		thischar++;
+	}
+
+	while ((*thischar != '@') && ((thischar - commandline) < 250) && (*thischar))
+		thischar++;
+
+	if (*thischar == '@') {
+		at_symbol = thischar;
+		*at_symbol = 0;
+	}
+	
+	/* 
+	 * For the filewriter, you can be able to resume, but not suspend,
+	 * because the resume2= is set correctly, but the filewriter_target
+	 * isn't. 
+	 *
+	 * We may have come here as a result of setting resume2 or
+	 * filewriter_target. We only test the filewriter target in the
+	 * former case (it's already done in the later), and we do it before
+	 * setting the block number ourselves. It will overwrite the values
+	 * given on the command line if we don't.
+	 */
+
+	if (!setting_filewriter_target)
+		__test_filewriter_target(filewriter_target, 1);
+
+	if (colon)
+		target_firstblock = (int) simple_strtoul(colon + 1, NULL, 0);
+	else
+		target_firstblock = 0;
+
+	if (at_symbol) {
+		target_blocksize = (int) simple_strtoul(at_symbol + 1, NULL, 0);
+		if (target_blocksize & (SECTOR_SIZE - 1)) {
+			printk("Filewriter: Blocksizes are multiples of %d.\n", SECTOR_SIZE);
+			result = -EINVAL;
+			goto out;
+		}
+	}
+	
+	printk("Suspend2 Filewriter: Testing whether you can resume:\n");
+
+	filewriter_get_target_info(commandline, 0, 1);
+
+	if (!filewriter_target_bdev || IS_ERR(filewriter_target_bdev)) {
+		filewriter_target_bdev = NULL;
+		result = -1;
+		goto out;
+	}
+
+	if (target_blocksize)
+		set_devinfo(filewriter_target_bdev, ffs(target_blocksize));
+
+	result = __test_filewriter_target(commandline, 1);
+
+out:
+	if (result)
+		clear_suspend_state(SUSPEND_CAN_SUSPEND);
+	else
+		set_suspend_state(SUSPEND_CAN_SUSPEND);
+
+	printk("Resuming %sabled.\n",  result ? "dis" : "en");
+
+	if (colon)
+		*colon = ':';
+	if (at_symbol)
+		*at_symbol = '@';
+
+	return result;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#31 lore raw

PATCH 31/35 Serialise filewriter config in image header.

In reply to Nigel Cunningham

Routines to save and load the target in the image header.

 kernel/power/suspend_file.c |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +25 -0
index ee371ce..d0730fc 100644
@@ -988,3 +988,28 @@ out:
 	return result;
 }
 
+/* filewriter_save_config_info
+ *
+ * Description:	Save the target's name, not for resume time, but for all_settings.
+ * Arguments:	Buffer:		Pointer to a buffer of size PAGE_SIZE.
+ * Returns:	Number of bytes used for saving our data.
+ */
+
+static int filewriter_save_config_info(char *buffer)
+{
+	strcpy(buffer, filewriter_target);
+	return strlen(filewriter_target) + 1;
+}
+
+/* filewriter_load_config_info
+ *
+ * Description:	Reload target's name.
+ * Arguments:	Buffer:		Pointer to the start of the data.
+ *		Size:		Number of bytes that were saved.
+ */
+
+static void filewriter_load_config_info(char *buffer, int size)
+{
+	strcpy(filewriter_target, buffer);
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#32 lore raw

PATCH 32/35 Filewriter initialise.

In reply to Nigel Cunningham

Initialise the filewriter at the beginning of some work.

 kernel/power/suspend_file.c |   26 ++++++++++++++++++++++++++
 1 files changed, 26 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +26 -0
index d0730fc..ad35967 100644
@@ -1013,3 +1013,29 @@ static void filewriter_load_config_info(
 	strcpy(filewriter_target, buffer);
 }
 
+static int filewriter_initialise(int starting_cycle)
+{
+	int result = 0;
+
+	if (starting_cycle) {
+	       if (suspend_active_writer != &filewriterops)
+			return 0;
+
+		if (!*filewriter_target) {
+			printk("Filewriter is the active writer,  but no filename has been set.\n");
+			return 1;
+		}
+	}
+
+	if (filewriter_target)
+		filewriter_get_target_info(filewriter_target, starting_cycle, 0);
+
+	if (starting_cycle && (filewriter_image_exists() == -1)) {
+		printk("%s is does not have a valid signature for suspending.\n",
+				filewriter_target);
+		result = 1;
+	}
+
+	return result;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#33 lore raw

PATCH 33/35 Filewriter proc data.

In reply to Nigel Cunningham

Definitions for filewriter proc entries.

 kernel/power/suspend_file.c |   30 ++++++++++++++++++++++++++++++
 1 files changed, 30 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +30 -0
index ad35967..4ec51c2 100644
@@ -1039,3 +1039,33 @@ static int filewriter_initialise(int sta
 	return result;
 }
 
+static struct suspend_proc_data filewriter_proc_data[] = {
+
+	{
+	 .filename			= "filewriter_target",
+	 .permissions			= PROC_RW,
+	 .type				= SUSPEND_PROC_DATA_STRING,
+	 .needs_storage_manager		= 2,
+	 .data = {
+		 .string = {
+			 .variable	= filewriter_target,
+			 .max_length	= 256,
+		 }
+	 },
+	 .write_proc			= test_filewriter_target,
+	},
+
+	{ .filename			= "disable_filewriter",
+	  .permissions			= PROC_RW,
+	  .type				= SUSPEND_PROC_DATA_INTEGER,
+	  .data = {
+		.integer = {
+			.variable	= &filewriterops.disabled,
+			.minimum	= 0,
+			.maximum	= 1,
+		}
+	  },
+	  .write_proc			= attempt_to_parse_resume_device2,
+	}
+};
+

--
Nigel Cunningham nigel at suspend2 dot net

#34 lore raw

PATCH 34/35 Filewriter ops.

In reply to Nigel Cunningham

Declare the filewriter ops structure, which provides the core with access to its functions.

 kernel/power/suspend_file.c |   26 ++++++++++++++++++++++++++
 1 files changed, 26 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +26 -0
index 4ec51c2..56c7a76 100644
@@ -1069,3 +1069,29 @@ static struct suspend_proc_data filewrit
 	}
 };
 
+static struct suspend_module_ops filewriterops = {
+	.type					= WRITER_MODULE,
+	.name					= "File Writer",
+	.module					= THIS_MODULE,
+	.print_debug_info			= filewriter_print_debug_stats,
+	.save_config_info			= filewriter_save_config_info,
+	.load_config_info			= filewriter_load_config_info,
+	.storage_needed				= filewriter_storage_needed,
+	.initialise				= filewriter_initialise,
+	.cleanup				= filewriter_cleanup,
+
+	.storage_available 	= filewriter_storage_available,
+	.storage_allocated	= filewriter_storage_allocated,
+	.release_storage	= filewriter_release_storage,
+	.allocate_header_space	= filewriter_allocate_header_space,
+	.allocate_storage	= filewriter_allocate_storage,
+	.image_exists		= filewriter_image_exists,
+	.mark_resume_attempted	= filewriter_mark_resume_attempted,
+	.write_header_init	= filewriter_write_header_init,
+	.write_header_cleanup	= filewriter_write_header_cleanup,
+	.read_header_init	= filewriter_read_header_init,
+	.read_header_cleanup	= filewriter_read_header_cleanup,
+	.invalidate_image	= filewriter_invalidate_image,
+	.parse_sig_location	= filewriter_parse_sig_location,
+};
+

--
Nigel Cunningham nigel at suspend2 dot net

#35 lore raw

PATCH 35/35 Filewriter load/unload routines.

In reply to Nigel Cunningham

Load and unload procedures for the filewriter. Set fields in the ops structure from the block writer, register with the core and register proc entries. Do the reverse on unload.

 kernel/power/suspend_file.c |   44 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 44 insertions(+), 0 deletions(-)
kernel/power/suspend_file.c +44 -0
index 56c7a76..1d8b296 100644
@@ -1095,3 +1095,47 @@ static struct suspend_module_ops filewri
 	.parse_sig_location	= filewriter_parse_sig_location,
 };
 
+/* ---- Registration ---- */
+static __init int filewriter_load(void)
+{
+	int result;
+	int i, numfiles = sizeof(filewriter_proc_data) / sizeof(struct suspend_proc_data);
+	
+	printk("Suspend2 FileWriter loading.\n");
+
+	filewriterops.read_chunk = suspend_bio_ops.read_chunk;
+	filewriterops.write_chunk = suspend_bio_ops.write_chunk;
+	filewriterops.rw_init = suspend_bio_ops.rw_init;
+	filewriterops.rw_cleanup = suspend_bio_ops.rw_cleanup;
+	filewriterops.rw_header_chunk =
+		suspend_bio_ops.rw_header_chunk;
+
+	if (!(result = suspend_register_module(&filewriterops))) {
+		for (i=0; i< numfiles; i++)
+			suspend_register_procfile(&filewriter_proc_data[i]);
+	} else
+		printk("Suspend2 FileWriter unable to register!\n");
+
+	return result;
+}
+
+#ifdef MODULE
+static __exit void filewriter_unload(void)
+{
+	int i, numfiles = sizeof(filewriter_proc_data) / sizeof(struct suspend_proc_data);
+
+	printk("Suspend2 FileWriter unloading.\n");
+
+	for (i=0; i< numfiles; i++)
+		suspend_unregister_procfile(&filewriter_proc_data[i]);
+	suspend_unregister_module(&filewriterops);
+}
+
+module_init(filewriter_load);
+module_exit(filewriter_unload);
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Nigel Cunningham");
+MODULE_DESCRIPTION("Suspend2 filewriter");
+#else
+late_initcall(filewriter_load);
+#endif

--
Nigel Cunningham nigel at suspend2 dot net