shithub: lwext4

Download patch

ref: 9b38aef3133341e68a40756bd9fba1b81edb203b
parent: 6a77e61636740a3263aa579c29a140d29b5bf254
author: Grzegorz Kostka <kostka.grzegorz@gmail.com>
date: Wed May 31 11:12:07 EDT 2017

ext4: fix transaction start/stop scopes

Remove transaction scope from ext4_generic_open2 method.

--- a/src/ext4.c
+++ b/src/ext4.c
@@ -966,9 +966,6 @@
 	if (parent_inode)
 		*parent_inode = ref.index;
 
-	if (flags & O_CREAT)
-		ext4_trans_start(mp);
-
 	len = ext4_path_check(path, &is_goal);
 	while (1) {
 
@@ -1070,7 +1067,7 @@
 
 		if (name_off)
 			*name_off += len + 1;
-	};
+	}
 
 	if (r != EOK) {
 		ext4_fs_put_inode_ref(&ref);
@@ -1094,19 +1091,9 @@
 
 		if (f->flags & O_APPEND)
 			f->fpos = f->fsize;
-
 	}
 
-	r = ext4_fs_put_inode_ref(&ref);
-	if (flags & O_CREAT) {
-		if (r == EOK)
-			ext4_trans_stop(mp);
-		else
-			ext4_trans_abort(mp);
-
-	}
-
-	return r;
+	return ext4_fs_put_inode_ref(&ref);
 }
 
 /****************************************************************************/
@@ -1117,6 +1104,9 @@
 {
 	uint32_t iflags;
 	int filetype;
+	int r;
+	struct ext4_mountpoint *mp = ext4_get_mount(path);
+
 	if (ext4_parse_flags(flags, &iflags) == false)
 		return EINVAL;
 
@@ -1125,8 +1115,20 @@
 	else
 		filetype = EXT4_DE_DIR;
 
-	return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
-				  name_off);
+	if (iflags & O_CREAT)
+		ext4_trans_start(mp);
+
+	r = ext4_generic_open2(f, path, iflags, filetype, parent_inode,
+				name_off);
+
+	if (iflags & O_CREAT) {
+		if (r == EOK)
+			ext4_trans_stop(mp);
+		else
+			ext4_trans_abort(mp);
+	}
+
+	return r;
 }
 
 static int ext4_create_hardlink(const char *path,
@@ -1285,15 +1287,16 @@
 		return EINVAL;
 
 	EXT4_MP_LOCK(mp);
-	ext4_trans_start(mp);
-
 	r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
 			       &parent_inode, &name_off);
-	if (r != EOK)
-		goto Finish;
+	if (r != EOK) {
+		EXT4_MP_UNLOCK(mp);
+		return r;
+	}
 
 	child_inode = f.inode;
 	ext4_fclose(&f);
+	ext4_trans_start(mp);
 
 	/*We have file to unlink. Load it.*/
 	r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
@@ -1342,15 +1345,17 @@
 		return EROFS;
 
 	EXT4_MP_LOCK(mp);
-	ext4_trans_start(mp);
 
 	r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
 				&parent_inode, &name_off);
-	if (r != EOK)
-		goto Finish;
+	if (r != EOK) {
+		EXT4_MP_UNLOCK(mp);
+		return r;
+	}
 
 	child_inode = f.inode;
 	ext4_fclose(&f);
+	ext4_trans_start(mp);
 
 	/*Load parent*/
 	r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
@@ -1436,6 +1441,7 @@
 {
 	ext4_file f;
 	uint32_t parent_inode;
+	uint32_t child_inode;
 	uint32_t name_off;
 	bool is_goal;
 	int r;
@@ -1451,16 +1457,17 @@
 		return EROFS;
 
 	EXT4_MP_LOCK(mp);
-	ext4_trans_start(mp);
-
-	r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN,
+	r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
 			       &parent_inode, &name_off);
 	if (r != EOK) {
-		ext4_trans_abort(mp);
 		EXT4_MP_UNLOCK(mp);
 		return r;
 	}
 
+	child_inode = f.inode;
+	ext4_fclose(&f);
+	ext4_trans_start(mp);
+
 	/*Load parent*/
 	r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
 	if (r != EOK) {
@@ -1470,7 +1477,7 @@
 	}
 
 	/*We have file to delete. Load it.*/
-	r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
+	r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child);
 	if (r != EOK) {
 		ext4_fs_put_inode_ref(&parent);
 		ext4_trans_abort(mp);
@@ -1563,12 +1570,23 @@
         filetype = EXT4_DE_REG_FILE;
 
 	EXT4_MP_LOCK(mp);
-
 	ext4_block_cache_write_back(mp->fs.bdev, 1);
+
+	if (flags & O_CREAT)
+		ext4_trans_start(mp);
+
 	r = ext4_generic_open2(file, path, flags, filetype, NULL, NULL);
-	ext4_block_cache_write_back(mp->fs.bdev, 0);
 
+	if (flags & O_CREAT) {
+		if (r == EOK)
+			ext4_trans_stop(mp);
+		else
+			ext4_trans_abort(mp);
+	}
+
+	ext4_block_cache_write_back(mp->fs.bdev, 0);
 	EXT4_MP_UNLOCK(mp);
+
 	return r;
 }
 
@@ -2043,13 +2061,12 @@
 	int r;
 	ext4_file f;
 
-	ext4_trans_start(mp);
-	r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
-	if (r != EOK) {
-		ext4_trans_abort(mp);
+	r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
+	if (r != EOK)
 		return r;
-	}
 
