Skip to contentSkip to conversation

f2fs-tools: avoid mounting f2fs if tools already open the device

PATCH In review Sheng Yong 3 participants 4 messages Jul 4, 2018 – Jul 10, 2018 Library code

lore
Share & tools
lore

Sheng Yong

author
#0 lore raw

PATCH 1/1 f2fs-tools: avoid mounting f2fs if tools already open the device

If the block device is opened by tools, F2FS should not be mounted. Especially when fsck is running, errors unexpected may happen. So if tools open a block device, we give it the O_EXCL flag to make sure the block device is opened exclusivly.

---
 lib/libf2fs.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
lib/libf2fs.c +17 -10
index b25fbf2..5625cff 100644
@@ -783,21 +783,35 @@ int get_device_info(int i)
 #endif
 	struct device_info *dev = c.devices + i;
 
+	stat_buf = malloc(sizeof(struct stat));
+	ASSERT(stat_buf);
+	if (stat(dev->path, stat_buf) < 0 ) {
+		MSG(0, "\tError: Failed to get the device stat!\n");
+		free(stat_buf);
+		return -1;
+	}
+
 	if (c.sparse_mode) {
-		fd = open((char *)dev->path, O_RDWR | O_CREAT | O_BINARY, 0644);
+		fd = open(dev->path, O_RDWR | O_CREAT | O_BINARY, 0644);
 	} else {
-		fd = open((char *)dev->path, O_RDWR);
+		if (S_ISBLK(stat_buf->st_mode))
+			fd = open(dev->path, O_RDWR | O_EXCL);
+		else
+			fd = open(dev->path, O_RDWR);
 	}
 	if (fd < 0) {
 		MSG(0, "\tError: Failed to open the device!\n");
+		free(stat_buf);
 		return -1;
 	}
 
 	dev->fd = fd;
 
 	if (c.sparse_mode) {
-		if (f2fs_init_sparse_file())
+		if (f2fs_init_sparse_file()) {
+			free(stat_buf);
 			return -1;
+		}
 	}
 
 	if (c.kd == -1) {
@@ -810,13 +824,6 @@ int get_device_info(int i)
 		}
 	}
 
-	stat_buf = malloc(sizeof(struct stat));
-	if (fstat(fd, stat_buf) < 0 ) {
-		MSG(0, "\tError: Failed to get the device stat!\n");
-		free(stat_buf);
-		return -1;
-	}
-
 	if (c.sparse_mode) {
 		dev->total_sectors = c.device_size / dev->sector_size;
 	} else if (S_ISREG(stat_buf->st_mode)) {
Signature · 6 lines
2.17.1


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
#1 lore raw
Quoted 4 lines · Sheng Yong

If the block device is opened by tools, F2FS should not be mounted. Especially when fsck is running, errors unexpected may happen. So if tools open a block device, we give it the O_EXCL flag to make sure the block device is opened exclusivly.

Should we treat block inode and regular inode as the same?

Not sure about this, since for debuging, if we can't umount due to panic in f2fs, still we can dump last data in device.

Thanks,

Quoted 54 lines
---
 lib/libf2fs.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
lib/libf2fs.c +17 -3
index b25fbf2..5625cff 100644
@@ -783,21 +783,35 @@ int get_device_info(int i)
 #endif
 	struct device_info *dev = c.devices + i;
 
+	stat_buf = malloc(sizeof(struct stat));
+	ASSERT(stat_buf);
+	if (stat(dev->path, stat_buf) < 0 ) {
+		MSG(0, "\tError: Failed to get the device stat!\n");
+		free(stat_buf);
+		return -1;
+	}
+
 	if (c.sparse_mode) {
-		fd = open((char *)dev->path, O_RDWR | O_CREAT | O_BINARY, 0644);
+		fd = open(dev->path, O_RDWR | O_CREAT | O_BINARY, 0644);
 	} else {
-		fd = open((char *)dev->path, O_RDWR);
+		if (S_ISBLK(stat_buf->st_mode))
+			fd = open(dev->path, O_RDWR | O_EXCL);
+		else
+			fd = open(dev->path, O_RDWR);
 	}
 	if (fd < 0) {
 		MSG(0, "\tError: Failed to open the device!\n");
+		free(stat_buf);
 		return -1;
 	}
 
 	dev->fd = fd;
 
 	if (c.sparse_mode) {
-		if (f2fs_init_sparse_file())
+		if (f2fs_init_sparse_file()) {
+			free(stat_buf);
 			return -1;
+		}
 	}
 
 	if (c.kd == -1) {
@@ -810,13 +824,6 @@ int get_device_info(int i)
 		}
 	}
 

lib/libf2fs.c int get_device_info(int i) · line 829

