public inbox for linux-mtd@lists.infradead.org
 help / color / mirror / Atom feed
From: "Jörn Engel" <joern@wohnheim.fh-wedel.de>
To: "Gareth Bult (Encryptec)" <Gareth@Encryptec.net>
Cc: Linux MTD <linux-mtd@lists.infradead.org>
Subject: [PATCH 19/22] Fold various erase functions
Date: Tue, 21 Dec 2004 15:09:23 +0100	[thread overview]
Message-ID: <20041221140923.GY22636@wohnheim.fh-wedel.de> (raw)
In-Reply-To: <20041221140617.GX22636@wohnheim.fh-wedel.de>

After the previous 18 patches and your work, it becomes obvious that
write_pages() is effectively two functions with very little overlap.
This patch moves the erase part of that function out and simplifies
it.

Signed-off-by: Jörn Engel <joern@wohnheim.fh-wedel.de>
---

 blockmtd.c |  117 ++++++++++++++++++++++++++++++-------------------------------
 1 files changed, 58 insertions(+), 59 deletions(-)

--- linux-2.6.9cow/drivers/mtd/devices/blockmtd.c~blockmtd_erase	2004-12-21 01:52:12.000000000 +0100
+++ linux-2.6.9cow/drivers/mtd/devices/blockmtd.c	2004-12-21 02:12:15.000000000 +0100
@@ -121,70 +121,35 @@
  * part of the page being modified. Pages are added to the bio and then written
  * out.
  */