+	ext4_trans_start(mp);
+
 	r = ext4_fs_get_inode_ref(&mp->fs, f.inode, inode_ref);
 	if (r != EOK) {
 		ext4_trans_abort(mp);
@@ -2502,10 +2519,10 @@
 	filetype = EXT4_DE_SYMLINK;
 
 	EXT4_MP_LOCK(mp);
+	ext4_block_cache_write_back(mp->fs.bdev, 1);
 	ext4_trans_start(mp);
 
-	ext4_block_cache_write_back(mp->fs.bdev, 1);
-	r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
+	r = ext4_generic_open2(&f, path, O_RDWR | O_CREAT, filetype, NULL, NULL);
 	if (r == EOK)
 		r = ext4_fsymlink_set(&f, target, strlen(target));
 	else
@@ -2514,13 +2531,12 @@
 	ext4_fclose(&f);
 
 Finish:
-	ext4_block_cache_write_back(mp->fs.bdev, 0);
-
 	if (r != EOK)
 		ext4_trans_abort(mp);
 	else
 		ext4_trans_stop(mp);
 
+	ext4_block_cache_write_back(mp->fs.bdev, 0);
 	EXT4_MP_UNLOCK(mp);
 	return r;
 }
@@ -2552,11 +2568,6 @@
 
 Finish:
 	ext4_block_cache_write_back(mp->fs.bdev, 0);
-	if (r != EOK)
-		ext4_trans_abort(mp);
-	else
-		ext4_trans_stop(mp);
-
 	EXT4_MP_UNLOCK(mp);
 	return r;
 }
@@ -2616,27 +2627,27 @@
 		return EINVAL;
 
 	EXT4_MP_LOCK(mp);
+	ext4_block_cache_write_back(mp->fs.bdev, 1);
 	ext4_trans_start(mp);
 
-	ext4_block_cache_write_back(mp->fs.bdev, 1);
-	r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
+	r = ext4_generic_open2(&f, path, O_RDWR | O_CREAT, filetype, NULL, NULL);
 	if (r == EOK) {
 		if (filetype == EXT4_DE_CHRDEV ||
 		    filetype == EXT4_DE_BLKDEV)
 			r = ext4_mknod_set(&f, dev);
-	} else
+	} else {
 		goto Finish;
+	}
 
 	ext4_fclose(&f);
 
 Finish:
-	ext4_block_cache_write_back(mp->fs.bdev, 0);
-
 	if (r != EOK)
 		ext4_trans_abort(mp);
 	else
 		ext4_trans_stop(mp);
 
+	ext4_block_cache_write_back(mp->fs.bdev, 0);
 	EXT4_MP_UNLOCK(mp);
 	return r;
 }
@@ -2666,13 +2677,15 @@
 		return EINVAL;
 
 	EXT4_MP_LOCK(mp);
-	ext4_trans_start(mp);
+	r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
+	if (r != EOK) {
+		EXT4_MP_UNLOCK(mp);
+		return r;
+	}
 
-	r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
-	if (r != EOK)
-		goto Finish;
 	inode = f.inode;
 	ext4_fclose(&f);
+	ext4_trans_start(mp);
 
 	r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
 	if (r != EOK)
@@ -2714,9 +2727,10 @@
 		return EINVAL;
 
 	EXT4_MP_LOCK(mp);
-	r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
+	r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
 	if (r != EOK)
 		goto Finish;
+
 	inode = f.inode;
 	ext4_fclose(&f);
 
@@ -2747,7 +2761,7 @@
 		return ENOENT;
 
 	EXT4_MP_LOCK(mp);
-	r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
+	r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
 	if (r != EOK)
 		goto Finish;
 	inode = f.inode;
@@ -2836,13 +2850,15 @@
 		return EINVAL;
 
 	EXT4_MP_LOCK(mp);
-	ext4_trans_start(mp);
+	r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
+	if (r != EOK) {
+		EXT4_MP_LOCK(mp);
+		return r;
+	}
 
-	r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
-	if (r != EOK)
-		goto Finish;
 	inode = f.inode;
 	ext4_fclose(&f);
+	ext4_trans_start(mp);
 
 	r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
 	if (r != EOK)
@@ -2898,7 +2914,6 @@
 	/*Check if exist.*/
 	r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
 	if (r != EOK) {
-		ext4_trans_abort(mp);
 		EXT4_MP_UNLOCK(mp);
 		return r;
 	}
@@ -2905,7 +2920,6 @@
 
 	path += name_off;
 	len = ext4_path_check(path, &is_goal);
-
 	inode_current = f.inode;
 
 	ext4_block_cache_write_back(mp->fs.bdev, 1);
@@ -3118,7 +3132,6 @@
 {
 	int r;
 	ext4_file f;
-
 	struct ext4_mountpoint *mp = ext4_get_mount(path);
 
 	if (!mp)
@@ -3128,26 +3141,16 @@
 		return EROFS;
 
 	EXT4_MP_LOCK(mp);
-	ext4_trans_start(mp);
 
 	/*Check if exist.*/
 	r = ext4_generic_open(&f, path, "r", false, 0, 0);
-	if (r == EOK) {
-		/*Directory already created*/
-		ext4_trans_stop(mp);
-		EXT4_MP_UNLOCK(mp);
-		return r;
-	}
+	if (r == EOK)
+		goto Finish;
 
-	/*Create new dir*/
+	/*Create new directory.*/
 	r = ext4_generic_open(&f, path, "w", false, 0, 0);
-	if (r != EOK) {
-		ext4_trans_abort(mp);
-		EXT4_MP_UNLOCK(mp);
-		return r;
-	}
 
-	ext4_trans_stop(mp);
+Finish:
 	EXT4_MP_UNLOCK(mp);
 	return r;
 }