Quoted 10 lines
lib/libf2fs.c +0 -7
-	stat_buf = malloc(sizeof(struct stat));
-	if (fstat(fd, stat_buf) < 0 ) {
-		MSG(0, "\tError: Failed to get the device stat!\n");
-		free(stat_buf);
-		return -1;
-	}
-
 	if (c.sparse_mode) {
 		dev->total_sectors = c.device_size / dev->sector_size;
 	} else if (S_ISREG(stat_buf->st_mode)) {

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

author
#2 lore raw

Hi, Chao,

Quoted 7 lines · Chao Yu
Quoted 4 lines · Sheng Yong

If the block device is opened by tools, F2FS should not be mounted. Especially when fsck is running, errors unexpected may happen. So if tools open a block device, we give it the O_EXCL flag to make sure the block device is opened exclusivly.

Should we treat block inode and regular inode as the same?

I'm afraid not, the behavior of O_EXCL is not defined for regular inode if it is used without O_CREAT. If f2fs is installed in a image file (not a block device), we may still find a another way to avoid fsck and f2fs run concurrently :(

thanks

Quoted 59 lines

Not sure about this, since for debuging, if we can't umount due to panic in f2fs, still we can dump last data in device.

Thanks,

Quoted 53 lines
---
  lib/libf2fs.c | 27 +++++++++++++++++----------
  1 file changed, 17 insertions(+), 10 deletions(-)
lib/libf2fs.c +17 -3
index b25fbf2..5625cff 100644
@@ -783,21 +783,35 @@ int get_device_info(int i)
  #endif
  	struct device_info *dev = c.devices + i;
 
+	stat_buf = malloc(sizeof(struct stat));
+	ASSERT(stat_buf);
+	if (stat(dev->path, stat_buf) < 0 ) {
+		MSG(0, "\tError: Failed to get the device stat!\n");
+		free(stat_buf);
+		return -1;
+	}
+
  	if (c.sparse_mode) {
-		fd = open((char *)dev->path, O_RDWR | O_CREAT | O_BINARY, 0644);
+		fd = open(dev->path, O_RDWR | O_CREAT | O_BINARY, 0644);
  	} else {
-		fd = open((char *)dev->path, O_RDWR);
+		if (S_ISBLK(stat_buf->st_mode))
+			fd = open(dev->path, O_RDWR | O_EXCL);
+		else
+			fd = open(dev->path, O_RDWR);
  	}
  	if (fd < 0) {
  		MSG(0, "\tError: Failed to open the device!\n");
+		free(stat_buf);
  		return -1;
  	}
 
  	dev->fd = fd;
 
  	if (c.sparse_mode) {
-		if (f2fs_init_sparse_file())
+		if (f2fs_init_sparse_file()) {
+			free(stat_buf);
  			return -1;
+		}
  	}
 
  	if (c.kd == -1) {
@@ -810,13 +824,6 @@ int get_device_info(int i)
  		}
  	}
 
Quoted 14 lines

lib/libf2fs.c int get_device_info(int i) · line 829

Quoted 11 lines
lib/libf2fs.c +0 -7
-	stat_buf = malloc(sizeof(struct stat));
-	if (fstat(fd, stat_buf) < 0 ) {
-		MSG(0, "\tError: Failed to get the device stat!\n");
-		free(stat_buf);
-		return -1;
-	}
-
  	if (c.sparse_mode) {
  		dev->total_sectors = c.device_size / dev->sector_size;
  	} else if (S_ISREG(stat_buf->st_mode)) {

.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

Reviewed-by
#3 lore raw

Hi Sheng,

Quoted 9 lines · Sheng Yong

Hi, Chao,

Quoted 6 lines · Chao Yu
Quoted 4 lines · Sheng Yong

If the block device is opened by tools, F2FS should not be mounted. Especially when fsck is running, errors unexpected may happen. So if tools open a block device, we give it the O_EXCL flag to make sure the block device is opened exclusivly.

Quoted 6 lines
Quoted 1 line

Should we treat block inode and regular inode as the same?

I'm afraid not, the behavior of O_EXCL is not defined for regular inode if it is used without O_CREAT. If f2fs is installed in a image file (not a block device), we may still find a another way to avoid fsck and f2fs run concurrently :(

Correct, I misunderstand semantics of this flag.

Quoted 10 lines

thanks

Quoted 8 lines

Not sure about this, since for debuging, if we can't umount due to panic in f2fs, still we can dump last data in device.

Thanks,

Quoted 2 lines

Thanks,

Quoted 67 lines
Quoted 51 lines
Quoted 51 lines
---
  lib/libf2fs.c | 27 +++++++++++++++++----------
  1 file changed, 17 insertions(+), 10 deletions(-)
lib/libf2fs.c +17 -3
index b25fbf2..5625cff 100644
@@ -783,21 +783,35 @@ int get_device_info(int i)
  #endif
  	struct device_info *dev = c.devices + i;
 
+	stat_buf = malloc(sizeof(struct stat));
+	ASSERT(stat_buf);
+	if (stat(dev->path, stat_buf) < 0 ) {
+		MSG(0, "\tError: Failed to get the device stat!\n");
+		free(stat_buf);
+		return -1;
+	}
+
  	if (c.sparse_mode) {
-		fd = open((char *)dev->path, O_RDWR | O_CREAT | O_BINARY, 0644);
+		fd = open(dev->path, O_RDWR | O_CREAT | O_BINARY, 0644);
  	} else {
-		fd = open((char *)dev->path, O_RDWR);
+		if (S_ISBLK(stat_buf->st_mode))
+			fd = open(dev->path, O_RDWR | O_EXCL);
+		else
+			fd = open(dev->path, O_RDWR);
  	}
  	if (fd < 0) {
  		MSG(0, "\tError: Failed to open the device!\n");
+		free(stat_buf);
  		return -1;
  	}
 
  	dev->fd = fd;
 
  	if (c.sparse_mode) {
-		if (f2fs_init_sparse_file())
+		if (f2fs_init_sparse_file()) {
+			free(stat_buf);
  			return -1;
+		}
  	}
 
  	if (c.kd == -1) {
@@ -810,13 +824,6 @@ int get_device_info(int i)
  		}
  	}
 
Quoted 14 lines

lib/libf2fs.c int get_device_info(int i) · line 829

Quoted 11 lines
lib/libf2fs.c +0 -7
-	stat_buf = malloc(sizeof(struct stat));
-	if (fstat(fd, stat_buf) < 0 ) {
-		MSG(0, "\tError: Failed to get the device stat!\n");
-		free(stat_buf);
-		return -1;
-	}
-
  	if (c.sparse_mode) {
  		dev->total_sectors = c.device_size / dev->sector_size;
  	} else if (S_ISREG(stat_buf->st_mode)) {

.

Quoted 2 lines

.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot