shithub: lwext4

Download patch

ref: b768b83c4ebe8e3320cbc677c45c2ddbcde24e70
parent: 26c97621007a91216a3067148982a416beed712e
author: ngkaho1234 <[email protected]>
date: Mon Oct 19 09:35:05 EDT 2015

Simplify the code logic of ext4_fread & ext4_fwrite.

--- a/lwext4/ext4.c
+++ b/lwext4/ext4.c
@@ -1256,10 +1256,8 @@
 {
 	uint32_t u;
 	ext4_fsblk_t fblock;
-	ext4_fsblk_t fblock_start;
 	uint32_t fblock_cnt;
 	uint32_t sblock;
-	uint32_t sblock_end;
 	uint32_t block_size;
 	uint8_t *u8_buf = buf;
 	int r;
@@ -1291,7 +1289,6 @@
 	block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
 	size = size > (f->fsize - f->fpos) ? (f->fsize - f->fpos) : size;
 	sblock = (f->fpos) / block_size;
-	sblock_end = (f->fpos + size) / block_size;
 	u = (f->fpos) % block_size;
 
 	/*If the size of symlink is smaller than 60 bytes*/
@@ -1352,28 +1349,18 @@
 		sblock++;
 	}
 
-	fblock_start = 0;
 	fblock_cnt = 0;
 	while (size >= block_size) {
-		while (sblock < sblock_end) {
-			r = ext4_fs_get_inode_data_block_index(&ref, sblock,
-							       &fblock);
-			if (r != EOK)
-				goto Finish;
 
-			sblock++;
+		r = ext4_fs_get_inode_data_block_index(&ref, sblock,
+						       &fblock);
+		if (r != EOK)
+			goto Finish;
 
-			if (!fblock_start) {
-				fblock_start = fblock;
-			}
+		sblock++;
+		fblock_cnt = 1;
 
-			if ((fblock_start + fblock_cnt) != fblock)
-				break;
-
-			fblock_cnt++;
-		}
-
-		r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
+		r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock,
 					   fblock_cnt);
 		if (r != EOK)
 			goto Finish;
@@ -1385,8 +1372,6 @@
 		if (rcnt)
 			*rcnt += block_size * fblock_cnt;
 
-		fblock_start = fblock;
-		fblock_cnt = 1;
 	}
 
 	if (size) {
@@ -1422,10 +1407,8 @@
 	ext4_fsblk_t fblock;
 
 	uint32_t sblock;
-	uint32_t sblock_end;
 	uint32_t file_blocks;
 	uint32_t block_size;
-	ext4_fsblk_t fblock_start;
 	uint32_t fblock_cnt;
 
 	struct ext4_block b;
@@ -1457,13 +1440,8 @@
 
 	block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
 
-	sblock_end = (f->fpos + size) / block_size;
+	file_blocks = (f->fsize + block_size - 1) / block_size;
 
-	file_blocks = (f->fsize / block_size);
-
-	if (f->fsize % block_size)
-		file_blocks++;
-
 	sblock = (f->fpos) / block_size;
 
 	u = (f->fpos) % block_size;
@@ -1501,36 +1479,25 @@
 	if (r != EOK)
 		goto Finish;
 
-	fblock_start = 0;
 	fblock_cnt = 0;
 	while (size >= block_size) {
 
-		while (sblock < sblock_end) {
-			if (sblock < file_blocks) {
-				r = ext4_fs_init_inode_data_block_index(
-				    &ref, sblock, &fblock);
-				if (r != EOK)
-					break;
-			} else {
-				r = ext4_fs_append_inode_block(&ref, &fblock,
-							       &sblock);
-				if (r != EOK)
-					break;
-			}
-
-			sblock++;
-
-			if (!fblock_start) {
-				fblock_start = fblock;
-			}
-
-			if ((fblock_start + fblock_cnt) != fblock)
+		if (sblock < file_blocks) {
+			r = ext4_fs_init_inode_data_block_index(
+			    &ref, sblock, &fblock);
+			if (r != EOK)
 				break;
-
-			fblock_cnt++;
+		} else {
+			r = ext4_fs_append_inode_block(&ref, &fblock,
+						       &sblock);
+			if (r != EOK)
+				break;
 		}
 
-		r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
+		sblock++;
+		fblock_cnt = 1;
+
+		r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock,
 					   fblock_cnt);
 		if (r != EOK)
 			break;
@@ -1542,8 +1509,6 @@
 		if (wcnt)
 			*wcnt += block_size * fblock_cnt;
 
-		fblock_start = fblock;
-		fblock_cnt = 1;
 	}
 
 	/*Stop write back cache mode*/