-
-static int write_pages(struct blockmtd_dev *dev, const u_char *buf, loff_t to, size_t len, size_t *retlen)
+static int my_read(struct page **page, struct address_space *mapping, int index)
+{
+	int err = 0;
+	
+	*page = page_readahead(mapping, index);
+	if (unlikely(!*page)) {
+		ERROR("unable to read page (%d) in write_pages\n", index);
+		err = -ENOMEM;
+	}
+	if (IS_ERR(*page))
+		err = PTR_ERR(*page);
+	if (err)
+		printk("READ ERROR\n");
+	return err;
+}
+static int write_pages(struct blockmtd_dev *dev, const u_char *buf, loff_t to,
+		size_t len, size_t *retlen)
 {
 	struct	page *page;
 	struct	address_space *mapping = dev->blkdev->bd_inode->i_mapping;
-	//
 	int	err	= 0;			// return status
 	int	index	= to >> PAGE_SHIFT;	// page index
 	int	offset 	= to & ~PAGE_MASK;	// page offset
-	//
-
-	static int my_read(void)
-	{
-		err=0;
-		page = page_readahead(mapping,index);
-		if( unlikely(!page) ) {
-			ERROR("unable to read page (%d) in write_pages\n", index);
-			err=-ENOMEM;
-		}
-		//if(IS_ERR(page)) err=-EIO;
-		if(err) printk("READ ERROR\n");
-		return err;
-	}
-	//
-	static int my_erase(void)
-	{
-		int pages = len >> PAGE_SHIFT;
-		unsigned char *p;
-		unsigned char *max;
-		int format;
-
-		while(pages) {
-			//printk(KERN_INFO "readahead on index (%d)\n",index);
-			if(my_read()) return err;
-
-			max = ((char*)page_address(page)+PAGE_SIZE);
-			format = 0;
-
-			for(p=(unsigned char*)page_address(page); p<max; p++) 
-				if(*p != 0xff) {
-					format=1;
-					break;
-				}
-
-			if(format) {
-				lock_page(page);
-				memset(page_address(page), 0xff, PAGE_SIZE);
-				set_page_dirty(page);
-				unlock_page(page);
-			}
-			page_cache_release(page);
-			pages--;
-			index++;
-		}
-		*retlen = len;
-		return 0;
-	}
 
 	static int my_write(void)
 	{
 		int cpylen;
 
 		if(retlen) *retlen = 0;
-		//printk(KERN_INFO "Write req: %d\n",len);
 		while(len) {
 			if((offset+len) > PAGE_SIZE) 
 				cpylen = PAGE_SIZE - offset;	// multiple pages
@@ -193,7 +158,9 @@
 			//
 			//	Get page
 			//
-			if(my_read()) return err;
+			err = my_read(&page, mapping, index);
+			if (err)
+				return err;
 			//
 			if(memcmp(page_address(page)+offset,buf,cpylen)) {
 				lock_page(page);
@@ -216,29 +183,61 @@
 
 	err=0;
 	down(&dev->write_mutex);
-	err = buf ? my_write() : my_erase();
+	err = my_write();
 	up(&dev->write_mutex);
 	return err;
 }
 
 
 /* erase a specified part of the device */
+static int _blockmtd_erase(struct blockmtd_dev *dev, loff_t to, size_t len)
+{
+	struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
+	struct page *page;
+	int index = to >> PAGE_SHIFT;	// page index
+	int pages = len >> PAGE_SHIFT;
+	u_long *p;
+	u_long *max;
+
+	while (pages) {
+		page = page_readahead(mapping, index);
+		if (!page)
+			return -ENOMEM;
+		if (IS_ERR(page))
+			return PTR_ERR(page);
+
+		max = (u_long*)page_address(page) + PAGE_SIZE;
+		for (p=(u_long*)page_address(page); p<max; p++) 
+			if (*p != -1UL) {
+				lock_page(page);
+				memset(page_address(page), 0xff, PAGE_SIZE);
+				set_page_dirty(page);
+				unlock_page(page);
+				break;
+			}
+
+		page_cache_release(page);
+		pages--;
+		index++;
+	}
+	return 0;
+}
 static int blockmtd_erase(struct mtd_info *mtd, struct erase_info *instr)
 {
 	struct blockmtd_dev *dev = mtd->priv;
 	size_t from = instr->addr;
 	size_t len = instr->len;
-	size_t retlen;
 	int err;
 
 	instr->state = MTD_ERASING;
-	err = write_pages(dev, NULL, from, len, &retlen);
-	if (err || retlen != len) {
+	down(&dev->write_mutex);
+	err = _blockmtd_erase(dev, from, len);
+	up(&dev->write_mutex);
+	if (err) {
 		ERROR("erase failed err = %d", err);
 		instr->state = MTD_ERASE_FAILED;
-	} else {
+	} else
 		instr->state = MTD_ERASE_DONE;
-	}
 
 	instr->state = MTD_ERASE_DONE;
 	mtd_erase_callback(instr);

  reply	other threads:[~2004-12-21 14:09 UTC|newest]

Thread overview: 70+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-15 23:19 JFFS2 mount time Gareth Bult (Encryptec)
2004-12-16  0:15 ` Josh Boyer
2004-12-16  1:02   ` Gareth Bult (Encryptec)
2004-12-16 12:53     ` Josh Boyer
2004-12-16 21:22       ` Gareth Bult (Encryptec)
2004-12-16 21:28         ` Josh Boyer
2004-12-16 21:47           ` Gareth Bult (Encryptec)
2004-12-17 12:54             ` Josh Boyer
2004-12-17 15:33               ` Gareth Bult (Encryptec)
2004-12-17 16:02                 ` Josh Boyer
2004-12-17 16:46                   ` Gareth Bult (Encryptec)
2004-12-17 17:08                     ` Artem B. Bityuckiy
2004-12-17 17:10                     ` Josh Boyer
2004-12-17 17:26                       ` Gareth Bult (Encryptec)
2004-12-17 17:35                         ` Josh Boyer
2004-12-17 18:09                           ` Gareth Bult (Encryptec)
2004-12-17 19:14                             ` jasmine
2004-12-17 20:55                               ` Gareth Bult (Encryptec)
2004-12-18 16:02                           ` Jörn Engel
2004-12-20 16:34                             ` Josh Boyer
2004-12-20 17:12                               ` Gareth Bult (Encryptec)
2004-12-21 13:09                                 ` Jörn Engel
2004-12-21 13:24                                   ` Gareth Bult (Encryptec)
2004-12-21 13:34                                     ` Jörn Engel
2004-12-18 16:19             ` Jörn Engel
2004-12-18 17:32               ` Gareth Bult (Encryptec)
2004-12-18 17:52                 ` Jörn Engel
2004-12-18 18:11                   ` Jörn Engel
2004-12-18 20:48                     ` Gareth Bult (Encryptec)
2004-12-19  2:44                       ` Jörn Engel
2004-12-21 13:30                       ` Jörn Engel
2004-12-21 13:39                         ` [PATCH 1/22] Add drivers/mtd/devices/blockmtd.c Jörn Engel
2004-12-21 13:41                           ` [PATCH 2/22] Add copyrights Jörn Engel
2004-12-21 13:42                             ` [PATCH 3/22] Remove read-only option Jörn Engel
2004-12-21 13:44                               ` [PATCH 4/22] Change init/exit functions Jörn Engel
2004-12-21 13:45                                 ` [PATCH 5/22] Remove gcc warnings Jörn Engel
2004-12-21 13:47                                   ` [PATCH 6/22] Remove debug macros Jörn Engel
2004-12-21 13:48                                     ` [PATCH 7/22] Lindent Jörn Engel
2004-12-21 13:49                                       ` [PATCH 8/22] Remove sync interface Jörn Engel
2004-12-21 13:51                                         ` [PATCH 9/22] Change parameter interface to phram-style Jörn Engel
2004-12-21 13:53                                           ` [PATCH 10/22] Cleanup macro usage Jörn Engel
2004-12-21 13:54                                             ` [PATCH 11/22] kfree simplifications Jörn Engel
2004-12-21 13:55                                               ` [PATCH 12/22] change blockmtd_sync Jörn Engel
2004-12-21 13:57                                                 ` [PATCH 13/22] remove erase regions Jörn Engel
2004-12-21 14:01                                                   ` [PATCH 14/22] Change add_device Jörn Engel
2004-12-21 14:02                                                     ` [PATCH 15/22] Rename unreadable mutex Jörn Engel
2004-12-21 14:03                                                       ` [PATCH 16/22] list changes Jörn Engel
2004-12-21 14:04                                                         ` [PATCH 17/22] Rename central struct Jörn Engel
2004-12-21 14:06                                                           ` [PATCH 18/22] Function renaming Jörn Engel
2004-12-21 14:09                                                             ` Jörn Engel [this message]
2004-12-21 14:10                                                               ` [PATCH 20/22] Fold various write functions Jörn Engel
2004-12-21 14:11                                                                 ` [PATCH 21/22] Default erase size Jörn Engel
2004-12-21 14:13                                                                   ` [PATCH 22/22] Readahead Jörn Engel
2004-12-21 18:42                                                   ` [PATCH 13/22] remove erase regions Christopher Hoover
2004-12-21 18:49                                                     ` Jörn Engel
2004-12-21 21:09                                                       ` Christopher Hoover
2004-12-22  2:47                                                         ` Eric W. Biederman
2004-12-22  8:59                                                           ` Jörn Engel
2004-12-22 10:05                                                             ` Eric W. Biederman
2004-12-22 10:41                                                               ` Jörn Engel
2004-12-21 13:42                           ` [PATCH 1/22] Add drivers/mtd/devices/blockmtd.c Gareth Bult (Encryptec)
2004-12-21 14:15                             ` Jörn Engel
2004-12-21 13:40                         ` JFFS2 mount time Gareth Bult (Encryptec)
2004-12-21 15:00                           ` David Woodhouse
     [not found]                             ` <1103644945.10792.175.camel@squizzey.bult.co.uk>
2004-12-21 16:04                               ` Jörn Engel
2004-12-16 13:43 ` Ferenc Havasi
2004-12-20 16:01   ` Gareth Bult (Encryptec)
2004-12-20 16:09     ` Ferenc Havasi
2004-12-20 16:39       ` Gareth Bult (Encryptec)
2004-12-20 17:48       ` Gareth Bult (Encryptec)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20041221140923.GY22636@wohnheim.fh-wedel.de \
    --to=joern@wohnheim.fh-wedel.de \
    --cc=Gareth@Encryptec.net \
    --cc=linux-mtd@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox