Upstream-Status: inappropriate

From c196bdeae7932c5d54bbdb7e7574d3cdae46ad02 Mon Sep 17 00:00:00 2001
From: Corey Minyard <cminyard@mvista.com>
Date: Sat, 4 Jun 2011 22:04:24 -0500
Subject: [PATCH 03/19] Add get_blkmap and put_blkmap.

Add routines for getting an putting a block map.  This does not do
anything functional, but is getting ready for when blockmaps are
byteswapped when being read and written.
---
 genext2fs.c |   84 ++++++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 57 insertions(+), 27 deletions(-)

diff --git a/genext2fs.c b/genext2fs.c
index bd06369..0b5ba6f 100644
--- a/genext2fs.c
+++ b/genext2fs.c
@@ -837,6 +837,36 @@ put_blk(blk_info *bi)
 {
 }
 
+// Used by get_blkmap/put_blkmap to hold information about an block map
+// owned by the user.
+typedef struct
+{
+	blk_info *bi;
+} blkmap_info;
+
+// Return a given block map from a filesystem.  Make sure to call
+// put_blkmap when you are done with it.
+static inline uint32 *
+get_blkmap(filesystem *fs, uint32 blk, blkmap_info **rbmi)
+{
+	blkmap_info *bmi;
+	uint8 *b;
+
+	bmi = malloc(sizeof(*bmi));
+	if (!bmi)
+		error_msg_and_die("get_blkmap: out of memory");
+	b = get_blk(fs, blk, &bmi->bi);
+	*rbmi = bmi;
+	return (uint32 *) b;
+}
+
+static inline void
+put_blkmap(blkmap_info *bmi)
+{
+	put_blk(bmi->bi);
+	free(bmi);
+}
+
 // Used by get_nod/put_nod to hold information about an inode owned
 // by the user.
 typedef struct
@@ -1020,12 +1050,12 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 {
 	uint32 *bkref = 0;
 	uint32 bk = 0;
+	blkmap_info *bmi1 = NULL, *bmi2 = NULL, *bmi3 = NULL;
 	uint32 *b;
 	int extend = 0, reduce = 0;
 	inode *inod;
 	nod_info *ni;
 	uint32 *iblk;
-	blk_info *bi1 = NULL, *bi2 = NULL, *bi3 = NULL;
 
 	if(create && (*create) < 0)
 		reduce = 1;
@@ -1072,7 +1102,7 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 			iblk[bw->bpdir] = alloc_blk(fs,nod);
 		if(reduce) // free indirect block
 			free_blk(fs, iblk[bw->bpdir]);
-		b = (uint32*)get_blk(fs, iblk[bw->bpdir], &bi1);
+		b = get_blkmap(fs, iblk[bw->bpdir], &bmi1);
 		bkref = &b[bw->bpind];
 		if(extend) // allocate first block
 			*bkref = hole ? 0 : alloc_blk(fs,nod);
@@ -1083,7 +1113,7 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 	else if((bw->bpdir == EXT2_IND_BLOCK) && (bw->bpind < BLOCKSIZE/4 - 1))
 	{
 		bw->bpind++;
-		b = (uint32*)get_blk(fs, iblk[bw->bpdir], &bi1);
+		b = get_blkmap(fs, iblk[bw->bpdir], &bmi1);
 		bkref = &b[bw->bpind];
 		if(extend) // allocate block
 			*bkref = hole ? 0 : alloc_blk(fs,nod);
@@ -1101,12 +1131,12 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 			iblk[bw->bpdir] = alloc_blk(fs,nod);
 		if(reduce) // free double indirect block
 			free_blk(fs, iblk[bw->bpdir]);
-		b = (uint32*)get_blk(fs, iblk[bw->bpdir], &bi1);
+		b = get_blkmap(fs, iblk[bw->bpdir], &bmi1);
 		if(extend) // allocate first indirect block
 			b[bw->bpind] = alloc_blk(fs,nod);
 		if(reduce) // free  firstindirect block
 			free_blk(fs, b[bw->bpind]);
-		b = (uint32*)get_blk(fs, b[bw->bpind], &bi1);
+		b = get_blkmap(fs, b[bw->bpind], &bmi2);
 		bkref = &b[bw->bpdind];
 		if(extend) // allocate first block
 			*bkref = hole ? 0 : alloc_blk(fs,nod);
@@ -1117,8 +1147,8 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 	else if((bw->bpdir == EXT2_DIND_BLOCK) && (bw->bpdind < BLOCKSIZE/4 - 1))
 	{
 		bw->bpdind++;
-		b = (uint32*)get_blk(fs, iblk[bw->bpdir], &bi1);
-		b = (uint32*)get_blk(fs, b[bw->bpind], &bi2);
+		b = get_blkmap(fs, iblk[bw->bpdir], &bmi1);
+		b = get_blkmap(fs, b[bw->bpind], &bmi2);
 		bkref = &b[bw->bpdind];
 		if(extend) // allocate block
 			*bkref = hole ? 0 : alloc_blk(fs,nod);
@@ -1131,12 +1161,12 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 		bw->bnum++;
 		bw->bpdind = 0;
 		bw->bpind++;
-		b = (uint32*)get_blk(fs, iblk[bw->bpdir], &bi1);
+		b = get_blkmap(fs, iblk[bw->bpdir], &bmi1);
 		if(extend) // allocate indirect block
 			b[bw->bpind] = alloc_blk(fs,nod);
 		if(reduce) // free indirect block
 			free_blk(fs, b[bw->bpind]);
-		b = (uint32*)get_blk(fs, b[bw->bpind], &bi2);
+		b = get_blkmap(fs, b[bw->bpind], &bmi2);
 		bkref = &b[bw->bpdind];
 		if(extend) // allocate first block
 			*bkref = hole ? 0 : alloc_blk(fs,nod);
@@ -1159,17 +1189,17 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 			iblk[bw->bpdir] = alloc_blk(fs,nod);
 		if(reduce) // free triple indirect block
 			free_blk(fs, iblk[bw->bpdir]);
-		b = (uint32*)get_blk(fs, iblk[bw->bpdir], &bi1);
+		b = get_blkmap(fs, iblk[bw->bpdir], &bmi1);
 		if(extend) // allocate first double indirect block
 			b[bw->bpind] = alloc_blk(fs,nod);
 		if(reduce) // free first double indirect block
 			free_blk(fs, b[bw->bpind]);
-		b = (uint32*)get_blk(fs, b[bw->bpind], &bi2);
+		b = get_blkmap(fs, b[bw->bpind], &bmi2);
 		if(extend) // allocate first indirect block
 			b[bw->bpdind] = alloc_blk(fs,nod);
 		if(reduce) // free first indirect block
 			free_blk(fs, b[bw->bpind]);
-		b = (uint32*)get_blk(fs, b[bw->bpdind], &bi3);
+		b = get_blkmap(fs, b[bw->bpdind], &bmi3);
 		bkref = &b[bw->bptind];
 		if(extend) // allocate first data block
 			*bkref = hole ? 0 : alloc_blk(fs,nod);
@@ -1183,9 +1213,9 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 		  (bw->bptind < BLOCKSIZE/4 -1) )
 	{
 		bw->bptind++;
-		b = (uint32*)get_blk(fs, iblk[bw->bpdir], &bi1);
-		b = (uint32*)get_blk(fs, b[bw->bpind], &bi2);
-		b = (uint32*)get_blk(fs, b[bw->bpdind], &bi3);
+		b = get_blkmap(fs, iblk[bw->bpdir], &bmi1);
+		b = get_blkmap(fs, b[bw->bpind], &bmi2);
+		b = get_blkmap(fs, b[bw->bpdind], &bmi3);
 		bkref = &b[bw->bptind];
 		if(extend) // allocate data block
 			*bkref = hole ? 0 : alloc_blk(fs,nod);
@@ -1202,13 +1232,13 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 		bw->bnum++;
 		bw->bptind = 0;
 		bw->bpdind++;
-		b = (uint32*)get_blk(fs, iblk[bw->bpdir], &bi1);
-		b = (uint32*)get_blk(fs, b[bw->bpind], &bi2);
+		b = get_blkmap(fs, iblk[bw->bpdir], &bmi1);
+		b = get_blkmap(fs, b[bw->bpind], &bmi2);
 		if(extend) // allocate single indirect block
 			b[bw->bpdind] = alloc_blk(fs,nod);
 		if(reduce) // free indirect block
 			free_blk(fs, b[bw->bpind]);
-		b = (uint32*)get_blk(fs, b[bw->bpdind], &bi3);
+		b = get_blkmap(fs, b[bw->bpdind], &bmi3);
 		bkref = &b[bw->bptind];
 		if(extend) // allocate first data block
 			*bkref = hole ? 0 : alloc_blk(fs,nod);
@@ -1225,17 +1255,17 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 		bw->bpdind = 0;
 		bw->bptind = 0;
 		bw->bpind++;
-		b = (uint32*)get_blk(fs, iblk[bw->bpdir], &bi1);
+		b = get_blkmap(fs, iblk[bw->bpdir], &bmi1);
 		if(extend) // allocate double indirect block
 			b[bw->bpind] = alloc_blk(fs,nod);
 		if(reduce) // free double indirect block
 			free_blk(fs, b[bw->bpind]);
-		b = (uint32*)get_blk(fs, b[bw->bpind], &bi2);
+		b = get_blkmap(fs, b[bw->bpind], &bmi2);
 		if(extend) // allocate single indirect block
 			b[bw->bpdind] = alloc_blk(fs,nod);
 		if(reduce) // free indirect block
 			free_blk(fs, b[bw->bpind]);
-		b = (uint32*)get_blk(fs, b[bw->bpdind], &bi3);
+		b = get_blkmap(fs, b[bw->bpdind], &bmi3);
 		bkref = &b[bw->bptind];
 		if(extend) // allocate first block
 			*bkref = hole ? 0 : alloc_blk(fs,nod);
@@ -1247,12 +1277,12 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 	/* End change for walking triple indirection */
 
 	bk = *bkref;
-	if (bi3)
-		put_blk(bi3);
-	if (bi2)
-		put_blk(bi2);
-	if (bi1)
-		put_blk(bi1);
+	if (bmi3)
+		put_blkmap(bmi3);
+	if (bmi2)
+		put_blkmap(bmi2);
+	if (bmi1)
+		put_blkmap(bmi1);
 
 	if(bk)
 	{
-- 
1.7.4.1

